vm

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2025 License: MIT Imports: 29 Imported by: 0

Documentation

Overview

Package vm contains the Elk Virtual Machine. It interprets Elk Bytecode produced by the Elk compiler.

Package vm contains the Elk Virtual Machine. It interprets Elk Bytecode produced by the Elk compiler.

Index

Constants

View Source
const (
	UpvalueLongIndexFlag bitfield.BitFlag8 = 1 << iota
	UpvalueLocalFlag
)
View Source
const CallFrameSize = unsafe.Sizeof(CallFrame{})
View Source
const ClosureTerminatorFlag byte = 0xff

Variables

View Source
var CALL_STACK_SIZE int
View Source
var DEFAULT_THREAD_POOL_QUEUE_SIZE int
View Source
var DEFAULT_THREAD_POOL_SIZE int
View Source
var DefaultThreadPool = &ThreadPool{}
View Source
var DeletedHashSetValue value.Value = value.Ref(DeletedHashSetValueType{})
View Source
var INIT_VALUE_STACK_SIZE int
View Source
var MAX_VALUE_STACK_SIZE int

Functions

func Accessor

func Accessor(
	container *value.MethodContainer,
	attrName string,
)

Utility method that creates a new setter and getter method and attaches them as methods to the given container. Panics when the methods cannot be defined.

func Alias

func Alias(container *value.MethodContainer, newName, oldName string)

Utility method that defines an alternative name for an existing method.

func ArrayListContains

func ArrayListContains(vm *Thread, list *value.ArrayList, val value.Value) (bool, value.Value)

func ArrayListEqual

func ArrayListEqual(vm *Thread, x, y *value.ArrayList) (bool, value.Value)

func ArrayTupleContains

func ArrayTupleContains(vm *Thread, tuple *value.ArrayTuple, val value.Value) (bool, value.Value)

func ArrayTupleEqual

func ArrayTupleEqual(vm *Thread, x, y *value.ArrayTuple) (bool, value.Value)

func BeginlessClosedRangeContains

func BeginlessClosedRangeContains(vm *Thread, r *value.BeginlessClosedRange, val value.Value) (bool, value.Value)

Checks whether a value is contained in the beginless closed range

func BeginlessClosedRangeEqual

func BeginlessClosedRangeEqual(vm *Thread, x, y *value.BeginlessClosedRange) (bool, value.Value)

Checks whether two beginless closed ranges are equal

func BeginlessOpenRangeContains

func BeginlessOpenRangeContains(vm *Thread, r *value.BeginlessOpenRange, val value.Value) (bool, value.Value)

Checks whether a value is contained in the open range

func BeginlessOpenRangeEqual

func BeginlessOpenRangeEqual(vm *Thread, x, y *value.BeginlessOpenRange) (bool, value.Value)

Checks whether two open ranges are equal

func ClosedRangeContains

func ClosedRangeContains(vm *Thread, r *value.ClosedRange, val value.Value) (bool, value.Value)

Checks whether a value is contained in the closed range

func ClosedRangeEqual

func ClosedRangeEqual(vm *Thread, x *value.ClosedRange, y *value.ClosedRange) (bool, value.Value)

Checks whether two closed ranges are equal

func ClosedRangeIteratorNext

func ClosedRangeIteratorNext(vm *Thread, i *value.ClosedRangeIterator) (value.Value, value.Value)

Get the next element of the range

func Decrement

func Decrement(vm *Thread, val value.Value) (value.Value, value.Value)

Decrement the given value

func Def

func Def(
	container *value.MethodContainer,
	name string,
	function NativeFunction,
	opts ...DefOption,
)

Utility method that creates a new native method and attaches it to the given container.

Panics when the method cannot be defined.

func DefineAccessor

func DefineAccessor(
	container *value.MethodContainer,
	attrName value.Symbol,
	index int,
)

Utility method that creates a new setter and getter method and attaches them as methods to the given method map.

func DefineGetter

func DefineGetter(
	container *value.MethodContainer,
	name value.Symbol,
	index int,
)

Creates a getter method and attaches it to the given container.

func DefineNativeMethod

func DefineNativeMethod(
	container *value.MethodContainer,
	name value.Symbol,
	params int,
	optParams int,
	function NativeFunction,
) (err value.Value)

Define a native method in the given container. Returns an error when the method couldn't be defined.

func DefineSetter

func DefineSetter(
	container *value.MethodContainer,
	attrName value.Symbol,
	index int,
)

Creates a setter method and attaches it to the given container.

func EndlessClosedRangeContains

func EndlessClosedRangeContains(vm *Thread, r *value.EndlessClosedRange, val value.Value) (bool, value.Value)

Checks whether a value is contained in the closed range

func EndlessClosedRangeEqual

func EndlessClosedRangeEqual(vm *Thread, x *value.EndlessClosedRange, y *value.EndlessClosedRange) (bool, value.Value)

Checks whether two closed ranges are equal

func EndlessClosedRangeIteratorNext

func EndlessClosedRangeIteratorNext(vm *Thread, i *value.EndlessClosedRangeIterator) (value.Value, value.Value)

Get the next element of the range

func EndlessOpenRangeContains

func EndlessOpenRangeContains(vm *Thread, r *value.EndlessOpenRange, val value.Value) (bool, value.Value)

Checks whether a value is contained in the endless open range

func EndlessOpenRangeEqual

func EndlessOpenRangeEqual(vm *Thread, x, y *value.EndlessOpenRange) (bool, value.Value)

Checks whether two Endless open ranges are equal

func EndlessOpenRangeIteratorNext

func EndlessOpenRangeIteratorNext(vm *Thread, i *value.EndlessOpenRangeIterator) (value.Value, value.Value)

Get the next element of the range

func Equal

func Equal(vm *Thread, left, right value.Value) (value.Value, value.Value)

Check whether two values are equal

func Getter

func Getter(
	container *value.MethodContainer,
	name string,
)

Utility method that creates a new getter method and attaches it as a method to the given container. It panics when the method cannot be defined.

func GreaterThan

func GreaterThan(vm *Thread, left, right value.Value) (value.Value, value.Value)

Check whether the left value is greater than the right

func GreaterThanEqual

func GreaterThanEqual(vm *Thread, left, right value.Value) (value.Value, value.Value)

Check whether the left value is greater than or equal to the right

func Hash

func Hash(vm *Thread, key value.Value) (value.UInt64, value.Value)

Calculate the hash for the given value

func HashMapConcat

func HashMapConcat(vm *Thread, x *value.HashMap, y *value.HashMap) (*value.HashMap, value.Value)

Create a new map containing the pairs of both maps.

func HashMapContains

func HashMapContains(vm *Thread, hashMap *value.HashMap, pair *value.Pair) (bool, value.Value)

Check if the given pair is present in the map

func HashMapContainsKey

func HashMapContainsKey(vm *Thread, hashMap *value.HashMap, key value.Value) (bool, value.Value)

Check if the given key is present in the map

func HashMapContainsValue

func HashMapContainsValue(vm *Thread, hashMap *value.HashMap, val value.Value) (bool, value.Value)

Check if the given value is present in the map

func HashMapCopy

func HashMapCopy(vm *Thread, target *value.HashMap, source *value.HashMap) value.Value

Copy the pairs of one hashmap to the other.

func HashMapCopyTable

func HashMapCopyTable(vm *Thread, target *value.HashMap, source []value.Pair) value.Value

func HashMapDelete

func HashMapDelete(vm *Thread, hashMap *value.HashMap, key value.Value) (bool, value.Value)

Delete the given key from the hashMap

func HashMapEqual

func HashMapEqual(vm *Thread, x *value.HashMap, y *value.HashMap) (bool, value.Value)

Checks whether two hash maps are equal

func HashMapGet

func HashMapGet(vm *Thread, hashMap *value.HashMap, key value.Value) (value.Value, value.Value)

Get the element under the given key. Returns (value, undefined) when the value has been found. Returns (undefined, undefined) when the key is not present. Returns (undefined, err) when there was an error.

func HashMapGrow

func HashMapGrow(vm *Thread, hashMap *value.HashMap, newSlots int) value.Value

Add additional n empty slots for new elements.

func HashMapIndex

func HashMapIndex(vm *Thread, hashMap *value.HashMap, key value.Value) (int, value.Value)

Get the index that the key should be inserted into. Returns (nil, err) when an error has been encountered. Returns (-1, nil) when there's no room for new values.

func HashMapLaxEqual

func HashMapLaxEqual(vm *Thread, x *value.HashMap, y *value.HashMap) (bool, value.Value)

Checks whether two hash maps are equal (lax)

func HashMapSet

func HashMapSet(vm *Thread, hashMap *value.HashMap, key, val value.Value) value.Value

Set a value under the given key.

func HashMapSetCapacity

func HashMapSetCapacity(vm *Thread, hashMap *value.HashMap, capacity int) value.Value

Resize the given hashmap to the desired capacity.

func HashMapSetWithMaxLoad

func HashMapSetWithMaxLoad(vm *Thread, hashMap *value.HashMap, key, val value.Value, maxLoad float64) value.Value

func HashRecordConcat

func HashRecordConcat(vm *Thread, x *value.HashRecord, y *value.HashRecord) (*value.HashMap, value.Value)

Create a new map containing the pairs of both maps.

func HashRecordContains

func HashRecordContains(vm *Thread, hrec *value.HashRecord, pair *value.Pair) (bool, value.Value)

Check if the given pair is present in the record

func HashRecordContainsKey

func HashRecordContainsKey(vm *Thread, hrec *value.HashRecord, key value.Value) (bool, value.Value)

Check if the given key is present in the record

func HashRecordContainsValue

func HashRecordContainsValue(vm *Thread, hrec *value.HashRecord, val value.Value) (bool, value.Value)

Check if the given value is present in the record

func HashRecordCopy

func HashRecordCopy(vm *Thread, target *value.HashRecord, source *value.HashRecord) value.Value

Copy the pairs of one hash record to the other.

func HashRecordCopyTable

func HashRecordCopyTable(vm *Thread, target *value.HashRecord, source []value.Pair) value.Value

func HashRecordDelete

func HashRecordDelete(vm *Thread, hashRecord *value.HashRecord, key value.Value) (bool, value.Value)

Delete the given key from the hashMap

func HashRecordEqual

func HashRecordEqual(vm *Thread, x *value.HashRecord, y *value.HashRecord) (bool, value.Value)

Checks whether two hash records are equal

func HashRecordGet

func HashRecordGet(vm *Thread, hashRecord *value.HashRecord, key value.Value) (value.Value, value.Value)

Get the element under the given key.

func HashRecordGrow

func HashRecordGrow(vm *Thread, hashRecord *value.HashRecord, newSlots int) value.Value

Add additional n empty slots for new elements.

func HashRecordLaxEqual

func HashRecordLaxEqual(vm *Thread, x *value.HashRecord, y *value.HashRecord) (bool, value.Value)

Checks whether two hash records are equal (lax)

func HashRecordSet

func HashRecordSet(vm *Thread, hashRecord *value.HashRecord, key, val value.Value) value.Value

Set a value under the given key.

func HashRecordSetCapacity

func HashRecordSetCapacity(vm *Thread, hashRecord *value.HashRecord, capacity int) value.Value

Resize the given hash record to the desired capacity.

func HashRecordSetWithMaxLoad

func HashRecordSetWithMaxLoad(vm *Thread, hashRecord *value.HashRecord, key, val value.Value, maxLoad float64) value.Value

func HashSetAppend

func HashSetAppend(vm *Thread, set *value.HashSet, val value.Value) value.Value

Set a value under the given key.

func HashSetAppendWithMaxLoad

func HashSetAppendWithMaxLoad(vm *Thread, set *value.HashSet, val value.Value, maxLoad float64) value.Value

func HashSetContains

func HashSetContains(vm *Thread, set *value.HashSet, val value.Value) (bool, value.Value)

Check whether the given value is contained within the set.

func HashSetCopy

func HashSetCopy(vm *Thread, target *value.HashSet, source *value.HashSet) value.Value

Copy the pairs of one hashmap to the other.

func HashSetCopyTable

func HashSetCopyTable(vm *Thread, target *value.HashSet, source []value.Value) value.Value

func HashSetDelete

func HashSetDelete(vm *Thread, hashSet *value.HashSet, val value.Value) (bool, value.Value)

Delete the given value from the hash set

func HashSetEqual

func HashSetEqual(vm *Thread, x *value.HashSet, y *value.HashSet) (bool, value.Value)

Checks whether two hash sets are equal

func HashSetGrow

func HashSetGrow(vm *Thread, set *value.HashSet, newSlots int) value.Value

Add additional n empty slots for new elements.

func HashSetIndex

func HashSetIndex(vm *Thread, set *value.HashSet, val value.Value) (int, value.Value)

Get the index that the value should be inserted into. Returns (0, err) when an error has been encountered. Returns (-1, undefined) when there's no room for new values.

func HashSetIntersection

func HashSetIntersection(vm *Thread, x *value.HashSet, y *value.HashSet) (*value.HashSet, value.Value)

Create a new set that is the intersection of the given two sets

func HashSetSetCapacity

func HashSetSetCapacity(vm *Thread, set *value.HashSet, capacity int) value.Value

Resize the given set to the desired capacity.

func HashSetUnion

func HashSetUnion(vm *Thread, x *value.HashSet, y *value.HashSet) (*value.HashSet, value.Value)

Create a new set that is the union of the given two sets

func Increment

func Increment(vm *Thread, val value.Value) (value.Value, value.Value)

Increment the given value

func InitGlobalEnvironment

func InitGlobalEnvironment()

func Inspect

func Inspect(vm *Thread, val value.Value) (value.Value, value.Value)

Return the string representation of a value for debugging

func InspectWithColor

func InspectWithColor(vm *Thread, val value.Value) (string, value.Value)

Return the string representation of a value for debugging

func Iterate

func Iterate(vm *Thread, collectionValue value.Value) iter.Seq2[value.Value, value.Value]

Iterate over an iterable value

func LaxEqual

func LaxEqual(vm *Thread, left, right value.Value) (value.Value, value.Value)

Check whether two values are equal (lax)

func LeftOpenRangeContains

func LeftOpenRangeContains(vm *Thread, r *value.LeftOpenRange, val value.Value) (bool, value.Value)

Checks whether a value is contained in the left open range

func LeftOpenRangeEqual

func LeftOpenRangeEqual(vm *Thread, x, y *value.LeftOpenRange) (bool, value.Value)

Checks whether two left open ranges are equal

func LeftOpenRangeIteratorNext

func LeftOpenRangeIteratorNext(vm *Thread, i *value.LeftOpenRangeIterator) (value.Value, value.Value)

Get the next element of the range

func LessThan

func LessThan(vm *Thread, left, right value.Value) (value.Value, value.Value)

Check whether the left value is less than the right

func LessThanEqual

func LessThanEqual(vm *Thread, left, right value.Value) (value.Value, value.Value)

Check whether the left value is less than or equal to the right

func MustNewHashMapWithCapacityAndElements

func MustNewHashMapWithCapacityAndElements(vm *Thread, capacity int, elements ...value.Pair) *value.HashMap

func MustNewHashMapWithElements

func MustNewHashMapWithElements(vm *Thread, elements ...value.Pair) *value.HashMap

Create a new hashmap with the given entries.

func MustNewHashRecordWithCapacityAndElements

func MustNewHashRecordWithCapacityAndElements(vm *Thread, capacity int, elements ...value.Pair) *value.HashRecord

func MustNewHashRecordWithElements

func MustNewHashRecordWithElements(vm *Thread, elements ...value.Pair) *value.HashRecord

Create a new hash record with the given entries.

func MustNewHashSetWithCapacityAndElements

func MustNewHashSetWithCapacityAndElements(vm *Thread, capacity int, elements ...value.Value) *value.HashSet

func MustNewHashSetWithCapacityAndElementsMaxLoad

func MustNewHashSetWithCapacityAndElementsMaxLoad(vm *Thread, capacity int, maxLoad float64, elements ...value.Value) *value.HashSet

func MustNewHashSetWithElements

func MustNewHashSetWithElements(vm *Thread, elements ...value.Value) *value.HashSet

Create a new hash set with the given entries.

func NewHashMapComparer

func NewHashMapComparer(opts *cmp.Options) cmp.Option

func NewHashMapWithCapacityAndElements

func NewHashMapWithCapacityAndElements(vm *Thread, capacity int, elements ...value.Pair) (*value.HashMap, value.Value)

func NewHashMapWithElements

func NewHashMapWithElements(vm *Thread, elements ...value.Pair) (*value.HashMap, value.Value)

Create a new hashmap with the given entries.

func NewHashRecordComparer

func NewHashRecordComparer(opts *cmp.Options) cmp.Option

func NewHashRecordWithCapacityAndElements

func NewHashRecordWithCapacityAndElements(vm *Thread, capacity int, elements ...value.Pair) (*value.HashRecord, value.Value)

func NewHashRecordWithElements

func NewHashRecordWithElements(vm *Thread, elements ...value.Pair) (*value.HashRecord, value.Value)

Create a new hash record with the given entries.

func NewHashSetComparer

func NewHashSetComparer(opts *cmp.Options) cmp.Option

func NewHashSetWithCapacityAndElements

func NewHashSetWithCapacityAndElements(vm *Thread, capacity int, elements ...value.Value) (*value.HashSet, value.Value)

func NewHashSetWithCapacityAndElementsMaxLoad

func NewHashSetWithCapacityAndElementsMaxLoad(vm *Thread, capacity int, maxLoad float64, elements ...value.Value) (*value.HashSet, value.Value)

func NewHashSetWithElements

func NewHashSetWithElements(vm *Thread, elements ...value.Value) (*value.HashSet, value.Value)

Create a new hash set with the given entries.

func NewNativeMethodComparer

func NewNativeMethodComparer() cmp.Option

func NextBuiltin

func NextBuiltin(vm *Thread, val value.Value) (result, err value.Value)

Call `next`

func ObjectHash

func ObjectHash(val value.Value) value.UInt64

func OnceDo

func OnceDo(vm *Thread, once *value.Once, fn value.Value) (err value.Value)

func OpenRangeContains

func OpenRangeContains(vm *Thread, r *value.OpenRange, val value.Value) (bool, value.Value)

Checks whether a value is contained in the open range

func OpenRangeEqual

func OpenRangeEqual(vm *Thread, x *value.OpenRange, y *value.OpenRange) (bool, value.Value)

Checks whether two open ranges are equal

func OpenRangeIteratorNext

func OpenRangeIteratorNext(vm *Thread, i *value.OpenRangeIterator) (value.Value, value.Value)

Get the next element of the range

func PairEqual

func PairEqual(vm *Thread, x *value.Pair, y *value.Pair) (bool, value.Value)

Checks whether two pairs are equal

func PrintError

func PrintError(stderr io.Writer, stackTrace *value.StackTrace, err value.Value)

func RightOpenRangeContains

func RightOpenRangeContains(vm *Thread, r *value.RightOpenRange, val value.Value) (bool, value.Value)

Checks whether a value is contained in the range

func RightOpenRangeEqual

func RightOpenRangeEqual(vm *Thread, x, y *value.RightOpenRange) (bool, value.Value)

Checks whether two right open ranges are equal

func RightOpenRangeIteratorNext

func RightOpenRangeIteratorNext(vm *Thread, i *value.RightOpenRangeIterator) (value.Value, value.Value)

Get the next element of the range

func Setter

func Setter(
	container *value.MethodContainer,
	attrName string,
)

Utility method that creates a new setter method and attaches it as a method to the given container. Panics when the method cannot be defined.

func SubscriptBuiltin

func SubscriptBuiltin(vm *Thread, collection, key value.Value) (result, err value.Value)

func SubscriptSetBuiltin

func SubscriptSetBuiltin(vm *Thread, collection, key, val value.Value) (err value.Value)

Types

type BytecodeFunction

type BytecodeFunction struct {
	Instructions []byte
	Values       []value.Value // The value pool
	LineInfoList bytecode.LineInfoList
	Location     *position.Location
	Doc          value.Value
	CatchEntries []*CatchEntry
	UpvalueCount int
	// contains filtered or unexported fields
}

A single unit of Elk bytecode.

func NewBytecodeFunction

func NewBytecodeFunction(
	name value.Symbol,
	instruct []byte,
	loc *position.Location,
	lineInfo bytecode.LineInfoList,
	paramCount int,
	optParamCount int,
	values []value.Value,
) *BytecodeFunction

Create a new bytecode method.

func NewBytecodeFunctionNoParams

func NewBytecodeFunctionNoParams(
	name value.Symbol,
	instruct []byte,
	loc *position.Location,
	lineInfo bytecode.LineInfoList,
	values []value.Value,
) *BytecodeFunction

Create a new bytecode method.

func NewBytecodeFunctionSimple

func NewBytecodeFunctionSimple(name value.Symbol, instruct []byte, loc *position.Location) *BytecodeFunction

Create a new bytecode method.

func NewBytecodeFunctionWithCatchEntries

func NewBytecodeFunctionWithCatchEntries(
	name value.Symbol,
	instruct []byte,
	loc *position.Location,
	lineInfo bytecode.LineInfoList,
	params int,
	optParamCount int,
	values []value.Value,
	catchEntries []*CatchEntry,
) *BytecodeFunction

Create a new bytecode method.

func NewBytecodeFunctionWithOptions

func NewBytecodeFunctionWithOptions(opts ...BytecodeFunctionOption) *BytecodeFunction

Create a new bytecode method with options.

func NewBytecodeFunctionWithUpvalues

func NewBytecodeFunctionWithUpvalues(
	name value.Symbol,
	instruct []byte,
	loc *position.Location,
	lineInfo bytecode.LineInfoList,
	params int,
	optParamCount int,
	values []value.Value,
	upvalueCount int,
) *BytecodeFunction

Create a new bytecode method.

func (*BytecodeFunction) AddBytes

func (f *BytecodeFunction) AddBytes(bytes ...byte)

Add bytes to the bytecode chunk.

func (*BytecodeFunction) AddInstruction

func (f *BytecodeFunction) AddInstruction(lineNumber int, op bytecode.OpCode, bytes ...byte)

Add an instruction to the bytecode chunk.

func (*BytecodeFunction) AddParameter

func (f *BytecodeFunction) AddParameter()

Add a parameter to the method.

func (*BytecodeFunction) AddValue

func (f *BytecodeFunction) AddValue(obj value.Value) (int, IntSize)

Add a value to the value pool. Returns the index of the constant.

func (*BytecodeFunction) AppendUint16

func (f *BytecodeFunction) AppendUint16(n uint16)

Append two bytes to the bytecode chunk.

func (*BytecodeFunction) AppendUint32

func (f *BytecodeFunction) AppendUint32(n uint32)

Append four bytes to the bytecode chunk.

func (*BytecodeFunction) Class

func (*BytecodeFunction) Class() *value.Class

func (*BytecodeFunction) Copy

func (b *BytecodeFunction) Copy() value.Reference

func (*BytecodeFunction) DirectClass

func (*BytecodeFunction) DirectClass() *value.Class

func (*BytecodeFunction) Disassemble

func (f *BytecodeFunction) Disassemble(output io.Writer) error

Disassemble the bytecode chunk and write the output to a writer.

func (*BytecodeFunction) DisassembleInstruction

func (f *BytecodeFunction) DisassembleInstruction(output io.Writer, offset int) (int, error)

func (*BytecodeFunction) DisassembleStdout

func (f *BytecodeFunction) DisassembleStdout()

Disassemble the bytecode chunk and write the output to stdout.

func (*BytecodeFunction) DisassembleString

func (f *BytecodeFunction) DisassembleString() (string, error)

Disassemble the bytecode chunk and return a string containing the result.

func (*BytecodeFunction) Error

func (b *BytecodeFunction) Error() string

func (*BytecodeFunction) FileName

func (b *BytecodeFunction) FileName() string

func (*BytecodeFunction) GetLineNumber

func (b *BytecodeFunction) GetLineNumber(ip int) int

func (*BytecodeFunction) IncrementOptionalParameterCount

func (b *BytecodeFunction) IncrementOptionalParameterCount()

func (*BytecodeFunction) Inspect

func (b *BytecodeFunction) Inspect() string

func (*BytecodeFunction) InstanceVariables

func (*BytecodeFunction) InstanceVariables() *value.InstanceVariables

func (*BytecodeFunction) Name

func (b *BytecodeFunction) Name() value.Symbol

func (*BytecodeFunction) OptionalParameterCount

func (b *BytecodeFunction) OptionalParameterCount() int

func (*BytecodeFunction) ParameterCount

func (b *BytecodeFunction) ParameterCount() int

func (*BytecodeFunction) RemoveByte

func (f *BytecodeFunction) RemoveByte()

Add an instruction to the bytecode chunk.

func (*BytecodeFunction) SetOptionalParameterCount

func (b *BytecodeFunction) SetOptionalParameterCount(optParamCount int)

func (*BytecodeFunction) SetParameterCount

func (b *BytecodeFunction) SetParameterCount(paramCount int)

func (*BytecodeFunction) SingletonClass

func (*BytecodeFunction) SingletonClass() *value.Class

type BytecodeFunctionOption

type BytecodeFunctionOption func(*BytecodeFunction)

func BytecodeFunctionWithInstructions

func BytecodeFunctionWithInstructions(instructs []byte) BytecodeFunctionOption

func BytecodeFunctionWithLineInfoList

func BytecodeFunctionWithLineInfoList(lineInfo bytecode.LineInfoList) BytecodeFunctionOption

func BytecodeFunctionWithLocation

func BytecodeFunctionWithLocation(loc *position.Location) BytecodeFunctionOption

func BytecodeFunctionWithName

func BytecodeFunctionWithName(name value.Symbol) BytecodeFunctionOption

func BytecodeFunctionWithOptionalParameters

func BytecodeFunctionWithOptionalParameters(optParams int) BytecodeFunctionOption

func BytecodeFunctionWithParameters

func BytecodeFunctionWithParameters(params int) BytecodeFunctionOption

func BytecodeFunctionWithStringName

func BytecodeFunctionWithStringName(name string) BytecodeFunctionOption

type CallFrame

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

Contains the data of a single function call.

func (*CallFrame) FileName

func (c *CallFrame) FileName() string

func (*CallFrame) LineNumber

func (c *CallFrame) LineNumber() int

func (*CallFrame) Name

func (c *CallFrame) Name() value.Symbol

func (*CallFrame) ToCallFrameObject

func (cf *CallFrame) ToCallFrameObject() value.CallFrame

type CatchEntry

type CatchEntry struct {
	From        int  // index of the first instruction that can be handled by this catch
	To          int  // index of the last instruction that can be handled by this catch
	JumpAddress int  // index of the byte that the VM should jump to
	Finally     bool // whether this entry is for a finally clause
}

func NewCatchEntry

func NewCatchEntry(from, to, jumpAddress int, finally bool) *CatchEntry

func (*CatchEntry) ByteRange

func (c *CatchEntry) ByteRange() int

Number of bytes this catch covers

type Closure

type Closure struct {
	VMID     int64 // ID of the VM that created this closure, should be -1 if it was not created through a VM
	Bytecode *BytecodeFunction
	Self     value.Value
	Upvalues []*Upvalue
}

Wraps a bytecode function with associated local variables from the outer context

func NewClosure

func NewClosure(vmID int64, bytecode *BytecodeFunction, self value.Value) *Closure

Create a new closure

func NewNativeClosure

func NewNativeClosure(bytecode *BytecodeFunction, self value.Value) *Closure

Create a new native closure (created without a VM)

func (*Closure) Class

func (*Closure) Class() *value.Class

func (*Closure) Copy

func (c *Closure) Copy() value.Reference

func (*Closure) DirectClass

func (*Closure) DirectClass() *value.Class

func (*Closure) Error

func (c *Closure) Error() string

func (*Closure) HasOpenUpvalues

func (c *Closure) HasOpenUpvalues() bool

func (*Closure) Inspect

func (c *Closure) Inspect() string

func (*Closure) InstanceVariables

func (*Closure) InstanceVariables() *value.InstanceVariables

func (*Closure) SingletonClass

func (*Closure) SingletonClass() *value.Class

type Colorizer

type Colorizer func(string) (string, error)

func MakeColorizer

func MakeColorizer(vm *Thread, val value.Value) Colorizer

func (Colorizer) Colorize

func (f Colorizer) Colorize(s string) (string, error)

type DefOption

type DefOption func(*NativeMethod)

func DefWithOptionalParameters

func DefWithOptionalParameters(optParams int) DefOption

Define how many parameters are optional (have default values). Optional arguments will be populated with `undefined` when no value was given in the call.

func DefWithParameters

func DefWithParameters(params int) DefOption

Define parameters used by the method

type DeletedHashSetValueType

type DeletedHashSetValueType struct{}

func (DeletedHashSetValueType) Class

func (DeletedHashSetValueType) Copy

func (DeletedHashSetValueType) DirectClass

func (DeletedHashSetValueType) DirectClass() *value.Class

func (DeletedHashSetValueType) Error

func (e DeletedHashSetValueType) Error() string

func (DeletedHashSetValueType) Inspect

func (DeletedHashSetValueType) Inspect() string

func (DeletedHashSetValueType) InstanceVariables

func (DeletedHashSetValueType) InstanceVariables() *value.InstanceVariables

func (DeletedHashSetValueType) SingletonClass

func (DeletedHashSetValueType) SingletonClass() *value.Class

type Generator

type Generator struct {
	Bytecode *BytecodeFunction
	// contains filtered or unexported fields
}

Wraps a bytecode function with state that is necessary for pausing and resuming execution

func NewGeneratorForBytecode

func NewGeneratorForBytecode(bytecode *BytecodeFunction, args ...value.Value) *Generator

Create a new generator that executes the given piece of bytecode.

func (*Generator) Class

func (*Generator) Class() *value.Class

func (*Generator) Copy

func (c *Generator) Copy() value.Reference

func (*Generator) DirectClass

func (*Generator) DirectClass() *value.Class

func (*Generator) Error

func (c *Generator) Error() string

func (*Generator) Inspect

func (c *Generator) Inspect() string

func (*Generator) InstanceVariables

func (*Generator) InstanceVariables() *value.InstanceVariables

func (*Generator) SingletonClass

func (*Generator) SingletonClass() *value.Class

type GetterMethod

type GetterMethod struct {
	AttributeName value.Symbol
	Doc           value.Value
	IvarIndex     int
}

A simple getter method.

func NewGetterMethod

func NewGetterMethod(attrName value.Symbol, index int) *GetterMethod

Create a new getter method.

func (*GetterMethod) Call

func (g *GetterMethod) Call(self value.Value) (value.Value, value.Value)

func (*GetterMethod) Class

func (*GetterMethod) Class() *value.Class

func (*GetterMethod) Copy

func (g *GetterMethod) Copy() value.Reference

func (*GetterMethod) DirectClass

func (*GetterMethod) DirectClass() *value.Class

func (*GetterMethod) Error

func (g *GetterMethod) Error() string

func (*GetterMethod) Inspect

func (g *GetterMethod) Inspect() string

func (*GetterMethod) InstanceVariables

func (*GetterMethod) InstanceVariables() *value.InstanceVariables

func (*GetterMethod) Name

func (g *GetterMethod) Name() value.Symbol

func (*GetterMethod) NamedRestParameter

func (*GetterMethod) NamedRestParameter() bool

func (*GetterMethod) OptionalParameterCount

func (*GetterMethod) OptionalParameterCount() int

func (*GetterMethod) ParameterCount

func (*GetterMethod) ParameterCount() int

func (*GetterMethod) Parameters

func (*GetterMethod) Parameters() []value.Symbol

func (*GetterMethod) PostRestParameterCount

func (*GetterMethod) PostRestParameterCount() int

func (*GetterMethod) SingletonClass

func (*GetterMethod) SingletonClass() *value.Class

type IntSize

type IntSize uint8

Size of an integer.

type LocalBox

type LocalBox Upvalue

Represents a pointer to a local variable/value

func (*LocalBox) Class

func (*LocalBox) Class() *value.Class

func (*LocalBox) Close

func (l *LocalBox) Close()

func (*LocalBox) Copy

func (v *LocalBox) Copy() value.Reference

func (*LocalBox) DirectClass

func (*LocalBox) DirectClass() *value.Class

func (*LocalBox) Error

func (l *LocalBox) Error() string

func (*LocalBox) Get

func (l *LocalBox) Get() value.Value

Retrieve the value stored in the box

func (*LocalBox) Inspect

func (l *LocalBox) Inspect() string

func (*LocalBox) InstanceVariables

func (*LocalBox) InstanceVariables() *value.InstanceVariables

func (*LocalBox) IsClosed

func (l *LocalBox) IsClosed() bool

func (*LocalBox) IsOpen

func (l *LocalBox) IsOpen() bool

func (*LocalBox) LocalAddress

func (l *LocalBox) LocalAddress() uintptr

func (*LocalBox) Next

func (l *LocalBox) Next(step int) *value.Box

Return the box of the next value in memory

func (*LocalBox) NextImmutableBox

func (l *LocalBox) NextImmutableBox(step int) *value.ImmutableBox

func (*LocalBox) Prev

func (l *LocalBox) Prev(step int) *value.Box

Return the box of the previous value in memory

func (*LocalBox) PrevImmutableBox

func (l *LocalBox) PrevImmutableBox(step int) *value.ImmutableBox

func (*LocalBox) Set

func (l *LocalBox) Set(v value.Value)

Set the value in the box

func (*LocalBox) SingletonClass

func (*LocalBox) SingletonClass() *value.Class

func (*LocalBox) ToBox

func (l *LocalBox) ToBox() *value.Box

func (*LocalBox) ToImmutableBox

func (l *LocalBox) ToImmutableBox() *value.ImmutableBox

type NativeFunction

type NativeFunction func(vm *Thread, args []value.Value) (returnVal, err value.Value)

An implementation of a native Elk method.

type NativeMethod

type NativeMethod struct {
	Function NativeFunction
	Doc      value.Value
	// contains filtered or unexported fields
}

A native Elk method

func NewNativeMethod

func NewNativeMethod(
	name value.Symbol,
	params int,
	optParams int,
	function NativeFunction,
) *NativeMethod

Create a new native method.

func (*NativeMethod) Class

func (*NativeMethod) Class() *value.Class

func (*NativeMethod) Copy

func (n *NativeMethod) Copy() value.Reference

func (*NativeMethod) DirectClass

func (*NativeMethod) DirectClass() *value.Class

func (*NativeMethod) Error

func (n *NativeMethod) Error() string

func (*NativeMethod) Inspect

func (n *NativeMethod) Inspect() string

func (*NativeMethod) InstanceVariables

func (*NativeMethod) InstanceVariables() *value.InstanceVariables

func (*NativeMethod) Name

func (n *NativeMethod) Name() value.Symbol

func (*NativeMethod) OptionalParameterCount

func (n *NativeMethod) OptionalParameterCount() int

func (*NativeMethod) ParameterCount

func (n *NativeMethod) ParameterCount() int

func (*NativeMethod) SingletonClass

func (*NativeMethod) SingletonClass() *value.Class

type NativePromiseBody

type NativePromiseBody struct {
	Function NativeFunction
	Args     []value.Value
}

func NewNativePromiseBody

func NewNativePromiseBody(fn NativeFunction, args ...value.Value) *NativePromiseBody

type Option

type Option func(*Thread) // constructor option function

func WithStderr

func WithStderr(stderr io.Writer) Option

Assign the given io.Writer as the Stderr of the VM.

func WithStdin

func WithStdin(stdin io.Reader) Option

Assign the given io.Reader as the Stdin of the VM.

func WithStdout

func WithStdout(stdout io.Writer) Option

Assign the given io.Writer as the Stdout of the VM.

func WithThreadPool

func WithThreadPool(tp *ThreadPool) Option

type Promise

type Promise struct {
	Body       PromiseBody
	ThreadPool *ThreadPool
	// contains filtered or unexported fields
}

func NewBytecodePromise

func NewBytecodePromise(threadPool *ThreadPool, bytecode *BytecodeFunction, args ...value.Value) *Promise

Create a new promise for a piece of bytecode executed by the VM

func NewExternalPromise

func NewExternalPromise(threadPool *ThreadPool) *Promise

Returns a new husk promise. It does not contain any code and is supposed to be resolved or rejected externally.

func NewNativePromise

func NewNativePromise(threadPool *ThreadPool, fn NativeFunction, args ...value.Value) *Promise

Create a new native promise.

func NewPromise

func NewPromise(threadPool *ThreadPool, generator *Generator) *Promise

Create a new promise executed by the VM

func NewRejectedPromise

func NewRejectedPromise(err value.Value) *Promise

func NewResolvedPromise

func NewResolvedPromise(result value.Value) *Promise

func (*Promise) AwaitSync

func (p *Promise) AwaitSync() (value.Value, *value.StackTrace, value.Value)

Wait for the result of the promise

func (*Promise) Class

func (*Promise) Class() *value.Class

func (*Promise) Copy

func (p *Promise) Copy() value.Reference

func (*Promise) DirectClass

func (*Promise) DirectClass() *value.Class

func (*Promise) Error

func (p *Promise) Error() string

func (*Promise) Inspect

func (p *Promise) Inspect() string

func (*Promise) InstanceVariables

func (*Promise) InstanceVariables() *value.InstanceVariables

func (*Promise) IsResolved

func (p *Promise) IsResolved() bool

func (*Promise) RegisterContinuation

func (p *Promise) RegisterContinuation(continuation *Promise)

func (*Promise) RegisterContinuationUnsafe

func (p *Promise) RegisterContinuationUnsafe(continuation *Promise)

func (*Promise) Reject

func (p *Promise) Reject(err value.Value, stackTrace *value.StackTrace)

func (*Promise) Resolve

func (p *Promise) Resolve(result value.Value)

func (*Promise) ResolveReject

func (p *Promise) ResolveReject(result, err value.Value)

func (*Promise) SingletonClass

func (*Promise) SingletonClass() *value.Class

type PromiseBody

type PromiseBody interface {
	// contains filtered or unexported methods
}

type SetterMethod

type SetterMethod struct {
	Doc           value.Value
	AttributeName value.Symbol

	IvarIndex int
	// contains filtered or unexported fields
}

A simple setter method.

func NewSetterMethod

func NewSetterMethod(attrName value.Symbol, index int) *SetterMethod

Create a new getter method.

func (*SetterMethod) Call

func (s *SetterMethod) Call(self value.Value, val value.Value) (value.Value, value.Value)

func (*SetterMethod) Class

func (*SetterMethod) Class() *value.Class

func (*SetterMethod) Copy

func (s *SetterMethod) Copy() value.Reference

func (*SetterMethod) DirectClass

func (*SetterMethod) DirectClass() *value.Class

func (*SetterMethod) Error

func (s *SetterMethod) Error() string

func (*SetterMethod) Inspect

func (s *SetterMethod) Inspect() string

func (*SetterMethod) InstanceVariables

func (*SetterMethod) InstanceVariables() *value.InstanceVariables

func (*SetterMethod) Name

func (s *SetterMethod) Name() value.Symbol

func (*SetterMethod) NamedRestParameter

func (*SetterMethod) NamedRestParameter() bool

func (*SetterMethod) OptionalParameterCount

func (*SetterMethod) OptionalParameterCount() int

func (*SetterMethod) ParameterCount

func (*SetterMethod) ParameterCount() int

func (*SetterMethod) Parameters

func (*SetterMethod) Parameters() []value.Symbol

func (*SetterMethod) PostRestParameterCount

func (*SetterMethod) PostRestParameterCount() int

func (*SetterMethod) SingletonClass

func (*SetterMethod) SingletonClass() *value.Class

type Thread

type Thread struct {
	ID     int64
	Stdin  io.Reader // standard output used by the VM
	Stdout io.Writer // standard input used by the VM
	Stderr io.Writer // standard error used by the VM
	// contains filtered or unexported fields
}

A single instance of the Elk Virtual Machine.

func New

func New(opts ...Option) *Thread

Create a new VM instance.

func (*Thread) AddCallFrame

func (vm *Thread) AddCallFrame(cf CallFrame)

func (*Thread) BuildStackTrace

func (vm *Thread) BuildStackTrace() *value.StackTrace

func (*Thread) BuildStackTracePrepend

func (vm *Thread) BuildStackTracePrepend(base *value.StackTrace) *value.StackTrace

func (*Thread) CallCallable

func (vm *Thread) CallCallable(args ...value.Value) (value.Value, value.Value)

Call a callable value from Go code, preserving the state of the VM.

func (*Thread) CallClosure

func (vm *Thread) CallClosure(closure *Closure, args ...value.Value) (value.Value, value.Value)

Call an Elk closure from Go code, preserving the state of the VM.

func (*Thread) CallGeneratorNext

func (vm *Thread) CallGeneratorNext(generator *Generator) (value.Value, value.Value)

func (*Thread) CallMethod

func (vm *Thread) CallMethod(method value.Method, args ...value.Value) (value.Value, value.Value)

func (*Thread) CallMethodByName

func (vm *Thread) CallMethodByName(name value.Symbol, args ...value.Value) (value.Value, value.Value)

Call an Elk method from Go code, preserving the state of the VM.

func (*Thread) CallMethodByNameWithCache

func (vm *Thread) CallMethodByNameWithCache(name value.Symbol, cc **value.CallCache, args ...value.Value) (value.Value, value.Value)

Call an Elk method from Go code, preserving the state of the VM.

func (*Thread) Class

func (*Thread) Class() *value.Class

func (*Thread) ClearStackFrames

func (vm *Thread) ClearStackFrames()

func (*Thread) Copy

func (vm *Thread) Copy() value.Reference

func (*Thread) DirectClass

func (*Thread) DirectClass() *value.Class

func (*Thread) Err

func (vm *Thread) Err() value.Value

Get the stored error.

func (*Thread) ErrStackTrace

func (vm *Thread) ErrStackTrace() *value.StackTrace

Get the stored error stack trace.

func (*Thread) Error

func (vm *Thread) Error() string

func (*Thread) GetStackTrace

func (vm *Thread) GetStackTrace() *value.StackTrace

func (*Thread) Inspect

func (vm *Thread) Inspect() string

func (*Thread) InspectCallStack

func (vm *Thread) InspectCallStack()

func (*Thread) InspectValueStack

func (vm *Thread) InspectValueStack()

func (*Thread) InstanceVariables

func (vm *Thread) InstanceVariables() *value.InstanceVariables

func (*Thread) InterpretREPL

func (vm *Thread) InterpretREPL(fn *BytecodeFunction) (value.Value, value.Value)

Execute the given bytecode chunk.

func (*Thread) InterpretTopLevel

func (vm *Thread) InterpretTopLevel(fn *BytecodeFunction) (value.Value, value.Value)

Execute the given bytecode chunk.

func (*Thread) Panic

func (vm *Thread) Panic(err value.Value)

func (*Thread) PopCallFrame

func (vm *Thread) PopCallFrame()

func (*Thread) PrintError

func (vm *Thread) PrintError()

func (*Thread) PrintErrorValue

func (vm *Thread) PrintErrorValue(err value.Value)

func (*Thread) ResetError

func (vm *Thread) ResetError()

func (*Thread) SingletonClass

func (*Thread) SingletonClass() *value.Class

func (*Thread) StackTop

func (vm *Thread) StackTop() value.Value

Get the value on top of the value stack.

func (*Thread) StateSymbol

func (vm *Thread) StateSymbol() value.Symbol

func (*Thread) ValueStack

func (vm *Thread) ValueStack() []value.Value

type ThreadPool

type ThreadPool struct {
	Threads   []*Thread
	TaskQueue chan *Promise
}

func NewThreadPool

func NewThreadPool(threadCount, queueSize int, opts ...Option) *ThreadPool

func (*ThreadPool) AddTask

func (t *ThreadPool) AddTask(promise *Promise)

func (*ThreadPool) Class

func (*ThreadPool) Class() *value.Class

func (*ThreadPool) Close

func (t *ThreadPool) Close()

func (*ThreadPool) Copy

func (t *ThreadPool) Copy() value.Reference

func (*ThreadPool) DirectClass

func (*ThreadPool) DirectClass() *value.Class

func (*ThreadPool) Error

func (t *ThreadPool) Error() string

func (*ThreadPool) Inspect

func (t *ThreadPool) Inspect() string

func (*ThreadPool) InstanceVariables

func (*ThreadPool) InstanceVariables() *value.InstanceVariables

func (*ThreadPool) SingletonClass

func (*ThreadPool) SingletonClass() *value.Class

func (*ThreadPool) TaskQueueSize

func (t *ThreadPool) TaskQueueSize() int

func (*ThreadPool) ThreadCount

func (t *ThreadPool) ThreadCount() int

type Upvalue

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

Represents a captured variable from an outer context

func NewClosedUpvalue

func NewClosedUpvalue(val value.Value) *Upvalue

Create a new upvalue that is already closed

func NewUpvalue

func NewUpvalue(slot *value.Value) *Upvalue

func (*Upvalue) Class

func (*Upvalue) Class() *value.Class

func (*Upvalue) Close

func (u *Upvalue) Close()

func (*Upvalue) Copy

func (u *Upvalue) Copy() value.Reference

func (*Upvalue) DirectClass

func (*Upvalue) DirectClass() *value.Class

func (*Upvalue) Error

func (u *Upvalue) Error() string

func (*Upvalue) Get

func (u *Upvalue) Get() value.Value

func (*Upvalue) Inspect

func (*Upvalue) Inspect() string

func (*Upvalue) InstanceVariables

func (*Upvalue) InstanceVariables() *value.InstanceVariables

func (*Upvalue) IsClosed

func (u *Upvalue) IsClosed() bool

func (*Upvalue) IsOpen

func (u *Upvalue) IsOpen() bool

func (*Upvalue) Set

func (u *Upvalue) Set(v value.Value)

func (*Upvalue) SingletonClass

func (*Upvalue) SingletonClass() *value.Class

Jump to

Keyboard shortcuts

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