From 69fcd8e34857a018f849fd242b30fd91aa3d4ba5 Mon Sep 17 00:00:00 2001 From: sammyette Date: Sat, 20 Jul 2024 14:49:43 -0400 Subject: [PATCH] refactor: rewrite runner module for moonlight, add hilbish.runnerMode function --- api.go | 20 ++++++++------ moonlight/function_golua.go | 4 +++ moonlight/value_golua.go | 9 ++++++ runnermode.go | 55 ++++++++++++++++++------------------- 4 files changed, 51 insertions(+), 37 deletions(-) diff --git a/api.go b/api.go index 6c8d501..52c4209 100644 --- a/api.go +++ b/api.go @@ -46,7 +46,9 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value { "cwd": {hlcwd, 0, false}, /* "exec": {hlexec, 1, false}, + */ "runnerMode": {hlrunnerMode, 1, false}, + /* "goro": {hlgoro, 1, true}, "highlighter": {hlhighlighter, 1, false}, "hinter": {hlhinter, 1, false}, @@ -108,8 +110,8 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value { //mod.Set(rt.StringValue("completions"), rt.TableValue(hshcomp)) // hilbish.runner table - //runnerModule := runnerModeLoader(rtm) - //mod.Set(rt.StringValue("runner"), rt.TableValue(runnerModule)) + runnerModule := runnerModeLoader(mlr) + hshMod.SetField("runner", moonlight.TableValue(runnerModule)) // hilbish.jobs table jobs = newJobHandler() @@ -749,6 +751,7 @@ func hlinputMode(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { return c.Next(), nil } +*/ // runnerMode(mode) // Sets the execution/runner mode for interactive Hilbish. @@ -759,25 +762,24 @@ func hlinputMode(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { // will call it to execute user input instead. // Read [about runner mode](../features/runner-mode) for more information. // #param mode string|function -func hlrunnerMode(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { - if err := c.Check1Arg(); err != nil { +func hlrunnerMode(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { + if err := mlr.Check1Arg(c); err != nil { return nil, err } - mode := c.Arg(0) + mode := mlr.Arg(c, 0) - switch mode.Type() { - case rt.StringType: + switch moonlight.Type(mode) { + case moonlight.StringType: switch mode.AsString() { case "hybrid", "hybridRev", "lua", "sh": runnerMode = mode default: return nil, errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.AsString()) } - case rt.FunctionType: runnerMode = mode + case moonlight.FunctionType: runnerMode = mode default: return nil, errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.TypeName()) } return c.Next(), nil } -*/ // hinter(line, pos) // The command line hint handler. It gets called on every key insert to diff --git a/moonlight/function_golua.go b/moonlight/function_golua.go index ad30337..5597968 100644 --- a/moonlight/function_golua.go +++ b/moonlight/function_golua.go @@ -22,6 +22,10 @@ func (mlr *Runtime) ClosureArg(c *GoCont, num int) (*Closure, error) { return c.cont.ClosureArg(num) } +func (mlr *Runtime) Arg(c *GoCont, num int) Value { + return c.cont.Arg(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/moonlight/value_golua.go b/moonlight/value_golua.go index 2300da4..1233563 100644 --- a/moonlight/value_golua.go +++ b/moonlight/value_golua.go @@ -5,6 +5,11 @@ import ( ) type Value = rt.Value +type ValueType = rt.ValueType +const ( + StringType = rt.StringType + FunctionType = rt.FunctionType +) func StringValue(str string) Value { return rt.StringValue(str) @@ -21,3 +26,7 @@ func BoolValue(b bool) Value { func TableValue(t *Table) Value { return rt.TableValue(t.lt) } + +func Type(v Value) ValueType { + return ValueType(v.Type()) +} diff --git a/runnermode.go b/runnermode.go index e66e5fa..3394877 100644 --- a/runnermode.go +++ b/runnermode.go @@ -1,7 +1,7 @@ package main import ( - "hilbish/util" + "hilbish/moonlight" rt "github.com/arnodel/golua/runtime" ) @@ -49,17 +49,15 @@ hilbish.runnerMode(function(input) end) ``` */ -func runnerModeLoader(rtm *rt.Runtime) *rt.Table { - exports := map[string]util.LuaExport{ - /* +func runnerModeLoader(rtm *moonlight.Runtime) *moonlight.Table { + exports := map[string]moonlight.Export{ "sh": {shRunner, 1, false}, "lua": {luaRunner, 1, false}, "setMode": {hlrunnerMode, 1, false}, - */ } - mod := rt.NewTable() - util.SetExports(rtm, mod, exports) + mod := moonlight.NewTable() + rtm.SetExports(mod, exports) return mod } @@ -78,27 +76,27 @@ func _runnerMode() {} // Runs a command in Hilbish's shell script interpreter. // This is the equivalent of using `source`. // #param cmd string -func shRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { - if err := c.Check1Arg(); err != nil { +func shRunner(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { + if err := mlr.Check1Arg(c); err != nil { return nil, err } - cmd, err := c.StringArg(0) + cmd, err := mlr.StringArg(c, 0) if err != nil { return nil, err } _, exitCode, cont, err := execSh(aliases.Resolve(cmd)) - var luaErr rt.Value = rt.NilValue + var luaErr moonlight.Value = rt.NilValue if err != nil { - luaErr = rt.StringValue(err.Error()) + luaErr = moonlight.StringValue(err.Error()) } - runnerRet := rt.NewTable() - runnerRet.Set(rt.StringValue("input"), rt.StringValue(cmd)) - runnerRet.Set(rt.StringValue("exitCode"), rt.IntValue(int64(exitCode))) - runnerRet.Set(rt.StringValue("continue"), rt.BoolValue(cont)) - runnerRet.Set(rt.StringValue("err"), luaErr) + runnerRet := moonlight.NewTable() + runnerRet.SetField("input", moonlight.StringValue(cmd)) + runnerRet.SetField("exitCode", moonlight.IntValue(int(exitCode))) + runnerRet.SetField("continue", moonlight.BoolValue(cont)) + runnerRet.SetField("err", luaErr) - return c.PushingNext(t.Runtime, rt.TableValue(runnerRet)), nil + return mlr.PushNext1(c, moonlight.TableValue(runnerRet)), nil } // #interface runner @@ -106,24 +104,25 @@ func shRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { // Evaluates `cmd` as Lua input. This is the same as using `dofile` // or `load`, but is appropriated for the runner interface. // #param cmd string -func luaRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { - if err := c.Check1Arg(); err != nil { +func luaRunner(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { + if err := mlr.Check1Arg(c); err != nil { return nil, err } - cmd, err := c.StringArg(0) + cmd, err := mlr.StringArg(c, 0) if err != nil { return nil, err } input, exitCode, err := handleLua(cmd) - var luaErr rt.Value = rt.NilValue + var luaErr moonlight.Value = rt.NilValue if err != nil { - luaErr = rt.StringValue(err.Error()) + luaErr = moonlight.StringValue(err.Error()) } - runnerRet := rt.NewTable() - runnerRet.Set(rt.StringValue("input"), rt.StringValue(input)) - runnerRet.Set(rt.StringValue("exitCode"), rt.IntValue(int64(exitCode))) - runnerRet.Set(rt.StringValue("err"), luaErr) + runnerRet := moonlight.NewTable() + runnerRet.SetField("input", moonlight.StringValue(input)) + runnerRet.SetField("exitCode", moonlight.IntValue(int(exitCode))) + runnerRet.SetField("err", luaErr) - return c.PushingNext(t.Runtime, rt.TableValue(runnerRet)), nil + + return mlr.PushNext1(c, moonlight.TableValue(runnerRet)), nil }