Documentation
¶
Overview ¶
pkg/vm/closure.go
pkg/vm/frame.go
pkg/vm/instructions.go Instruction decoder for register-based VM
pkg/vm/module.go Module loading functionality for the VM.
pkg/vm/reg_frame.go
pkg/vm/reg_vm.go Register-based Virtual Machine implementation
pkg/vm/runcode.go Support for dynamic code execution via runCode builtin
pkg/vm/stack.go
pkg/vm/value.go NaN Boxing implementation for efficient value representation
NaN boxing uses the IEEE 754 NaN representation to encode multiple types in a single 64-bit value without heap allocation.
Memory layout: - Floats: stored as native IEEE 754 double (no NaN boxing needed) - Integers: stored in NaN payload (48 bits) - Booleans: stored in NaN payload - Null: special NaN value - Objects: stored as pointer in NaN payload
IMPORTANT: For boxed objects, we maintain a global registry to ensure GC visibility. The Value stores an index into this registry.
pkg/vm/value_frame.go Value-based frame for zero-allocation local variable access
pkg/vm/value_ops.go Value-based execution methods for hot path optimization These methods work directly with Value type, avoiding heap allocation
pkg/vm/value_stack.go Value-based stack implementation optimized for NaN-boxed values
pkg/vm/value_vm.go Value-optimized VM execution for hot paths
pkg/vm/vm.go
Index ¶
- Constants
- Variables
- func ClearRegistry()
- func DecodeBuiltin(code []byte, ip int) (builtinIdx byte, numArgs byte)
- func DecodeCall(code []byte, ip int) (funcReg byte, numArgs byte)
- func DecodeClosure(code []byte, ip int) (dstReg byte, funcIdx int, numFree byte)
- func DecodeConst(code []byte, ip int) (reg byte, constIdx int)
- func DecodeField(code []byte, ip int) (reg byte, objReg byte, nameIdx int)
- func DecodeJump(code []byte, ip int) (offset int)
- func DecodeJumpCond(code []byte, ip int) (condReg byte, offset int)
- func DecodeMethodCall(code []byte, ip int) (objReg byte, nameIdx int, numArgs byte)
- func DecodeReg1(code []byte, ip int) (reg byte)
- func DecodeReg2(code []byte, ip int) (dst, src byte)
- func DecodeReg3(code []byte, ip int) (dst, src1, src2 byte)
- func ExecuteRunCode(code string, args *objects.Map) (objects.Object, error)
- func InstructionLen(op compiler.Opcode) int
- func IsRegisterOpcode(op compiler.Opcode) bool
- func RegistryStats() (total, used int)
- func RunCodeInRegVM(code string, args *objects.Map, regVM *RegVM) (objects.Object, error)
- func RunCodeInVM(code string, args *objects.Map, vm *VM) (objects.Object, error)
- func SetRunCodeCallback(fn RunCodeFunc)
- type Closure
- type ExceptionHandler
- type Frame
- type InlineCacheEntry
- type InstructionInfo
- type RegFrame
- func (f *RegFrame) ClearArgRegisters()
- func (f *RegFrame) CopyArgRegisters(src *RegFrame, numArgs int)
- func (f *RegFrame) GetConstant(idx int) Value
- func (f *RegFrame) GetFree(idx int) Value
- func (f *RegFrame) GetGlobal(idx int) Value
- func (f *RegFrame) GetLocal(idx int) Value
- func (f *RegFrame) GetReg(reg int) Value
- func (f *RegFrame) Instructions() []byte
- func (f *RegFrame) Release()
- func (f *RegFrame) SetFree(idx int, val Value)
- func (f *RegFrame) SetGlobal(idx int, val Value)
- func (f *RegFrame) SetLocal(idx int, val Value)
- func (f *RegFrame) SetReg(reg int, val Value)
- type RegVM
- func (vm *RegVM) GetCallStack() string
- func (vm *RegVM) Globals() []Value
- func (vm *RegVM) GlobalsAsObjects() []objects.Object
- func (vm *RegVM) LastPopped() Value
- func (vm *RegVM) LastPoppedObject() objects.Object
- func (vm *RegVM) LastResult() Value
- func (vm *RegVM) Run() error
- func (vm *RegVM) SetCurrentModule(mod *objects.Module)
- func (vm *RegVM) SetLoader(loader *module.Loader)
- func (vm *RegVM) SetSourcePath(path string)
- func (vm *RegVM) StackTop() Value
- type RunCodeFunc
- type Stack
- func (s *Stack) LastPopped() objects.Object
- func (s *Stack) Len() int
- func (s *Stack) Peek(n int) objects.Object
- func (s *Stack) Pop() objects.Object
- func (s *Stack) PopSkipGC() objects.Object
- func (s *Stack) Push(obj objects.Object) error
- func (s *Stack) SetTop(obj objects.Object)
- func (s *Stack) Top() objects.Object
- type VM
- func (vm *VM) ExecuteValueOp(op compiler.Opcode) (bool, error)
- func (vm *VM) GetCallStack() string
- func (vm *VM) Globals() []objects.Object
- func (vm *VM) LastPopped() objects.Object
- func (vm *VM) Run() error
- func (vm *VM) SetCurrentModule(mod *objects.Module)
- func (vm *VM) SetLoader(loader *module.Loader)
- func (vm *VM) SetSourcePath(path string)
- func (vm *VM) StackTop() objects.Object
- type Value
- func (v Value) Add(other Value) (Value, bool)
- func (v Value) Div(other Value) (Value, bool)
- func (v Value) Equal(other Value) (bool, bool)
- func (v Value) EqualValue(other Value) Value
- func (v Value) GetBool() bool
- func (v Value) GetFloat() float64
- func (v Value) GetInt() int64
- func (v Value) GetObject() objects.Object
- func (v Value) Greater(other Value) (bool, bool)
- func (v Value) GreaterEqual(other Value) Value
- func (v Value) GreaterValue(other Value) Value
- func (v Value) IsBool() bool
- func (v Value) IsFloat() bool
- func (v Value) IsInt() bool
- func (v Value) IsNull() bool
- func (v Value) IsNumber() bool
- func (v Value) IsObject() bool
- func (v Value) IsTruthy() bool
- func (v Value) Less(other Value) (bool, bool)
- func (v Value) LessEqual(other Value) Value
- func (v Value) LessValue(other Value) Value
- func (v Value) Mod(other Value) (Value, bool)
- func (v Value) Mul(other Value) (Value, bool)
- func (v Value) Neg() (Value, bool)
- func (v Value) NotEqual(other Value) (bool, bool)
- func (v Value) NotEqualValue(other Value) Value
- func (v Value) String() string
- func (v Value) Sub(other Value) (Value, bool)
- func (v Value) ToFloat() (float64, bool)
- func (v Value) ToInt() (int64, bool)
- func (v Value) ToObject() objects.Object
- type ValueFrame
- type ValueStack
- func (s *ValueStack) Drop(n int)
- func (s *ValueStack) Dup() error
- func (s *ValueStack) Get(i int) Value
- func (s *ValueStack) LastPopped() Value
- func (s *ValueStack) Len() int
- func (s *ValueStack) MustPush(v Value)
- func (s *ValueStack) Peek(n int) Value
- func (s *ValueStack) Pop() Value
- func (s *ValueStack) PopNoClear() Value
- func (s *ValueStack) Push(v Value) error
- func (s *ValueStack) Reset()
- func (s *ValueStack) Rot3()
- func (s *ValueStack) Set(i int, v Value)
- func (s *ValueStack) SetTop(v Value)
- func (s *ValueStack) Swap()
- func (s *ValueStack) Top() Value
- type ValueVM
Constants ¶
const ( GlobalsSize = 65536 MaxFrames = 1024 )
Constants
const InlineCacheSize = 256
InlineCacheSize is the size of the method cache
const StackSize = 2048
StackSize is the maximum number of elements on the stack
const ValueStackSize = 2048
ValueStackSize is the maximum number of elements on the stack
Variables ¶
var ( ValueNull = Value(tagNullValue) ValueTrue = Value(tagBoolValue | 1) ValueFalse = Value(tagBoolValue) )
Special values - pre-computed for fast access
Functions ¶
func ClearRegistry ¶ added in v0.4.22
func ClearRegistry()
ClearRegistry clears the global object registry This should be called when starting a new execution context
func DecodeBuiltin ¶ added in v0.4.21
DecodeBuiltin decodes a builtin call instruction
func DecodeCall ¶ added in v0.4.21
DecodeCall decodes a call instruction
func DecodeClosure ¶ added in v0.4.21
DecodeClosure decodes a closure creation instruction
func DecodeConst ¶ added in v0.4.21
DecodeConst decodes an instruction with register and constant index
func DecodeField ¶ added in v0.4.21
DecodeField decodes a field access instruction
func DecodeJump ¶ added in v0.4.21
DecodeJump decodes a jump instruction with signed 16-bit offset
func DecodeJumpCond ¶ added in v0.4.21
DecodeJumpCond decodes a conditional jump instruction with signed 16-bit offset
func DecodeMethodCall ¶ added in v0.4.21
DecodeMethodCall decodes a method call instruction
func DecodeReg1 ¶ added in v0.4.21
DecodeReg1 decodes a 1-operand register instruction: reg
func DecodeReg2 ¶ added in v0.4.21
DecodeReg2 decodes a 2-operand register instruction: dst, src
func DecodeReg3 ¶ added in v0.4.21
DecodeReg3 decodes a 3-operand register instruction: dst, src1, src2
func ExecuteRunCode ¶ added in v0.4.19
ExecuteRunCode is called by the runCode builtin
func InstructionLen ¶ added in v0.4.21
InstructionLen returns the length of an instruction in bytes
func IsRegisterOpcode ¶ added in v0.4.21
IsRegisterOpcode checks if an opcode is a register-based operation
func RegistryStats ¶ added in v0.4.22
func RegistryStats() (total, used int)
RegistryStats returns statistics about the object registry
func RunCodeInRegVM ¶ added in v0.4.21
RunCodeInRegVM executes code in the register VM context
func RunCodeInVM ¶ added in v0.4.19
RunCodeInVM executes code in the context of the given VM
func SetRunCodeCallback ¶ added in v0.4.19
func SetRunCodeCallback(fn RunCodeFunc)
SetRunCodeCallback registers the callback for runCode builtin
Types ¶
type Closure ¶
type Closure struct {
Fn *compiler.CompiledFunction
FreeVars []objects.Object // Free variables for stack VM
Constants []objects.Object // Constants from the creating VM
Globals []objects.Object // Globals from the creating module (for exported functions)
// FreeVarsValues is used by register VM for shared mutable free variables
// When set, register VM uses this instead of FreeVars
FreeVarsValues []Value
}
Closure represents a function with captured variables
type ExceptionHandler ¶ added in v0.4.19
type ExceptionHandler struct {
// contains filtered or unexported fields
}
ExceptionHandler represents a try-catch-finally handler
type Frame ¶
type Frame struct {
Fn *compiler.CompiledFunction
IP int // Instruction pointer (index into Instructions)
BasePointer int // Stack base pointer for this frame
Locals []objects.Object // Local variables
FreeVars []objects.Object // Free variables (captured from closure)
Constants []objects.Object // Constants for this frame (from closure or VM)
Globals []objects.Object // Globals for this frame (from closure's module or VM)
This objects.Object // 'this' for method calls
}
Frame represents a call frame for function execution
func FrameFromValueFrame ¶ added in v0.4.21
func FrameFromValueFrame(vf *ValueFrame) *Frame
FrameFromValueFrame converts a ValueFrame to a regular Frame Used when falling back to Object-based execution
func NewFrame ¶
func NewFrame(fn *compiler.CompiledFunction, basePointer int) *Frame
NewFrame creates a new call frame
func (*Frame) Instructions ¶
Instructions returns the compiled function's instructions
type InlineCacheEntry ¶
InlineCacheEntry represents a cached method lookup
type InstructionInfo ¶ added in v0.4.21
type InstructionInfo struct {
Opcode compiler.Opcode
Dst byte
Src1 byte
Src2 byte
ConstIdx int
Offset int
NumArgs byte
}
InstructionInfo holds decoded instruction information
func DecodeInstruction ¶ added in v0.4.21
func DecodeInstruction(code []byte, ip int) (InstructionInfo, int)
DecodeInstruction decodes a single instruction from bytecode Returns the instruction info and the number of bytes consumed
type RegFrame ¶ added in v0.4.21
type RegFrame struct {
Fn *compiler.CompiledFunction
IP int // Instruction pointer
Registers [compiler.NumRegisters]Value // Fixed-size register array
FreeVars []Value // Closure free variables
Constants []Value // Constants for this frame
Globals []Value // Global variables reference
This Value // 'this' for method calls
Locals []Value // Local variables (for spilled values)
}
RegFrame represents a call frame for the register-based VM Each frame has 256 fixed registers (R0-R255)
func NewRegFrame ¶ added in v0.4.21
func NewRegFrame(fn *compiler.CompiledFunction) *RegFrame
NewRegFrame creates a new register-based call frame
func (*RegFrame) ClearArgRegisters ¶ added in v0.4.21
func (f *RegFrame) ClearArgRegisters()
ClearArgRegisters clears the argument registers
func (*RegFrame) CopyArgRegisters ¶ added in v0.4.21
CopyArgRegisters copies argument values from source frame's return registers to this frame's argument registers (R0-R7)
func (*RegFrame) GetConstant ¶ added in v0.4.21
GetConstant returns a constant value
func (*RegFrame) Instructions ¶ added in v0.4.21
Instructions returns the compiled function's instructions
func (*RegFrame) Release ¶ added in v0.4.21
func (f *RegFrame) Release()
Release returns the frame to the pool for reuse
type RegVM ¶ added in v0.4.21
type RegVM struct {
// contains filtered or unexported fields
}
RegVM is a register-based virtual machine
func NewRegVMWithGlobals ¶ added in v0.4.21
NewRegVMWithGlobals creates a register VM with custom globals
func NewRegVMWithObjectGlobals ¶ added in v0.4.21
NewRegVMWithObjectGlobals creates a register VM with globals as objects.Object
func (*RegVM) GetCallStack ¶ added in v0.4.21
GetCallStack returns the current call stack
func (*RegVM) GlobalsAsObjects ¶ added in v0.4.21
GlobalsAsObjects returns the globals as objects.Object slice
func (*RegVM) LastPopped ¶ added in v0.4.21
LastPopped returns the last popped value from temp stack Note: For register VM, prefer LastResult() which returns from ReturnRegister
func (*RegVM) LastPoppedObject ¶ added in v0.4.21
LastPoppedObject returns the result as objects.Object For register VM, this returns the value from ReturnRegister
func (*RegVM) LastResult ¶ added in v0.4.21
LastResult returns the value in the ReturnRegister This is the preferred method for getting results from the register VM
func (*RegVM) SetCurrentModule ¶ added in v0.4.21
SetCurrentModule sets the current module context
func (*RegVM) SetSourcePath ¶ added in v0.4.21
SetSourcePath sets the source file path
type RunCodeFunc ¶ added in v0.4.19
RunCodeFunc is the signature for the runCode callback
func GetRunCodeCallback ¶ added in v0.4.19
func GetRunCodeCallback() RunCodeFunc
GetRunCodeCallback returns the current callback
type Stack ¶
type Stack struct {
// contains filtered or unexported fields
}
Stack represents the VM operand stack
func (*Stack) LastPopped ¶
LastPopped returns the last popped element
func (*Stack) Peek ¶
Peek returns the n-th element from the top (0 = top, 1 = second from top, etc.)
func (*Stack) Pop ¶
Pop pops an object from the stack Optimized: skip nil check when stack is known to be non-empty
func (*Stack) PopSkipGC ¶
PopSkipGC pops an object without clearing the reference (faster, but may delay GC)
func (*Stack) Push ¶
Push pushes an object onto the stack Optimized: bounds check only when near limit
type VM ¶
type VM struct {
// contains filtered or unexported fields
}
VM is the virtual machine that executes bytecode
func NewWithGlobalsStore ¶
NewWithGlobalsStore creates a new VM with a custom globals store
func (*VM) ExecuteValueOp ¶ added in v0.4.21
ExecuteValueOp executes a Value-based opcode Returns true if the opcode was handled, false to fall back to Object path
func (*VM) GetCallStack ¶
GetCallStack returns the current call stack as a formatted string
func (*VM) LastPopped ¶
LastPopped returns the last popped element from the stack
func (*VM) Run ¶
Run executes the bytecode Optimized: caches frame pointer in local variable to reduce method calls
func (*VM) SetCurrentModule ¶
SetCurrentModule sets the current module context
func (*VM) SetSourcePath ¶
SetSourcePath sets the source file path for module resolution
type Value ¶ added in v0.4.21
type Value uint64
Value is a NaN-boxed value that can represent multiple types efficiently
func NewFloat ¶ added in v0.4.21
NewFloat creates a Value from a float64 Normal floats are stored directly as IEEE 754 doubles
func NewInt ¶ added in v0.4.21
NewInt creates a Value from an integer Stores up to 48 bits of integer data
func (Value) EqualValue ¶ added in v0.4.21
EqualValue returns a Value representing equality comparison
func (Value) GreaterEqual ¶ added in v0.4.21
GreaterEqual compares if v >= other
func (Value) GreaterValue ¶ added in v0.4.21
GreaterValue returns a Value representing greater than comparison
func (Value) IsNumber ¶ added in v0.4.21
IsNumber returns true if the value is a number (int or float)
func (Value) IsObject ¶ added in v0.4.21
IsObject returns true if the value is a tagged object pointer
func (Value) LessValue ¶ added in v0.4.21
LessValue returns a Value representing less than comparison
func (Value) NotEqualValue ¶ added in v0.4.21
NotEqualValue returns a Value representing inequality comparison
type ValueFrame ¶ added in v0.4.21
type ValueFrame struct {
Fn *compiler.CompiledFunction
IP int // Instruction pointer
BasePointer int // Stack base pointer
Locals []Value // Value-based local variables
FreeVars []Value // Value-based free variables
Constants []Value // Value-based constants
Globals []objects.Object
This objects.Object
}
ValueFrame represents a call frame optimized for Value-based execution Uses Value type for locals to avoid heap allocation for primitives
func NewValueFrame ¶ added in v0.4.21
func NewValueFrame(fn *compiler.CompiledFunction, basePointer int) *ValueFrame
NewValueFrame creates a new Value-based call frame
func ValueFrameFromFrame ¶ added in v0.4.21
func ValueFrameFromFrame(of *Frame) *ValueFrame
ValueFrameFromFrame converts a regular Frame to a ValueFrame
func (*ValueFrame) GetFreeVar ¶ added in v0.4.21
func (f *ValueFrame) GetFreeVar(idx int) Value
GetFreeVar gets a free variable as Value
func (*ValueFrame) GetLocal ¶ added in v0.4.21
func (f *ValueFrame) GetLocal(idx int) Value
GetLocal gets a local variable as Value
func (*ValueFrame) Instructions ¶ added in v0.4.21
func (f *ValueFrame) Instructions() []byte
Instructions returns the compiled function's instructions
func (*ValueFrame) Release ¶ added in v0.4.21
func (f *ValueFrame) Release()
Release returns the frame to the pool
func (*ValueFrame) SetFreeVar ¶ added in v0.4.21
func (f *ValueFrame) SetFreeVar(idx int, v Value)
SetFreeVar sets a free variable
func (*ValueFrame) SetLocal ¶ added in v0.4.21
func (f *ValueFrame) SetLocal(idx int, v Value)
SetLocal sets a local variable
type ValueStack ¶ added in v0.4.21
type ValueStack struct {
// contains filtered or unexported fields
}
ValueStack represents the VM operand stack using NaN-boxed values
func NewValueStack ¶ added in v0.4.21
func NewValueStack() *ValueStack
NewValueStack creates a new value stack
func (*ValueStack) Drop ¶ added in v0.4.21
func (s *ValueStack) Drop(n int)
Drop removes n elements from the top
func (*ValueStack) Dup ¶ added in v0.4.21
func (s *ValueStack) Dup() error
Dup duplicates the top element
func (*ValueStack) Get ¶ added in v0.4.21
func (s *ValueStack) Get(i int) Value
Get returns the value at index i
func (*ValueStack) LastPopped ¶ added in v0.4.21
func (s *ValueStack) LastPopped() Value
LastPopped returns the last popped element
func (*ValueStack) Len ¶ added in v0.4.21
func (s *ValueStack) Len() int
Len returns the number of elements on the stack
func (*ValueStack) MustPush ¶ added in v0.4.21
func (s *ValueStack) MustPush(v Value)
MustPush pushes without error checking (for hot paths)
func (*ValueStack) Peek ¶ added in v0.4.21
func (s *ValueStack) Peek(n int) Value
Peek returns the n-th element from the top (0 = top)
func (*ValueStack) Pop ¶ added in v0.4.21
func (s *ValueStack) Pop() Value
Pop pops a value from the stack
func (*ValueStack) PopNoClear ¶ added in v0.4.21
func (s *ValueStack) PopNoClear() Value
PopNoClear pops without clearing (slightly faster, delays GC)
func (*ValueStack) Push ¶ added in v0.4.21
func (s *ValueStack) Push(v Value) error
Push pushes a value onto the stack Inlined in hot paths for performance
func (*ValueStack) Rot3 ¶ added in v0.4.21
func (s *ValueStack) Rot3()
Rot3 rotates the top 3 elements: [a, b, c] -> [b, c, a]
func (*ValueStack) Set ¶ added in v0.4.21
func (s *ValueStack) Set(i int, v Value)
Set sets the value at index i
func (*ValueStack) SetTop ¶ added in v0.4.21
func (s *ValueStack) SetTop(v Value)
SetTop sets the top element
func (*ValueStack) Swap ¶ added in v0.4.21
func (s *ValueStack) Swap()
Swap swaps the top two elements
func (*ValueStack) Top ¶ added in v0.4.21
func (s *ValueStack) Top() Value
Top returns the top element without removing it
type ValueVM ¶ added in v0.4.21
type ValueVM struct {
// contains filtered or unexported fields
}
ValueVM is a lightweight VM optimized for numeric operations using Value type It's designed for hot loops and arithmetic-heavy code
func NewValueVM ¶ added in v0.4.21
NewValueVM creates a Value-optimized VM from compiled bytecode
func (*ValueVM) ResultObject ¶ added in v0.4.21
ResultObject returns the result as an Object