From 4f4646032c8d5c18ce297fbfedc5ffdfa38fc3c0 Mon Sep 17 00:00:00 2001 From: Daniel Kang Date: Fri, 1 Feb 2019 11:25:57 -0800 Subject: [PATCH] Update documentation for compiled scripts --- docs/interoperability.md | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/docs/interoperability.md b/docs/interoperability.md index d5b90392..3cf70079 100644 --- a/docs/interoperability.md +++ b/docs/interoperability.md @@ -70,12 +70,23 @@ func main() { // retrieve value of 'a' a := c.Get("a") - fmt.Println(a.Int()) + 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" } ``` A variable `b` is defined by the user before compilation using [Script.Add](https://godoc.org/github.com/d5/tengo/script#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/script#Compiled.Get) function. See [documentation](https://godoc.org/github.com/d5/tengo/script#Variable) for the full list of variable value functions. +Value of the global variables can be replaced using [Compiled.Set](https://godoc.org/github.com/d5/tengo/script#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)_. + ### Type Conversion Table When adding a Variable _([Script.Add](https://godoc.org/github.com/d5/tengo/script#Script.Add))_, Script converts Go values into Tengo values based on the following conversion table.