From: Yuval Lifshitz Date: Sun, 14 May 2023 13:47:56 +0000 (+0000) Subject: rgw/lua: use lua_gettop instead of magic numbers X-Git-Tag: v19.0.0~909^2~2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=2c703f3f56929203b35de82ad3a31a796232bd25;p=ceph.git rgw/lua: use lua_gettop instead of magic numbers Signed-off-by: Yuval Lifshitz --- diff --git a/src/rgw/rgw_lua_utils.h b/src/rgw/rgw_lua_utils.h index cc77dae7a896..ba703f1c57d0 100644 --- a/src/rgw/rgw_lua_utils.h +++ b/src/rgw/rgw_lua_utils.h @@ -126,31 +126,42 @@ void create_metatable(lua_State* L, bool toplevel, Upvalues... upvalues) } // create metatable [[maybe_unused]] const auto rc = luaL_newmetatable(L, MetaTable::Name().c_str()); + const auto table_stack_pos = lua_gettop(L); + + // add "index" closure to metatable lua_pushliteral(L, "__index"); for (const auto upvalue : upvalue_arr) { lua_pushlightuserdata(L, upvalue); } lua_pushcclosure(L, MetaTable::IndexClosure, upvals_size); - lua_rawset(L, -3); + lua_rawset(L, table_stack_pos); + + // add "newindex" closure to metatable lua_pushliteral(L, "__newindex"); for (const auto upvalue : upvalue_arr) { lua_pushlightuserdata(L, upvalue); } lua_pushcclosure(L, MetaTable::NewIndexClosure, upvals_size); - lua_rawset(L, -3); + lua_rawset(L, table_stack_pos); + + // add "pairs" closure to metatable lua_pushliteral(L, "__pairs"); for (const auto upvalue : upvalue_arr) { lua_pushlightuserdata(L, upvalue); } lua_pushcclosure(L, MetaTable::PairsClosure, upvals_size); - lua_rawset(L, -3); + lua_rawset(L, table_stack_pos); + + // add "len" closure to metatable lua_pushliteral(L, "__len"); for (const auto upvalue : upvalue_arr) { lua_pushlightuserdata(L, upvalue); } lua_pushcclosure(L, MetaTable::LenClosure, upvals_size); - lua_rawset(L, -3); + lua_rawset(L, table_stack_pos); + // tie metatable and table + ceph_assert(lua_gettop(L) == table_stack_pos); lua_setmetatable(L, -2); }