2022-03-20 19:15:44 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-04-04 10:40:02 +00:00
|
|
|
"hilbish/util"
|
|
|
|
|
|
|
|
rt "github.com/arnodel/golua/runtime"
|
2022-03-20 19:15:44 +00:00
|
|
|
)
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
func runnerModeLoader(rtm *rt.Runtime) *rt.Table {
|
|
|
|
exports := map[string]util.LuaExport{
|
|
|
|
"sh": {shRunner, 1, false},
|
|
|
|
"lua": {luaRunner, 1, false},
|
|
|
|
"setMode": {hlrunnerMode, 1, false},
|
2022-03-20 19:15:44 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
mod := rt.NewTable()
|
|
|
|
util.SetExports(rtm, mod, exports)
|
2022-03-20 19:15:44 +00:00
|
|
|
|
|
|
|
return mod
|
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
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)
|
2022-03-20 19:15:44 +00:00
|
|
|
if err != nil {
|
2022-04-04 10:40:02 +00:00
|
|
|
return nil, err
|
2022-03-20 19:15:44 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
exitCode, err := handleSh(cmd)
|
|
|
|
var luaErr rt.Value = rt.NilValue
|
|
|
|
if err != nil {
|
|
|
|
luaErr = rt.StringValue(err.Error())
|
|
|
|
}
|
2022-03-20 19:15:44 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.PushingNext(t.Runtime, rt.IntValue(int64(exitCode)), luaErr), nil
|
2022-03-20 19:15:44 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
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)
|
2022-03-20 19:15:44 +00:00
|
|
|
if err != nil {
|
2022-04-04 10:40:02 +00:00
|
|
|
return nil, err
|
2022-03-20 19:15:44 +00:00
|
|
|
}
|
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
exitCode, err := handleLua(cmd)
|
|
|
|
var luaErr rt.Value = rt.NilValue
|
|
|
|
if err != nil {
|
|
|
|
luaErr = rt.StringValue(err.Error())
|
|
|
|
}
|
2022-03-20 19:15:44 +00:00
|
|
|
|
2022-04-04 10:40:02 +00:00
|
|
|
return c.PushingNext(t.Runtime, rt.IntValue(int64(exitCode)), luaErr), nil
|
2022-03-20 19:15:44 +00:00
|
|
|
}
|