Compare commits

..

2 Commits

Author SHA1 Message Date
TorchedSammy e6563fbd8b
fix: add newline at the beginning of file buffer when there is shebang
this makes the line count in error messages line up properly
2022-04-02 20:59:55 -04:00
TorchedSammy 8a34783fee
fix: add names at chunk load for context in errors 2022-04-02 19:13:35 -04:00
1 changed files with 9 additions and 3 deletions

View File

@ -42,7 +42,7 @@ func SetField(rtm *rt.Runtime, module *rt.Table, field string, value rt.Value, d
// DoString runs the code string in the Lua runtime. // DoString runs the code string in the Lua runtime.
func DoString(rtm *rt.Runtime, code string) error { func DoString(rtm *rt.Runtime, code string) error {
chunk, err := rtm.CompileAndLoadLuaChunk("", []byte(code), rt.TableValue(rtm.GlobalEnv())) chunk, err := rtm.CompileAndLoadLuaChunk("<string>", []byte(code), rt.TableValue(rtm.GlobalEnv()))
if chunk != nil { if chunk != nil {
_, err = rt.Call1(rtm.MainThread(), rt.FunctionValue(chunk)) _, err = rt.Call1(rtm.MainThread(), rt.FunctionValue(chunk))
} }
@ -71,15 +71,16 @@ func DoFile(rtm *rt.Runtime, path string) error {
return err return err
} }
var buf []byte
if c == byte('#') { if c == byte('#') {
// shebang - skip that line // shebang - skip that line
_, err := reader.ReadBytes('\n') _, err := reader.ReadBytes('\n')
if err != nil && err != io.EOF { if err != nil && err != io.EOF {
return err return err
} }
buf = []byte{'\n'}
} }
var buf []byte
for { for {
line, err := reader.ReadBytes('\n') line, err := reader.ReadBytes('\n')
if err != nil { if err != nil {
@ -92,7 +93,12 @@ func DoFile(rtm *rt.Runtime, path string) error {
buf = append(buf, line...) buf = append(buf, line...)
} }
return DoString(rtm, string(buf)) chunk, err := rtm.CompileAndLoadLuaChunk(path, buf, rt.TableValue(rtm.GlobalEnv()))
if chunk != nil {
_, err = rt.Call1(rtm.MainThread(), rt.FunctionValue(chunk))
}
return err
} }
// HandleStrCallback handles function parameters for Go functions which take // HandleStrCallback handles function parameters for Go functions which take