[lua] Add __len operator to Properties

This commit is contained in:
David Capello 2023-01-02 12:17:11 -03:00
parent c77c95181a
commit ef0596fd01
2 changed files with 14 additions and 0 deletions

View File

@ -26,6 +26,18 @@ struct Properties {
Properties(doc::ObjectId id) : id(id) { }
};
int Properties_len(lua_State* L)
{
auto propObj = get_obj<Properties>(L, 1);
auto obj = static_cast<doc::WithUserData*>(get_object(propObj->id));
if (!obj)
return luaL_error(L, "the object with these properties was destroyed");
auto& properties = obj->userData().properties();
lua_pushinteger(L, properties.size());
return 1;
}
int Properties_index(lua_State* L)
{
auto propObj = get_obj<Properties>(L, 1);
@ -105,6 +117,7 @@ int Properties_newindex(lua_State* L)
}
const luaL_Reg Properties_methods[] = {
{ "__len", Properties_len },
{ "__index", Properties_index },
{ "__newindex", Properties_newindex },
{ nullptr, nullptr }

View File

@ -15,6 +15,7 @@ do
assert(spr.properties.c == "hi")
-- TODO we don't have too much precision saving fixed points in properties
assert(math.abs(spr.properties.d - 2.3) < 0.00001)
assert(#spr.properties == 4)
spr.properties.a = false
assert(spr.properties.a == false)