Move metatable protection asserts to tests

7220-lua-add-a-general-purpose-lexical-parser
uramer 2 years ago
parent 2a35bae655
commit d24c506b0e

@ -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…
Cancel
Save