finalizationRegistry-ccw-with-symbol-keys.js (2930B)
1 // |jit-test| --enable-symbols-as-weakmap-keys 2 3 // Test combinations of arguments in different compartments. 4 5 gczeal(0); 6 7 let heldValues = []; 8 9 function ccwToObject() { 10 return evalcx('({})', newGlobal({newCompartment: true})); 11 } 12 13 function newRegistry() { 14 return new FinalizationRegistry(value => { 15 heldValues.push(value); 16 }); 17 } 18 19 function ccwToRegistry() { 20 let global = newGlobal({newCompartment: true}); 21 global.heldValues = heldValues; 22 return global.eval( 23 `new FinalizationRegistry(value => heldValues.push(value))`); 24 } 25 26 function incrementalGC() { 27 startgc(1); 28 while (gcstate() !== "NotActive") { 29 gcslice(1000); 30 } 31 } 32 33 const RegistryKinds = ['SameZoneRegistry', 'OtherZoneRegistry']; 34 function makeRegistry(kind) { 35 if (kind === 'SameZoneRegistry') { 36 return new FinalizationRegistry(value => { 37 heldValues.push(value); 38 }); 39 } 40 41 assertEq(kind, 'OtherZoneRegistry'); 42 let global = newGlobal({newCompartment: true}); 43 global.heldValues = heldValues; 44 return global.eval( 45 `new FinalizationRegistry(value => heldValues.push(value))`); 46 return evalcx('({})', newGlobal({newCompartment: true})); 47 } 48 49 const ObjectKinds = ['SameZoneObject', 'OtherZoneObject']; 50 function makeObject(kind) { 51 if (kind === 'SameZoneObject') { 52 return {}; 53 } 54 55 assertEq(kind, 'OtherZoneObject'); 56 return evalcx('({})', newGlobal({newCompartment: true})); 57 } 58 59 const TargetKinds = ['SameZoneObject', 'OtherZoneObject', 'Symbol']; 60 function makeTarget(kind) { 61 if (kind === 'SameZoneObject') { 62 return {}; 63 } 64 65 if (kind === 'OtherZoneObject') { 66 return evalcx('({})', newGlobal({newCompartment: true})); 67 } 68 69 assertEq(kind, 'Symbol'); 70 return Symbol(); 71 } 72 73 for (let registryReachable of [false, true]) { 74 for (let registryKind of RegistryKinds) { 75 for (let targetKind of TargetKinds) { 76 for (let heldValueKind of ObjectKinds) { 77 for (let tokenKind of TargetKinds) { 78 let registry = makeRegistry(registryKind); 79 let target = makeTarget(targetKind); 80 let heldValue = makeObject(heldValueKind); 81 let token = makeTarget(tokenKind); 82 83 registry.register(target, heldValue, token); 84 registry.unregister(token); 85 86 registry.register(target, heldValue, token); 87 88 target = undefined; 89 token = undefined; 90 heldValue = undefined; 91 if (!registryReachable) { 92 registry = undefined; 93 } 94 incrementalGC(); 95 heldValues.length = 0; // Clear, don't replace. 96 drainJobQueue(); 97 98 if (registryReachable) { 99 assertEq(heldValues.length, 1); 100 } else { 101 // The cleanup callback may or may not be run depending on 102 // which order the zones are swept in, which itself depends on 103 // the arrangement of CCWs. 104 assertEq(heldValues.length <= 1, true); 105 } 106 } 107 } 108 } 109 } 110 }