vm

package
v0.0.0-...-ac15bac Latest Latest
Warning

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

Go to latest
Published: May 2, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

View Source
var CALL_STACK_SIZE int
View Source
var VALUE_STACK_SIZE int

BENCHMARK: compare with a dynamically allocated array

Functions

func Accessor

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

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. Panics when the method cannot be defined.

func ArrayListContains

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

func ArrayListEqual

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

func ArrayTupleContains

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

func ArrayTupleEqual

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

func BeginlessClosedRangeContains

func BeginlessClosedRangeContains(vm *VM, 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 *VM, x, y *value.BeginlessClosedRange) (bool, value.Value)

Checks whether two beginless closed ranges are equal

func BeginlessOpenRangeContains

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

Checks whether a value is contained in the open range

func BeginlessOpenRangeEqual

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

Checks whether two open ranges are equal

func ClosedRangeContains

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

Checks whether a value is contained in the closed range

func ClosedRangeEqual

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

Checks whether two closed ranges are equal

func ClosedRangeIteratorNext

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

Get the next element of the range

func Decrement

func Decrement(vm *VM, 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,
	sealed bool,
) *value.Error

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,
	sealed bool,
) *value.Error

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

func DefineNativeMethod

func DefineNativeMethod(
	container *value.MethodContainer,
	name value.Symbol,
	params []value.Symbol,
	optParams int,
	postParams int,
	namedRestParam bool,
	sealed bool,
	function NativeFunction,
) *value.Error

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,
	sealed bool,
) *value.Error

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

func EndlessClosedRangeContains

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

Checks whether a value is contained in the closed range

func EndlessClosedRangeEqual

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

Checks whether two closed ranges are equal

func EndlessClosedRangeIteratorNext

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

Get the next element of the range

func EndlessOpenRangeContains

func EndlessOpenRangeContains(vm *VM, 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 *VM, x, y *value.EndlessOpenRange) (bool, value.Value)

Checks whether two Endless open ranges are equal

func EndlessOpenRangeIteratorNext

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

Get the next element of the range

func Equal

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

Check whether two values are equal

func Getter

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

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 *VM, left, right value.Value) (value.Value, value.Value)

Check whether the left value is greater than the right

func GreaterThanEqual

func GreaterThanEqual(vm *VM, 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 *VM, key value.Value) (value.UInt64, value.Value)

Calculate the hash for the given value

func HashMapContains

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

Check if the given pair is present in the map

func HashMapContainsKey

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

Check if the given key is present in the map

func HashMapContainsValue

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

Check if the given value is present in the map

func HashMapCopy

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

Copy the pairs of one hashmap to the other.

func HashMapCopyTable

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

func HashMapDelete

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

Delete the given key from the hashMap

func HashMapEqual

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

Checks whether two hash maps are equal

func HashMapGet

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

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

func HashMapGrow

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

Add additional n empty slots for new elements.

func HashMapIndex

func HashMapIndex(vm *VM, 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 *VM, x *value.HashMap, y *value.HashMap) (bool, value.Value)

Checks whether two hash maps are equal (lax)

func HashMapSet

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

Set a value under the given key.

func HashMapSetCapacity

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

Resize the given hashmap to the desired capacity.

func HashMapSetWithMaxLoad

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

func HashRecordContains

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

Check if the given pair is present in the record

func HashRecordContainsKey

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

Check if the given key is present in the record

func HashRecordContainsValue

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

Check if the given value is present in the record

func HashRecordCopy

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

Copy the pairs of one hash record to the other.

func HashRecordCopyTable

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

func HashRecordDelete

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

Delete the given key from the hashMap

func HashRecordEqual

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

Checks whether two hash records are equal

func HashRecordGet

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

Get the element under the given key.

func HashRecordGrow

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

Add additional n empty slots for new elements.

func HashRecordLaxEqual

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

Checks whether two hash records are equal (lax)

func HashRecordSet

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

Set a value under the given key.

func HashRecordSetCapacity

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

Resize the given hash record to the desired capacity.

func HashRecordSetWithMaxLoad

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

func HashSetAppend

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

Set a value under the given key.

func HashSetAppendWithMaxLoad

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

func HashSetContains

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

Check whether the given value is contained within the set.

func HashSetCopy

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

Copy the pairs of one hashmap to the other.

func HashSetCopyTable

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

func HashSetDelete

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

Delete the given value from the hash set

func HashSetEqual

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

Checks whether two hash sets are equal

func HashSetGrow

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

Add additional n empty slots for new elements.

func HashSetIndex

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

Get the index that the value 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 HashSetIntersection

func HashSetIntersection(vm *VM, 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 *VM, set *value.HashSet, capacity int) value.Value

Resize the given set to the desired capacity.

func HashSetUnion

func HashSetUnion(vm *VM, 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 *VM, val value.Value) (value.Value, value.Value)

Increment the given value

func LaxEqual

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

Check whether two values are equal (lax)

func LeftOpenRangeContains

func LeftOpenRangeContains(vm *VM, 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 *VM, x, y *value.LeftOpenRange) (bool, value.Value)

Checks whether two left open ranges are equal

func LeftOpenRangeIteratorNext

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

Get the next element of the range

func LessThan

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

Check whether the left value is less than the right

func LessThanEqual

func LessThanEqual(vm *VM, 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 *VM, capacity int, elements ...value.Pair) *value.HashMap

func MustNewHashMapWithElements

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

Create a new hashmap with the given entries.

func MustNewHashRecordWithCapacityAndElements

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

func MustNewHashRecordWithElements

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

Create a new hash record with the given entries.

func MustNewHashSetWithCapacityAndElements

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

func MustNewHashSetWithCapacityAndElementsMaxLoad

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

func MustNewHashSetWithElements

func MustNewHashSetWithElements(vm *VM, 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 *VM, capacity int, elements ...value.Pair) (*value.HashMap, value.Value)

func NewHashMapWithElements

func NewHashMapWithElements(vm *VM, 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 *VM, capacity int, elements ...value.Pair) (*value.HashRecord, value.Value)

func NewHashRecordWithElements

func NewHashRecordWithElements(vm *VM, 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 *VM, capacity int, elements ...value.Value) (*value.HashSet, value.Value)

func NewHashSetWithCapacityAndElementsMaxLoad

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

func NewHashSetWithElements

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

Create a new hash set with the given entries.

func NewNativeMethodComparer

func NewNativeMethodComparer() cmp.Option

func OpenRangeContains

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

Checks whether a value is contained in the open range

func OpenRangeEqual

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

Checks whether two open ranges are equal

func OpenRangeIteratorNext

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

Get the next element of the range

func PairEqual

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

Checks whether two pairs are equal

func RightOpenRangeContains

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

Checks whether a value is contained in the range

func RightOpenRangeEqual

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

Checks whether two right open ranges are equal

func RightOpenRangeIteratorNext

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

Get the next element of the range

func Setter

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

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.

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
	// 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,
	params []value.Symbol,
	optParamCount int,
	postRestParamCount int,
	namedRestParam bool,
	sealed bool,
	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 []value.Symbol,
	optParamCount int,
	postRestParamCount int,
	namedRestParam bool,
	sealed bool,
	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 (*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(name value.Symbol)

Add a parameter to the method.

func (*BytecodeFunction) AddParameterString

func (f *BytecodeFunction) AddParameterString(name string)

Add a parameter to the method.

func (*BytecodeFunction) AddValue

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

Add a constant to the constant 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.Value

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, instructionIndex 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) 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) IncrementPostRestParameterCount

func (b *BytecodeFunction) IncrementPostRestParameterCount()

func (*BytecodeFunction) Inspect

func (b *BytecodeFunction) Inspect() string

func (*BytecodeFunction) InstanceVariables

func (*BytecodeFunction) InstanceVariables() value.SymbolMap

func (*BytecodeFunction) IsSealed

func (b *BytecodeFunction) IsSealed() bool

func (*BytecodeFunction) Name

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

func (*BytecodeFunction) NamedRestParameter

func (b *BytecodeFunction) NamedRestParameter() bool

func (*BytecodeFunction) OptionalParameterCount

func (b *BytecodeFunction) OptionalParameterCount() int

func (*BytecodeFunction) ParameterCount

func (b *BytecodeFunction) ParameterCount() int

func (*BytecodeFunction) Parameters

func (b *BytecodeFunction) Parameters() []value.Symbol

func (*BytecodeFunction) PostRestParameterCount

func (b *BytecodeFunction) PostRestParameterCount() int

func (*BytecodeFunction) SetNamedRestParameter

func (b *BytecodeFunction) SetNamedRestParameter(present bool)

func (*BytecodeFunction) SetOptionalParameterCount

func (b *BytecodeFunction) SetOptionalParameterCount(optParamCount int)

func (*BytecodeFunction) SetParameters

func (b *BytecodeFunction) SetParameters(params []value.Symbol)

func (*BytecodeFunction) SetPostRestParameterCount

func (b *BytecodeFunction) SetPostRestParameterCount(postParamCount int)

func (*BytecodeFunction) SetSealed

func (b *BytecodeFunction) SetSealed()

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 BytecodeFunctionWithNamedRestParameter

func BytecodeFunctionWithNamedRestParameter() BytecodeFunctionOption

func BytecodeFunctionWithOptionalParameters

func BytecodeFunctionWithOptionalParameters(optParams int) BytecodeFunctionOption

func BytecodeFunctionWithParameters

func BytecodeFunctionWithParameters(params []value.Symbol) BytecodeFunctionOption

func BytecodeFunctionWithPositionalRestParameter

func BytecodeFunctionWithPositionalRestParameter() BytecodeFunctionOption

func BytecodeFunctionWithPostParameters

func BytecodeFunctionWithPostParameters(postParams int) BytecodeFunctionOption

func BytecodeFunctionWithSealed

func BytecodeFunctionWithSealed() 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

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 {
	Bytecode *BytecodeFunction
	Self     value.Value
}

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

func NewClosure

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

Create a new closure

func (*Closure) Class

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

func (*Closure) Copy

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

func (*Closure) DirectClass

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

func (*Closure) Inspect

func (c *Closure) Inspect() string

func (*Closure) InstanceVariables

func (*Closure) InstanceVariables() value.SymbolMap

func (*Closure) SingletonClass

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

type DefOption

type DefOption func(*NativeMethod)

func DefWithNamedRestParameter

func DefWithNamedRestParameter() DefOption

Set the last parameter as a named rest parameter eg. `**rest`

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 ...string) DefOption

Define parameters used by the method

func DefWithPositionalRestParameter

func DefWithPositionalRestParameter() DefOption

Set the last parameter as a positional rest parameter eg. `*rest`

func DefWithPostParameters

func DefWithPostParameters(postParams int) DefOption

Define the number of parameters that appear after the positional rest parameter eg. 2 for `a, *b, c, d`

func DefWithSealed

func DefWithSealed() DefOption

Make the method non-overridable

type GetterMethod

type GetterMethod struct {
	AttributeName value.Symbol
	Doc           value.Value
	// contains filtered or unexported fields
}

A simple getter method.

func NewGetterMethod

func NewGetterMethod(attrName value.Symbol, sealed bool) *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.Value

func (*GetterMethod) DirectClass

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

func (*GetterMethod) Inspect

func (g *GetterMethod) Inspect() string

func (*GetterMethod) InstanceVariables

func (*GetterMethod) InstanceVariables() value.SymbolMap

func (*GetterMethod) IsSealed

func (g *GetterMethod) IsSealed() bool

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

func (g *GetterMethod) SetSealed()

func (*GetterMethod) SingletonClass

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

type GetterOption

type GetterOption func(*GetterMethod)

func GetterWithSealed

func GetterWithSealed(sealed bool) GetterOption

type IntSize

type IntSize uint8

Size of an integer.

type NativeFunction

type NativeFunction func(vm *VM, 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 []value.Symbol,
	optParams int,
	postParams int,
	namedRestParam bool,
	sealed bool,
	function NativeFunction,
) *NativeMethod

Create a new native method.

func (*NativeMethod) Class

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

func (*NativeMethod) Copy

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

func (*NativeMethod) DirectClass

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

func (*NativeMethod) Inspect

func (n *NativeMethod) Inspect() string

func (*NativeMethod) InstanceVariables

func (*NativeMethod) InstanceVariables() value.SymbolMap

func (*NativeMethod) IsSealed

func (n *NativeMethod) IsSealed() bool

func (*NativeMethod) Name

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

func (*NativeMethod) NamedRestParameter

func (n *NativeMethod) NamedRestParameter() bool

func (*NativeMethod) OptionalParameterCount

func (n *NativeMethod) OptionalParameterCount() int

func (*NativeMethod) ParameterCount

func (n *NativeMethod) ParameterCount() int

func (*NativeMethod) Parameters

func (n *NativeMethod) Parameters() []value.Symbol

func (*NativeMethod) PostRestParameterCount

func (n *NativeMethod) PostRestParameterCount() int

func (*NativeMethod) SetSealed

func (n *NativeMethod) SetSealed()

func (*NativeMethod) SingletonClass

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

type Option

type Option func(*VM) // 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.

type SetterMethod

type SetterMethod struct {
	Doc           value.Value
	AttributeName value.Symbol
	// contains filtered or unexported fields
}

A simple setter method.

func NewSetterMethod

func NewSetterMethod(attrName value.Symbol, sealed bool) *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.Value

func (*SetterMethod) DirectClass

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

func (*SetterMethod) Inspect

func (s *SetterMethod) Inspect() string

func (*SetterMethod) InstanceVariables

func (*SetterMethod) InstanceVariables() value.SymbolMap

func (*SetterMethod) IsSealed

func (s *SetterMethod) IsSealed() bool

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

func (s *SetterMethod) SetSealed()

func (*SetterMethod) SingletonClass

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

type SetterOption

type SetterOption func(*SetterMethod)

func SetterWithSealed

func SetterWithSealed(sealed bool) SetterOption

type VM

type VM struct {
	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) *VM

Create a new VM instance.

func (*VM) BuildStackTrace

func (vm *VM) BuildStackTrace() string

func (*VM) CallCallable

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

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

func (*VM) CallClosure

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

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

func (*VM) CallMethod

func (vm *VM) CallMethod(name value.Symbol, args ...value.Value) (value.Value, value.Value)

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

func (*VM) Err

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

Get the stored error.

func (*VM) ErrStackTrace

func (vm *VM) ErrStackTrace() string

Get the stored error stack trace.

func (*VM) InspectStack

func (vm *VM) InspectStack()

func (*VM) InterpretREPL

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

Execute the given bytecode chunk.

func (*VM) InterpretTopLevel

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

Execute the given bytecode chunk.

func (*VM) PrintError

func (vm *VM) PrintError()

func (*VM) ResetError

func (vm *VM) ResetError()

func (*VM) Stack

func (vm *VM) Stack() []value.Value

func (*VM) StackTop

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

Get the value on top of the value stack.

Jump to

Keyboard shortcuts

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