Skip to content

Commit 9bd10d8

Browse files
author
Fabrice Bellard
committedMar 13, 2025
simplified the handling of closures
1 parent dfd9c93 commit 9bd10d8

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed
 

‎quickjs.c

+16-22
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,7 @@ typedef struct JSVarRef {
363363
struct {
364364
int __gc_ref_count; /* corresponds to header.ref_count */
365365
uint8_t __gc_mark; /* corresponds to header.mark/gc_obj_type */
366-
uint8_t is_detached : 1;
367-
uint8_t is_arg : 1;
368-
uint16_t var_idx; /* index of the corresponding function variable on
369-
the stack */
366+
uint8_t is_detached;
370367
};
371368
};
372369
JSValue *pvalue; /* pointer to the value, either on the stack or
@@ -15673,10 +15670,16 @@ static JSVarRef *get_var_ref(JSContext *ctx, JSStackFrame *sf,
1567315670
{
1567415671
JSVarRef *var_ref;
1567515672
struct list_head *el;
15673+
JSValue *pvalue;
15674+
15675+
if (is_arg)
15676+
pvalue = &sf->arg_buf[var_idx];
15677+
else
15678+
pvalue = &sf->var_buf[var_idx];
1567615679

1567715680
list_for_each(el, &sf->var_ref_list) {
1567815681
var_ref = list_entry(el, JSVarRef, var_ref_link);
15679-
if (var_ref->var_idx == var_idx && var_ref->is_arg == is_arg) {
15682+
if (var_ref->pvalue == pvalue) {
1568015683
var_ref->header.ref_count++;
1568115684
return var_ref;
1568215685
}
@@ -15688,8 +15691,6 @@ static JSVarRef *get_var_ref(JSContext *ctx, JSStackFrame *sf,
1568815691
var_ref->header.ref_count = 1;
1568915692
add_gc_object(ctx->rt, &var_ref->header, JS_GC_OBJ_TYPE_VAR_REF);
1569015693
var_ref->is_detached = FALSE;
15691-
var_ref->is_arg = is_arg;
15692-
var_ref->var_idx = var_idx;
1569315694
list_add_tail(&var_ref->var_ref_link, &sf->var_ref_list);
1569415695
if (sf->js_mode & JS_MODE_ASYNC) {
1569515696
/* The stack frame is detached and may be destroyed at any
@@ -15705,10 +15706,7 @@ static JSVarRef *get_var_ref(JSContext *ctx, JSStackFrame *sf,
1570515706
} else {
1570615707
var_ref->async_func = NULL;
1570715708
}
15708-
if (is_arg)
15709-
var_ref->pvalue = &sf->arg_buf[var_idx];
15710-
else
15711-
var_ref->pvalue = &sf->var_buf[var_idx];
15709+
var_ref->pvalue = pvalue;
1571215710
return var_ref;
1571315711
}
1571415712

@@ -15936,37 +15934,33 @@ static void close_var_refs(JSRuntime *rt, JSStackFrame *sf)
1593615934
{
1593715935
struct list_head *el, *el1;
1593815936
JSVarRef *var_ref;
15939-
int var_idx;
1594015937

1594115938
list_for_each_safe(el, el1, &sf->var_ref_list) {
1594215939
var_ref = list_entry(el, JSVarRef, var_ref_link);
1594315940
/* no need to unlink var_ref->var_ref_link as the list is never used afterwards */
1594415941
if (var_ref->async_func)
1594515942
async_func_free(rt, var_ref->async_func);
15946-
var_idx = var_ref->var_idx;
15947-
if (var_ref->is_arg)
15948-
var_ref->value = JS_DupValueRT(rt, sf->arg_buf[var_idx]);
15949-
else
15950-
var_ref->value = JS_DupValueRT(rt, sf->var_buf[var_idx]);
15943+
var_ref->value = JS_DupValueRT(rt, *var_ref->pvalue);
1595115944
var_ref->pvalue = &var_ref->value;
1595215945
/* the reference is no longer to a local variable */
1595315946
var_ref->is_detached = TRUE;
1595415947
}
1595515948
}
1595615949

15957-
static void close_lexical_var(JSContext *ctx, JSStackFrame *sf, int idx, int is_arg)
15950+
static void close_lexical_var(JSContext *ctx, JSStackFrame *sf, int var_idx)
1595815951
{
15952+
JSValue *pvalue;
1595915953
struct list_head *el, *el1;
1596015954
JSVarRef *var_ref;
15961-
int var_idx = idx;
1596215955

15956+
pvalue = &sf->var_buf[var_idx];
1596315957
list_for_each_safe(el, el1, &sf->var_ref_list) {
1596415958
var_ref = list_entry(el, JSVarRef, var_ref_link);
15965-
if (var_idx == var_ref->var_idx && var_ref->is_arg == is_arg) {
15959+
if (var_ref->pvalue == pvalue) {
1596615960
list_del(&var_ref->var_ref_link);
1596715961
if (var_ref->async_func)
1596815962
async_func_free(ctx->rt, var_ref->async_func);
15969-
var_ref->value = JS_DupValue(ctx, sf->var_buf[var_idx]);
15963+
var_ref->value = JS_DupValue(ctx, *var_ref->pvalue);
1597015964
var_ref->pvalue = &var_ref->value;
1597115965
/* the reference is no longer to a local variable */
1597215966
var_ref->is_detached = TRUE;
@@ -17199,7 +17193,7 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
1719917193
int idx;
1720017194
idx = get_u16(pc);
1720117195
pc += 2;
17202-
close_lexical_var(ctx, sf, idx, FALSE);
17196+
close_lexical_var(ctx, sf, idx);
1720317197
}
1720417198
BREAK;
1720517199

0 commit comments

Comments
 (0)