-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGopherLua.go
60 lines (50 loc) · 1.26 KB
/
GopherLua.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
package GopherLua
import (
lua "github.com/yuin/gopher-lua"
"math"
)
type Lua struct {
State *lua.LState
module []*Module
}
//拓展模块需要实现的接口
type Module interface {
RegisterType(L *lua.LState)
Close()
}
func NewState() *Lua {
return &Lua{
State: lua.NewState(),
}
}
func (instance *Lua) DoString(source string) error {
return instance.State.DoString(source)
}
func (instance *Lua) DoFile(path string) error {
return instance.State.DoFile(path)
}
func (instance *Lua) ExecuteFunc(funcName string, returnParamsCount int, args ...lua.LValue) error {
return instance.State.CallByParam(lua.P{
Fn: instance.State.GetGlobal(funcName),
NRet: returnParamsCount,
Protect: false,
}, args...)
}
func (instance *Lua) Register(module ...Module) {
for _, interfaceModule := range module {
instance.module = append(instance.module, &interfaceModule)
interfaceModule.RegisterType(instance.State)
}
}
func (instance *Lua) Close() {
for i := range instance.module {
var module = instance.module[i]
(*module).Close()
}
}
//获取栈中指定返回值,并pop出去,节约内存
func (instance *Lua) GetAndPop(idx int) lua.LValue {
var value = instance.State.Get(idx)
instance.State.Pop(int(math.Abs(float64(idx))))
return value
}