2
2
mirror of https://github.com/Hilbis/Hilbish synced 2025-07-03 09:44:17 +00:00

Compare commits

...

5 Commits

Author SHA1 Message Date
754bb6c9c2
fix: add TableArg for golua 2025-06-14 14:01:41 -04:00
6d793f35eb
feat: add missing functions, os.setenv
clua now starts with no errors.
2025-06-14 13:59:25 -04:00
19938fa8ef
feat: add hilbish.runner and hilbish.appendPath 2025-06-14 13:40:28 -04:00
3e85e1bf68
feat: add hilbish.completion (enough to init nature) 2025-06-14 13:26:41 -04:00
417ccf7ca8
feat: add hilbish.cwd 2025-06-14 12:31:25 -04:00
9 changed files with 135 additions and 91 deletions

36
api.go
View File

@ -44,9 +44,7 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value {
var exports = map[string]moonlight.Export{ var exports = map[string]moonlight.Export{
/* /*
"alias": {hlalias, 2, false}, "alias": {hlalias, 2, false},
"appendPath": {hlappendPath, 1, false},
"complete": {hlcomplete, 2, false}, "complete": {hlcomplete, 2, 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},
@ -55,6 +53,8 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value {
"multiprompt": {hlmultiprompt, 1, false}, "multiprompt": {hlmultiprompt, 1, false},
"prependPath": {hlprependPath, 1, false}, "prependPath": {hlprependPath, 1, false},
*/ */
"appendPath": {hlappendPath, 1, false},
"cwd": {hlcwd, 0, false},
"prompt": {hlprompt, 1, true}, "prompt": {hlprompt, 1, true},
/* /*
"inputMode": {hlinputMode, 1, false}, "inputMode": {hlinputMode, 1, false},
@ -106,14 +106,14 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value {
//mod.Set(rt.StringValue("history"), rt.TableValue(historyModule)) //mod.Set(rt.StringValue("history"), rt.TableValue(historyModule))
// hilbish.completion table // hilbish.completion table
//hshcomp := completionLoader(rtm) hshcomp := completionLoader(mlr)
// TODO: REMOVE "completion" AND ONLY USE "completions" WITH AN S // TODO: REMOVE "completion" AND ONLY USE "completions" WITH AN S
//mod.Set(rt.StringValue("completion"), rt.TableValue(hshcomp)) hshMod.SetField("completion", moonlight.TableValue(hshcomp))
//mod.Set(rt.StringValue("completions"), rt.TableValue(hshcomp)) hshMod.SetField("completions", moonlight.TableValue(hshcomp))
// hilbish.runner table // hilbish.runner table
//runnerModule := runnerModeLoader(mlr) runnerModule := runnerModeLoader(mlr)
//hshMod.SetField("runner", moonlight.TableValue(runnerModule)) hshMod.SetField("runner", moonlight.TableValue(runnerModule))
// hilbish.jobs table // hilbish.jobs table
jobs = newJobHandler() jobs = newJobHandler()
@ -475,11 +475,11 @@ hilbish.appendPath {
} }
#example #example
*/ */
func hlappendPath(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { func hlappendPath(mlr *moonlight.Runtime) error {
if err := mlr.Check1Arg(); err != nil { if err := mlr.Check1Arg(); err != nil {
return nil, err return err
} }
arg := mlr.Arg(c, 0) arg := mlr.Arg(0)
// check if dir is a table or a string // check if dir is a table or a string
if moonlight.Type(arg) == moonlight.TableType { if moonlight.Type(arg) == moonlight.TableType {
@ -491,10 +491,10 @@ func hlappendPath(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont,
} else if moonlight.Type(arg) == moonlight.StringType { } else if moonlight.Type(arg) == moonlight.StringType {
appendPath(arg.AsString()) appendPath(arg.AsString())
} else { } else {
return nil, errors.New("bad argument to appendPath (expected string or table, got " + arg.TypeName() + ")") return errors.New("bad argument to appendPath (expected string or table, got " + arg.TypeName() + ")")
} }
return c.Next(), nil return nil
} }
func appendPath(dir string) { func appendPath(dir string) {
@ -770,11 +770,11 @@ 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(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { func hlrunnerMode(mlr *moonlight.Runtime) error {
if err := mlr.Check1Arg(); err != nil { if err := mlr.Check1Arg(); err != nil {
return nil, err return err
} }
mode := mlr.Arg(c, 0) mode := mlr.Arg(0)
switch moonlight.Type(mode) { switch moonlight.Type(mode) {
case moonlight.StringType: case moonlight.StringType:
@ -782,15 +782,15 @@ func hlrunnerMode(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont,
case "hybrid", "hybridRev", "lua", "sh": case "hybrid", "hybridRev", "lua", "sh":
runnerMode = mode runnerMode = mode
default: default:
return nil, errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.AsString()) return errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.AsString())
} }
case moonlight.FunctionType: case moonlight.FunctionType:
runnerMode = mode runnerMode = mode
default: default:
return nil, errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.TypeName()) return errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.TypeName())
} }
return c.Next(), nil return nil
} }
// hinter(line, pos) // hinter(line, pos)

View File

@ -2,13 +2,12 @@ package main
import ( import (
//"errors" //"errors"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
"os"
"hilbish/moonlight"
"hilbish/util" "hilbish/util"
//rt "github.com/arnodel/golua/runtime"
) )
var charEscapeMap = []string{ var charEscapeMap = []string{
@ -191,21 +190,19 @@ func escapeFilename(fname string) string {
// #interface completion // #interface completion
// tab completions // tab completions
// The completions interface deals with tab completions. // The completions interface deals with tab completions.
/* func completionLoader(mlr *moonlight.Runtime) *moonlight.Table {
func completionLoader(rtm *rt.Runtime) *rt.Table { exports := map[string]moonlight.Export{
exports := map[string]util.LuaExport{
"bins": {hcmpBins, 3, false}, "bins": {hcmpBins, 3, false},
"call": {hcmpCall, 4, false}, //"call": {hcmpCall, 4, false},
"files": {hcmpFiles, 3, false}, // "files": {hcmpFiles, 3, false},
"handler": {hcmpHandler, 2, false}, // "handler": {hcmpHandler, 2, false},
} }
mod := rt.NewTable() mod := moonlight.NewTable()
util.SetExports(rtm, mod, exports) mlr.SetExports(mod, exports)
return mod return mod
} }
*/
// #interface completion // #interface completion
// bins(query, ctx, fields) -> entries (table), prefix (string) // bins(query, ctx, fields) -> entries (table), prefix (string)
@ -233,23 +230,23 @@ hilbish.complete('command.sudo', function(query, ctx, fields)
end) end)
#example #example
*/ */
/* func hcmpBins(mlr *moonlight.Runtime) error {
func hcmpBins(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { query, ctx, fds, err := getCompleteParams(mlr)
query, ctx, fds, err := getCompleteParams(t, c)
if err != nil { if err != nil {
return nil, err return err
} }
completions, pfx := binaryComplete(query, ctx, fds) completions, _ := binaryComplete(query, ctx, fds)
luaComps := rt.NewTable() luaComps := moonlight.NewTable()
for i, comp := range completions { for i, comp := range completions {
luaComps.Set(rt.IntValue(int64(i + 1)), rt.StringValue(comp)) luaComps.Set(moonlight.IntValue(int64(i+1)), moonlight.StringValue(comp))
} }
return c.PushingNext(t.Runtime, rt.TableValue(luaComps), rt.StringValue(pfx)), nil mlr.PushNext1(moonlight.TableValue(luaComps))
//return c.PushingNext(t.Runtime, rt.TableValue(luaComps), rt.StringValue(pfx)), nil
return nil
} }
*/
// #interface completion // #interface completion
// call(name, query, ctx, fields) -> completionGroups (table), prefix (string) // call(name, query, ctx, fields) -> completionGroups (table), prefix (string)
@ -352,35 +349,33 @@ function hilbish.completion.handler(line, pos)
end end
#example #example
*/ */
/* func hcmpHandler(mlr *moonlight.Runtime) error {
func hcmpHandler(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { return nil
return c.Next(), nil
} }
func getCompleteParams(t *rt.Thread, c *rt.GoCont) (string, string, []string, error) { func getCompleteParams(mlr *moonlight.Runtime) (string, string, []string, error) {
if err := c.CheckNArgs(3); err != nil { if err := mlr.CheckNArgs(3); err != nil {
return "", "", []string{}, err return "", "", []string{}, err
} }
query, err := c.StringArg(0) query, err := mlr.StringArg(0)
if err != nil { if err != nil {
return "", "", []string{}, err return "", "", []string{}, err
} }
ctx, err := c.StringArg(1) ctx, err := mlr.StringArg(1)
if err != nil { if err != nil {
return "", "", []string{}, err return "", "", []string{}, err
} }
fields, err := c.TableArg(2) fields, err := mlr.TableArg(2)
if err != nil { if err != nil {
return "", "", []string{}, err return "", "", []string{}, err
} }
var fds []string var fds []string
util.ForEach(fields, func(k rt.Value, v rt.Value) { moonlight.ForEach(fields, func(k moonlight.Value, v moonlight.Value) {
if v.Type() == rt.StringType { if v.Type() == moonlight.StringType {
fds = append(fds, v.AsString()) fds = append(fds, v.AsString())
} }
}) })
return query, ctx, fds, err return query, ctx, fds, err
} }
*/

View File

@ -213,8 +213,8 @@ func (b *Bait) callRecoverer(event string, handler *Listener, err interface{}) {
func (b *Bait) Loader(rtm *moonlight.Runtime) moonlight.Value { func (b *Bait) Loader(rtm *moonlight.Runtime) moonlight.Value {
exports := map[string]moonlight.Export{ exports := map[string]moonlight.Export{
/*
"catch": {b.bcatch, 2, false}, "catch": {b.bcatch, 2, false},
/*
"catchOnce": util.LuaExport{b.bcatchOnce, 2, false}, "catchOnce": util.LuaExport{b.bcatchOnce, 2, false},
"throw": util.LuaExport{b.bthrow, 1, true}, "throw": util.LuaExport{b.bthrow, 1, true},
"release": util.LuaExport{b.brelease, 2, false}, "release": util.LuaExport{b.brelease, 2, false},
@ -261,15 +261,15 @@ bait.catch('hilbish.exit', function()
end) end)
#example #example
*/ */
func (b *Bait) bcatch(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { func (b *Bait) bcatch(mlr *moonlight.Runtime) error {
name, catcher, err := util.HandleStrCallback(mlr) name, catcher, err := util.HandleStrCallback(mlr)
if err != nil { if err != nil {
return nil, err return err
} }
b.OnLua(name, catcher) b.OnLua(name, catcher)
return c.Next(), nil return nil
} }
/* /*

View File

@ -2,10 +2,6 @@
package moonlight package moonlight
import (
"fmt"
)
type Callable interface { type Callable interface {
Continuation(*Runtime, Cont) Cont Continuation(*Runtime, Cont) Cont
} }
@ -16,8 +12,6 @@ type Closure struct {
} }
func (mlr *Runtime) ClosureArg(num int) (*Closure, error) { func (mlr *Runtime) ClosureArg(num int) (*Closure, error) {
fmt.Println("type at ", num, "is", mlr.state.LTypename(num))
return &Closure{ return &Closure{
refIdx: -1, refIdx: -1,
}, nil }, nil

View File

@ -3,6 +3,7 @@
package moonlight package moonlight
import ( import (
"errors"
"fmt" "fmt"
"github.com/aarzilli/golua/lua" "github.com/aarzilli/golua/lua"
@ -36,8 +37,13 @@ func (mlr *Runtime) StringArg(num int) (string, error) {
return mlr.state.CheckString(num + 1), nil return mlr.state.CheckString(num + 1), nil
} }
func (mlr *Runtime) Arg(c *GoCont, num int) Value { func (mlr *Runtime) TableArg(num int) (*Table, error) {
return c.vals[num] //return mlr.state.CheckType(num+1, lua.LUA_TTABLE)
return nil, errors.New("TableArg unimplemented")
}
func (mlr *Runtime) Arg(num int) Value {
return NilValue
} }
func (mlr *Runtime) GoFunction(fun GoToLuaFunc) *GoFunctionFunc { func (mlr *Runtime) GoFunction(fun GoToLuaFunc) *GoFunctionFunc {

View File

@ -20,11 +20,22 @@ func (mlr *Runtime) StringArg(num int) (string, error) {
return mlr.rt.MainThread().CurrentCont().(*rt.GoCont).StringArg(num) return mlr.rt.MainThread().CurrentCont().(*rt.GoCont).StringArg(num)
} }
func (mlr *Runtime) TableArg(num int) (*Table, error) {
tbl, err := mlr.rt.MainThread().CurrentCont().(*rt.GoCont).TableArg(num)
if err != nil {
return nil, err
}
return &Table{
lt: tbl,
}, nil
}
func (mlr *Runtime) ClosureArg(num int) (*Closure, error) { func (mlr *Runtime) ClosureArg(num int) (*Closure, error) {
return mlr.rt.MainThread().CurrentCont().(*rt.GoCont).ClosureArg(num) return mlr.rt.MainThread().CurrentCont().(*rt.GoCont).ClosureArg(num)
} }
func (mlr *Runtime) Arg(c *GoCont, num int) Value { func (mlr *Runtime) Arg(num int) Value {
return mlr.rt.MainThread().CurrentCont().(*rt.GoCont).Arg(num) return mlr.rt.MainThread().CurrentCont().(*rt.GoCont).Arg(num)
} }

View File

@ -4,6 +4,7 @@ package moonlight
import ( import (
"fmt" "fmt"
"os"
"github.com/aarzilli/golua/lua" "github.com/aarzilli/golua/lua"
) )
@ -17,9 +18,39 @@ func NewRuntime() *Runtime {
L := lua.NewState() L := lua.NewState()
L.OpenLibs() L.OpenLibs()
return &Runtime{ mlr := &Runtime{
state: L, state: L,
} }
mlr.Extras()
return mlr
}
func (mlr *Runtime) Extras() {
mlr.state.GetGlobal("os")
mlr.pushToState(FunctionValue(mlr.GoFunction(setenv)))
mlr.state.SetField(-2, "setenv")
}
func setenv(mlr *Runtime) error {
if err := mlr.CheckNArgs(2); err != nil {
return err
}
env, err := mlr.StringArg(0)
if err != nil {
return err
}
varr, err := mlr.StringArg(1)
if err != nil {
return err
}
os.Setenv(env, varr)
return nil
} }
func (mlr *Runtime) PushNext1(v Value) { func (mlr *Runtime) PushNext1(v Value) {

View File

@ -1,7 +1,7 @@
-- Prelude initializes everything else for our shell -- Prelude initializes everything else for our shell
local _ = require 'succulent' -- Function additions local _ = require 'succulent' -- Function additions
local bait = require 'bait' local bait = require 'bait'
--local fs = require 'fs' local fs = require 'fs'
package.path = package.path .. ';' .. hilbish.dataDir .. '/?/init.lua' package.path = package.path .. ';' .. hilbish.dataDir .. '/?/init.lua'
.. ';' .. hilbish.dataDir .. '/?/?.lua' .. ";" .. hilbish.dataDir .. '/?.lua' .. ';' .. hilbish.dataDir .. '/?/?.lua' .. ";" .. hilbish.dataDir .. '/?.lua'
@ -18,6 +18,8 @@ if not hilbish.midnightEdition then
-- it didnt work normally, idk -- it didnt work normally, idk
return function() return hilbish.module.load(path) end, path return function() return hilbish.module.load(path) end, path
end) end)
else
pcall = unsafe_pcall
end end
require 'nature.commands' require 'nature.commands'
@ -64,6 +66,7 @@ do
}) })
end end
--[[
do do
local startSearchPath = hilbish.userDir.data .. '/hilbish/start/?/init.lua;' local startSearchPath = hilbish.userDir.data .. '/hilbish/start/?/init.lua;'
.. hilbish.userDir.data .. '/hilbish/start/?.lua' .. hilbish.userDir.data .. '/hilbish/start/?.lua'
@ -80,6 +83,7 @@ do
package.path = package.path .. ';' .. startSearchPath package.path = package.path .. ';' .. startSearchPath
end end
]]--
bait.catch('error', function(event, handler, err) bait.catch('error', function(event, handler, err)
print(string.format('Encountered an error in %s handler\n%s', event, err:sub(8))) print(string.format('Encountered an error in %s handler\n%s', event, err:sub(8)))

View File

@ -1,5 +1,7 @@
package main package main
import "hilbish/moonlight"
// #interface runner // #interface runner
// interactive command runner customization // interactive command runner customization
/* The runner interface contains functions that allow the user to change /* The runner interface contains functions that allow the user to change
@ -42,6 +44,7 @@ hilbish.runnerMode(function(input)
return hilbish.runner.sh(input) return hilbish.runner.sh(input)
end) end)
``` ```
*/
func runnerModeLoader(rtm *moonlight.Runtime) *moonlight.Table { func runnerModeLoader(rtm *moonlight.Runtime) *moonlight.Table {
exports := map[string]moonlight.Export{ exports := map[string]moonlight.Export{
"sh": {shRunner, 1, false}, "sh": {shRunner, 1, false},
@ -69,13 +72,13 @@ 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(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { func shRunner(mlr *moonlight.Runtime) error {
if err := mlr.Check1Arg(c); err != nil { if err := mlr.Check1Arg(); err != nil {
return nil, err return err
} }
cmd, err := mlr.StringArg(c, 0) cmd, err := mlr.StringArg(0)
if err != nil { if err != nil {
return nil, err return err
} }
_, exitCode, cont, err := execSh(aliases.Resolve(cmd)) _, exitCode, cont, err := execSh(aliases.Resolve(cmd))
@ -89,7 +92,8 @@ func shRunner(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, erro
runnerRet.SetField("continue", moonlight.BoolValue(cont)) runnerRet.SetField("continue", moonlight.BoolValue(cont))
runnerRet.SetField("err", luaErr) runnerRet.SetField("err", luaErr)
return mlr.PushNext1(c, moonlight.TableValue(runnerRet)), nil mlr.PushNext1(moonlight.TableValue(runnerRet))
return nil
} }
// #interface runner // #interface runner
@ -97,13 +101,13 @@ func shRunner(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, erro
// 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(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { func luaRunner(mlr *moonlight.Runtime) error {
if err := mlr.Check1Arg(c); err != nil { if err := mlr.Check1Arg(); err != nil {
return nil, err return err
} }
cmd, err := mlr.StringArg(c, 0) cmd, err := mlr.StringArg(0)
if err != nil { if err != nil {
return nil, err return err
} }
input, exitCode, err := handleLua(cmd) input, exitCode, err := handleLua(cmd)
@ -116,7 +120,6 @@ func luaRunner(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, err
runnerRet.SetField("exitCode", moonlight.IntValue(int64(exitCode))) runnerRet.SetField("exitCode", moonlight.IntValue(int64(exitCode)))
runnerRet.SetField("err", luaErr) runnerRet.SetField("err", luaErr)
mlr.PushNext1(moonlight.TableValue(runnerRet))
return mlr.PushNext1(c, moonlight.TableValue(runnerRet)), nil return nil
} }
*/