diff --git a/apps/openmw_test_suite/lua/test_ui_content.cpp b/apps/openmw_test_suite/lua/test_ui_content.cpp index 10b67638fd..13ee9b6a7d 100644 --- a/apps/openmw_test_suite/lua/test_ui_content.cpp +++ b/apps/openmw_test_suite/lua/test_ui_content.cpp @@ -36,6 +36,17 @@ namespace } }; + TEST_F(LuaUiContentTest, ProtectedMetatable) + { + mLuaState.sol()["makeContent"] = mNew; + mLuaState.sol()["M"] = makeContent(makeTable()).getMetatable(); + std::string testScript = R"( + assert(not pcall(function() setmetatable(makeContent{}, {}) end), 'Metatable is not protected') + assert(getmetatable(makeContent{}) ~= M, 'Metatable is not protected') + )"; + EXPECT_NO_THROW(mLuaState.sol().safe_script(testScript)); + } + TEST_F(LuaUiContentTest, Create) { auto table = makeTable(); diff --git a/components/lua_ui/content.hpp b/components/lua_ui/content.hpp index 310d0b9cbe..b29bf82818 100644 --- a/components/lua_ui/content.hpp +++ b/components/lua_ui/content.hpp @@ -67,7 +67,7 @@ namespace LuaUi::Content { if (index < size()) // for some reason mTable[key] = value doesn't call __newindex - mTable[sol::metatable_key][sol::meta_function::new_index].get()( + getMetatable()[sol::meta_function::new_index].get()( mTable, toLua(index), sol::nil); else throw std::range_error("Invalid Content index"); @@ -97,6 +97,8 @@ namespace LuaUi::Content return std::nullopt; } + sol::table getMetatable() const { return mTable[sol::metatable_key].get(); } + private: sol::table mTable; diff --git a/components/lua_ui/content.lua b/components/lua_ui/content.lua index 7fe1d2cff1..cd63efc7ca 100644 --- a/components/lua_ui/content.lua +++ b/components/lua_ui/content.lua @@ -137,7 +137,4 @@ end M.__ipairs = M.__pairs M.__metatable = {} -assert(not pcall(function() setmetatable(M.new {}, {}) end), 'Metatable is not protected') -assert(getmetatable(M.new {}) ~= M, 'Metatable is not protected') - return M