Fix Content::View::remove

7220-lua-add-a-general-purpose-lexical-parser
uramer 2 years ago
parent fc1430af95
commit 3618b3f409

@ -119,15 +119,17 @@ namespace
auto table = makeTable(); auto table = makeTable();
LuaUi::Content::View content = makeContent(table); LuaUi::Content::View content = makeContent(table);
EXPECT_ANY_THROW(content.at(0)); EXPECT_ANY_THROW(content.at(0));
EXPECT_EQ(content.size(), 0);
content.assign(content.size(), makeTable()); content.assign(content.size(), makeTable());
EXPECT_EQ(content.size(), 1);
content.assign(content.size(), makeTable()); content.assign(content.size(), makeTable());
EXPECT_EQ(content.size(), 2);
content.assign(content.size(), makeTable()); content.assign(content.size(), makeTable());
EXPECT_EQ(content.size(), 3); EXPECT_EQ(content.size(), 3);
EXPECT_ANY_THROW(content.at(3)); EXPECT_ANY_THROW(content.at(3));
EXPECT_ANY_THROW(content.remove(3)); EXPECT_ANY_THROW(content.remove(3));
EXPECT_NO_THROW(content.remove(1)); // TODO: something cursed happens here, even __newindex is not called! content.remove(2);
EXPECT_EQ(content.size(), 2);
EXPECT_NO_THROW(content.at(2));
EXPECT_EQ(content.size(), 2); EXPECT_EQ(content.size(), 2);
EXPECT_ANY_THROW(content.at(2));
} }
} }

@ -78,16 +78,18 @@ namespace LuaUi::Content
void remove(size_t index) void remove(size_t index)
{ {
if (index < size()) 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<sol::protected_function>()(mTable, toLua(index), sol::nil);
else else
throw std::domain_error("Invalid Content index"); throw std::domain_error("Invalid Content index");
} }
void remove(std::string_view name) void remove(std::string_view name)
{ {
if (indexOf(name).has_value()) auto index = indexOf(name);
mTable[name] = sol::nil; if (index.has_value())
remove(index.value());
else else
throw std::domain_error("Invalid Content index"); throw std::domain_error("Invalid Content key");
} }
std::optional<size_t> indexOf(std::string_view name) const std::optional<size_t> indexOf(std::string_view name) const
{ {

@ -79,7 +79,6 @@ M.__index = function(self, key)
return rawget(self, index) return rawget(self, index)
end end
local function remove(self, index) local function remove(self, index)
print('remove', #self, index)
local oldName = nameAt(self, index) local oldName = nameAt(self, index)
if oldName then if oldName then
self.__nameIndex[oldName] = nil self.__nameIndex[oldName] = nil
@ -95,7 +94,6 @@ local function remove(self, index)
end end
end end
rawset(self, #self, nil) rawset(self, #self, nil)
print('removed', #self)
end end
local function assign(self, index, value) local function assign(self, index, value)
local oldName = nameAt(self, index) local oldName = nameAt(self, index)
@ -108,7 +106,6 @@ local function assign(self, index, value)
end end
end end
M.__newindex = function(self, key, value) M.__newindex = function(self, key, value)
print('__newindex ', key, value)
local index = getIndexFromKey(self, key) local index = getIndexFromKey(self, key)
if value == nil then if value == nil then
remove(self, index) remove(self, index)
@ -131,14 +128,5 @@ M.__pairs = function(self)
end end
M.__ipairs = M.__pairs 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 return M
)" )"
Loading…
Cancel
Save