/
luaplugins.go
46 lines (37 loc) · 918 Bytes
/
luaplugins.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
package luaplugins
import (
"github.com/aarzilli/golua/lua"
"github.com/stevedonovan/luar"
)
type Runtime struct {
plugins map[string]*lua.State
}
func GetRuntime() *Runtime {
return &Runtime{
plugins: make(map[string]*lua.State),
}
}
func (r Runtime) FileExtension() string {
return ".lua"
}
func (r Runtime) InitPlugin(name, source string, implements func(string)) error {
context := luar.Init()
luar.Register(context, "", luar.Map{
"implements": func(interfaceName string) {
implements(interfaceName)
},
})
context.DoString(source)
r.plugins[name] = context
return nil
}
func (r Runtime) CallPlugin(name, function string, args []interface{}) (interface{}, error) {
luafn := luar.NewLuaObjectFromName(r.plugins[name], function)
value, err := luafn.Call(args...)
if err != nil {
return nil, err
}
return value, nil
}
func (r Runtime) LoadEnvironment(environment interface{}) {
}