mirror of
https://github.com/OpenMW/openmw.git
synced 2025-04-01 21:36:42 +00:00
Move metatable protection asserts to tests
This commit is contained in:
parent
2a35bae655
commit
d24c506b0e
3 changed files with 14 additions and 4 deletions
|
@ -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)
|
TEST_F(LuaUiContentTest, Create)
|
||||||
{
|
{
|
||||||
auto table = makeTable();
|
auto table = makeTable();
|
||||||
|
|
|
@ -67,7 +67,7 @@ namespace LuaUi::Content
|
||||||
{
|
{
|
||||||
if (index < size())
|
if (index < size())
|
||||||
// for some reason mTable[key] = value doesn't call __newindex
|
// 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);
|
mTable, toLua(index), sol::nil);
|
||||||
else
|
else
|
||||||
throw std::range_error("Invalid Content index");
|
throw std::range_error("Invalid Content index");
|
||||||
|
@ -97,6 +97,8 @@ namespace LuaUi::Content
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sol::table getMetatable() const { return mTable[sol::metatable_key].get<sol::table>(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
sol::table mTable;
|
sol::table mTable;
|
||||||
|
|
||||||
|
|
|
@ -137,7 +137,4 @@ end
|
||||||
M.__ipairs = M.__pairs
|
M.__ipairs = M.__pairs
|
||||||
M.__metatable = {}
|
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
|
return M
|
||||||
|
|
Loading…
Reference in a new issue