Documentation
¶
Index ¶
- type Callback
- type Compiled
- func (c *Compiled) Call(fn tengo.Object, args ...interface{}) (interface{}, error)
- func (c *Compiled) CallByName(fn string, args ...interface{}) (interface{}, error)
- func (c *Compiled) CallByNameContext(ctx context.Context, fn string, args ...interface{}) (interface{}, error)
- func (c *Compiled) CallContext(ctx context.Context, fn tengo.Object, args ...interface{}) (interface{}, error)
- func (c *Compiled) Clone() *Compiled
- func (c *Compiled) Get(name string) *tengo.Variable
- func (c *Compiled) GetAll() []*tengo.Variable
- func (c *Compiled) IsDefined(name string) bool
- func (c *Compiled) Set(name string, value interface{}) error
- type Script
- func (s *Script) Add(name string, value interface{}) error
- func (s *Script) CompileRun() (*Compiled, error)
- func (s *Script) CompileRunContext(ctx context.Context) (*Compiled, error)
- func (s *Script) Remove(name string) bool
- func (s *Script) SetImports(modules *tengo.ModuleMap)
- func (s *Script) SetMaxAllocs(n int64)
- func (s *Script) Trace(w io.Writer)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Callback ¶
type Callback struct { Args []interface{} // contains filtered or unexported fields }
Callback is a wrapper to call a callable tengo.Object from Go with Call() and CallContext() methods. *Compiled object must be set before Call() and CallContext() calls, otherwise an error is thrown. Callback is intended to be created in a Go function that is invoked by tengo script to capture callable tengo.Object and additional arguments if required later. Args is deliberately exposed to use it as arguments to CallXXX methods but it is optional. Note: Do not call CallXXX methods while script is running, it locks the VM.
func NewCallback ¶
func NewCallback(fn tengo.Object, args ...interface{}) *Callback
NewCallback creates Callback object. See Callback type.
func (*Callback) Call ¶
Call calls tengo.Object and returns result. Set *Compiled object before Call().
func (*Callback) CallContext ¶
CallContext calls tengo.Object and returns result. Set *Compiled object before CallContext().
type Compiled ¶
type Compiled struct {
// contains filtered or unexported fields
}
Compiled is a compiled instance of the user script. Use Script.CompileRun() to create Compiled object.
func (*Compiled) Call ¶
Call calls callable tengo.Object with given arguments, and returns result. args must be convertible to supported Tengo types.
func (*Compiled) CallByName ¶
CallByName calls callable tengo.Object by its name and with given arguments, and returns result. args must be convertible to supported Tengo types.
Example ¶
package main import ( "fmt" "github.com/tauraamui/tengox" ) func main() { module := `add := func(a, b, c) { return a + b + c; }` script := tengox.NewScript([]byte(module)) // script is compiled and run compl, err := script.CompileRun() // CompileRunContext() is available if err != nil { panic(err) } // CallByNameContext() is available v, err := compl.CallByName("add", 1, 2, 3) // 1+2+3 if err != nil { panic(err) } fmt.Println(v)
Output:
func (*Compiled) CallByNameContext ¶
func (c *Compiled) CallByNameContext(ctx context.Context, fn string, args ...interface{}) (interface{}, error)
CallByNameContext calls callable tengo.Object by its name and with given arguments, and returns result. args must be convertible to supported Tengo types.
Example ¶
package main import ( "context" "fmt" "github.com/tauraamui/tengox" ) func main() { module := `stringer := func(s) { return string(s); }` ctx, cancel := context.WithCancel(context.Background()) defer cancel() script := tengox.NewScript([]byte(module)) // script is compiled and run compl, err := script.CompileRunContext(ctx) if err != nil { panic(err) } // string function is a builtin so it is not in global variables, stringer // is compiled function which calls builtin string function. v, err := compl.CallByNameContext(ctx, "stringer", 123456) if err != nil { panic(err) } fmt.Println(v) }
Output: 123456
func (*Compiled) CallContext ¶
func (c *Compiled) CallContext(ctx context.Context, fn tengo.Object, args ...interface{}) (interface{}, error)
CallContext calls callable tengo.Object with given arguments, and returns result. args must be convertible to supported Tengo types.
func (*Compiled) Clone ¶
Clone creates a new copy of Compiled. Cloned copies are safe for concurrent use. Clones occupy less memory..
func (*Compiled) GetAll ¶
func (c *Compiled) GetAll() []*tengo.Variable
GetAll returns all the variables that are defined by the compiled script.
type Script ¶
type Script struct {
// contains filtered or unexported fields
}
Script is used to call callable Tengo objects from Go. Methods are derived from Tengo's Script type. Unlike Tengo.
func (*Script) CompileRun ¶
CompileRun compiles the source script to bytecode and run it once to fill global objects.
func (*Script) CompileRunContext ¶
CompileRunContext compiles the source script to bytecode and run it once to fill global objects.
func (*Script) Remove ¶
Remove removes (undefines) an existing variable for the script. It returns false if the variable name is not defined.
func (*Script) SetImports ¶
func (s *Script) SetImports(modules *tengo.ModuleMap)
SetImports sets import modules.
func (*Script) SetMaxAllocs ¶
SetMaxAllocs sets the maximum number of objects allocations during the run time. Compiled script will return tengo.ErrObjectAllocLimit error if it exceeds this limit.