diff --git a/api.go b/api.go index 6dc4bebf..45335be5 100644 --- a/api.go +++ b/api.go @@ -15,13 +15,16 @@ package main import ( "bytes" "errors" + //"fmt" "io" "os" + //"os/exec" "runtime" "strings" - //"syscall" + + //"syscall //"time" //"hilbish/util" @@ -40,26 +43,26 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value { println("hilbish loader called") var exports = map[string]moonlight.Export{ /* - "alias": {hlalias, 2, false}, - "appendPath": {hlappendPath, 1, false}, - "complete": {hlcomplete, 2, false}, - "cwd": {hlcwd, 0, false}, - "exec": {hlexec, 1, false}, - "runnerMode": {hlrunnerMode, 1, false}, - "goro": {hlgoro, 1, true}, - "highlighter": {hlhighlighter, 1, false}, - "hinter": {hlhinter, 1, false}, - "multiprompt": {hlmultiprompt, 1, false}, - "prependPath": {hlprependPath, 1, false}, + "alias": {hlalias, 2, false}, + "appendPath": {hlappendPath, 1, false}, + "complete": {hlcomplete, 2, false}, + "cwd": {hlcwd, 0, false}, + "exec": {hlexec, 1, false}, + "runnerMode": {hlrunnerMode, 1, false}, + "goro": {hlgoro, 1, true}, + "highlighter": {hlhighlighter, 1, false}, + "hinter": {hlhinter, 1, false}, + "multiprompt": {hlmultiprompt, 1, false}, + "prependPath": {hlprependPath, 1, false}, */ "prompt": {hlprompt, 1, true}, /* - "inputMode": {hlinputMode, 1, false}, - "interval": {hlinterval, 2, false}, - "read": {hlread, 1, false}, - "run": {hlrun, 1, true}, - "timeout": {hltimeout, 2, false}, - "which": {hlwhich, 1, false}, + "inputMode": {hlinputMode, 1, false}, + "interval": {hlinterval, 2, false}, + "read": {hlread, 1, false}, + "run": {hlrun, 1, true}, + "timeout": {hltimeout, 2, false}, + "which": {hlwhich, 1, false}, */ } hshMod = moonlight.NewTable() @@ -134,19 +137,19 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value { // very meta if moonlight.IsMidnight() { - moduleModule := moduleLoader(mlr) - hshMod.SetField("module", moonlight.TableValue(moduleModule)) + //moduleModule := moduleLoader(mlr) + //hshMod.SetField("module", moonlight.TableValue(moduleModule)) } return moonlight.TableValue(hshMod) } func getenv(key, fallback string) string { - value := os.Getenv(key) - if len(value) == 0 { - return fallback - } - return value + value := os.Getenv(key) + if len(value) == 0 { + return fallback + } + return value } func setVimMode(mode string) { @@ -302,7 +305,6 @@ func hlcwd(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) return mlr.PushNext1(c, moonlight.StringValue(cwd)), nil } - // read(prompt) -> input (string) // Read input from the user, using Hilbish's line editor/input reader. // This is a separate instance from the one Hilbish actually uses. @@ -320,7 +322,7 @@ func hlread(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { // substitute with an empty string prompt = "" } - + lualr := &lineReader{ rl: readline.NewInstance(), } @@ -352,37 +354,39 @@ hilbish.prompt '%u@%h :%d $' -- prompt: user@hostname: ~/directory $ #example */ -func hlprompt(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { - err := mlr.Check1Arg(c) +func hlprompt(mlr *moonlight.Runtime) error { + err := mlr.Check1Arg() if err != nil { - return nil, err + return err } - p, err := mlr.StringArg(c, 0) + p, err := mlr.StringArg(0) if err != nil { - return nil, err + return err } typ := "left" // optional 2nd arg /* - if len(c.Etc()) != 0 { - ltyp := c.Etc()[0] - var ok bool - typ, ok = ltyp.TryString() - if !ok { - return nil, errors.New("bad argument to run (expected string, got " + ltyp.TypeName() + ")") + if len(c.Etc()) != 0 { + ltyp := c.Etc()[0] + var ok bool + typ, ok = ltyp.TryString() + if !ok { + return nil, errors.New("bad argument to run (expected string, got " + ltyp.TypeName() + ")") + } } - } */ switch typ { - case "left": - prompt = p - lr.SetPrompt(fmtPrompt(prompt)) - case "right": lr.SetRightPrompt(fmtPrompt(p)) - default: return nil, errors.New("expected prompt type to be right or left, got " + typ) + case "left": + prompt = p + lr.SetPrompt(fmtPrompt(prompt)) + case "right": + lr.SetRightPrompt(fmtPrompt(p)) + default: + return errors.New("expected prompt type to be right or left, got " + typ) } - return c.Next(), nil + return nil } // multiprompt(str) @@ -400,7 +404,7 @@ will look like: user ~ ∆ echo "hey --> ...!" -so then you get +so then you get user ~ ∆ echo "hey --> ...!" hey ...! @@ -437,15 +441,15 @@ hilbish.alias('dircount', 'ls %1 | wc -l') */ //func hlalias(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { func hlalias(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { - if err := mlr.CheckNArgs(c, 2); err != nil { + if err := mlr.CheckNArgs(2); err != nil { return nil, err } - cmd, err := mlr.StringArg(c, 0) + cmd, err := mlr.StringArg(0) if err != nil { return nil, err } - orig, err := mlr.StringArg(c, 1) + orig, err := mlr.StringArg(1) if err != nil { return nil, err } @@ -471,7 +475,7 @@ hilbish.appendPath { #example */ func hlappendPath(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { - if err := mlr.Check1Arg(c); err != nil { + if err := mlr.Check1Arg(); err != nil { return nil, err } arg := mlr.Arg(c, 0) @@ -498,7 +502,7 @@ func appendPath(dir string) { // if dir isnt already in $PATH, add it if !strings.Contains(pathenv, dir) { - os.Setenv("PATH", pathenv + string(os.PathListSeparator) + dir) + os.Setenv("PATH", pathenv+string(os.PathListSeparator)+dir) } } @@ -595,7 +599,7 @@ func hltimeout(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { interval := time.Duration(ms) * time.Millisecond timer := timers.create(timerTimeout, interval, cb) timer.start() - + return c.PushingNext1(t.Runtime, rt.UserDataValue(timer.ud)), nil } @@ -766,19 +770,23 @@ func hlinputMode(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { // Read [about runner mode](../features/runner-mode) for more information. // #param mode string|function func hlrunnerMode(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { - if err := mlr.Check1Arg(c); err != nil { + if err := mlr.Check1Arg(); err != nil { return nil, err } mode := mlr.Arg(c, 0) 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 moonlight.FunctionType: runnerMode = mode - default: return nil, errors.New("execMode: expected either a function or hybrid, hybridRev, lua, sh. Received " + mode.TypeName()) + 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 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 diff --git a/go.mod b/go.mod index 19cbc608..81ff8d17 100644 --- a/go.mod +++ b/go.mod @@ -1,20 +1,20 @@ module hilbish -go 1.21 +go 1.23.0 -toolchain go1.22.2 +toolchain go1.24.3 require ( - github.com/aarzilli/golua v0.0.0-20210507130708-11106aa57765 - github.com/arnodel/golua v0.0.0-20230215163904-e0b5347eaaa1 - github.com/atsushinee/go-markdown-generator v0.0.0-20191121114853-83f9e1f68504 - github.com/blackfireio/osinfo v1.0.5 - github.com/maxlandon/readline v1.0.14 + github.com/aarzilli/golua v0.0.0-20250217091409-248753f411c4 + github.com/arnodel/golua v0.1.0 + github.com/atsushinee/go-markdown-generator v0.0.0-20231027094725-92d26ffbe778 + github.com/blackfireio/osinfo v1.1.0 + github.com/maxlandon/readline v1.1.3 github.com/pborman/getopt v1.1.0 github.com/sahilm/fuzzy v0.1.1 - golang.org/x/sys v0.22.0 - golang.org/x/term v0.22.0 - mvdan.cc/sh/v3 v3.8.0 + golang.org/x/sys v0.33.0 + golang.org/x/term v0.32.0 + mvdan.cc/sh/v3 v3.11.0 ) require ( @@ -25,8 +25,8 @@ require ( github.com/muesli/cancelreader v0.2.2 // indirect github.com/olekukonko/ts v0.0.0-20171002115256-78ecb04241c0 // indirect github.com/rivo/uniseg v0.4.7 // indirect - golang.org/x/sync v0.7.0 // indirect - golang.org/x/text v0.14.0 // indirect + golang.org/x/sync v0.15.0 // indirect + golang.org/x/text v0.26.0 // indirect ) replace mvdan.cc/sh/v3 => github.com/Rosettea/sh/v3 v3.4.0-0.dev.0.20240720131751-805c301321fd @@ -35,4 +35,4 @@ replace github.com/maxlandon/readline => ./readline replace layeh.com/gopher-luar => github.com/layeh/gopher-luar v1.0.10 -replace github.com/arnodel/golua => github.com/Rosettea/golua v0.0.0-20240427174124-d239074c1749 +replace github.com/arnodel/golua => github.com/Rosettea/golua v0.0.0-20241104031959-5551ea280f23 diff --git a/go.sum b/go.sum index 53f9b332..0ed45646 100644 --- a/go.sum +++ b/go.sum @@ -1,17 +1,25 @@ github.com/Rosettea/golua v0.0.0-20240427174124-d239074c1749 h1:jIFnWBTsYw8s7RX7H2AOXjDVhWP3ol7OzUVaPN2KnGI= github.com/Rosettea/golua v0.0.0-20240427174124-d239074c1749/go.mod h1:9jzpYPiU2is0HVGCiuIOBSXdergHUW44IEjmuN1UrIE= +github.com/Rosettea/golua v0.0.0-20241104031959-5551ea280f23 h1:mUZnT0gmDEmTkqXsbnDbuJ3CNil7DCOMiCQYgjbKIdI= +github.com/Rosettea/golua v0.0.0-20241104031959-5551ea280f23/go.mod h1:9jzpYPiU2is0HVGCiuIOBSXdergHUW44IEjmuN1UrIE= github.com/Rosettea/sh/v3 v3.4.0-0.dev.0.20240720131751-805c301321fd h1:THNle0FR2g7DMO1y3Bx1Zr7rYeiLXt3st3UkxEsMzL4= github.com/Rosettea/sh/v3 v3.4.0-0.dev.0.20240720131751-805c301321fd/go.mod h1:YZalN5H7WNQw3DGij6IvHsEhn5YMW7M2FCwG6gnfKy4= github.com/aarzilli/golua v0.0.0-20210507130708-11106aa57765 h1:N6gB4UCRBZz8twlJbMFiCKj0zX5Et2nFU/LRafT4x80= github.com/aarzilli/golua v0.0.0-20210507130708-11106aa57765/go.mod h1:hMjfaJVSqVnxenMlsxrq3Ni+vrm9Hs64tU4M7dhUoO4= +github.com/aarzilli/golua v0.0.0-20250217091409-248753f411c4 h1:gW5i3FQAMcbkNgo/A87gCKAbBMalAO8BlPIMo9Gk2Ow= +github.com/aarzilli/golua v0.0.0-20250217091409-248753f411c4/go.mod h1:hMjfaJVSqVnxenMlsxrq3Ni+vrm9Hs64tU4M7dhUoO4= github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8= github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo= github.com/arnodel/strftime v0.1.6 h1:0hc0pUvk8KhEMXE+htyaOUV42zNcf/csIbjzEFCJqsw= github.com/arnodel/strftime v0.1.6/go.mod h1:5NbK5XqYK8QpRZpqKNt4OlxLtIB8cotkLk4KTKzJfWs= github.com/atsushinee/go-markdown-generator v0.0.0-20191121114853-83f9e1f68504 h1:R1/AOzdMbopSliUTTEHvHbyNmnZ3YxY5GvdhTkpPsSY= github.com/atsushinee/go-markdown-generator v0.0.0-20191121114853-83f9e1f68504/go.mod h1:kHBCvAXJIatTX1pw6tLiOspjGc3MhUDRlog9yrCUS+k= +github.com/atsushinee/go-markdown-generator v0.0.0-20231027094725-92d26ffbe778 h1:iBzH7EQLFyjkpwXihHWf7QbbzfYfxAlyP4pTjCJbnMw= +github.com/atsushinee/go-markdown-generator v0.0.0-20231027094725-92d26ffbe778/go.mod h1:kHBCvAXJIatTX1pw6tLiOspjGc3MhUDRlog9yrCUS+k= github.com/blackfireio/osinfo v1.0.5 h1:6hlaWzfcpb87gRmznVf7wSdhysGqLRz9V/xuSdCEXrA= github.com/blackfireio/osinfo v1.0.5/go.mod h1:Pd987poVNmd5Wsx6PRPw4+w7kLlf9iJxoRKPtPAjOrA= +github.com/blackfireio/osinfo v1.1.0 h1:1LMkMiFL42+Brx7r3MKuf7UTlXBRgebFLJQAfoFafj8= +github.com/blackfireio/osinfo v1.1.0/go.mod h1:Pd987poVNmd5Wsx6PRPw4+w7kLlf9iJxoRKPtPAjOrA= github.com/creack/pty v1.1.21 h1:1/QdRyBaHHJP61QkWMXlOIBfsgdDeeKfK8SYVUWJKf0= github.com/creack/pty v1.1.21/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr4O4= github.com/evilsocket/islazy v1.11.0 h1:B5w6uuS6ki6iDG+aH/RFeoMb8ijQh/pGabewqp2UeJ0= @@ -40,9 +48,17 @@ github.com/sahilm/fuzzy v0.1.1 h1:ceu5RHF8DGgoi+/dR5PsECjCDH1BE3Fnmpo7aVXOdRA= github.com/sahilm/fuzzy v0.1.1/go.mod h1:VFvziUEIMCrT6A6tw2RFIXPXXmzXbOsSHF0DOI8ZK9Y= golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.15.0 h1:KWH3jNZsfyT6xfAfKiz6MRNmd46ByHDYaZ7KSkCtdW8= +golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA= golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI= golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw= +golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k= golang.org/x/term v0.22.0 h1:BbsgPEJULsl2fV/AT3v15Mjva5yXKQDyKf+TbDz7QJk= golang.org/x/term v0.22.0/go.mod h1:F3qCibpT5AMpCRfhfT53vVJwhLtIVHhB9XDjfFvnMI4= +golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg= +golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ= golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.26.0 h1:P42AVeLghgTYr4+xUnTRKDMqpar+PtX7KWuNQL21L8M= +golang.org/x/text v0.26.0/go.mod h1:QK15LZJUUQVJxhz7wXgxSy/CJaTFjd0G+YLonydOVQA= diff --git a/golibs/bait/bait.go b/golibs/bait/bait.go index b0e7bb27..d6869c6f 100644 --- a/golibs/bait/bait.go +++ b/golibs/bait/bait.go @@ -35,6 +35,7 @@ import ( ) type listenerType int + const ( goListener listenerType = iota luaListener @@ -44,24 +45,24 @@ const ( type Recoverer func(event string, handler *Listener, err interface{}) // Listener is a struct that holds the handler for an event. -type Listener struct{ - typ listenerType - once bool - caller func(...interface{}) +type Listener struct { + typ listenerType + once bool + caller func(...interface{}) luaCaller *moonlight.Closure } -type Bait struct{ +type Bait struct { recoverer Recoverer - handlers map[string][]*Listener - rtm *moonlight.Runtime + handlers map[string][]*Listener + rtm *moonlight.Runtime } // New creates a new Bait instance. func New(rtm *moonlight.Runtime) *Bait { b := &Bait{ handlers: make(map[string][]*Listener), - rtm: rtm, + rtm: rtm, } return b @@ -87,22 +88,24 @@ func (b *Bait) Emit(event string, args ...interface{}) { for _, arg := range args { var luarg moonlight.Value switch arg.(type) { - case moonlight.Value: luarg = arg.(moonlight.Value) - default: luarg = moonlight.AsValue(arg) + case moonlight.Value: + luarg = arg.(moonlight.Value) + default: + luarg = moonlight.AsValue(arg) } luaArgs = append(luaArgs, luarg) } /* - _, err := b.rtm.Call1(funcVal, luaArgs...) - if err != nil { - if event != "error" { - b.Emit("error", event, handle.luaCaller, err.Error()) - return + _, err := b.rtm.Call1(funcVal, luaArgs...) + if err != nil { + if event != "error" { + b.Emit("error", event, handle.luaCaller, err.Error()) + return + } + // if there is an error in an error event handler, panic instead + // (calls the go recoverer function) + panic(err) } - // if there is an error in an error event handler, panic instead - // (calls the go recoverer function) - panic(err) - } */ } else { handle.caller(args...) @@ -117,7 +120,7 @@ func (b *Bait) Emit(event string, args ...interface{}) { // On adds a Go function handler for an event. func (b *Bait) On(event string, handler func(...interface{})) *Listener { listener := &Listener{ - typ: goListener, + typ: goListener, caller: handler, } @@ -128,7 +131,7 @@ func (b *Bait) On(event string, handler func(...interface{})) *Listener { // OnLua adds a Lua function handler for an event. func (b *Bait) OnLua(event string, handler *moonlight.Closure) *Listener { listener := &Listener{ - typ: luaListener, + typ: luaListener, luaCaller: handler, } b.addListener(event, listener) @@ -161,8 +164,8 @@ func (b *Bait) OffLua(event string, handler *moonlight.Closure) { // Once adds a Go function listener for an event that only runs once. func (b *Bait) Once(event string, handler func(...interface{})) *Listener { listener := &Listener{ - typ: goListener, - once: true, + typ: goListener, + once: true, caller: handler, } b.addListener(event, listener) @@ -173,8 +176,8 @@ func (b *Bait) Once(event string, handler func(...interface{})) *Listener { // OnceLua adds a Lua function listener for an event that only runs once. func (b *Bait) OnceLua(event string, handler *moonlight.Closure) *Listener { listener := &Listener{ - typ: luaListener, - once: true, + typ: luaListener, + once: true, luaCaller: handler, } b.addListener(event, listener) @@ -195,11 +198,10 @@ func (b *Bait) addListener(event string, listener *Listener) { b.handlers[event] = append(b.handlers[event], listener) } - func (b *Bait) removeListener(event string, idx int) { - b.handlers[event][idx] = b.handlers[event][len(b.handlers[event]) - 1] + b.handlers[event][idx] = b.handlers[event][len(b.handlers[event])-1] - b.handlers[event] = b.handlers[event][:len(b.handlers[event]) - 1] + b.handlers[event] = b.handlers[event][:len(b.handlers[event])-1] } func (b *Bait) callRecoverer(event string, handler *Listener, err interface{}) { @@ -211,12 +213,12 @@ func (b *Bait) callRecoverer(event string, handler *Listener, err interface{}) { func (b *Bait) Loader(rtm *moonlight.Runtime) moonlight.Value { exports := map[string]moonlight.Export{ - "catch": {b.bcatch, 2, false}, /* - "catchOnce": util.LuaExport{b.bcatchOnce, 2, false}, - "throw": util.LuaExport{b.bthrow, 1, true}, - "release": util.LuaExport{b.brelease, 2, false}, - "hooks": util.LuaExport{b.bhooks, 1, false}, + "catch": {b.bcatch, 2, false}, + "catchOnce": util.LuaExport{b.bcatchOnce, 2, false}, + "throw": util.LuaExport{b.bthrow, 1, true}, + "release": util.LuaExport{b.brelease, 2, false}, + "hooks": util.LuaExport{b.bhooks, 1, false}, */ } mod := moonlight.NewTable() @@ -231,8 +233,10 @@ func handleHook(t *rt.Thread, c *rt.GoCont, name string, catcher *rt.Closure, ar for _, arg := range args { var luarg rt.Value switch arg.(type) { - case rt.Value: luarg = arg.(rt.Value) - default: luarg = rt.AsValue(arg) + case rt.Value: + luarg = arg.(rt.Value) + default: + luarg = rt.AsValue(arg) } luaArgs = append(luaArgs, luarg) } diff --git a/golibs/commander/commander.go b/golibs/commander/commander.go index 486db424..520396ac 100644 --- a/golibs/commander/commander.go +++ b/golibs/commander/commander.go @@ -33,19 +33,19 @@ This sink is for writing errors, as the name would suggest. package commander import ( + "hilbish/golibs/bait" "hilbish/moonlight" "hilbish/util" - "hilbish/golibs/bait" ) -type Commander struct{ - Events *bait.Bait +type Commander struct { + Events *bait.Bait Commands map[string]*moonlight.Closure } func New(rtm *moonlight.Runtime) *Commander { c := &Commander{ - Events: bait.New(rtm), + Events: bait.New(rtm), Commands: make(map[string]*moonlight.Closure), } @@ -54,9 +54,11 @@ func New(rtm *moonlight.Runtime) *Commander { func (c *Commander) Loader(rtm *moonlight.Runtime) moonlight.Value { exports := map[string]moonlight.Export{ - "register": {c.cregister, 2, false}, - "deregister": {c.cderegister, 1, false}, - "registry": {c.cregistry, 0, false}, + /* + "register": {c.cregister, 2, false}, + "deregister": {c.cderegister, 1, false}, + "registry": {c.cregistry, 0, false}, + */ } mod := moonlight.NewTable() rtm.SetExports(mod, exports) @@ -95,19 +97,21 @@ func (c *Commander) cregister(mlr *moonlight.Runtime, ct *moonlight.GoCont) (moo // deregister(name) // Removes the named command. Note that this will only remove Commander-registered commands. // #param name string Name of the command to remove. -func (c *Commander) cderegister(mlr *moonlight.Runtime, ct *moonlight.GoCont) (moonlight.Cont, error) { - if err := mlr.Check1Arg(ct); err != nil { - return nil, err +/* +func (c *Commander) cderegister(mlr *moonlight.Runtime) error { + if err := mlr.Check1Arg(); err != nil { + return err } - cmdName, err := mlr.StringArg(ct, 0) + cmdName, err := mlr.StringArg(0) if err != nil { - return nil, err + return err } delete(c.Commands, cmdName) - return ct.Next(), err + return err } +*/ // registry() -> table // Returns all registered commanders. Returns a list of tables with the following keys: diff --git a/golibs/fs/fs.go b/golibs/fs/fs.go index b8945118..b5b8ab6e 100644 --- a/golibs/fs/fs.go +++ b/golibs/fs/fs.go @@ -9,20 +9,20 @@ package fs import ( "fmt" + "os" "path/filepath" "strconv" - "os" "strings" "hilbish/moonlight" "hilbish/util" - rt "github.com/arnodel/golua/runtime" "github.com/arnodel/golua/lib/iolib" + rt "github.com/arnodel/golua/runtime" "mvdan.cc/sh/v3/interp" ) -type fs struct{ +type fs struct { runner *interp.Runner } @@ -36,20 +36,16 @@ func (f *fs) Loader(rtm *moonlight.Runtime) moonlight.Value { println("fs loader called") exports := map[string]moonlight.Export{ /* - "cd": util.LuaExport{f.fcd, 1, false}, - "mkdir": util.LuaExport{f.fmkdir, 2, false}, - "stat": util.LuaExport{f.fstat, 1, false}, - */ - "readdir": {f.freaddir, 1, false}, - /* - "abs": util.LuaExport{f.fabs, 1, false}, - "basename": util.LuaExport{f.fbasename, 1, false}, - */ - "dir": {f.fdir, 1, false}, - /* - "glob": util.LuaExport{f.fglob, 1, false}, - "join": util.LuaExport{f.fjoin, 0, true}, - "pipe": util.LuaExport{f.fpipe, 0, false}, + "cd": util.LuaExport{f.fcd, 1, false}, + "mkdir": util.LuaExport{f.fmkdir, 2, false}, + "stat": util.LuaExport{f.fstat, 1, false}, + "readdir": {f.freaddir, 1, false}, + "abs": util.LuaExport{f.fabs, 1, false}, + "basename": util.LuaExport{f.fbasename, 1, false}, + "dir": {f.fdir, 1, false}, + "glob": util.LuaExport{f.fglob, 1, false}, + "join": util.LuaExport{f.fjoin, 0, true}, + "pipe": util.LuaExport{f.fpipe, 0, false}, */ } @@ -126,18 +122,21 @@ func (f *fs) fcd(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { // `~/Documents/doc.txt` then this function will return `~/Documents`. // #param path string Path to get the directory for. // #returns string -func (f *fs) fdir(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { - if err := mlr.Check1Arg(c); err != nil { - return nil, err +/* +func (f *fs) fdir(mlr *moonlight.Runtime, c *moonlight.GoCont) error { + if err := mlr.Check1Arg(); err != nil { + return err } - path, err := mlr.StringArg(c, 0) + path, err := mlr.StringArg(0) if err != nil { - return nil, err + return err } - next := mlr.PushNext1(c, moonlight.StringValue(filepath.Dir(path))) - return next, nil + println(patg) + //next := mlr.PushNext1(c, moonlight.StringValue(filepath.Dir(path))) + return nil } +*/ // glob(pattern) -> matches (table) // Match all files based on the provided `pattern`. @@ -175,9 +174,9 @@ func (f *fs) fglob(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { luaMatches := rt.NewTable() for i, match := range matches { - luaMatches.Set(rt.IntValue(int64(i + 1)), rt.StringValue(match)) + luaMatches.Set(rt.IntValue(int64(i+1)), rt.StringValue(match)) } - + return c.PushingNext(t.Runtime, rt.TableValue(luaMatches)), nil } @@ -197,7 +196,7 @@ func (f *fs) fjoin(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { for i, v := range c.Etc() { if v.Type() != rt.StringType { // +2; go indexes of 0 and first arg from above - return nil, fmt.Errorf("bad argument #%d to run (expected string, got %s)", i + 1, v.TypeName()) + return nil, fmt.Errorf("bad argument #%d to run (expected string, got %s)", i+1, v.TypeName()) } strs[i] = v.AsString() } @@ -261,15 +260,16 @@ func (f *fs) fpipe(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { return c.PushingNext(t.Runtime, rfLua.Value(t.Runtime), wfLua.Value(t.Runtime)), nil } + // readdir(path) -> table[string] // Returns a list of all files and directories in the provided path. // #param dir string // #returns table func (f *fs) freaddir(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { - if err := mlr.Check1Arg(c); err != nil { + if err := mlr.Check1Arg(); err != nil { return nil, err } - dir, err := mlr.StringArg(c, 0) + dir, err := mlr.StringArg(0) if err != nil { return nil, err } @@ -281,7 +281,7 @@ func (f *fs) freaddir(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Co return nil, err } for i, entry := range dirEntries { - names.Set(moonlight.IntValue(int64(i + 1)), moonlight.StringValue(entry.Name())) + names.Set(moonlight.IntValue(int64(i+1)), moonlight.StringValue(entry.Name())) } return mlr.PushNext1(c, moonlight.TableValue(names)), nil @@ -330,9 +330,8 @@ func (f *fs) fstat(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { statTbl := rt.NewTable() statTbl.Set(rt.StringValue("name"), rt.StringValue(pathinfo.Name())) statTbl.Set(rt.StringValue("size"), rt.IntValue(pathinfo.Size())) - statTbl.Set(rt.StringValue("mode"), rt.StringValue("0" + strconv.FormatInt(int64(pathinfo.Mode().Perm()), 8))) + statTbl.Set(rt.StringValue("mode"), rt.StringValue("0"+strconv.FormatInt(int64(pathinfo.Mode().Perm()), 8))) statTbl.Set(rt.StringValue("isDir"), rt.BoolValue(pathinfo.IsDir())) - + return c.PushingNext1(t.Runtime, rt.TableValue(statTbl)), nil } - diff --git a/golibs/terminal/terminal.go b/golibs/terminal/terminal.go index 270d6682..95cafdb5 100644 --- a/golibs/terminal/terminal.go +++ b/golibs/terminal/terminal.go @@ -14,10 +14,12 @@ var termState *term.State func Loader(rtm *moonlight.Runtime) moonlight.Value { exports := map[string]moonlight.Export{ - "setRaw": {termsetRaw, 0, false}, - "restoreState": {termrestoreState, 0, false}, - "size": {termsize, 0, false}, - "saveState": {termsaveState, 0, false}, + /* + "setRaw": {termsetRaw, 0, false}, + "restoreState": {termrestoreState, 0, false}, + "size": {termsize, 0, false}, + "saveState": {termsaveState, 0, false}, + */ } mod := moonlight.NewTable() diff --git a/lua.go b/lua.go index 9e05af1d..64b35385 100644 --- a/lua.go +++ b/lua.go @@ -38,17 +38,17 @@ func luaInit() { hooks.Off(event, handler) }) l.LoadLibrary(hooks.Loader, "bait") -/* - // Add Ctrl-C handler - hooks.On("signal.sigint", func(...interface{}) { - if !interactive { - os.Exit(0) - } - }) + /* + // Add Ctrl-C handler + hooks.On("signal.sigint", func(...interface{}) { + if !interactive { + os.Exit(0) + } + }) - lr.rl.RawInputCallback = func(r []rune) { - hooks.Emit("hilbish.rawInput", string(r)) - } + lr.rl.RawInputCallback = func(r []rune) { + hooks.Emit("hilbish.rawInput", string(r)) + } */ // Add more paths that Lua can require from diff --git a/module.go b/module.go index e0a6efd7..2ebd5144 100644 --- a/module.go +++ b/module.go @@ -1,8 +1,6 @@ package main import ( - "plugin" - "hilbish/moonlight" ) @@ -46,7 +44,7 @@ If you attempt to require and print the result (`print(require 'plugin')`), it w */ func moduleLoader(mlr *moonlight.Runtime) *moonlight.Table { exports := map[string]moonlight.Export{ - "load": {moduleLoad, 2, false}, + //"load": {moduleLoad, 2, false}, } mod := moonlight.NewTable() @@ -59,12 +57,13 @@ func moduleLoader(mlr *moonlight.Runtime) *moonlight.Table { // load(path) // Loads a module at the designated `path`. // It will throw if any error occurs. -// #param path string +// #param path string +/* func moduleLoad(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { if err := mlr.Check1Arg(c); err != nil { return nil, err } - + path, err := mlr.StringArg(c, 0) if err != nil { return nil, err @@ -89,3 +88,4 @@ func moduleLoad(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, er return mlr.PushNext1(c, val), nil } +*/ diff --git a/moonlight/closure_clua.go b/moonlight/closure_clua.go index 549c20a7..6259c273 100644 --- a/moonlight/closure_clua.go +++ b/moonlight/closure_clua.go @@ -1,22 +1,23 @@ //go:build midnight + package moonlight import ( "fmt" ) -type Callable interface{ +type Callable interface { Continuation(*Runtime, Cont) Cont } -type Closure struct{ +type Closure struct { refIdx int // so since we cant store the actual lua closure, // we need a index to the ref in the lua registry... or something like that. } -func (mlr *Runtime) ClosureArg(c *GoCont, num int) (*Closure, error) { +func (mlr *Runtime) ClosureArg(num int) (*Closure, error) { fmt.Println("type at ", num, "is", mlr.state.LTypename(num)) - + return &Closure{ refIdx: -1, }, nil diff --git a/moonlight/export_clua.go b/moonlight/export_clua.go index 88573925..3162590a 100644 --- a/moonlight/export_clua.go +++ b/moonlight/export_clua.go @@ -1,4 +1,5 @@ //go:build midnight + package moonlight func (mlr *Runtime) SetExports(tbl *Table, exports map[string]Export) { diff --git a/moonlight/function.go b/moonlight/function.go index 3ebdff58..d2f2382e 100644 --- a/moonlight/function.go +++ b/moonlight/function.go @@ -1,3 +1,3 @@ package moonlight -type GoToLuaFunc func(mlr *Runtime, c *GoCont) (Cont, error) +type GoToLuaFunc func(mlr *Runtime) error diff --git a/moonlight/function_clua.go b/moonlight/function_clua.go index ee3d4820..5d70f025 100644 --- a/moonlight/function_clua.go +++ b/moonlight/function_clua.go @@ -1,4 +1,5 @@ //go:build midnight + package moonlight import ( @@ -7,18 +8,18 @@ import ( "github.com/aarzilli/golua/lua" ) -type GoFunctionFunc struct{ +type GoFunctionFunc struct { cf lua.LuaGoFunction } func (gf GoFunctionFunc) Continuation(mlr *Runtime, c Cont) Cont { return &GoCont{ - f: gf, + f: gf, vals: []Value{}, } } -func (mlr *Runtime) CheckNArgs(c *GoCont, num int) error { +func (mlr *Runtime) CheckNArgs(num int) error { args := mlr.state.GetTop() if args < num { return fmt.Errorf("%d arguments needed", num) @@ -27,11 +28,11 @@ func (mlr *Runtime) CheckNArgs(c *GoCont, num int) error { return nil } -func (mlr *Runtime) Check1Arg(c *GoCont) error { - return mlr.CheckNArgs(c, 1) +func (mlr *Runtime) Check1Arg() error { + return mlr.CheckNArgs(1) } -func (mlr *Runtime) StringArg(c *GoCont, num int) (string, error) { +func (mlr *Runtime) StringArg(num int) (string, error) { return mlr.state.CheckString(num + 1), nil } @@ -39,23 +40,24 @@ func (mlr *Runtime) Arg(c *GoCont, num int) Value { return c.vals[num] } -func (mlr *Runtime) GoFunction(fun GoToLuaFunc) GoFunctionFunc { - return GoFunctionFunc{ +func (mlr *Runtime) GoFunction(fun GoToLuaFunc) *GoFunctionFunc { + return &GoFunctionFunc{ cf: func(L *lua.State) int { - cont, err := fun(mlr, &GoCont{}) + err := fun(mlr) if err != nil { L.RaiseError(err.Error()) return 0 } - for _, val := range cont.(*GoCont).vals { + /*for _, val := range cont.(*GoCont).vals { switch Type(val) { - case StringType: - L.PushString(val.AsString()) + case StringType: + L.PushString(val.AsString()) } - } + }*/ - return len(cont.(*GoCont).vals) + //return len(cont.(*GoCont).vals) + return 0 }, } } diff --git a/moonlight/function_golua.go b/moonlight/function_golua.go index 9f9b05f9..e000caeb 100644 --- a/moonlight/function_golua.go +++ b/moonlight/function_golua.go @@ -1,4 +1,5 @@ //go:build !midnight + package moonlight import ( @@ -7,32 +8,28 @@ import ( type GoFunctionFunc = rt.GoFunctionFunc -func (mlr *Runtime) CheckNArgs(c *GoCont, num int) error { - return c.cont.CheckNArgs(num) +func (mlr *Runtime) CheckNArgs(num int) error { + return mlr.rt.MainThread().CurrentCont().(*rt.GoCont).CheckNArgs(num) } -func (mlr *Runtime) Check1Arg(c *GoCont) error { - return c.cont.CheckNArgs(1) +func (mlr *Runtime) Check1Arg() error { + return mlr.rt.MainThread().CurrentCont().(*rt.GoCont).Check1Arg() } -func (mlr *Runtime) StringArg(c *GoCont, num int) (string, error) { - return c.cont.StringArg(num) +func (mlr *Runtime) StringArg(num int) (string, error) { + return mlr.rt.MainThread().CurrentCont().(*rt.GoCont).StringArg(num) } -func (mlr *Runtime) ClosureArg(c *GoCont, num int) (*Closure, error) { - return c.cont.ClosureArg(num) +func (mlr *Runtime) ClosureArg(num int) (*Closure, error) { + return mlr.rt.MainThread().CurrentCont().(*rt.GoCont).ClosureArg(num) } func (mlr *Runtime) Arg(c *GoCont, num int) Value { - return c.cont.Arg(num) + return mlr.rt.MainThread().CurrentCont().(*rt.GoCont).Arg(num) } func (mlr *Runtime) GoFunction(fun GoToLuaFunc) GoFunctionFunc { return func(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { - gocont := GoCont{ - cont: c, - thread: t, - } - return fun(mlr, &gocont) + return c.Next(), fun(mlr) } } diff --git a/moonlight/runtime_clua.go b/moonlight/runtime_clua.go index e42e1866..f1fc1c8c 100644 --- a/moonlight/runtime_clua.go +++ b/moonlight/runtime_clua.go @@ -1,11 +1,12 @@ //go:build midnight + package moonlight import ( "github.com/aarzilli/golua/lua" ) -type Runtime struct{ +type Runtime struct { state *lua.State } @@ -43,12 +44,17 @@ func (mlr *Runtime) Call1(f Value, args ...Value) (Value, error) { func (mlr *Runtime) pushToState(v Value) { switch v.Type() { - case NilType: mlr.state.PushNil() - case StringType: mlr.state.PushString(v.AsString()) - case TableType: - tbl := v.AsTable() - tbl.SetRuntime(mlr) - tbl.Push() - default: mlr.state.PushNil() + case NilType: + mlr.state.PushNil() + case StringType: + mlr.state.PushString(v.AsString()) + case TableType: + tbl := v.AsTable() + tbl.SetRuntime(mlr) + tbl.Push() + case FunctionType: + mlr.state.PushGoClosure(v.AsLuaFunction()) + default: + mlr.state.PushNil() } } diff --git a/moonlight/table_clua.go b/moonlight/table_clua.go index 58cbcac1..17d8341e 100644 --- a/moonlight/table_clua.go +++ b/moonlight/table_clua.go @@ -69,6 +69,7 @@ func (t *Table) syncToLua() { t.SetField(k.AsString(), v) } } + func ForEach(tbl *Table, cb func(key Value, val Value)) { } diff --git a/moonlight/value_clua.go b/moonlight/value_clua.go index 52b44fa5..0157e330 100644 --- a/moonlight/value_clua.go +++ b/moonlight/value_clua.go @@ -1,8 +1,11 @@ //go:build midnight + package moonlight -type Value struct{ - iface interface{} +import "github.com/aarzilli/golua/lua" + +type Value struct { + iface interface{} relIdx int refIdx int } @@ -10,6 +13,7 @@ type Value struct{ var NilValue = Value{nil, -1, -1} type ValueType uint8 + const ( NilType ValueType = iota BoolType @@ -34,7 +38,7 @@ func IntValue(i int64) Value { func StringValue(str string) Value { return Value{iface: str} -} +} func TableValue(t *Table) Value { return Value{iface: t} @@ -50,13 +54,18 @@ func AsValue(i interface{}) Value { } switch v := i.(type) { - case bool: return BoolValue(v) - case int64: return IntValue(v) - case string: return StringValue(v) - case *Table: return TableValue(v) - case Value: return v - default: - return Value{iface: i} + case bool: + return BoolValue(v) + case int64: + return IntValue(v) + case string: + return StringValue(v) + case *Table: + return TableValue(v) + case Value: + return v + default: + return Value{iface: i} } } @@ -66,12 +75,18 @@ func (v Value) Type() ValueType { } switch v.iface.(type) { - case bool: return BoolType - case int64: return IntType - case string: return StringType - case *Table: return TableType - case *Closure: return FunctionType - default: return UnknownType + case bool: + return BoolType + case int64: + return IntType + case string: + return StringType + case *Table: + return TableType + case *GoFunctionFunc: + return FunctionType + default: + return UnknownType } } @@ -91,18 +106,28 @@ func (v Value) AsTable() *Table { return v.iface.(*Table) } +func (v Value) AsLuaFunction() lua.LuaGoFunction { + return v.iface.(*GoFunctionFunc).cf +} + func ToString(v Value) string { return v.AsString() } func (v Value) TypeName() string { switch v.iface.(type) { - case bool: return "bool" - case int64: return "number" - case string: return "string" - case *Table: return "table" - case *Closure: return "function" - default: return "" + case bool: + return "bool" + case int64: + return "number" + case string: + return "string" + case *Table: + return "table" + case *Closure: + return "function" + default: + return "" } } diff --git a/nature/init.lua b/nature/init.lua index a90b5404..9bfc4ac8 100644 --- a/nature/init.lua +++ b/nature/init.lua @@ -1,4 +1,8 @@ -- Prelude initializes everything else for our shell +print(require 'hilbish') +print('hilbitch type', type(hilbish)) +print(hilbish.prompt) +print(hilbish.ver) --[[ local _ = require 'succulent' -- Function additions local bait = require 'bait' diff --git a/runnermode.go b/runnermode.go index 269831da..4da015b2 100644 --- a/runnermode.go +++ b/runnermode.go @@ -1,9 +1,5 @@ package main -import ( - "hilbish/moonlight" -) - // #interface runner // interactive command runner customization /* The runner interface contains functions that allow the user to change @@ -25,7 +21,7 @@ to the history. - `err` (string): A string to indicate an interal error for the runner. It can be set to a few special values for Hilbish to throw the right hooks and have a better looking message: -`[command]: not-found` will throw a command.not-found hook based on what `[command]` is. +`[command]: not-found` will throw a command.not-found hook based on what `[command]` is. `[command]: not-executable` will throw a command.not-executable hook. - `continue` (boolean): Whether to prompt the user for more input. @@ -46,7 +42,6 @@ hilbish.runnerMode(function(input) return hilbish.runner.sh(input) end) ``` -*/ func runnerModeLoader(rtm *moonlight.Runtime) *moonlight.Table { exports := map[string]moonlight.Export{ "sh": {shRunner, 1, false}, @@ -124,3 +119,4 @@ func luaRunner(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, err return mlr.PushNext1(c, moonlight.TableValue(runnerRet)), nil } +*/ diff --git a/util/util.go b/util/util.go index 46a6a2de..2e080477 100644 --- a/util/util.go +++ b/util/util.go @@ -1,8 +1,8 @@ package util import ( - "strings" "os/user" + "strings" "hilbish/moonlight" @@ -18,14 +18,14 @@ func SetField(module *rt.Table, field string, value rt.Value) { // HandleStrCallback handles function parameters for Go functions which take // a string and a closure. func HandleStrCallback(mlr *moonlight.Runtime, c *moonlight.GoCont) (string, *moonlight.Closure, error) { - if err := mlr.CheckNArgs(c, 2); err != nil { + if err := mlr.CheckNArgs(2); err != nil { return "", nil, err } - name, err := mlr.StringArg(c, 0) + name, err := mlr.StringArg(0) if err != nil { return "", nil, err } - cb, err := mlr.ClosureArg(c, 1) + cb, err := mlr.ClosureArg(1) if err != nil { return "", nil, err }