mirror of https://github.com/Hilbis/Hilbish
feat: implement parts of fs
parent
40a90c899b
commit
a5f695eb98
5
api.go
5
api.go
|
@ -127,8 +127,9 @@ func hilbishLoader(mlr *moonlight.Runtime) moonlight.Value {
|
||||||
//util.SetField(rtm, versionModule, "release", rt.StringValue(releaseName))
|
//util.SetField(rtm, versionModule, "release", rt.StringValue(releaseName))
|
||||||
//mod.Set(rt.StringValue("version"), rt.TableValue(versionModule))
|
//mod.Set(rt.StringValue("version"), rt.TableValue(versionModule))
|
||||||
|
|
||||||
//pluginModule := moduleLoader(rtm)
|
// very meta
|
||||||
//mod.Set(rt.StringValue("module"), rt.TableValue(pluginModule))
|
moduleModule := moduleLoader(mlr)
|
||||||
|
hshMod.SetField("module", moonlight.TableValue(moduleModule))
|
||||||
|
|
||||||
return moonlight.TableValue(hshMod)
|
return moonlight.TableValue(hshMod)
|
||||||
}
|
}
|
||||||
|
|
4
exec.go
4
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("out"), rt.UserDataValue(stdout.ud))
|
||||||
sinks.Set(rt.StringValue("err"), rt.UserDataValue(stderr.ud))
|
sinks.Set(rt.StringValue("err"), rt.UserDataValue(stderr.ud))
|
||||||
|
|
||||||
t := rt.NewThread(l)
|
//t := rt.NewThread(l)
|
||||||
sig := make(chan os.Signal)
|
sig := make(chan os.Signal)
|
||||||
exit := make(chan bool)
|
exit := make(chan bool)
|
||||||
|
|
||||||
|
@ -368,7 +368,7 @@ func execHandle(bg bool) interp.ExecHandlerFunc {
|
||||||
signal.Notify(sig, os.Interrupt)
|
signal.Notify(sig, os.Interrupt)
|
||||||
select {
|
select {
|
||||||
case <-sig:
|
case <-sig:
|
||||||
t.KillContext()
|
//t.KillContext()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -14,50 +14,51 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"hilbish/moonlight"
|
||||||
"hilbish/util"
|
"hilbish/util"
|
||||||
|
|
||||||
rt "github.com/arnodel/golua/runtime"
|
rt "github.com/arnodel/golua/runtime"
|
||||||
"github.com/arnodel/golua/lib/packagelib"
|
|
||||||
"github.com/arnodel/golua/lib/iolib"
|
"github.com/arnodel/golua/lib/iolib"
|
||||||
"mvdan.cc/sh/v3/interp"
|
"mvdan.cc/sh/v3/interp"
|
||||||
)
|
)
|
||||||
|
|
||||||
type fs struct{
|
type fs struct{
|
||||||
runner *interp.Runner
|
runner *interp.Runner
|
||||||
Loader packagelib.Loader
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(runner *interp.Runner) *fs {
|
func New(runner *interp.Runner) *fs {
|
||||||
f := &fs{
|
return &fs{
|
||||||
runner: runner,
|
runner: runner,
|
||||||
}
|
}
|
||||||
f.Loader = packagelib.Loader{
|
|
||||||
Load: f.loaderFunc,
|
|
||||||
Name: "fs",
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return f
|
func (f *fs) Loader(rtm *moonlight.Runtime) moonlight.Value {
|
||||||
}
|
exports := map[string]moonlight.Export{
|
||||||
|
/*
|
||||||
func (f *fs) loaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
|
|
||||||
exports := map[string]util.LuaExport{
|
|
||||||
"cd": util.LuaExport{f.fcd, 1, false},
|
"cd": util.LuaExport{f.fcd, 1, false},
|
||||||
"mkdir": util.LuaExport{f.fmkdir, 2, false},
|
"mkdir": util.LuaExport{f.fmkdir, 2, false},
|
||||||
"stat": util.LuaExport{f.fstat, 1, 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},
|
"abs": util.LuaExport{f.fabs, 1, false},
|
||||||
"basename": util.LuaExport{f.fbasename, 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},
|
"glob": util.LuaExport{f.fglob, 1, false},
|
||||||
"join": util.LuaExport{f.fjoin, 0, true},
|
"join": util.LuaExport{f.fjoin, 0, true},
|
||||||
"pipe": util.LuaExport{f.fpipe, 0, false},
|
"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
|
// 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`.
|
// `~/Documents/doc.txt` then this function will return `~/Documents`.
|
||||||
// #param path string Path to get the directory for.
|
// #param path string Path to get the directory for.
|
||||||
// #returns string
|
// #returns string
|
||||||
func (f *fs) fdir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func (f *fs) fdir(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
|
||||||
if err := c.Check1Arg(); err != nil {
|
if err := mlr.Check1Arg(c); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
path, err := c.StringArg(0)
|
path, err := mlr.StringArg(c, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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)
|
// 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.
|
// Returns a list of all files and directories in the provided path.
|
||||||
// #param dir string
|
// #param dir string
|
||||||
// #returns table
|
// #returns table
|
||||||
func (f *fs) freaddir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func (f *fs) freaddir(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
|
||||||
if err := c.Check1Arg(); err != nil {
|
if err := mlr.Check1Arg(c); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
dir, err := c.StringArg(0)
|
dir, err := mlr.StringArg(c, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
dir = util.ExpandHome(dir)
|
dir = util.ExpandHome(dir)
|
||||||
names := rt.NewTable()
|
names := moonlight.NewTable()
|
||||||
|
|
||||||
dirEntries, err := os.ReadDir(dir)
|
dirEntries, err := os.ReadDir(dir)
|
||||||
if err != nil {
|
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()))
|
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) -> {}
|
// stat(path) -> {}
|
||||||
|
|
6
lua.go
6
lua.go
|
@ -7,7 +7,7 @@ import (
|
||||||
//"hilbish/util"
|
//"hilbish/util"
|
||||||
"hilbish/golibs/bait"
|
"hilbish/golibs/bait"
|
||||||
//"hilbish/golibs/commander"
|
//"hilbish/golibs/commander"
|
||||||
//"hilbish/golibs/fs"
|
"hilbish/golibs/fs"
|
||||||
//"hilbish/golibs/terminal"
|
//"hilbish/golibs/terminal"
|
||||||
|
|
||||||
"hilbish/moonlight"
|
"hilbish/moonlight"
|
||||||
|
@ -24,9 +24,9 @@ func luaInit() {
|
||||||
l.DoString("hilbish = require 'hilbish'")
|
l.DoString("hilbish = require 'hilbish'")
|
||||||
|
|
||||||
// Add fs and terminal module module to Lua
|
// Add fs and terminal module module to Lua
|
||||||
/*
|
|
||||||
f := fs.New(runner)
|
f := fs.New(runner)
|
||||||
lib.LoadLibs(l, f.Loader)
|
l.LoadLibrary(f.Loader, "fs")
|
||||||
|
/*
|
||||||
lib.LoadLibs(l, terminal.Loader)
|
lib.LoadLibs(l, terminal.Loader)
|
||||||
|
|
||||||
cmds = commander.New(l)
|
cmds = commander.New(l)
|
||||||
|
|
24
module.go
24
module.go
|
@ -3,9 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"plugin"
|
"plugin"
|
||||||
|
|
||||||
"hilbish/util"
|
"hilbish/moonlight"
|
||||||
|
|
||||||
rt "github.com/arnodel/golua/runtime"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// #interface module
|
// #interface module
|
||||||
|
@ -46,13 +44,13 @@ func Loader(rtm *rt.Runtime) rt.Value {
|
||||||
This can be compiled with `go build -buildmode=plugin plugin.go`.
|
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!"
|
If you attempt to require and print the result (`print(require 'plugin')`), it will show "hello world!"
|
||||||
*/
|
*/
|
||||||
func moduleLoader(rtm *rt.Runtime) *rt.Table {
|
func moduleLoader(mlr *moonlight.Runtime) *moonlight.Table {
|
||||||
exports := map[string]util.LuaExport{
|
exports := map[string]moonlight.Export{
|
||||||
"load": {moduleLoad, 2, false},
|
"load": {moduleLoad, 2, false},
|
||||||
}
|
}
|
||||||
|
|
||||||
mod := rt.NewTable()
|
mod := moonlight.NewTable()
|
||||||
util.SetExports(rtm, mod, exports)
|
mlr.SetExports(mod, exports)
|
||||||
|
|
||||||
return mod
|
return mod
|
||||||
}
|
}
|
||||||
|
@ -62,12 +60,12 @@ func moduleLoader(rtm *rt.Runtime) *rt.Table {
|
||||||
// Loads a module at the designated `path`.
|
// Loads a module at the designated `path`.
|
||||||
// It will throw if any error occurs.
|
// It will throw if any error occurs.
|
||||||
// #param path string
|
// #param path string
|
||||||
func moduleLoad(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
func moduleLoad(mlr *moonlight.Runtime, c *moonlight.GoCont) (moonlight.Cont, error) {
|
||||||
if err := c.CheckNArgs(1); err != nil {
|
if err := mlr.Check1Arg(c); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
path, err := c.StringArg(0)
|
path, err := mlr.StringArg(c, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
@ -82,12 +80,12 @@ func moduleLoad(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
loader, ok := value.(func(*rt.Runtime) rt.Value)
|
loader, ok := value.(func(*moonlight.Runtime) moonlight.Value)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
val := loader(t.Runtime)
|
val := loader(mlr)
|
||||||
|
|
||||||
return c.PushingNext1(t.Runtime, val), nil
|
return mlr.PushNext1(c, val), nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -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()
|
||||||
|
}
|
|
@ -6,21 +6,24 @@ import (
|
||||||
|
|
||||||
type GoFunctionFunc = rt.GoFunctionFunc
|
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 {
|
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) {
|
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 {
|
func (mlr *Runtime) GoFunction(fun GoToLuaFunc) rt.GoFunctionFunc {
|
||||||
return func(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
return func(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
|
||||||
gocont := GoCont(*c)
|
gocont := GoCont{
|
||||||
|
cont: c,
|
||||||
|
thread: t,
|
||||||
|
}
|
||||||
return fun(mlr, &gocont)
|
return fun(mlr, &gocont)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,3 +33,12 @@ func specificRuntimeToGeneric(rtm *rt.Runtime) *Runtime {
|
||||||
func (mlr *Runtime) UnderlyingRuntime() *rt.Runtime {
|
func (mlr *Runtime) UnderlyingRuntime() *rt.Runtime {
|
||||||
return mlr.rt
|
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)
|
||||||
|
}
|
||||||
|
|
|
@ -22,6 +22,10 @@ func (t *Table) SetField(key string, value Value) {
|
||||||
t.lt.Set(rt.StringValue(key), 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 {
|
func (mlr *Runtime) GlobalTable() *Table {
|
||||||
return &Table{
|
return &Table{
|
||||||
lt: mlr.rt.GlobalEnv(),
|
lt: mlr.rt.GlobalEnv(),
|
||||||
|
|
Loading…
Reference in New Issue