Compare commits

..

4 Commits

Author SHA1 Message Date
TorchedSammy 50f703d9b3
fix: remove prelude error printing 2022-04-02 17:25:11 -04:00
TorchedSammy f94d7ac81f
chore: add comment for reason of unreading byte 2022-04-02 15:53:39 -04:00
TorchedSammy 48ab7c13ac
fix: read file manually in DoFile to avoid shebang 2022-04-02 15:30:55 -04:00
TorchedSammy e91cf98634
feat: run script when provided path 2022-04-02 15:27:15 -04:00
3 changed files with 47 additions and 10 deletions

1
lua.go
View File

@ -61,7 +61,6 @@ func luaInit() {
fmt.Fprintln(os.Stderr, "Missing preload file, builtins may be missing.")
}
}
fmt.Println(err)
}
func runConfig(confpath string) {

13
main.go
View File

@ -10,6 +10,7 @@ import (
"runtime"
"strings"
"hilbish/util"
"hilbish/golibs/bait"
rt "github.com/arnodel/golua/runtime"
@ -151,17 +152,17 @@ func main() {
}
if getopt.NArgs() > 0 {
/*luaArgs := l.NewTable()
for _, arg := range getopt.Args() {
luaArgs.Append(lua.LString(arg))
luaArgs := rt.NewTable()
for i, arg := range getopt.Args() {
luaArgs.Set(rt.IntValue(int64(i + 1)), rt.StringValue(arg))
}
l.SetGlobal("args", luaArgs)
err := l.DoFile(getopt.Arg(0))
l.GlobalEnv().Set(rt.StringValue("args"), rt.TableValue(luaArgs))
err := util.DoFile(l, getopt.Arg(0))
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}*/
}
os.Exit(0)
}

View File

@ -1,6 +1,8 @@
package util
import (
"bufio"
"io"
"os"
rt "github.com/arnodel/golua/runtime"
@ -49,13 +51,48 @@ func DoString(rtm *rt.Runtime, code string) error {
}
// DoFile runs the contents of the file in the Lua runtime.
func DoFile(rtm *rt.Runtime, filename string) error {
data, err := os.ReadFile(filename)
func DoFile(rtm *rt.Runtime, path string) error {
f, err := os.Open(path)
defer f.Close()
if err != nil {
return err
}
reader := bufio.NewReader(f)
c, err := reader.ReadByte()
if err != nil && err != io.EOF {
return err
}
// unread so a char won't be missing
err = reader.UnreadByte()
if err != nil {
return err
}
return DoString(rtm, string(data))
if c == byte('#') {
// shebang - skip that line
_, err := reader.ReadBytes('\n')
if err != nil && err != io.EOF {
return err
}
}
var buf []byte
for {
line, err := reader.ReadBytes('\n')
if err != nil {
if err == io.EOF {
break
}
return err
}
buf = append(buf, line...)
}
return DoString(rtm, string(buf))
}
// HandleStrCallback handles function parameters for Go functions which take