From a5f695eb980cfae10b39c21958324978fea8a974 Mon Sep 17 00:00:00 2001 From: sammyette Date: Sat, 20 Jul 2024 11:34:39 -0400 Subject: [PATCH] feat: implement parts of fs --- api.go | 5 ++-- exec.go | 4 +-- golibs/fs/fs.go | 56 +++++++++++++++++++------------------ lua.go | 6 ++-- module.go | 24 ++++++++-------- moonlight/cont_golua.go | 16 +++++++++++ moonlight/function_golua.go | 17 ++++++----- moonlight/runtime_golua.go | 9 ++++++ moonlight/table_golua.go | 4 +++ 9 files changed, 87 insertions(+), 54 deletions(-) create mode 100644 moonlight/cont_golua.go diff --git a/api.go b/api.go index 1aca72f..9428fe4 100644 --- a/api.go +++ b/api.go @@ -127,8 +127,9 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value { //util.SetField(rtm, versionModule, "release", rt.StringValue(releaseName)) //mod.Set(rt.StringValue("version"), rt.TableValue(versionModule)) - //pluginModule := moduleLoader(rtm) - //mod.Set(rt.StringValue("module"), rt.TableValue(pluginModule)) + // very meta + moduleModule := moduleLoader(mlr) + hshMod.SetField("module", moonlight.TableValue(moduleModule)) return moonlight.TableValue(hshMod) } diff --git a/exec.go b/exec.go index 3b1d6ae..412b07a 100644 --- a/exec.go +++ b/exec.go @@ -352,7 +352,7 @@ func execHandle(bg bool) interp.ExecHandlerFunc { sinks.Set(rt.StringValue("out"), rt.UserDataValue(stdout.ud)) sinks.Set(rt.StringValue("err"), rt.UserDataValue(stderr.ud)) - t := rt.NewThread(l) + //t := rt.NewThread(l) sig := make(chan os.Signal) exit := make(chan bool) @@ -368,7 +368,7 @@ func execHandle(bg bool) interp.ExecHandlerFunc { signal.Notify(sig, os.Interrupt) select { case <-sig: - t.KillContext() + //t.KillContext() return } diff --git a/golibs/fs/fs.go b/golibs/fs/fs.go index 13f972d..61ad799 100644 --- a/golibs/fs/fs.go +++ b/golibs/fs/fs.go @@ -14,50 +14,51 @@ import ( "os" "strings" + "hilbish/moonlight" "hilbish/util" rt "github.com/arnodel/golua/runtime" - "github.com/arnodel/golua/lib/packagelib" "github.com/arnodel/golua/lib/iolib" "mvdan.cc/sh/v3/interp" ) type fs struct{ runner *interp.Runner - Loader packagelib.Loader } func New(runner *interp.Runner) *fs { - f := &fs{ + return &fs{ runner: runner, } - f.Loader = packagelib.Loader{ - Load: f.loaderFunc, - Name: "fs", - } - - return f } -func (f *fs) loaderFunc(rtm *rt.Runtime) (rt.Value, func()) { - exports := map[string]util.LuaExport{ +func (f *fs) Loader(rtm *moonlight.Runtime) moonlight.Value { + 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": util.LuaExport{f.freaddir, 1, false}, + */ + "readdir": {f.freaddir, 1, false}, + /* "abs": util.LuaExport{f.fabs, 1, false}, "basename": util.LuaExport{f.fbasename, 1, false}, - "dir": util.LuaExport{f.fdir, 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}, + */ } - mod := rt.NewTable() - util.SetExports(rtm, mod, exports) - mod.Set(rt.StringValue("pathSep"), rt.StringValue(string(os.PathSeparator))) - mod.Set(rt.StringValue("pathListSep"), rt.StringValue(string(os.PathListSeparator))) - return rt.TableValue(mod), nil + mod := moonlight.NewTable() + rtm.SetExports(mod, exports) + + mod.SetField("pathSep", rt.StringValue(string(os.PathSeparator))) + mod.SetField("pathListSep", rt.StringValue(string(os.PathListSeparator))) + + return moonlight.TableValue(mod) } // abs(path) -> string @@ -124,16 +125,17 @@ 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(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { - if err := c.Check1Arg(); err != nil { +func (f *fs) fdir(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { + if err := mlr.Check1Arg(c); err != nil { return nil, err } - path, err := c.StringArg(0) + path, err := mlr.StringArg(c, 0) if err != nil { return nil, err } - return c.PushingNext(t.Runtime, rt.StringValue(filepath.Dir(path))), nil + next := mlr.PushNext1(c, moonlight.StringValue(filepath.Dir(path))) + return next, nil } // glob(pattern) -> matches (table) @@ -262,16 +264,16 @@ func (f *fs) fpipe(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { // Returns a list of all files and directories in the provided path. // #param dir string // #returns table -func (f *fs) freaddir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { - if err := c.Check1Arg(); err != nil { +func (f *fs) freaddir(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { + if err := mlr.Check1Arg(c); err != nil { return nil, err } - dir, err := c.StringArg(0) + dir, err := mlr.StringArg(c, 0) if err != nil { return nil, err } dir = util.ExpandHome(dir) - names := rt.NewTable() + names := moonlight.NewTable() dirEntries, err := os.ReadDir(dir) if err != nil { @@ -281,7 +283,7 @@ func (f *fs) freaddir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { names.Set(rt.IntValue(int64(i + 1)), rt.StringValue(entry.Name())) } - return c.PushingNext1(t.Runtime, rt.TableValue(names)), nil + return mlr.PushNext1(c, moonlight.TableValue(names)), nil } // stat(path) -> {} diff --git a/lua.go b/lua.go index 8cd8556..3f1bdc5 100644 --- a/lua.go +++ b/lua.go @@ -7,7 +7,7 @@ import ( //"hilbish/util" "hilbish/golibs/bait" //"hilbish/golibs/commander" - //"hilbish/golibs/fs" + "hilbish/golibs/fs" //"hilbish/golibs/terminal" "hilbish/moonlight" @@ -24,9 +24,9 @@ func luaInit() { l.DoString("hilbish = require 'hilbish'") // Add fs and terminal module module to Lua - /* f := fs.New(runner) - lib.LoadLibs(l, f.Loader) + l.LoadLibrary(f.Loader, "fs") + /* lib.LoadLibs(l, terminal.Loader) cmds = commander.New(l) diff --git a/module.go b/module.go index bf4e32a..e0a6efd 100644 --- a/module.go +++ b/module.go @@ -3,9 +3,7 @@ package main import ( "plugin" - "hilbish/util" - - rt "github.com/arnodel/golua/runtime" + "hilbish/moonlight" ) // #interface module @@ -46,13 +44,13 @@ func Loader(rtm *rt.Runtime) rt.Value { This can be compiled with `go build -buildmode=plugin plugin.go`. If you attempt to require and print the result (`print(require 'plugin')`), it will show "hello world!" */ -func moduleLoader(rtm *rt.Runtime) *rt.Table { - exports := map[string]util.LuaExport{ +func moduleLoader(mlr *moonlight.Runtime) *moonlight.Table { + exports := map[string]moonlight.Export{ "load": {moduleLoad, 2, false}, } - mod := rt.NewTable() - util.SetExports(rtm, mod, exports) + mod := moonlight.NewTable() + mlr.SetExports(mod, exports) return mod } @@ -62,12 +60,12 @@ func moduleLoader(rtm *rt.Runtime) *rt.Table { // Loads a module at the designated `path`. // It will throw if any error occurs. // #param path string -func moduleLoad(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { - if err := c.CheckNArgs(1); err != nil { +func moduleLoad(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) { + if err := mlr.Check1Arg(c); err != nil { return nil, err } - path, err := c.StringArg(0) + path, err := mlr.StringArg(c, 0) if err != nil { return nil, err } @@ -82,12 +80,12 @@ func moduleLoad(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { return nil, err } - loader, ok := value.(func(*rt.Runtime) rt.Value) + loader, ok := value.(func(*moonlight.Runtime) moonlight.Value) if !ok { return nil, nil } - val := loader(t.Runtime) + val := loader(mlr) - return c.PushingNext1(t.Runtime, val), nil + return mlr.PushNext1(c, val), nil } diff --git a/moonlight/cont_golua.go b/moonlight/cont_golua.go new file mode 100644 index 0000000..d34993e --- /dev/null +++ b/moonlight/cont_golua.go @@ -0,0 +1,16 @@ +package moonlight + +import ( + rt "github.com/arnodel/golua/runtime" +) + +type GoCont struct{ + cont *rt.GoCont + thread *rt.Thread +} +type Cont = rt.Cont +type Closure = rt.Closure + +func (gc *GoCont) Next() Cont { + return gc.cont.Next() +} diff --git a/moonlight/function_golua.go b/moonlight/function_golua.go index c48bdd2..5e882ae 100644 --- a/moonlight/function_golua.go +++ b/moonlight/function_golua.go @@ -6,21 +6,24 @@ import ( type GoFunctionFunc = rt.GoFunctionFunc -type GoCont = rt.GoCont -type Cont = rt.Cont -type Closure = rt.Closure - func (mlr *Runtime) CheckNArgs(c *GoCont, num int) error { - return c.CheckNArgs(num) + return c.cont.CheckNArgs(num) +} + +func (mlr *Runtime) Check1Arg(c *GoCont) error { + return c.cont.CheckNArgs(1) } func (mlr *Runtime) StringArg(c *GoCont, num int) (string, error) { - return c.StringArg(num) + return c.cont.StringArg(num) } func (mlr *Runtime) GoFunction(fun GoToLuaFunc) rt.GoFunctionFunc { return func(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) { - gocont := GoCont(*c) + gocont := GoCont{ + cont: c, + thread: t, + } return fun(mlr, &gocont) } } diff --git a/moonlight/runtime_golua.go b/moonlight/runtime_golua.go index 6072ea4..095931f 100644 --- a/moonlight/runtime_golua.go +++ b/moonlight/runtime_golua.go @@ -33,3 +33,12 @@ func specificRuntimeToGeneric(rtm *rt.Runtime) *Runtime { func (mlr *Runtime) UnderlyingRuntime() *rt.Runtime { return mlr.rt } + +// Push will push a Lua value onto the stack. +func (mlr *Runtime) Push(c *GoCont, v Value) { + c.cont.Push(c.thread.Runtime, v) +} + +func (mlr *Runtime) PushNext1(c *GoCont, v Value) Cont { + return c.cont.PushingNext1(c.thread.Runtime, v) +} diff --git a/moonlight/table_golua.go b/moonlight/table_golua.go index 3c64eb9..4f2ff36 100644 --- a/moonlight/table_golua.go +++ b/moonlight/table_golua.go @@ -22,6 +22,10 @@ func (t *Table) SetField(key string, value Value) { t.lt.Set(rt.StringValue(key), value) } +func (t *Table) Set(key Value, value Value) { + t.lt.Set(key, value) +} + func (mlr *Runtime) GlobalTable() *Table { return &Table{ lt: mlr.rt.GlobalEnv(),