Define array size only when necessary

- new `__pairs` metamethod for `CObject`s, cycles through all of an object's fields, in alphabetical order
- stray semicolon!
This commit is contained in:
Cooliokid956 2026-02-07 01:17:44 -06:00
parent e58858f4e8
commit 47e674e65a
5 changed files with 2213 additions and 2176 deletions

View file

@ -97,7 +97,7 @@ WINDOWS_AUTO_BUILDER ?= 0
# Setup extra cflags
EXTRA_CFLAGS ?=
EXTRA_CPP_FLAGS ?=
EXTRA_CFLAGS += -Wno-format-security -Wno-trigraphs -Wno-missing-braces
EXTRA_CFLAGS += -Wno-format-security -Wno-trigraphs -Wno-missing-braces -Wno-missing-field-initializers
dev:; @$(MAKE) DEVELOPMENT=1

View file

@ -574,8 +574,10 @@ def build_struct(struct):
row.append('offsetof(%s%s, %s), ' % (struct_str, name, field['identifier']))
row.append('%s, ' % fimmutable)
row.append('%s, ' % lot )
row.append('%s, ' % size )
row.append('sizeof(%s)' % ftype )
if size != 1:
row.append('%s, ' % size )
row.append('sizeof(%s)' % ftype )
else: row[-1] = row[-1][:-2]
row.extend(['\\'] * (8 - len(row)))
row.append(endStr)
if field.get('function'):

View file

@ -21,10 +21,9 @@
// Optional parameters must be contiguous until the last parameter (a mandatory parameter following an optional parameter is not allowed)
#define OPTIONAL
/* A macro to tell autogen the field `name` is a property member of the struct that calls `get` or `set` accessors
- get: fun(self) -> value
- set: fun(self, value)
*/
// A macro to tell autogen the field `name` is a property member of the struct that calls `get` or `set` accessors
// - get: fun(self) -> value
// - set: fun(self, value)
#define PROPERTY(name, get, set)
// A macro to tell autogen the field `name` is a function member of the struct that calls `c_function`

View file

@ -560,7 +560,7 @@ static int smlua__get_field(lua_State* L) {
}
// CObject property
if (data->valueType == LVT_PROPERTY) {;
if (data->valueType == LVT_PROPERTY) {
lua_getglobal(L, data->get);
lua_pushvalue(L, 1);
smlua_pcall(L, 1, 1, 0);
@ -569,15 +569,15 @@ static int smlua__get_field(lua_State* L) {
}
u8* p = ((u8*)(intptr_t)pointer) + data->valueOffset;
if (data->count == 1) {
if (smlua_push_field(L, p, data)) {
LOG_LUA_LINE("_get_field on unimplemented type '%d', key '%s'", data->valueType, key);
if (data->count > 1) {
smlua_push_object(L, LOT_ARRAY, p, data);
if (!gSmLuaConvertSuccess) {
LOG_LUA_LINE("_get_field failed to retrieve value type '%d', key '%s'", data->valueType, key);
return 0;
}
} else {
smlua_push_object(L, LOT_ARRAY, p, data);
if (!gSmLuaConvertSuccess) {
LOG_LUA_LINE("_set_field failed to retrieve value type '%d', key '%s'", data->valueType, key);
if (smlua_push_field(L, p, data)) {
LOG_LUA_LINE("_get_field on unimplemented type '%d', key '%s'", data->valueType, key);
return 0;
}
}
@ -672,6 +672,41 @@ static int smlua__set_field(lua_State* L) {
return 1;
}
int smlua__iter(lua_State *L) {
lua_rawgeti(L, 1, 1);
int i = lua_tointeger(L, -1);
lua_pop(L, 1);
lua_rawgeti(L, 1, 2);
const CObject *cobj = lua_touserdata(L, -1);
lua_pop(L, 1);
extern struct LuaObjectTable sLuaObjectAutogenTable[];
struct LuaObjectTable* ot = &sLuaObjectAutogenTable[cobj->lot - LOT_AUTOGEN_MIN - 1];
if (i >= ot->fieldCount) { return 0; }
u8* pointer = (u8*)(intptr_t) cobj->pointer;
struct LuaObjectField* data = &ot->fields[i];
lua_pushstring(L, data->key);
smlua_push_field(L, pointer, data);
lua_pushinteger(L, ++i);
lua_rawseti(L, 1, 1);
return 2;
}
int smlua__pairs(lua_State *L) {
lua_pushcfunction(L, smlua__iter);
lua_newtable(L);
lua_pushinteger(L, 0); lua_rawseti(L, -2, 1);
lua_pushvalue (L, 1); lua_rawseti(L, -2, 2);
lua_pushnil(L);
return 3;
}
int smlua__eq(lua_State *L) {
const CObject *a = lua_touserdata(L, 1);
const CObject *b = lua_touserdata(L, 2);
@ -724,6 +759,7 @@ void smlua_cobject_init_globals(void) {
luaL_Reg cObjectMethods[] = {
{ "__index", smlua__get_field },
{ "__newindex", smlua__set_field },
{ "__pairs", smlua__pairs },
{ "__eq", smlua__eq },
{ "__bnot", smlua__bnot },
{ "__metatable", NULL },

File diff suppressed because it is too large Load diff