Skip to content

Commit 741df99

Browse files
committedOct 7, 2014
Fix object reference pushing functions when called from coroutines
1 parent 28438bb commit 741df99

File tree

10 files changed

+42
-45
lines changed

10 files changed

+42
-45
lines changed
 

‎src/script/cpp_api/s_base.cpp

+4-7
Original file line numberDiff line numberDiff line change
@@ -238,22 +238,18 @@ void ScriptApiBase::removeObjectReference(ServerActiveObject *cobj)
238238
}
239239

240240
// Creates a new anonymous reference if cobj=NULL or id=0
241-
void ScriptApiBase::objectrefGetOrCreate(
241+
void ScriptApiBase::objectrefGetOrCreate(lua_State *L,
242242
ServerActiveObject *cobj)
243243
{
244-
lua_State *L = getStack();
245-
246244
if(cobj == NULL || cobj->getId() == 0){
247245
ObjectRef::create(L, cobj);
248246
} else {
249-
objectrefGet(cobj->getId());
247+
objectrefGet(L, cobj->getId());
250248
}
251249
}
252250

253-
void ScriptApiBase::objectrefGet(u16 id)
251+
void ScriptApiBase::objectrefGet(lua_State *L, u16 id)
254252
{
255-
lua_State *L = getStack();
256-
257253
// Get core.object_refs[i]
258254
lua_getglobal(L, "core");
259255
lua_getfield(L, -1, "object_refs");
@@ -263,3 +259,4 @@ void ScriptApiBase::objectrefGet(u16 id)
263259
lua_remove(L, -2); // object_refs
264260
lua_remove(L, -2); // core
265261
}
262+

‎src/script/cpp_api/s_base.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ class ScriptApiBase {
7878
GUIEngine* getGuiEngine() { return m_guiengine; }
7979
void setGuiEngine(GUIEngine* guiengine) { m_guiengine = guiengine; }
8080

81-
void objectrefGetOrCreate(ServerActiveObject *cobj);
82-
void objectrefGet(u16 id);
81+
void objectrefGetOrCreate(lua_State *L, ServerActiveObject *cobj);
82+
void objectrefGet(lua_State *L, u16 id);
8383

8484
JMutex m_luastackmutex;
8585
// Stack index of Lua error handler

‎src/script/cpp_api/s_entity.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ bool ScriptApiEntity::luaentity_Add(u16 id, const char *name)
5656

5757
// Add object reference
5858
// This should be userdata with metatable ObjectRef
59-
objectrefGet(id);
59+
objectrefGet(L, id);
6060
luaL_checktype(L, -1, LUA_TUSERDATA);
6161
if (!luaL_checkudata(L, -1, "ObjectRef"))
6262
luaL_typerror(L, -1, "ObjectRef");
@@ -236,8 +236,8 @@ void ScriptApiEntity::luaentity_Punch(u16 id,
236236
return;
237237
}
238238
luaL_checktype(L, -1, LUA_TFUNCTION);
239-
lua_pushvalue(L, object); // self
240-
objectrefGetOrCreate(puncher); // Clicker reference
239+
lua_pushvalue(L, object); // self
240+
objectrefGetOrCreate(L, puncher); // Clicker reference
241241
lua_pushnumber(L, time_from_last_punch);
242242
push_tool_capabilities(L, *toolcap);
243243
push_v3f(L, dir);
@@ -267,7 +267,7 @@ void ScriptApiEntity::luaentity_Rightclick(u16 id,
267267
}
268268
luaL_checktype(L, -1, LUA_TFUNCTION);
269269
lua_pushvalue(L, object); // self
270-
objectrefGetOrCreate(clicker); // Clicker reference
270+
objectrefGetOrCreate(L, clicker); // Clicker reference
271271
// Call with 2 arguments, 0 results
272272
if (lua_pcall(L, 2, 0, m_errorhandler))
273273
scriptError();

‎src/script/cpp_api/s_env.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ void ScriptApiEnv::player_event(ServerActiveObject* player, std::string type)
7070
lua_getfield(L, -1, "registered_playerevents");
7171

7272
// Call callbacks
73-
objectrefGetOrCreate(player); // player
73+
objectrefGetOrCreate(L, player); // player
7474
lua_pushstring(L,type.c_str()); // event type
7575
try {
7676
script_run_callbacks(L, 2, RUN_CALLBACKS_MODE_FIRST);

‎src/script/cpp_api/s_inventory.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ int ScriptApiDetached::detached_inventory_AllowMove(
4747
lua_pushstring(L, to_list.c_str()); // to_list
4848
lua_pushinteger(L, to_index + 1); // to_index
4949
lua_pushinteger(L, count); // count
50-
objectrefGetOrCreate(player); // player
50+
objectrefGetOrCreate(L, player); // player
5151
if (lua_pcall(L, 7, 1, m_errorhandler))
5252
scriptError();
5353
if(!lua_isnumber(L, -1))
@@ -76,7 +76,7 @@ int ScriptApiDetached::detached_inventory_AllowPut(
7676
lua_pushstring(L, listname.c_str()); // listname
7777
lua_pushinteger(L, index + 1); // index
7878
LuaItemStack::create(L, stack); // stack
79-
objectrefGetOrCreate(player); // player
79+
objectrefGetOrCreate(L, player); // player
8080
if (lua_pcall(L, 5, 1, m_errorhandler))
8181
scriptError();
8282
if (!lua_isnumber(L, -1))
@@ -105,7 +105,7 @@ int ScriptApiDetached::detached_inventory_AllowTake(
105105
lua_pushstring(L, listname.c_str()); // listname
106106
lua_pushinteger(L, index + 1); // index
107107
LuaItemStack::create(L, stack); // stack
108-
objectrefGetOrCreate(player); // player
108+
objectrefGetOrCreate(L, player); // player
109109
if (lua_pcall(L, 5, 1, m_errorhandler))
110110
scriptError();
111111
if (!lua_isnumber(L, -1))
@@ -138,7 +138,7 @@ void ScriptApiDetached::detached_inventory_OnMove(
138138
lua_pushstring(L, to_list.c_str()); // to_list
139139
lua_pushinteger(L, to_index + 1); // to_index
140140
lua_pushinteger(L, count); // count
141-
objectrefGetOrCreate(player); // player
141+
objectrefGetOrCreate(L, player); // player
142142
if (lua_pcall(L, 7, 0, m_errorhandler))
143143
scriptError();
144144
}
@@ -163,7 +163,7 @@ void ScriptApiDetached::detached_inventory_OnPut(
163163
lua_pushstring(L, listname.c_str()); // listname
164164
lua_pushinteger(L, index + 1); // index
165165
LuaItemStack::create(L, stack); // stack
166-
objectrefGetOrCreate(player); // player
166+
objectrefGetOrCreate(L, player); // player
167167
if (lua_pcall(L, 5, 0, m_errorhandler))
168168
scriptError();
169169
}
@@ -188,7 +188,7 @@ void ScriptApiDetached::detached_inventory_OnTake(
188188
lua_pushstring(L, listname.c_str()); // listname
189189
lua_pushinteger(L, index + 1); // index
190190
LuaItemStack::create(L, stack); // stack
191-
objectrefGetOrCreate(player); // player
191+
objectrefGetOrCreate(L, player); // player
192192
if (lua_pcall(L, 5, 0, m_errorhandler))
193193
scriptError();
194194
}

‎src/script/cpp_api/s_item.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ bool ScriptApiItem::item_OnDrop(ItemStack &item,
4040

4141
// Call function
4242
LuaItemStack::create(L, item);
43-
objectrefGetOrCreate(dropper);
43+
objectrefGetOrCreate(L, dropper);
4444
pushFloatPos(L, pos);
4545
if (lua_pcall(L, 3, 1, m_errorhandler))
4646
scriptError();
@@ -66,7 +66,7 @@ bool ScriptApiItem::item_OnPlace(ItemStack &item,
6666

6767
// Call function
6868
LuaItemStack::create(L, item);
69-
objectrefGetOrCreate(placer);
69+
objectrefGetOrCreate(L, placer);
7070
pushPointedThing(pointed);
7171
if (lua_pcall(L, 3, 1, m_errorhandler))
7272
scriptError();
@@ -92,7 +92,7 @@ bool ScriptApiItem::item_OnUse(ItemStack &item,
9292

9393
// Call function
9494
LuaItemStack::create(L, item);
95-
objectrefGetOrCreate(user);
95+
objectrefGetOrCreate(L, user);
9696
pushPointedThing(pointed);
9797
if (lua_pcall(L, 3, 1, m_errorhandler))
9898
scriptError();
@@ -115,7 +115,7 @@ bool ScriptApiItem::item_OnCraft(ItemStack &item, ServerActiveObject *user,
115115
lua_getglobal(L, "core");
116116
lua_getfield(L, -1, "on_craft");
117117
LuaItemStack::create(L, item);
118-
objectrefGetOrCreate(user);
118+
objectrefGetOrCreate(L, user);
119119

120120
// Push inventory list
121121
std::vector<ItemStack> items;
@@ -146,7 +146,7 @@ bool ScriptApiItem::item_CraftPredict(ItemStack &item, ServerActiveObject *user,
146146
lua_getglobal(L, "core");
147147
lua_getfield(L, -1, "craft_predict");
148148
LuaItemStack::create(L, item);
149-
objectrefGetOrCreate(user);
149+
objectrefGetOrCreate(L, user);
150150

151151
//Push inventory list
152152
std::vector<ItemStack> items;
@@ -229,7 +229,7 @@ void ScriptApiItem::pushPointedThing(const PointedThing& pointed)
229229
{
230230
lua_pushstring(L, "object");
231231
lua_setfield(L, -2, "type");
232-
objectrefGet(pointed.object_id);
232+
objectrefGet(L, pointed.object_id);
233233
lua_setfield(L, -2, "ref");
234234
}
235235
else

‎src/script/cpp_api/s_node.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ bool ScriptApiNode::node_on_punch(v3s16 p, MapNode node,
103103
// Call function
104104
push_v3s16(L, p);
105105
pushnode(L, node, ndef);
106-
objectrefGetOrCreate(puncher);
106+
objectrefGetOrCreate(L, puncher);
107107
pushPointedThing(pointed);
108108
if (lua_pcall(L, 4, 0, m_errorhandler))
109109
scriptError();
@@ -124,7 +124,7 @@ bool ScriptApiNode::node_on_dig(v3s16 p, MapNode node,
124124
// Call function
125125
push_v3s16(L, p);
126126
pushnode(L, node, ndef);
127-
objectrefGetOrCreate(digger);
127+
objectrefGetOrCreate(L, digger);
128128
if (lua_pcall(L, 3, 0, m_errorhandler))
129129
scriptError();
130130
return true;
@@ -227,7 +227,7 @@ void ScriptApiNode::node_on_receive_fields(v3s16 p,
227227
lua_pushlstring(L, value.c_str(), value.size());
228228
lua_settable(L, -3);
229229
}
230-
objectrefGetOrCreate(sender); // player
230+
objectrefGetOrCreate(L, sender); // player
231231
if (lua_pcall(L, 4, 0, m_errorhandler))
232232
scriptError();
233233
}

‎src/script/cpp_api/s_nodemeta.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowMove(v3s16 p,
5353
lua_pushstring(L, to_list.c_str()); // to_list
5454
lua_pushinteger(L, to_index + 1); // to_index
5555
lua_pushinteger(L, count); // count
56-
objectrefGetOrCreate(player); // player
56+
objectrefGetOrCreate(L, player); // player
5757
if (lua_pcall(L, 7, 1, m_errorhandler))
5858
scriptError();
5959
if (!lua_isnumber(L, -1))
@@ -88,7 +88,7 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowPut(v3s16 p,
8888
lua_pushstring(L, listname.c_str()); // listname
8989
lua_pushinteger(L, index + 1); // index
9090
LuaItemStack::create(L, stack); // stack
91-
objectrefGetOrCreate(player); // player
91+
objectrefGetOrCreate(L, player); // player
9292
if (lua_pcall(L, 5, 1, m_errorhandler))
9393
scriptError();
9494
if(!lua_isnumber(L, -1))
@@ -123,7 +123,7 @@ int ScriptApiNodemeta::nodemeta_inventory_AllowTake(v3s16 p,
123123
lua_pushstring(L, listname.c_str()); // listname
124124
lua_pushinteger(L, index + 1); // index
125125
LuaItemStack::create(L, stack); // stack
126-
objectrefGetOrCreate(player); // player
126+
objectrefGetOrCreate(L, player); // player
127127
if (lua_pcall(L, 5, 1, m_errorhandler))
128128
scriptError();
129129
if (!lua_isnumber(L, -1))
@@ -161,7 +161,7 @@ void ScriptApiNodemeta::nodemeta_inventory_OnMove(v3s16 p,
161161
lua_pushstring(L, to_list.c_str()); // to_list
162162
lua_pushinteger(L, to_index + 1); // to_index
163163
lua_pushinteger(L, count); // count
164-
objectrefGetOrCreate(player); // player
164+
objectrefGetOrCreate(L, player); // player
165165
if (lua_pcall(L, 7, 0, m_errorhandler))
166166
scriptError();
167167
}
@@ -190,7 +190,7 @@ void ScriptApiNodemeta::nodemeta_inventory_OnPut(v3s16 p,
190190
lua_pushstring(L, listname.c_str()); // listname
191191
lua_pushinteger(L, index + 1); // index
192192
LuaItemStack::create(L, stack); // stack
193-
objectrefGetOrCreate(player); // player
193+
objectrefGetOrCreate(L, player); // player
194194
if (lua_pcall(L, 5, 0, m_errorhandler))
195195
scriptError();
196196
}
@@ -219,7 +219,7 @@ void ScriptApiNodemeta::nodemeta_inventory_OnTake(v3s16 p,
219219
lua_pushstring(L, listname.c_str()); // listname
220220
lua_pushinteger(L, index + 1); // index
221221
LuaItemStack::create(L, stack); // stack
222-
objectrefGetOrCreate(player); // player
222+
objectrefGetOrCreate(L, player); // player
223223
if (lua_pcall(L, 5, 0, m_errorhandler))
224224
scriptError();
225225
}

‎src/script/cpp_api/s_player.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void ScriptApiPlayer::on_newplayer(ServerActiveObject *player)
2929
lua_getglobal(L, "core");
3030
lua_getfield(L, -1, "registered_on_newplayers");
3131
// Call callbacks
32-
objectrefGetOrCreate(player);
32+
objectrefGetOrCreate(L, player);
3333
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
3434
}
3535

@@ -41,7 +41,7 @@ void ScriptApiPlayer::on_dieplayer(ServerActiveObject *player)
4141
lua_getglobal(L, "core");
4242
lua_getfield(L, -1, "registered_on_dieplayers");
4343
// Call callbacks
44-
objectrefGetOrCreate(player);
44+
objectrefGetOrCreate(L, player);
4545
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
4646
}
4747

@@ -53,7 +53,7 @@ bool ScriptApiPlayer::on_respawnplayer(ServerActiveObject *player)
5353
lua_getglobal(L, "core");
5454
lua_getfield(L, -1, "registered_on_respawnplayers");
5555
// Call callbacks
56-
objectrefGetOrCreate(player);
56+
objectrefGetOrCreate(L, player);
5757
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_OR);
5858
bool positioning_handled_by_some = lua_toboolean(L, -1);
5959
return positioning_handled_by_some;
@@ -84,7 +84,7 @@ void ScriptApiPlayer::on_joinplayer(ServerActiveObject *player)
8484
lua_getglobal(L, "core");
8585
lua_getfield(L, -1, "registered_on_joinplayers");
8686
// Call callbacks
87-
objectrefGetOrCreate(player);
87+
objectrefGetOrCreate(L, player);
8888
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
8989
}
9090

@@ -96,7 +96,7 @@ void ScriptApiPlayer::on_leaveplayer(ServerActiveObject *player)
9696
lua_getglobal(L, "core");
9797
lua_getfield(L, -1, "registered_on_leaveplayers");
9898
// Call callbacks
99-
objectrefGetOrCreate(player);
99+
objectrefGetOrCreate(L, player);
100100
script_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
101101
}
102102

@@ -109,7 +109,7 @@ void ScriptApiPlayer::on_cheat(ServerActiveObject *player,
109109
lua_getglobal(L, "core");
110110
lua_getfield(L, -1, "registered_on_cheats");
111111
// Call callbacks
112-
objectrefGetOrCreate(player);
112+
objectrefGetOrCreate(L, player);
113113
lua_newtable(L);
114114
lua_pushlstring(L, cheat_type.c_str(), cheat_type.size());
115115
lua_setfield(L, -2, "type");
@@ -127,7 +127,7 @@ void ScriptApiPlayer::on_playerReceiveFields(ServerActiveObject *player,
127127
lua_getfield(L, -1, "registered_on_player_receive_fields");
128128
// Call callbacks
129129
// param 1
130-
objectrefGetOrCreate(player);
130+
objectrefGetOrCreate(L, player);
131131
// param 2
132132
lua_pushstring(L, formname.c_str());
133133
// param 3

‎src/script/lua_api/l_env.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ int ModApiEnvMod::l_add_entity(lua_State *L)
375375
if(objectid == 0)
376376
return 0;
377377
// Return ObjectRef
378-
getScriptApiBase(L)->objectrefGetOrCreate(obj);
378+
getScriptApiBase(L)->objectrefGetOrCreate(L, obj);
379379
return 1;
380380
}
381381

@@ -440,7 +440,7 @@ int ModApiEnvMod::l_get_player_by_name(lua_State *L)
440440
return 1;
441441
}
442442
// Put player on stack
443-
getScriptApiBase(L)->objectrefGetOrCreate(sao);
443+
getScriptApiBase(L)->objectrefGetOrCreate(L, sao);
444444
return 1;
445445
}
446446

@@ -459,7 +459,7 @@ int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L)
459459
for(u32 i = 0; iter != ids.end(); iter++) {
460460
ServerActiveObject *obj = env->getActiveObject(*iter);
461461
// Insert object reference into table
462-
script->objectrefGetOrCreate(obj);
462+
script->objectrefGetOrCreate(L, obj);
463463
lua_rawseti(L, -2, ++i);
464464
}
465465
return 1;

0 commit comments

Comments
 (0)
Please sign in to comment.