diff options
| author | unwox <me@unwox.com> | 2024-09-27 15:33:49 +0600 |
|---|---|---|
| committer | unwox <me@unwox.com> | 2024-09-27 15:33:49 +0600 |
| commit | 81a1002523eb7ac78fb2a55e070e7058ca31c0e0 (patch) | |
| tree | 1a8fe886680d4c8040ea2aec46c9ffed412a30f4 | |
| parent | adae08e3dfca081da1a0bc70f3d242ea2b79efb3 (diff) | |
make Lua:LoadFile return error instead of boolean
| -rw-r--r-- | lua.go | 18 |
1 files changed, 11 insertions, 7 deletions
@@ -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. |
