]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
rgw/lua: use lua_gettop instead of magic numbers
authorYuval Lifshitz <ylifshit@redhat.com>
Sun, 14 May 2023 13:47:56 +0000 (13:47 +0000)
committerYuval Lifshitz <ylifshit@redhat.com>
Wed, 21 Jun 2023 08:25:14 +0000 (08:25 +0000)
Signed-off-by: Yuval Lifshitz <ylifshit@redhat.com>
src/rgw/rgw_lua_utils.h

index cc77dae7a896af214acf174be6586b84a4c4813d..ba703f1c57d0d31542f31db3cc13aabb0922e19c 100644 (file)
@@ -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);
 }