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_item.cpp | 70 +++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 29 deletions(-) (limited to 'src/script/cpp_api/s_item.cpp') diff --git a/src/script/cpp_api/s_item.cpp b/src/script/cpp_api/s_item.cpp index 1d5f218cf..49729e57b 100644 --- a/src/script/cpp_api/s_item.cpp +++ b/src/script/cpp_api/s_item.cpp @@ -34,6 +34,9 @@ bool ScriptApiItem::item_OnDrop(ItemStack &item, { SCRIPTAPI_PRECHECKHEADER + lua_pushcfunction(L, script_error_handler); + int errorhandler = lua_gettop(L); + // Push callback function on stack if(!getItemCallback(item.name.c_str(), "on_drop")) return false; @@ -42,10 +45,11 @@ bool ScriptApiItem::item_OnDrop(ItemStack &item, LuaItemStack::create(L, item); objectrefGetOrCreate(dropper); pushFloatPos(L, pos); - if(lua_pcall(L, 3, 1, 0)) - scriptError("error: %s", lua_tostring(L, -1)); + if(lua_pcall(L, 3, 1, errorhandler)) + scriptError(); if(!lua_isnil(L, -1)) item = read_item(L,-1, getServer()); + lua_pop(L, 2); // Pop item and error handler return true; } @@ -54,6 +58,9 @@ bool ScriptApiItem::item_OnPlace(ItemStack &item, { SCRIPTAPI_PRECHECKHEADER + lua_pushcfunction(L, script_error_handler); + int errorhandler = lua_gettop(L); + // Push callback function on stack if(!getItemCallback(item.name.c_str(), "on_place")) return false; @@ -62,10 +69,11 @@ bool ScriptApiItem::item_OnPlace(ItemStack &item, LuaItemStack::create(L, item); objectrefGetOrCreate(placer); pushPointedThing(pointed); - if(lua_pcall(L, 3, 1, 0)) - scriptError("error: %s", lua_tostring(L, -1)); + if(lua_pcall(L, 3, 1, errorhandler)) + scriptError(); if(!lua_isnil(L, -1)) item = read_item(L,-1, getServer()); + lua_pop(L, 2); // Pop item and error handler return true; } @@ -74,6 +82,9 @@ bool ScriptApiItem::item_OnUse(ItemStack &item, { SCRIPTAPI_PRECHECKHEADER + lua_pushcfunction(L, script_error_handler); + int errorhandler = lua_gettop(L); + // Push callback function on stack if(!getItemCallback(item.name.c_str(), "on_use")) return false; @@ -82,10 +93,11 @@ bool ScriptApiItem::item_OnUse(ItemStack &item, LuaItemStack::create(L, item); objectrefGetOrCreate(user); pushPointedThing(pointed); - if(lua_pcall(L, 3, 1, 0)) - scriptError("error: %s", lua_tostring(L, -1)); + if(lua_pcall(L, 3, 1, errorhandler)) + scriptError(); if(!lua_isnil(L, -1)) item = read_item(L,-1, getServer()); + lua_pop(L, 2); // Pop item and error handler return true; } @@ -94,6 +106,9 @@ bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user, { SCRIPTAPI_PRECHECKHEADER + lua_pushcfunction(L, script_error_handler); + int errorhandler = lua_gettop(L); + lua_getglobal(L, "minetest"); lua_getfield(L, -1, "on_craft"); LuaItemStack::create(L, item); @@ -106,10 +121,11 @@ bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user, push_items(L, items); InvRef::create(L, craft_inv); - if(lua_pcall(L, 4, 1, 0)) - scriptError("error: %s", lua_tostring(L, -1)); + if(lua_pcall(L, 4, 1, errorhandler)) + scriptError(); if(!lua_isnil(L, -1)) item = read_item(L,-1, getServer()); + lua_pop(L, 2); // Pop item and error handler return true; } @@ -118,11 +134,14 @@ bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user, { SCRIPTAPI_PRECHECKHEADER + lua_pushcfunction(L, script_error_handler); + int errorhandler = lua_gettop(L); + lua_getglobal(L, "minetest"); lua_getfield(L, -1, "craft_predict"); LuaItemStack::create(L, item); objectrefGetOrCreate(user); - + //Push inventory list std::vector items; for(u32 i=0; igetSize(); i++) @@ -130,10 +149,11 @@ bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user, push_items(L, items); InvRef::create(L, craft_inv); - if(lua_pcall(L, 4, 1, 0)) - scriptError("error: %s", lua_tostring(L, -1)); + if(lua_pcall(L, 4, 1, errorhandler)) + scriptError(); if(!lua_isnil(L, -1)) item = read_item(L,-1, getServer()); + lua_pop(L, 2); // Pop item and error handler return true; } @@ -149,15 +169,15 @@ bool ScriptApiItem::getItemCallback(const char *name, const char *callbackname) lua_getglobal(L, "minetest"); lua_getfield(L, -1, "registered_items"); - lua_remove(L, -2); + lua_remove(L, -2); // Remove minetest luaL_checktype(L, -1, LUA_TTABLE); lua_getfield(L, -1, name); - lua_remove(L, -2); + lua_remove(L, -2); // Remove registered_items // Should be a table if(lua_type(L, -1) != LUA_TTABLE) { // Report error and clean up - errorstream<<"Item \""<