From 371b39a09a0bf248d674fae718f5ff369e895b66 Mon Sep 17 00:00:00 2001 From: ShadowNinja Date: Tue, 5 Nov 2013 12:06:15 -0500 Subject: Pass a errfunc to lua_pcall to get a traceback --- src/script/cpp_api/s_node.cpp | 92 +++++++++++++++++++++++++++++-------------- 1 file changed, 62 insertions(+), 30 deletions(-) (limited to 'src/script/cpp_api/s_node.cpp') 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::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 } + -- cgit v1.2.3