-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloader.go
42 lines (34 loc) · 917 Bytes
/
loader.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
package netcl
import lua "github.com/yuin/gopher-lua"
const (
luaNetClientTypeName = "net_conn"
luaDefaultModName = "netcl"
)
var exports = map[string]lua.LGFunction{
"dial": Dial,
}
func registerNetClientType(L *lua.LState) {
methods := map[string]lua.LGFunction{
"read": Read,
"readline": ReadLine,
"write": Write,
"close": Close,
"set_timeouts": SetTimeouts,
}
mt := L.NewTypeMetatable(luaNetClientTypeName)
L.SetGlobal(luaNetClientTypeName, mt)
L.SetField(mt, "__index", L.SetFuncs(L.NewTable(), methods))
}
// Loader loads module into givel Lua state
func Loader(L *lua.LState) int {
registerNetClientType(L)
t := L.NewTable()
L.SetFuncs(t, exports)
L.Push(t)
return 1
}
// Preload add netcl to given lua state's package preload table.
// Lua: local netcl = requirte("netcl")
func Preload(L *lua.LState) {
L.PreloadModule(luaDefaultModName, Loader)
}