Documentation
¶
Index ¶
- Variables
- func Register(typ Type, f FactoryFunc) error
- func SetLogger(l Logger)
- 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) ExecuteFromKey(ctx context.Context, key string) (any, error)
- func (p *AutoGrowEnginePool) ExecuteFromKeys(ctx context.Context, keys []string) ([]any, error)
- func (p *AutoGrowEnginePool) ExecuteString(ctx context.Context, name string, code string) (any, error)
- func (p *AutoGrowEnginePool) GetGlobal(name string) (any, error)
- func (p *AutoGrowEnginePool) GetLastError() error
- func (p *AutoGrowEnginePool) GetSource() source.Reader
- func (p *AutoGrowEnginePool) Load(ctx context.Context, key string) error
- func (p *AutoGrowEnginePool) LoadMulti(ctx context.Context, keys []string) error
- func (p *AutoGrowEnginePool) LoadString(ctx context.Context, name string, code 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)
- func (p *AutoGrowEnginePool) SetSource(source source.Reader)
- func (p *AutoGrowEnginePool) StartWatch(ctx context.Context, key string) error
- func (p *AutoGrowEnginePool) StopWatch(key string) error
- 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) ExecuteFromKey(ctx context.Context, key string) (any, error)
- func (p *EnginePool) ExecuteFromKeys(ctx context.Context, keys []string) ([]any, error)
- func (p *EnginePool) ExecuteString(ctx context.Context, name string, code string) (any, error)
- func (p *EnginePool) GetGlobal(name string) (any, error)
- func (p *EnginePool) GetLastError() error
- func (p *EnginePool) GetSource() source.Reader
- func (p *EnginePool) InitAll(ctx context.Context) error
- func (p *EnginePool) IsClosed() bool
- func (p *EnginePool) Load(ctx context.Context, key string) error
- func (p *EnginePool) LoadMulti(ctx context.Context, keys []string) error
- func (p *EnginePool) LoadString(ctx context.Context, name string, code 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)
- func (p *EnginePool) SetSource(source source.Reader)
- func (p *EnginePool) StartWatch(ctx context.Context, key string) error
- func (p *EnginePool) StopWatch(key string) error
- type FactoryFunc
- type FunctionRegistrar
- type GlobalAccessor
- type Logger
- 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 ModuleRegistrar
- type ScriptEngine
- type ScriptExecutor
- type ScriptLoader
- type ScriptWatcher
- type SlogLogger
- func (s SlogLogger) Debug(ctx context.Context, msg string, args ...any)
- func (s SlogLogger) Error(ctx context.Context, msg string, args ...any)
- func (s SlogLogger) Info(ctx context.Context, msg string, args ...any)
- func (s SlogLogger) Warn(ctx context.Context, msg string, args ...any)
- func (s SlogLogger) With(args ...any) Logger
- type Type
Constants ¶
This section is empty.
Variables ¶
var ErrCapabilityNotSupported = errors.New("script engine: capability not supported by this engine type")
ErrCapabilityNotSupported is returned when an engine does not implement an optional capability interface (e.g. ModuleRegistrar, ScriptWatcher).
Callers that receive this error should gracefully degrade instead of treating it as a fatal failure.
Functions ¶
func Register ¶
func Register(typ Type, f FactoryFunc) error
Register registers a FactoryFunc for the given Type. Returns an error if a factory for typ is already registered.
func SetLogger ¶ added in v0.0.6
func SetLogger(l Logger)
SetLogger sets the package-level Logger used by all engines, pools and submodules. Pass nil to revert to the silent nopLogger.
SetLogger is safe to call concurrently with log calls; callers that want to mutate the logger without races should hold their own coordination.
func Unregister ¶
Unregister removes the factory registered for typ. 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 is an Engine pool that can grow on demand up to a configured maximum. Idle instances are reused; when none is available and the cap has not been reached, a new Engine is created on the fly.
func NewAutoGrowEnginePool ¶ added in v0.0.2
func NewAutoGrowEnginePool(initialSize, maxSize int, typ Type) (*AutoGrowEnginePool, error)
NewAutoGrowEnginePool creates a pool that can grow on demand.
- initialSize: number of Engines to create eagerly (>= 0)
- maxSize: upper bound on instance count (must be >= initialSize and >= 1)
- typ: selects the registered factory used to build each Engine
func (*AutoGrowEnginePool) Acquire ¶ added in v0.0.2
func (p *AutoGrowEnginePool) Acquire() (Engine, error)
Acquire returns an Engine from the pool.
- If an idle instance is available, it is returned immediately.
- Otherwise, if total < max, a new instance is created and returned.
- Otherwise the call blocks until an instance is released.
func (*AutoGrowEnginePool) CallFunction ¶ added in v0.0.2
func (p *AutoGrowEnginePool) CallFunction(ctx context.Context, name string, args ...any) (any, error)
CallFunction invokes the named script function on an acquired Engine.
func (*AutoGrowEnginePool) ClearError ¶ added in v0.0.2
func (p *AutoGrowEnginePool) ClearError()
ClearError clears the last error on an acquired Engine.
func (*AutoGrowEnginePool) Close ¶ added in v0.0.2
func (p *AutoGrowEnginePool) Close() error
Close closes the pool and destroys every currently idle instance. Engines that are still borrowed must be Closed by their callers (or will be Closed when eventually released).
func (*AutoGrowEnginePool) Execute ¶ added in v0.0.2
func (p *AutoGrowEnginePool) Execute(ctx context.Context) (any, error)
Execute runs every previously-loaded script on an acquired Engine and returns the combined result.
func (*AutoGrowEnginePool) ExecuteFromKey ¶ added in v0.0.6
ExecuteFromKey loads (via the bound Source) and immediately runs the script identified by key on an acquired Engine.
func (*AutoGrowEnginePool) ExecuteFromKeys ¶ added in v0.0.6
ExecuteFromKeys is the multi-key variant of ExecuteFromKey; results are returned in the same order as keys.
func (*AutoGrowEnginePool) ExecuteString ¶ added in v0.0.2
func (p *AutoGrowEnginePool) ExecuteString(ctx context.Context, name string, code string) (any, error)
ExecuteString compiles and immediately runs the inline script (name+code) on an acquired Engine, bypassing the bound Source.
func (*AutoGrowEnginePool) GetGlobal ¶ added in v0.0.2
func (p *AutoGrowEnginePool) GetGlobal(name string) (any, error)
GetGlobal reads a global variable from an acquired Engine.
func (*AutoGrowEnginePool) GetLastError ¶ added in v0.0.2
func (p *AutoGrowEnginePool) GetLastError() error
GetLastError returns the last error from an acquired Engine.
func (*AutoGrowEnginePool) GetSource ¶ added in v0.0.6
func (p *AutoGrowEnginePool) GetSource() source.Reader
GetSource returns the ScriptSource bound to an acquired Engine (or nil).
func (*AutoGrowEnginePool) Load ¶ added in v0.0.6
func (p *AutoGrowEnginePool) Load(ctx context.Context, key string) error
Load loads a single script from the bound Source into an acquired Engine.
func (*AutoGrowEnginePool) LoadMulti ¶ added in v0.0.6
func (p *AutoGrowEnginePool) LoadMulti(ctx context.Context, keys []string) error
LoadMulti loads multiple scripts from the bound Source into an acquired Engine.
func (*AutoGrowEnginePool) LoadString ¶ added in v0.0.2
LoadString compiles an inline script (name+code) on an acquired Engine. It does NOT go through the bound Source.
func (*AutoGrowEnginePool) RegisterFunction ¶ added in v0.0.2
func (p *AutoGrowEnginePool) RegisterFunction(name string, fn any) error
RegisterFunction registers a host function on an acquired Engine. Note: the registration is LOCAL to that Engine instance.
func (*AutoGrowEnginePool) RegisterGlobal ¶ added in v0.0.2
func (p *AutoGrowEnginePool) RegisterGlobal(name string, value any) error
RegisterGlobal registers a global variable on an acquired Engine. Note: the registration is LOCAL to that Engine instance.
func (*AutoGrowEnginePool) RegisterModule ¶ added in v0.0.2
func (p *AutoGrowEnginePool) RegisterModule(name string, module any) error
RegisterModule registers a module on an acquired Engine. Note: the registration is LOCAL to that Engine instance.
func (*AutoGrowEnginePool) Release ¶ added in v0.0.2
func (p *AutoGrowEnginePool) Release(e Engine)
Release returns an Engine to the pool. If the pool is closed or the channel is full, the Engine is Closed instead and the live counter is decremented.
func (*AutoGrowEnginePool) SetSource ¶ added in v0.0.6
func (p *AutoGrowEnginePool) SetSource(source source.Reader)
SetSource binds a ScriptSource on an acquired Engine. Note: the binding is LOCAL to that Engine instance; other engines in the pool are unaffected. See the package-level note above for pool-wide setup.
func (*AutoGrowEnginePool) StartWatch ¶ added in v0.0.6
func (p *AutoGrowEnginePool) StartWatch(ctx context.Context, key string) error
StartWatch starts watching the script identified by `key` on an acquired Engine. Note: the watch is LOCAL to that Engine instance.
func (*AutoGrowEnginePool) StopWatch ¶ added in v0.0.6
func (p *AutoGrowEnginePool) StopWatch(key string) error
StopWatch stops watching the script identified by `key` on an acquired Engine.
type Engine ¶
type Engine interface {
ScriptEngine
ScriptLoader
ScriptExecutor
GlobalAccessor
FunctionRegistrar
ModuleRegistrar
ScriptWatcher
}
Engine is the aggregate interface combining all capability interfaces. Full-featured engines (Lua, JavaScript, ...) implement this.
Lightweight engines (CEL, Expr) may implement only a subset; callers should use the capability helper functions (AsLoader, AsExecutor, ...) or direct type assertions to check for support.
Implementations are expected to be safe for concurrent use of the methods documented as such; see each method's comment for details.
func NewScriptEngine ¶ added in v0.0.2
NewScriptEngine creates an Engine instance using the factory registered for typ.
type EnginePool ¶ added in v0.0.2
type EnginePool struct {
// contains filtered or unexported fields
}
EnginePool manages a fixed number of independent Engine instances to support concurrent execution. The pool is created via NewEnginePool, which uses the factory registered for typ to instantiate each Engine.
func NewEnginePool ¶ added in v0.0.2
func NewEnginePool(size int, typ Type) (*EnginePool, error)
NewEnginePool creates and initializes a pool of size Engines. typ selects the registered factory used to create each Engine.
func (*EnginePool) Acquire ¶ added in v0.0.2
func (p *EnginePool) Acquire() (Engine, error)
Acquire takes an Engine out of the pool. It blocks until one becomes available. Returns an error if the pool is already closed.
func (*EnginePool) CallFunction ¶ added in v0.0.2
CallFunction invokes the named script function on an acquired Engine.
func (*EnginePool) ClearError ¶ added in v0.0.2
func (p *EnginePool) ClearError()
ClearError clears the last error on an acquired Engine.
func (*EnginePool) Close ¶ added in v0.0.2
func (p *EnginePool) Close() error
Close closes the pool and destroys all pooled Engines.
func (*EnginePool) Execute ¶ added in v0.0.2
func (p *EnginePool) Execute(ctx context.Context) (any, error)
Execute runs every previously-loaded script on an acquired Engine and returns the combined result.
func (*EnginePool) ExecuteFromKey ¶ added in v0.0.6
ExecuteFromKey loads (via the bound Source) and immediately runs the script identified by key on an acquired Engine.
func (*EnginePool) ExecuteFromKeys ¶ added in v0.0.6
ExecuteFromKeys is the multi-key variant of ExecuteFromKey; results are returned in the same order as keys.
func (*EnginePool) ExecuteString ¶ added in v0.0.2
ExecuteString compiles and immediately runs the inline script (name+code) on an acquired Engine, bypassing the bound Source.
func (*EnginePool) GetGlobal ¶ added in v0.0.2
func (p *EnginePool) GetGlobal(name string) (any, error)
GetGlobal reads a global variable from an acquired Engine.
func (*EnginePool) GetLastError ¶ added in v0.0.2
func (p *EnginePool) GetLastError() error
GetLastError returns the last error from an acquired Engine.
func (*EnginePool) GetSource ¶ added in v0.0.6
func (p *EnginePool) GetSource() source.Reader
GetSource returns the ScriptSource bound to an acquired Engine (or nil).
func (*EnginePool) InitAll ¶ added in v0.0.2
func (p *EnginePool) InitAll(ctx context.Context) error
InitAll re-initializes every Engine in the pool. It acquires all instances, calls Init on each, and releases them back.
func (*EnginePool) IsClosed ¶ added in v0.0.2
func (p *EnginePool) IsClosed() bool
IsClosed reports whether the pool has been closed.
func (*EnginePool) Load ¶ added in v0.0.6
func (p *EnginePool) Load(ctx context.Context, key string) error
Load loads a single script from the bound Source into an acquired Engine.
func (*EnginePool) LoadMulti ¶ added in v0.0.6
func (p *EnginePool) LoadMulti(ctx context.Context, keys []string) error
LoadMulti loads multiple scripts from the bound Source into an acquired Engine.
func (*EnginePool) LoadString ¶ added in v0.0.2
LoadString compiles an inline script (name+code) on an acquired Engine. It does NOT go through the bound Source.
func (*EnginePool) RegisterFunction ¶ added in v0.0.2
func (p *EnginePool) RegisterFunction(name string, fn any) error
RegisterFunction registers a host function on an acquired Engine. Note: the registration is LOCAL to that Engine instance.
func (*EnginePool) RegisterGlobal ¶ added in v0.0.2
func (p *EnginePool) RegisterGlobal(name string, value any) error
RegisterGlobal registers a global variable on an acquired Engine. Note: the registration is LOCAL to that Engine instance.
func (*EnginePool) RegisterModule ¶ added in v0.0.2
func (p *EnginePool) RegisterModule(name string, module any) error
RegisterModule registers a module on an acquired Engine. Note: the registration is LOCAL to that Engine instance.
func (*EnginePool) Release ¶ added in v0.0.2
func (p *EnginePool) Release(e Engine)
Release returns an Engine to the pool. If the pool is already closed, the Engine is Closed instead.
func (*EnginePool) SetSource ¶ added in v0.0.6
func (p *EnginePool) SetSource(source source.Reader)
SetSource binds a ScriptSource on an acquired Engine. Note: the binding is LOCAL to that Engine instance; other engines in the pool are unaffected. See the package-level note above for pool-wide setup.
func (*EnginePool) StartWatch ¶ added in v0.0.6
func (p *EnginePool) StartWatch(ctx context.Context, key string) error
StartWatch starts watching the script identified by `key` on an acquired Engine. Note: the watch is LOCAL to that Engine instance.
func (*EnginePool) StopWatch ¶ added in v0.0.6
func (p *EnginePool) StopWatch(key string) error
StopWatch stops watching the script identified by `key` on an acquired Engine.
type FactoryFunc ¶
FactoryFunc is the factory function signature used to create an Engine instance.
func GetFactory ¶
func GetFactory(typ Type) (FactoryFunc, bool)
GetFactory returns the FactoryFunc registered for typ and whether it existed.
type FunctionRegistrar ¶ added in v0.0.6
type FunctionRegistrar interface {
// RegisterFunction registers a host function that scripts can call by `name`.
// The concrete type accepted for `fn` depends on the engine implementation.
RegisterFunction(name string, fn any) error
// CallFunction invokes the script-side function registered as `name` with the
// given arguments and returns its result. ctx can be used to cancel/timeout
// the call.
CallFunction(ctx context.Context, name string, args ...any) (any, error)
}
FunctionRegistrar provides host-function registration and script-function invocation.
func AsFunctionRegistrar ¶ added in v0.0.6
func AsFunctionRegistrar(e any) FunctionRegistrar
AsFunctionRegistrar returns the FunctionRegistrar capability of e, or nil if unsupported.
type GlobalAccessor ¶ added in v0.0.6
type GlobalAccessor interface {
// RegisterGlobal registers or overwrites a global variable visible to scripts.
RegisterGlobal(name string, value any) error
// GetGlobal reads the value of a global variable. Returns an error if the name
// is undefined.
GetGlobal(name string) (any, error)
}
GlobalAccessor provides read/write access to global variables visible to scripts.
func AsGlobalAccessor ¶ added in v0.0.6
func AsGlobalAccessor(e any) GlobalAccessor
AsGlobalAccessor returns the GlobalAccessor capability of e, or nil if unsupported.
type Logger ¶ added in v0.0.6
type Logger interface {
// Debug logs a message at DEBUG level with optional structured key/value
// pairs passed as args (alternating keys and values).
Debug(ctx context.Context, msg string, args ...any)
// Info logs a message at INFO level.
Info(ctx context.Context, msg string, args ...any)
// Warn logs a message at WARN level.
Warn(ctx context.Context, msg string, args ...any)
// Error logs a message at ERROR level.
Error(ctx context.Context, msg string, args ...any)
// With returns a new Logger instance with the given key-value pairs attached.
// This is typically used to distinguish modules, e.g., logger.With("module", "goja").
With(args ...any) Logger
}
Logger is the minimal logging interface used by script_engine internals.
It is deliberately small (4 methods, no WithAttrs/WithGroup) so that any project can adapt its own backend (the stdlib *slog.Logger, zap, zerolog, kratos log, ...) with a few lines of glue code and inject it via SetLogger.
The first argument is always a context, so backends that support it can extract trace ids / request-scoped attributes. Callers that have no context at hand may pass nil.
func GetLogger ¶ added in v0.0.6
func GetLogger() Logger
GetLogger returns the currently active package-level Logger. It is the entry point submodules use to obtain the shared logger without taking a direct dependency on any logging framework.
func NewSlogLogger ¶ added in v0.0.6
func NewSlogLogger() Logger
NewSlogLogger builds a SlogLogger backed by the stdlib slog with sensible defaults: a text handler writing to stderr at INFO level.
Callers needing a different format / level / destination should either:
type Manager ¶ added in v0.0.2
type Manager struct {
// contains filtered or unexported fields
}
Manager manages the lifecycle and access of multiple Engine instances.
- Useful when the application needs multiple engine instances, unified Init/Close, or name-based lookup.
- If the project only needs a single global Engine, Manager is unnecessary.
func (*Manager) CloseAll ¶ added in v0.0.2
CloseAll closes every registered Engine. Individual Close errors are ignored; the last non-nil error is returned.
func (*Manager) GetDefault ¶ added in v0.0.2
GetDefault returns the default Engine.
func (*Manager) Register ¶ added in v0.0.2
Register registers an Engine without initializing it. Returns an error if name already exists.
func (*Manager) Remove ¶ added in v0.0.2
Remove unregisters the Engine with the given name. If closeIfExists is true and the Engine exists, it is also Closed.
func (*Manager) SetDefault ¶ added in v0.0.2
SetDefault sets the default engine name used by GetDefault.
type ModuleRegistrar ¶ added in v0.0.6
type ModuleRegistrar interface {
// RegisterModule registers a module (e.g. map[string]any, native loader, ...)
// under `name` so scripts can require/use it. The accepted shape of `module`
// depends on the engine implementation.
RegisterModule(name string, module any) error
}
ModuleRegistrar provides module registration for engines that support a module system (e.g. Lua's require, JavaScript's import).
Lightweight expression engines (CEL, Expr) typically do NOT implement this.
func AsModuleRegistrar ¶ added in v0.0.6
func AsModuleRegistrar(e any) ModuleRegistrar
AsModuleRegistrar returns the ModuleRegistrar capability of e, or nil if unsupported.
type ScriptEngine ¶ added in v0.0.6
type ScriptEngine interface {
// GetType returns the Type identifier of this script engine.
GetType() Type
// Init initializes the script engine. Must be called before any Load*/Execute*.
// Returns an error if initialization fails or if the engine is already initialized.
Init(ctx context.Context) error
// Close releases all resources held by the engine (runtime, VM, handles).
// Returns an error if teardown fails. After Close, the engine must be re-Init'd
// before reuse.
Close() error
// IsInitialized reports whether the engine has been initialized and not yet closed.
IsInitialized() bool
// GetLastError returns the last error recorded by the engine, or nil if none.
GetLastError() error
// ClearError clears the engine's last-error state.
ClearError()
}
ScriptEngine is the minimal lifecycle interface that every engine implementation — regardless of its feature set — must satisfy.
Full-featured engines (Lua, JavaScript) additionally implement the optional capability interfaces below and are aggregated by Engine. Lightweight engines (CEL, Expr) may implement only ScriptEngine + ScriptExecutor and skip the rest.
type ScriptExecutor ¶ added in v0.0.6
type ScriptExecutor interface {
// Execute runs every script previously loaded via Load/LoadMulti/LoadString and
// returns the combined result.
Execute(ctx context.Context) (any, error)
// ExecuteFromKey loads the script identified by `key` from the bound Source and
// immediately runs it, all in one step.
ExecuteFromKey(ctx context.Context, key string) (any, error)
// ExecuteFromKeys is the multi-key variant of ExecuteFromKey; results are
// returned in the same order as `keys`.
ExecuteFromKeys(ctx context.Context, keys []string) ([]any, error)
// ExecuteString compiles and immediately runs an inline string script, bypassing
// the bound Source. `name` is used for diagnostics.
ExecuteString(ctx context.Context, name string, code string) (any, error)
}
ScriptExecutor provides script execution capabilities.
func AsExecutor ¶ added in v0.0.6
func AsExecutor(e any) ScriptExecutor
AsExecutor returns the ScriptExecutor capability of e, or nil if unsupported.
type ScriptLoader ¶ added in v0.0.6
type ScriptLoader interface {
// SetSource binds a ScriptSource (FileSource / S3 / Mem / Multi / ...) to the
// engine. Subsequent Load / ExecuteFromKey / ExecuteFromKeys calls read through it.
// Passing nil clears any previously bound source.
SetSource(source source.Reader)
// GetSource returns the currently bound ScriptSource, or nil if none has been set.
GetSource() source.Reader
// Load loads a single script from the bound Source using the given key
// (path / object key / script id, ...). Loaded scripts are kept by the engine
// for later Execute runs.
Load(ctx context.Context, key string) error
// LoadMulti loads multiple scripts from the bound Source in order.
// It aborts on the first error.
LoadMulti(ctx context.Context, keys []string) error
// LoadString compiles an inline script given directly as a string. It does NOT
// go through the bound Source; use Load/LoadMulti for source-driven loading.
// `name` is used for diagnostics (stack traces, error messages).
LoadString(ctx context.Context, name string, code string) error
}
ScriptLoader provides source-driven script loading.
Loading is uniformly driven by the bound ScriptSource so the engine itself stays decoupled from concrete IO mechanisms (filesystem, S3, memory, ...).
func AsLoader ¶ added in v0.0.6
func AsLoader(e any) ScriptLoader
AsLoader returns the ScriptLoader capability of e, or nil if unsupported.
type ScriptWatcher ¶ added in v0.0.6
type ScriptWatcher interface {
// StartWatch starts watching the script identified by `key` for changes via the
// bound Source's Watch capability. When a change is detected, the script is
// automatically reloaded. Returns an error if the source doesn't implement
// Watcher or is not bound.
StartWatch(ctx context.Context, key string) error
// StopWatch stops watching the script identified by `key` and cleans up
// the associated goroutine.
StopWatch(key string) error
}
ScriptWatcher provides hot-reload (Watch) capabilities for scripts bound via a Source that implements source.Watcher.
Engines that do not support hot-reload (CEL, Expr, ...) do NOT implement this.
func AsWatcher ¶ added in v0.0.6
func AsWatcher(e any) ScriptWatcher
AsWatcher returns the ScriptWatcher capability of e, or nil if unsupported.
type SlogLogger ¶ added in v0.0.6
type SlogLogger struct {
// L is the underlying slog logger. It MUST be non-nil; [NewSlogLogger]
// always returns a ready-to-use instance.
L *slog.Logger
}
SlogLogger adapts the stdlib *slog.Logger to the Logger interface.
This is the reference implementation returned by NewSlogLogger. Callers that already own a configured *slog.Logger can wrap it directly:
scriptEngine.SetLogger(scriptEngine.SlogLogger{L: mySlogLogger})
func (SlogLogger) Debug ¶ added in v0.0.6
func (s SlogLogger) Debug(ctx context.Context, msg string, args ...any)
Debug forwards to slog.Logger.DebugContext.
func (SlogLogger) Error ¶ added in v0.0.6
func (s SlogLogger) Error(ctx context.Context, msg string, args ...any)
Error forwards to slog.Logger.ErrorContext.
func (SlogLogger) Info ¶ added in v0.0.6
func (s SlogLogger) Info(ctx context.Context, msg string, args ...any)
Info forwards to slog.Logger.InfoContext.
func (SlogLogger) Warn ¶ added in v0.0.6
func (s SlogLogger) Warn(ctx context.Context, msg string, args ...any)
Warn forwards to slog.Logger.WarnContext.
func (SlogLogger) With ¶ added in v0.0.6
func (s SlogLogger) With(args ...any) Logger
With returns a new SlogLogger whose underlying *slog.Logger has the given key-value pairs attached. This is typically used to distinguish modules, e.g., logger.With("module", "goja"). The returned logger will include these attributes in every log record it produces.
type Type ¶
type Type string
Type identifies a script engine implementation.
const ( UnknownType Type = "unknown" // LuaType is the Lua 5.1 engine powered by gopher-lua. LuaType Type = "lua" // JavaScriptType is the ECMAScript 5.1+ engine powered by goja. JavaScriptType Type = "javascript" // GPythonType is the pure-Go Python 3.4 engine powered by gpython. GPythonType Type = "gpython" // YaegiType is the native Go script engine powered by Traefik Yaegi. YaegiType Type = "yaegi" // WazeroType is the WebAssembly engine powered by tetratelabs/wazero. WazeroType Type = "wazero" // CELType is the Google Common Expression Language powered by cel-go. CELType Type = "cel" // ExprType is the lightweight expression engine powered by antonmedv/expr. ExprType Type = "expr" // StarlarkType is the hermetic / safe script engine powered by google/starlark-go. StarlarkType Type = "starlark" // TclType is the 100 %-compatible Tcl engine powered by modernc.org/tcl. TclType Type = "tcl" )
func ListFactories ¶
func ListFactories() []Type
ListFactories returns a snapshot of all currently registered Types.