From 741df993ff33832d773536ed571c1a67ed93b5cb Mon Sep 17 00:00:00 2001
From: ShadowNinja <shadowninja@minetest.net>
Date: Thu, 2 Oct 2014 15:58:13 -0400
Subject: [PATCH] Fix object reference pushing functions when called from
 coroutines

---
 src/script/cpp_api/s_base.cpp      | 11 ++++-------
 src/script/cpp_api/s_base.h        |  4 ++--
 src/script/cpp_api/s_entity.cpp    |  8 ++++----
 src/script/cpp_api/s_env.cpp       |  2 +-
 src/script/cpp_api/s_inventory.cpp | 12 ++++++------
 src/script/cpp_api/s_item.cpp      | 12 ++++++------
 src/script/cpp_api/s_node.cpp      |  6 +++---
 src/script/cpp_api/s_nodemeta.cpp  | 12 ++++++------
 src/script/cpp_api/s_player.cpp    | 14 +++++++-------
 src/script/lua_api/l_env.cpp       |  6 +++---
 10 files changed, 42 insertions(+), 45 deletions(-)

diff --git a/src/script/cpp_api/s_base.cpp b/src/script/cpp_api/s_base.cpp
index d27506fbe..779819007 100644
--- a/src/script/cpp_api/s_base.cpp
+++ b/src/script/cpp_api/s_base.cpp
@@ -238,22 +238,18 @@ void ScriptApiBase::removeObjectReference(ServerActiveObject *cobj)
 }
 
 // Creates a new anonymous reference if cobj=NULL or id=0
-void ScriptApiBase::objectrefGetOrCreate(
+void ScriptApiBase::objectrefGetOrCreate(lua_State *L,
 		ServerActiveObject *cobj)
 {
-	lua_State *L = getStack();
-
 	if(cobj == NULL || cobj->getId() == 0){
 		ObjectRef::create(L, cobj);
 	} else {
-		objectrefGet(cobj->getId());
+		objectrefGet(L, cobj->getId());
 	}
 }
 
-void ScriptApiBase::objectrefGet(u16 id)
+void ScriptApiBase::objectrefGet(lua_State *L, u16 id)
 {
-	lua_State *L = getStack();
-
 	// Get core.object_refs[i]
 	lua_getglobal(L, "core");
 	lua_getfield(L, -1, "object_refs");
@@ -263,3 +259,4 @@ void ScriptApiBase::objectrefGet(u16 id)
 	lua_remove(L, -2); // object_refs
 	lua_remove(L, -2); // core
 }
+
diff --git a/src/script/cpp_api/s_base.h b/src/script/cpp_api/s_base.h
index 8520c262d..4ea3677a9 100644
--- a/src/script/cpp_api/s_base.h
+++ b/src/script/cpp_api/s_base.h
@@ -78,8 +78,8 @@ class ScriptApiBase {
 	GUIEngine* getGuiEngine() { return m_guiengine; }
 	void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
 
-	void objectrefGetOrCreate(ServerActiveObject *cobj);
-	void objectrefGet(u16 id);
+	void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
+	void objectrefGet(lua_State *L, u16 id);
 
 	JMutex          m_luastackmutex;
 	// Stack index of Lua error handler
diff --git a/src/script/cpp_api/s_entity.cpp b/src/script/cpp_api/s_entity.cpp
index ab8c6ee95..b52bde18a 100644
--- a/src/script/cpp_api/s_entity.cpp
+++ b/src/script/cpp_api/s_entity.cpp
@@ -56,7 +56,7 @@ bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
 
 	// Add object reference
 	// This should be userdata with metatable ObjectRef
-	objectrefGet(id);
+	objectrefGet(L, id);
 	luaL_checktype(L, -1, LUA_TUSERDATA);
 	if (!luaL_checkudata(L, -1, "ObjectRef"))
 		luaL_typerror(L, -1, "ObjectRef");
@@ -236,8 +236,8 @@ void ScriptApiEntity::luaentity_Punch(u16 id,
 		return;
 	}
 	luaL_checktype(L, -1, LUA_TFUNCTION);
-	lua_pushvalue(L, object);                // self
-	objectrefGetOrCreate(puncher);           // Clicker reference
+	lua_pushvalue(L, object);  // self
+	objectrefGetOrCreate(L, puncher);  // Clicker reference
 	lua_pushnumber(L, time_from_last_punch);
 	push_tool_capabilities(L, *toolcap);
 	push_v3f(L, dir);
@@ -267,7 +267,7 @@ void ScriptApiEntity::luaentity_Rightclick(u16 id,
 	}
 	luaL_checktype(L, -1, LUA_TFUNCTION);
 	lua_pushvalue(L, object); // self
-	objectrefGetOrCreate(clicker); // Clicker reference
+	objectrefGetOrCreate(L, clicker); // Clicker reference
 	// Call with 2 arguments, 0 results
 	if (lua_pcall(L, 2, 0, m_errorhandler))
 		scriptError();
diff --git a/src/script/cpp_api/s_env.cpp b/src/script/cpp_api/s_env.cpp
index 4b5feee96..2fe7d8074 100644
--- a/src/script/cpp_api/s_env.cpp
+++ b/src/script/cpp_api/s_env.cpp
@@ -70,7 +70,7 @@ void ScriptApiEnv::player_event(ServerActiveObject* player, std::string type)
 	lua_getfield(L, -1, "registered_playerevents");
 
 	// Call callbacks
-	objectrefGetOrCreate(player);   // player
+	objectrefGetOrCreate(L, player);   // player
 	lua_pushstring(L,type.c_str()); // event type
 	try {
 		script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_FIRST);
diff --git a/src/script/cpp_api/s_inventory.cpp b/src/script/cpp_api/s_inventory.cpp
index f423a9f33..422faf150 100644
--- a/src/script/cpp_api/s_inventory.cpp
+++ b/src/script/cpp_api/s_inventory.cpp
@@ -47,7 +47,7 @@ int ScriptApiDetached::detached_inventory_AllowMove(
 	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
+	objectrefGetOrCreate(L, player);      // player
 	if (lua_pcall(L, 7, 1, m_errorhandler))
 		scriptError();
 	if(!lua_isnumber(L, -1))
@@ -76,7 +76,7 @@ int ScriptApiDetached::detached_inventory_AllowPut(
 	lua_pushstring(L, listname.c_str()); // listname
 	lua_pushinteger(L, index + 1);       // index
 	LuaItemStack::create(L, stack);      // stack
-	objectrefGetOrCreate(player);        // player
+	objectrefGetOrCreate(L, player);     // player
 	if (lua_pcall(L, 5, 1, m_errorhandler))
 		scriptError();
 	if (!lua_isnumber(L, -1))
@@ -105,7 +105,7 @@ int ScriptApiDetached::detached_inventory_AllowTake(
 	lua_pushstring(L, listname.c_str()); // listname
 	lua_pushinteger(L, index + 1);       // index
 	LuaItemStack::create(L, stack);      // stack
-	objectrefGetOrCreate(player);        // player
+	objectrefGetOrCreate(L, player);     // player
 	if (lua_pcall(L, 5, 1, m_errorhandler))
 		scriptError();
 	if (!lua_isnumber(L, -1))
@@ -138,7 +138,7 @@ void ScriptApiDetached::detached_inventory_OnMove(
 	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
+	objectrefGetOrCreate(L, player);      // player
 	if (lua_pcall(L, 7, 0, m_errorhandler))
 		scriptError();
 }
@@ -163,7 +163,7 @@ void ScriptApiDetached::detached_inventory_OnPut(
 	lua_pushstring(L, listname.c_str()); // listname
 	lua_pushinteger(L, index + 1);       // index
 	LuaItemStack::create(L, stack);      // stack
-	objectrefGetOrCreate(player);        // player
+	objectrefGetOrCreate(L, player);     // player
 	if (lua_pcall(L, 5, 0, m_errorhandler))
 		scriptError();
 }
@@ -188,7 +188,7 @@ void ScriptApiDetached::detached_inventory_OnTake(
 	lua_pushstring(L, listname.c_str()); // listname
 	lua_pushinteger(L, index + 1);       // index
 	LuaItemStack::create(L, stack);      // stack
-	objectrefGetOrCreate(player);        // player
+	objectrefGetOrCreate(L, player);     // player
 	if (lua_pcall(L, 5, 0, m_errorhandler))
 		scriptError();
 }
diff --git a/src/script/cpp_api/s_item.cpp b/src/script/cpp_api/s_item.cpp
index ab82b6b47..e3a9ac7aa 100644
--- a/src/script/cpp_api/s_item.cpp
+++ b/src/script/cpp_api/s_item.cpp
@@ -40,7 +40,7 @@ bool ScriptApiItem::item_OnDrop(ItemStack &item,
 
 	// Call function
 	LuaItemStack::create(L, item);
-	objectrefGetOrCreate(dropper);
+	objectrefGetOrCreate(L, dropper);
 	pushFloatPos(L, pos);
 	if (lua_pcall(L, 3, 1, m_errorhandler))
 		scriptError();
@@ -66,7 +66,7 @@ bool ScriptApiItem::item_OnPlace(ItemStack &item,
 
 	// Call function
 	LuaItemStack::create(L, item);
-	objectrefGetOrCreate(placer);
+	objectrefGetOrCreate(L, placer);
 	pushPointedThing(pointed);
 	if (lua_pcall(L, 3, 1, m_errorhandler))
 		scriptError();
@@ -92,7 +92,7 @@ bool ScriptApiItem::item_OnUse(ItemStack &item,
 
 	// Call function
 	LuaItemStack::create(L, item);
-	objectrefGetOrCreate(user);
+	objectrefGetOrCreate(L, user);
 	pushPointedThing(pointed);
 	if (lua_pcall(L, 3, 1, m_errorhandler))
 		scriptError();
@@ -115,7 +115,7 @@ bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user,
 	lua_getglobal(L, "core");
 	lua_getfield(L, -1, "on_craft");
 	LuaItemStack::create(L, item);
-	objectrefGetOrCreate(user);
+	objectrefGetOrCreate(L, user);
 	
 	// Push inventory list
 	std::vector<ItemStack> items;
@@ -146,7 +146,7 @@ bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user,
 	lua_getglobal(L, "core");
 	lua_getfield(L, -1, "craft_predict");
 	LuaItemStack::create(L, item);
-	objectrefGetOrCreate(user);
+	objectrefGetOrCreate(L, user);
 
 	//Push inventory list
 	std::vector<ItemStack> items;
@@ -229,7 +229,7 @@ void ScriptApiItem::pushPointedThing(const PointedThing& pointed)
 	{
 		lua_pushstring(L, "object");
 		lua_setfield(L, -2, "type");
-		objectrefGet(pointed.object_id);
+		objectrefGet(L, pointed.object_id);
 		lua_setfield(L, -2, "ref");
 	}
 	else
diff --git a/src/script/cpp_api/s_node.cpp b/src/script/cpp_api/s_node.cpp
index 8c9b46c2a..05f908004 100644
--- a/src/script/cpp_api/s_node.cpp
+++ b/src/script/cpp_api/s_node.cpp
@@ -103,7 +103,7 @@ bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
 	// Call function
 	push_v3s16(L, p);
 	pushnode(L, node, ndef);
-	objectrefGetOrCreate(puncher);
+	objectrefGetOrCreate(L, puncher);
 	pushPointedThing(pointed);
 	if (lua_pcall(L, 4, 0, m_errorhandler))
 		scriptError();
@@ -124,7 +124,7 @@ bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
 	// Call function
 	push_v3s16(L, p);
 	pushnode(L, node, ndef);
-	objectrefGetOrCreate(digger);
+	objectrefGetOrCreate(L, digger);
 	if (lua_pcall(L, 3, 0, m_errorhandler))
 		scriptError();
 	return true;
@@ -227,7 +227,7 @@ void ScriptApiNode::node_on_receive_fields(v3s16 p,
 		lua_pushlstring(L, value.c_str(), value.size());
 		lua_settable(L, -3);
 	}
-	objectrefGetOrCreate(sender);        // player
+	objectrefGetOrCreate(L, sender);        // player
 	if (lua_pcall(L, 4, 0, m_errorhandler))
 		scriptError();
 }
diff --git a/src/script/cpp_api/s_nodemeta.cpp b/src/script/cpp_api/s_nodemeta.cpp
index bf6a4f73b..01eee337c 100644
--- a/src/script/cpp_api/s_nodemeta.cpp
+++ b/src/script/cpp_api/s_nodemeta.cpp
@@ -53,7 +53,7 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowMove(v3s16 p,
 	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
+	objectrefGetOrCreate(L, player);      // player
 	if (lua_pcall(L, 7, 1, m_errorhandler))
 		scriptError();
 	if (!lua_isnumber(L, -1))
@@ -88,7 +88,7 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowPut(v3s16 p,
 	lua_pushstring(L, listname.c_str()); // listname
 	lua_pushinteger(L, index + 1);       // index
 	LuaItemStack::create(L, stack);      // stack
-	objectrefGetOrCreate(player);        // player
+	objectrefGetOrCreate(L, player);     // player
 	if (lua_pcall(L, 5, 1, m_errorhandler))
 		scriptError();
 	if(!lua_isnumber(L, -1))
@@ -123,7 +123,7 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowTake(v3s16 p,
 	lua_pushstring(L, listname.c_str()); // listname
 	lua_pushinteger(L, index + 1);       // index
 	LuaItemStack::create(L, stack);      // stack
-	objectrefGetOrCreate(player);        // player
+	objectrefGetOrCreate(L, player);     // player
 	if (lua_pcall(L, 5, 1, m_errorhandler))
 		scriptError();
 	if (!lua_isnumber(L, -1))
@@ -161,7 +161,7 @@ void ScriptApiNodemeta::nodemeta_inventory_OnMove(v3s16 p,
 	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
+	objectrefGetOrCreate(L, player);      // player
 	if (lua_pcall(L, 7, 0, m_errorhandler))
 		scriptError();
 }
@@ -190,7 +190,7 @@ void ScriptApiNodemeta::nodemeta_inventory_OnPut(v3s16 p,
 	lua_pushstring(L, listname.c_str()); // listname
 	lua_pushinteger(L, index + 1);       // index
 	LuaItemStack::create(L, stack);      // stack
-	objectrefGetOrCreate(player);        // player
+	objectrefGetOrCreate(L, player);     // player
 	if (lua_pcall(L, 5, 0, m_errorhandler))
 		scriptError();
 }
@@ -219,7 +219,7 @@ void ScriptApiNodemeta::nodemeta_inventory_OnTake(v3s16 p,
 	lua_pushstring(L, listname.c_str()); // listname
 	lua_pushinteger(L, index + 1);       // index
 	LuaItemStack::create(L, stack);      // stack
-	objectrefGetOrCreate(player);        // player
+	objectrefGetOrCreate(L, player);     // player
 	if (lua_pcall(L, 5, 0, m_errorhandler))
 		scriptError();
 }
diff --git a/src/script/cpp_api/s_player.cpp b/src/script/cpp_api/s_player.cpp
index d7375082a..81bfd4505 100644
--- a/src/script/cpp_api/s_player.cpp
+++ b/src/script/cpp_api/s_player.cpp
@@ -29,7 +29,7 @@ void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
 	lua_getglobal(L, "core");
 	lua_getfield(L, -1, "registered_on_newplayers");
 	// Call callbacks
-	objectrefGetOrCreate(player);
+	objectrefGetOrCreate(L, player);
 	script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
 }
 
@@ -41,7 +41,7 @@ void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player)
 	lua_getglobal(L, "core");
 	lua_getfield(L, -1, "registered_on_dieplayers");
 	// Call callbacks
-	objectrefGetOrCreate(player);
+	objectrefGetOrCreate(L, player);
 	script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
 }
 
@@ -53,7 +53,7 @@ bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
 	lua_getglobal(L, "core");
 	lua_getfield(L, -1, "registered_on_respawnplayers");
 	// Call callbacks
-	objectrefGetOrCreate(player);
+	objectrefGetOrCreate(L, player);
 	script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_OR);
 	bool positioning_handled_by_some = lua_toboolean(L, -1);
 	return positioning_handled_by_some;
@@ -84,7 +84,7 @@ void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
 	lua_getglobal(L, "core");
 	lua_getfield(L, -1, "registered_on_joinplayers");
 	// Call callbacks
-	objectrefGetOrCreate(player);
+	objectrefGetOrCreate(L, player);
 	script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
 }
 
@@ -96,7 +96,7 @@ void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player)
 	lua_getglobal(L, "core");
 	lua_getfield(L, -1, "registered_on_leaveplayers");
 	// Call callbacks
-	objectrefGetOrCreate(player);
+	objectrefGetOrCreate(L, player);
 	script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
 }
 
@@ -109,7 +109,7 @@ void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
 	lua_getglobal(L, "core");
 	lua_getfield(L, -1, "registered_on_cheats");
 	// Call callbacks
-	objectrefGetOrCreate(player);
+	objectrefGetOrCreate(L, player);
 	lua_newtable(L);
 	lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
 	lua_setfield(L, -2, "type");
@@ -127,7 +127,7 @@ void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
 	lua_getfield(L, -1, "registered_on_player_receive_fields");
 	// Call callbacks
 	// param 1
-	objectrefGetOrCreate(player);
+	objectrefGetOrCreate(L, player);
 	// param 2
 	lua_pushstring(L, formname.c_str());
 	// param 3
diff --git a/src/script/lua_api/l_env.cpp b/src/script/lua_api/l_env.cpp
index fedaccd0f..7c7a68b7e 100644
--- a/src/script/lua_api/l_env.cpp
+++ b/src/script/lua_api/l_env.cpp
@@ -375,7 +375,7 @@ int ModApiEnvMod::l_add_entity(lua_State *L)
 	if(objectid == 0)
 		return 0;
 	// Return ObjectRef
-	getScriptApiBase(L)->objectrefGetOrCreate(obj);
+	getScriptApiBase(L)->objectrefGetOrCreate(L, obj);
 	return 1;
 }
 
@@ -440,7 +440,7 @@ int ModApiEnvMod::l_get_player_by_name(lua_State *L)
 		return 1;
 	}
 	// Put player on stack
-	getScriptApiBase(L)->objectrefGetOrCreate(sao);
+	getScriptApiBase(L)->objectrefGetOrCreate(L, sao);
 	return 1;
 }
 
@@ -459,7 +459,7 @@ int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L)
 	for(u32 i = 0; iter != ids.end(); iter++) {
 		ServerActiveObject *obj = env->getActiveObject(*iter);
 		// Insert object reference into table
-		script->objectrefGetOrCreate(obj);
+		script->objectrefGetOrCreate(L, obj);
 		lua_rawseti(L, -2, ++i);
 	}
 	return 1;
-- 
GitLab