vm

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package vm executes compiled Kukel programs on a stack machine.

Each call has a frame. A frame's slots (parameters, temporaries, locals) sit on the value stack at frame.base, followed by its operand stack. The callee value sits just below the slots.

Index

Constants

View Source
const MaxFrames = 10000

MaxFrames bounds the call depth; deeper recursion is a stack overflow.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builtins

type Builtins func(name string) (*value.BuiltinObj, bool)

Builtins resolves built-in functions by name.

type RuntimeError

type RuntimeError struct {
	Value value.Value  // the thrown value (an Error object for runtime errors)
	Kind  string       // the kind of an Error object; "" for other values
	Msg   string       // the message of an Error object, or the value as text
	Trace []TraceEntry // innermost call first
	Fatal bool         // try cannot catch it (exit, the step limit)
	Exit  *int         // set by exit(code): not an error, the program ends
}

RuntimeError is a thrown exception: the thrown value and the stack trace at the throw. It is what Run returns for an exception nobody caught.

func (*RuntimeError) Error

func (e *RuntimeError) Error() string

Error returns the message and the stack trace. Long traces (deep recursion) show only the first and last traceEnds calls.

type TraceEntry

type TraceEntry struct {
	Func string
	File string
	Line int
}

TraceEntry is one line of a stack trace.

type VM

type VM struct {

	// StepLimit, when positive, stops the program with a fatal error after
	// that many backward jumps, calls of Kukel functions and thrown
	// exceptions (for tests that run untrusted code).
	StepLimit int
	// contains filtered or unexported fields
}

VM runs one program.

func New

func New(prog *bytecode.Program, builtins Builtins, stdout io.Writer, stdin io.Reader) (*VM, error)

New prepares prog for running: it binds the functions and built-ins to their global slots.

func (*VM) Call

func (vm *VM) Call(fn value.Value, args []value.Value) (value.Value, error)

Call calls fn from a built-in (sort's comparator, for example). The built-in's own arguments stay on the stack below the new call.

func (*VM) Rand

func (vm *VM) Rand() *rand.Rand

func (*VM) Run

func (vm *VM) Run(args []string) (int, error)

Run runs the global initializers, then main(args). It returns main's result as the exit code (an int result; anything else is 0).

func (*VM) SetStderr

func (vm *VM) SetStderr(w io.Writer)

SetStderr sets where eprint writes.

func (*VM) Started

func (vm *VM) Started() time.Time

func (*VM) Stderr

func (vm *VM) Stderr() io.Writer

func (*VM) Stdin

func (vm *VM) Stdin() *bufio.Reader

func (*VM) Stdout

func (vm *VM) Stdout() io.Writer

Stdout, Stdin and Call implement value.Env for the built-ins.

Jump to

Keyboard shortcuts

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