forked from navy1125/gostudy
/
luatest.go
93 lines (77 loc) · 1.56 KB
/
luatest.go
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package main
import (
"fmt"
"github.com/stevedonovan/luar"
"reflect"
"strings"
)
const test = `
for i = 1,10 do
Print(MSG,i)
end
`
type MyStruct struct {
Name string
Age int
}
func TestCall(v interface{}) {
fmt.Println("aaaaaaaaaaaaaaaaaaaaaaa", reflect.TypeOf(v))
}
const code = `
Print(#M)
Print(M.one)
Print 'pairs over Go maps'
for k,v in pairs(M) do
Print(k,v)
end
Print 'ipairs over Go slices'
for i,v in ipairs(S) do
Print(i,v)
end
ST.Name="dddddddddddddd"
Print(ST.Name)
testcall({a=1,b=2})
`
func main() {
whj := "wang::hai::jun"
fmt.Println(strings.Split(whj, "::"))
TestCall("111")
L := luar.Init()
defer L.Close()
M := luar.Map{
"one": "ein",
"two": "zwei",
"three": "drei",
}
S := []string{"alfred", "alice", "bob", "frodo"}
ST := &MyStruct{"Dolly", 46}
luar.Register(L, "", luar.Map{
"Print": fmt.Println,
"testcall": TestCall,
"print": fmt.Println,
"MSG": "hello", // can also register constants
"M": M,
"S": S,
"ST": ST,
})
//L.DoString(test)
L.DoString(code)
L.GetGlobal("print")
print := luar.NewLuaObject(L, -1)
print.Call("one two", 12)
L.GetGlobal("package")
pack := luar.NewLuaObject(L, -1)
fmt.Println(pack.Get("path"))
lcopy := luar.NewLuaObjectFromValue
gsub := luar.NewLuaObjectFromName(L, "string.gsub")
rmap := lcopy(L, luar.Map{
"NAME": "Dolly",
"HOME": "where you belong",
})
res, err := gsub.Call("hello $NAME go $HOME", "%$(%u+)", rmap)
if res == nil {
fmt.Println("error", err)
} else {
fmt.Println("\033[0;31mresult\033[0m", res)
}
}