1
0
mirror of https://gitlab.com/OpenMW/openmw.git synced 2025-01-06 00:55:50 +00:00

Move metatable protection asserts to tests

This commit is contained in:
uramer 2023-02-01 16:34:32 +01:00
parent 2a35bae655
commit d24c506b0e
3 changed files with 14 additions and 4 deletions

View File

@ -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();

View File

@ -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<sol::protected_function>()(
getMetatable()[sol::meta_function::new_index].get<sol::protected_function>()(
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<sol::table>(); }
private:
sol::table mTable;

View File

@ -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