summaryrefslogtreecommitdiff
path: root/src/script/cpp_api/s_node.cpp
diff options
context:
space:
mode:
authorShadowNinja <shadowninja@minetest.net>2013-11-05 12:06:15 -0500
committerShadowNinja <shadowninja@minetest.net>2013-11-15 14:13:31 -0500
commit371b39a09a0bf248d674fae718f5ff369e895b66 (patch)
treeda8bb27e27a9c89eac895d211721de11a3781533 /src/script/cpp_api/s_node.cpp
parent3f519eb72922607329e1e6a48768d84d1f443efc (diff)
downloadminetest-371b39a09a0bf248d674fae718f5ff369e895b66.tar.gz
minetest-371b39a09a0bf248d674fae718f5ff369e895b66.tar.bz2
minetest-371b39a09a0bf248d674fae718f5ff369e895b66.zip
Pass a errfunc to lua_pcall to get a traceback
Diffstat (limited to 'src/script/cpp_api/s_node.cpp')
-rw-r--r--src/script/cpp_api/s_node.cpp92
1 files changed, 62 insertions, 30 deletions
diff --git a/src/script/cpp_api/s_node.cpp b/src/script/cpp_api/s_node.cpp
index 92fd00a74..cd8451cf0 100644
--- a/src/script/cpp_api/s_node.cpp
+++ b/src/script/cpp_api/s_node.cpp
@@ -91,6 +91,9 @@ bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
{
SCRIPTAPI_PRECHECKHEADER
+ lua_pushcfunction(L, script_error_handler);
+ int errorhandler = lua_gettop(L);
+
INodeDefManager *ndef = getServer()->ndef();
// Push callback function on stack
@@ -101,8 +104,9 @@ bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
push_v3s16(L, p);
pushnode(L, node, ndef);
objectrefGetOrCreate(puncher);
- if(lua_pcall(L, 3, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 3, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
return true;
}
@@ -111,6 +115,9 @@ bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
{
SCRIPTAPI_PRECHECKHEADER
+ lua_pushcfunction(L, script_error_handler);
+ int errorhandler = lua_gettop(L);
+
INodeDefManager *ndef = getServer()->ndef();
// Push callback function on stack
@@ -121,8 +128,9 @@ bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
push_v3s16(L, p);
pushnode(L, node, ndef);
objectrefGetOrCreate(digger);
- if(lua_pcall(L, 3, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 3, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
return true;
}
@@ -130,6 +138,9 @@ void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
{
SCRIPTAPI_PRECHECKHEADER
+ lua_pushcfunction(L, script_error_handler);
+ int errorhandler = lua_gettop(L);
+
INodeDefManager *ndef = getServer()->ndef();
// Push callback function on stack
@@ -138,14 +149,18 @@ void ScriptApiNode::node_on_construct(v3s16 p, MapNode node)
// Call function
push_v3s16(L, p);
- if(lua_pcall(L, 1, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 1, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
}
void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
{
SCRIPTAPI_PRECHECKHEADER
+ lua_pushcfunction(L, script_error_handler);
+ int errorhandler = lua_gettop(L);
+
INodeDefManager *ndef = getServer()->ndef();
// Push callback function on stack
@@ -154,14 +169,18 @@ void ScriptApiNode::node_on_destruct(v3s16 p, MapNode node)
// Call function
push_v3s16(L, p);
- if(lua_pcall(L, 1, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 1, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
}
void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
{
SCRIPTAPI_PRECHECKHEADER
+ lua_pushcfunction(L, script_error_handler);
+ int errorhandler = lua_gettop(L);
+
INodeDefManager *ndef = getServer()->ndef();
// Push callback function on stack
@@ -171,14 +190,18 @@ void ScriptApiNode::node_after_destruct(v3s16 p, MapNode node)
// Call function
push_v3s16(L, p);
pushnode(L, node, ndef);
- if(lua_pcall(L, 2, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 2, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
}
bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
{
SCRIPTAPI_PRECHECKHEADER
+ lua_pushcfunction(L, script_error_handler);
+ int errorhandler = lua_gettop(L);
+
INodeDefManager *ndef = getServer()->ndef();
// Push callback function on stack
@@ -188,12 +211,10 @@ bool ScriptApiNode::node_on_timer(v3s16 p, MapNode node, f32 dtime)
// Call function
push_v3s16(L, p);
lua_pushnumber(L,dtime);
- if(lua_pcall(L, 2, 1, 0))
- scriptError("error: %s", lua_tostring(L, -1));
- if((bool)lua_isboolean(L,-1) && (bool)lua_toboolean(L,-1) == true)
- return true;
-
- return false;
+ if(lua_pcall(L, 2, 1, errorhandler))
+ scriptError();
+ lua_remove(L, errorhandler); // Remove error handler
+ return (bool) lua_isboolean(L, -1) && (bool) lua_toboolean(L, -1) == true;
}
void ScriptApiNode::node_on_receive_fields(v3s16 p,
@@ -203,6 +224,9 @@ void ScriptApiNode::node_on_receive_fields(v3s16 p,
{
SCRIPTAPI_PRECHECKHEADER
+ lua_pushcfunction(L, script_error_handler);
+ int errorhandler = lua_gettop(L);
+
INodeDefManager *ndef = getServer()->ndef();
// If node doesn't exist, we don't know what callback to call
@@ -215,12 +239,9 @@ void ScriptApiNode::node_on_receive_fields(v3s16 p,
return;
// Call function
- // param 1
- push_v3s16(L, p);
- // param 2
- lua_pushstring(L, formname.c_str());
- // param 3
- lua_newtable(L);
+ push_v3s16(L, p); // pos
+ lua_pushstring(L, formname.c_str()); // formname
+ lua_newtable(L); // fields
for(std::map<std::string, std::string>::const_iterator
i = fields.begin(); i != fields.end(); i++){
const std::string &name = i->first;
@@ -229,26 +250,37 @@ void ScriptApiNode::node_on_receive_fields(v3s16 p,
lua_pushlstring(L, value.c_str(), value.size());
lua_settable(L, -3);
}
- // param 4
- objectrefGetOrCreate(sender);
- if(lua_pcall(L, 4, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ objectrefGetOrCreate(sender); // player
+ if(lua_pcall(L, 4, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
}
void ScriptApiNode::node_falling_update(v3s16 p)
{
SCRIPTAPI_PRECHECKHEADER
+
+ lua_pushcfunction(L, script_error_handler);
+ int errorhandler = lua_gettop(L);
+
lua_getglobal(L, "nodeupdate");
push_v3s16(L, p);
- if(lua_pcall(L, 1, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 1, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
}
void ScriptApiNode::node_falling_update_single(v3s16 p)
{
SCRIPTAPI_PRECHECKHEADER
+
+ lua_pushcfunction(L, script_error_handler);
+ int errorhandler = lua_gettop(L);
+
lua_getglobal(L, "nodeupdate_single");
push_v3s16(L, p);
- if(lua_pcall(L, 1, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ if(lua_pcall(L, 1, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
}
+