From 3618b3f409ecdd79f4a9fff2f4cecdf31a3a707e Mon Sep 17 00:00:00 2001 From: uramer Date: Sun, 29 Jan 2023 18:52:18 +0100 Subject: [PATCH] Fix Content::View::remove --- apps/openmw_test_suite/lua/test_ui_content.cpp | 8 +++++--- components/lua_ui/content.hpp | 10 ++++++---- components/lua_ui/content.lua | 12 ------------ 3 files changed, 11 insertions(+), 19 deletions(-) diff --git a/apps/openmw_test_suite/lua/test_ui_content.cpp b/apps/openmw_test_suite/lua/test_ui_content.cpp index b9b24e9684..57b744b005 100644 --- a/apps/openmw_test_suite/lua/test_ui_content.cpp +++ b/apps/openmw_test_suite/lua/test_ui_content.cpp @@ -119,15 +119,17 @@ namespace auto table = makeTable(); LuaUi::Content::View content = makeContent(table); EXPECT_ANY_THROW(content.at(0)); + EXPECT_EQ(content.size(), 0); content.assign(content.size(), makeTable()); + EXPECT_EQ(content.size(), 1); content.assign(content.size(), makeTable()); + EXPECT_EQ(content.size(), 2); content.assign(content.size(), makeTable()); EXPECT_EQ(content.size(), 3); EXPECT_ANY_THROW(content.at(3)); EXPECT_ANY_THROW(content.remove(3)); - EXPECT_NO_THROW(content.remove(1)); // TODO: something cursed happens here, even __newindex is not called! - EXPECT_EQ(content.size(), 2); - EXPECT_NO_THROW(content.at(2)); + content.remove(2); EXPECT_EQ(content.size(), 2); + EXPECT_ANY_THROW(content.at(2)); } } diff --git a/components/lua_ui/content.hpp b/components/lua_ui/content.hpp index 8487dea27a..72db6440d3 100644 --- a/components/lua_ui/content.hpp +++ b/components/lua_ui/content.hpp @@ -78,16 +78,18 @@ namespace LuaUi::Content void remove(size_t index) { if (index < size()) - mTable[toLua(index)] = sol::nil; + // for some reason mTable[key] = value doesn't call __newindex + mTable[sol::metatable_key][sol::meta_function::new_index].get()(mTable, toLua(index), sol::nil); else throw std::domain_error("Invalid Content index"); } void remove(std::string_view name) { - if (indexOf(name).has_value()) - mTable[name] = sol::nil; + auto index = indexOf(name); + if (index.has_value()) + remove(index.value()); else - throw std::domain_error("Invalid Content index"); + throw std::domain_error("Invalid Content key"); } std::optional indexOf(std::string_view name) const { diff --git a/components/lua_ui/content.lua b/components/lua_ui/content.lua index b7bec3701c..79fd87c79e 100644 --- a/components/lua_ui/content.lua +++ b/components/lua_ui/content.lua @@ -79,7 +79,6 @@ M.__index = function(self, key) return rawget(self, index) end local function remove(self, index) - print('remove', #self, index) local oldName = nameAt(self, index) if oldName then self.__nameIndex[oldName] = nil @@ -95,7 +94,6 @@ local function remove(self, index) end end rawset(self, #self, nil) - print('removed', #self) end local function assign(self, index, value) local oldName = nameAt(self, index) @@ -108,7 +106,6 @@ local function assign(self, index, value) end end M.__newindex = function(self, key, value) - print('__newindex ', key, value) local index = getIndexFromKey(self, key) if value == nil then remove(self, index) @@ -131,14 +128,5 @@ M.__pairs = function(self) end M.__ipairs = M.__pairs -local test = M.new({}) -test:insert(1, {}) -test[2] = {} -assert(#test == 2, "Wrong size") -test:add({ name = 'a' }) -assert(getIndexFromKey(test, 'a') == 3, getIndexFromKey(test, 'a')) -assert(type(test.a) == 'table', type(test.a)) -assert(test.a.name == 'a', 'wrong table') - return M )" \ No newline at end of file