From 8c58e01928ca5e69dfd6906850bdbc6061b58c30 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 4 Sep 2024 12:32:32 +0200 Subject: [PATCH] Fix FinalizationRegistry with primitive held value (#496) Apparently test262 does not test FinalizationRegistry#register() with held values that are not objects. Fixes: https://github.com/quickjs-ng/quickjs/issues/494 --- quickjs.c | 2 +- tests/test_builtin.js | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/quickjs.c b/quickjs.c index 7883095..64db3db 100644 --- a/quickjs.c +++ b/quickjs.c @@ -53083,7 +53083,7 @@ static void reset_weak_ref(JSRuntime *rt, JSWeakRefRecord **first_weak_ref) /** * During the GC sweep phase the held object might be collected first. */ - if (JS_IsLiveObject(frd->ctx->rt, fre->held_val)) { + if (!JS_IsObject(fre->held_val) || JS_IsLiveObject(frd->ctx->rt, fre->held_val)) { JSValue func = js_dup(frd->cb); JSValue ret = JS_Call(frd->ctx, func, JS_UNDEFINED, 1, &fre->held_val); JS_FreeValueRT(rt, func); diff --git a/tests/test_builtin.js b/tests/test_builtin.js index 1e54587..a5b03a0 100644 --- a/tests/test_builtin.js +++ b/tests/test_builtin.js @@ -992,6 +992,23 @@ function test_proxy_is_array() } } +function test_finalization_registry() +{ + { + let expected = {}; + let actual; + let finrec = new FinalizationRegistry(v => { actual = v }); + finrec.register({}, expected); // {} goes out of scope immediately + assert(actual, expected); + } + { + let actual; + let finrec = new FinalizationRegistry(v => { actual = v }); + finrec.register({}, 42); // {} goes out of scope immediately + assert(actual, 42); + } +} + function test_cur_pc() { var a = []; @@ -1059,6 +1076,7 @@ test_weak_set(); test_generator(); test_proxy_iter(); test_proxy_is_array(); +test_finalization_registry(); test_exception_source_pos(); test_function_source_pos(); test_exception_prepare_stack();