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

@ -73,6 +73,7 @@ func (a *aliasHandler) Loader(rtm *rt.Runtime) *rt.Table {
"list": util.LuaExport{a.luaList, 0, false},
"del": util.LuaExport{a.luaDelete, 1, false},
}
mod := rt.NewTable()
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},
/*
"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:

28
exec.go
View File

@ -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) {

View File

@ -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
}
*/