From 81a1002523eb7ac78fb2a55e070e7058ca31c0e0 Mon Sep 17 00:00:00 2001 From: unwox Date: Fri, 27 Sep 2024 15:33:49 +0600 Subject: make Lua:LoadFile return error instead of boolean --- lua.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/lua.go b/lua.go index e86005a..4867d22 100644 --- a/lua.go +++ b/lua.go @@ -58,12 +58,11 @@ func (l *Lua) Close() { // Require loads and executes the file pushing results onto the Lua stack. func (l *Lua) Require(file string) error { - if !l.LoadFile(file) { - errMsg := l.ToString(-1) - l.Pop(1) - return errors.New("could not open the file:\n" + errMsg) + err := l.LoadFile(file) + if err != nil { + return errors.New("could not open the file:\n" + err.Error()) } - err := l.PCall(0, C.LUA_MULTRET) + err = l.PCall(0, C.LUA_MULTRET) if err != nil { return errors.New("could not execute the file:\n" + err.Error()) } @@ -83,11 +82,16 @@ func (l *Lua) PCall(nargs int, nresults int) error { } // LoadFile loads the file code in the current Lua context. -func (l *Lua) LoadFile(file string) bool { +func (l *Lua) LoadFile(file string) error { cstr := C.CString(file) defer C.free(unsafe.Pointer(cstr)) res := C.luaL_loadfilex(l.l, cstr, nil) - return res == C.LUA_OK + if res != C.LUA_OK { + errMsg := l.ToString(-1) + l.Pop(1) + return errors.New(errMsg) + } + return nil } // StackLen returns the length of the Lua stack. -- cgit v1.2.3