Documentation
¶
Index ¶
- func Register(typ Type, f FactoryFunc) error
- func Unregister(typ Type) bool
- type AutoGrowEnginePool
- func (p *AutoGrowEnginePool) Acquire() (Engine, error)
- func (p *AutoGrowEnginePool) CallFunction(ctx context.Context, name string, args ...any) (any, error)
- func (p *AutoGrowEnginePool) ClearError()
- func (p *AutoGrowEnginePool) Close() error
- func (p *AutoGrowEnginePool) Execute(ctx context.Context) (any, error)
- func (p *AutoGrowEnginePool) ExecuteFile(ctx context.Context, filePath string) (any, error)
- func (p *AutoGrowEnginePool) ExecuteString(ctx context.Context, source string) (any, error)
- func (p *AutoGrowEnginePool) GetGlobal(name string) (any, error)
- func (p *AutoGrowEnginePool) GetLastError() error
- func (p *AutoGrowEnginePool) LoadFile(ctx context.Context, filePath string) error
- func (p *AutoGrowEnginePool) LoadReader(ctx context.Context, reader io.Reader, name string) error
- func (p *AutoGrowEnginePool) LoadString(ctx context.Context, source string) error
- func (p *AutoGrowEnginePool) RegisterFunction(name string, fn any) error
- func (p *AutoGrowEnginePool) RegisterGlobal(name string, value any) error
- func (p *AutoGrowEnginePool) RegisterModule(name string, module any) error
- func (p *AutoGrowEnginePool) Release(e Engine)
- type CallResult
- type Engine
- type EnginePool
- func (p *EnginePool) Acquire() (Engine, error)
- func (p *EnginePool) CallFunction(ctx context.Context, name string, args ...any) (any, error)
- func (p *EnginePool) ClearError()
- func (p *EnginePool) Close() error
- func (p *EnginePool) Execute(ctx context.Context) (any, error)
- func (p *EnginePool) ExecuteFile(ctx context.Context, filePath string) (any, error)
- func (p *EnginePool) ExecuteString(ctx context.Context, source string) (any, error)
- func (p *EnginePool) GetGlobal(name string) (any, error)
- func (p *EnginePool) GetLastError() error
- func (p *EnginePool) InitAll(ctx context.Context) error
- func (p *EnginePool) IsClosed() bool
- func (p *EnginePool) LoadFile(ctx context.Context, filePath string) error
- func (p *EnginePool) LoadReader(ctx context.Context, reader io.Reader, name string) error
- func (p *EnginePool) LoadString(ctx context.Context, source string) error
- func (p *EnginePool) RegisterFunction(name string, fn any) error
- func (p *EnginePool) RegisterGlobal(name string, value any) error
- func (p *EnginePool) RegisterModule(name string, module any) error
- func (p *EnginePool) Release(e Engine)
- type ExecuteOptions
- type FactoryFunc
- type Manager
- func (m *Manager) CloseAll() error
- func (m *Manager) Get(name string) (Engine, bool)
- func (m *Manager) GetDefault() (Engine, bool)
- func (m *Manager) InitAll(ctx context.Context) error
- func (m *Manager) Register(name string, eng Engine) error
- func (m *Manager) Remove(name string, closeIfExists bool)
- func (m *Manager) SetDefault(name string)
- type Type
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Register ¶
func Register(typ Type, f FactoryFunc) error
Register registers a FactoryFunc for a given Type.
func Unregister ¶
Unregister removes a registered factory by Type. It returns true if a factory was removed.
Types ¶
type AutoGrowEnginePool ¶ added in v0.0.2
type AutoGrowEnginePool struct {
// contains filtered or unexported fields
}
AutoGrowEnginePool 是可按需扩展但有上限的引擎池。
func NewAutoGrowEnginePool ¶ added in v0.0.2
func NewAutoGrowEnginePool(initialSize, maxSize int, typ Type) (*AutoGrowEnginePool, error)
NewAutoGrowEnginePool 创建一个可自增长的池。 initialSize: 初始创建数量(>=0) maxSize: 池允许的最大实例数(必须 >= initialSize && >=1)
func (*AutoGrowEnginePool) Acquire ¶ added in v0.0.2
func (p *AutoGrowEnginePool) Acquire() (Engine, error)
Acquire 获取一个 Engine:优先立即取空闲实例;若无且未到 max,则创建并返回新实例;否则阻塞等待。
func (*AutoGrowEnginePool) CallFunction ¶ added in v0.0.2
func (*AutoGrowEnginePool) ClearError ¶ added in v0.0.2
func (p *AutoGrowEnginePool) ClearError()
func (*AutoGrowEnginePool) Close ¶ added in v0.0.2
func (p *AutoGrowEnginePool) Close() error
Close 关闭池并销毁所有空闲实例。已借出的实例应由调用方关闭或归还后会被关闭。
func (*AutoGrowEnginePool) Execute ¶ added in v0.0.2
func (p *AutoGrowEnginePool) Execute(ctx context.Context) (any, error)
func (*AutoGrowEnginePool) ExecuteFile ¶ added in v0.0.2
func (*AutoGrowEnginePool) ExecuteString ¶ added in v0.0.2
func (*AutoGrowEnginePool) GetGlobal ¶ added in v0.0.2
func (p *AutoGrowEnginePool) GetGlobal(name string) (any, error)
func (*AutoGrowEnginePool) GetLastError ¶ added in v0.0.2
func (p *AutoGrowEnginePool) GetLastError() error
func (*AutoGrowEnginePool) LoadFile ¶ added in v0.0.2
func (p *AutoGrowEnginePool) LoadFile(ctx context.Context, filePath string) error
func (*AutoGrowEnginePool) LoadReader ¶ added in v0.0.2
func (*AutoGrowEnginePool) LoadString ¶ added in v0.0.2
func (p *AutoGrowEnginePool) LoadString(ctx context.Context, source string) error
func (*AutoGrowEnginePool) RegisterFunction ¶ added in v0.0.2
func (p *AutoGrowEnginePool) RegisterFunction(name string, fn any) error
func (*AutoGrowEnginePool) RegisterGlobal ¶ added in v0.0.2
func (p *AutoGrowEnginePool) RegisterGlobal(name string, value any) error
func (*AutoGrowEnginePool) RegisterModule ¶ added in v0.0.2
func (p *AutoGrowEnginePool) RegisterModule(name string, module any) error
func (*AutoGrowEnginePool) Release ¶ added in v0.0.2
func (p *AutoGrowEnginePool) Release(e Engine)
Release 归还 Engine;若池已关闭或通道已满则关闭该实例。
type Engine ¶
type Engine interface {
// GetType get the type of the script engine
GetType() Type
// Init initialize the script engine
Init(ctx context.Context) error
// Close the script engine and release resources
Close() error
// IsInitialized check if the engine is initialized
IsInitialized() bool
// LoadString load script from string source
LoadString(ctx context.Context, source string) error
// LoadFile load script from file path
LoadFile(ctx context.Context, filePath string) error
// LoadReader load script from io.Reader
LoadReader(ctx context.Context, reader io.Reader, name string) error
// Execute execute the loaded script
Execute(ctx context.Context) (any, error)
// ExecuteString execute script from string source
ExecuteString(ctx context.Context, source string) (any, error)
// ExecuteFile execute script from file path
ExecuteFile(ctx context.Context, filePath string) (any, error)
// RegisterGlobal register a global variable
RegisterGlobal(name string, value any) error
// GetGlobal get a global variable
GetGlobal(name string) (any, error)
// RegisterFunction register a function with the given name
RegisterFunction(name string, fn any) error
// CallFunction call a function with the given name and arguments
CallFunction(ctx context.Context, name string, args ...any) (any, error)
// RegisterModule register a module with the given name
RegisterModule(name string, module any) error
// GetLastError get the last error occurred in the engine
GetLastError() error
// ClearError clear the last error
ClearError()
}
Engine Define the interface for script engines
func NewScriptEngine ¶ added in v0.0.2
NewScriptEngine 使用已注册的工厂函数创建一个 Engine 实例。
type EnginePool ¶ added in v0.0.2
type EnginePool struct {
// contains filtered or unexported fields
}
EnginePool 管理多个独立 Engine 实例以支持并发执行。 NewEnginePool 需要提供一个 factory 用于创建单个 Engine 实例。
func NewEnginePool ¶ added in v0.0.2
func NewEnginePool(size int, typ Type) (*EnginePool, error)
NewEnginePool 创建并初始化一个包含 size 个 Engine 的池。 factory 用于创建单个 Engine(例如 newLuaEngine)。
func (*EnginePool) Acquire ¶ added in v0.0.2
func (p *EnginePool) Acquire() (Engine, error)
Acquire 从池中获取一个 Engine(会阻塞直到有可用的)。
func (*EnginePool) CallFunction ¶ added in v0.0.2
func (*EnginePool) ClearError ¶ added in v0.0.2
func (p *EnginePool) ClearError()
func (*EnginePool) Close ¶ added in v0.0.2
func (p *EnginePool) Close() error
Close 关闭池并销毁所有子 Engine。
func (*EnginePool) Execute ¶ added in v0.0.2
func (p *EnginePool) Execute(ctx context.Context) (any, error)
func (*EnginePool) ExecuteFile ¶ added in v0.0.2
func (*EnginePool) ExecuteString ¶ added in v0.0.2
func (*EnginePool) GetGlobal ¶ added in v0.0.2
func (p *EnginePool) GetGlobal(name string) (any, error)
func (*EnginePool) GetLastError ¶ added in v0.0.2
func (p *EnginePool) GetLastError() error
func (*EnginePool) InitAll ¶ added in v0.0.2
func (p *EnginePool) InitAll(ctx context.Context) error
func (*EnginePool) IsClosed ¶ added in v0.0.2
func (p *EnginePool) IsClosed() bool
IsClosed 返回池是否已关闭。
func (*EnginePool) LoadFile ¶ added in v0.0.2
func (p *EnginePool) LoadFile(ctx context.Context, filePath string) error
func (*EnginePool) LoadReader ¶ added in v0.0.2
func (*EnginePool) LoadString ¶ added in v0.0.2
func (p *EnginePool) LoadString(ctx context.Context, source string) error
func (*EnginePool) RegisterFunction ¶ added in v0.0.2
func (p *EnginePool) RegisterFunction(name string, fn any) error
func (*EnginePool) RegisterGlobal ¶ added in v0.0.2
func (p *EnginePool) RegisterGlobal(name string, value any) error
func (*EnginePool) RegisterModule ¶ added in v0.0.2
func (p *EnginePool) RegisterModule(name string, module any) error
func (*EnginePool) Release ¶ added in v0.0.2
func (p *EnginePool) Release(e Engine)
Release 将 Engine 放回池中;若池已关闭则关闭该 Engine。
type ExecuteOptions ¶
ExecuteOptions 执行选项
type FactoryFunc ¶
FactoryFunc 是用于创建 Engine 实例的工厂函数类型。
func GetFactory ¶
func GetFactory(typ Type) (FactoryFunc, bool)
GetFactory returns a registered FactoryFunc for a given Type and whether it existed.
type Manager ¶ added in v0.0.2
type Manager struct {
// contains filtered or unexported fields
}
Manager 管理多个 Engine 实例的生命周期与访问。 - 适用于需要多个引擎实例、统一 Init/Close、或按 name 获取的场景。 - 若项目只需要单个全局 Engine,可不使用 Manager。
func (*Manager) GetDefault ¶ added in v0.0.2
GetDefault 获取默认引擎。
func (*Manager) SetDefault ¶ added in v0.0.2
SetDefault 设置默认引擎名,便于不指定 name 时使用。