111 lines
2.7 KiB
Go
111 lines
2.7 KiB
Go
package runner
|
|
|
|
import (
|
|
"strings"
|
|
"sync"
|
|
|
|
luajit "git.sharkk.net/Sky/LuaJIT-to-Go"
|
|
)
|
|
|
|
// CoreModuleRegistry manages the initialization and reloading of core modules
|
|
type CoreModuleRegistry struct {
|
|
modules map[string]StateInitFunc
|
|
mu sync.RWMutex
|
|
}
|
|
|
|
// NewCoreModuleRegistry creates a new core module registry
|
|
func NewCoreModuleRegistry() *CoreModuleRegistry {
|
|
return &CoreModuleRegistry{
|
|
modules: make(map[string]StateInitFunc),
|
|
}
|
|
}
|
|
|
|
// Register adds a module to the registry
|
|
func (r *CoreModuleRegistry) Register(name string, initFunc StateInitFunc) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
r.modules[name] = initFunc
|
|
}
|
|
|
|
// Initialize initializes all registered modules
|
|
func (r *CoreModuleRegistry) Initialize(state *luajit.State) error {
|
|
r.mu.RLock()
|
|
defer r.mu.RUnlock()
|
|
|
|
// Convert to StateInitFunc
|
|
initFunc := CombineInitFuncs(r.getInitFuncs()...)
|
|
return initFunc(state)
|
|
}
|
|
|
|
// getInitFuncs returns all module init functions
|
|
func (r *CoreModuleRegistry) getInitFuncs() []StateInitFunc {
|
|
funcs := make([]StateInitFunc, 0, len(r.modules))
|
|
for _, initFunc := range r.modules {
|
|
funcs = append(funcs, initFunc)
|
|
}
|
|
return funcs
|
|
}
|
|
|
|
// InitializeModule initializes a specific module
|
|
func (r *CoreModuleRegistry) InitializeModule(state *luajit.State, name string) error {
|
|
r.mu.RLock()
|
|
defer r.mu.RUnlock()
|
|
|
|
initFunc, ok := r.modules[name]
|
|
if !ok {
|
|
return nil // Module not found, no error
|
|
}
|
|
|
|
return initFunc(state)
|
|
}
|
|
|
|
// ModuleNames returns a list of all registered module names
|
|
func (r *CoreModuleRegistry) ModuleNames() []string {
|
|
r.mu.RLock()
|
|
defer r.mu.RUnlock()
|
|
|
|
names := make([]string, 0, len(r.modules))
|
|
for name := range r.modules {
|
|
names = append(names, name)
|
|
}
|
|
return names
|
|
}
|
|
|
|
// MatchModuleName checks if a file path corresponds to a registered module
|
|
func (r *CoreModuleRegistry) MatchModuleName(modName string) (string, bool) {
|
|
r.mu.RLock()
|
|
defer r.mu.RUnlock()
|
|
|
|
// Exact match
|
|
if _, ok := r.modules[modName]; ok {
|
|
return modName, true
|
|
}
|
|
|
|
// Check if the module name ends with a registered module
|
|
for name := range r.modules {
|
|
if strings.HasSuffix(modName, "."+name) {
|
|
return name, true
|
|
}
|
|
}
|
|
|
|
return "", false
|
|
}
|
|
|
|
// Global registry instance
|
|
var GlobalRegistry = NewCoreModuleRegistry()
|
|
|
|
// Initialize global registry with core modules
|
|
func init() {
|
|
GlobalRegistry.Register("http", HTTPModuleInitFunc())
|
|
GlobalRegistry.Register("cookie", CookieModuleInitFunc())
|
|
}
|
|
|
|
// RegisterCoreModule is a helper to register a core module
|
|
// with the global registry
|
|
func RegisterCoreModule(name string, initFunc StateInitFunc) {
|
|
GlobalRegistry.Register(name, initFunc)
|
|
}
|
|
|
|
// To add a new module, simply call:
|
|
// RegisterCoreModule("new_module_name", NewModuleInitFunc())
|