Skip to content

Commit 69332fb

Browse files
Fabrice Bellardsaghul
Fabrice Bellard
authored andcommitted
simplified the handling of closures
1 parent 4dcccd0 commit 69332fb

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed

quickjs.c

+16-27
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,7 @@ typedef struct JSVarRef {
364364
struct {
365365
int __gc_ref_count; /* corresponds to header.ref_count */
366366
uint8_t __gc_mark; /* corresponds to header.mark/gc_obj_type */
367-
368-
/* 0 : the JSVarRef is on the stack. header.link is an element
369-
of JSStackFrame.var_ref_list.
370-
1 : the JSVarRef is detached. header.link has the normal meanning
371-
*/
372-
uint8_t is_detached : 1;
373-
uint8_t is_arg : 1;
374-
uint16_t var_idx; /* index of the corresponding function variable on
375-
the stack */
367+
bool is_detached;
376368
};
377369
};
378370
JSValue *pvalue; /* pointer to the value, either on the stack or
@@ -14440,10 +14432,16 @@ static JSVarRef *get_var_ref(JSContext *ctx, JSStackFrame *sf,
1444014432
{
1444114433
JSVarRef *var_ref;
1444214434
struct list_head *el;
14435+
JSValue *pvalue;
14436+
14437+
if (is_arg)
14438+
pvalue = &sf->arg_buf[var_idx];
14439+
else
14440+
pvalue = &sf->var_buf[var_idx];
1444314441

1444414442
list_for_each(el, &sf->var_ref_list) {
1444514443
var_ref = list_entry(el, JSVarRef, header.link);
14446-
if (var_ref->var_idx == var_idx && var_ref->is_arg == is_arg) {
14444+
if (var_ref->pvalue == pvalue) {
1444714445
var_ref->header.ref_count++;
1444814446
return var_ref;
1444914447
}
@@ -14454,13 +14452,8 @@ static JSVarRef *get_var_ref(JSContext *ctx, JSStackFrame *sf,
1445414452
return NULL;
1445514453
var_ref->header.ref_count = 1;
1445614454
var_ref->is_detached = false;
14457-
var_ref->is_arg = is_arg;
14458-
var_ref->var_idx = var_idx;
1445914455
list_add_tail(&var_ref->header.link, &sf->var_ref_list);
14460-
if (is_arg)
14461-
var_ref->pvalue = &sf->arg_buf[var_idx];
14462-
else
14463-
var_ref->pvalue = &sf->var_buf[var_idx];
14456+
var_ref->pvalue = pvalue;
1446414457
var_ref->value = JS_UNDEFINED;
1446514458
return var_ref;
1446614459
}
@@ -14689,32 +14682,28 @@ static void close_var_refs(JSRuntime *rt, JSStackFrame *sf)
1468914682
{
1469014683
struct list_head *el, *el1;
1469114684
JSVarRef *var_ref;
14692-
int var_idx;
1469314685

1469414686
list_for_each_safe(el, el1, &sf->var_ref_list) {
1469514687
var_ref = list_entry(el, JSVarRef, header.link);
14696-
var_idx = var_ref->var_idx;
14697-
if (var_ref->is_arg)
14698-
var_ref->value = js_dup(sf->arg_buf[var_idx]);
14699-
else
14700-
var_ref->value = js_dup(sf->var_buf[var_idx]);
14688+
var_ref->value = js_dup(*var_ref->pvalue);
1470114689
var_ref->pvalue = &var_ref->value;
1470214690
/* the reference is no longer to a local variable */
1470314691
var_ref->is_detached = true;
1470414692
add_gc_object(rt, &var_ref->header, JS_GC_OBJ_TYPE_VAR_REF);
1470514693
}
1470614694
}
1470714695

14708-
static void close_lexical_var(JSContext *ctx, JSStackFrame *sf, int idx, int is_arg)
14696+
static void close_lexical_var(JSContext *ctx, JSStackFrame *sf, int var_idx)
1470914697
{
14698+
JSValue *pvalue;
1471014699
struct list_head *el, *el1;
1471114700
JSVarRef *var_ref;
14712-
int var_idx = idx;
1471314701

14702+
pvalue = &sf->var_buf[var_idx];
1471414703
list_for_each_safe(el, el1, &sf->var_ref_list) {
1471514704
var_ref = list_entry(el, JSVarRef, header.link);
14716-
if (var_idx == var_ref->var_idx && var_ref->is_arg == is_arg) {
14717-
var_ref->value = js_dup(sf->var_buf[var_idx]);
14705+
if (var_ref->pvalue == pvalue) {
14706+
var_ref->value = js_dup(*var_ref->pvalue);
1471814707
var_ref->pvalue = &var_ref->value;
1471914708
list_del(&var_ref->header.link);
1472014709
/* the reference is no longer to a local variable */
@@ -15976,7 +15965,7 @@ static JSValue JS_CallInternal(JSContext *caller_ctx, JSValueConst func_obj,
1597615965
int idx;
1597715966
idx = get_u16(pc);
1597815967
pc += 2;
15979-
close_lexical_var(ctx, sf, idx, FALSE);
15968+
close_lexical_var(ctx, sf, idx);
1598015969
}
1598115970
BREAK;
1598215971

0 commit comments

Comments
 (0)