summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/gc/weakRefs.js
blob: e052aa779b098b7e06995e33ef8c1d004739c30f (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// https://tc39.es/ecma262/#sec-addtokeptobjects
// When the abstract operation AddToKeptObjects is called with a target object
// reference, it adds the target to an identity Set that will point strongly at
// the target until the end of the current Job.
//
// https://tc39.es/proposal-weakrefs/#sec-weakref-invariants
// When WeakRef.prototype.deref is called, the referent (if it's not already
// dead) is kept alive so that subsequent, synchronous accesses also return the
// object.

function testSameCompartmentWeakRef(
  targetReachable,
  weakRefReachable) {

  let target = {};

  let weakref = new WeakRef(target);
  assertEq(weakref.deref(), target);

  if (!targetReachable) {
    target = undefined;
  }

  if (!weakRefReachable) {
    weakRef = undefined;
  }

  clearKeptObjects();
  gc();

  if (weakRefReachable) {
    if (targetReachable) {
      assertEq(weakref.deref(), target);
    } else {
      assertEq(weakref.deref(), undefined);
    }
  }
}

let serial = 0;

function testCrossCompartmentWeakRef(
  targetReachable,
  weakRefReachable,
  collectTargetZone,
  collectWeakRefZone,
  sameZone) {

  gc();

  let id = serial++;
  let global = newGlobal(sameZone ? {sameZoneAs: this} : {newCompartment: true});
  global.eval('var target = {};');
  global.target.id = id;

  let weakref = new WeakRef(global.target);
  assertEq(weakref.deref(), global.target);

  if (!targetReachable) {
    global.target = undefined;
  }

  if (!weakRefReachable) {
    weakRef = undefined;
  }

  if (collectTargetZone || collectWeakRefZone) {
    clearKeptObjects();

    if (collectTargetZone) {
      schedulezone(global);
    }
    if (collectWeakRefZone) {
      schedulezone(this);
    }

    // Incremental GC so we use sweep groups. Shrinking GC to test updating
    // pointers.
    startgc(1, 'shrinking');
    while (gcstate() !== 'NotActive') {
      gcslice(1000, {dontStart: true});
    }
  }

  if (!(collectWeakRefZone && !weakRefReachable)) {
    if (collectTargetZone && !targetReachable) {
      assertEq(weakref.deref(), undefined);
    } else if (targetReachable) {
      assertEq(weakref.deref(), global.target);
    } else {
      // Target is not strongly reachable but hasn't been collected yet. We
      // can get it back through deref() but must check it based on properties.
      assertEq(weakref.deref() !== undefined, true);
      assertEq(weakref.deref().id, id);
    }
  }
}

gczeal(0);

for (let targetReachable of [true, false]) {
  for (let weakRefReachable of [true, false]) {
    testSameCompartmentWeakRef(targetReachable, weakRefReachable);
  }
}

for (let targetReachable of [true, false]) {
  for (let weakRefReachable of [true, false]) {
    for (let collectTargetZone of [true, false]) {
      for (let collectWeakRefZone of [true, false]) {
        for (let sameZone of [true, false]) {
          if (sameZone && (collectTargetZone != collectWeakRefZone)) {
            continue;
          }

          testCrossCompartmentWeakRef(targetReachable, weakRefReachable,
                                      collectTargetZone, collectWeakRefZone, sameZone);
        }
      }
    }
  }
}