From 56045ed236a23dbdcc91e44dceb17cd7c22536c2 Mon Sep 17 00:00:00 2001 From: sammyette Date: Sat, 20 Jul 2024 10:50:04 -0400 Subject: [PATCH] feat: implement bait loading --- golibs/bait/bait.go | 31 ++++++++++++++----------------- golibs/commander/commander.go | 3 ++- lua.go | 9 ++++----- moonlight/function_golua.go | 1 + 4 files changed, 21 insertions(+), 23 deletions(-) diff --git a/golibs/bait/bait.go b/golibs/bait/bait.go index 1f85c76..09a7898 100644 --- a/golibs/bait/bait.go +++ b/golibs/bait/bait.go @@ -28,10 +28,10 @@ package bait import ( "errors" + "hilbish/moonlight" "hilbish/util" rt "github.com/arnodel/golua/runtime" - "github.com/arnodel/golua/lib/packagelib" ) type listenerType int @@ -48,26 +48,21 @@ type Listener struct{ typ listenerType once bool caller func(...interface{}) - luaCaller *rt.Closure + luaCaller *moonlight.Closure } type Bait struct{ - Loader packagelib.Loader recoverer Recoverer handlers map[string][]*Listener - rtm *rt.Runtime + rtm *moonlight.Runtime } // New creates a new Bait instance. -func New(rtm *rt.Runtime) *Bait { +func New(rtm *moonlight.Runtime) *Bait { b := &Bait{ handlers: make(map[string][]*Listener), rtm: rtm, } - b.Loader = packagelib.Loader{ - Load: b.loaderFunc, - Name: "bait", - } return b } @@ -88,16 +83,16 @@ func (b *Bait) Emit(event string, args ...interface{}) { if handle.typ == luaListener { funcVal := rt.FunctionValue(handle.luaCaller) - var luaArgs []rt.Value + var luaArgs []moonlight.Value for _, arg := range args { - var luarg rt.Value + var luarg moonlight.Value switch arg.(type) { case rt.Value: luarg = arg.(rt.Value) default: luarg = rt.AsValue(arg) } luaArgs = append(luaArgs, luarg) } - _, err := rt.Call1(b.rtm.MainThread(), funcVal, luaArgs...) + _, err := b.rtm.Call1(funcVal, luaArgs...) if err != nil { if event != "error" { b.Emit("error", event, handle.luaCaller, err.Error()) @@ -212,18 +207,20 @@ func (b *Bait) callRecoverer(event string, handler *Listener, err interface{}) { b.recoverer(event, handler, err) } -func (b *Bait) loaderFunc(rtm *rt.Runtime) (rt.Value, func()) { - exports := map[string]util.LuaExport{ +func (b *Bait) Loader(rtm *moonlight.Runtime) moonlight.Value { + exports := map[string]moonlight.Export{ + /* "catch": util.LuaExport{b.bcatch, 2, false}, "catchOnce": util.LuaExport{b.bcatchOnce, 2, false}, "throw": util.LuaExport{b.bthrow, 1, true}, "release": util.LuaExport{b.brelease, 2, false}, "hooks": util.LuaExport{b.bhooks, 1, false}, + */ } - mod := rt.NewTable() - util.SetExports(rtm, mod, exports) + mod := moonlight.NewTable() + rtm.SetExports(mod, exports) - return rt.TableValue(mod), nil + return moonlight.TableValue(mod) } func handleHook(t *rt.Thread, c *rt.GoCont, name string, catcher *rt.Closure, args ...interface{}) { diff --git a/golibs/commander/commander.go b/golibs/commander/commander.go index 840aaa1..1a9a90f 100644 --- a/golibs/commander/commander.go +++ b/golibs/commander/commander.go @@ -33,6 +33,7 @@ This sink is for writing errors, as the name would suggest. package commander import ( + "hilbish/moonlight" "hilbish/util" "hilbish/golibs/bait" @@ -46,7 +47,7 @@ type Commander struct{ Commands map[string]*rt.Closure } -func New(rtm *rt.Runtime) *Commander { +func New(rtm *moonlight.Runtime) *Commander { c := &Commander{ Events: bait.New(rtm), Commands: make(map[string]*rt.Closure), diff --git a/lua.go b/lua.go index 161fa38..3755f0e 100644 --- a/lua.go +++ b/lua.go @@ -5,7 +5,7 @@ import ( "os" //"hilbish/util" - //"hilbish/golibs/bait" + "hilbish/golibs/bait" //"hilbish/golibs/commander" //"hilbish/golibs/fs" //"hilbish/golibs/terminal" @@ -30,15 +30,14 @@ func luaInit() { cmds = commander.New(l) lib.LoadLibs(l, cmds.Loader) - +*/ hooks = bait.New(l) hooks.SetRecoverer(func(event string, handler *bait.Listener, err interface{}) { fmt.Println("Error in `error` hook handler:", err) hooks.Off(event, handler) }) - - lib.LoadLibs(l, hooks.Loader) - + l.LoadLibrary(hooks.Loader, "bait") +/* // Add Ctrl-C handler hooks.On("signal.sigint", func(...interface{}) { if !interactive { diff --git a/moonlight/function_golua.go b/moonlight/function_golua.go index ad019ff..c48bdd2 100644 --- a/moonlight/function_golua.go +++ b/moonlight/function_golua.go @@ -8,6 +8,7 @@ type GoFunctionFunc = rt.GoFunctionFunc type GoCont = rt.GoCont type Cont = rt.Cont +type Closure = rt.Closure func (mlr *Runtime) CheckNArgs(c *GoCont, num int) error { return c.CheckNArgs(num)