feat: implement fs

lua5.4
TorchedSammy 2022-03-28 19:36:06 -04:00
parent b887ad4fa9
commit a7722fa331
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 78 additions and 49 deletions

View File

@ -8,51 +8,71 @@ import (
"strings" "strings"
"hilbish/util" "hilbish/util"
"github.com/yuin/gopher-lua" rt "github.com/arnodel/golua/runtime"
"github.com/arnodel/golua/lib/packagelib"
) )
func Loader(L *lua.LState) int { var Loader = packagelib.Loader{
mod := L.SetFuncs(L.NewTable(), exports) Load: LoaderFunc,
Name: "fs",
}
func LoaderFunc(rtm *rt.Runtime) (rt.Value, func()) {
exports := map[string]util.LuaExport{
"cd": util.LuaExport{fcd, 1, false},
"mkdir": util.LuaExport{fmkdir, 2, false},
"stat": util.LuaExport{fstat, 1, false},
"readdir": util.LuaExport{freaddir, 1, false},
}
mod := rt.NewTable()
util.SetExports(rtm, mod, exports)
/*
util.Document(L, mod, `The fs module provides easy and simple access to util.Document(L, mod, `The fs module provides easy and simple access to
filesystem functions and other things, and acts an filesystem functions and other things, and acts an
addition to the Lua standard library's I/O and fs functions.`) addition to the Lua standard library's I/O and fs functions.`)
*/
L.Push(mod) return rt.TableValue(mod), nil
return 1
}
var exports = map[string]lua.LGFunction{
"cd": fcd,
"mkdir": fmkdir,
"stat": fstat,
"readdir": freaddir,
} }
// cd(dir) // cd(dir)
// Changes directory to `dir` // Changes directory to `dir`
// --- @param dir string // --- @param dir string
func fcd(L *lua.LState) int { func fcd(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
path := L.CheckString(1) if err := c.Check1Arg(); err != nil {
return nil, err
err := os.Chdir(strings.TrimSpace(path)) }
path, err := c.StringArg(0)
if err != nil { if err != nil {
e := err.(*os.PathError).Err.Error() return nil, err
L.RaiseError(e + ": " + path)
} }
return 0 err = os.Chdir(strings.TrimSpace(path))
if err != nil {
return nil, err
}
return c.Next(), err
} }
// mkdir(name, recursive) // mkdir(name, recursive)
// Makes a directory called `name`. If `recursive` is true, it will create its parent directories. // Makes a directory called `name`. If `recursive` is true, it will create its parent directories.
// --- @param name string // --- @param name string
// --- @param recursive boolean // --- @param recursive boolean
func fmkdir(L *lua.LState) int { func fmkdir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
dirname := L.CheckString(1) if err := c.CheckNArgs(2); err != nil {
recursive := L.ToBool(2) return nil, err
}
dirname, err := c.StringArg(0)
if err != nil {
return nil, err
}
recursive, err := c.BoolArg(1)
if err != nil {
return nil, err
}
path := strings.TrimSpace(dirname) path := strings.TrimSpace(dirname)
var err error
if recursive { if recursive {
err = os.MkdirAll(path, 0744) err = os.MkdirAll(path, 0744)
@ -60,51 +80,58 @@ func fmkdir(L *lua.LState) int {
err = os.Mkdir(path, 0744) err = os.Mkdir(path, 0744)
} }
if err != nil { if err != nil {
L.RaiseError(err.Error() + ": " + path) return nil, err
} }
return 0 return c.Next(), err
} }
// stat(path) // stat(path)
// Returns info about `path` // Returns info about `path`
// --- @param path string // --- @param path string
func fstat(L *lua.LState) int { func fstat(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
path := L.CheckString(1) if err := c.Check1Arg(); err != nil {
return nil, err
}
path, err := c.StringArg(0)
if err != nil {
return nil, err
}
pathinfo, err := os.Stat(path) pathinfo, err := os.Stat(path)
if err != nil { if err != nil {
L.RaiseError(err.Error() + ": " + path) return nil, err
return 0
} }
statTbl := L.NewTable() statTbl := rt.NewTable()
L.SetField(statTbl, "name", lua.LString(pathinfo.Name())) statTbl.Set(rt.StringValue("name"), rt.StringValue(pathinfo.Name()))
L.SetField(statTbl, "size", lua.LNumber(pathinfo.Size())) statTbl.Set(rt.StringValue("size"), rt.IntValue(pathinfo.Size()))
L.SetField(statTbl, "mode", lua.LString("0" + strconv.FormatInt(int64(pathinfo.Mode().Perm()), 8))) statTbl.Set(rt.StringValue("mode"), rt.StringValue("0" + strconv.FormatInt(int64(pathinfo.Mode().Perm()), 8)))
L.SetField(statTbl, "isDir", lua.LBool(pathinfo.IsDir())) statTbl.Set(rt.StringValue("isDir"), rt.BoolValue(pathinfo.IsDir()))
L.Push(statTbl)
return c.PushingNext1(t.Runtime, rt.TableValue(statTbl)), nil
return 1
} }
// readdir(dir) // readdir(dir)
// Returns a table of files in `dir` // Returns a table of files in `dir`
// --- @param dir string // --- @param dir string
// --- @return table // --- @return table
func freaddir(L *lua.LState) int { func freaddir(t *rt.Thread, c *rt.GoCont) (rt.Cont, error) {
dir := L.CheckString(1) if err := c.Check1Arg(); err != nil {
names := L.NewTable() return nil, err
}
dir, err := c.StringArg(0)
if err != nil {
return nil, err
}
names := rt.NewTable()
dirEntries, err := os.ReadDir(dir) dirEntries, err := os.ReadDir(dir)
if err != nil { if err != nil {
L.RaiseError(err.Error() + ": " + dir) return nil, err
return 0
} }
for _, entry := range dirEntries { for i, entry := range dirEntries {
names.Append(lua.LString(entry.Name())) names.Set(rt.IntValue(int64(i + 1)), rt.StringValue(entry.Name()))
} }
L.Push(names) return c.PushingNext1(t.Runtime, rt.TableValue(names)), nil
return 1
} }

6
lua.go
View File

@ -7,8 +7,8 @@ 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"
*/ */
rt "github.com/arnodel/golua/runtime" rt "github.com/arnodel/golua/runtime"
@ -26,7 +26,8 @@ func luaInit() {
util.DoString(l, "hilbish = require 'hilbish'") util.DoString(l, "hilbish = require 'hilbish'")
// Add fs and terminal module module to Lua // Add fs and terminal module module to Lua
/* l.PreloadModule("fs", fs.Loader) lib.LoadLibs(l, fs.Loader)
/*
l.PreloadModule("terminal", terminal.Loader) l.PreloadModule("terminal", terminal.Loader)
*/ */
@ -63,6 +64,7 @@ func luaInit() {
fmt.Fprintln(os.Stderr, "Missing preload file, builtins may be missing.") fmt.Fprintln(os.Stderr, "Missing preload file, builtins may be missing.")
} }
} }
fmt.Println(err)
} }
func runConfig(confpath string) { func runConfig(confpath string) {