]> git.kaiwu.me - quickjs.git/commitdiff
added forgotten js_weakref_is_live() tests
authorFabrice Bellard <fabrice@bellard.org>
Sat, 5 Apr 2025 13:41:51 +0000 (15:41 +0200)
committerFabrice Bellard <fabrice@bellard.org>
Sat, 5 Apr 2025 13:41:51 +0000 (15:41 +0200)
quickjs.c
tests/test_builtin.js

index e6253e69256f2fd0b0c7677cbe368a5579e092cd..2b0de174802550fa346ac975a48b81195fa65591 100644 (file)
--- 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);
index 7ff303f8fe9adb981ca1515b09c7db47dfc2571a..38b26403511fbf73c3c558daf01b7d510a3cde09 100644 (file)
@@ -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);