vm

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 1, 2026 License: MIT Imports: 12 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 BuiltinFuncs = map[int]core.Value{
	7:  core.NewBuiltinFunctionValue("bool", builtinBool, 0, true),
	38: core.NewBuiltinFunctionValue("byte", builtinByte, 0, true),
	9:  core.NewBuiltinFunctionValue("rune", builtinRune, 0, true),
	6:  core.NewBuiltinFunctionValue("int", builtinInt, 0, true),
	8:  core.NewBuiltinFunctionValue("float", builtinFloat, 0, true),
	34: core.NewBuiltinFunctionValue("decimal", builtinDecimal, 0, true),
	11: core.NewBuiltinFunctionValue("time", builtinTime, 0, true),
	5:  core.NewBuiltinFunctionValue("string", builtinString, 0, true),
	36: core.NewBuiltinFunctionValue("runes", builtinRunes, 0, true),
	10: core.NewBuiltinFunctionValue("bytes", builtinBytes, 0, true),
	21: core.NewBuiltinFunctionValue("dict", builtinDict, 0, true),
	30: core.NewBuiltinFunctionValue("range", builtinRange, 2, true),
	33: core.NewBuiltinFunctionValue("error", builtinError, 0, true),

	15: core.NewBuiltinFunctionValue("is_bool", builtinIsBool, 1, false),
	39: core.NewBuiltinFunctionValue("is_byte", builtinIsByte, 1, false),
	16: core.NewBuiltinFunctionValue("is_rune", builtinIsRune, 1, false),
	12: core.NewBuiltinFunctionValue("is_int", builtinIsInt, 1, false),
	13: core.NewBuiltinFunctionValue("is_float", builtinIsFloat, 1, false),
	35: core.NewBuiltinFunctionValue("is_decimal", builtinIsDecimal, 1, false),
	23: core.NewBuiltinFunctionValue("is_time", builtinIsTime, 1, false),
	14: core.NewBuiltinFunctionValue("is_string", builtinIsString, 1, false),
	37: core.NewBuiltinFunctionValue("is_runes", builtinIsRunes, 1, false),
	17: core.NewBuiltinFunctionValue("is_bytes", builtinIsBytes, 1, false),
	18: core.NewBuiltinFunctionValue("is_array", builtinIsArray, 1, false),
	31: core.NewBuiltinFunctionValue("is_dict", builtinIsDict, 1, false),
	20: core.NewBuiltinFunctionValue("is_record", builtinIsRecord, 1, false),
	32: core.NewBuiltinFunctionValue("is_range", builtinIsRange, 1, false),
	24: core.NewBuiltinFunctionValue("is_error", builtinIsError, 1, false),

	25: core.NewBuiltinFunctionValue("is_undefined", builtinIsUndefined, 1, false),
	26: core.NewBuiltinFunctionValue("is_function", builtinIsFunction, 1, false),
	27: core.NewBuiltinFunctionValue("is_callable", builtinIsCallable, 1, false),
	22: core.NewBuiltinFunctionValue("is_iterable", builtinIsIterable, 1, false),
	19: core.NewBuiltinFunctionValue("is_immutable", builtinIsImmutable, 1, false),

	0:  core.NewBuiltinFunctionValue("len", builtinLen, 1, false),
	1:  core.NewBuiltinFunctionValue("copy", builtinCopy, 1, false),
	2:  core.NewBuiltinFunctionValue("append", builtinAppend, 2, true),
	3:  core.NewBuiltinFunctionValue("delete", builtinDelete, 2, false),
	4:  core.NewBuiltinFunctionValue("splice", builtinSplice, 1, true),
	29: core.NewBuiltinFunctionValue("format", builtinFormat, 1, true),
	28: core.NewBuiltinFunctionValue("type_name", builtinTypeName, 1, false),
}

do not change builtin function indexes as it will break compatibility 40..99 are reserved for future builtin functions

Functions

func CountObjects

func CountObjects(o core.Value) (c int)

CountObjects returns the number of objects that a given object o contains. For scalar value types, it will always be 1. For compound value types, this will include its elements and all of their elements recursively.

func FormatInstructions

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

FormatInstructions returns string representation of bytecode instructions.

func MakeInstruction

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

MakeInstruction returns a bytecode for an opcode and the operands.

Types

type Bytecode

type Bytecode struct {
	FileSet      *parser.SourceFileSet
	MainFunction *core.CompiledFunction
	Constants    []core.Value
}

Bytecode is a compiled instructions and constants.

func (*Bytecode) CountObjects

func (b *Bytecode) CountObjects() int

CountObjects returns the number of objects found in Constants.

func (*Bytecode) Decode

func (b *Bytecode) Decode(alloc *core.Arena, r io.Reader, modules *ModuleMap) error

Decode reads Bytecode data from the reader.

func (*Bytecode) Encode

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

Encode writes Bytecode data to the writer.

func (*Bytecode) FormatConstants

func (b *Bytecode) FormatConstants() (output []string)

FormatConstants returns human readable string representations of compiled constants.

func (*Bytecode) FormatInstructions

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

FormatInstructions returns human readable string representations of compiled instructions.

func (*Bytecode) RemoveDuplicates

func (b *Bytecode) RemoveDuplicates()

RemoveDuplicates finds and remove the duplicate values in Constants. Note this function mutates Bytecode.

func (*Bytecode) Size

func (b *Bytecode) Size() int64

Size of the bytecode in bytes (as much as we can calculate it without reflection and black magic)

type Importable

type Importable interface {
	// Import should return either an Object or module source code ([]byte).
	Import(alloc *core.Arena, moduleName string) (any, error)
}

Importable interface represents importable module instance.

type Module

type Module struct {
	Attrs map[string]core.Value
}

func (*Module) AsImmutableRecord

func (m *Module) AsImmutableRecord(alloc *core.Arena, moduleName string) (core.Value, error)

AsImmutableRecord converts builtin module into an immutable record.

func (*Module) Import

func (m *Module) Import(alloc *core.Arena, moduleName string) (any, error)

Import returns an immutable record for the module.

type ModuleGetter

type ModuleGetter interface {
	Get(name string) Importable
}

ModuleGetter enables implementing dynamic module loading.

type ModuleMap

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

ModuleMap represents a set of named modules. Use NewModuleMap to create a new module map.

func NewModuleMap

func NewModuleMap() *ModuleMap

NewModuleMap creates a new module map.

func (*ModuleMap) Add

func (m *ModuleMap) Add(name string, module Importable)

Add adds an import module.

func (*ModuleMap) AddBuiltinModule

func (m *ModuleMap) AddBuiltinModule(name string, attrs map[string]core.Value)

AddBuiltinModule adds a builtin module.

func (*ModuleMap) AddMap

func (m *ModuleMap) AddMap(o *ModuleMap)

AddMap adds named modules from another module map.

func (*ModuleMap) AddSourceModule

func (m *ModuleMap) AddSourceModule(name string, src []byte)

AddSourceModule adds a source module.

func (*ModuleMap) Copy

func (m *ModuleMap) Copy() *ModuleMap

Copy creates a copy of the module map.

func (*ModuleMap) Get

func (m *ModuleMap) Get(name string) Importable

Get returns an import module identified by name. It returns if the name is not found.

func (*ModuleMap) GetBuiltinModule

func (m *ModuleMap) GetBuiltinModule(name string) *Module

GetBuiltinModule returns a builtin module identified by name. It returns if the name is not found or the module is not a builtin module.

func (*ModuleMap) GetSourceModule

func (m *ModuleMap) GetSourceModule(name string) *SourceModule

GetSourceModule returns a source module identified by name. It returns if the name is not found or the module is not a source module.

func (*ModuleMap) Len

func (m *ModuleMap) Len() int

Len returns the number of named modules.

func (*ModuleMap) Remove

func (m *ModuleMap) Remove(name string)

Remove removes a named module.

type SourceModule

type SourceModule struct {
	Src []byte
}

SourceModule is an importable module that's written in Kavun.

func (*SourceModule) Import

func (m *SourceModule) Import(*core.Arena, string) (any, error)

Import returns a module source code.

type Symbol

type Symbol struct {
	Name          string
	Scope         SymbolScope
	Index         int
	LocalAssigned bool // if the local symbol is assigned at least once
}

Symbol represents a symbol in the symbol table.

type SymbolScope

type SymbolScope string

SymbolScope represents a symbol scope.

const (
	ScopeGlobal  SymbolScope = "GLOBAL"
	ScopeLocal   SymbolScope = "LOCAL"
	ScopeBuiltin SymbolScope = "BUILTIN"
	ScopeFree    SymbolScope = "FREE"
)

List of symbol scopes

type SymbolTable

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

SymbolTable represents a symbol table.

func NewSymbolTable

func NewSymbolTable() *SymbolTable

NewSymbolTable creates a SymbolTable.

func (*SymbolTable) BuiltinSymbols

func (t *SymbolTable) BuiltinSymbols() []*Symbol

BuiltinSymbols returns builtin symbols for the scope.

func (*SymbolTable) Define

func (t *SymbolTable) Define(name string) *Symbol

Define adds a new symbol in the current scope.

func (*SymbolTable) DefineBuiltin

func (t *SymbolTable) DefineBuiltin(index int, name string) *Symbol

DefineBuiltin adds a symbol for builtin function.

func (*SymbolTable) Fork

func (t *SymbolTable) Fork(block bool) *SymbolTable

Fork creates a new symbol table for a new scope.

func (*SymbolTable) FreeSymbols

func (t *SymbolTable) FreeSymbols() []*Symbol

FreeSymbols returns free symbols for the scope.

func (*SymbolTable) MaxSymbols

func (t *SymbolTable) MaxSymbols() int

MaxSymbols returns the total number of symbols defined in the scope.

func (*SymbolTable) Names

func (t *SymbolTable) Names() []string

Names returns the name of all the symbols.

func (*SymbolTable) Parent

func (t *SymbolTable) Parent(skipBlock bool) *SymbolTable

Parent returns the outer scope of the current symbol table.

func (*SymbolTable) Resolve

func (t *SymbolTable) Resolve(
	name string,
	recur bool,
) (*Symbol, int, bool)

Resolve resolves a symbol with a given name.

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) Allocator

func (v *VM) Allocator() *core.Arena

Allocator returns the allocator used by the VM.

func (*VM) Call

func (v *VM) Call(fn *core.CompiledFunction, 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 clears the remaining stack and frames to release references to heap objects and help GC. This step is not strictly necessary for correctness, but can help reduce memory usage and GC overhead when the same VM is reused for multiple runs.

func (*VM) IsStackEmpty

func (v *VM) IsStackEmpty() bool

IsStackEmpty tests if the stack is empty or not.

func (*VM) Reset added in v0.0.7

func (v *VM) Reset(alloc *core.Arena, 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