fix: read file manually in DoFile to avoid shebang

lua5.4
TorchedSammy 2022-04-02 15:30:55 -04:00
parent e91cf98634
commit 48ab7c13ac
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
1 changed files with 39 additions and 3 deletions

View File

@ -1,6 +1,8 @@
package util package util
import ( import (
"bufio"
"io"
"os" "os"
rt "github.com/arnodel/golua/runtime" rt "github.com/arnodel/golua/runtime"
@ -49,13 +51,47 @@ func DoString(rtm *rt.Runtime, code string) error {
} }
// DoFile runs the contents of the file in the Lua runtime. // DoFile runs the contents of the file in the Lua runtime.
func DoFile(rtm *rt.Runtime, filename string) error { func DoFile(rtm *rt.Runtime, path string) error {
data, err := os.ReadFile(filename) 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
}
err = reader.UnreadByte()
if err != nil { if err != nil {
return err 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 // HandleStrCallback handles function parameters for Go functions which take