feat: implement parts of fs

midnight-edition
sammyette 2024-07-20 11:34:39 -04:00
parent 40a90c899b
commit a5f695eb98
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
9 changed files with 87 additions and 54 deletions

5
api.go
View File

@ -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)
}

View File

@ -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
}

View File

@ -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) -> {}

6
lua.go
View File

@ -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)

View File

@ -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
}

View File

@ -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()
}

View File

@ -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)
}
}

View File

@ -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)
}

View File

@ -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(),