mirror of
https://github.com/Hilbis/Hilbish
synced 2025-07-01 08:42:04 +00:00
refactor: change go function types
this is the only way i could think of to be able to push go functions to lua on the clua side. this may or may not need adjustments on golua side though...
This commit is contained in:
parent
95391d74f6
commit
9e77f0ba32
50
api.go
50
api.go
@ -15,13 +15,16 @@ package main
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
|
||||
//"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
//"os/exec"
|
||||
"runtime"
|
||||
"strings"
|
||||
//"syscall"
|
||||
|
||||
//"syscall
|
||||
//"time"
|
||||
|
||||
//"hilbish/util"
|
||||
@ -134,8 +137,8 @@ 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)
|
||||
@ -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.
|
||||
@ -352,14 +354,14 @@ 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
|
||||
@ -378,11 +380,13 @@ func hlprompt(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, erro
|
||||
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 "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)
|
||||
@ -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)
|
||||
@ -766,7 +770,7 @@ 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)
|
||||
@ -774,11 +778,15 @@ func hlrunnerMode(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont,
|
||||
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 "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.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
|
||||
|
26
go.mod
26
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
|
||||
|
16
go.sum
16
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=
|
||||
|
@ -35,6 +35,7 @@ import (
|
||||
)
|
||||
|
||||
type listenerType int
|
||||
|
||||
const (
|
||||
goListener listenerType = iota
|
||||
luaListener
|
||||
@ -87,8 +88,10 @@ 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)
|
||||
}
|
||||
@ -195,7 +198,6 @@ 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]
|
||||
|
||||
@ -211,8 +213,8 @@ 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},
|
||||
/*
|
||||
"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},
|
||||
@ -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)
|
||||
}
|
||||
|
@ -33,9 +33,9 @@ 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 {
|
||||
@ -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},
|
||||
*/
|
||||
}
|
||||
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:
|
||||
|
@ -9,16 +9,16 @@ 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"
|
||||
)
|
||||
|
||||
@ -39,14 +39,10 @@ func (f *fs) Loader(rtm *moonlight.Runtime) moonlight.Value {
|
||||
"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`.
|
||||
@ -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
|
||||
}
|
||||
@ -335,4 +335,3 @@ func (f *fs) fstat(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||
|
||||
return c.PushingNext1(t.Runtime, rt.TableValue(statTbl)), nil
|
||||
}
|
||||
|
||||
|
@ -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},
|
||||
*/
|
||||
}
|
||||
|
||||
mod := moonlight.NewTable()
|
||||
|
@ -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()
|
||||
@ -60,6 +58,7 @@ func moduleLoader(mlr *moonlight.Runtime) *moonlight.Table {
|
||||
// Loads a module at the designated `path`.
|
||||
// It will throw if any error occurs.
|
||||
// #param path string
|
||||
/*
|
||||
func moduleLoad(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
|
||||
if err := mlr.Check1Arg(c); 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
|
||||
}
|
||||
*/
|
||||
|
@ -1,4 +1,5 @@
|
||||
//go:build midnight
|
||||
|
||||
package moonlight
|
||||
|
||||
import (
|
||||
@ -14,7 +15,7 @@ type Closure struct{
|
||||
// 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{
|
||||
|
@ -1,4 +1,5 @@
|
||||
//go:build midnight
|
||||
|
||||
package moonlight
|
||||
|
||||
func (mlr *Runtime) SetExports(tbl *Table, exports map[string]Export) {
|
||||
|
@ -1,3 +1,3 @@
|
||||
package moonlight
|
||||
|
||||
type GoToLuaFunc func(mlr *Runtime, c *GoCont) (Cont, error)
|
||||
type GoToLuaFunc func(mlr *Runtime) error
|
||||
|
@ -1,4 +1,5 @@
|
||||
//go:build midnight
|
||||
|
||||
package moonlight
|
||||
|
||||
import (
|
||||
@ -18,7 +19,7 @@ func (gf GoFunctionFunc) Continuation(mlr *Runtime, c Cont) Cont {
|
||||
}
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
||||
}*/
|
||||
|
||||
return len(cont.(*GoCont).vals)
|
||||
//return len(cont.(*GoCont).vals)
|
||||
return 0
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
//go:build midnight
|
||||
|
||||
package moonlight
|
||||
|
||||
import (
|
||||
@ -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 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 FunctionType:
|
||||
mlr.state.PushGoClosure(v.AsLuaFunction())
|
||||
default:
|
||||
mlr.state.PushNil()
|
||||
}
|
||||
}
|
||||
|
@ -69,6 +69,7 @@ func (t *Table) syncToLua() {
|
||||
t.SetField(k.AsString(), v)
|
||||
}
|
||||
}
|
||||
|
||||
func ForEach(tbl *Table, cb func(key Value, val Value)) {
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,9 @@
|
||||
//go:build midnight
|
||||
|
||||
package moonlight
|
||||
|
||||
import "github.com/aarzilli/golua/lua"
|
||||
|
||||
type Value struct {
|
||||
iface interface{}
|
||||
relIdx int
|
||||
@ -10,6 +13,7 @@ type Value struct{
|
||||
var NilValue = Value{nil, -1, -1}
|
||||
|
||||
type ValueType uint8
|
||||
|
||||
const (
|
||||
NilType ValueType = iota
|
||||
BoolType
|
||||
@ -50,11 +54,16 @@ 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
|
||||
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 "<unknown type>"
|
||||
case bool:
|
||||
return "bool"
|
||||
case int64:
|
||||
return "number"
|
||||
case string:
|
||||
return "string"
|
||||
case *Table:
|
||||
return "table"
|
||||
case *Closure:
|
||||
return "function"
|
||||
default:
|
||||
return "<unknown type>"
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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'
|
||||
|
@ -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
|
||||
@ -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
|
||||
}
|
||||
*/
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user