embed

package
v0.14.2 Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Overview

Package embed provides a high-level API for embedding AILANG in Go programs.

This package simplifies using AILANG as a scripting/extension language within Go applications. It handles module compilation, caching, and provides convenient value conversion between Go and AILANG types.

Basic usage:

engine := embed.New()
defer engine.Close()

// Load and call an AILANG function
result, err := engine.Call("my/module", "myFunction", 42, "hello")
if err != nil {
    log.Fatal(err)
}

// Extract Go value
value, err := embed.ToInt(result)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FromGo

func FromGo(v interface{}) (eval.Value, error)

FromGo converts a Go value to an AILANG value. Supported types:

  • nil → UnitValue
  • bool → BoolValue
  • int, int8, int16, int32, int64 → IntValue
  • uint, uint8, uint16, uint32, uint64 → IntValue
  • float32, float64 → FloatValue
  • string → StringValue
  • []byte → BytesValue
  • []T → ListValue (recursive)
  • map[string]T → RecordValue (recursive)
  • struct → RecordValue (exported fields only)

func FromGoPreserveFloats

func FromGoPreserveFloats(v interface{}) (eval.Value, error)

FromGoPreserveFloats converts a Go value to an AILANG value, preserving float64 as FloatValue even for whole numbers. Use this for direct Go calls where you want float64 to remain as float (not converted to int for JSON compatibility).

func FromJSON

func FromJSON(data []byte) (eval.Value, error)

FromJSON converts JSON bytes to an AILANG value.

func IsUnit

func IsUnit(v eval.Value) bool

IsUnit checks if a value is the unit value (equivalent to nil/void).

func ToBool

func ToBool(v eval.Value) (bool, error)

ToBool extracts a bool from an AILANG value.

func ToBytes

func ToBytes(v eval.Value) ([]byte, error)

ToBytes extracts a []byte from an AILANG value.

func ToFloat

func ToFloat(v eval.Value) (float64, error)

ToFloat extracts a float64 from an AILANG value.

func ToGo

func ToGo(v eval.Value) (interface{}, error)

ToGo converts an AILANG value to a Go value. The result type depends on the input:

  • UnitValue → nil
  • BoolValue → bool
  • IntValue → int
  • FloatValue → float64
  • StringValue → string
  • BytesValue → []byte
  • ListValue → []interface{}
  • ArrayValue → []interface{}
  • TupleValue → []interface{}
  • RecordValue → map[string]interface{}
  • TaggedValue → map[string]interface{} with "__tag" key

func ToInt

func ToInt(v eval.Value) (int, error)

ToInt extracts an int from an AILANG value.

func ToJSON

func ToJSON(v eval.Value) ([]byte, error)

ToJSON converts an AILANG value to JSON bytes.

func ToList

func ToList(v eval.Value) ([]eval.Value, error)

ToList extracts a []eval.Value from an AILANG value.

func ToRecord

func ToRecord(v eval.Value) (map[string]eval.Value, error)

ToRecord extracts a map[string]eval.Value from an AILANG value.

func ToString

func ToString(v eval.Value) (string, error)

ToString extracts a string from an AILANG value.

Types

type Engine

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

Engine manages AILANG module compilation and execution. It caches compiled modules for efficient repeated calls.

func New

func New(basePath string) *Engine

New creates a new AILANG embedding engine. basePath is the root directory for resolving module imports. The basePath should be the root of the project containing both user modules and the stdlib directory.

func (*Engine) Call

func (e *Engine) Call(modulePath, funcName string, args ...interface{}) (eval.Value, error)

Call invokes an exported function from a module with the given arguments. Arguments are automatically converted from Go types to AILANG values. The module is loaded if not already cached.

func (*Engine) CallJSON

func (e *Engine) CallJSON(modulePath, funcName string, inputJSON []byte) ([]byte, error)

CallJSON is a convenience method that accepts JSON input and returns JSON output. Useful for language-agnostic integrations.

func (*Engine) CallPreserveFloats

func (e *Engine) CallPreserveFloats(modulePath, funcName string, args ...interface{}) (eval.Value, error)

CallPreserveFloats is like Call but preserves Go float64 values as AILANG FloatValue, even for whole numbers like 100.0. Use this for direct Go calls where you need float arguments to stay as floats (not be converted to int for JSON compatibility).

func (*Engine) Close

func (e *Engine) Close() error

Close releases resources held by the engine.

func (*Engine) Eval

func (e *Engine) Eval(code string) (eval.Value, error)

Eval compiles and evaluates AILANG code directly (not from a file). Useful for one-off expressions or REPL-like functionality.

func (*Engine) GetCallValue

func (e *Engine) GetCallValue() func(eval.Value, eval.Value) (eval.Value, error)

GetCallValue returns the evaluator's CallValue function, which can be used to invoke AILANG function values from Go code (e.g., for stream event handlers). Returns a function with signature: func(fn eval.Value, arg eval.Value) (eval.Value, error)

func (*Engine) GetCallValueN

func (e *Engine) GetCallValueN() func(eval.Value, []eval.Value) (eval.Value, error)

GetCallValueN returns the evaluator's CallValueN function for multi-arg callbacks. M-ITERATIVE-LIST: Used by iterative builtins (foldl, etc.) via serve-api.

func (*Engine) HasExport

func (e *Engine) HasExport(modulePath, name string) bool

func (*Engine) InvalidateModule

func (e *Engine) InvalidateModule(modulePath string)

InvalidateModule clears all caches for a module, forcing recompilation on next call. This is used by hot reload to pick up source file changes.

func (*Engine) ListExports

func (e *Engine) ListExports(modulePath string) ([]string, error)

ListExports returns the names of all exported functions/values in a module.

func (*Engine) Load

func (e *Engine) Load(modulePath string) error

Load compiles and loads a module, caching it for future calls. modulePath is the path relative to basePath (e.g., "internal/transforms/event_formatter").

func (*Engine) PreloadModule

func (e *Engine) PreloadModule(path string, loaded *loader.LoadedModule)

HasExport checks if a module exports a value with the given name. PreloadModule preloads a compiled module into the runtime's loader cache. This ensures transitive dependencies are available when functions execute.

func (*Engine) SetEffContext

func (e *Engine) SetEffContext(ctx interface{})

SetEffContext configures the effect context for all subsequent calls. This enables effectful AILANG functions (IO, FS, Net, AI, etc.). The ctx parameter should be an *effects.EffContext; it uses interface{} to avoid an import cycle between embed and effects packages.

Jump to

Keyboard shortcuts

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