Skip to content

Commit

Permalink
support parse declaration multiple variables are declared at once
Browse files Browse the repository at this point in the history
Signed-off-by: Jianhui Zhao <[email protected]>
  • Loading branch information
zhaojh329 committed Jul 8, 2024
1 parent 5fa3f17 commit 820df71
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
20 changes: 15 additions & 5 deletions ffi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,7 @@ static int cparse_record_field(lua_State *L, struct crecord_field **fields)

while (true) {
struct crecord_field *field;
struct ctype ct = {};
struct ctype bt = {}, ct;
bool flexible = false;
int array_size;
char *name;
Expand All @@ -1556,17 +1556,21 @@ static int cparse_record_field(lua_State *L, struct crecord_field **fields)
return nfield;

if (cparse_check_tok(L, tok) == TOK_STRUCT || cparse_check_tok(L, tok) == TOK_UNION) {
tok = cparse_record(L, &ct, cparse_check_tok(L, tok) == TOK_UNION);
tok = cparse_record(L, &bt, cparse_check_tok(L, tok) == TOK_UNION);
if (tok == ';') {
field = calloc(1, sizeof(struct crecord_field) + 1);
if (!field)
return luaL_error(L, "no mem");
ct = bt;
goto add;
}
} else {
tok = cparse_basetype(L, tok, &ct);
tok = cparse_basetype(L, tok, &bt);
}

again:
ct = bt;

tok = cparse_pointer(L, tok, &ct);

check_void_forbidden(L, &ct, tok);
Expand All @@ -1587,15 +1591,21 @@ static int cparse_record_field(lua_State *L, struct crecord_field **fields)
memcpy(field->name, name, yyget_leng());

tok = cparse_array(L, yylex(), &flexible, &array_size);
if (cparse_check_tok(L, tok) != ';')
return cparse_expected_error(L, tok, ";");

if (array_size >= 0)
cparse_new_array(L, array_size, &ct);

add:
field->ct = ctype_lookup(L, &ct, false);
fields[nfield++] = field;

if (cparse_check_tok(L, tok) == ',') {
tok = yylex();
goto again;
}

if (cparse_check_tok(L, tok) != ';')
return cparse_expected_error(L, tok, ";");
}
}

Expand Down
6 changes: 6 additions & 0 deletions tests/basetype.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,12 @@ ffi.cdef([[
char name[0];
};
typedef struct md5_ctx {
uint32_t lo, hi;
uint32_t a, b, c, d;
unsigned char buffer[64];
} md5_ctx_t;
int printf(const char *format, ...);
struct ComplexStruct *fun1();
struct ComplexStruct *fun2(int a[]);
Expand Down

0 comments on commit 820df71

Please sign in to comment.