vm

package
v0.4.20 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Overview

pkg/vm/closure.go

pkg/vm/frame.go

pkg/vm/module.go Module loading functionality for the VM.

pkg/vm/runcode.go Support for dynamic code execution via runCode builtin

pkg/vm/stack.go

pkg/vm/vm.go

Index

Constants

View Source
const (
	GlobalsSize = 65536
	MaxFrames   = 1024
)

Constants

View Source
const InlineCacheSize = 256

InlineCacheSize is the size of the method cache

View Source
const StackSize = 2048

StackSize is the maximum number of elements on the stack

Variables

This section is empty.

Functions

func ExecuteRunCode added in v0.4.19

func ExecuteRunCode(code string, args *objects.Map) (objects.Object, error)

ExecuteRunCode is called by the runCode builtin

func RunCodeInVM added in v0.4.19

func RunCodeInVM(code string, args *objects.Map, vm *VM) (objects.Object, error)

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
	Constants []objects.Object // Constants from the creating VM
	Globals   []objects.Object // Globals from the creating module (for exported functions)
}

Closure represents a function with captured variables

func (*Closure) HashKey

func (c *Closure) HashKey() objects.HashKey

HashKey returns the hash key

func (*Closure) Inspect

func (c *Closure) Inspect() string

Inspect returns a string representation

func (*Closure) ToBool

func (c *Closure) ToBool() *objects.Bool

ToBool returns the boolean value

func (*Closure) Type

func (c *Closure) Type() objects.ObjectType

Type returns the object type

func (*Closure) TypeTag

func (c *Closure) TypeTag() objects.TypeTag

TypeTag returns the type tag for fast type checking

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 NewFrame

func NewFrame(fn *compiler.CompiledFunction, basePointer int) *Frame

NewFrame creates a new call frame

func (*Frame) Instructions

func (f *Frame) Instructions() []byte

Instructions returns the compiled function's instructions

func (*Frame) Release

func (f *Frame) Release()

Release returns the frame to the pool for reuse

type InlineCacheEntry

type InlineCacheEntry struct {
	Class  *objects.Class
	Method objects.Object
}

InlineCacheEntry represents a cached method lookup

type RunCodeFunc added in v0.4.19

type RunCodeFunc func(code string, args *objects.Map) (objects.Object, error)

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 NewStack

func NewStack() *Stack

NewStack creates a new stack with the default size

func (*Stack) LastPopped

func (s *Stack) LastPopped() objects.Object

LastPopped returns the last popped element

func (*Stack) Len

func (s *Stack) Len() int

Len returns the number of elements on the stack

func (*Stack) Peek

func (s *Stack) Peek(n int) objects.Object

Peek returns the n-th element from the top (0 = top, 1 = second from top, etc.)

func (*Stack) Pop

func (s *Stack) Pop() objects.Object

Pop pops an object from the stack Optimized: skip nil check when stack is known to be non-empty

func (*Stack) PopSkipGC

func (s *Stack) PopSkipGC() objects.Object

PopSkipGC pops an object without clearing the reference (faster, but may delay GC)

func (*Stack) Push

func (s *Stack) Push(obj objects.Object) error

Push pushes an object onto the stack Optimized: bounds check only when near limit

func (*Stack) SetTop

func (s *Stack) SetTop(obj objects.Object)

SetTop sets the top element without changing stack pointer

func (*Stack) Top

func (s *Stack) Top() objects.Object

Top returns the top element without removing it

type VM

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

VM is the virtual machine that executes bytecode

func New

func New(bytecode *compiler.Bytecode) *VM

New creates a new VM with the given bytecode

func NewWithGlobalsStore

func NewWithGlobalsStore(bytecode *compiler.Bytecode, globals []objects.Object) *VM

NewWithGlobalsStore creates a new VM with a custom globals store

func (*VM) GetCallStack

func (vm *VM) GetCallStack() string

GetCallStack returns the current call stack as a formatted string

func (*VM) Globals

func (vm *VM) Globals() []objects.Object

Globals returns the globals array

func (*VM) LastPopped

func (vm *VM) LastPopped() objects.Object

LastPopped returns the last popped element from the stack

func (*VM) Run

func (vm *VM) Run() error

Run executes the bytecode Optimized: caches frame pointer in local variable to reduce method calls

func (*VM) SetCurrentModule

func (vm *VM) SetCurrentModule(mod *objects.Module)

SetCurrentModule sets the current module context

func (*VM) SetLoader

func (vm *VM) SetLoader(loader *module.Loader)

SetLoader sets the module loader (for sharing between VMs)

func (*VM) SetSourcePath

func (vm *VM) SetSourcePath(path string)

SetSourcePath sets the source file path for module resolution

func (*VM) StackTop

func (vm *VM) StackTop() objects.Object

StackTop returns the top of the stack

Jump to

Keyboard shortcuts

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