mirror of https://github.com/Hilbis/Hilbish
feat: add moonlight to commander
parent
a5f695eb98
commit
57a65cb039
|
@ -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
|
||||
}
|
||||
*/
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
8
lua.go
8
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)
|
||||
|
|
|
@ -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{
|
||||
|
|
10
util/util.go
10
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
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue