-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbinder.go
141 lines (114 loc) · 2.54 KB
/
binder.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
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
package binder
import (
"github.com/yuin/gopher-lua"
"io/ioutil"
)
// Handler is binder function handler
type Handler func(*Context) error
// Binder is a binder... that's all
type Binder struct {
*Loader
state *lua.LState
loaders []*Loader
}
// Load apply Loader
func (b *Binder) Load(loader *Loader) {
b.loaders = append(b.loaders, loader)
}
// DoString runs lua script string
func (b *Binder) DoString(s string) error {
b.load()
return b.do(b.state.DoString(s), func(problem int) *source {
return newSource(s, problem)
})
}
// DoFile runs lua script file
func (b *Binder) DoFile(f string) error {
b.load()
return b.do(b.state.DoFile(f), func(problem int) *source {
s, _ := ioutil.ReadFile(f)
return newSource(string(s), problem)
})
}
// do applies returns improved errors if it needed
func (b *Binder) do(err error, h errSourceHandler) error {
if err != nil {
return newError(err, h)
}
return nil
}
// source returns lua source script
func (b *Binder) source() string {
return b.state.String()
}
func (b *Binder) load() {
loaders := append([]*Loader{b.Loader}, b.loaders...)
for _, l := range loaders {
l.load(b.state)
}
}
// New returns new binder instance
func New(opts ...Options) *Binder {
options := []lua.Options{}
if len(opts) > 0 {
o := opts[0]
options = append(options, lua.Options{
CallStackSize: o.CallStackSize,
RegistrySize: o.RegistrySize,
SkipOpenLibs: o.SkipOpenLibs,
IncludeGoStackTrace: o.IncludeGoStackTrace,
})
}
s := lua.NewState(options...)
defer s.Close()
if len(opts) > 0 && opts[0].SkipOpenLibs {
type lib struct {
Name string
Func lua.LGFunction
}
libs := []lib{
{lua.LoadLibName, lua.OpenPackage},
{lua.BaseLibName, lua.OpenBase},
{lua.TabLibName, lua.OpenTable},
}
for _, l := range libs {
s.Push(s.NewFunction(l.Func))
s.Push(lua.LString(l.Name))
s.Call(1, 0)
}
}
b := &Binder{
state: s,
loaders: []*Loader{},
}
b.Loader = NewLoader()
return b
}
// NewLoader returns new loader
func NewLoader() *Loader {
return &Loader{
funcs: map[string]Handler{},
modules: []*Module{},
tables: []*Table{},
}
}
func exports(funcs map[string]Handler) map[string]lua.LGFunction {
e := make(map[string]lua.LGFunction, len(funcs))
for name, handler := range funcs {
e[name] = handle(handler)
}
return e
}
func handle(handler Handler) lua.LGFunction {
return func(state *lua.LState) int {
c := &Context{
state: state,
}
err := handler(c)
if err != nil {
c.error(err.Error())
return 0
}
return c.pushed
}
}