From 3bea73460a4a8d15a8461b7163ce86e865daff26 Mon Sep 17 00:00:00 2001 From: TorchedSammy <38820196+TorchedSammy@users.noreply.github.com> Date: Tue, 29 Mar 2022 20:16:03 -0400 Subject: [PATCH] feat: implement runner mode --- aliases.go | 3 ++- api.go | 35 ++++++++++++++++++------------ exec.go | 28 ++++++++++-------------- runnermode.go | 59 +++++++++++++++++++++++++++++---------------------- 4 files changed, 68 insertions(+), 57 deletions(-) diff --git a/aliases.go b/aliases.go index b8e54f1..3c3aa2a 100644 --- a/aliases.go +++ b/aliases.go @@ -66,13 +66,14 @@ func (a *aliasHandler) Resolve(cmdstr string) string { // lua section -func (a *aliasHandler) Loader(rtm *rt.Runtime) *rt.Table { +func (a *aliasHandler) Loader(rtm *rt.Runtime) *rt.Table { // create a lua module with our functions hshaliasesLua := map[string]util.LuaExport{ "add": util.LuaExport{a.luaAdd, 2, false}, "list": util.LuaExport{a.luaList, 0, false}, "del": util.LuaExport{a.luaDelete, 1, false}, } + mod := rt.NewTable() util.SetExports(rtm, mod, hshaliasesLua) diff --git a/api.go b/api.go index f3cd70d..2899d90 100644 --- a/api.go +++ b/api.go @@ -31,7 +31,9 @@ var exports = map[string]util.LuaExport{ "cwd": util.LuaExport{hlcwd, 0, false}, /* "exec": hlexec, - "runnerMode": hlrunnerMode, +*/ + "runnerMode": util.LuaExport{hlrunnerMode, 1, false}, +/* "goro": hlgoro, "highlighter": hlhighlighter, "hinter": hlhinter, @@ -129,11 +131,11 @@ Check out the {blue}{bold}guide{reset} command to get started. util.Document(L, hshcomp, "Completions interface for Hilbish.") L.SetField(mod, "completion", hshcomp) - // hilbish.runner table - runnerModule := runnerModeLoader(L) - util.Document(L, runnerModule, "Runner/exec interface for Hilbish.") - L.SetField(mod, "runner", runnerModule) */ + // hilbish.runner table + runnerModule := runnerModeLoader(rtm) + //util.Document(L, runnerModule, "Runner/exec interface for Hilbish.") + mod.Set(rt.StringValue("runner"), rt.TableValue(runnerModule)) // hilbish.jobs table jobs = newJobHandler() @@ -545,7 +547,6 @@ 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. This determines whether // Hilbish wll try to run input as Lua and/or sh or only do one of either. @@ -553,26 +554,32 @@ func hlinputMode(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { // sh, and lua. It also accepts a function, to which if it is passed one // will call it to execute user input instead. // --- @param mode string|function -func hlrunnerMode(L *lua.LState) int { - mode := L.CheckAny(1) +func hlrunnerMode(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { + if err := c.Check1Arg(); err != nil { + return nil, err + } + mode := c.Arg(0) + switch mode.Type() { - case lua.LTString: - switch mode.String() { + case rt.StringType: + switch mode.AsString() { // no fallthrough doesnt work so eh case "hybrid": fallthrough case "hybridRev": fallthrough case "lua": fallthrough case "sh": runnerMode = mode - default: L.RaiseError("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received %v", mode) + default: return nil, errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.AsString()) } - case lua.LTFunction: runnerMode = mode - default: L.RaiseError("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received %v", mode) + case rt.FunctionType: runnerMode = mode + default: return nil, errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.TypeName()) } - return 0 + return c.Next(), nil } +/* + // hinter(cb) // Sets the hinter function. This will be called on every key insert to determine // what text to use as an inline hint. The callback is passed 2 arguments: diff --git a/exec.go b/exec.go index 87feeaa..2c5ba67 100644 --- a/exec.go +++ b/exec.go @@ -24,15 +24,15 @@ import ( ) var errNotExec = errors.New("not executable") -//var runnerMode lua.LValue = lua.LString("hybrid") +var runnerMode rt.Value = rt.StringValue("hybrid") func runInput(input string, priv bool) { running = true cmdString := aliases.Resolve(input) hooks.Em.Emit("command.preexec", input, cmdString) -// if runnerMode.Type() == lua.LTString { - switch /*runnerMode.String()*/ "hybrid" { + if runnerMode.Type() == rt.StringType { + switch runnerMode.AsString() { case "hybrid": _, err := handleLua(cmdString) if err == nil { @@ -68,35 +68,29 @@ func runInput(input string, priv bool) { } cmdFinish(exitCode, cmdString, priv) } -// } else { + } else { // can only be a string or function so - /* - err := l.CallByParam(lua.P{ - Fn: runnerMode, - NRet: 2, - Protect: true, - }, lua.LString(cmdString)) + term := rt.NewTerminationWith(l.MainThread().CurrentCont(), 2, false) + err := rt.Call(l.MainThread(), runnerMode, []rt.Value{rt.StringValue(cmdString)}, term) if err != nil { fmt.Fprintln(os.Stderr, err) cmdFinish(124, cmdString, priv) return } - luaexitcode := l.Get(-2) // first return value (makes sense right i love stacks) - runErr := l.Get(-1) - l.Pop(2) + luaexitcode := term.Get(0) // first return value (makes sense right i love stacks) + runErr := term.Get(1) var exitCode uint8 - if code, ok := luaexitcode.(lua.LNumber); luaexitcode != lua.LNil && ok { + if code, ok := luaexitcode.TryInt(); ok { exitCode = uint8(code) } - if runErr != lua.LNil { + if runErr != rt.NilValue { fmt.Fprintln(os.Stderr, runErr) } cmdFinish(exitCode, cmdString, priv) - */ -// } + } } func handleLua(cmdString string) (uint8, error) { diff --git a/runnermode.go b/runnermode.go index 14049a0..7b682b4 100644 --- a/runnermode.go +++ b/runnermode.go @@ -1,47 +1,56 @@ package main -/* import ( + "hilbish/util" + + rt "github.com/arnodel/golua/runtime" ) -func runnerModeLoader(L *lua.LState) *lua.LTable { - exports := map[string]lua.LGFunction{ - "sh": shRunner, - "lua": luaRunner, - "setMode": hlrunnerMode, +func runnerModeLoader(rtm *rt.Runtime) *rt.Table { + exports := map[string]util.LuaExport{ + "sh": {shRunner, 1, false}, + "lua": {luaRunner, 1, false}, + "setMode": {hlrunnerMode, 1, false}, } - mod := L.SetFuncs(L.NewTable(), exports) - L.SetField(mod, "mode", runnerMode) + mod := rt.NewTable() + util.SetExports(rtm, mod, exports) return mod } -func shRunner(L *lua.LState) int { - cmd := L.CheckString(1) +func shRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { + if err := c.Check1Arg(); err != nil { + return nil, err + } + cmd, err := c.StringArg(0) + if err != nil { + return nil, err + } + exitCode, err := handleSh(cmd) - var luaErr lua.LValue = lua.LNil + var luaErr rt.Value = rt.NilValue if err != nil { - luaErr = lua.LString(err.Error()) + luaErr = rt.StringValue(err.Error()) } - L.Push(lua.LNumber(exitCode)) - L.Push(luaErr) - - return 2 + return c.PushingNext(t.Runtime, rt.IntValue(int64(exitCode)), luaErr), nil } -func luaRunner(L *lua.LState) int { - cmd := L.CheckString(1) +func luaRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { + if err := c.Check1Arg(); err != nil { + return nil, err + } + cmd, err := c.StringArg(0) + if err != nil { + return nil, err + } + exitCode, err := handleLua(cmd) - var luaErr lua.LValue = lua.LNil + var luaErr rt.Value = rt.NilValue if err != nil { - luaErr = lua.LString(err.Error()) + luaErr = rt.StringValue(err.Error()) } - L.Push(lua.LNumber(exitCode)) - L.Push(luaErr) - - return 2 + return c.PushingNext(t.Runtime, rt.IntValue(int64(exitCode)), luaErr), nil } -*/