tengo: github.com/d5/tengo Index | Examples | Files | Directories

package tengo

import "github.com/d5/tengo"

Code:

// 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

Examples

Package Files

builtins.go bytecode.go compiler.go doc.go errors.go formatter.go instructions.go iterator.go modules.go objects.go script.go symbol_table.go tengo.go variable.go vm.go

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
)

func CountObjects Uses

func CountObjects(o Object) (c int)

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

func Format Uses

func Format(format string, a ...Object) (string, error)

Format is like fmt.Sprintf but using Objects.

func FormatInstructions Uses

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

FormatInstructions returns string representation of bytecode instructions.

func MakeInstruction Uses

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

MakeInstruction returns a bytecode for an opcode and the operands.

func ToBool Uses

func ToBool(o Object) (v bool, ok bool)

ToBool will try to convert object o to bool value.

func ToByteSlice Uses

func ToByteSlice(o Object) (v []byte, ok bool)

ToByteSlice will try to convert object o to []byte value.

func ToFloat64 Uses

func ToFloat64(o Object) (v float64, ok bool)

ToFloat64 will try to convert object o to float64 value.

func ToInt Uses

func ToInt(o Object) (v int, ok bool)

ToInt will try to convert object o to int value.

func ToInt64 Uses

func ToInt64(o Object) (v int64, ok bool)

ToInt64 will try to convert object o to int64 value.

func ToInterface Uses

func ToInterface(o Object) (res interface{})

ToInterface attempts to convert an object o to an interface{} value

func ToRune Uses

func ToRune(o Object) (v rune, ok bool)

ToRune will try to convert object o to rune value.

func ToString Uses

func ToString(o Object) (v string, ok bool)

ToString will try to convert object o to string value.

func ToTime Uses

func ToTime(o Object) (v time.Time, ok bool)

ToTime will try to convert object o to time.Time value.

type Array Uses

type Array struct {
    ObjectImpl
    Value []Object
}

Array represents an array of objects.

func (*Array) BinaryOp Uses

func (o *Array) BinaryOp(op token.Token, rhs Object) (Object, error)

BinaryOp returns another object that is the result of a given binary operator and a right-hand side object.

func (*Array) CanIterate Uses

func (o *Array) CanIterate() bool

CanIterate returns whether the Object can be Iterated.

func (*Array) Copy Uses

func (o *Array) Copy() Object

Copy returns a copy of the type.

func (*Array) Equals Uses

func (o *Array) Equals(x Object) bool

Equals returns true if the value of the type is equal to the value of another object.

func (*Array) IndexGet Uses

func (o *Array) IndexGet(index Object) (res Object, err error)

IndexGet returns an element at a given index.

func (*Array) IndexSet Uses

func (o *Array) IndexSet(index, value Object) (err error)

IndexSet sets an element at a given index.

func (*Array) IsFalsy Uses

func (o *Array) IsFalsy() bool

IsFalsy returns true if the value of the type is falsy.

func (*Array) Iterate Uses

func (o *Array) Iterate() Iterator

Iterate creates an array iterator.

func (*Array) String Uses

func (o *Array) String() string

func (*Array) TypeName Uses

func (o *Array) TypeName() string

TypeName returns the name of the type.

type ArrayIterator Uses

type ArrayIterator struct {
    ObjectImpl
    // contains filtered or unexported fields
}

ArrayIterator is an iterator for an array.

func (*ArrayIterator) Copy Uses

func (i *ArrayIterator) Copy() Object

Copy returns a copy of the type.

func (*ArrayIterator) Equals Uses

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 Uses

func (i *ArrayIterator) IsFalsy() bool

IsFalsy returns true if the value of the type is falsy.

func (*ArrayIterator) Key Uses

func (i *ArrayIterator) Key() Object

Key returns the key or index value of the current element.

func (*ArrayIterator) Next Uses

func (i *ArrayIterator) Next() bool

Next returns true if there are more elements to iterate.

func (*ArrayIterator) String Uses

func (i *ArrayIterator) String() string

func (*ArrayIterator) TypeName Uses

func (i *ArrayIterator) TypeName() string

TypeName returns the name of the type.

func (*ArrayIterator) Value Uses

func (i *ArrayIterator) Value() Object

Value returns the value of the current element.

type Bool Uses

type Bool struct {
    ObjectImpl
    // contains filtered or unexported fields
}

Bool represents a boolean value.

func (*Bool) Copy Uses

func (o *Bool) Copy() Object

Copy returns a copy of the type.

func (*Bool) Equals Uses

func (o *Bool) Equals(x Object) bool

Equals returns true if the value of the type is equal to the value of another object.

func (*Bool) GobDecode Uses

func (o *Bool) GobDecode(b []byte) (err error)

GobDecode decodes bool value from input bytes.

func (*Bool) GobEncode Uses

func (o *Bool) GobEncode() (b []byte, err error)

GobEncode encodes bool values into bytes.

func (*Bool) IsFalsy Uses

func (o *Bool) IsFalsy() bool

IsFalsy returns true if the value of the type is falsy.

func (*Bool) String Uses

func (o *Bool) String() string

func (*Bool) TypeName Uses

func (o *Bool) TypeName() string

TypeName returns the name of the type.

type BuiltinFunction Uses

type BuiltinFunction struct {
    ObjectImpl
    Name  string
    Value CallableFunc
}

BuiltinFunction represents a builtin function.

func GetAllBuiltinFunctions Uses

func GetAllBuiltinFunctions() []*BuiltinFunction

GetAllBuiltinFunctions returns all builtin function objects.

func (*BuiltinFunction) Call Uses

func (o *BuiltinFunction) Call(args ...Object) (Object, error)

Call executes a builtin function.

func (*BuiltinFunction) CanCall Uses

func (o *BuiltinFunction) CanCall() bool

CanCall returns whether the Object can be Called.

func (*BuiltinFunction) Copy Uses

func (o *BuiltinFunction) Copy() Object

Copy returns a copy of the type.

func (*BuiltinFunction) Equals Uses

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 Uses

func (o *BuiltinFunction) String() string

func (*BuiltinFunction) TypeName Uses

func (o *BuiltinFunction) TypeName() string

TypeName returns the name of the type.

type BuiltinModule Uses

type BuiltinModule struct {
    Attrs map[string]Object
}

BuiltinModule is an importable module that's written in Go.

func (*BuiltinModule) AsImmutableMap Uses

func (m *BuiltinModule) AsImmutableMap(moduleName string) *ImmutableMap

AsImmutableMap converts builtin module into an immutable map.

func (*BuiltinModule) Import Uses

func (m *BuiltinModule) Import(moduleName string) (interface{}, error)

Import returns an immutable map for the module.

type Bytecode Uses

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

Bytecode is a compiled instructions and constants.

func (*Bytecode) CountObjects Uses

func (b *Bytecode) CountObjects() int

CountObjects returns the number of objects found in Constants.

func (*Bytecode) Decode Uses

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

Decode reads Bytecode data from the reader.

func (*Bytecode) Encode Uses

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

Encode writes Bytecode data to the writer.

func (*Bytecode) FormatConstants Uses

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

FormatConstants returns human readable string representations of compiled constants.

func (*Bytecode) FormatInstructions Uses

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

FormatInstructions returns human readable string representations of compiled instructions.

func (*Bytecode) RemoveDuplicates Uses

func (b *Bytecode) RemoveDuplicates()

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

type Bytes Uses

type Bytes struct {
    ObjectImpl
    Value []byte
}

Bytes represents a byte array.

func (*Bytes) BinaryOp Uses

func (o *Bytes) BinaryOp(op token.Token, rhs Object) (Object, error)

BinaryOp returns another object that is the result of a given binary operator and a right-hand side object.

func (*Bytes) CanIterate Uses

func (o *Bytes) CanIterate() bool

CanIterate returns whether the Object can be Iterated.

func (*Bytes) Copy Uses

func (o *Bytes) Copy() Object

Copy returns a copy of the type.

func (*Bytes) Equals Uses

func (o *Bytes) Equals(x Object) bool

Equals returns true if the value of the type is equal to the value of another object.

func (*Bytes) IndexGet Uses

func (o *Bytes) IndexGet(index Object) (res Object, err error)

IndexGet returns an element (as Int) at a given index.

func (*Bytes) IsFalsy Uses

func (o *Bytes) IsFalsy() bool

IsFalsy returns true if the value of the type is falsy.

func (*Bytes) Iterate Uses

func (o *Bytes) Iterate() Iterator

Iterate creates a bytes iterator.

func (*Bytes) String Uses

func (o *Bytes) String() string

func (*Bytes) TypeName Uses

func (o *Bytes) TypeName() string

TypeName returns the name of the type.

type BytesIterator Uses

type BytesIterator struct {
    ObjectImpl
    // contains filtered or unexported fields
}

BytesIterator represents an iterator for a string.

func (*BytesIterator) Copy Uses

func (i *BytesIterator) Copy() Object

Copy returns a copy of the type.

func (*BytesIterator) Equals Uses

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 Uses

func (i *BytesIterator) Key() Object

Key returns the key or index value of the current element.

func (*BytesIterator) Next Uses

func (i *BytesIterator) Next() bool

Next returns true if there are more elements to iterate.

func (*BytesIterator) String Uses

func (i *BytesIterator) String() string

func (*BytesIterator) TypeName Uses

func (i *BytesIterator) TypeName() string

TypeName returns the name of the type.

func (*BytesIterator) Value Uses

func (i *BytesIterator) Value() Object

Value returns the value of the current element.

type CallableFunc Uses

type CallableFunc = func(args ...Object) (ret Object, err error)

CallableFunc is a function signature for the callable functions.

type Char Uses

type Char struct {
    ObjectImpl
    Value rune
}

Char represents a character value.

func (*Char) BinaryOp Uses

func (o *Char) BinaryOp(op token.Token, rhs Object) (Object, error)

BinaryOp returns another object that is the result of a given binary operator and a right-hand side object.

func (*Char) Copy Uses

func (o *Char) Copy() Object

Copy returns a copy of the type.

func (*Char) Equals Uses

func (o *Char) Equals(x Object) bool

Equals returns true if the value of the type is equal to the value of another object.

func (*Char) IsFalsy Uses

func (o *Char) IsFalsy() bool

IsFalsy returns true if the value of the type is falsy.

func (*Char) String Uses

func (o *Char) String() string

func (*Char) TypeName Uses

func (o *Char) TypeName() string

TypeName returns the name of the type.

type Compiled Uses

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 Uses

func (c *Compiled) Clone() *Compiled

Clone creates a new copy of Compiled. Cloned copies are safe for concurrent use by multiple goroutines.

func (*Compiled) Get Uses

func (c *Compiled) Get(name string) *Variable

Get returns a variable identified by the name.

func (*Compiled) GetAll Uses

func (c *Compiled) GetAll() []*Variable

GetAll returns all the variables that are defined by the compiled script.

func (*Compiled) IsDefined Uses

func (c *Compiled) IsDefined(name string) bool

IsDefined returns true if the variable name is defined (has value) before or after the execution.

func (*Compiled) Run Uses

func (c *Compiled) Run() error

Run executes the compiled script in the virtual machine.

func (*Compiled) RunContext Uses

func (c *Compiled) RunContext(ctx context.Context) (err error)

RunContext is like Run but includes a context.

func (*Compiled) Set Uses

func (c *Compiled) Set(name string, value interface{}) error

Set replaces the value of a global variable identified by the name. An error will be returned if the name was not defined during compilation.

type CompiledFunction Uses

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
}

CompiledFunction represents a compiled function.

func (*CompiledFunction) CanCall Uses

func (o *CompiledFunction) CanCall() bool

CanCall returns whether the Object can be Called.

func (*CompiledFunction) Copy Uses

func (o *CompiledFunction) Copy() Object

Copy returns a copy of the type.

func (*CompiledFunction) Equals Uses

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 Uses

func (o *CompiledFunction) SourcePos(ip int) parser.Pos

SourcePos returns the source position of the instruction at ip.

func (*CompiledFunction) String Uses

func (o *CompiledFunction) String() string

func (*CompiledFunction) TypeName Uses

func (o *CompiledFunction) TypeName() string

TypeName returns the name of the type.

type Compiler Uses

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

Compiler compiles the AST into a bytecode.

func NewCompiler Uses

func NewCompiler(
    file *parser.SourceFile,
    symbolTable *SymbolTable,
    constants []Object,
    modules *ModuleMap,
    trace io.Writer,
) *Compiler

NewCompiler creates a Compiler.

func (*Compiler) Bytecode Uses

func (c *Compiler) Bytecode() *Bytecode

Bytecode returns a compiled bytecode.

func (*Compiler) Compile Uses

func (c *Compiler) Compile(node parser.Node) error

Compile compiles the AST node.

func (*Compiler) EnableFileImport Uses

func (c *Compiler) EnableFileImport(enable bool)

EnableFileImport enables or disables module loading from local files. Local file modules are disabled by default.

type CompilerError Uses

type CompilerError struct {
    FileSet *parser.SourceFileSet
    Node    parser.Node
    Err     error
}

CompilerError represents a compiler error.

func (*CompilerError) Error Uses

func (e *CompilerError) Error() string

type ErrInvalidArgumentType Uses

type ErrInvalidArgumentType struct {
    Name     string
    Expected string
    Found    string
}

ErrInvalidArgumentType represents an invalid argument value type error.

func (ErrInvalidArgumentType) Error Uses

func (e ErrInvalidArgumentType) Error() string

type Error Uses

type Error struct {
    ObjectImpl
    Value Object
}

Error represents an error value.

func (*Error) Copy Uses

func (o *Error) Copy() Object

Copy returns a copy of the type.

func (*Error) Equals Uses

func (o *Error) Equals(x Object) bool

Equals returns true if the value of the type is equal to the value of another object.

func (*Error) IndexGet Uses

func (o *Error) IndexGet(index Object) (res Object, err error)

IndexGet returns an element at a given index.

func (*Error) IsFalsy Uses

func (o *Error) IsFalsy() bool

IsFalsy returns true if the value of the type is falsy.

func (*Error) String Uses

func (o *Error) String() string

func (*Error) TypeName Uses

func (o *Error) TypeName() string

TypeName returns the name of the type.

type Float Uses

type Float struct {
    ObjectImpl
    Value float64
}

Float represents a floating point number value.

func (*Float) BinaryOp Uses

func (o *Float) BinaryOp(op token.Token, rhs Object) (Object, error)

BinaryOp returns another object that is the result of a given binary operator and a right-hand side object.

func (*Float) Copy Uses

func (o *Float) Copy() Object

Copy returns a copy of the type.

func (*Float) Equals Uses

func (o *Float) Equals(x Object) bool

Equals returns true if the value of the type is equal to the value of another object.

func (*Float) IsFalsy Uses

func (o *Float) IsFalsy() bool

IsFalsy returns true if the value of the type is falsy.

func (*Float) String Uses

func (o *Float) String() string

func (*Float) TypeName Uses

func (o *Float) TypeName() string

TypeName returns the name of the type.

type ImmutableArray Uses

type ImmutableArray struct {
    ObjectImpl
    Value []Object
}

ImmutableArray represents an immutable array of objects.

func (*ImmutableArray) BinaryOp Uses

func (o *ImmutableArray) BinaryOp(op token.Token, rhs Object) (Object, error)

BinaryOp returns another object that is the result of a given binary operator and a right-hand side object.

func (*ImmutableArray) CanIterate Uses

func (o *ImmutableArray) CanIterate() bool

CanIterate returns whether the Object can be Iterated.

func (*ImmutableArray) Copy Uses

func (o *ImmutableArray) Copy() Object

Copy returns a copy of the type.

func (*ImmutableArray) Equals Uses

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 Uses

func (o *ImmutableArray) IndexGet(index Object) (res Object, err error)

IndexGet returns an element at a given index.

func (*ImmutableArray) IsFalsy Uses

func (o *ImmutableArray) IsFalsy() bool

IsFalsy returns true if the value of the type is falsy.

func (*ImmutableArray) Iterate Uses

func (o *ImmutableArray) Iterate() Iterator

Iterate creates an array iterator.

func (*ImmutableArray) String Uses

func (o *ImmutableArray) String() string

func (*ImmutableArray) TypeName Uses

func (o *ImmutableArray) TypeName() string

TypeName returns the name of the type.

type ImmutableMap Uses

type ImmutableMap struct {
    ObjectImpl
    Value map[string]Object
}

ImmutableMap represents an immutable map object.

func (*ImmutableMap) CanIterate Uses

func (o *ImmutableMap) CanIterate() bool

CanIterate returns whether the Object can be Iterated.

func (*ImmutableMap) Copy Uses

func (o *ImmutableMap) Copy() Object

Copy returns a copy of the type.

func (*ImmutableMap) Equals Uses

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 Uses

func (o *ImmutableMap) IndexGet(index Object) (res Object, err error)

IndexGet returns the value for the given key.

func (*ImmutableMap) IsFalsy Uses

func (o *ImmutableMap) IsFalsy() bool

IsFalsy returns true if the value of the type is falsy.

func (*ImmutableMap) Iterate Uses

func (o *ImmutableMap) Iterate() Iterator

Iterate creates an immutable map iterator.

func (*ImmutableMap) String Uses

func (o *ImmutableMap) String() string

func (*ImmutableMap) TypeName Uses

func (o *ImmutableMap) TypeName() string

TypeName returns the name of the type.

type Importable Uses

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 Uses

type Int struct {
    ObjectImpl
    Value int64
}

Int represents an integer value.

func (*Int) BinaryOp Uses

func (o *Int) BinaryOp(op token.Token, rhs Object) (Object, error)

BinaryOp returns another object that is the result of a given binary operator and a right-hand side object.

func (*Int) Copy Uses

func (o *Int) Copy() Object

Copy returns a copy of the type.

func (*Int) Equals Uses

func (o *Int) Equals(x Object) bool

Equals returns true if the value of the type is equal to the value of another object.

func (*Int) IsFalsy Uses

func (o *Int) IsFalsy() bool

IsFalsy returns true if the value of the type is falsy.

func (*Int) String Uses

func (o *Int) String() string

func (*Int) TypeName Uses

func (o *Int) TypeName() string

TypeName returns the name of the type.

type Iterator Uses

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 Uses

type Map struct {
    ObjectImpl
    Value map[string]Object
}

Map represents a map of objects.

func (*Map) CanIterate Uses

func (o *Map) CanIterate() bool

CanIterate returns whether the Object can be Iterated.

func (*Map) Copy Uses

func (o *Map) Copy() Object

Copy returns a copy of the type.

func (*Map) Equals Uses

func (o *Map) Equals(x Object) bool

Equals returns true if the value of the type is equal to the value of another object.

func (*Map) IndexGet Uses

func (o *Map) IndexGet(index Object) (res Object, err error)

IndexGet returns the value for the given key.

func (*Map) IndexSet Uses

func (o *Map) IndexSet(index, value Object) (err error)

IndexSet sets the value for the given key.

func (*Map) IsFalsy Uses

func (o *Map) IsFalsy() bool

IsFalsy returns true if the value of the type is falsy.

func (*Map) Iterate Uses

func (o *Map) Iterate() Iterator

Iterate creates a map iterator.

func (*Map) String Uses

func (o *Map) String() string

func (*Map) TypeName Uses

func (o *Map) TypeName() string

TypeName returns the name of the type.

type MapIterator Uses

type MapIterator struct {
    ObjectImpl
    // contains filtered or unexported fields
}

MapIterator represents an iterator for the map.

func (*MapIterator) Copy Uses

func (i *MapIterator) Copy() Object

Copy returns a copy of the type.

func (*MapIterator) Equals Uses

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 Uses

func (i *MapIterator) IsFalsy() bool

IsFalsy returns true if the value of the type is falsy.

func (*MapIterator) Key Uses

func (i *MapIterator) Key() Object

Key returns the key or index value of the current element.

func (*MapIterator) Next Uses

func (i *MapIterator) Next() bool

Next returns true if there are more elements to iterate.

func (*MapIterator) String Uses

func (i *MapIterator) String() string

func (*MapIterator) TypeName Uses

func (i *MapIterator) TypeName() string

TypeName returns the name of the type.

func (*MapIterator) Value Uses

func (i *MapIterator) Value() Object

Value returns the value of the current element.

type ModuleMap Uses

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

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

func NewModuleMap Uses

func NewModuleMap() *ModuleMap

NewModuleMap creates a new module map.

func (*ModuleMap) Add Uses

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

Add adds an import module.

func (*ModuleMap) AddBuiltinModule Uses

func (m *ModuleMap) AddBuiltinModule(name string, attrs map[string]Object)

AddBuiltinModule adds a builtin module.

func (*ModuleMap) AddMap Uses

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

AddMap adds named modules from another module map.

func (*ModuleMap) AddSourceModule Uses

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

AddSourceModule adds a source module.

func (*ModuleMap) Copy Uses

func (m *ModuleMap) Copy() *ModuleMap

Copy creates a copy of the module map.

func (*ModuleMap) Get Uses

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 Uses

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 Uses

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

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

func (*ModuleMap) Len Uses

func (m *ModuleMap) Len() int

Len returns the number of named modules.

func (*ModuleMap) Remove Uses

func (m *ModuleMap) Remove(name string)

Remove removes a named module.

type Object Uses

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.

var (
    // TrueValue represents a true value.
    TrueValue Object = &Bool{value: true}

    // FalseValue represents a false value.
    FalseValue Object = &Bool{value: false}

    // UndefinedValue represents an undefined value.
    UndefinedValue Object = &Undefined{}
)

func FromInterface Uses

func FromInterface(v interface{}) (Object, error)

FromInterface will attempt to convert an interface{} v to a Tengo Object

type ObjectImpl Uses

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 Uses

func (o *ObjectImpl) BinaryOp(_ token.Token, _ Object) (Object, error)

BinaryOp returns another object that is the result of a given binary operator and a right-hand side object.

func (*ObjectImpl) Call Uses

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 Uses

func (o *ObjectImpl) CanCall() bool

CanCall returns whether the Object can be Called.

func (*ObjectImpl) CanIterate Uses

func (o *ObjectImpl) CanIterate() bool

CanIterate returns whether the Object can be Iterated.

func (*ObjectImpl) Copy Uses

func (o *ObjectImpl) Copy() Object

Copy returns a copy of the type.

func (*ObjectImpl) Equals Uses

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 Uses

func (o *ObjectImpl) IndexGet(_ Object) (res Object, err error)

IndexGet returns an element at a given index.

func (*ObjectImpl) IndexSet Uses

func (o *ObjectImpl) IndexSet(_, _ Object) (err error)

IndexSet sets an element at a given index.

func (*ObjectImpl) IsFalsy Uses

func (o *ObjectImpl) IsFalsy() bool

IsFalsy returns true if the value of the type is falsy.

func (*ObjectImpl) Iterate Uses

func (o *ObjectImpl) Iterate() Iterator

Iterate returns an iterator.

func (*ObjectImpl) String Uses

func (o *ObjectImpl) String() string

func (*ObjectImpl) TypeName Uses

func (o *ObjectImpl) TypeName() string

TypeName returns the name of the type.

type ObjectPtr Uses

type ObjectPtr struct {
    ObjectImpl
    Value *Object
}

ObjectPtr represents a free variable.

func (*ObjectPtr) Copy Uses

func (o *ObjectPtr) Copy() Object

Copy returns a copy of the type.

func (*ObjectPtr) Equals Uses

func (o *ObjectPtr) Equals(x Object) bool

Equals returns true if the value of the type is equal to the value of another object.

func (*ObjectPtr) IsFalsy Uses

func (o *ObjectPtr) IsFalsy() bool

IsFalsy returns true if the value of the type is falsy.

func (*ObjectPtr) String Uses

func (o *ObjectPtr) String() string

func (*ObjectPtr) TypeName Uses

func (o *ObjectPtr) TypeName() string

TypeName returns the name of the type.

type Script Uses

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

Script can simplify compilation and execution of embedded scripts.

func NewScript Uses

func NewScript(input []byte) *Script

NewScript creates a Script instance with an input script.

func (*Script) Add Uses

func (s *Script) Add(name string, value interface{}) error

Add adds a new variable or updates an existing variable to the script.

func (*Script) Compile Uses

func (s *Script) Compile() (*Compiled, error)

Compile compiles the script with all the defined variables, and, returns Compiled object.

func (*Script) EnableFileImport Uses

func (s *Script) EnableFileImport(enable bool)

EnableFileImport enables or disables module loading from local files. Local file modules are disabled by default.

func (*Script) Remove Uses

func (s *Script) Remove(name string) bool

Remove removes (undefines) an existing variable for the script. It returns false if the variable name is not defined.

func (*Script) Run Uses

func (s *Script) Run() (compiled *Compiled, err error)

Run compiles and runs the scripts. Use returned compiled object to access global variables.

func (*Script) RunContext Uses

func (s *Script) RunContext(
    ctx context.Context,
) (compiled *Compiled, err error)

RunContext is like Run but includes a context.

func (*Script) SetImports Uses

func (s *Script) SetImports(modules *ModuleMap)

SetImports sets import modules.

func (*Script) SetMaxAllocs Uses

func (s *Script) SetMaxAllocs(n int64)

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 Uses

func (s *Script) SetMaxConstObjects(n int)

SetMaxConstObjects sets the maximum number of objects in the compiled constants.

type SourceModule Uses

type SourceModule struct {
    Src []byte
}

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

func (*SourceModule) Import Uses

func (m *SourceModule) Import(_ string) (interface{}, error)

Import returns a module source code.

type String Uses

type String struct {
    ObjectImpl
    Value string
    // contains filtered or unexported fields
}

String represents a string value.

func (*String) BinaryOp Uses

func (o *String) BinaryOp(op token.Token, rhs Object) (Object, error)

BinaryOp returns another object that is the result of a given binary operator and a right-hand side object.

func (*String) CanIterate Uses

func (o *String) CanIterate() bool

CanIterate returns whether the Object can be Iterated.

func (*String) Copy Uses

func (o *String) Copy() Object

Copy returns a copy of the type.

func (*String) Equals Uses

func (o *String) Equals(x Object) bool

Equals returns true if the value of the type is equal to the value of another object.

func (*String) IndexGet Uses

func (o *String) IndexGet(index Object) (res Object, err error)

IndexGet returns a character at a given index.

func (*String) IsFalsy Uses

func (o *String) IsFalsy() bool

IsFalsy returns true if the value of the type is falsy.

func (*String) Iterate Uses

func (o *String) Iterate() Iterator

Iterate creates a string iterator.

func (*String) String Uses

func (o *String) String() string

func (*String) TypeName Uses

func (o *String) TypeName() string

TypeName returns the name of the type.

type StringIterator Uses

type StringIterator struct {
    ObjectImpl
    // contains filtered or unexported fields
}

StringIterator represents an iterator for a string.

func (*StringIterator) Copy Uses

func (i *StringIterator) Copy() Object

Copy returns a copy of the type.

func (*StringIterator) Equals Uses

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 Uses

func (i *StringIterator) IsFalsy() bool

IsFalsy returns true if the value of the type is falsy.

func (*StringIterator) Key Uses

func (i *StringIterator) Key() Object

Key returns the key or index value of the current element.

func (*StringIterator) Next Uses

func (i *StringIterator) Next() bool

Next returns true if there are more elements to iterate.

func (*StringIterator) String Uses

func (i *StringIterator) String() string

func (*StringIterator) TypeName Uses

func (i *StringIterator) TypeName() string

TypeName returns the name of the type.

func (*StringIterator) Value Uses

func (i *StringIterator) Value() Object

Value returns the value of the current element.

type Symbol Uses

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 Uses

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 Uses

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

SymbolTable represents a symbol table.

func NewSymbolTable Uses

func NewSymbolTable() *SymbolTable

NewSymbolTable creates a SymbolTable.

func (*SymbolTable) BuiltinSymbols Uses

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

BuiltinSymbols returns builtin symbols for the scope.

func (*SymbolTable) Define Uses

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

Define adds a new symbol in the current scope.

func (*SymbolTable) DefineBuiltin Uses

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

DefineBuiltin adds a symbol for builtin function.

func (*SymbolTable) Fork Uses

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

Fork creates a new symbol table for a new scope.

func (*SymbolTable) FreeSymbols Uses

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

FreeSymbols returns free symbols for the scope.

func (*SymbolTable) MaxSymbols Uses

func (t *SymbolTable) MaxSymbols() int

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

func (*SymbolTable) Names Uses

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

Names returns the name of all the symbols.

func (*SymbolTable) Parent Uses

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

Parent returns the outer scope of the current symbol table.

func (*SymbolTable) Resolve Uses

func (t *SymbolTable) Resolve(
    name string,
) (symbol *Symbol, depth int, ok bool)

Resolve resolves a symbol with a given name.

type Time Uses

type Time struct {
    ObjectImpl
    Value time.Time
}

Time represents a time value.

func (*Time) BinaryOp Uses

func (o *Time) BinaryOp(op token.Token, rhs Object) (Object, error)

BinaryOp returns another object that is the result of a given binary operator and a right-hand side object.

func (*Time) Copy Uses

func (o *Time) Copy() Object

Copy returns a copy of the type.

func (*Time) Equals Uses

func (o *Time) Equals(x Object) bool

Equals returns true if the value of the type is equal to the value of another object.

func (*Time) IsFalsy Uses

func (o *Time) IsFalsy() bool

IsFalsy returns true if the value of the type is falsy.

func (*Time) String Uses

func (o *Time) String() string

func (*Time) TypeName Uses

func (o *Time) TypeName() string

TypeName returns the name of the type.

type Undefined Uses

type Undefined struct {
    ObjectImpl
}

Undefined represents an undefined value.

func (*Undefined) CanIterate Uses

func (o *Undefined) CanIterate() bool

CanIterate returns whether the Object can be Iterated.

func (*Undefined) Copy Uses

func (o *Undefined) Copy() Object

Copy returns a copy of the type.

func (*Undefined) Equals Uses

func (o *Undefined) Equals(x Object) bool

Equals returns true if the value of the type is equal to the value of another object.

func (*Undefined) IndexGet Uses

func (o *Undefined) IndexGet(_ Object) (Object, error)

IndexGet returns an element at a given index.

func (*Undefined) IsFalsy Uses

func (o *Undefined) IsFalsy() bool

IsFalsy returns true if the value of the type is falsy.

func (*Undefined) Iterate Uses

func (o *Undefined) Iterate() Iterator

Iterate creates a map iterator.

func (*Undefined) Key Uses

func (o *Undefined) Key() Object

Key returns the key or index value of the current element.

func (*Undefined) Next Uses

func (o *Undefined) Next() bool

Next returns true if there are more elements to iterate.

func (*Undefined) String Uses

func (o *Undefined) String() string

func (*Undefined) TypeName Uses

func (o *Undefined) TypeName() string

TypeName returns the name of the type.

func (*Undefined) Value Uses

func (o *Undefined) Value() Object

Value returns the value of the current element.

type UserFunction Uses

type UserFunction struct {
    ObjectImpl
    Name       string
    Value      CallableFunc
    EncodingID string
}

UserFunction represents a user function.

func (*UserFunction) Call Uses

func (o *UserFunction) Call(args ...Object) (Object, error)

Call invokes a user function.

func (*UserFunction) CanCall Uses

func (o *UserFunction) CanCall() bool

CanCall returns whether the Object can be Called.

func (*UserFunction) Copy Uses

func (o *UserFunction) Copy() Object

Copy returns a copy of the type.

func (*UserFunction) Equals Uses

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 Uses

func (o *UserFunction) String() string

func (*UserFunction) TypeName Uses

func (o *UserFunction) TypeName() string

TypeName returns the name of the type.

type VM Uses

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

VM is a virtual machine that executes the bytecode compiled by Compiler.

func NewVM Uses

func NewVM(
    bytecode *Bytecode,
    globals []Object,
    maxAllocs int64,
) *VM

NewVM creates a VM.

func (*VM) Abort Uses

func (v *VM) Abort()

Abort aborts the execution.

func (*VM) IsStackEmpty Uses

func (v *VM) IsStackEmpty() bool

IsStackEmpty tests if the stack is empty or not.

func (*VM) Run Uses

func (v *VM) Run() (err error)

Run starts the execution.

type Variable Uses

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

Variable is a user-defined variable for the script.

func NewVariable Uses

func NewVariable(name string, value interface{}) (*Variable, error)

NewVariable creates a Variable.

func (*Variable) Array Uses

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 Uses

func (v *Variable) Bool() bool

Bool returns bool value of the variable value. It returns 0 if the value is not convertible to bool.

func (*Variable) Bytes Uses

func (v *Variable) Bytes() []byte

Bytes returns a byte slice of the variable value. It returns nil if the value is not convertible to byte slice.

func (*Variable) Char Uses

func (v *Variable) Char() rune

Char returns rune value of the variable value. It returns 0 if the value is not convertible to rune.

func (*Variable) Error Uses

func (v *Variable) Error() error

Error returns an error if the underlying value is error object. If not, this returns nil.

func (*Variable) Float Uses

func (v *Variable) Float() float64

Float returns float64 value of the variable value. It returns 0.0 if the value is not convertible to float64.

func (*Variable) Int Uses

func (v *Variable) Int() int

Int returns int value of the variable value. It returns 0 if the value is not convertible to int.

func (*Variable) Int64 Uses

func (v *Variable) Int64() int64

Int64 returns int64 value of the variable value. It returns 0 if the value is not convertible to int64.

func (*Variable) IsUndefined Uses

func (v *Variable) IsUndefined() bool

IsUndefined returns true if the underlying value is undefined.

func (*Variable) Map Uses

func (v *Variable) Map() map[string]interface{}

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) Name Uses

func (v *Variable) Name() string

Name returns the name of the variable.

func (*Variable) Object Uses

func (v *Variable) Object() 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 Uses

func (v *Variable) String() string

String returns string value of the variable value. It returns 0 if the value is not convertible to string.

func (*Variable) Value Uses

func (v *Variable) Value() interface{}

Value returns an empty interface of the variable value.

func (*Variable) ValueType Uses

func (v *Variable) ValueType() string

ValueType returns the name of the value type.

Directories

PathSynopsis
cmd/bench
cmd/tengo
parser
require
stdlib
stdlib/json
token

Package tengo imports 18 packages (graph). Updated 2020-03-16. Refresh now. Tools for package owners.