Documentation ¶
Overview ¶
Example ¶
// Tengo script code src := ` each := func(seq, fn) { for x in seq { fn(x) } } sum := 0 mul := 1 each([a, b, c, d], func(x) { sum += x mul *= x })` // create a new Script instance script := tengo.NewScript([]byte(src)) // set values _ = script.Add("a", 1) _ = script.Add("b", 9) _ = script.Add("c", 8) _ = script.Add("d", 4) // run the script compiled, err := script.RunContext(context.Background()) if err != nil { panic(err) } // retrieve values sum := compiled.Get("sum") mul := compiled.Get("mul") fmt.Println(sum, mul)
Output: 22 288
Index ¶
- Constants
- Variables
- func CountObjects(o Object) (c int)
- func Format(format string, a ...Object) (string, error)
- func FormatInstructions(b []byte, posOffset int) []string
- func MakeInstruction(opcode parser.Opcode, operands ...int) []byte
- func ToBool(o Object) (v bool, ok bool)
- func ToByteSlice(o Object) (v []byte, ok bool)
- func ToFloat64(o Object) (v float64, ok bool)
- func ToInt(o Object) (v int, ok bool)
- func ToInt64(o Object) (v int64, ok bool)
- func ToInterface(o Object) (res interface{})
- func ToRune(o Object) (v rune, ok bool)
- func ToString(o Object) (v string, ok bool)
- func ToTime(o Object) (v time.Time, ok bool)
- type Array
- func (o *Array) BinaryOp(op token.Token, rhs Object) (Object, error)
- func (o *Array) CanIterate() bool
- func (o *Array) Copy() Object
- func (o *Array) Equals(x Object) bool
- func (o *Array) IndexGet(index Object) (res Object, err error)
- func (o *Array) IndexSet(index, value Object) (err error)
- func (o *Array) IsFalsy() bool
- func (o *Array) Iterate() Iterator
- func (o *Array) String() string
- func (o *Array) TypeName() string
- type ArrayIterator
- func (i *ArrayIterator) Copy() Object
- func (i *ArrayIterator) Equals(Object) bool
- func (i *ArrayIterator) IsFalsy() bool
- func (i *ArrayIterator) Key() Object
- func (i *ArrayIterator) Next() bool
- func (i *ArrayIterator) String() string
- func (i *ArrayIterator) TypeName() string
- func (i *ArrayIterator) Value() Object
- type Bool
- type BuiltinFunction
- type BuiltinModule
- type Bytecode
- type Bytes
- func (o *Bytes) BinaryOp(op token.Token, rhs Object) (Object, error)
- func (o *Bytes) CanIterate() bool
- func (o *Bytes) Copy() Object
- func (o *Bytes) Equals(x Object) bool
- func (o *Bytes) IndexGet(index Object) (res Object, err error)
- func (o *Bytes) IsFalsy() bool
- func (o *Bytes) Iterate() Iterator
- func (o *Bytes) String() string
- func (o *Bytes) TypeName() string
- type BytesIterator
- type CallableFunc
- type Char
- type Compiled
- func (c *Compiled) Clone() *Compiled
- func (c *Compiled) Get(name string) *Variable
- func (c *Compiled) GetAll() []*Variable
- func (c *Compiled) IsDefined(name string) bool
- func (c *Compiled) Run() error
- func (c *Compiled) RunContext(ctx context.Context) (err error)
- func (c *Compiled) Set(name string, value interface{}) error
- type CompiledFunction
- type Compiler
- type CompilerError
- type ErrInvalidArgumentType
- type Error
- type Float
- type ImmutableArray
- func (o *ImmutableArray) BinaryOp(op token.Token, rhs Object) (Object, error)
- func (o *ImmutableArray) CanIterate() bool
- func (o *ImmutableArray) Copy() Object
- func (o *ImmutableArray) Equals(x Object) bool
- func (o *ImmutableArray) IndexGet(index Object) (res Object, err error)
- func (o *ImmutableArray) IsFalsy() bool
- func (o *ImmutableArray) Iterate() Iterator
- func (o *ImmutableArray) String() string
- func (o *ImmutableArray) TypeName() string
- type ImmutableMap
- func (o *ImmutableMap) CanIterate() bool
- func (o *ImmutableMap) Copy() Object
- func (o *ImmutableMap) Equals(x Object) bool
- func (o *ImmutableMap) IndexGet(index Object) (res Object, err error)
- func (o *ImmutableMap) IsFalsy() bool
- func (o *ImmutableMap) Iterate() Iterator
- func (o *ImmutableMap) String() string
- func (o *ImmutableMap) TypeName() string
- type Importable
- type Int
- type Iterator
- type Map
- func (o *Map) CanIterate() bool
- func (o *Map) Copy() Object
- func (o *Map) Equals(x Object) bool
- func (o *Map) IndexGet(index Object) (res Object, err error)
- func (o *Map) IndexSet(index, value Object) (err error)
- func (o *Map) IsFalsy() bool
- func (o *Map) Iterate() Iterator
- func (o *Map) String() string
- func (o *Map) TypeName() string
- type MapIterator
- type ModuleMap
- func (m *ModuleMap) Add(name string, module Importable)
- func (m *ModuleMap) AddBuiltinModule(name string, attrs map[string]Object)
- func (m *ModuleMap) AddMap(o *ModuleMap)
- func (m *ModuleMap) AddSourceModule(name string, src []byte)
- func (m *ModuleMap) Copy() *ModuleMap
- func (m *ModuleMap) Get(name string) Importable
- func (m *ModuleMap) GetBuiltinModule(name string) *BuiltinModule
- func (m *ModuleMap) GetSourceModule(name string) *SourceModule
- func (m *ModuleMap) Len() int
- func (m *ModuleMap) Remove(name string)
- type Object
- type ObjectImpl
- func (o *ObjectImpl) BinaryOp(_ token.Token, _ Object) (Object, error)
- func (o *ObjectImpl) Call(_ ...Object) (ret Object, err error)
- func (o *ObjectImpl) CanCall() bool
- func (o *ObjectImpl) CanIterate() bool
- func (o *ObjectImpl) Copy() Object
- func (o *ObjectImpl) Equals(x Object) bool
- func (o *ObjectImpl) IndexGet(_ Object) (res Object, err error)
- func (o *ObjectImpl) IndexSet(_, _ Object) (err error)
- func (o *ObjectImpl) IsFalsy() bool
- func (o *ObjectImpl) Iterate() Iterator
- func (o *ObjectImpl) String() string
- func (o *ObjectImpl) TypeName() string
- type ObjectPtr
- type Script
- func (s *Script) Add(name string, value interface{}) error
- func (s *Script) Compile() (*Compiled, error)
- func (s *Script) EnableFileImport(enable bool)
- func (s *Script) Remove(name string) bool
- func (s *Script) Run() (compiled *Compiled, err error)
- func (s *Script) RunContext(ctx context.Context) (compiled *Compiled, err error)
- func (s *Script) SetImportDir(dir string) error
- func (s *Script) SetImports(modules *ModuleMap)
- func (s *Script) SetMaxAllocs(n int64)
- func (s *Script) SetMaxConstObjects(n int)
- type SourceModule
- type String
- func (o *String) BinaryOp(op token.Token, rhs Object) (Object, error)
- func (o *String) CanIterate() bool
- func (o *String) Copy() Object
- func (o *String) Equals(x Object) bool
- func (o *String) IndexGet(index Object) (res Object, err error)
- func (o *String) IsFalsy() bool
- func (o *String) Iterate() Iterator
- func (o *String) String() string
- func (o *String) TypeName() string
- type StringIterator
- func (i *StringIterator) Copy() Object
- func (i *StringIterator) Equals(Object) bool
- func (i *StringIterator) IsFalsy() bool
- func (i *StringIterator) Key() Object
- func (i *StringIterator) Next() bool
- func (i *StringIterator) String() string
- func (i *StringIterator) TypeName() string
- func (i *StringIterator) Value() Object
- type Symbol
- type SymbolScope
- type SymbolTable
- func (t *SymbolTable) BuiltinSymbols() []*Symbol
- func (t *SymbolTable) Define(name string) *Symbol
- func (t *SymbolTable) DefineBuiltin(index int, name string) *Symbol
- func (t *SymbolTable) Fork(block bool) *SymbolTable
- func (t *SymbolTable) FreeSymbols() []*Symbol
- func (t *SymbolTable) MaxSymbols() int
- func (t *SymbolTable) Names() []string
- func (t *SymbolTable) Parent(skipBlock bool) *SymbolTable
- func (t *SymbolTable) Resolve(name string, recur bool) (*Symbol, int, bool)
- type Time
- type Undefined
- func (o *Undefined) CanIterate() bool
- func (o *Undefined) Copy() Object
- func (o *Undefined) Equals(x Object) bool
- func (o *Undefined) IndexGet(_ Object) (Object, error)
- func (o *Undefined) IsFalsy() bool
- func (o *Undefined) Iterate() Iterator
- func (o *Undefined) Key() Object
- func (o *Undefined) Next() bool
- func (o *Undefined) String() string
- func (o *Undefined) TypeName() string
- func (o *Undefined) Value() Object
- type UserFunction
- type VM
- type Variable
- func (v *Variable) Array() []interface{}
- func (v *Variable) Bool() bool
- func (v *Variable) Bytes() []byte
- func (v *Variable) Char() rune
- func (v *Variable) Error() error
- func (v *Variable) Float() float64
- func (v *Variable) Int() int
- func (v *Variable) Int64() int64
- func (v *Variable) IsUndefined() bool
- func (v *Variable) Map() map[string]interface{}
- func (v *Variable) Name() string
- func (v *Variable) Object() Object
- func (v *Variable) String() string
- func (v *Variable) Value() interface{}
- func (v *Variable) ValueType() string
Examples ¶
Constants ¶
const ( // GlobalsSize is the maximum number of global variables for a VM. GlobalsSize = 1024 // StackSize is the maximum stack size for a VM. StackSize = 2048 // MaxFrames is the maximum number of function frames for a VM. MaxFrames = 1024 )
Variables ¶
var ( // ErrStackOverflow is a stack overflow error. ErrStackOverflow = errors.New("stack overflow") // ErrObjectAllocLimit is an objects allocation limit error. ErrObjectAllocLimit = errors.New("object allocation limit exceeded") // ErrIndexOutOfBounds is an error where a given index is out of the // bounds. ErrIndexOutOfBounds = errors.New("index out of bounds") // ErrInvalidIndexType represents an invalid index type. ErrInvalidIndexType = errors.New("invalid index type") // ErrInvalidIndexValueType represents an invalid index value type. ErrInvalidIndexValueType = errors.New("invalid index value type") // ErrInvalidIndexOnError represents an invalid index on error. ErrInvalidIndexOnError = errors.New("invalid index on error") // ErrInvalidOperator represents an error for invalid operator usage. ErrInvalidOperator = errors.New("invalid operator") // ErrWrongNumArguments represents a wrong number of arguments error. ErrWrongNumArguments = errors.New("wrong number of arguments") // ErrBytesLimit represents an error where the size of bytes value exceeds // the limit. ErrBytesLimit = errors.New("exceeding bytes size limit") // ErrStringLimit represents an error where the size of string value // exceeds the limit. ErrStringLimit = errors.New("exceeding string size limit") // ErrNotIndexable is an error where an Object is not indexable. ErrNotIndexable = errors.New("not indexable") // ErrNotIndexAssignable is an error where an Object is not index // assignable. ErrNotIndexAssignable = errors.New("not index-assignable") // ErrNotImplemented is an error where an Object has not implemented a // required method. ErrNotImplemented = errors.New("not implemented") )
var ( // MaxStringLen is the maximum byte-length for string value. Note this // limit applies to all compiler/VM instances in the process. MaxStringLen = 2147483647 // MaxBytesLen is the maximum length for bytes value. Note this limit // applies to all compiler/VM instances in the process. MaxBytesLen = 2147483647 )
Functions ¶
func CountObjects ¶
CountObjects returns the number of objects that a given object o contains. For scalar value types, it will always be 1. For compound value types, this will include its elements and all of their elements recursively.
func FormatInstructions ¶
FormatInstructions returns string representation of bytecode instructions.
func MakeInstruction ¶
MakeInstruction returns a bytecode for an opcode and the operands.
func ToByteSlice ¶
ToByteSlice will try to convert object o to []byte value.
func ToInterface ¶
func ToInterface(o Object) (res interface{})
ToInterface attempts to convert an object o to an interface{} value
Types ¶
type Array ¶
type Array struct { ObjectImpl Value []Object }
Array represents an array of objects.
func (*Array) BinaryOp ¶
BinaryOp returns another object that is the result of a given binary operator and a right-hand side object.
func (*Array) CanIterate ¶
CanIterate returns whether the Object can be Iterated.
func (*Array) Equals ¶
Equals returns true if the value of the type is equal to the value of another object.
type ArrayIterator ¶
type ArrayIterator struct { ObjectImpl // contains filtered or unexported fields }
ArrayIterator is an iterator for an array.
func (*ArrayIterator) Equals ¶
func (i *ArrayIterator) Equals(Object) bool
Equals returns true if the value of the type is equal to the value of another object.
func (*ArrayIterator) IsFalsy ¶
func (i *ArrayIterator) IsFalsy() bool
IsFalsy returns true if the value of the type is falsy.
func (*ArrayIterator) Key ¶
func (i *ArrayIterator) Key() Object
Key returns the key or index value of the current element.
func (*ArrayIterator) Next ¶
func (i *ArrayIterator) Next() bool
Next returns true if there are more elements to iterate.
func (*ArrayIterator) String ¶
func (i *ArrayIterator) String() string
func (*ArrayIterator) TypeName ¶
func (i *ArrayIterator) TypeName() string
TypeName returns the name of the type.
func (*ArrayIterator) Value ¶
func (i *ArrayIterator) Value() Object
Value returns the value of the current element.
type Bool ¶
type Bool struct { ObjectImpl // contains filtered or unexported fields }
Bool represents a boolean value.
func (*Bool) Equals ¶
Equals returns true if the value of the type is equal to the value of another object.
type BuiltinFunction ¶
type BuiltinFunction struct { ObjectImpl Name string Value CallableFunc }
BuiltinFunction represents a builtin function.
func GetAllBuiltinFunctions ¶
func GetAllBuiltinFunctions() []*BuiltinFunction
GetAllBuiltinFunctions returns all builtin function objects.
func (*BuiltinFunction) Call ¶
func (o *BuiltinFunction) Call(args ...Object) (Object, error)
Call executes a builtin function.
func (*BuiltinFunction) CanCall ¶
func (o *BuiltinFunction) CanCall() bool
CanCall returns whether the Object can be Called.
func (*BuiltinFunction) Copy ¶
func (o *BuiltinFunction) Copy() Object
Copy returns a copy of the type.
func (*BuiltinFunction) Equals ¶
func (o *BuiltinFunction) Equals(_ Object) bool
Equals returns true if the value of the type is equal to the value of another object.
func (*BuiltinFunction) String ¶
func (o *BuiltinFunction) String() string
func (*BuiltinFunction) TypeName ¶
func (o *BuiltinFunction) TypeName() string
TypeName returns the name of the type.
type BuiltinModule ¶
BuiltinModule is an importable module that's written in Go.
func (*BuiltinModule) AsImmutableMap ¶
func (m *BuiltinModule) AsImmutableMap(moduleName string) *ImmutableMap
AsImmutableMap converts builtin module into an immutable map.
func (*BuiltinModule) Import ¶
func (m *BuiltinModule) Import(moduleName string) (interface{}, error)
Import returns an immutable map for the module.
type Bytecode ¶
type Bytecode struct { FileSet *parser.SourceFileSet MainFunction *CompiledFunction Constants []Object }
Bytecode is a compiled instructions and constants.
func (*Bytecode) CountObjects ¶
CountObjects returns the number of objects found in Constants.
func (*Bytecode) FormatConstants ¶
FormatConstants returns human readable string representations of compiled constants.
func (*Bytecode) FormatInstructions ¶
FormatInstructions returns human readable string representations of compiled instructions.
func (*Bytecode) RemoveDuplicates ¶
func (b *Bytecode) RemoveDuplicates()
RemoveDuplicates finds and remove the duplicate values in Constants. Note this function mutates Bytecode.
type Bytes ¶
type Bytes struct { ObjectImpl Value []byte }
Bytes represents a byte array.
func (*Bytes) BinaryOp ¶
BinaryOp returns another object that is the result of a given binary operator and a right-hand side object.
func (*Bytes) CanIterate ¶
CanIterate returns whether the Object can be Iterated.
func (*Bytes) Equals ¶
Equals returns true if the value of the type is equal to the value of another object.
type BytesIterator ¶
type BytesIterator struct { ObjectImpl // contains filtered or unexported fields }
BytesIterator represents an iterator for a string.
func (*BytesIterator) Equals ¶
func (i *BytesIterator) Equals(Object) bool
Equals returns true if the value of the type is equal to the value of another object.
func (*BytesIterator) Key ¶
func (i *BytesIterator) Key() Object
Key returns the key or index value of the current element.
func (*BytesIterator) Next ¶
func (i *BytesIterator) Next() bool
Next returns true if there are more elements to iterate.
func (*BytesIterator) String ¶
func (i *BytesIterator) String() string
func (*BytesIterator) TypeName ¶
func (i *BytesIterator) TypeName() string
TypeName returns the name of the type.
func (*BytesIterator) Value ¶
func (i *BytesIterator) Value() Object
Value returns the value of the current element.
type CallableFunc ¶
CallableFunc is a function signature for the callable functions.
type Char ¶
type Char struct { ObjectImpl Value rune }
Char represents a character value.
func (*Char) BinaryOp ¶
BinaryOp returns another object that is the result of a given binary operator and a right-hand side object.
func (*Char) Equals ¶
Equals returns true if the value of the type is equal to the value of another object.
type Compiled ¶
type Compiled struct {
// contains filtered or unexported fields
}
Compiled is a compiled instance of the user script. Use Script.Compile() to create Compiled object.
func (*Compiled) Clone ¶
Clone creates a new copy of Compiled. Cloned copies are safe for concurrent use by multiple goroutines.
func (*Compiled) IsDefined ¶
IsDefined returns true if the variable name is defined (has value) before or after the execution.
func (*Compiled) RunContext ¶
RunContext is like Run but includes a context.
type CompiledFunction ¶
type CompiledFunction struct { ObjectImpl Instructions []byte NumLocals int // number of local variables (including function parameters) NumParameters int VarArgs bool SourceMap map[int]parser.Pos Free []*ObjectPtr UsesReceiver bool }
CompiledFunction represents a compiled function.
func (*CompiledFunction) CanCall ¶
func (o *CompiledFunction) CanCall() bool
CanCall returns whether the Object can be Called.
func (*CompiledFunction) Copy ¶
func (o *CompiledFunction) Copy() Object
Copy returns a copy of the type.
func (*CompiledFunction) Equals ¶
func (o *CompiledFunction) Equals(_ Object) bool
Equals returns true if the value of the type is equal to the value of another object.
func (*CompiledFunction) SourcePos ¶
func (o *CompiledFunction) SourcePos(ip int) parser.Pos
SourcePos returns the source position of the instruction at ip.
func (*CompiledFunction) String ¶
func (o *CompiledFunction) String() string
func (*CompiledFunction) TypeName ¶
func (o *CompiledFunction) TypeName() string
TypeName returns the name of the type.
type Compiler ¶
type Compiler struct {
// contains filtered or unexported fields
}
Compiler compiles the AST into a bytecode.
func NewCompiler ¶
func NewCompiler( file *parser.SourceFile, symbolTable *SymbolTable, constants []Object, modules *ModuleMap, trace io.Writer, ) *Compiler
NewCompiler creates a Compiler.
func (*Compiler) EnableFileImport ¶
EnableFileImport enables or disables module loading from local files. Local file modules are disabled by default.
func (*Compiler) SetImportDir ¶
SetImportDir sets the initial import directory path for file imports.
type CompilerError ¶
type CompilerError struct { FileSet *parser.SourceFileSet Node parser.Node Err error }
CompilerError represents a compiler error.
func (*CompilerError) Error ¶
func (e *CompilerError) Error() string
type ErrInvalidArgumentType ¶
ErrInvalidArgumentType represents an invalid argument value type error.
func (ErrInvalidArgumentType) Error ¶
func (e ErrInvalidArgumentType) Error() string
type Error ¶
type Error struct { ObjectImpl Value Object }
Error represents an error value.
func (*Error) Equals ¶
Equals returns true if the value of the type is equal to the value of another object.
type Float ¶
type Float struct { ObjectImpl Value float64 }
Float represents a floating point number value.
func (*Float) BinaryOp ¶
BinaryOp returns another object that is the result of a given binary operator and a right-hand side object.
func (*Float) Equals ¶
Equals returns true if the value of the type is equal to the value of another object.
type ImmutableArray ¶
type ImmutableArray struct { ObjectImpl Value []Object }
ImmutableArray represents an immutable array of objects.
func (*ImmutableArray) BinaryOp ¶
BinaryOp returns another object that is the result of a given binary operator and a right-hand side object.
func (*ImmutableArray) CanIterate ¶
func (o *ImmutableArray) CanIterate() bool
CanIterate returns whether the Object can be Iterated.
func (*ImmutableArray) Copy ¶
func (o *ImmutableArray) Copy() Object
Copy returns a copy of the type.
func (*ImmutableArray) Equals ¶
func (o *ImmutableArray) Equals(x Object) bool
Equals returns true if the value of the type is equal to the value of another object.
func (*ImmutableArray) IndexGet ¶
func (o *ImmutableArray) IndexGet(index Object) (res Object, err error)
IndexGet returns an element at a given index.
func (*ImmutableArray) IsFalsy ¶
func (o *ImmutableArray) IsFalsy() bool
IsFalsy returns true if the value of the type is falsy.
func (*ImmutableArray) Iterate ¶
func (o *ImmutableArray) Iterate() Iterator
Iterate creates an array iterator.
func (*ImmutableArray) String ¶
func (o *ImmutableArray) String() string
func (*ImmutableArray) TypeName ¶
func (o *ImmutableArray) TypeName() string
TypeName returns the name of the type.
type ImmutableMap ¶
type ImmutableMap struct { ObjectImpl Value map[string]Object }
ImmutableMap represents an immutable map object.
func (*ImmutableMap) CanIterate ¶
func (o *ImmutableMap) CanIterate() bool
CanIterate returns whether the Object can be Iterated.
func (*ImmutableMap) Equals ¶
func (o *ImmutableMap) Equals(x Object) bool
Equals returns true if the value of the type is equal to the value of another object.
func (*ImmutableMap) IndexGet ¶
func (o *ImmutableMap) IndexGet(index Object) (res Object, err error)
IndexGet returns the value for the given key.
func (*ImmutableMap) IsFalsy ¶
func (o *ImmutableMap) IsFalsy() bool
IsFalsy returns true if the value of the type is falsy.
func (*ImmutableMap) Iterate ¶
func (o *ImmutableMap) Iterate() Iterator
Iterate creates an immutable map iterator.
func (*ImmutableMap) String ¶
func (o *ImmutableMap) String() string
func (*ImmutableMap) TypeName ¶
func (o *ImmutableMap) TypeName() string
TypeName returns the name of the type.
type Importable ¶
type Importable interface { // Import should return either an Object or module source code ([]byte). Import(moduleName string) (interface{}, error) }
Importable interface represents importable module instance.
type Int ¶
type Int struct { ObjectImpl Value int64 }
Int represents an integer value.
func (*Int) BinaryOp ¶
BinaryOp returns another object that is the result of a given binary operator and a right-hand side object.
func (*Int) Equals ¶
Equals returns true if the value of the type is equal to the value of another object.
type Iterator ¶
type Iterator interface { Object // Next returns true if there are more elements to iterate. Next() bool // Key returns the key or index value of the current element. Key() Object // Value returns the value of the current element. Value() Object }
Iterator represents an iterator for underlying data type.
type Map ¶
type Map struct { ObjectImpl Value map[string]Object }
Map represents a map of objects.
func (*Map) CanIterate ¶
CanIterate returns whether the Object can be Iterated.
func (*Map) Equals ¶
Equals returns true if the value of the type is equal to the value of another object.
type MapIterator ¶
type MapIterator struct { ObjectImpl // contains filtered or unexported fields }
MapIterator represents an iterator for the map.
func (*MapIterator) Equals ¶
func (i *MapIterator) Equals(Object) bool
Equals returns true if the value of the type is equal to the value of another object.
func (*MapIterator) IsFalsy ¶
func (i *MapIterator) IsFalsy() bool
IsFalsy returns true if the value of the type is falsy.
func (*MapIterator) Key ¶
func (i *MapIterator) Key() Object
Key returns the key or index value of the current element.
func (*MapIterator) Next ¶
func (i *MapIterator) Next() bool
Next returns true if there are more elements to iterate.
func (*MapIterator) String ¶
func (i *MapIterator) String() string
func (*MapIterator) TypeName ¶
func (i *MapIterator) TypeName() string
TypeName returns the name of the type.
func (*MapIterator) Value ¶
func (i *MapIterator) Value() Object
Value returns the value of the current element.
type ModuleMap ¶
type ModuleMap struct {
// contains filtered or unexported fields
}
ModuleMap represents a set of named modules. Use NewModuleMap to create a new module map.
func (*ModuleMap) Add ¶
func (m *ModuleMap) Add(name string, module Importable)
Add adds an import module.
func (*ModuleMap) AddBuiltinModule ¶
AddBuiltinModule adds a builtin module.
func (*ModuleMap) AddSourceModule ¶
AddSourceModule adds a source module.
func (*ModuleMap) Get ¶
func (m *ModuleMap) Get(name string) Importable
Get returns an import module identified by name. It returns if the name is not found.
func (*ModuleMap) GetBuiltinModule ¶
func (m *ModuleMap) GetBuiltinModule(name string) *BuiltinModule
GetBuiltinModule returns a builtin module identified by name. It returns if the name is not found or the module is not a builtin module.
func (*ModuleMap) GetSourceModule ¶
func (m *ModuleMap) GetSourceModule(name string) *SourceModule
GetSourceModule returns a source module identified by name. It returns if the name is not found or the module is not a source module.
type Object ¶
type Object interface { // TypeName should return the name of the type. TypeName() string // String should return a string representation of the type's value. String() string // BinaryOp should return another object that is the result of a given // binary operator and a right-hand side object. If BinaryOp returns an // error, the VM will treat it as a run-time error. BinaryOp(op token.Token, rhs Object) (Object, error) // IsFalsy should return true if the value of the type should be considered // as falsy. IsFalsy() bool // Equals should return true if the value of the type should be considered // as equal to the value of another object. Equals(another Object) bool // Copy should return a copy of the type (and its value). Copy function // will be used for copy() builtin function which is expected to deep-copy // the values generally. Copy() Object // IndexGet should take an index Object and return a result Object or an // error for indexable objects. Indexable is an object that can take an // index and return an object. If error is returned, the runtime will treat // it as a run-time error and ignore returned value. If Object is not // indexable, ErrNotIndexable should be returned as error. If nil is // returned as value, it will be converted to UndefinedToken value by the // runtime. IndexGet(index Object) (value Object, err error) // IndexSet should take an index Object and a value Object for index // assignable objects. Index assignable is an object that can take an index // and a value on the left-hand side of the assignment statement. If Object // is not index assignable, ErrNotIndexAssignable should be returned as // error. If an error is returned, it will be treated as a run-time error. IndexSet(index, value Object) error // Iterate should return an Iterator for the type. Iterate() Iterator // CanIterate should return whether the Object can be Iterated. CanIterate() bool // Call should take an arbitrary number of arguments and returns a return // value and/or an error, which the VM will consider as a run-time error. Call(args ...Object) (ret Object, err error) // CanCall should return whether the Object can be Called. CanCall() bool }
Object represents an object in the VM.
func FromInterface ¶
FromInterface will attempt to convert an interface{} v to a Tengo Object
type ObjectImpl ¶
type ObjectImpl struct { }
ObjectImpl represents a default Object Implementation. To defined a new value type, one can embed ObjectImpl in their type declarations to avoid implementing all non-significant methods. TypeName() and String() methods still need to be implemented.
func (*ObjectImpl) BinaryOp ¶
BinaryOp returns another object that is the result of a given binary operator and a right-hand side object.
func (*ObjectImpl) Call ¶
func (o *ObjectImpl) Call(_ ...Object) (ret Object, err error)
Call takes an arbitrary number of arguments and returns a return value and/or an error.
func (*ObjectImpl) CanCall ¶
func (o *ObjectImpl) CanCall() bool
CanCall returns whether the Object can be Called.
func (*ObjectImpl) CanIterate ¶
func (o *ObjectImpl) CanIterate() bool
CanIterate returns whether the Object can be Iterated.
func (*ObjectImpl) Equals ¶
func (o *ObjectImpl) Equals(x Object) bool
Equals returns true if the value of the type is equal to the value of another object.
func (*ObjectImpl) IndexGet ¶
func (o *ObjectImpl) IndexGet(_ Object) (res Object, err error)
IndexGet returns an element at a given index.
func (*ObjectImpl) IndexSet ¶
func (o *ObjectImpl) IndexSet(_, _ Object) (err error)
IndexSet sets an element at a given index.
func (*ObjectImpl) IsFalsy ¶
func (o *ObjectImpl) IsFalsy() bool
IsFalsy returns true if the value of the type is falsy.
func (*ObjectImpl) String ¶
func (o *ObjectImpl) String() string
func (*ObjectImpl) TypeName ¶
func (o *ObjectImpl) TypeName() string
TypeName returns the name of the type.
type ObjectPtr ¶
type ObjectPtr struct { ObjectImpl Value *Object }
ObjectPtr represents a free variable.
func (*ObjectPtr) Equals ¶
Equals returns true if the value of the type is equal to the value of another object.
type Script ¶
type Script struct {
// contains filtered or unexported fields
}
Script can simplify compilation and execution of embedded scripts.
func (*Script) Compile ¶
Compile compiles the script with all the defined variables, and, returns Compiled object.
func (*Script) EnableFileImport ¶
EnableFileImport enables or disables module loading from local files. Local file modules are disabled by default.
func (*Script) Remove ¶
Remove removes (undefines) an existing variable for the script. It returns false if the variable name is not defined.
func (*Script) Run ¶
Run compiles and runs the scripts. Use returned compiled object to access global variables.
func (*Script) RunContext ¶
RunContext is like Run but includes a context.
func (*Script) SetImportDir ¶
SetImportDir sets the initial import directory for script files.
func (*Script) SetImports ¶
SetImports sets import modules.
func (*Script) SetMaxAllocs ¶
SetMaxAllocs sets the maximum number of objects allocations during the run time. Compiled script will return ErrObjectAllocLimit error if it exceeds this limit.
func (*Script) SetMaxConstObjects ¶
SetMaxConstObjects sets the maximum number of objects in the compiled constants.
type SourceModule ¶
type SourceModule struct {
Src []byte
}
SourceModule is an importable module that's written in Tengo.
func (*SourceModule) Import ¶
func (m *SourceModule) Import(_ string) (interface{}, error)
Import returns a module source code.
type String ¶
type String struct { ObjectImpl Value string // contains filtered or unexported fields }
String represents a string value.
func (*String) BinaryOp ¶
BinaryOp returns another object that is the result of a given binary operator and a right-hand side object.
func (*String) CanIterate ¶
CanIterate returns whether the Object can be Iterated.
func (*String) Equals ¶
Equals returns true if the value of the type is equal to the value of another object.
type StringIterator ¶
type StringIterator struct { ObjectImpl // contains filtered or unexported fields }
StringIterator represents an iterator for a string.
func (*StringIterator) Copy ¶
func (i *StringIterator) Copy() Object
Copy returns a copy of the type.
func (*StringIterator) Equals ¶
func (i *StringIterator) Equals(Object) bool
Equals returns true if the value of the type is equal to the value of another object.
func (*StringIterator) IsFalsy ¶
func (i *StringIterator) IsFalsy() bool
IsFalsy returns true if the value of the type is falsy.
func (*StringIterator) Key ¶
func (i *StringIterator) Key() Object
Key returns the key or index value of the current element.
func (*StringIterator) Next ¶
func (i *StringIterator) Next() bool
Next returns true if there are more elements to iterate.
func (*StringIterator) String ¶
func (i *StringIterator) String() string
func (*StringIterator) TypeName ¶
func (i *StringIterator) TypeName() string
TypeName returns the name of the type.
func (*StringIterator) Value ¶
func (i *StringIterator) Value() Object
Value returns the value of the current element.
type Symbol ¶
type Symbol struct { Name string Scope SymbolScope Index int LocalAssigned bool // if the local symbol is assigned at least once }
Symbol represents a symbol in the symbol table.
type SymbolScope ¶
type SymbolScope string
SymbolScope represents a symbol scope.
const ( ScopeGlobal SymbolScope = "GLOBAL" ScopeLocal SymbolScope = "LOCAL" ScopeBuiltin SymbolScope = "BUILTIN" ScopeFree SymbolScope = "FREE" )
List of symbol scopes
type SymbolTable ¶
type SymbolTable struct {
// contains filtered or unexported fields
}
SymbolTable represents a symbol table.
func (*SymbolTable) BuiltinSymbols ¶
func (t *SymbolTable) BuiltinSymbols() []*Symbol
BuiltinSymbols returns builtin symbols for the scope.
func (*SymbolTable) Define ¶
func (t *SymbolTable) Define(name string) *Symbol
Define adds a new symbol in the current scope.
func (*SymbolTable) DefineBuiltin ¶
func (t *SymbolTable) DefineBuiltin(index int, name string) *Symbol
DefineBuiltin adds a symbol for builtin function.
func (*SymbolTable) Fork ¶
func (t *SymbolTable) Fork(block bool) *SymbolTable
Fork creates a new symbol table for a new scope.
func (*SymbolTable) FreeSymbols ¶
func (t *SymbolTable) FreeSymbols() []*Symbol
FreeSymbols returns free symbols for the scope.
func (*SymbolTable) MaxSymbols ¶
func (t *SymbolTable) MaxSymbols() int
MaxSymbols returns the total number of symbols defined in the scope.
func (*SymbolTable) Names ¶
func (t *SymbolTable) Names() []string
Names returns the name of all the symbols.
func (*SymbolTable) Parent ¶
func (t *SymbolTable) Parent(skipBlock bool) *SymbolTable
Parent returns the outer scope of the current symbol table.
type Time ¶
type Time struct { ObjectImpl Value time.Time }
Time represents a time value.
func (*Time) BinaryOp ¶
BinaryOp returns another object that is the result of a given binary operator and a right-hand side object.
func (*Time) Equals ¶
Equals returns true if the value of the type is equal to the value of another object.
type Undefined ¶
type Undefined struct {
ObjectImpl
}
Undefined represents an undefined value.
func (*Undefined) CanIterate ¶
CanIterate returns whether the Object can be Iterated.
func (*Undefined) Equals ¶
Equals returns true if the value of the type is equal to the value of another object.
type UserFunction ¶
type UserFunction struct { ObjectImpl Name string Value CallableFunc EncodingID string }
UserFunction represents a user function.
func (*UserFunction) Call ¶
func (o *UserFunction) Call(args ...Object) (Object, error)
Call invokes a user function.
func (*UserFunction) CanCall ¶
func (o *UserFunction) CanCall() bool
CanCall returns whether the Object can be Called.
func (*UserFunction) Equals ¶
func (o *UserFunction) Equals(_ Object) bool
Equals returns true if the value of the type is equal to the value of another object.
func (*UserFunction) String ¶
func (o *UserFunction) String() string
func (*UserFunction) TypeName ¶
func (o *UserFunction) TypeName() string
TypeName returns the name of the type.
type VM ¶
type VM struct {
// contains filtered or unexported fields
}
VM is a virtual machine that executes the bytecode compiled by Compiler.
func (*VM) IsStackEmpty ¶
IsStackEmpty tests if the stack is empty or not.
type Variable ¶
type Variable struct {
// contains filtered or unexported fields
}
Variable is a user-defined variable for the script.
func NewVariable ¶
NewVariable creates a Variable.
func (*Variable) Array ¶
func (v *Variable) Array() []interface{}
Array returns []interface value of the variable value. It returns 0 if the value is not convertible to []interface.
func (*Variable) Bool ¶
Bool returns bool value of the variable value. It returns 0 if the value is not convertible to bool.
func (*Variable) Bytes ¶
Bytes returns a byte slice of the variable value. It returns nil if the value is not convertible to byte slice.
func (*Variable) Char ¶
Char returns rune value of the variable value. It returns 0 if the value is not convertible to rune.
func (*Variable) Error ¶
Error returns an error if the underlying value is error object. If not, this returns nil.
func (*Variable) Float ¶
Float returns float64 value of the variable value. It returns 0.0 if the value is not convertible to float64.
func (*Variable) Int ¶
Int returns int value of the variable value. It returns 0 if the value is not convertible to int.
func (*Variable) Int64 ¶
Int64 returns int64 value of the variable value. It returns 0 if the value is not convertible to int64.
func (*Variable) IsUndefined ¶
IsUndefined returns true if the underlying value is undefined.
func (*Variable) Map ¶
Map returns map[string]interface{} value of the variable value. It returns 0 if the value is not convertible to map[string]interface{}.
func (*Variable) Object ¶
Object returns an underlying Object of the variable value. Note that returned Object is a copy of an actual Object used in the script.
func (*Variable) String ¶
String returns string value of the variable value. It returns 0 if the value is not convertible to string.