-
Notifications
You must be signed in to change notification settings - Fork 4
/
loader.go
59 lines (48 loc) · 1.01 KB
/
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package binder
import (
"github.com/yuin/gopher-lua"
)
// Loader is basic loader object
type Loader struct {
funcs map[string]Handler
modules []*Module
tables []*Table
}
// Func assign handler with specified alias
func (l *Loader) Func(name string, handler Handler) {
l.funcs[name] = handler
}
// Module creates new module and returns it
func (l *Loader) Module(name string) *Module {
m := &Module{
name: name,
fields: map[string]lua.LValue{},
funcs: map[string]Handler{},
}
l.modules = append(l.modules, m)
return m
}
// Table creates new table and returns it
func (l *Loader) Table(name string) *Table {
t := &Table{
name: name,
static: map[string]Handler{},
dynamic: map[string]Handler{},
}
l.tables = append(l.tables, t)
return t
}
func (l *Loader) load(s *lua.LState) {
f := exports(l.funcs)
for name, fn := range f {
s.SetGlobal(name, s.NewFunction(fn))
}
for _, m := range l.modules {
m.state = s
m.load()
}
for _, t := range l.tables {
t.state = s
t.load()
}
}