1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-06-27 16:36:03 +00:00

Only push the Lua error handler once

This commit is contained in:
ShadowNinja 2014-04-15 13:30:46 -04:00
parent 1838a3fd69
commit db4ea4658c
9 changed files with 154 additions and 279 deletions

View file

@ -33,11 +33,8 @@ int ScriptApiDetached::detached_inventory_AllowMove(
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
// Push callback function on stack
if(!getDetachedInventoryCallback(name, "allow_move"))
if (!getDetachedInventoryCallback(name, "allow_move"))
return count;
// function(inv, from_list, from_index, to_list, to_index, count, player)
@ -51,12 +48,12 @@ int ScriptApiDetached::detached_inventory_AllowMove(
lua_pushinteger(L, to_index + 1); // to_index
lua_pushinteger(L, count); // count
objectrefGetOrCreate(player); // player
if(lua_pcall(L, 7, 1, errorhandler))
if (lua_pcall(L, 7, 1, m_errorhandler))
scriptError();
if(!lua_isnumber(L, -1))
throw LuaError("allow_move should return a number. name=" + name);
int ret = luaL_checkinteger(L, -1);
lua_pop(L, 2); // Pop integer and error handler
lua_pop(L, 1); // Pop integer
return ret;
}
@ -68,11 +65,8 @@ int ScriptApiDetached::detached_inventory_AllowPut(
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
// Push callback function on stack
if(!getDetachedInventoryCallback(name, "allow_put"))
if (!getDetachedInventoryCallback(name, "allow_put"))
return stack.count; // All will be accepted
// Call function(inv, listname, index, stack, player)
@ -83,12 +77,12 @@ int ScriptApiDetached::detached_inventory_AllowPut(
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
if(lua_pcall(L, 5, 1, errorhandler))
if (lua_pcall(L, 5, 1, m_errorhandler))
scriptError();
if(!lua_isnumber(L, -1))
if (!lua_isnumber(L, -1))
throw LuaError("allow_put should return a number. name=" + name);
int ret = luaL_checkinteger(L, -1);
lua_pop(L, 2); // Pop integer and error handler
lua_pop(L, 1); // Pop integer
return ret;
}
@ -100,11 +94,8 @@ int ScriptApiDetached::detached_inventory_AllowTake(
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
// Push callback function on stack
if(!getDetachedInventoryCallback(name, "allow_take"))
if (!getDetachedInventoryCallback(name, "allow_take"))
return stack.count; // All will be accepted
// Call function(inv, listname, index, stack, player)
@ -115,12 +106,12 @@ int ScriptApiDetached::detached_inventory_AllowTake(
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
if(lua_pcall(L, 5, 1, errorhandler))
if (lua_pcall(L, 5, 1, m_errorhandler))
scriptError();
if(!lua_isnumber(L, -1))
if (!lua_isnumber(L, -1))
throw LuaError("allow_take should return a number. name=" + name);
int ret = luaL_checkinteger(L, -1);
lua_pop(L, 2); // Pop integer and error handler
lua_pop(L, 1); // Pop integer
return ret;
}
@ -133,11 +124,8 @@ void ScriptApiDetached::detached_inventory_OnMove(
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
// Push callback function on stack
if(!getDetachedInventoryCallback(name, "on_move"))
if (!getDetachedInventoryCallback(name, "on_move"))
return;
// function(inv, from_list, from_index, to_list, to_index, count, player)
@ -151,9 +139,8 @@ void ScriptApiDetached::detached_inventory_OnMove(
lua_pushinteger(L, to_index + 1); // to_index
lua_pushinteger(L, count); // count
objectrefGetOrCreate(player); // player
if(lua_pcall(L, 7, 0, errorhandler))
if (lua_pcall(L, 7, 0, m_errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
}
// Report put items
@ -164,11 +151,8 @@ void ScriptApiDetached::detached_inventory_OnPut(
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
// Push callback function on stack
if(!getDetachedInventoryCallback(name, "on_put"))
if (!getDetachedInventoryCallback(name, "on_put"))
return;
// Call function(inv, listname, index, stack, player)
@ -180,9 +164,8 @@ void ScriptApiDetached::detached_inventory_OnPut(
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
if(lua_pcall(L, 5, 0, errorhandler))
if (lua_pcall(L, 5, 0, m_errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
}
// Report taken items
@ -193,11 +176,8 @@ void ScriptApiDetached::detached_inventory_OnTake(
{
SCRIPTAPI_PRECHECKHEADER
lua_pushcfunction(L, script_error_handler);
int errorhandler = lua_gettop(L);
// Push callback function on stack
if(!getDetachedInventoryCallback(name, "on_take"))
if (!getDetachedInventoryCallback(name, "on_take"))
return;
// Call function(inv, listname, index, stack, player)
@ -209,9 +189,8 @@ void ScriptApiDetached::detached_inventory_OnTake(
lua_pushinteger(L, index + 1); // index
LuaItemStack::create(L, stack); // stack
objectrefGetOrCreate(player); // player
if(lua_pcall(L, 5, 0, errorhandler))
if (lua_pcall(L, 5, 0, m_errorhandler))
scriptError();
lua_pop(L, 1); // Pop error handler
}
// Retrieves minetest.detached_inventories[name][callbackname]