aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSimon Ser <contact@emersion.fr>2019-12-09 17:25:02 +0100
committerSimon Ser <contact@emersion.fr>2019-12-09 17:25:02 +0100
commitf42cb45457cc0b581a731745593caeed7b770849 (patch)
treed0f8b7042a70e3200a3ba3487300573fec12f1fc
parent9617b412e8f1a2e3c3d112bbf5ece29bc2af37ee (diff)
downloadalps-f42cb45457cc0b581a731745593caeed7b770849.tar.gz
alps-f42cb45457cc0b581a731745593caeed7b770849.zip
Change Lua API to koushin.on_render
-rw-r--r--plugin.go34
1 files changed, 27 insertions, 7 deletions
diff --git a/plugin.go b/plugin.go
index b94c330..d2737d0 100644
--- a/plugin.go
+++ b/plugin.go
@@ -16,25 +16,33 @@ type Plugin interface {
}
type luaPlugin struct {
- filename string
- state *lua.LState
+ filename string
+ state *lua.LState
+ renderCallbacks map[string]*lua.LFunction
}
func (p *luaPlugin) Name() string {
return p.filename
}
+func (p *luaPlugin) onRender(l *lua.LState) int {
+ name := l.CheckString(1)
+ f := l.CheckFunction(2)
+ p.renderCallbacks[name] = f
+ return 0
+}
+
func (p *luaPlugin) Render(name string, data interface{}) error {
- global := p.state.GetGlobal("render")
- if global == nil {
+ f, ok := p.renderCallbacks[name]
+ if !ok {
return nil
}
if err := p.state.CallByParam(lua.P{
- Fn: global,
+ Fn: f,
NRet: 0,
Protect: true,
- }, lua.LString(name), luar.New(p.state, data)); err != nil {
+ }, luar.New(p.state, data)); err != nil {
return err
}
@@ -48,11 +56,23 @@ func (p *luaPlugin) Close() error {
func loadLuaPlugin(filename string) (*luaPlugin, error) {
l := lua.NewState()
+ p := &luaPlugin{
+ filename: filename,
+ state: l,
+ renderCallbacks: make(map[string]*lua.LFunction),
+ }
+
+ mt := l.NewTypeMetatable("koushin")
+ l.SetGlobal("koushin", mt)
+ l.SetField(mt, "on_render", l.NewFunction(p.onRender))
+ // TODO: set_filter
+
if err := l.DoFile(filename); err != nil {
+ l.Close()
return nil, err
}
- return &luaPlugin{filename, l}, nil
+ return p, nil
}
func loadAllLuaPlugins(log echo.Logger) ([]Plugin, error) {