unregister-cleaned-up-cell.optional.any.js (2300B)
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.unregister 7 info: | 8 FinalizationRegistry.prototype.cleanupSome ( [ callback ] ) 9 10 1. Let finalizationRegistry be the this value. 11 ... 12 5. Perform ! CleanupFinalizationRegistry(finalizationRegistry, callback). 13 ... 14 15 CleanupFinalizationRegistry ( finalizationRegistry [ , callback ] ) 16 17 ... 18 3. While finalizationRegistry.[[Cells]] contains a Record cell such that cell.[[WeakRefTarget]] is ~empty~, then an implementation may perform the following steps, 19 a. Choose any such cell. 20 b. Remove cell from finalizationRegistry.[[Cells]]. 21 c. Perform ? Call(callback, undefined, << cell.[[HeldValue]] >>). 22 ... 23 24 FinalizationRegistry.prototype.unregister ( unregisterToken ) 25 26 1. Let removed be false. 27 2. For each Record { [[Target]], [[Holdings]], [[UnregisterToken]] } cell that is an element of finalizationRegistry.[[Cells]], do 28 a. If SameValue(cell.[[UnregisterToken]], unregisterToken) is true, then 29 i. Remove cell from finalizationRegistry.[[Cells]]. 30 ii. Set removed to true. 31 3. Return removed. 32 33 ---*/ 34 35 let value = 'target!'; 36 let token = {}; 37 let finalizationRegistry = new FinalizationRegistry(function() {}); 38 39 function emptyCells() { 40 let target = {}; 41 finalizationRegistry.register(target, value, token); 42 43 let prom = maybeGarbageCollectAndCleanupAsync(target); 44 target = null; 45 46 return prom; 47 } 48 49 promise_test(() => { 50 return (async () => { 51 assert_implements( 52 typeof FinalizationRegistry.prototype.cleanupSome === 'function', 53 'FinalizationRegistry.prototype.cleanupSome is not implemented.' 54 ); 55 56 await emptyCells(); 57 let called = 0; 58 let holdings = []; 59 finalizationRegistry.cleanupSome((holding) => { 60 called += 1; 61 holdings.push(holding); 62 }); 63 64 assert_equals(called, 1); 65 assert_equals(holdings.length, 1); 66 assert_equals(holdings[0], value); 67 68 let res = finalizationRegistry.unregister(token); 69 assert_equals(res, false, 'unregister after iterating over it in cleanup'); 70 71 })().catch(resolveGarbageCollection); 72 }, 'Cannot unregister a cell that has been cleaned up');