aboutsummaryrefslogtreecommitdiff
path: root/src/script/cpp_api/s_nodemeta.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_nodemeta.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_nodemeta.cpp')
-rw-r--r--src/script/cpp_api/s_nodemeta.cpp162
1 files changed, 79 insertions, 83 deletions
diff --git a/src/script/cpp_api/s_nodemeta.cpp b/src/script/cpp_api/s_nodemeta.cpp
index e87464c61..1f04383f1 100644
--- a/src/script/cpp_api/s_nodemeta.cpp
+++ b/src/script/cpp_api/s_nodemeta.cpp
@@ -34,6 +34,9 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowMove(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
@@ -47,25 +50,21 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowMove(v3s16 p,
return count;
// function(pos, from_list, from_index, to_list, to_index, count, player)
- // pos
- push_v3s16(L, p);
- // from_list
- lua_pushstring(L, from_list.c_str());
- // from_index
- lua_pushinteger(L, from_index + 1);
- // to_list
- lua_pushstring(L, to_list.c_str());
- // to_index
- lua_pushinteger(L, to_index + 1);
- // count
- lua_pushinteger(L, count);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 7, 1, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ push_v3s16(L, p); // pos
+ lua_pushstring(L, from_list.c_str()); // from_list
+ lua_pushinteger(L, from_index + 1); // from_index
+ lua_pushstring(L, to_list.c_str()); // to_list
+ lua_pushinteger(L, to_index + 1); // to_index
+ lua_pushinteger(L, count); // count
+ objectrefGetOrCreate(player); // player
+ if(lua_pcall(L, 7, 1, errorhandler))
+ scriptError();
+ lua_remove(L, errorhandler); // Remove error handler
if(!lua_isnumber(L, -1))
throw LuaError(L, "allow_metadata_inventory_move should return a number");
- return luaL_checkinteger(L, -1);
+ int num = luaL_checkinteger(L, -1);
+ lua_pop(L, 1); // Pop integer
+ return num;
}
// Return number of accepted items to be put
@@ -75,6 +74,9 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowPut(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
@@ -88,21 +90,19 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowPut(v3s16 p,
return stack.count;
// Call function(pos, listname, index, stack, player)
- // pos
- push_v3s16(L, p);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 5, 1, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ push_v3s16(L, p); // pos
+ lua_pushstring(L, listname.c_str()); // listname
+ lua_pushinteger(L, index + 1); // index
+ LuaItemStack::create(L, stack); // stack
+ objectrefGetOrCreate(player); // player
+ if(lua_pcall(L, 5, 1, errorhandler))
+ scriptError();
+ lua_remove(L, errorhandler); // Remove error handler
if(!lua_isnumber(L, -1))
throw LuaError(L, "allow_metadata_inventory_put should return a number");
- return luaL_checkinteger(L, -1);
+ int num = luaL_checkinteger(L, -1);
+ lua_pop(L, 1); // Pop integer
+ return num;
}
// Return number of accepted items to be taken
@@ -112,6 +112,9 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowTake(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
@@ -125,21 +128,19 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowTake(v3s16 p,
return stack.count;
// Call function(pos, listname, index, count, player)
- // pos
- push_v3s16(L, p);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 5, 1, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ push_v3s16(L, p); // pos
+ lua_pushstring(L, listname.c_str()); // listname
+ lua_pushinteger(L, index + 1); // index
+ LuaItemStack::create(L, stack); // stack
+ objectrefGetOrCreate(player); // player
+ if(lua_pcall(L, 5, 1, errorhandler))
+ scriptError();
+ lua_remove(L, errorhandler); // Remove error handler
if(!lua_isnumber(L, -1))
throw LuaError(L, "allow_metadata_inventory_take should return a number");
- return luaL_checkinteger(L, -1);
+ int num = luaL_checkinteger(L, -1);
+ lua_pop(L, 1); // Pop integer
+ return num;
}
// Report moved items
@@ -150,6 +151,9 @@ void ScriptApiNodemeta::nodemeta_inventory_OnMove(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
@@ -163,22 +167,16 @@ void ScriptApiNodemeta::nodemeta_inventory_OnMove(v3s16 p,
return;
// function(pos, from_list, from_index, to_list, to_index, count, player)
- // pos
- push_v3s16(L, p);
- // from_list
- lua_pushstring(L, from_list.c_str());
- // from_index
- lua_pushinteger(L, from_index + 1);
- // to_list
- lua_pushstring(L, to_list.c_str());
- // to_index
- lua_pushinteger(L, to_index + 1);
- // count
- lua_pushinteger(L, count);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 7, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ push_v3s16(L, p); // pos
+ lua_pushstring(L, from_list.c_str()); // from_list
+ lua_pushinteger(L, from_index + 1); // from_index
+ lua_pushstring(L, to_list.c_str()); // to_list
+ lua_pushinteger(L, to_index + 1); // to_index
+ lua_pushinteger(L, count); // count
+ objectrefGetOrCreate(player); // player
+ if(lua_pcall(L, 7, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
}
// Report put items
@@ -188,6 +186,9 @@ void ScriptApiNodemeta::nodemeta_inventory_OnPut(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
@@ -201,18 +202,14 @@ void ScriptApiNodemeta::nodemeta_inventory_OnPut(v3s16 p,
return;
// Call function(pos, listname, index, stack, player)
- // pos
- push_v3s16(L, p);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 5, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ push_v3s16(L, p); // pos
+ lua_pushstring(L, listname.c_str()); // listname
+ lua_pushinteger(L, index + 1); // index
+ LuaItemStack::create(L, stack); // stack
+ objectrefGetOrCreate(player); // player
+ if(lua_pcall(L, 5, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
}
// Report taken items
@@ -222,6 +219,9 @@ void ScriptApiNodemeta::nodemeta_inventory_OnTake(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
@@ -235,18 +235,14 @@ void ScriptApiNodemeta::nodemeta_inventory_OnTake(v3s16 p,
return;
// Call function(pos, listname, index, stack, player)
- // pos
- push_v3s16(L, p);
- // listname
- lua_pushstring(L, listname.c_str());
- // index
- lua_pushinteger(L, index + 1);
- // stack
- LuaItemStack::create(L, stack);
- // player
- objectrefGetOrCreate(player);
- if(lua_pcall(L, 5, 0, 0))
- scriptError("error: %s", lua_tostring(L, -1));
+ push_v3s16(L, p); // pos
+ lua_pushstring(L, listname.c_str()); // listname
+ lua_pushinteger(L, index + 1); // index
+ LuaItemStack::create(L, stack); // stack
+ objectrefGetOrCreate(player); // player
+ if(lua_pcall(L, 5, 0, errorhandler))
+ scriptError();
+ lua_pop(L, 1); // Pop error handler
}
ScriptApiNodemeta::ScriptApiNodemeta() {