From 1c38699b5acfc5c2652d143a6fb863538535ba5b Mon Sep 17 00:00:00 2001 From: John Alanbrook Date: Fri, 6 Feb 2026 01:41:03 -0600 Subject: [PATCH] fix scope resolution --- source/quickjs.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/source/quickjs.c b/source/quickjs.c index ef41f96b..adc44987 100644 --- a/source/quickjs.c +++ b/source/quickjs.c @@ -32509,13 +32509,25 @@ static cJSON *mach_gen_program (MachGenState *s, cJSON *ast) { mach_scan_vars (s, stmt); } + /* Pre-register function names as local variables (hoisting) */ + cJSON *functions = cJSON_GetObjectItem (ast, "functions"); + cJSON *func_node; + cJSON_ArrayForEach (func_node, functions) { + cJSON *name_obj = cJSON_GetObjectItem (func_node, "name"); + if (name_obj && cJSON_IsString (name_obj)) { + const char *name = cJSON_GetStringValue (name_obj); + if (!mach_find_var_info (s, name)) { + int slot = 1 + s->nr_args + s->nr_local_slots++; + mach_add_var (s, name, slot, MACH_VAR_LOCAL, 1); + } + } + } + /* Adjust temp slot start after all locals are allocated */ s->next_temp_slot = 1 + s->nr_local_slots; if (s->next_temp_slot > s->max_slot) s->max_slot = s->next_temp_slot; /* Process top-level function declarations */ - cJSON *functions = cJSON_GetObjectItem (ast, "functions"); - cJSON *func_node; cJSON_ArrayForEach (func_node, functions) { cJSON *name_obj = cJSON_GetObjectItem (func_node, "name"); if (name_obj && cJSON_IsString (name_obj)) {