tengox

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2021 License: MIT Imports: 7 Imported by: 1

README

tengox

I archived this repo in favor of a new script language uGO for Go.

Experimental features for Tengo Scripting Language

A special thanks to Tom Gascoigne for his contribution.

tengox is created to call callable Tengo functions from Go in an easier way. Please see tests, example files and godoc.

You should pin tengox to a git tag (if any) in your go.mod file to use the stable code.

func ExampleCompiled_CallByName() {
	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: 6

	// you can clone your compiled code like this
	// clone := compl.Clone()
	// cloned code run in a different VM
}

Documentation

Index

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

func (cb *Callback) Call(args ...interface{}) (interface{}, error)

Call calls tengo.Object and returns result. Set *Compiled object before Call().

func (*Callback) CallContext

func (cb *Callback) CallContext(ctx context.Context,
	args ...interface{}) (interface{}, error)

CallContext calls tengo.Object and returns result. Set *Compiled object before CallContext().

func (*Callback) Set

func (cb *Callback) Set(c *Compiled) *Callback

Set sets compiled object, which is required to call callables.

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

func (c *Compiled) Call(fn tengo.Object,
	args ...interface{}) (interface{}, error)

Call calls callable tengo.Object with given arguments, and returns result. args must be convertible to supported Tengo types.

func (*Compiled) CallByName

func (c *Compiled) CallByName(fn string, args ...interface{}) (interface{}, error)

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

func (c *Compiled) Clone() *Compiled

Clone creates a new copy of Compiled. Cloned copies are safe for concurrent use. Clones occupy less memory..

func (*Compiled) Get

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

Get returns a variable identified by the name.

func (*Compiled) GetAll

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

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

func (*Compiled) IsDefined

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

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

func (*Compiled) Set

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

func NewScript(src []byte) *Script

NewScript creates a Script object.

func (*Script) Add

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

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

func (*Script) CompileRun

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

CompileRun compiles the source script to bytecode and run it once to fill global objects.

func (*Script) CompileRunContext

func (s *Script) CompileRunContext(ctx context.Context) (*Compiled, error)

CompileRunContext compiles the source script to bytecode and run it once to fill global objects.

func (*Script) Remove

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

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

SetImports sets import modules.

func (*Script) SetMaxAllocs

func (s *Script) SetMaxAllocs(n int64)

SetMaxAllocs sets the maximum number of objects allocations during the run time. Compiled script will return tengo.ErrObjectAllocLimit error if it exceeds this limit.

func (*Script) Trace

func (s *Script) Trace(w io.Writer)

Trace set a tracer for compiler and VM for debugging purposes.

Jump to

Keyboard shortcuts

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