xgo/docs/interoperability.md

261 lines
7.6 KiB
Markdown
Raw Normal View History

2019-01-24 04:35:57 +03:00
# Interoperability
2020-03-04 17:53:38 +03:00
## Table of Contents
2019-01-24 04:35:57 +03:00
- [Using Scripts](#using-scripts)
- [Type Conversion Table](#type-conversion-table)
2019-01-31 08:50:15 +03:00
- [User Types](#user-types)
- [Sandbox Environments](#sandbox-environments)
- [Concurrency](#concurrency)
2019-01-25 00:56:41 +03:00
- [Compiler and VM](#compiler-and-vm)
2019-01-24 04:35:57 +03:00
## Using Scripts
2019-12-20 22:40:38 +03:00
Embedding and executing the Tengo code in Go is very easy. At a high level,
this process is like:
2019-01-24 04:35:57 +03:00
2019-12-20 22:40:38 +03:00
- create a [Script](https://godoc.org/github.com/d5/tengo#Script) instance with
your code,
- _optionally_ add some
[Script Variables](https://godoc.org/github.com/d5/tengo#Variable) to Script,
2019-01-24 04:35:57 +03:00
- compile or directly run the script,
2019-12-20 22:40:38 +03:00
- retrieve _output_ values from the
[Compiled](https://godoc.org/github.com/d5/tengo#Compiled) instance.
2019-01-24 04:35:57 +03:00
2019-12-20 22:40:38 +03:00
The following is an example where a Tengo script is compiled and run with no
input/output variables.
2019-01-24 03:30:41 +03:00
```golang
2019-12-30 00:38:51 +03:00
import "github.com/d5/tengo/v2"
2019-01-24 03:30:41 +03:00
var code = `
reduce := func(seq, fn) {
s := 0
for x in seq { fn(x, s) }
return s
}
print(reduce([1, 2, 3], func(x, s) { s += x }))
`
func main() {
2019-12-20 22:40:38 +03:00
s := tengo.NewScript([]byte(code))
2019-01-24 03:30:41 +03:00
if _, err := s.Run(); err != nil {
panic(err)
}
}
```
2019-12-20 22:40:38 +03:00
Here's another example where an input variable is added to the script, and, an
output variable is accessed through
[Variable.Int](https://godoc.org/github.com/d5/tengo#Variable.Int) function:
2019-01-24 03:30:41 +03:00
```golang
import (
2020-03-04 17:53:38 +03:00
"fmt"
2019-01-24 03:30:41 +03:00
2020-03-04 17:53:38 +03:00
"github.com/d5/tengo/v2"
2019-01-24 03:30:41 +03:00
)
func main() {
2020-03-04 17:53:38 +03:00
s := tengo.NewScript([]byte(`a := b + 20`))
// define variable 'b'
_ = s.Add("b", 10)
// compile the source
c, err := s.Compile()
if err != nil {
panic(err)
}
// run the compiled bytecode
// a compiled bytecode 'c' can be executed multiple times without re-compiling it
if err := c.Run(); err != nil {
panic(err)
}
// retrieve value of 'a'
a := c.Get("a")
fmt.Println(a.Int()) // prints "30"
// re-run after replacing value of 'b'
if err := c.Set("b", 20); err != nil {
panic(err)
}
if err := c.Run(); err != nil {
panic(err)
}
fmt.Println(c.Get("a").Int()) // prints "40"
2019-01-24 03:30:41 +03:00
}
```
2019-12-20 22:40:38 +03:00
A variable `b` is defined by the user before compilation using
[Script.Add](https://godoc.org/github.com/d5/tengo#Script.Add) function. Then a
compiled bytecode `c` is used to execute the bytecode and get the value of
global variables. In this example, the value of global variable `a` is read
using [Compiled.Get](https://godoc.org/github.com/d5/tengo#Compiled.Get)
function. See
[documentation](https://godoc.org/github.com/d5/tengo#Variable) for the
full list of variable value functions.
2019-01-24 03:30:41 +03:00
2019-12-20 22:40:38 +03:00
Value of the global variables can be replaced using
[Compiled.Set](https://godoc.org/github.com/d5/tengo#Compiled.Set) function.
But it will return an error if you try to set the value of un-defined global
variables _(e.g. trying to set the value of `x` in the example)_.
2019-01-24 04:35:57 +03:00
### Type Conversion Table
2019-01-24 03:30:41 +03:00
2019-12-20 22:40:38 +03:00
When adding a Variable
_([Script.Add](https://godoc.org/github.com/d5/tengo#Script.Add))_, Script
converts Go values into Tengo values based on the following conversion table.
2019-01-24 03:30:41 +03:00
2019-01-24 04:35:57 +03:00
| Go Type | Tengo Type | Note |
| :--- | :--- | :--- |
|`nil`|`Undefined`||
|`string`|`String`||
|`int64`|`Int`||
|`int`|`Int`||
|`bool`|`Bool`||
|`rune`|`Char`||
|`byte`|`Char`||
|`float64`|`Float`||
|`[]byte`|`Bytes`||
2019-01-31 08:50:15 +03:00
|`time.Time`|`Time`||
2019-01-24 04:35:57 +03:00
|`error`|`Error{String}`|use `error.Error()` as String value|
|`map[string]Object`|`Map`||
|`map[string]interface{}`|`Map`|individual elements converted to Tengo objects|
|`[]Object`|`Array`||
|`[]interface{}`|`Array`|individual elements converted to Tengo objects|
|`Object`|`Object`|_(no type conversion performed)_|
2019-01-24 03:30:41 +03:00
2019-01-31 08:50:15 +03:00
### User Types
2019-12-20 22:40:38 +03:00
Users can add and use a custom user type in Tengo code by implementing
[Object](https://godoc.org/github.com/d5/tengo#Object) interface. Tengo runtime
will treat the user types in the same way it does to the runtime types with no
performance overhead. See
[Object Types](https://github.com/d5/tengo/blob/master/docs/objects.md) for
more details.
2019-01-25 00:56:41 +03:00
## Sandbox Environments
2019-12-20 22:40:38 +03:00
To securely compile and execute _potentially_ unsafe script code, you can use
the following Script functions.
2019-01-24 04:35:57 +03:00
2020-03-04 17:53:38 +03:00
### Script.SetImports(modules *objects.ModuleMap)
2019-12-20 22:40:38 +03:00
SetImports sets the import modules with corresponding names. Script **does not**
include any modules by default. You can use this function to include the
[Standard Library](https://github.com/d5/tengo/blob/master/docs/stdlib.md).
2019-01-25 00:56:41 +03:00
```golang
2019-12-20 22:40:38 +03:00
s := tengo.NewScript([]byte(`math := import("math"); a := math.abs(-19.84)`))
2019-04-09 05:51:26 +03:00
s.SetImports(stdlib.GetModuleMap("math"))
// or, to include all stdlib at once
2019-04-09 05:51:26 +03:00
s.SetImports(stdlib.GetModuleMap(stdlib.AllModuleNames()...))
```
2019-12-20 22:40:38 +03:00
You can also include Tengo's written module using `objects.SourceModule`
(which implements `objects.Importable`).
2019-01-25 00:56:41 +03:00
```golang
2019-12-20 22:40:38 +03:00
s := tengo.NewScript([]byte(`double := import("double"); a := double(20)`))
2019-01-25 00:56:41 +03:00
2019-12-20 22:40:38 +03:00
mods := tengo.NewModuleMap()
mods.AddSourceModule("double", []byte(`export func(x) { return x * 2 }`))
s.SetImports(mods)
2019-01-25 00:56:41 +03:00
```
To dynamically load or generate code for imported modules, implement and
provide a `tengo.ModuleGetter`.
```golang
type DynamicModules struct {
mods tengo.ModuleGetter
fallback func (name string) tengo.Importable
}
func (dm *DynamicModules) Get(name string) tengo.Importable {
if mod := dm.mods.Get(name); mod != nil {
return mod
}
return dm.fallback()
}
// ...
mods := &DynamicModules{
mods: stdlib.GetModuleMap("math"),
fallback: func(name string) tengo.Importable {
src := ... // load or generate src for `name`
return &tengo.SourceModule{Src: src}
},
}
s := tengo.NewScript(`foo := import("foo")`)
s.SetImports(mods)
```
2020-03-04 17:53:38 +03:00
### Script.SetMaxAllocs(n int64)
2019-12-20 22:40:38 +03:00
SetMaxAllocs sets the maximum number of object allocations. Note this is a
cumulative metric that tracks only the object creations. Set this to a negative
number (e.g. `-1`) if you don't need to limit the number of allocations.
2020-03-04 17:53:38 +03:00
### Script.EnableFileImport(enable bool)
2019-12-20 22:40:38 +03:00
EnableFileImport enables or disables module loading from the local files. It's
2020-03-04 17:53:38 +03:00
disabled by default.
2020-03-04 17:53:38 +03:00
### tengo.MaxStringLen
2019-12-20 22:40:38 +03:00
Sets the maximum byte-length of string values. This limit applies to all
running VM instances in the process. Also it's not recommended to set or update
2020-03-04 17:53:38 +03:00
this value while any VM is executing.
2020-03-04 17:53:38 +03:00
### tengo.MaxBytesLen
2019-12-20 22:40:38 +03:00
Sets the maximum length of bytes values. This limit applies to all running VM
instances in the process. Also it's not recommended to set or update this value
while any VM is executing.
## Concurrency
2019-12-20 22:40:38 +03:00
A compiled script (`Compiled`) can be used to run the code multiple
times by a goroutine. If you want to run the compiled script by multiple
goroutine, you should use `Compiled.Clone` function to make a copy of Compiled
instances.
2020-03-04 17:53:38 +03:00
### Compiled.Clone()
2019-12-20 22:40:38 +03:00
Clone creates a new copy of Compiled instance. Cloned copies are safe for
concurrent use by multiple goroutines.
```golang
for i := 0; i < concurrency; i++ {
2019-12-20 22:40:38 +03:00
go func(compiled *tengo.Compiled) {
// inputs
_ = compiled.Set("a", rand.Intn(10))
_ = compiled.Set("b", rand.Intn(10))
_ = compiled.Set("c", rand.Intn(10))
2020-03-04 17:53:38 +03:00
if err := compiled.Run(); err != nil {
panic(err)
}
2020-03-04 17:53:38 +03:00
// outputs
d = compiled.Get("d").Int()
e = compiled.Get("e").Int()
}(compiled.Clone()) // Pass the cloned copy of Compiled
}
2020-03-04 17:53:38 +03:00
```
2019-01-25 00:56:41 +03:00
## Compiler and VM
2019-12-20 22:40:38 +03:00
Although it's not recommended, you can directly create and run the Tengo
[Compiler](https://godoc.org/github.com/d5/tengo#Compiler), and
[VM](https://godoc.org/github.com/d5/tengo#VM) for yourself instead of using
Scripts and Script Variables. It's a bit more involved as you have to manage
the symbol tables and global variables between them, but, basically that's what
Script and Script Variable is doing internally.
2019-01-25 00:56:41 +03:00
_TODO: add more information here_