fix tests

This commit is contained in:
2026-02-07 00:09:58 -06:00
parent aba8eb66bd
commit 16059cca4e

View File

@@ -32921,7 +32921,7 @@ static void mach_gen_emit_get_elem (MachGenState *s, int dest, int obj, int idx)
}
static void mach_gen_emit_set_elem (MachGenState *s, int obj, int idx, int val) {
mach_gen_emit_3 (s, "store", obj, idx, val);
mach_gen_emit_3 (s, "store", obj, val, idx);
}
static void mach_gen_emit_call (MachGenState *s, int dest, int func_slot, cJSON *args) {
@@ -33051,6 +33051,12 @@ static int mach_gen_binary (MachGenState *s, cJSON *node) {
return dest;
}
/* Comma operator: evaluate left (discard), evaluate right (keep) */
if (strcmp (kind, ",") == 0) {
mach_gen_expr (s, left, -1);
return mach_gen_expr (s, right, -1);
}
int left_slot = mach_gen_expr (s, left, -1);
int right_slot = mach_gen_expr (s, right, -1);
int dest = mach_gen_alloc_slot (s);
@@ -33069,15 +33075,19 @@ static int mach_gen_compound_assign (MachGenState *s, cJSON *node, const char *o
cJSON *level_node = cJSON_GetObjectItem (left, "level");
int level = level_node ? (int)cJSON_GetNumberValue (level_node) : -1;
int left_slot = mach_gen_alloc_slot (s);
if (level == 0) {
if (level == 0 || level == -1) {
int local = mach_gen_find_var (s, name);
if (local >= 0) mach_gen_emit_2 (s, "move", left_slot, local);
} else if (level > 0) {
if (local >= 0) {
mach_gen_emit_2 (s, "move", left_slot, local);
level = 0; /* treat as local for the store below */
}
}
if (level > 0) {
MachGenState *target = s;
for (int i = 0; i < level; i++) target = target->parent;
int slot = mach_gen_find_var (target, name);
mach_gen_emit_3 (s, "get", left_slot, slot, level);
} else {
} else if (level == -1) {
cJSON *instr = cJSON_CreateArray ();
cJSON_AddItemToArray (instr, cJSON_CreateString ("access"));
cJSON_AddItemToArray (instr, cJSON_CreateNumber (left_slot));
@@ -33158,9 +33168,17 @@ static int mach_gen_assign (MachGenState *s, cJSON *node) {
const char *name = cJSON_GetStringValue (cJSON_GetObjectItem (left, "name"));
cJSON *level_node = cJSON_GetObjectItem (left, "level");
int level = level_node ? (int)cJSON_GetNumberValue (level_node) : -1;
if (level == 0) {
if (level == 0 || level == -1) {
int slot = mach_gen_find_var (s, name);
if (slot >= 0) mach_gen_emit_2 (s, "move", slot, val_slot);
else if (level == -1) {
/* No annotation and not local — set global */
cJSON *instr = cJSON_CreateArray ();
cJSON_AddItemToArray (instr, cJSON_CreateString ("set_var"));
cJSON_AddItemToArray (instr, cJSON_CreateString (name));
cJSON_AddItemToArray (instr, cJSON_CreateNumber (val_slot));
mach_gen_add_instr (s, instr);
}
} else if (level > 0) {
MachGenState *target = s;
for (int i = 0; i < level; i++) target = target->parent;
@@ -33229,7 +33247,8 @@ static int mach_gen_expr (MachGenState *s, cJSON *expr, int target) {
const char *name = cJSON_GetStringValue (cJSON_GetObjectItem (expr, "name"));
cJSON *level_node = cJSON_GetObjectItem (expr, "level");
int level = level_node ? (int)cJSON_GetNumberValue (level_node) : -1;
if (level == 0) {
if (level == 0 || level == -1) {
/* level 0 = known local; level -1 = no annotation, try local first */
int slot = mach_gen_find_var (s, name);
if (slot >= 0) return slot;
} else if (level > 0) {
@@ -33551,6 +33570,13 @@ static void mach_gen_statement (MachGenState *s, cJSON *stmt) {
return;
}
if (strcmp (kind, "var_list") == 0 || strcmp (kind, "def_list") == 0) {
cJSON *list = cJSON_GetObjectItem (stmt, "list");
cJSON *child;
cJSON_ArrayForEach (child, list) { mach_gen_statement (s, child); }
return;
}
if (strcmp (kind, "block") == 0) {
cJSON *stmts = cJSON_GetObjectItem (stmt, "statements");
cJSON *child;
@@ -33562,6 +33588,8 @@ static void mach_gen_statement (MachGenState *s, cJSON *stmt) {
cJSON *cond = cJSON_GetObjectItem (stmt, "expression");
cJSON *then_stmts = cJSON_GetObjectItem (stmt, "then");
cJSON *else_stmts = cJSON_GetObjectItem (stmt, "else");
/* Parser uses "list" for else-if chains */
if (!else_stmts) else_stmts = cJSON_GetObjectItem (stmt, "list");
char *else_label = mach_gen_label (s, "if_else");
char *end_label = mach_gen_label (s, "if_end");
int cond_slot = mach_gen_expr (s, cond, -1);
@@ -35163,13 +35191,12 @@ static JSValue mcode_exec(JSContext *ctx, JSMCode *code, JSValue this_obj,
pc = 0;
} else {
/* C or bytecode function — collect args from frame slots */
int c_argc = 0;
JSValue c_argv[16];
int nr_slots = (int)objhdr_cap56(new_frame->hdr);
for (int i = 1; i < nr_slots && c_argc < 16; i++) {
if (JS_IsNull(new_frame->slots[i])) break;
c_argv[c_argc++] = new_frame->slots[i];
}
int c_argc = (nr_slots >= 2) ? nr_slots - 2 : 0;
if (c_argc > 16) c_argc = 16;
JSValue c_argv[16];
for (int i = 0; i < c_argc; i++)
c_argv[i] = new_frame->slots[i + 1];
ctx->reg_current_frame = frame_ref.val;
ctx->rt->current_register_pc = pc > 0 ? pc - 1 : 0;
JSValue c_result = JS_Call(ctx, new_frame->function, new_frame->slots[0], c_argc, c_argv);