Hilbish/golibs/bait/bait.go

36 lines
631 B
Go
Raw Normal View History

2021-03-26 05:12:55 +00:00
package bait
import (
"github.com/chuckpreslar/emission"
"github.com/yuin/gopher-lua"
2021-03-30 23:47:02 +00:00
"layeh.com/gopher-luar"
2021-03-26 05:12:55 +00:00
)
2021-03-30 23:47:02 +00:00
type Bait struct{
Em *emission.Emitter
}
2021-03-26 05:12:55 +00:00
func New() Bait {
2021-03-30 23:47:02 +00:00
return Bait{
Em: emission.NewEmitter(),
}
2021-03-26 05:12:55 +00:00
}
func (b *Bait) Loader(L *lua.LState) int {
2021-03-30 23:47:02 +00:00
mod := L.SetFuncs(L.NewTable(), map[string]lua.LGFunction{})
L.SetField(mod, "throw", luar.New(L, b.throw))
L.SetField(mod, "catch", luar.New(L, b.catch))
2021-03-26 05:12:55 +00:00
L.Push(mod)
return 1
}
2021-03-30 23:47:02 +00:00
func (b *Bait) throw(name string, args ...interface{}) {
b.Em.Emit(name, args...)
}
func (b *Bait) catch(name string, catcher func(interface{})) {
b.Em.On(name, catcher)
}