cleanup-prevented-with-unregister.optional.any.js (2066B)
1 // META: script=/common/gc.js 2 // META: script=resources/maybe-garbage-collect.js 3 // ├──> maybeGarbageCollectAndCleanupAsync 4 // └──> resolveGarbageCollection 5 /*--- 6 esid: sec-finalization-registry.prototype.cleanupSome 7 info: | 8 FinalizationRegistry.prototype.cleanupSome ( [ callback ] ) 9 10 1. Let finalizationRegistry be the this value. 11 2. If Type(finalizationRegistry) is not Object, throw a TypeError exception. 12 3. If finalizationRegistry does not have a [[Cells]] internal slot, throw a TypeError exception. 13 4. If callback is not undefined and IsCallable(callback) is false, throw a TypeError exception. 14 5. Perform ? CleanupFinalizationRegistry(finalizationRegistry, callback). 15 6. Return undefined. 16 17 FinalizationRegistry.prototype.unregister ( unregisterToken ) 18 19 1. Let removed be false. 20 2. For each Record { [[Target]], [[Holdings]], [[UnregisterToken]] } cell that is an element of finalizationRegistry.[[Cells]], do 21 a. If SameValue(cell.[[UnregisterToken]], unregisterToken) is true, then 22 i. Remove cell from finalizationRegistry.[[Cells]]. 23 ii. Set removed to true. 24 3. Return removed. 25 ---*/ 26 let token = {}; 27 let finalizationRegistry = new FinalizationRegistry(function() {}); 28 29 function emptyCells() { 30 let target = {}; 31 finalizationRegistry.register(target, 'target!', token); 32 33 let prom = maybeGarbageCollectAndCleanupAsync(target); 34 target = null; 35 36 return prom; 37 } 38 39 promise_test(() => { 40 return (async () => { 41 assert_implements( 42 typeof FinalizationRegistry.prototype.cleanupSome === 'function', 43 'FinalizationRegistry.prototype.cleanupSome is not implemented.' 44 ); 45 46 await emptyCells(); 47 let called = 0; 48 49 let res = finalizationRegistry.unregister(token); 50 assert_equals(res, true, 'unregister target before iterating over it in cleanup'); 51 52 finalizationRegistry.cleanupSome((holding) => { 53 called += 1; 54 }); 55 56 assert_equals(called, 0, 'callback was not called'); 57 })().catch(resolveGarbageCollection); 58 }, 'Cleanup might be prevented with an unregister usage');