fix: handle errors in user config

lua5.4
TorchedSammy 2022-03-28 17:40:36 -04:00
parent 9fc2247c20
commit d34aa09e8e
Signed by: sammyette
GPG Key ID: 904FC49417B44DCD
2 changed files with 22 additions and 7 deletions

10
lua.go
View File

@ -4,6 +4,7 @@ import (
"fmt"
"os"
"hilbish/util"
"hilbish/golibs/bait"
/*
"hilbish/golibs/commander"
@ -71,14 +72,9 @@ func runConfig(confpath string) {
if !interactive {
return
}
data, err := os.ReadFile(confpath)
err := util.DoFile(l, confpath)
if err != nil {
fmt.Fprintln(os.Stderr, err, "\nAn error has occured while loading your config! Falling back to minimal default config.")
chunk, _ := l.CompileAndLoadLuaChunk("", []byte(minimalconf), rt.TableValue(l.GlobalEnv()))
_, err := rt.Call1(l.MainThread(), rt.FunctionValue(chunk))
fmt.Println(err)
util.DoString(l, minimalconf)
}
chunk, _ := l.CompileAndLoadLuaChunk("", data, rt.TableValue(l.GlobalEnv()))
_, err = rt.Call1(l.MainThread(), rt.FunctionValue(chunk))
fmt.Println("config", err)
}

View File

@ -1,6 +1,8 @@
package util
import (
"os"
"github.com/yuin/gopher-lua"
rt "github.com/arnodel/golua/runtime"
)
@ -36,3 +38,20 @@ func SetField(rtm *rt.Runtime, module *rt.Table, field string, value rt.Value, d
module.Set(rt.StringValue(field), value)
}
func DoString(rtm *rt.Runtime, code string) error {
chunk, err := rtm.CompileAndLoadLuaChunk("", []byte(code), rt.TableValue(rtm.GlobalEnv()))
if chunk != nil {
_, err = rt.Call1(rtm.MainThread(), rt.FunctionValue(chunk))
}
return err
}
func DoFile(rtm *rt.Runtime, filename string) error {
data, err := os.ReadFile(filename)
if err != nil {
return err
}
return DoString(rtm, string(data))
}