Skip to content

Commit

Permalink
draft: fix SETLIST without active table
Browse files Browse the repository at this point in the history
Previously failed on e.g. `print({0})` in `luac5.1 -s`
  • Loading branch information
half-duplex committed Jun 11, 2021
1 parent 895d923 commit c09be19
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 additions & 12 deletions luadec/decompile.c
Expand Up @@ -929,34 +929,38 @@ void AddToTable(Function* F, DecTable* tbl, const char* value, const char* key)
AddToList(list, (ListItem*)item);
}

void SetList(Function* F, int a, int b, int c) {
int i;
int SetList(Function* F, int a, int b, int c) {
int i = 1, j;
DecTable* tbl = (DecTable*)FindFromListTail(&(F->tables), (ListItemCmpFn)MatchTable, &a);
if (tbl == NULL) {
sprintf(errortmp, "No list found for R%d , SetList fails", a);
SET_ERROR(F, errortmp);
return;
UnsetPending(F, a);
}
if (b == 0) {
const char* rstr;
i = 1;
while (1) {
rstr = GetR(F, a + i);
if (error)
return;
return tbl ? 0 : i;
if (strcmp(rstr,".end") == 0)
break;
AddToTable(F, tbl, rstr, NULL); // Lua5.1 specific TODO: it's not really this :(
if (tbl) {
AddToTable(F, tbl, rstr, NULL); // Lua5.1 specific TODO: it's not really this :(
}
i++;
};
} //should be {...} or func(func()) ,when b == 0, that will use all avaliable reg from R(a)

for (i = 1; i <= b; i++) {
const char* rstr = GetR(F, a + i);
for (j = 1; j <= b; j++) {
const char* rstr = GetR(F, a + j);
if (error)
return;
AddToTable(F, tbl, rstr, NULL); // Lua5.1 specific TODO: it's not really this :(
return tbl ? 0 : i + j - 2;
if (tbl) {
AddToTable(F, tbl, rstr, NULL); // Lua5.1 specific TODO: it's not really this :(
}
}

return tbl ? 0 : i + j - 2;
}

void UnsetPending(Function* F, int r) {
Expand Down Expand Up @@ -3026,7 +3030,31 @@ char* ProcessCode(Proto* f, int indent, int func_checking, char* funcnumstr) {
}
#endif
}
TRY(SetList(F, a, b, c));

const char *astr;
const char *cstr;
int setlist;
/*
* first try to add into a list
*/
TRY(setlist = SetList(F, a, b, c));
if (setlist) {
/*
* if failed, just output an assignment
*/
for (int i = setlist; i >= 1; i--) {
TRY(astr = GetR(F, a));
if (isIdentifier(astr)) {
StringBuffer_set(str, astr);
} else {
StringBuffer_printf(str, "(%s)", astr);
}
StringBuffer_addPrintf(str, "[%d]", (c-1)*50+i); // todo: s/50/LFS_SIZE or w/e
PENDING(a+i) = 1;
TRY(cstr = GetR(F, a+i));
TRY(AssignGlobalOrUpvalue(F, StringBuffer_getRef(str), cstr));
}
}
break;
}
#if LUA_VERSION_NUM == 501
Expand Down

0 comments on commit c09be19

Please sign in to comment.