interpreter

package
v0.4.24 Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

pkg/interpreter/convert.go Type conversion helpers between Go and xxlang objects.

pkg/interpreter/interpreter.go Clean embedding API for using xxlang as a Go library.

pkg/interpreter/options.go Functional options for configuring the interpreter.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromGo

func FromGo(value interface{}) (objects.Object, error)

FromGo converts a Go value to an xxlang object. Supported Go types:

  • int, int8, int16, int32, int64 -> Int
  • uint, uint8, uint16, uint32, uint64 -> Int
  • float32, float64 -> Float
  • string -> String
  • bool -> Bool
  • []T -> Array (elements converted recursively)
  • map[string]T -> Map (values converted recursively)
  • nil -> Null
  • objects.Object -> returned as-is

func ToArray

func ToArray(obj objects.Object) ([]objects.Object, error)

ToArray converts an object to a slice of objects if possible. Returns an error if the object is not an Array.

func ToBool

func ToBool(obj objects.Object) (bool, error)

ToBool converts an object to a bool if possible. Returns an error if the object is not a Bool.

func ToFloat

func ToFloat(obj objects.Object) (float64, error)

ToFloat converts an object to a float64 if possible. Returns an error if the object is not a Float.

func ToGo

func ToGo(obj objects.Object) interface{}

ToGo converts an xxlang object to a Go value. Returns a Go native type that best represents the object:

  • Int -> int64
  • Float -> float64
  • String -> string
  • Bool -> bool
  • Array -> []interface{} (elements converted recursively)
  • Map -> map[string]interface{} (values converted recursively)
  • Null -> nil
  • Other -> objects.Object (returned as-is)

func ToInt

func ToInt(obj objects.Object) (int64, error)

ToInt converts an object to an int64 if possible. Returns an error if the object is not an Int.

func ToMap

func ToMap(obj objects.Object) (map[string]objects.Object, error)

ToMap converts an object to a map of objects if possible. Returns an error if the object is not a Map.

func ToString

func ToString(obj objects.Object) (string, error)

ToString converts an object to a string if possible. Returns an error if the object is not a String.

Types

type Interpreter

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

Interpreter wraps VM and Compiler for easy embedding. It provides a high-level API for evaluating xxlang code and passing values between Go and xxlang.

func New

func New(opts ...Option) *Interpreter

New creates a new interpreter with the given options. Default interpreter uses RegisterVM for best performance. JIT is disabled by default for stability.

func (*Interpreter) Eval

func (i *Interpreter) Eval(code string) (objects.Object, error)

Eval compiles and executes xxlang code, returning the result. This is the primary method for running xxlang code from Go.

func (*Interpreter) EvalFile

func (i *Interpreter) EvalFile(path string) (objects.Object, error)

EvalFile loads and executes an xxlang source file. The file path is used for module resolution and error messages.

func (*Interpreter) GetGlobal

func (i *Interpreter) GetGlobal(name string) (objects.Object, bool)

GetGlobal retrieves a global variable from the interpreter. Returns the xxlang object directly (not converted to Go).

func (*Interpreter) GetGlobalAs

func (i *Interpreter) GetGlobalAs(name string) (interface{}, bool)

GetGlobalAs retrieves a global variable and converts it to a Go value. This is a convenience method that combines GetGlobal and ToGo.

func (*Interpreter) GetJITConfig added in v0.4.23

func (i *Interpreter) GetJITConfig() JITConfig

GetJITConfig returns the current JIT configuration.

func (*Interpreter) Globals

func (i *Interpreter) Globals() []objects.Object

Globals returns the interpreter's global variables slice. This is useful for advanced use cases where direct access is needed.

func (*Interpreter) JITEnabled added in v0.4.23

func (i *Interpreter) JITEnabled() bool

JITEnabled returns whether JIT compilation is enabled.

func (*Interpreter) Loader

func (i *Interpreter) Loader() *module.Loader

Loader returns the interpreter's module loader. This can be used to pre-load modules or check loaded modules.

func (*Interpreter) Reset

func (i *Interpreter) Reset()

Reset clears all state and creates a fresh interpreter. This is useful when you want to start over without creating a new instance.

func (*Interpreter) SetGlobal

func (i *Interpreter) SetGlobal(name string, value interface{}) error

SetGlobal sets a global variable in the interpreter. The value is converted from Go to xxlang using FromGo.

func (*Interpreter) SetJITConfig added in v0.4.23

func (i *Interpreter) SetJITConfig(config JITConfig)

SetJITConfig sets the JIT configuration.

func (*Interpreter) SetJITEnabled added in v0.4.23

func (i *Interpreter) SetJITEnabled(enabled bool)

SetJITEnabled enables or disables JIT compilation.

type JITConfig added in v0.4.23

type JITConfig struct {
	// Enabled controls whether JIT compilation is enabled.
	// Default is false (interpreter mode).
	Enabled bool

	// HotThreshold is the number of calls before a function is JIT compiled.
	// Default is 100.
	HotThreshold int

	// MaxCodeSize is the maximum bytecode size for JIT compilation.
	// Default is 4096 bytes.
	MaxCodeSize int

	// Debug enables JIT debug output.
	Debug bool
}

JITConfig holds JIT compiler settings for the interpreter.

type Option

type Option func(*Interpreter)

Option configures the interpreter.

func WithGlobal

func WithGlobal(name string, value objects.Object) Option

WithGlobal sets a single global variable. This is a convenience option for setting a few globals without creating a full slice.

func WithGlobalGo

func WithGlobalGo(name string, value interface{}) Option

WithGlobalGo sets a single global variable from a Go value. The value is converted using FromGo.

func WithGlobals

func WithGlobals(globals []objects.Object) Option

WithGlobals sets initial global variables. The globals slice is copied, so modifications to the original slice after calling this will not affect the interpreter.

func WithJIT added in v0.4.23

func WithJIT() Option

WithJIT enables JIT compilation for hot paths. JIT is experimental and disabled by default. Use this option to enable it for compute-intensive workloads.

func WithJITConfig added in v0.4.23

func WithJITConfig(config JITConfig) Option

WithJITConfig sets custom JIT configuration. Use this to fine-tune JIT behavior.

func WithJITDebug added in v0.4.23

func WithJITDebug() Option

WithJITDebug enables debug output for JIT compilation.

func WithJITThreshold added in v0.4.23

func WithJITThreshold(threshold int) Option

WithJITThreshold sets the hot path threshold for JIT compilation. Functions called more than this threshold will be JIT compiled. Default is 100.

func WithLoader

func WithLoader(loader *module.Loader) Option

WithLoader sets a custom module loader. This is useful for sharing modules between multiple interpreters or for pre-loading modules.

func WithStdlib

func WithStdlib() Option

WithStdlib enables standard library modules. This registers all stdlib modules (math, string, etc.) so they can be imported with `import "math"`.

Jump to

Keyboard shortcuts

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