bas

package
v0.0.0-...-c7f8db1 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2022 License: MIT Imports: 22 Imported by: 2

Documentation

Index

Constants

View Source
const (
	ValueSize = unsafe.Sizeof(Value{})
)
View Source
const Version int64 = 499

Variables

View Source
var (
	Nil     = Value{}
	Zero    = Int64(0)
	NullStr = Str("")
	False   = Value{0, falseMarker}
	True    = Value{1, trueMarker}
)
View Source
var Proto struct {
	Object, Bool, Str, Int, Float, Func, Native, Bytes, Array, Error, Panic      Object
	NativeMap, NativePtr, NativeIntf, Channel                                    Object
	Reader, Writer, Closer, ReadWriter, ReadCloser, WriteCloser, ReadWriteCloser NativeMeta
	WrapperMeta, ReadlinesMeta, ArrayMeta, VarargMeta, BytesMeta, StringsMeta    NativeMeta
	ErrorMeta, PanicMeta                                                         NativeMeta
}

Functions

func AddTopFunc

func AddTopFunc(k string, f func(*Env))

func AddTopValue

func AddTopValue(k string, v Value)

func DeepEqual

func DeepEqual(a, b Value) bool

func Fprint

func Fprint(w io.Writer, values ...Value)

func Fprintf

func Fprintf(w io.Writer, format string, args ...Value)

func Fprintln

func Fprintln(w io.Writer, values ...Value)

func GetTopIndex

func GetTopIndex(v Value) int

func NewShape

func NewShape(s string) func(v Value) error

func TestShapeFast

func TestShapeFast(v Value, shape string) (err error)

func Write

func Write(w io.Writer, v Value) (int, error)

Types

type Env

type Env struct {
	A Value
	// contains filtered or unexported fields
}

Env is the environment for a function to run within. 'stack' represents the global stack, a running function use 'stack[stackOffset:]' as its local stack. 'A' stores the result of the execution. 'global' is the topmost function scope, a.k.a. Program.

func (*Env) Bool

func (env *Env) Bool(idx int) bool

Bool returns value at 'idx' in current stack and asserts its Type() to be a boolean.

func (*Env) Caller

func (env *Env) Caller() *Object

func (*Env) Copy

func (e *Env) Copy() *Env

func (*Env) CopyStack

func (env *Env) CopyStack() []Value

CopyStack returns a copy of current stack.

func (*Env) Float64

func (env *Env) Float64(idx int) float64

Float64 returns value at 'idx' in current stack and asserts its Type() to be a number.

func (*Env) Get

func (env *Env) Get(index int) Value

Get gets value at 'index' in current stack, Get(-1) means env.A.

func (*Env) Int

func (env *Env) Int(idx int) int

Int returns value at 'idx' in current stack and asserts its Type() to be a number.

func (*Env) Int64

func (env *Env) Int64(idx int) int64

Int64 returns value at 'idx' in current stack and asserts its Type() to be a number.

func (*Env) IntDefault

func (env *Env) IntDefault(idx int, defaultValue int) int

IntDefault returns value at 'idx' in current stack and asserts its Type() to be a number. If value is Nil, then 'defaultValue' will be returned.

func (*Env) Interface

func (env *Env) Interface(idx int) interface{}

Interface returns value at 'idx' in current stack as interface{}

func (*Env) Jump

func (e *Env) Jump(label string)

func (*Env) MustProgram

func (e *Env) MustProgram() *Program

func (*Env) Native

func (env *Env) Native(idx int) *Native

Native returns value at 'idx' in current stack and asserts its Type() to be a Native.

func (*Env) Num

func (env *Env) Num(idx int) Value

Num returns value at 'idx' in current stack and asserts its Type() to be a number.

func (*Env) Object

func (env *Env) Object(idx int) *Object

Object returns value at 'idx' in current stack and asserts its Type() to be an Object.

func (*Env) Self

func (env *Env) Self() *Object

func (*Env) Set

func (env *Env) Set(index int, value Value)

Set sets 'value' at 'index' in current stack.

func (*Env) SetA

func (env *Env) SetA(a Value) bool

func (*Env) SetError

func (env *Env) SetError(err error) bool

func (*Env) Shape

func (env *Env) Shape(idx int, s string) Value

func (*Env) Size

func (env *Env) Size() int

func (*Env) Stack

func (env *Env) Stack() []Value

Stack returns current stack as a reference.

func (*Env) Str

func (env *Env) Str(idx int) string

Str returns value at 'idx' in current stack and asserts its Type() to be a string.

func (*Env) StrDefault

func (env *Env) StrDefault(idx int, defaultValue string, minLen int) (res string)

func (*Env) String

func (env *Env) String() string

func (*Env) This

func (env *Env) This() Value

type ExecError

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

ExecError represents the runtime error/panic.

func (*ExecError) Error

func (e *ExecError) Error() string

func (*ExecError) GetCause

func (e *ExecError) GetCause() interface{}

func (*ExecError) IsPanic

func (e *ExecError) IsPanic() bool

type Map

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

func TopSymbols

func TopSymbols() Map

func (Map) Cap

func (m Map) Cap() int

Cap returns the capacity of the map in terms of key-value pairs, one pair is (ValueSize * 2 + 8) bytes.

func (*Map) Clear

func (m *Map) Clear()

Clear clears all keys in the map, where already allocated memory will be reused.

func (Map) Contains

func (m Map) Contains(k Value) bool

Contains returns true if the map contains 'k'.

func (Map) Copy

func (m Map) Copy() Map

func (Map) DebugString

func (m Map) DebugString() string

func (*Map) Delete

func (m *Map) Delete(k Value) (prev Value)

Delete deletes a key from the map, returns deleted value if existed

func (Map) FindNext

func (m Map) FindNext(k Value) (Value, Value)

FindNext finds the next key after 'k', returns nil if not found. The output is stable between map changes (e.g. Delete).

func (Map) Foreach

func (m Map) Foreach(f func(Value, *Value) bool)

Foreach iterates all keys in the map, for each of them, 'f(key, &value)' will be called. Values are passed by pointers and it is legal to manipulate them directly in 'f'. Deletions are allowed during Foreach(), but the iteration may be incomplete therefore.

func (Map) Get

func (m Map) Get(k Value) (v Value, exists bool)

Get retrieves the value by 'k', returns false as the second argument if not found.

func (*Map) Init

func (m *Map) Init(count int) *Map

Init pre-allocates enough memory for 'count' key and clears all old data.

func (Map) Len

func (m Map) Len() int

Len returns the count of keys in the map.

func (*Map) Merge

func (m *Map) Merge(src *Map) *Map

func (*Map) Set

func (m *Map) Set(k, v Value) (prev Value)

Set upserts a key-value pair in the map. Nil key is not allowed.

func (Map) String

func (m Map) String() string

func (Map) ToObject

func (m Map) ToObject() *Object

type Native

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

func NewNative

func NewNative(any interface{}) *Native

func NewNativeWithMeta

func NewNativeWithMeta(any interface{}, meta *NativeMeta) *Native

func (*Native) Append

func (a *Native) Append(v ...Value)

func (*Native) Cap

func (a *Native) Cap() int

func (*Native) Clear

func (a *Native) Clear()

func (*Native) Concat

func (a *Native) Concat(b *Native)

func (*Native) Copy

func (a *Native) Copy(start, end int, from *Native)

func (*Native) Get

func (a *Native) Get(v int) Value

func (*Native) GetKey

func (a *Native) GetKey(k Value) (Value, bool)

func (*Native) HasPrototype

func (a *Native) HasPrototype(p *Object) bool

func (*Native) IsTypedArray

func (a *Native) IsTypedArray() bool

func (*Native) IsUntypedArray

func (a *Native) IsUntypedArray() bool

func (*Native) Len

func (a *Native) Len() int

func (*Native) Marshal

func (a *Native) Marshal(w io.Writer, mt typ.MarshalType)

func (*Native) Prototype

func (a *Native) Prototype() *Object

func (*Native) Set

func (a *Native) Set(idx int, v Value)

func (*Native) SetKey

func (a *Native) SetKey(k, v Value)

func (*Native) Slice

func (a *Native) Slice(start, end int) *Native

func (*Native) SliceInplace

func (a *Native) SliceInplace(start, end int)

func (*Native) ToValue

func (a *Native) ToValue() Value

func (*Native) Unwrap

func (a *Native) Unwrap() interface{}

func (*Native) UnwrapFunc

func (a *Native) UnwrapFunc(f func(interface{}) interface{})

func (*Native) Values

func (a *Native) Values() []Value

type NativeMeta

type NativeMeta struct {
	Name         string
	Proto        *Object
	Len          func(*Native) int
	Cap          func(*Native) int
	Clear        func(*Native)
	Values       func(*Native) []Value
	Get          func(*Native, int) Value
	Set          func(*Native, int, Value)
	GetKey       func(*Native, Value) (Value, bool)
	SetKey       func(*Native, Value, Value)
	Append       func(*Native, ...Value)
	Slice        func(*Native, int, int) *Native
	SliceInplace func(*Native, int, int)
	Copy         func(*Native, int, int, *Native)
	Concat       func(*Native, *Native)
	Marshal      func(*Native, io.Writer, typ.MarshalType)
	Next         func(*Native, Value) Value
}

func NewEmptyNativeMeta

func NewEmptyNativeMeta(name string, proto *Object) *NativeMeta

type Object

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

func NewBareFunc

func NewBareFunc(f string, varg bool, np byte, ss uint16,
	locals, caps []string, jt map[string]int, code internal.Packet) *Object

func NewNamedObject

func NewNamedObject(name string, size int) *Object

func NewObject

func NewObject(size int) *Object

func (*Object) AddMethod

func (m *Object) AddMethod(name string, fun func(*Env)) *Object

AddMethod binds function 'fun' to property 'name' in the object, making 'fun' a method of the object. This differs from 'Set(name, Func(name, fun))' because the latter one, as not being a method, can't use 'this' argument when called.

func (*Object) Apply

func (m *Object) Apply(e *Env, this Value, args ...Value) Value

Apply calls the object with provided 'this' value, 'e' is for stacktracing and is optional.

func (*Object) Call

func (m *Object) Call(e *Env, args ...Value) (res Value)

Call calls the object, 'e' is for stacktracing and is optional.

func (*Object) Cap

func (m *Object) Cap() int

Cap returns the capacity of the object.

func (*Object) Clear

func (m *Object) Clear()

Clear clears all local properties in the object.

func (*Object) Contains

func (m *Object) Contains(name Value) bool

Contains returns true if object contains property 'name', inherited properties will also be checked.

func (*Object) Copy

func (m *Object) Copy() *Object

func (*Object) Delete

func (m *Object) Delete(name Value) (prev Value)

Delete deletes a local property from the object. Inherited properties are omitted and never deleted.

func (*Object) Find

func (m *Object) Find(name Value) (v Value, exists bool)

Find retrieves the property by 'name', returns false as the second argument if not found.

func (*Object) Foreach

func (m *Object) Foreach(f func(Value, *Value) bool)

Foreach iterates all local properties in the object, refer to 'Map.Foreach' for more helps.

func (*Object) Get

func (m *Object) Get(name Value) (v Value)

Get retrieves the property by 'name'.

func (*Object) GetDefault

func (m *Object) GetDefault(name, defaultValue Value) (v Value)

GetDefault retrieves the property by 'name', returns 'defaultValue' if not found.

func (*Object) GoString

func (obj *Object) GoString() string

func (*Object) HasOwnProperty

func (m *Object) HasOwnProperty(name Value) bool

HasOwnProperty returns true if 'name' is a local property in the object.

func (*Object) HasPrototype

func (m *Object) HasPrototype(proto *Object) bool

func (*Object) Len

func (m *Object) Len() int

Len returns the count of local properties in the object.

func (*Object) Merge

func (m *Object) Merge(src *Object) *Object

func (*Object) Name

func (m *Object) Name() string

func (*Object) Prototype

func (m *Object) Prototype() *Object

func (*Object) Set

func (m *Object) Set(name, v Value) (prev Value)

Set sets a local property in the object. Inherited property with the same name will be shadowed.

func (*Object) SetProp

func (m *Object) SetProp(name string, v Value) *Object

SetProp sets property by string 'name', which is short for Set(Str(name), v).

func (*Object) SetPrototype

func (m *Object) SetPrototype(m2 *Object) *Object

func (*Object) String

func (m *Object) String() string

func (*Object) ToMap

func (m *Object) ToMap() Map

func (*Object) ToValue

func (m *Object) ToValue() Value

func (*Object) TryApply

func (m *Object) TryApply(e *Env, this Value, args ...Value) (res Value)

TryApply calls the object with provided 'this' value, 'e' is for stacktracing and is optional. Panic will be recovered and returned as an error.

func (*Object) TryCall

func (m *Object) TryCall(e *Env, args ...Value) (res Value)

TryCall calls the object, 'e' is for stacktracing and is optional. Panic will be recovered and returned as an error.

type Program

type Program struct {
	File         string
	Source       string
	MaxStackSize int64
	Globals      Map
	Stdout       io.Writer
	Stderr       io.Writer
	Stdin        io.Reader
	// contains filtered or unexported fields
}

func NewBareProgram

func NewBareProgram(coreStack []Value, top *Object, symbols, funcs *Map) *Program

func (*Program) Get

func (p *Program) Get(k string) (v Value, ok bool)

func (*Program) GoString

func (p *Program) GoString() string

func (*Program) LocalsObject

func (p *Program) LocalsObject() *Object

func (*Program) Run

func (p *Program) Run() (v1 Value)

func (*Program) Set

func (p *Program) Set(k string, v Value) (ok bool)

func (*Program) Stop

func (p *Program) Stop()

Stop stops the program from running unsafely, it can't stop any Go-native functions or goroutines.

type Stacktrace

type Stacktrace struct {
	Cursor      uint32
	StackOffset uint32
	Callable    *Object
}

type Value

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

Value is the basic data type used by the intepreter, an empty Value naturally represent nil.

func Array

func Array(v ...Value) Value

Array creates an untyped array from values.

func Bool

func Bool(v bool) Value

Bool creates a boolean value.

func Byte

func Byte(s byte) Value

Byte creates a one-byte string value.

func Bytes

func Bytes(b []byte) Value

Bytes creates a bytes array.

func Error

func Error(e *Env, err interface{}) Value

Error creates an error, 'e' can be nil, indicating that the returned error has no stacktrace.

func Float64

func Float64(f float64) Value

Float64 creates a number value.

func Func

func Func(name string, f func(*Env)) Value

Func creates a callable object

func Int

func Int(i int) Value

Int creates a number value.

func Int64

func Int64(i int64) Value

Int64 creates a number value.

func Rune

func Rune(r rune) Value

Rune creates a one-rune string value encoded in UTF-8.

func Str

func Str(s string) Value

Str creates a string value.

func UnsafeStr

func UnsafeStr(b []byte) Value

UnsafeStr creates a string value from []byte, its content may change if []byte changed.

func ValueOf

func ValueOf(i interface{}) Value

ValueOf creates a Value from any golang types.

func (Value) AssertNumber

func (v Value) AssertNumber(msg string) Value

func (Value) AssertObject

func (v Value) AssertObject(msg string) *Object

func (Value) AssertShape

func (v Value) AssertShape(shape, msg string) Value

func (Value) AssertString

func (v Value) AssertString(msg string) string

func (Value) Bool

func (v Value) Bool() bool

Bool returns value as a boolean, Type() should be checked beforehand.

func (Value) Bytes

func (v Value) Bytes() []byte

func (Value) Closer

func (v Value) Closer() io.Closer

Closer creates an io.Closer from value, Close() may fail if value doesn't support closing.

func (Value) Duration

func (v Value) Duration() time.Duration

Duration returns the number value as a time.Duration, it assumes the number represents the Unix timestamp in seconds.

func (Value) Equal

func (v Value) Equal(r Value) bool

Equal returns true if two values are equal.

func (Value) Error

func (v Value) Error() *ExecError

func (Value) Float64

func (v Value) Float64() float64

Float64 returns value as a float (integers will be promoted to floats), Type() should be checked beforehand.

func (Value) HasPrototype

func (a Value) HasPrototype(p *Object) bool

func (Value) HashCode

func (v Value) HashCode() uint32

HashCode returns the hash of value.

func (Value) Int

func (v Value) Int() int

Int returns value as an int.

func (Value) Int64

func (v Value) Int64() int64

Int64 returns value as an int64 (floats will be truncated to integers), Type() should be checked beforehand.

func (Value) Interface

func (v Value) Interface() interface{}

Interface returns value as an interface{}

func (Value) IsArray

func (v Value) IsArray() bool

IsArray returns true if value is an array.

func (Value) IsBytes

func (v Value) IsBytes() bool

func (Value) IsError

func (v Value) IsError() bool

func (Value) IsFalse

func (v Value) IsFalse() bool

IsFalse returns true if value is falsy: nil, false, empty string or 0.

func (Value) IsInt64

func (v Value) IsInt64() bool

IsInt64 returns true if value is an integer number.

func (Value) IsNil

func (v Value) IsNil() bool

IsNil returns true if value is nil.

func (Value) IsNumber

func (v Value) IsNumber() bool

IsNumber returns true if value is a number.

func (Value) IsObject

func (v Value) IsObject() bool

IsObject returns true if value is an object.

func (Value) IsPanic

func (v Value) IsPanic() bool

func (Value) IsString

func (v Value) IsString() bool

IsString returns true if value is a string.

func (Value) IsTrue

func (v Value) IsTrue() bool

IsTrue returns true if value is not falsy.

func (Value) JSONString

func (v Value) JSONString() string

func (Value) Len

func (v Value) Len() int

func (Value) Less

func (a Value) Less(b Value) bool

func (Value) MarshalJSON

func (v Value) MarshalJSON() ([]byte, error)

func (Value) Native

func (v Value) Native() *Native

Native returns value as a Native, Type() should be checked beforehand.

func (Value) Object

func (v Value) Object() *Object

Object returns value as an Object, Type() should be checked beforehand.

func (Value) Reader

func (v Value) Reader() io.Reader

Reader creates an io.Reader from value, Read() may fail if value doesn't support reading.

func (Value) Str

func (v Value) Str() string

Str returns value as a string, Type() should be checked beforehand.

func (Value) String

func (v Value) String() string

func (Value) Stringify

func (v Value) Stringify(p io.Writer, j typ.MarshalType)

func (Value) ToType

func (v Value) ToType(t reflect.Type) reflect.Value

ToType converts value to reflect.Value based on reflect.Type. The result, even not being Zero, may be illegal to use in certain calls.

func (Value) Type

func (v Value) Type() typ.ValueType

Type returns the type of value.

func (Value) UnsafeFloat64

func (v Value) UnsafeFloat64() float64

func (Value) UnsafeInt64

func (v Value) UnsafeInt64() int64

func (Value) Writer

func (v Value) Writer() io.Writer

Writer creates an io.Writer from value, Write() may fail if value doesn't support writing.

Jump to

Keyboard shortcuts

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