From 57a65cb039e31d956906105f1a14d5261d745229 Mon Sep 17 00:00:00 2001 From: sammyette Date: Sat, 20 Jul 2024 11:46:07 -0400 Subject: [PATCH] feat: add moonlight to commander --- golibs/bait/bait.go | 10 ++++++-- golibs/commander/commander.go | 44 +++++++++++++++-------------------- lua.go | 8 +++---- moonlight/function_golua.go | 4 ++++ util/util.go | 10 ++++---- 5 files changed, 41 insertions(+), 35 deletions(-) diff --git a/golibs/bait/bait.go b/golibs/bait/bait.go index 09a7898..9359bd9 100644 --- a/golibs/bait/bait.go +++ b/golibs/bait/bait.go @@ -26,10 +26,10 @@ this function will set the user prompt. package bait import ( - "errors" + //"errors" "hilbish/moonlight" - "hilbish/util" + //"hilbish/util" rt "github.com/arnodel/golua/runtime" ) @@ -255,6 +255,7 @@ bait.catch('hilbish.exit', function() end) #example */ +/* func (b *Bait) bcatch(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { name, catcher, err := util.HandleStrCallback(t, c) if err != nil { @@ -312,6 +313,7 @@ func (b *Bait) bhooks(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { return c.PushingNext1(t.Runtime, rt.TableValue(luaHandlers)), nil } +*/ // release(name, catcher) // Removes the `catcher` for the event with `name`. @@ -330,6 +332,7 @@ bait.release('event', hookCallback) -- and now hookCallback will no longer be ran for the event. #example */ +/* func (b *Bait) brelease(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { name, catcher, err := util.HandleStrCallback(t, c) if err != nil { @@ -340,6 +343,7 @@ func (b *Bait) brelease(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { return c.Next(), nil } +*/ // throw(name, ...args) // #param name string The name of the hook. @@ -355,6 +359,7 @@ bait.catch('gretting', function(greetTo) end) #example */ +/* func (b *Bait) bthrow(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { if err := c.Check1Arg(); err != nil { return nil, err @@ -371,3 +376,4 @@ func (b *Bait) bthrow(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { return c.Next(), nil } +*/ diff --git a/golibs/commander/commander.go b/golibs/commander/commander.go index 1a9a90f..53d55f3 100644 --- a/golibs/commander/commander.go +++ b/golibs/commander/commander.go @@ -38,12 +38,10 @@ import ( "hilbish/golibs/bait" rt "github.com/arnodel/golua/runtime" - "github.com/arnodel/golua/lib/packagelib" ) type Commander struct{ Events *bait.Bait - Loader packagelib.Loader Commands map[string]*rt.Closure } @@ -52,24 +50,20 @@ func New(rtm *moonlight.Runtime) *Commander { Events: bait.New(rtm), Commands: make(map[string]*rt.Closure), } - c.Loader = packagelib.Loader{ - Load: c.loaderFunc, - Name: "commander", - } return c } -func (c *Commander) loaderFunc(rtm *rt.Runtime) (rt.Value, func()) { - exports := map[string]util.LuaExport{ - "register": util.LuaExport{c.cregister, 2, false}, - "deregister": util.LuaExport{c.cderegister, 1, false}, - "registry": util.LuaExport{c.cregistry, 0, false}, +func (c *Commander) Loader(rtm *moonlight.Runtime) moonlight.Value { + exports := map[string]moonlight.Export{ + "register": {c.cregister, 2, false}, + "deregister": {c.cderegister, 1, false}, + "registry": {c.cregistry, 0, 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) } // register(name, cb) @@ -89,8 +83,8 @@ commander.register('hello', function(args, sinks) end) #example */ -func (c *Commander) cregister(t *rt.Thread, ct *rt.GoCont) (rt.Cont, error) { - cmdName, cmd, err := util.HandleStrCallback(t, ct) +func (c *Commander) cregister(mlr *moonlight.Runtime, ct *moonlight.GoCont) (moonlight.Cont, error) { + cmdName, cmd, err := util.HandleStrCallback(mlr, ct) if err != nil { return nil, err } @@ -103,11 +97,11 @@ func (c *Commander) cregister(t *rt.Thread, ct *rt.GoCont) (rt.Cont, error) { // deregister(name) // Removes the named command. Note that this will only remove Commander-registered commands. // #param name string Name of the command to remove. -func (c *Commander) cderegister(t *rt.Thread, ct *rt.GoCont) (rt.Cont, error) { - if err := ct.Check1Arg(); err != nil { +func (c *Commander) cderegister(mlr *moonlight.Runtime, ct *moonlight.GoCont) (moonlight.Cont, error) { + if err := mlr.Check1Arg(ct); err != nil { return nil, err } - cmdName, err := ct.StringArg(0) + cmdName, err := mlr.StringArg(ct, 0) if err != nil { return nil, err } @@ -121,14 +115,14 @@ func (c *Commander) cderegister(t *rt.Thread, ct *rt.GoCont) (rt.Cont, error) { // Returns all registered commanders. Returns a list of tables with the following keys: // - `exec`: The function used to run the commander. Commanders require args and sinks to be passed. // #returns table -func (c *Commander) cregistry(t *rt.Thread, ct *rt.GoCont) (rt.Cont, error) { - registryLua := rt.NewTable() +func (c *Commander) cregistry(mlr *moonlight.Runtime, ct *moonlight.GoCont) (moonlight.Cont, error) { + registryLua := moonlight.NewTable() for cmdName, cmd := range c.Commands { - cmdTbl := rt.NewTable() - cmdTbl.Set(rt.StringValue("exec"), rt.FunctionValue(cmd)) + cmdTbl := moonlight.NewTable() + cmdTbl.SetField("exec", rt.FunctionValue(cmd)) - registryLua.Set(rt.StringValue(cmdName), rt.TableValue(cmdTbl)) + registryLua.SetField(cmdName, moonlight.TableValue(cmdTbl)) } - return ct.PushingNext1(t.Runtime, rt.TableValue(registryLua)), nil + return mlr.PushNext1(ct, moonlight.TableValue(registryLua)), nil } diff --git a/lua.go b/lua.go index 3f1bdc5..69dc5b6 100644 --- a/lua.go +++ b/lua.go @@ -6,7 +6,7 @@ import ( //"hilbish/util" "hilbish/golibs/bait" - //"hilbish/golibs/commander" + "hilbish/golibs/commander" "hilbish/golibs/fs" //"hilbish/golibs/terminal" @@ -28,10 +28,10 @@ func luaInit() { l.LoadLibrary(f.Loader, "fs") /* lib.LoadLibs(l, terminal.Loader) - - cmds = commander.New(l) - lib.LoadLibs(l, cmds.Loader) */ + cmds = commander.New(l) + l.LoadLibrary(cmds.Loader, "commander") + hooks = bait.New(l) hooks.SetRecoverer(func(event string, handler *bait.Listener, err interface{}) { fmt.Println("Error in `error` hook handler:", err) diff --git a/moonlight/function_golua.go b/moonlight/function_golua.go index 5e882ae..ad30337 100644 --- a/moonlight/function_golua.go +++ b/moonlight/function_golua.go @@ -18,6 +18,10 @@ func (mlr *Runtime) StringArg(c *GoCont, num int) (string, error) { return c.cont.StringArg(num) } +func (mlr *Runtime) ClosureArg(c *GoCont, num int) (*Closure, error) { + return c.cont.ClosureArg(num) +} + func (mlr *Runtime) GoFunction(fun GoToLuaFunc) rt.GoFunctionFunc { return func(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { gocont := GoCont{ diff --git a/util/util.go b/util/util.go index c50fae3..46a6a2d 100644 --- a/util/util.go +++ b/util/util.go @@ -4,6 +4,8 @@ import ( "strings" "os/user" + "hilbish/moonlight" + rt "github.com/arnodel/golua/runtime" ) @@ -15,15 +17,15 @@ func SetField(module *rt.Table, field string, value rt.Value) { // HandleStrCallback handles function parameters for Go functions which take // a string and a closure. -func HandleStrCallback(t *rt.Thread, c *rt.GoCont) (string, *rt.Closure, error) { - if err := c.CheckNArgs(2); err != nil { +func HandleStrCallback(mlr *moonlight.Runtime, c *moonlight.GoCont) (string, *moonlight.Closure, error) { + if err := mlr.CheckNArgs(c, 2); err != nil { return "", nil, err } - name, err := c.StringArg(0) + name, err := mlr.StringArg(c, 0) if err != nil { return "", nil, err } - cb, err := c.ClosureArg(1) + cb, err := mlr.ClosureArg(c, 1) if err != nil { return "", nil, err }