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},
/*
"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

View File

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

View File

@ -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())
}

View File

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