]> git.lizzy.rs Git - minetest.git/blobdiff - src/script/cpp_api/s_nodemeta.cpp
Handle LuaErrors in Lua -> C++ calls on LuaJIT
[minetest.git] / src / script / cpp_api / s_nodemeta.cpp
index e87464c612a51d2857bac6e0d4570899bef36e39..d8fd4dcfe4166314cbbc63a3acedfb9798c6c680 100644 (file)
@@ -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);
+               throw LuaError(NULL, "allow_metadata_inventory_move should return a number");
+       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);
+               throw LuaError(NULL, "allow_metadata_inventory_put should return a number");
+       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);
+               throw LuaError(NULL, "allow_metadata_inventory_take should return a number");
+       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() {