Skip to content

Commit

Permalink
Improve error handling (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
Trey2k authored May 22, 2023
1 parent 73a6eb9 commit f9c97cd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/classes/luaAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ LuaError* LuaAPI::doFile(String fileName) {
LuaError* LuaAPI::doString(String code) {
// push the error handler onto the stack
lua_pushcfunction(lState, LuaState::luaErrorHandler);
luaL_loadstring(lState, code.ascii().get_data());
int ret = luaL_loadstring(lState, code.ascii().get_data());
if (ret != LUA_OK) {
return state.handleError(ret);
}

LuaError* err = execute(-2);
// pop the error handler from the stack
lua_pop(lState, 1);
Expand Down
4 changes: 3 additions & 1 deletion src/classes/luaCallable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ uint32_t LuaCallable::hash() const {
}

void LuaCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
lua_pushcfunction(state, LuaState::luaErrorHandler);

// Geting the lua function via the referance stored in funcRef
lua_rawgeti(state, LUA_REGISTRYINDEX, funcRef);

Expand All @@ -55,7 +57,7 @@ void LuaCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_r
}

// execute the function using a protected call.
int ret = lua_pcall(state, p_argcount, 1, 0);
int ret = lua_pcall(state, p_argcount, 1, -2 - p_argcount);
if (ret != LUA_OK) {
r_return_value = LuaState::handleError(state, ret);
} else r_return_value = LuaState::getVariant(state, -1, obj.ptr());
Expand Down

0 comments on commit f9c97cd

Please sign in to comment.