refactor: rewrite runner module for moonlight, add hilbish.runnerMode function

midnight-edition
sammyette 2024-07-20 14:49:43 -04:00
parent 0c904321f4
commit 69fcd8e348
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
4 changed files with 51 additions and 37 deletions

20
api.go
View File

@ -46,7 +46,9 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value {
"cwd": {hlcwd, 0, false}, "cwd": {hlcwd, 0, false},
/* /*
"exec": {hlexec, 1, false}, "exec": {hlexec, 1, false},
*/
"runnerMode": {hlrunnerMode, 1, false}, "runnerMode": {hlrunnerMode, 1, false},
/*
"goro": {hlgoro, 1, true}, "goro": {hlgoro, 1, true},
"highlighter": {hlhighlighter, 1, false}, "highlighter": {hlhighlighter, 1, false},
"hinter": {hlhinter, 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)) //mod.Set(rt.StringValue("completions"), rt.TableValue(hshcomp))
// hilbish.runner table // hilbish.runner table
//runnerModule := runnerModeLoader(rtm) runnerModule := runnerModeLoader(mlr)
//mod.Set(rt.StringValue("runner"), rt.TableValue(runnerModule)) hshMod.SetField("runner", moonlight.TableValue(runnerModule))
// hilbish.jobs table // hilbish.jobs table
jobs = newJobHandler() jobs = newJobHandler()
@ -749,6 +751,7 @@ 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. // 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. // will call it to execute user input instead.
// Read [about runner mode](../features/runner-mode) for more information. // Read [about runner mode](../features/runner-mode) for more information.
// #param mode string|function // #param mode string|function
func hlrunnerMode(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func hlrunnerMode(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
if err := c.Check1Arg(); err != nil { if err := mlr.Check1Arg(c); err != nil {
return nil, err return nil, err
} }
mode := c.Arg(0) mode := mlr.Arg(c, 0)
switch mode.Type() { switch moonlight.Type(mode) {
case rt.StringType: case moonlight.StringType:
switch mode.AsString() { switch mode.AsString() {
case "hybrid", "hybridRev", "lua", "sh": runnerMode = mode 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()) 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()) default: return nil, errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.TypeName())
} }
return c.Next(), nil return c.Next(), nil
} }
*/
// hinter(line, pos) // hinter(line, pos)
// The command line hint handler. It gets called on every key insert to // The command line hint handler. It gets called on every key insert to

View File

@ -22,6 +22,10 @@ func (mlr *Runtime) ClosureArg(c *GoCont, num int) (*Closure, error) {
return c.cont.ClosureArg(num) 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 { func (mlr *Runtime) GoFunction(fun GoToLuaFunc) rt.GoFunctionFunc {
return func(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { return func(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
gocont := GoCont{ gocont := GoCont{

View File

@ -5,6 +5,11 @@ import (
) )
type Value = rt.Value type Value = rt.Value
type ValueType = rt.ValueType
const (
StringType = rt.StringType
FunctionType = rt.FunctionType
)
func StringValue(str string) Value { func StringValue(str string) Value {
return rt.StringValue(str) return rt.StringValue(str)
@ -21,3 +26,7 @@ func BoolValue(b bool) Value {
func TableValue(t *Table) Value { func TableValue(t *Table) Value {
return rt.TableValue(t.lt) return rt.TableValue(t.lt)
} }
func Type(v Value) ValueType {
return ValueType(v.Type())
}

View File

@ -1,7 +1,7 @@
package main package main
import ( import (
"hilbish/util" "hilbish/moonlight"
rt "github.com/arnodel/golua/runtime" rt "github.com/arnodel/golua/runtime"
) )
@ -49,17 +49,15 @@ hilbish.runnerMode(function(input)
end) end)
``` ```
*/ */
func runnerModeLoader(rtm *rt.Runtime) *rt.Table { func runnerModeLoader(rtm *moonlight.Runtime) *moonlight.Table {
exports := map[string]util.LuaExport{ exports := map[string]moonlight.Export{
/*
"sh": {shRunner, 1, false}, "sh": {shRunner, 1, false},
"lua": {luaRunner, 1, false}, "lua": {luaRunner, 1, false},
"setMode": {hlrunnerMode, 1, false}, "setMode": {hlrunnerMode, 1, false},
*/
} }
mod := rt.NewTable() mod := moonlight.NewTable()
util.SetExports(rtm, mod, exports) rtm.SetExports(mod, exports)
return mod return mod
} }
@ -78,27 +76,27 @@ func _runnerMode() {}
// Runs a command in Hilbish's shell script interpreter. // Runs a command in Hilbish's shell script interpreter.
// This is the equivalent of using `source`. // This is the equivalent of using `source`.
// #param cmd string // #param cmd string
func shRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func shRunner(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
if err := c.Check1Arg(); err != nil { if err := mlr.Check1Arg(c); err != nil {
return nil, err return nil, err
} }
cmd, err := c.StringArg(0) cmd, err := mlr.StringArg(c, 0)
if err != nil { if err != nil {
return nil, err return nil, err
} }
_, exitCode, cont, err := execSh(aliases.Resolve(cmd)) _, exitCode, cont, err := execSh(aliases.Resolve(cmd))
var luaErr rt.Value = rt.NilValue var luaErr moonlight.Value = rt.NilValue
if err != nil { if err != nil {
luaErr = rt.StringValue(err.Error()) luaErr = moonlight.StringValue(err.Error())
} }
runnerRet := rt.NewTable() runnerRet := moonlight.NewTable()
runnerRet.Set(rt.StringValue("input"), rt.StringValue(cmd)) runnerRet.SetField("input", moonlight.StringValue(cmd))
runnerRet.Set(rt.StringValue("exitCode"), rt.IntValue(int64(exitCode))) runnerRet.SetField("exitCode", moonlight.IntValue(int(exitCode)))
runnerRet.Set(rt.StringValue("continue"), rt.BoolValue(cont)) runnerRet.SetField("continue", moonlight.BoolValue(cont))
runnerRet.Set(rt.StringValue("err"), luaErr) runnerRet.SetField("err", luaErr)
return c.PushingNext(t.Runtime, rt.TableValue(runnerRet)), nil return mlr.PushNext1(c, moonlight.TableValue(runnerRet)), nil
} }
// #interface runner // #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` // Evaluates `cmd` as Lua input. This is the same as using `dofile`
// or `load`, but is appropriated for the runner interface. // or `load`, but is appropriated for the runner interface.
// #param cmd string // #param cmd string
func luaRunner(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func luaRunner(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
if err := c.Check1Arg(); err != nil { if err := mlr.Check1Arg(c); err != nil {
return nil, err return nil, err
} }
cmd, err := c.StringArg(0) cmd, err := mlr.StringArg(c, 0)
if err != nil { if err != nil {
return nil, err return nil, err
} }
input, exitCode, err := handleLua(cmd) input, exitCode, err := handleLua(cmd)
var luaErr rt.Value = rt.NilValue var luaErr moonlight.Value = rt.NilValue
if err != nil { if err != nil {
luaErr = rt.StringValue(err.Error()) luaErr = moonlight.StringValue(err.Error())
} }
runnerRet := rt.NewTable() runnerRet := moonlight.NewTable()
runnerRet.Set(rt.StringValue("input"), rt.StringValue(input)) runnerRet.SetField("input", moonlight.StringValue(input))
runnerRet.Set(rt.StringValue("exitCode"), rt.IntValue(int64(exitCode))) runnerRet.SetField("exitCode", moonlight.IntValue(int(exitCode)))
runnerRet.Set(rt.StringValue("err"), luaErr) runnerRet.SetField("err", luaErr)
return c.PushingNext(t.Runtime, rt.TableValue(runnerRet)), nil
return mlr.PushNext1(c, moonlight.TableValue(runnerRet)), nil
} }