summaryrefslogtreecommitdiff
path: root/src/script/common
diff options
context:
space:
mode:
authorShadowNinja <shadowninja@minetest.net>2013-12-18 16:35:55 -0500
committerShadowNinja <shadowninja@minetest.net>2013-12-18 16:35:55 -0500
commit49cec3f78240ed6310a9b5dd05ce09a79ed5a12e (patch)
tree38e8199e49906357939e74b6ed0b00f6f54de976 /src/script/common
parent38d112033b3ba0ea0360fced334a279576aafc5d (diff)
downloadminetest-49cec3f78240ed6310a9b5dd05ce09a79ed5a12e.tar.gz
minetest-49cec3f78240ed6310a9b5dd05ce09a79ed5a12e.tar.bz2
minetest-49cec3f78240ed6310a9b5dd05ce09a79ed5a12e.zip
Handle LuaErrors in Lua -> C++ calls on LuaJIT
Diffstat (limited to 'src/script/common')
-rw-r--r--src/script/common/c_content.cpp2
-rw-r--r--src/script/common/c_internal.cpp12
-rw-r--r--src/script/common/c_internal.h1
-rw-r--r--src/script/common/c_types.cpp8
-rw-r--r--src/script/common/c_types.h5
5 files changed, 22 insertions, 6 deletions
diff --git a/src/script/common/c_content.cpp b/src/script/common/c_content.cpp
index d58479042..cf9f28d30 100644
--- a/src/script/common/c_content.cpp
+++ b/src/script/common/c_content.cpp
@@ -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");
}
}
diff --git a/src/script/common/c_internal.cpp b/src/script/common/c_internal.cpp
index 2866cfe86..90846676f 100644
--- a/src/script/common/c_internal.cpp
+++ b/src/script/common/c_internal.cpp
@@ -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);
diff --git a/src/script/common/c_internal.h b/src/script/common/c_internal.h
index eb6aa06e8..f3ef18d70 100644
--- a/src/script/common/c_internal.h
+++ b/src/script/common/c_internal.h
@@ -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);
diff --git a/src/script/common/c_types.cpp b/src/script/common/c_types.cpp
index a6faf9819..6ffad1cb1 100644
--- a/src/script/common/c_types.cpp
+++ b/src/script/common/c_types.cpp
@@ -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[] =
diff --git a/src/script/common/c_types.h b/src/script/common/c_types.h
index bc9f1cb96..709d4f34b 100644
--- a/src/script/common/c_types.h
+++ b/src/script/common/c_types.h
@@ -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;
};