-
Notifications
You must be signed in to change notification settings - Fork 1
/
lua_extern.go
100 lines (90 loc) · 2.03 KB
/
lua_extern.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
94
95
96
97
98
99
100
package lua
import (
"errors"
"unsafe"
)
//#cgo linux freebsd darwin pkg-config: luajit
//#cgo LDFLAGS: -lluajit-5.1 -ldl -lm
//#include "glua.h"
import "C"
var (
methodDic map[string]func(...interface{}) (interface{}, error)
)
func init() {
methodDic = make(map[string]func(...interface{}) (interface{}, error))
}
func RegisterExternMethod(methodName string, method func(...interface{}) (interface{}, error)) error {
_, ok := methodDic[methodName]
if ok {
return errors.New("Duplicate Method Name")
}
methodDic[methodName] = method
return nil
}
//export sync_go_method
func sync_go_method(vm *C.struct_lua_State) C.int {
count := int(C.lua_gettop(vm))
args := make([]interface{}, count)
for {
count = int(C.lua_gettop(vm))
if count == 0 {
break
}
args[count-1] = pullFromLua(vm, -1)
C.glua_pop(vm, 1)
}
methodName := args[0].(string)
if len(args) > 1 {
args = args[1:]
} else {
args = make([]interface{}, 0)
}
tagetMethod, ok := methodDic[methodName]
if false == ok {
C.lua_pushnil(vm)
cStr := C.CString("Invalid Method Name")
defer C.free(unsafe.Pointer(cStr))
C.lua_pushstring(vm, cStr)
return 2
}
res, err := tagetMethod(args...)
if err != nil {
pushToLua(vm, 0)
cStr := C.CString(err.Error())
defer C.free(unsafe.Pointer(cStr))
C.lua_pushstring(vm, cStr)
return 2
} else {
pushToLua(vm, res)
C.lua_pushnil(vm)
return 2
}
}
//export async_go_method
func async_go_method(vm *C.struct_lua_State) C.int {
count := int(C.lua_gettop(vm))
args := make([]interface{}, count)
for {
count = int(C.lua_gettop(vm))
if count == 0 {
break
}
args[count-1] = pullFromLua(vm, -1)
C.glua_pop(vm, 1)
}
methodName := args[0].(string)
if len(args) > 1 {
args = args[1:]
} else {
args = make([]interface{}, 0)
}
storeYieldContext(vm, methodName, args...)
return 0
}
func callExternMethod(methodName string, args ...interface{}) (interface{}, error) {
tagetMethod, ok := methodDic[methodName]
if false == ok {
return nil, errors.New("Invalid Method Name")
}
return tagetMethod(args...)
}