lua

package
v0.12.19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 31, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInstanceNotFound = errors.New("scripting: instance not found")
	ErrInstanceBusy     = errors.New("scripting: instance is locked")
	ErrInstancePaused   = errors.New("scripting: instance is paused")
	ErrInstanceKilled   = errors.New("scripting: instance is killed")
)

Manager level errors to aid callers when coordinating VM access.

Functions

This section is empty.

Types

type Instance

type Instance struct {
	// contains filtered or unexported fields
}

Instance stores metadata and synchronization primitives for a compiled Lua VM.

func (*Instance) Do added in v0.12.8

func (inst *Instance) Do(m *Manager, id uuid.UUID, action VMAction) error

Do executes the requested VM action for this instance.

func (*Instance) IsBusy added in v0.12.8

func (inst *Instance) IsBusy() bool

IsBusy reports whether the instance lock is currently held.

func (*Instance) RegisterFunction added in v0.12.8

func (inst *Instance) RegisterFunction(name string, fn lua.LGFunction)

RegisterFunction attaches an instance-scoped helper callable from Lua.

func (*Instance) RegisterFunctions added in v0.12.18

func (inst *Instance) RegisterFunctions(functions map[string]lua.LGFunction)

RegisterFunctions attaches instance-scoped helpers callable from Lua.

func (*Instance) RegisterGlobal added in v0.12.8

func (inst *Instance) RegisterGlobal(name string, value lua.LValue)

RegisterGlobal exposes an instance-scoped global Lua value.

func (*Instance) RegisterGlobals added in v0.12.18

func (inst *Instance) RegisterGlobals(globals map[string]lua.LValue)

RegisterGlobals exposes instance-scoped global Lua values.

func (*Instance) RegisterModule added in v0.12.18

func (inst *Instance) RegisterModule(name string, provider ModuleProvider)

RegisterModule applies an instance-scoped provider for a single module.

func (*Instance) RegisterModules added in v0.12.18

func (inst *Instance) RegisterModules(providers map[string]ModuleProvider)

RegisterModules applies instance-scoped module provider overrides/extensions.

type InstanceInfo added in v0.12.4

type InstanceInfo struct {
	Mode       VMMode
	Persistent bool
	Paused     bool
	Killed     bool
	Locked     bool
}

InstanceInfo reports metadata about a tracked VM instance.

type LuaInstanceModuleProvider added in v0.12.18

type LuaInstanceModuleProvider struct {
	InstanceID     uuid.UUID
	ExportFunction string
	InvokeFunction string
	// contains filtered or unexported fields
}

LuaInstanceModuleProvider resolves modules through another managed Lua VM. The provider VM must be persistent and expose export/invoke globals.

func NewLuaInstanceModuleProvider added in v0.12.18

func NewLuaInstanceModuleProvider(instanceID uuid.UUID, exportFunction, invokeFunction string) *LuaInstanceModuleProvider

NewLuaInstanceModuleProvider builds a provider backed by another VM instance.

func (*LuaInstanceModuleProvider) ProvideModule added in v0.12.18

func (p *LuaInstanceModuleProvider) ProvideModule(ctx *ModuleContext) (lua.LValue, error)

ProvideModule resolves a module by invoking export logic inside the provider VM.

type LuaSymbol added in v0.12.18

type LuaSymbol struct {
	// Value exports a literal Lua value as module[field].
	Value lua.LValue
	// Function exports a Go callback as module[field] when non-nil.
	Function lua.LGFunction
}

LuaSymbol describes a field exported from a Go-defined Lua module table.

type Manager

type Manager struct {
	// contains filtered or unexported fields
}

Manager coordinates multiple sandboxed Lua states.

func NewManager

func NewManager(logger *log.Loggee) *Manager

NewManager creates a manager with an optional logger. When logger is nil, scripting logs are dropped.

func (*Manager) Close

func (m *Manager) Close()

Close stops all persistent instances managed by this manager.

func (*Manager) Do added in v0.12.4

func (m *Manager) Do(id uuid.UUID, action VMAction) error

Do performs an action against a persistent VM identified by id.

func (*Manager) GetInstance added in v0.12.6

func (m *Manager) GetInstance(id uuid.UUID) (*Instance, error)

GetInstance retrieves a tracked VM instance by ID.

func (*Manager) InstanceInfo added in v0.12.4

func (m *Manager) InstanceInfo(id uuid.UUID) (InstanceInfo, error)

InstanceInfo returns metadata for the VM identified by id.

func (*Manager) PrepareModuleProviderVM added in v0.12.18

func (m *Manager) PrepareModuleProviderVM(config ModuleProviderVMConfig) (uuid.UUID, *Instance, error)

PrepareModuleProviderVM prepares a persistent VM meant to back module providers.

func (*Manager) PrepareVM added in v0.12.8

func (m *Manager) PrepareVM(mode VMMode, persistent bool, code string) (uuid.UUID, *Instance, error)

PrepareVM compiles the provided script and stores it for future execution. Persistent VMs remain tracked and can be controlled via Do; volatile ones run once after calling Run. Every VM receives a UUIDv7 identifier.

func (*Manager) RegisterFunction

func (m *Manager) RegisterFunction(name string, fn lua.LGFunction)

RegisterFunction exposes a Go function to all future Lua states managed by this manager.

func (*Manager) RegisterFunctions added in v0.12.18

func (m *Manager) RegisterFunctions(functions map[string]lua.LGFunction)

RegisterFunctions exposes Go functions to all future Lua states managed by this manager.

func (*Manager) RegisterGlobal added in v0.12.4

func (m *Manager) RegisterGlobal(name string, value lua.LValue)

RegisterGlobal exposes a Go value to all future Lua states managed by this manager.

func (*Manager) RegisterGlobals added in v0.12.18

func (m *Manager) RegisterGlobals(globals map[string]lua.LValue)

RegisterGlobals exposes Go values to all future Lua states managed by this manager.

func (*Manager) RegisterInstanceFunction added in v0.12.6

func (m *Manager) RegisterInstanceFunction(id uuid.UUID, name string, fn lua.LGFunction)

RegisterInstanceFunction attaches a helper to a specific VM.

func (*Manager) RegisterInstanceFunctions added in v0.12.18

func (m *Manager) RegisterInstanceFunctions(id uuid.UUID, functions map[string]lua.LGFunction)

RegisterInstanceFunctions attaches multiple helpers to a specific VM.

func (*Manager) RegisterInstanceGlobal added in v0.12.6

func (m *Manager) RegisterInstanceGlobal(id uuid.UUID, name string, value lua.LValue)

RegisterInstanceGlobal provides an instance-scoped global Lua value.

func (*Manager) RegisterInstanceGlobals added in v0.12.18

func (m *Manager) RegisterInstanceGlobals(id uuid.UUID, globals map[string]lua.LValue)

RegisterInstanceGlobals provides instance-scoped global Lua values.

func (*Manager) RegisterInstanceModule added in v0.12.18

func (m *Manager) RegisterInstanceModule(id uuid.UUID, name string, provider ModuleProvider)

RegisterInstanceModule registers a VM-specific provider for a single module.

func (*Manager) RegisterInstanceModules added in v0.12.18

func (m *Manager) RegisterInstanceModules(id uuid.UUID, providers map[string]ModuleProvider)

RegisterInstanceModules applies VM-specific module provider overrides/extensions.

func (*Manager) RegisterModule added in v0.12.18

func (m *Manager) RegisterModule(name string, provider ModuleProvider)

RegisterModule registers a manager-wide provider for a single module.

func (*Manager) RegisterModules added in v0.12.18

func (m *Manager) RegisterModules(providers map[string]ModuleProvider)

RegisterModules registers manager-wide module providers using module-name keys. Existing VMs are not backfilled; only newly prepared instances receive these bindings.

func (*Manager) SetDefaultSender

func (m *Manager) SetDefaultSender(sender string)

SetDefaultSender overrides the sender name used when logging from scripts.

type ModuleContext added in v0.12.18

type ModuleContext struct {
	Manager    *Manager
	Identifier string
	Mode       VMMode
	State      *lua.LState
	Module     string
}

ModuleContext carries metadata for resolving a single require("module") request.

type ModuleProvider added in v0.12.18

type ModuleProvider interface {
	ProvideModule(ctx *ModuleContext) (lua.LValue, error)
}

ModuleProvider resolves a single module binding into a Lua value.

func GoModule added in v0.12.18

func GoModule(symbols map[string]LuaSymbol) ModuleProvider

GoModule returns a ModuleProvider that materializes a table from Go symbols.

type ModuleProviderFunc added in v0.12.18

type ModuleProviderFunc func(ctx *ModuleContext) (lua.LValue, error)

ModuleProviderFunc adapts a function into a ModuleProvider.

func (ModuleProviderFunc) ProvideModule added in v0.12.18

func (f ModuleProviderFunc) ProvideModule(ctx *ModuleContext) (lua.LValue, error)

ProvideModule satisfies ModuleProvider.

type ModuleProviderVMConfig added in v0.12.18

type ModuleProviderVMConfig struct {
	Mode      VMMode
	Code      string
	Functions map[string]lua.LGFunction
	Globals   map[string]lua.LValue
	Modules   map[string]ModuleProvider
}

ModuleProviderVMConfig configures a persistent Lua VM intended to serve modules.

type VMAction added in v0.12.4

type VMAction uint8

VMAction enumerates supported manager operations for persistent VMs.

const (
	// Run executes the compiled script, blocking until the VM lock is held.
	Run VMAction = iota
	// RunSafe only executes when TryLock succeeds and otherwise returns ErrInstanceBusy.
	RunSafe
	// Terminate waits for the current run to finish, then removes the VM cleanly.
	Terminate
	// Kill forcibly removes the VM immediately without waiting for the run to finish.
	Kill
)

func (VMAction) String added in v0.12.4

func (a VMAction) String() string

String converts the VM action to a readable representation.

type VMMode added in v0.12.2

type VMMode uint8
const (
	// Sandboxed removes risky loaders and keeps require() restricted to registered modules.
	Sandboxed VMMode = iota
	// Elevated exposes os/io/package helpers while still skipping arbitrary C loading.
	Elevated
	// Unsafe opens the full set of Lua libs for debugging/compatibility scenarios.
	Unsafe
)

func (VMMode) String added in v0.12.4

func (m VMMode) String() string

String converts the VM mode to a readable representation.

Source Files

  • instance.go
  • manager.go
  • modules.go

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL