feat: implement runner mode

lua5.4
TorchedSammy 2022-03-29 20:16:03 -04:00
parent 5a3b28142c
commit 3bea73460a
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
4 changed files with 68 additions and 57 deletions

View File

@ -66,13 +66,14 @@ func (a *aliasHandler) Resolve(cmdstr string) string {
// lua section // 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 // create a lua module with our functions
hshaliasesLua := map[string]util.LuaExport{ hshaliasesLua := map[string]util.LuaExport{
"add": util.LuaExport{a.luaAdd, 2, false}, "add": util.LuaExport{a.luaAdd, 2, false},
"list": util.LuaExport{a.luaList, 0, false}, "list": util.LuaExport{a.luaList, 0, false},
"del": util.LuaExport{a.luaDelete, 1, false}, "del": util.LuaExport{a.luaDelete, 1, false},
} }
mod := rt.NewTable() mod := rt.NewTable()
util.SetExports(rtm, mod, hshaliasesLua) util.SetExports(rtm, mod, hshaliasesLua)

35
api.go
View File

@ -31,7 +31,9 @@ var exports = map[string]util.LuaExport{
"cwd": util.LuaExport{hlcwd, 0, false}, "cwd": util.LuaExport{hlcwd, 0, false},
/* /*
"exec": hlexec, "exec": hlexec,
"runnerMode": hlrunnerMode, */
"runnerMode": util.LuaExport{hlrunnerMode, 1, false},
/*
"goro": hlgoro, "goro": hlgoro,
"highlighter": hlhighlighter, "highlighter": hlhighlighter,
"hinter": hlhinter, "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.") util.Document(L, hshcomp, "Completions interface for Hilbish.")
L.SetField(mod, "completion", hshcomp) 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 // hilbish.jobs table
jobs = newJobHandler() jobs = newJobHandler()
@ -545,7 +547,6 @@ func hlinputMode(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
return c.Next(), nil return c.Next(), nil
} }
/*
// runnerMode(mode) // runnerMode(mode)
// Sets the execution/runner mode for interactive Hilbish. This determines whether // 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. // 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 // sh, and lua. It also accepts a function, to which if it is passed one
// will call it to execute user input instead. // will call it to execute user input instead.
// --- @param mode string|function // --- @param mode string|function
func hlrunnerMode(L *lua.LState) int { func hlrunnerMode(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
mode := L.CheckAny(1) if err := c.Check1Arg(); err != nil {
return nil, err
}
mode := c.Arg(0)
switch mode.Type() { switch mode.Type() {
case lua.LTString: case rt.StringType:
switch mode.String() { switch mode.AsString() {
// no fallthrough doesnt work so eh // no fallthrough doesnt work so eh
case "hybrid": fallthrough case "hybrid": fallthrough
case "hybridRev": fallthrough case "hybridRev": fallthrough
case "lua": fallthrough case "lua": fallthrough
case "sh": case "sh":
runnerMode = mode 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 case rt.FunctionType: 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.TypeName())
} }
return 0 return c.Next(), nil
} }
/*
// hinter(cb) // hinter(cb)
// Sets the hinter function. This will be called on every key insert to determine // 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: // what text to use as an inline hint. The callback is passed 2 arguments:

28
exec.go
View File

@ -24,15 +24,15 @@ import (
) )
var errNotExec = errors.New("not executable") 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) { func runInput(input string, priv bool) {
running = true running = true
cmdString := aliases.Resolve(input) cmdString := aliases.Resolve(input)
hooks.Em.Emit("command.preexec", input, cmdString) hooks.Em.Emit("command.preexec", input, cmdString)
// if runnerMode.Type() == lua.LTString { if runnerMode.Type() == rt.StringType {
switch /*runnerMode.String()*/ "hybrid" { switch runnerMode.AsString() {
case "hybrid": case "hybrid":
_, err := handleLua(cmdString) _, err := handleLua(cmdString)
if err == nil { if err == nil {
@ -68,35 +68,29 @@ func runInput(input string, priv bool) {
} }
cmdFinish(exitCode, cmdString, priv) cmdFinish(exitCode, cmdString, priv)
} }
// } else { } else {
// can only be a string or function so // can only be a string or function so
/* term := rt.NewTerminationWith(l.MainThread().CurrentCont(), 2, false)
err := l.CallByParam(lua.P{ err := rt.Call(l.MainThread(), runnerMode, []rt.Value{rt.StringValue(cmdString)}, term)
Fn: runnerMode,
NRet: 2,
Protect: true,
}, lua.LString(cmdString))
if err != nil { if err != nil {
fmt.Fprintln(os.Stderr, err) fmt.Fprintln(os.Stderr, err)
cmdFinish(124, cmdString, priv) cmdFinish(124, cmdString, priv)
return return
} }
luaexitcode := l.Get(-2) // first return value (makes sense right i love stacks) luaexitcode := term.Get(0) // first return value (makes sense right i love stacks)
runErr := l.Get(-1) runErr := term.Get(1)
l.Pop(2)
var exitCode uint8 var exitCode uint8
if code, ok := luaexitcode.(lua.LNumber); luaexitcode != lua.LNil && ok { if code, ok := luaexitcode.TryInt(); ok {
exitCode = uint8(code) exitCode = uint8(code)
} }
if runErr != lua.LNil { if runErr != rt.NilValue {
fmt.Fprintln(os.Stderr, runErr) fmt.Fprintln(os.Stderr, runErr)
} }
cmdFinish(exitCode, cmdString, priv) cmdFinish(exitCode, cmdString, priv)
*/ }
// }
} }
func handleLua(cmdString string) (uint8, error) { func handleLua(cmdString string) (uint8, error) {

View File

@ -1,47 +1,56 @@
package main package main
/*
import ( import (
"hilbish/util"
rt "github.com/arnodel/golua/runtime"
) )
func runnerModeLoader(L *lua.LState) *lua.LTable { func runnerModeLoader(rtm *rt.Runtime) *rt.Table {
exports := map[string]lua.LGFunction{ exports := map[string]util.LuaExport{
"sh": shRunner, "sh": {shRunner, 1, false},
"lua": luaRunner, "lua": {luaRunner, 1, false},
"setMode": hlrunnerMode, "setMode": {hlrunnerMode, 1, false},
} }
mod := L.SetFuncs(L.NewTable(), exports) mod := rt.NewTable()
L.SetField(mod, "mode", runnerMode) util.SetExports(rtm, mod, exports)
return mod return mod
} }
func shRunner(L *lua.LState) int { func shRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
cmd := L.CheckString(1) if err := c.Check1Arg(); err != nil {
return nil, err
}
cmd, err := c.StringArg(0)
if err != nil {
return nil, err
}
exitCode, err := handleSh(cmd) exitCode, err := handleSh(cmd)
var luaErr lua.LValue = lua.LNil var luaErr rt.Value = rt.NilValue
if err != nil { if err != nil {
luaErr = lua.LString(err.Error()) luaErr = rt.StringValue(err.Error())
} }
L.Push(lua.LNumber(exitCode)) return c.PushingNext(t.Runtime, rt.IntValue(int64(exitCode)), luaErr), nil
L.Push(luaErr)
return 2
} }
func luaRunner(L *lua.LState) int { func luaRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
cmd := L.CheckString(1) if err := c.Check1Arg(); err != nil {
return nil, err
}
cmd, err := c.StringArg(0)
if err != nil {
return nil, err
}
exitCode, err := handleLua(cmd) exitCode, err := handleLua(cmd)
var luaErr lua.LValue = lua.LNil var luaErr rt.Value = rt.NilValue
if err != nil { if err != nil {
luaErr = lua.LString(err.Error()) luaErr = rt.StringValue(err.Error())
} }
L.Push(lua.LNumber(exitCode)) return c.PushingNext(t.Runtime, rt.IntValue(int64(exitCode)), luaErr), nil
L.Push(luaErr)
return 2
} }
*/