-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
33 lines (26 loc) · 960 Bytes
/
Copy pathmain.go
File metadata and controls
33 lines (26 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// embed shows the data round-trip: build a table in Go and hand it to a script
// as a global, then convert Go values to Lua and back with ToValue/FromValue.
package main
import (
"fmt"
luapure "github.com/htcom-code/lua-pure/lua"
)
func main() {
L := luapure.NewState()
L.OpenLibs()
// A config table built in Go, exposed as a global the script reads.
cfg := luapure.NewTable()
cfg.SetStr("name", luapure.MkString("luapure"))
cfg.SetStr("level", luapure.Int(5))
L.SetGlobal("config", cfg.Value())
res, err := L.DoString(`return config.name, config.level * 2`, "=embed")
if err != nil {
panic(err)
}
fmt.Println(res[0].Str(), res[1].AsInt()) // luapure 10
// ToValue turns Go data into Lua values; FromValue converts them back.
list := L.ToValue([]any{10, 20, 30}).AsTable()
fmt.Println(list.Len(), list.GetInt(2).AsInt()) // 3 20
m := luapure.FromValue(L.ToValue(map[string]any{"x": 42})).(map[any]any)
fmt.Println(m["x"]) // 42
}