From f121cbdb5af0b959d1b1af0d8c705ba0cb53a8a3 Mon Sep 17 00:00:00 2001 From: Fabrice Bellard Date: Sat, 5 Apr 2025 15:41:51 +0200 Subject: [PATCH] added forgotten js_weakref_is_live() tests --- quickjs.c | 8 ++++++-- tests/test_builtin.js | 2 ++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/quickjs.c b/quickjs.c index e6253e6..2b0de17 100644 --- a/quickjs.c +++ b/quickjs.c @@ -46594,6 +46594,7 @@ static BOOL js_weakref_is_target(JSValueConst val) } /* JS_UNDEFINED is considered as a live weakref */ +/* XXX: add a specific JSWeakRef value type ? */ static BOOL js_weakref_is_live(JSValueConst val) { int *pref_count; @@ -53884,7 +53885,10 @@ static JSValue js_weakref_deref(JSContext *ctx, JSValueConst this_val, int argc, JSWeakRefData *wrd = JS_GetOpaque2(ctx, this_val, JS_CLASS_WEAK_REF); if (!wrd) return JS_EXCEPTION; - return JS_DupValue(ctx, wrd->target); + if (js_weakref_is_live(wrd->target)) + return JS_DupValue(ctx, wrd->target); + else + return JS_UNDEFINED; } static const JSCFunctionListEntry js_weakref_proto_funcs[] = { @@ -54051,7 +54055,7 @@ static JSValue js_finrec_unregister(JSContext *ctx, JSValueConst this_val, int a removed = FALSE; list_for_each_safe(el, el1, &frd->entries) { JSFinRecEntry *fre = list_entry(el, JSFinRecEntry, link); - if (js_same_value(ctx, fre->token, token)) { + if (js_weakref_is_live(fre->token) && js_same_value(ctx, fre->token, token)) { js_weakref_free(ctx->rt, fre->target); js_weakref_free(ctx->rt, fre->token); JS_FreeValue(ctx, fre->held_val); diff --git a/tests/test_builtin.js b/tests/test_builtin.js index 7ff303f..38b2640 100644 --- a/tests/test_builtin.js +++ b/tests/test_builtin.js @@ -849,6 +849,8 @@ function test_weak_ref() assert(w2.deref(), o); o = null; + assert(w1.deref(), undefined); + assert(w2.deref(), undefined); std.gc(); assert(w1.deref(), undefined); assert(w2.deref(), undefined);