vm

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// GlobalsSize is the maximum number of global variables for a VM.
	GlobalsSize = 1024

	// DefaultStackSize is the maximum stack size for a VM.
	DefaultStackSize = 2048

	// DefaultMaxFrames is the maximum number of function frames for a VM.
	DefaultMaxFrames = 1024
)

Variables

View Source
var BuiltinFunctionNames []string
View Source
var BuiltinFunctions = make(map[string]core.Value)

Functions

func FormatInstructions

func FormatInstructions(b []byte, posOffset int) ([]string, error)

FormatInstructions returns string representation of bytecode instructions.

func MakeInstruction

func MakeInstruction(opcode opcode.Opcode, operands ...int) ([]byte, error)

MakeInstruction returns a bytecode for an opcode and the operands.

func MustFormatInstructions added in v0.3.1

func MustFormatInstructions(b []byte, posOffset int) []string

MustFormatInstructions is like FormatInstructions but panics if the instructions cannot be formatted.

func MustMakeInstruction added in v0.3.1

func MustMakeInstruction(opcode opcode.Opcode, operands ...int) []byte

MustMakeInstruction is like MakeInstruction but panics if the instruction cannot be created.

Types

type Bytecode

type Bytecode struct {
	FileSet      *parser.SourceFileSet
	MainFunction *core.CompiledFunction
	Static       core.Static
}

Bytecode is a compiled instructions and constants.

func (*Bytecode) Decode

func (b *Bytecode) Decode(r io.Reader) error

Decode reads Bytecode data from the reader. NB: files in b.FileSet.File does not have their 'set' field properly set to b.FileSet as it's private field and not serialized by gob encoder/decoder.

func (*Bytecode) Encode

func (b *Bytecode) Encode(w io.Writer) error

Encode writes Bytecode data to the writer.

func (*Bytecode) FormatInstructions

func (b *Bytecode) FormatInstructions() ([]string, error)

FormatInstructions returns human readable string representations of compiled instructions.

func (*Bytecode) FormatStatics added in v0.4.1

func (b *Bytecode) FormatStatics() (output []string, err error)

FormatStatics returns human readable string representations of compiled static values.

func (*Bytecode) MustFormatInstructions added in v0.3.1

func (b *Bytecode) MustFormatInstructions() []string

MustFormatInstructions returns human readable string representations of compiled instructions.

func (*Bytecode) MustFormatStatics added in v0.4.1

func (b *Bytecode) MustFormatStatics() []string

MustFormatStatics returns human readable string representations of compiled static values.

type VM

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

VM is a virtual machine that executes the bytecode compiled by Compiler. VM must be used in a single-threaded context only.

func NewVM

func NewVM(maxFrames int, maxStack int) *VM

NewVM creates a VM.

func (*VM) Abort

func (v *VM) Abort()

Abort aborts the execution.

func (*VM) Call

func (v *VM) Call(cfv core.Value, args []core.Value) (core.Value, error)

Call calls a compiled function with the given arguments and returns the result.

func (*VM) Clear added in v0.0.8

func (v *VM) Clear()

Clear drops the VM's Go references to bytecode, globals, and per-frame state so the Go garbage collector can reclaim them. Clear is optional and only useful when the same VM is reused for multiple runs and you want to break Go references between runs to reduce live-heap pressure.

func (*VM) IsStackEmpty

func (v *VM) IsStackEmpty() bool

IsStackEmpty tests if the stack is empty or not.

func (*VM) Recover added in v0.2.1

func (v *VM) Recover() core.Value

Recover returns the in-flight error of the surrounding "deferred-for" frame (and clears it) so the surrounding function returns normally; otherwise Undefined.

Effective only when called directly from a deferred script function. Concretely we require:

  • v.curFrame is a real compiled Kavun function (not the trampoline / not nil), and
  • v.curFrame.deferredFor is non-nil (i.e. the current frame was entered as a deferred call).

Calling recover() one level deeper (through another Kavun function invocation) does not work because OpCall resets deferredFor on the new frame. Host builtins invoked as defers also do not flip deferredFor, so recover() from such a builtin returns Undefined as well. Matches Go's "recover only in a deferred function" rule.

func (*VM) Reset added in v0.0.7

func (v *VM) Reset(bytecode *Bytecode, globals []core.Value)

Reset resets the VM state to run new main function.

func (*VM) Run

func (v *VM) Run() (err error)

Run starts the execution.

Jump to

Keyboard shortcuts

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