cleanup-prevented-with-reference.optional.any.js (1747B)
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 18 let holdingsList = []; 19 function cb(holding) { 20 holdingsList.push(holding); 21 }; 22 let finalizationRegistry = new FinalizationRegistry(function() {}); 23 24 let referenced = {}; 25 26 function emptyCells() { 27 let target = {}; 28 finalizationRegistry.register(target, 'target!'); 29 finalizationRegistry.register(referenced, 'referenced'); 30 31 let prom = maybeGarbageCollectAndCleanupAsync(target); 32 target = null; 33 34 return prom; 35 } 36 37 promise_test(() => { 38 return (async () => { 39 assert_implements( 40 typeof FinalizationRegistry.prototype.cleanupSome === 'function', 41 'FinalizationRegistry.prototype.cleanupSome is not implemented.' 42 ); 43 44 await emptyCells(); 45 finalizationRegistry.cleanupSome(cb); 46 47 assert_equals(holdingsList.length, 1); 48 assert_equals(holdingsList[0], 'target!'); 49 assert_equals(typeof referenced, 'object', 'referenced preserved'); 50 })().catch(resolveGarbageCollection); 51 }, 'cleanupCallback has only one optional chance to be called for a GC that cleans up a registered target.');