summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/gc/finalizationRegistry-ccw.js
blob: 88c98519ca7aa52c12a29b2d3057b52cf159eb64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Test combinations of arguments in different compartments.

gczeal(0);

let heldValues = [];

function ccwToObject() {
    return evalcx('({})', newGlobal({newCompartment: true}));
}

function newRegistry() {
  return new FinalizationRegistry(value => {
    heldValues.push(value);
  });
}

function ccwToRegistry() {
  let global = newGlobal({newCompartment: true});
  global.heldValues = heldValues;
  return global.eval(
    `new FinalizationRegistry(value => heldValues.push(value))`);
}

function incrementalGC() {
  startgc(1);
  while (gcstate() !== "NotActive") {
    gcslice(1000);
  }
}

// Test the case when the registry remains live.
for (let w of [false, true]) {
  for (let x of [false, true]) {
    for (let y of [false, true]) {
      for (let z of [false, true]) {
        let registry = w ? ccwToRegistry(w) : newRegistry();
        let target = x ? ccwToObject() : {};
        let heldValue = y ? ccwToObject() : {};
        let token = z ? ccwToObject() : {};
        registry.register(target, heldValue, token);
        registry.unregister(token);
        registry.register(target, heldValue, token);
        target = undefined;
        token = undefined;
        heldValue = undefined;
        incrementalGC();
        heldValues.length = 0; // Clear, don't replace.
        drainJobQueue();
        assertEq(heldValues.length, 1);
      }
    }
  }
}

// Test the case when registry has no more references.
for (let w of [false, true]) {
  for (let x of [false, true]) {
    for (let y of [false, true]) {
      for (let z of [false, true]) {
        let registry = w ? ccwToRegistry(w) : newRegistry();
        let target = x ? ccwToObject() : {};
        let heldValue = y ? ccwToObject() : {};
        let token = z ? ccwToObject() : {};
        registry.register(target, heldValue, token);
        registry.unregister(token);
        registry.register(target, heldValue, token);
        target = undefined;
        token = undefined;
        heldValue = undefined;
        registry = undefined; // Remove last reference to registry.
        incrementalGC();
        heldValues.length = 0;
        drainJobQueue();
        // The cleanup callback may or may not be run depending on
        // which order the zones are swept in, which itself depends on
        // the arrangement of CCWs.
        assertEq(heldValues.length <= 1, true);
      }
    }
  }
}