Documentation
¶
Index ¶
- func Get(script string) (*ast.Program, bool)
- func Set(script string, program *ast.Program)
- type Kwargs
- type LibraryLoader
- type Scriptling
- func (p *Scriptling) CallFunction(name string, args ...interface{}) (object.Object, error)
- func (p *Scriptling) CallFunctionWithContext(ctx context.Context, name string, args ...interface{}) (object.Object, error)
- func (p *Scriptling) CallMethod(obj object.Object, methodName string, args ...interface{}) (object.Object, error)
- func (p *Scriptling) CallMethodWithContext(ctx context.Context, obj object.Object, methodName string, args ...interface{}) (object.Object, error)
- func (p *Scriptling) Clone() *Scriptling
- func (p *Scriptling) CreateInstance(className string, args ...interface{}) (object.Object, error)
- func (p *Scriptling) CreateInstanceWithContext(ctx context.Context, className string, args ...interface{}) (object.Object, error)
- func (p *Scriptling) EnableOutputCapture()
- func (p *Scriptling) Eval(input string) (object.Object, error)
- func (p *Scriptling) EvalFile(path string) (object.Object, error)
- func (p *Scriptling) EvalWithContext(ctx context.Context, input string) (result object.Object, err error)
- func (p *Scriptling) EvalWithTimeout(timeout time.Duration, input string) (object.Object, error)
- func (p *Scriptling) GetOutput() string
- func (p *Scriptling) GetVar(name string) (interface{}, object.Object)
- func (p *Scriptling) GetVarAsBool(name string) (bool, object.Object)
- func (p *Scriptling) GetVarAsDict(name string) (map[string]object.Object, object.Object)
- func (p *Scriptling) GetVarAsFloat(name string) (float64, object.Object)
- func (p *Scriptling) GetVarAsInt(name string) (int64, object.Object)
- func (p *Scriptling) GetVarAsList(name string) ([]object.Object, object.Object)
- func (p *Scriptling) GetVarAsObject(name string) (object.Object, error)
- func (p *Scriptling) GetVarAsSet(name string) (*object.Set, object.Object)
- func (p *Scriptling) GetVarAsString(name string) (string, object.Object)
- func (p *Scriptling) GetVarAsTuple(name string) ([]object.Object, object.Object)
- func (p *Scriptling) Import(names interface{}) error
- func (p *Scriptling) ListVars() []string
- func (p *Scriptling) LoadLibraryIntoEnv(name string, env *object.Environment) error
- func (p *Scriptling) RegisterFunc(name string, ...)
- func (p *Scriptling) RegisterLibrary(lib *object.Library)
- func (p *Scriptling) RegisterScriptFunc(name string, script string) error
- func (p *Scriptling) RegisterScriptLibrary(name string, script string) error
- func (p *Scriptling) ResetEnv(keepKeys ...string)
- func (p *Scriptling) SetInputReader(r io.Reader)
- func (p *Scriptling) SetLibraryLoader(loader LibraryLoader)
- func (p *Scriptling) SetObjectVar(name string, obj object.Object) error
- func (p *Scriptling) SetOutputWriter(w io.Writer)
- func (p *Scriptling) SetSourceFile(name string)
- func (p *Scriptling) SetVar(name string, value interface{}) error
- func (p *Scriptling) UnsetVar(name string)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Kwargs ¶
type Kwargs map[string]interface{}
Kwargs is a wrapper type to explicitly pass keyword arguments to CallFunction. Use this to distinguish between a map being passed as a dict argument vs kwargs.
type LibraryLoader ¶ added in v0.2.9
type LibraryLoader interface {
Load(name string) (source string, found bool, err error)
Description() string
}
LibraryLoader is the interface for loading libraries from various sources. This is an alias for the libloader.LibraryLoader interface to avoid import cycles.
type Scriptling ¶
type Scriptling struct {
// contains filtered or unexported fields
}
func New ¶
func New() *Scriptling
func (*Scriptling) CallFunction ¶
func (p *Scriptling) CallFunction(name string, args ...interface{}) (object.Object, error)
CallFunction calls a registered function by name with Go arguments. Args are Go types (int, string, etc.) that will be converted to Object. Returns object.Object - use .AsInt(), .AsString(), etc. to extract value.
Works with both Go-registered functions (via RegisterFunc) and script-defined functions.
To pass a map as a dict argument, use map[string]interface{} directly. To pass keyword arguments, wrap the map in Kwargs{}.
Example:
p.RegisterFunc("add", addFunc)
result, err := p.CallFunction("add", 10, 32)
sum, _ := result.AsInt()
// Pass a map as a dict argument
dataMap := map[string]interface{}{"key": "value"}
result, err := p.CallFunction("process", dataMap)
// With keyword arguments (use Kwargs wrapper)
result, err := p.CallFunction("format", "value", Kwargs{"prefix": ">>"})
func (*Scriptling) CallFunctionWithContext ¶
func (p *Scriptling) CallFunctionWithContext(ctx context.Context, name string, args ...interface{}) (object.Object, error)
CallFunctionWithContext calls a registered function by name with Go arguments and a context. The context can be used for cancellation or timeouts. Args are Go types (int, string, etc.) that will be converted to Object. Returns object.Object - use .AsInt(), .AsString(), etc. to extract value.
Works with both Go-registered functions (via RegisterFunc) and script-defined functions.
To pass a map as a dict argument, use map[string]interface{} directly. To pass keyword arguments, wrap the map in Kwargs{}.
Example:
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
result, err := p.CallFunctionWithContext(ctx, "add", 10, 32)
sum, _ := result.AsInt()
// Pass a map as a dict argument
dataMap := map[string]interface{}{"key": "value"}
result, err := p.CallFunctionWithContext(ctx, "process", dataMap)
// With keyword arguments (use Kwargs wrapper)
result, err := p.CallFunctionWithContext(ctx, "format", "value", Kwargs{"prefix": ">>"})
func (*Scriptling) CallMethod ¶
func (p *Scriptling) CallMethod(obj object.Object, methodName string, args ...interface{}) (object.Object, error)
CallMethod calls a method on a Scriptling object (typically an Instance). The obj should be an object.Object (usually obtained from CreateInstance or script evaluation). Args are Go types that will be converted to Object. Returns object.Object - use .AsInt(), .AsString(), etc. to extract value.
Example:
instance, _ := p.CreateInstance("Counter", 10)
result, err := p.CallMethod(instance, "increment")
value, _ := result.AsInt()
func (*Scriptling) CallMethodWithContext ¶
func (p *Scriptling) CallMethodWithContext(ctx context.Context, obj object.Object, methodName string, args ...interface{}) (object.Object, error)
CallMethodWithContext calls a method on a Scriptling object with a context. The context can be used for cancellation or timeouts.
Example:
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
instance, _ := p.CreateInstance("Counter", 10)
result, err := p.CallMethodWithContext(ctx, instance, "increment")
func (*Scriptling) Clone ¶ added in v0.2.0
func (p *Scriptling) Clone() *Scriptling
Clone creates a new Scriptling interpreter that shares library registrations (both Go and script libraries) with the parent but starts with a fresh, isolated environment. Already-imported libraries are NOT copied; each clone re-evaluates script libraries on first import, so no mutable state is shared. Useful for per-request / multi-tenant isolation.
func (*Scriptling) CreateInstance ¶
func (p *Scriptling) CreateInstance(className string, args ...interface{}) (object.Object, error)
CreateInstance creates an instance of a Scriptling class and returns it as an object.Object. The className should be the name of a class defined in the script or registered via a library. Args are Go types that will be converted to Object and passed to __init__.
Example:
p.Eval("class Counter:\n def __init__(self, start=0):\n self.value = start")
instance, err := p.CreateInstance("Counter", 10)
if err != nil {
// handle error
}
// Now you can use CallMethod on this instance
func (*Scriptling) CreateInstanceWithContext ¶
func (p *Scriptling) CreateInstanceWithContext(ctx context.Context, className string, args ...interface{}) (object.Object, error)
CreateInstanceWithContext creates an instance of a Scriptling class with a context. The context can be used for cancellation or timeouts.
Example:
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) defer cancel() instance, err := p.CreateInstanceWithContext(ctx, "Counter", 10)
func (*Scriptling) EnableOutputCapture ¶
func (p *Scriptling) EnableOutputCapture()
EnableOutputCapture enables capturing print output instead of sending to stdout
func (*Scriptling) Eval ¶
func (p *Scriptling) Eval(input string) (object.Object, error)
Eval executes script without timeout (backwards compatible)
func (*Scriptling) EvalFile ¶ added in v0.2.0
func (p *Scriptling) EvalFile(path string) (object.Object, error)
EvalFile reads a file from disk and evaluates it.
func (*Scriptling) EvalWithContext ¶
func (p *Scriptling) EvalWithContext(ctx context.Context, input string) (result object.Object, err error)
EvalWithContext executes script with context for timeout/cancellation. This method is safe against deep recursion (via call depth tracking) and recovers from panics during script execution.
func (*Scriptling) EvalWithTimeout ¶
EvalWithTimeout executes script with timeout
func (*Scriptling) GetOutput ¶
func (p *Scriptling) GetOutput() string
GetOutput returns captured output and clears the buffer
func (*Scriptling) GetVarAsBool ¶
func (p *Scriptling) GetVarAsBool(name string) (bool, object.Object)
func (*Scriptling) GetVarAsDict ¶
func (*Scriptling) GetVarAsFloat ¶
func (p *Scriptling) GetVarAsFloat(name string) (float64, object.Object)
func (*Scriptling) GetVarAsInt ¶
func (p *Scriptling) GetVarAsInt(name string) (int64, object.Object)
func (*Scriptling) GetVarAsList ¶
func (*Scriptling) GetVarAsObject ¶
func (p *Scriptling) GetVarAsObject(name string) (object.Object, error)
GetVarAsObject retrieves a variable from the environment as a scriptling Object.
func (*Scriptling) GetVarAsSet ¶ added in v0.2.0
func (*Scriptling) GetVarAsString ¶
func (p *Scriptling) GetVarAsString(name string) (string, object.Object)
Convenience methods for type-safe variable access
func (*Scriptling) GetVarAsTuple ¶ added in v0.2.0
func (*Scriptling) Import ¶
func (p *Scriptling) Import(names interface{}) error
Import imports a library into the current environment, making it available for use without needing an import statement in scripts
func (*Scriptling) ListVars ¶ added in v0.2.0
func (p *Scriptling) ListVars() []string
ListVars returns a sorted list of variable names in the current environment, excluding internal names (import builtin).
func (*Scriptling) LoadLibraryIntoEnv ¶
func (p *Scriptling) LoadLibraryIntoEnv(name string, env *object.Environment) error
LoadLibraryIntoEnv loads a library into the specified environment. This is useful for loading libraries into cloned environments for background tasks. Returns an error if the library cannot be loaded.
func (*Scriptling) RegisterFunc ¶
func (*Scriptling) RegisterLibrary ¶
func (p *Scriptling) RegisterLibrary(lib *object.Library)
RegisterLibrary registers a new library that can be imported by scripts The library name is extracted from the library itself
func (*Scriptling) RegisterScriptFunc ¶
func (p *Scriptling) RegisterScriptFunc(name string, script string) error
RegisterScriptFunc registers a function written in Scriptling The script should define a function and this method will extract it and register it by name
func (*Scriptling) RegisterScriptLibrary ¶
func (p *Scriptling) RegisterScriptLibrary(name string, script string) error
RegisterScriptLibrary registers a library written in Scriptling The script should define functions/values that will be available when the library is imported
func (*Scriptling) ResetEnv ¶ added in v0.2.7
func (p *Scriptling) ResetEnv(keepKeys ...string)
ResetEnv clears all user-defined variables from the environment, keeping only the listed keys (plus the "import" builtin which is always preserved). Imported library dicts remain if their names are passed in keepKeys. This allows VM reuse across requests without re-registering libraries.
func (*Scriptling) SetInputReader ¶
func (p *Scriptling) SetInputReader(r io.Reader)
SetInputReader sets a custom reader for input (e.g., for reading from a websocket)
func (*Scriptling) SetLibraryLoader ¶ added in v0.2.9
func (p *Scriptling) SetLibraryLoader(loader LibraryLoader)
SetLibraryLoader sets a loader for dynamically loading libraries. This is the preferred way to load libraries from various sources (filesystem, API, etc.) using the libloader package.
Example:
loader := libloader.NewChain(
libloader.NewFilesystem("/app/libs"),
libloader.NewAPI("https://api.example.com/libs"),
)
p.SetLibraryLoader(loader)
func (*Scriptling) SetObjectVar ¶
func (p *Scriptling) SetObjectVar(name string, obj object.Object) error
SetObjectVar sets a variable in the environment from a scriptling Object. This is useful when you already have a scriptling object (like an Instance) and want to set it directly without converting from Go types.
func (*Scriptling) SetOutputWriter ¶
func (p *Scriptling) SetOutputWriter(w io.Writer)
SetOutputWriter sets a custom writer for output (e.g., for streaming to a websocket or logger)
func (*Scriptling) SetSourceFile ¶
func (p *Scriptling) SetSourceFile(name string)
SetSourceFile sets the source file name used in error messages. When set, errors will include the file name and line number for better debugging.
func (*Scriptling) SetVar ¶
func (p *Scriptling) SetVar(name string, value interface{}) error
func (*Scriptling) UnsetVar ¶ added in v0.2.0
func (p *Scriptling) UnsetVar(name string)
UnsetVar removes a variable from the current environment.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package build contains build-time version information for Scriptling
|
Package build contains build-time version information for Scriptling |
|
Package evaliface provides an interface for calling functions from libraries without creating circular dependencies
|
Package evaliface provides an interface for calling functions from libraries without creating circular dependencies |
|
examples
|
|
|
call_method
command
|
|
|
extending
command
|
|
|
logging
command
|
|
|
mcp-client/direct
command
|
|
|
mcp-client/shared
command
|
|
|
mcp-client/with-openai
command
|
|
|
mcp-client/with-openai-instance
command
|
|
|
multi-environment
command
|
|
|
openai/instance
command
|
|
|
openai/shared
command
|
|
|
openai/streaming
command
|
|
|
scripts
command
|
|
|
Package extlibs provides external libraries that need explicit registration
|
Package extlibs provides external libraries that need explicit registration |
|
Package libloader provides a flexible library loading system for Scriptling.
|
Package libloader provides a flexible library loading system for Scriptling. |
|
Package lint provides code analysis functionality for Scriptling scripts.
|
Package lint provides code analysis functionality for Scriptling scripts. |
|
scripts
|
|
|
homebrew-formula
command
|
|
|
tools
|
|
|
echo
command
|
|
|
getversion
command
|
|