mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Handle LuaErrors in Lua -> C++ calls on LuaJIT
This commit is contained in:
parent
38d112033b
commit
49cec3f782
18 changed files with 113 additions and 134 deletions
|
@ -653,7 +653,7 @@ ItemStack read_item(lua_State* L, int index,Server* srv)
|
|||
}
|
||||
else
|
||||
{
|
||||
throw LuaError(L, "Expecting itemstack, itemstring, table or nil");
|
||||
throw LuaError(NULL, "Expecting itemstack, itemstring, table or nil");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -55,6 +55,18 @@ int script_error_handler(lua_State *L) {
|
|||
return 1;
|
||||
}
|
||||
|
||||
int script_exception_wrapper(lua_State *L, lua_CFunction f)
|
||||
{
|
||||
try {
|
||||
return f(L); // Call wrapped function and return result.
|
||||
} catch (const char *s) { // Catch and convert exceptions.
|
||||
lua_pushstring(L, s);
|
||||
} catch (LuaError& e) {
|
||||
lua_pushstring(L, e.what());
|
||||
}
|
||||
return lua_error(L); // Rethrow as a Lua error.
|
||||
}
|
||||
|
||||
void script_error(lua_State *L)
|
||||
{
|
||||
const char *s = lua_tostring(L, -1);
|
||||
|
|
|
@ -66,6 +66,7 @@ enum RunCallbacksMode
|
|||
|
||||
std::string script_get_backtrace(lua_State *L);
|
||||
int script_error_handler(lua_State *L);
|
||||
int script_exception_wrapper(lua_State *L, lua_CFunction f);
|
||||
void script_error(lua_State *L);
|
||||
void script_run_callbacks(lua_State *L, int nargs,
|
||||
RunCallbacksMode mode);
|
||||
|
|
|
@ -23,10 +23,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#include "common/c_internal.h"
|
||||
#include "itemdef.h"
|
||||
|
||||
LuaError::LuaError(lua_State *L, const std::string &s)
|
||||
LuaError::LuaError(lua_State *L, const std::string &s) :
|
||||
ServerError(s)
|
||||
{
|
||||
m_s = "LuaError: " + s;
|
||||
if (L) m_s += '\n' + script_get_backtrace(L);
|
||||
if (L) {
|
||||
m_s += '\n' + script_get_backtrace(L);
|
||||
}
|
||||
}
|
||||
|
||||
struct EnumString es_ItemType[] =
|
||||
|
|
|
@ -26,6 +26,8 @@ extern "C" {
|
|||
|
||||
#include <iostream>
|
||||
|
||||
#include "exceptions.h"
|
||||
|
||||
struct EnumString
|
||||
{
|
||||
int num;
|
||||
|
@ -50,7 +52,7 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
class LuaError : public std::exception
|
||||
class LuaError : public ServerError
|
||||
{
|
||||
public:
|
||||
LuaError(lua_State *L, const std::string &s);
|
||||
|
@ -61,7 +63,6 @@ public:
|
|||
{
|
||||
return m_s.c_str();
|
||||
}
|
||||
std::string m_s;
|
||||
};
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue