Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix object reference pushing functions when called from coroutines
  • Loading branch information
ShadowNinja committed Oct 7, 2014
1 parent 28438bb commit 741df99
Show file tree
Hide file tree
Showing 10 changed files with 42 additions and 45 deletions.
11 changes: 4 additions & 7 deletions src/script/cpp_api/s_base.cpp
Expand Up @@ -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");
Expand All @@ -263,3 +259,4 @@ void ScriptApiBase::objectrefGet(u16 id)
lua_remove(L, -2); // object_refs
lua_remove(L, -2); // core
}

4 changes: 2 additions & 2 deletions src/script/cpp_api/s_base.h
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions src/script/cpp_api/s_entity.cpp
Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/script/cpp_api/s_env.cpp
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/script/cpp_api/s_inventory.cpp
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand Down
12 changes: 6 additions & 6 deletions src/script/cpp_api/s_item.cpp
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/script/cpp_api/s_node.cpp
Expand Up @@ -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();
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
12 changes: 6 additions & 6 deletions src/script/cpp_api/s_nodemeta.cpp
Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down
14 changes: 7 additions & 7 deletions src/script/cpp_api/s_player.cpp
Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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;
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand All @@ -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");
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/script/lua_api/l_env.cpp
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand Down

0 comments on commit 741df99

Please sign in to comment.