[lua] We cannot detect existent keys with nil in a table (they aren't added to the table)

All non-existent keys return nil (and nil keys are not added when the
table is created, so we cannot even iterate the table to search for
keys like "always" or "sRGB"). The only solution is assigning a
non-nil value, e.g. { always=true }, { sRGB=true }, etc.
This commit is contained in:
David Capello 2020-07-28 17:37:31 -03:00
parent c6b42173cd
commit c4ed8c1450
4 changed files with 16 additions and 9 deletions

View File

@ -40,7 +40,7 @@ int ColorSpace_new(lua_State* L)
lua_pop(L, 1);
// Create sRGB profile with ColorSpace{ sRGB }
if (lua_getfield(L, 1, "sRGB") != LUA_TNONE) {
if (lua_is_key_true(L, 1, "sRGB")) {
lua_pop(L, 1);
push_new<gfx::ColorSpace>(L, *gfx::ColorSpace::MakeSRGB());
return 1;

View File

@ -379,14 +379,9 @@ int Dialog_newrow(lua_State* L)
dlg->autoNewRow = false;
if (lua_istable(L, 2)) {
// Dialog:newrow{ always }
const int type = lua_getfield(L, 2, "always");
if (type != LUA_TNONE) {
if (type == LUA_TNIL ||
lua_toboolean(L, -1)) {
dlg->autoNewRow = true;
}
lua_pop(L, 1);
}
if (lua_is_key_true(L, 2, "always"))
dlg->autoNewRow = true;
lua_pop(L, 1);
}
lua_pushvalue(L, 1);

View File

@ -93,5 +93,15 @@ void create_mt_getters_setters(lua_State* L,
ASSERT(lua_gettop(L) == top);
}
bool lua_is_key_true(lua_State* L, int tableIndex, const char* keyName)
{
bool result = false;
int type = lua_getfield(L, tableIndex, keyName);
if (type != LUA_TNIL && lua_toboolean(L, -1))
result = true;
lua_pop(L, 1);
return result;
}
} // namespace script
} // namespace app

View File

@ -128,6 +128,8 @@ void create_mt_getters_setters(lua_State* L,
const char* tname,
const Property* properties);
bool lua_is_key_true(lua_State* L, int tableIndex, const char* keyName);
#define REG_CLASS_PROPERTIES(L, T) { \
luaL_getmetatable(L, get_mtname<T>()); \
create_mt_getters_setters(L, get_mtname<T>(), T##_properties); \