Moonshark/core/workers/config.go

66 lines
1.7 KiB
Go

package workers
import (
luajit "git.sharkk.net/Sky/LuaJIT-to-Go"
)
// InitFunc is a function that initializes a Lua state
type InitFunc func(*luajit.State) error
// WorkerConfig contains configuration for worker initialization
type WorkerConfig struct {
// Functions maps Lua global names to Go functions
Functions map[string]luajit.GoFunction
// Modules maps module names to Lua code strings
Modules map[string]string
// ModulePaths maps module names to file paths
ModulePaths map[string]string
// PackagePath custom package.path to set (optional)
PackagePath string
// CustomInit allows for complex custom initialization
CustomInit InitFunc
}
// NewWorkerConfig creates a new worker configuration
func NewWorkerConfig() *WorkerConfig {
return &WorkerConfig{
Functions: make(map[string]luajit.GoFunction),
Modules: make(map[string]string),
ModulePaths: make(map[string]string),
}
}
// AddFunction registers a Go function to be available in Lua
func (c *WorkerConfig) AddFunction(name string, fn luajit.GoFunction) *WorkerConfig {
c.Functions[name] = fn
return c
}
// AddModule adds a Lua module from a string
func (c *WorkerConfig) AddModule(name, code string) *WorkerConfig {
c.Modules[name] = code
return c
}
// AddModuleFile adds a Lua module from a file
func (c *WorkerConfig) AddModuleFile(name, path string) *WorkerConfig {
c.ModulePaths[name] = path
return c
}
// SetPackagePath sets a custom package.path
func (c *WorkerConfig) SetPackagePath(path string) *WorkerConfig {
c.PackagePath = path
return c
}
// SetCustomInit sets a custom initialization function
func (c *WorkerConfig) SetCustomInit(fn InitFunc) *WorkerConfig {
c.CustomInit = fn
return c
}