feat: add moonlight to commander

midnight-edition
sammyette 2024-07-20 11:46:07 -04:00
parent a5f695eb98
commit 57a65cb039
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
5 changed files with 41 additions and 35 deletions

View File

@ -26,10 +26,10 @@ this function will set the user prompt.
package bait package bait
import ( import (
"errors" //"errors"
"hilbish/moonlight" "hilbish/moonlight"
"hilbish/util" //"hilbish/util"
rt "github.com/arnodel/golua/runtime" rt "github.com/arnodel/golua/runtime"
) )
@ -255,6 +255,7 @@ bait.catch('hilbish.exit', function()
end) end)
#example #example
*/ */
/*
func (b *Bait) bcatch(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func (b *Bait) bcatch(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
name, catcher, err := util.HandleStrCallback(t, c) name, catcher, err := util.HandleStrCallback(t, c)
if err != nil { 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 return c.PushingNext1(t.Runtime, rt.TableValue(luaHandlers)), nil
} }
*/
// release(name, catcher) // release(name, catcher)
// Removes the `catcher` for the event with `name`. // 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. -- and now hookCallback will no longer be ran for the event.
#example #example
*/ */
/*
func (b *Bait) brelease(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func (b *Bait) brelease(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
name, catcher, err := util.HandleStrCallback(t, c) name, catcher, err := util.HandleStrCallback(t, c)
if err != nil { if err != nil {
@ -340,6 +343,7 @@ func (b *Bait) brelease(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return c.Next(), nil return c.Next(), nil
} }
*/
// throw(name, ...args) // throw(name, ...args)
// #param name string The name of the hook. // #param name string The name of the hook.
@ -355,6 +359,7 @@ bait.catch('gretting', function(greetTo)
end) end)
#example #example
*/ */
/*
func (b *Bait) bthrow(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func (b *Bait) bthrow(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
if err := c.Check1Arg(); err != nil { if err := c.Check1Arg(); err != nil {
return nil, err return nil, err
@ -371,3 +376,4 @@ func (b *Bait) bthrow(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return c.Next(), nil return c.Next(), nil
} }
*/

View File

@ -38,12 +38,10 @@ import (
"hilbish/golibs/bait" "hilbish/golibs/bait"
rt "github.com/arnodel/golua/runtime" rt "github.com/arnodel/golua/runtime"
"github.com/arnodel/golua/lib/packagelib"
) )
type Commander struct{ type Commander struct{
Events *bait.Bait Events *bait.Bait
Loader packagelib.Loader
Commands map[string]*rt.Closure Commands map[string]*rt.Closure
} }
@ -52,24 +50,20 @@ func New(rtm *moonlight.Runtime) *Commander {
Events: bait.New(rtm), Events: bait.New(rtm),
Commands: make(map[string]*rt.Closure), Commands: make(map[string]*rt.Closure),
} }
c.Loader = packagelib.Loader{
Load: c.loaderFunc,
Name: "commander",
}
return c return c
} }
func (c *Commander) loaderFunc(rtm *rt.Runtime) (rt.Value, func()) { func (c *Commander) Loader(rtm *moonlight.Runtime) moonlight.Value {
exports := map[string]util.LuaExport{ exports := map[string]moonlight.Export{
"register": util.LuaExport{c.cregister, 2, false}, "register": {c.cregister, 2, false},
"deregister": util.LuaExport{c.cderegister, 1, false}, "deregister": {c.cderegister, 1, false},
"registry": util.LuaExport{c.cregistry, 0, false}, "registry": {c.cregistry, 0, false},
} }
mod := rt.NewTable() mod := moonlight.NewTable()
util.SetExports(rtm, mod, exports) rtm.SetExports(mod, exports)
return rt.TableValue(mod), nil return moonlight.TableValue(mod)
} }
// register(name, cb) // register(name, cb)
@ -89,8 +83,8 @@ commander.register('hello', function(args, sinks)
end) end)
#example #example
*/ */
func (c *Commander) cregister(t *rt.Thread, ct *rt.GoCont) (rt.Cont, error) { func (c *Commander) cregister(mlr *moonlight.Runtime, ct *moonlight.GoCont) (moonlight.Cont, error) {
cmdName, cmd, err := util.HandleStrCallback(t, ct) cmdName, cmd, err := util.HandleStrCallback(mlr, ct)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -103,11 +97,11 @@ func (c *Commander) cregister(t *rt.Thread, ct *rt.GoCont) (rt.Cont, error) {
// deregister(name) // deregister(name)
// Removes the named command. Note that this will only remove Commander-registered commands. // Removes the named command. Note that this will only remove Commander-registered commands.
// #param name string Name of the command to remove. // #param name string Name of the command to remove.
func (c *Commander) cderegister(t *rt.Thread, ct *rt.GoCont) (rt.Cont, error) { func (c *Commander) cderegister(mlr *moonlight.Runtime, ct *moonlight.GoCont) (moonlight.Cont, error) {
if err := ct.Check1Arg(); err != nil { if err := mlr.Check1Arg(ct); err != nil {
return nil, err return nil, err
} }
cmdName, err := ct.StringArg(0) cmdName, err := mlr.StringArg(ct, 0)
if err != nil { if err != nil {
return nil, err 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: // 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. // - `exec`: The function used to run the commander. Commanders require args and sinks to be passed.
// #returns table // #returns table
func (c *Commander) cregistry(t *rt.Thread, ct *rt.GoCont) (rt.Cont, error) { func (c *Commander) cregistry(mlr *moonlight.Runtime, ct *moonlight.GoCont) (moonlight.Cont, error) {
registryLua := rt.NewTable() registryLua := moonlight.NewTable()
for cmdName, cmd := range c.Commands { for cmdName, cmd := range c.Commands {
cmdTbl := rt.NewTable() cmdTbl := moonlight.NewTable()
cmdTbl.Set(rt.StringValue("exec"), rt.FunctionValue(cmd)) 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
} }

8
lua.go
View File

@ -6,7 +6,7 @@ import (
//"hilbish/util" //"hilbish/util"
"hilbish/golibs/bait" "hilbish/golibs/bait"
//"hilbish/golibs/commander" "hilbish/golibs/commander"
"hilbish/golibs/fs" "hilbish/golibs/fs"
//"hilbish/golibs/terminal" //"hilbish/golibs/terminal"
@ -28,10 +28,10 @@ func luaInit() {
l.LoadLibrary(f.Loader, "fs") l.LoadLibrary(f.Loader, "fs")
/* /*
lib.LoadLibs(l, terminal.Loader) 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 = bait.New(l)
hooks.SetRecoverer(func(event string, handler *bait.Listener, err interface{}) { hooks.SetRecoverer(func(event string, handler *bait.Listener, err interface{}) {
fmt.Println("Error in `error` hook handler:", err) fmt.Println("Error in `error` hook handler:", err)

View File

@ -18,6 +18,10 @@ func (mlr *Runtime) StringArg(c *GoCont, num int) (string, error) {
return c.cont.StringArg(num) 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 { func (mlr *Runtime) GoFunction(fun GoToLuaFunc) rt.GoFunctionFunc {
return func(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { return func(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
gocont := GoCont{ gocont := GoCont{

View File

@ -4,6 +4,8 @@ import (
"strings" "strings"
"os/user" "os/user"
"hilbish/moonlight"
rt "github.com/arnodel/golua/runtime" 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 // HandleStrCallback handles function parameters for Go functions which take
// a string and a closure. // a string and a closure.
func HandleStrCallback(t *rt.Thread, c *rt.GoCont) (string, *rt.Closure, error) { func HandleStrCallback(mlr *moonlight.Runtime, c *moonlight.GoCont) (string, *moonlight.Closure, error) {
if err := c.CheckNArgs(2); err != nil { if err := mlr.CheckNArgs(c, 2); err != nil {
return "", nil, err return "", nil, err
} }
name, err := c.StringArg(0) name, err := mlr.StringArg(c, 0)
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }
cb, err := c.ClosureArg(1) cb, err := mlr.ClosureArg(c, 1)
if err != nil { if err != nil {
return "", nil, err return "", nil, err
} }