-
Notifications
You must be signed in to change notification settings - Fork 0
/
userdata.go
50 lines (43 loc) · 1.06 KB
/
userdata.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
package gobindlua
import lua "github.com/yuin/gopher-lua"
type LuaRegistrar interface {
LuaModuleName() string
LuaModuleLoader(L *lua.LState) int
LuaRegisterGlobalMetatable(L *lua.LState)
}
func Register(L *lua.LState, toRegister ...LuaRegistrar) {
LuaArrayModuleLoader(L)
L.PreloadModule(ARRAY_MODULES_NAME, LuaArrayModuleLoader)
LuaArrayRegisterGlobalMetatable(L)
L.PreloadModule(MAP_MODULES_NAME, LuaMapModuleLoader)
LuaMapRegisterGlobalMetatable(L)
for _, i := range toRegister {
L.PreloadModule(i.LuaModuleName(), i.LuaModuleLoader)
i.LuaRegisterGlobalMetatable(L)
}
}
type LuaUserData interface {
LuaMetatableType() string
}
func NewUserData(data LuaUserData, L *lua.LState) *lua.LUserData {
return &lua.LUserData{
Value: data,
Env: L.Env,
Metatable: L.GetTypeMetatable(data.LuaMetatableType()),
}
}
func UnwrapLValueToAny(l lua.LValue) any {
switch t := l.(type) {
case *lua.LUserData:
switch s := t.Value.(type) {
case *GblSlice:
return s.Slice
case *GblMap:
return s.Map
default:
return s
}
default:
return l
}
}