diff --git a/Source/LuaBridge/detail/Namespace.h b/Source/LuaBridge/detail/Namespace.h index 32ee0449..b680e916 100644 --- a/Source/LuaBridge/detail/Namespace.h +++ b/Source/LuaBridge/detail/Namespace.h @@ -347,9 +347,13 @@ class Namespace : public detail::Registrar } else { - LUABRIDGE_ASSERT(lua_istable(L, -1)); // Stack: ns, st + LUABRIDGE_ASSERT(lua_istable(L, -1)); // Stack: ns, vst ++m_stackSize; + lua_getmetatable(L, -1); // Stack: ns, vst, st + lua_insert(L, -2); // Stack: ns, st, vst + lua_pop(L, 1); // Stack: ns, st + // Map T back from its stored tables lua_rawgetp(L, LUA_REGISTRYINDEX, detail::getConstRegistryKey()); // Stack: ns, st, co diff --git a/Tests/Source/ClassTests.cpp b/Tests/Source/ClassTests.cpp index 06ceae8d..9394f2d6 100644 --- a/Tests/Source/ClassTests.cpp +++ b/Tests/Source/ClassTests.cpp @@ -1718,6 +1718,27 @@ TEST_F(ClassStaticProperties, FieldPointers_Overridden) ASSERT_EQ(7, Derived::staticData); } +TEST_F(ClassStaticProperties, SubsequentRegistration) +{ + using Int = Class; + + luabridge::getGlobalNamespace(L) + .beginClass("Int") + .endClass() + .beginClass("Int") + .addStaticProperty("staticData", &Int::staticData, true) + .endClass(); + + Int::staticData = 10; + + runLua("result = Int.staticData"); + ASSERT_TRUE(result().isNumber()); + ASSERT_EQ(10, result()); + + runLua("Int.staticData = 20"); + ASSERT_EQ(20, Int::staticData); +} + struct ClassMetaMethods : ClassTests { };