engine

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: GPL-3.0 Imports: 3 Imported by: 0

Documentation

Overview

Package engine defines the Engine and Session interfaces that all scripting engine implementations must satisfy, along with the engine registry.

Concurrency model: a Session is NOT safe for concurrent use from multiple goroutines. The pool (internal/interp/pool) ensures each Session is owned by exactly one goroutine at a time. Parallelism is achieved by maintaining N sessions across N pool workers — no session is shared.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Register

func Register(name string, eng Engine)

Register registers eng under name. Called from engine init() functions. Panics on duplicate.

Types

type CallParams

type CallParams struct {
	Fn Value
}

CallParams configures a Session.Call invocation. Fn is the compiled value to run; the buzz engine ignores arguments and return-value counts (callers that need them use the concrete Session.CallValue), so only Fn is carried here.

type DebugReader

type DebugReader interface {
	// Frames walks the active call stack from innermost to outermost,
	// skipping host (Go/C) frames. The slice is fresh on every call.
	Frames() []Frame

	// Locals returns the named locals at the frame indicated by level
	// (0 = innermost). Returns an empty map if the level is out of range.
	Locals(level int) map[string]Value

	// Upvalues returns the captured upvalues at the frame indicated by level.
	// Returns an empty map if the function has no upvalues or the level is
	// out of range.
	Upvalues(level int) map[string]Value

	// CallDepth reports the number of active call frames. Used by step-over
	// to know when a frame boundary is crossed.
	CallDepth() int
}

DebugReader is an optional interface that sessions may implement to expose call-stack introspection. magus.pry() type-asserts the session to DebugReader at call time; sessions that don't implement it report no frames.

type DriversProvider

type DriversProvider interface {
	Drivers() []ReplDriver
}

DriversProvider is an optional interface for sessions that expose language-specific REPL drivers. The shared REPL calls Drivers() to get the available drivers and picks the appropriate one for each language mode.

type Engine

type Engine interface {
	// NewSession returns a fresh session with standard libraries loaded
	// and ctx bound for cancellation. Pass context.Background() for no cancellation.
	NewSession(ctx context.Context) (Session, error)
}

Engine creates Sessions for script execution. Implementations register themselves at init() time via Register.

func Lookup

func Lookup(name string) Engine

Lookup returns the engine registered under name, or nil if not found.

type Frame

type Frame struct {
	Source      string
	ShortSrc    string
	CurrentLine int
	Name        string // function name when discoverable
	What        string // engine-reported frame kind (e.g. "main", "tail")
}

Frame describes one entry on a scripting engine's call stack as reported by the engine's debug API. Source is the chunk name as the engine reports it (e.g. "@magusfile.buzz"); ShortSrc is a truncated form suitable for display; CurrentLine is the 1-based line number, or -1 if unknown.

type ReplDriver

type ReplDriver interface {
	// Language returns the driver's name (e.g. "buzz").
	Language() string

	// EvalLine evaluates snippet. Implementations should first try
	// "return "+snippet so that bare expressions print a result; if that
	// fails with a syntax error they should fall back to running snippet
	// as a statement. Returns (nil, nil) if execution succeeded with no
	// printable value. Multiple return values are supported.
	EvalLine(snippet string) ([]Value, error)

	// IsIncomplete returns true when err indicates that snippet is a partial
	// statement that needs more input (e.g. an unexpected end-of-input error).
	// The REPL accumulates additional lines when this returns true.
	IsIncomplete(err error) bool

	// LineDelta returns the net open-block delta for this line of source (e.g.
	// bracket-depth counting for JS). Engines that rely on error-based
	// continuation instead return 0 always. The REPL buffers more input while
	// the cumulative delta is positive.
	LineDelta(line string) int

	// HostBindingNames returns the names injected by the host runtime so the
	// REPL can omit them from .globals output.
	HostBindingNames() []string

	// UserGlobals returns the current user-defined globals with host bindings
	// filtered out. Returns nil when the engine does not support listing globals.
	UserGlobals() map[string]Value
}

ReplDriver is an optional interface sessions expose to allow the shared REPL to evaluate snippets, detect partial-input continuation, and filter host-injected globals — without knowing the engine's surface language.

Each engine exposes one driver per language it speaks; the REPL switches between drivers by language.

type Session

type Session interface {
	Close() error

	SetGlobal(name string, v Value)
	GetGlobal(name string) Value

	NewTable() Table
	LoadString(code string) (Value, error)
	DoString(code string) error
	Call(p CallParams) error
}

Session is a single isolated script-execution context. A Session is NOT safe for concurrent use from multiple goroutines; the pool ensures each Session is owned by exactly one goroutine at a time.

type StepEvent

type StepEvent int

StepEvent identifies which engine event fired a step hook.

const (
	StepLine StepEvent = iota
	StepCall
	StepReturn
)

type StepMask

type StepMask int

StepMask selects which engine events the step hook subscribes to. Combine values with bitwise OR.

const (
	MaskLine StepMask = 1 << iota
	MaskCall
	MaskReturn
)

type Stepper

type Stepper interface {
	SetStepHook(mask StepMask, cb func(StepEvent, Frame))
	ClearStepHook()
}

Stepper is an optional interface for sessions that support line-level step hooks: source-level instrumentation that injects hook trampolines before each statement at compile time, firing a callback as execution crosses statements.

SetStepHook installs cb to fire on the next event matching mask. cb is invoked synchronously on the script execution goroutine and may re-enter the pry REPL (the pry loop is re-entrant). After cb returns, execution resumes. ClearStepHook removes any installed hook.

type Table

type Table interface {
	Value
	RawSetString(key string, v Value)
	RawGetString(key string) Value
	RawSetInt(key int, v Value)
	RawGetInt(key int) Value
	ForEach(fn func(k, v Value))
	Len() int
}

Table is a script-side key-value collection. Constructed via Session.NewTable().

type Value

type Value interface {
	IsNil() bool
	String() string
	AsString() (string, bool)
	AsNumber() (float64, bool)
	AsBool() bool
	AsTable() (Table, bool)
	AsFunction() (Value, bool)
}

Value is a script-side value handle. Engine implementations choose their own concrete representation; callers use the methods below for type-safe access.

var NilValue Value = nilVal{}

NilValue is the nil Value.

func BoolValue

func BoolValue(b bool) Value

BoolValue wraps b as a boolean Value.

func NumberValue

func NumberValue(n float64) Value

NumberValue wraps n as a numeric Value.

func StringValue

func StringValue(s string) Value

StringValue wraps s as a string Value.

Directories

Path Synopsis
Package buzz adapts the standalone Buzz interpreter (magus/gopherbuzz) to magus's engine.Engine/engine.Session interfaces and registers it under the "buzz" key.
Package buzz adapts the standalone Buzz interpreter (magus/gopherbuzz) to magus's engine.Engine/engine.Session interfaces and registers it under the "buzz" key.

Jump to

Keyboard shortcuts

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