| 1 |
// Test combinations of arguments in different compartments. |
| 2 |
|
| 3 |
gczeal(0); |
| 4 |
|
| 5 |
let heldValues = []; |
| 6 |
|
| 7 |
function ccwToObject() { |
| 8 |
return evalcx('({})', newGlobal({newCompartment: true})); |
| 9 |
} |
| 10 |
|
| 11 |
function newRegistry() { |
| 12 |
return new FinalizationRegistry(value => { |
| 13 |
heldValues.push(value); |
| 14 |
}); |
| 15 |
} |
| 16 |
|
| 17 |
function ccwToRegistry() { |
| 18 |
let global = newGlobal({newCompartment: true}); |
| 19 |
global.heldValues = heldValues; |
| 20 |
return global.eval( |
| 21 |
`new FinalizationRegistry(value => heldValues.push(value))`); |
| 22 |
} |
| 23 |
|
| 24 |
function incrementalGC() { |
| 25 |
startgc(1); |
| 26 |
while (gcstate() !== "NotActive") { |
| 27 |
gcslice(1000); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
// Test the case when the registry remains live. |
| 32 |
for (let w of [false, true]) { |
| 33 |
for (let x of [false, true]) { |
| 34 |
for (let y of [false, true]) { |
| 35 |
for (let z of [false, true]) { |
| 36 |
let registry = w ? ccwToRegistry(w) : newRegistry(); |
| 37 |
let target = x ? ccwToObject() : {}; |
| 38 |
let heldValue = y ? ccwToObject() : {}; |
| 39 |
let token = z ? ccwToObject() : {}; |
| 40 |
registry.register(target, heldValue, token); |
| 41 |
registry.unregister(token); |
| 42 |
registry.register(target, heldValue, token); |
| 43 |
target = undefined; |
| 44 |
token = undefined; |
| 45 |
heldValue = undefined; |
| 46 |
incrementalGC(); |
| 47 |
heldValues.length = 0; // Clear, don't replace. |
| 48 |
drainJobQueue(); |
| 49 |
assertEq(heldValues.length, 1); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// Test the case when registry has no more references. |
| 56 |
for (let w of [false, true]) { |
| 57 |
for (let x of [false, true]) { |
| 58 |
for (let y of [false, true]) { |
| 59 |
for (let z of [false, true]) { |
| 60 |
let registry = w ? ccwToRegistry(w) : newRegistry(); |
| 61 |
let target = x ? ccwToObject() : {}; |
| 62 |
let heldValue = y ? ccwToObject() : {}; |
| 63 |
let token = z ? ccwToObject() : {}; |
| 64 |
registry.register(target, heldValue, token); |
| 65 |
registry.unregister(token); |
| 66 |
registry.register(target, heldValue, token); |
| 67 |
target = undefined; |
| 68 |
token = undefined; |
| 69 |
heldValue = undefined; |
| 70 |
registry = undefined; // Remove last reference to registry. |
| 71 |
incrementalGC(); |
| 72 |
heldValues.length = 0; |
| 73 |
drainJobQueue(); |
| 74 |
// The cleanup callback may or may not be run depending on |
| 75 |
// which order the zones are swept in, which itself depends on |
| 76 |
// the arrangement of CCWs. |
| 77 |
assertEq(heldValues.length <= 1, true); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |