Repository navigation
Expand file tree
/
Copy pathlua-func.go
More file actions
152 lines (132 loc) · 3.89 KB
/
Copy pathlua-func.go
File metadata and controls
152 lines (132 loc) · 3.89 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package lua
// #include "lua.h"
// #include "lauxlib.h"
// static void popN(lua_State *L, int n);
// static void pushGlobal(lua_State *L);
// static int pCall(lua_State *L, int nargs, int nresults) {
// return lua_pcall(L, nargs, nresults, 0);
// }
// // static int gc(lua_State *L, int what) {
// // return lua_gc(L, what);
// // }
import "C"
import (
elutils "github.com/rosbit/go-embedding-utils"
"reflect"
"unsafe"
"fmt"
)
func bindFunc(ctx *LuaContext, funcName string, funcVarPtr interface{}) (err error) {
helper, e := elutils.NewEmbeddingFuncHelper(funcVarPtr)
if e != nil {
err = e
return
}
helper.BindEmbeddingFunc(wrapFunc(ctx, funcName, helper))
return
}
func wrapFunc(ctx *LuaContext, funcName string, helper *elutils.EmbeddingFuncHelper) elutils.FnGoFunc {
return func(args []reflect.Value) (results []reflect.Value) {
ctx.mu.Lock()
defer ctx.mu.Unlock()
c := ctx.c
// reload the function when calling go-function
C.pushGlobal(c) // [ global ]
getVar(c, funcName) // [ global function ]
return callLuaFuncFromGo(c, helper, args)
}
}
func collectFuncResult(ctx *C.lua_State, nOut int) (goVal interface{}, err error) {
// [ some-obj o1 o2 .. oN ]
switch nOut {
case 0:
case 1:
goVal, err = fromLuaValue(ctx)
default:
res := make([]interface{}, nOut)
for i:=0; i<nOut; i++ {
C.lua_pushnil(ctx) // [ some-obj o1 o2 .. oN nil ]
C.lua_copy(ctx, C.int(i - nOut - 1), -1) // [ some-obj o1 o2 .. oN oI]
res[i], err = fromLuaValue(ctx)
C.popN(ctx, 1) // [ some-obj o1 o2 .. oN ]
if err != nil {
break
}
}
goVal = res
}
C.popN(ctx, C.int(nOut+1)) // []
return
}
// called by wrapFunc() and fromLuaFunc::bindGoFunc
func callLuaFuncFromGo(ctx *C.lua_State, helper *elutils.EmbeddingFuncHelper, args []reflect.Value) (results []reflect.Value) {
// defer C.gc(ctx, C.LUA_GCCOLLECT)
// [ some-obj function ]
// push Lua args
argc := 0
itArgs := helper.MakeGoFuncArgs(args)
for arg := range itArgs {
pushLuaMetaValue(ctx, arg)
argc += 1
}
// [ some-obj function arg1 arg2 ... argN ]
// call Lua function
nOut, withLastErr := helper.NumOut()
if withLastErr {
nOut -= 1
}
var err error
var goVal interface{}
if C.pCall(ctx, C.int(argc), C.int(nOut)) != 0 {
// [ some-obj err ]
err = fmt.Errorf("%s", C.GoString(C.lua_tolstring(ctx, -1, (*C.ulong)(unsafe.Pointer(nil)))))
C.popN(ctx, 2) // [ ]
goto OUT
}
// [ some-obj o1 o2 .. oN ]
goVal, err = collectFuncResult(ctx, nOut)
OUT:
// convert result to golang
results = helper.ToGolangResults(goVal, nOut > 1, err)
return
}
func callFunc(ctx *C.lua_State, args ...interface{}) (res interface{}, err error) {
// [ obj function ]
objIdx := int(C.lua_gettop(ctx) - 1)
n := len(args)
for _, arg := range args {
pushLuaMetaValue(ctx, arg)
}
// [ obj function arg1 arg2 ... argN ]
if C.pCall(ctx, C.int(n), C.LUA_MULTRET) != 0 {
// [ obj err ]
err = fmt.Errorf("%s", C.GoString(C.lua_tolstring(ctx, -1, (*C.ulong)(unsafe.Pointer(nil)))))
C.popN(ctx, 2) // [ ]
return
}
// [ obj o1 o2 ... oN ]
topIdx := int(C.lua_gettop(ctx))
nOut := topIdx - objIdx
res, err = collectFuncResult(ctx, nOut)
return
}
// called by value.go::fromLuaValue()
func fromLuaFunc(ctx *C.lua_State) (bindGoFunc elutils.FnBindGoFunc) {
// [ function ]
C.lua_pushnil(ctx) // [ funciton nil ]
C.lua_copy(ctx, -2, -1) // [ function function-duplicated ]
idx := C.luaL_ref(ctx, C.LUA_REGISTRYINDEX) // [ function ] with registry[idx] = function
bindGoFunc = func(fnVarPtr interface{}) elutils.FnGoFunc {
helper, e := elutils.NewEmbeddingFuncHelper(fnVarPtr)
if e != nil {
return nil
}
return func(args []reflect.Value) (results []reflect.Value) {
// reload the function when calling go-function
C.lua_pushnil(ctx) // [ nil ] used as a placeholder
C.lua_rawgeti(ctx, C.LUA_REGISTRYINDEX, C.lua_Integer(idx)) // [ nil function ]
return callLuaFuncFromGo(ctx, helper, args)
}
}
return bindGoFunc
}