summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/mochitest/test_getweakmapkeys.html
blob: 7942d9b945908600d8147fbc0e6469d8b59b6576 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=688277
-->
<head>
  <meta charset="utf-8">
  <title>Tests for nondeterministicGetWeakMapKeys</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  <script type="application/javascript">
  /** Test for Bug 688277 **/

  /* Fail gracefully if junk is passed in. */
  is(SpecialPowers.nondeterministicGetWeakMapKeys(11), undefined,
    "nondeterministicGetWeakMapKeys should return undefined for non-objects");
  is(SpecialPowers.nondeterministicGetWeakMapKeys({}), undefined,
    "nondeterministicGetWeakMapKeys should return undefined for non-weakmap objects");
  is(SpecialPowers.nondeterministicGetWeakMapKeys(null), undefined,
    "nondeterministicGetWeakMapKeys should return undefined for null");

  /* return an empty array for an empty WeakMap */
  let mempty = new WeakMap();
  is(SpecialPowers.nondeterministicGetWeakMapKeys(mempty).length, 0,
    "nondeterministicGetWeakMapKeys should return empty array for empty weakmap");

  /* Test freeing/nonfreeing. */
  let m = new WeakMap();
  let liveKeys = new Array();

  let add_elements = function () {
    let k1 = {};
    m.set(k1, "live1");
    liveKeys.push(k1);

    let k2 = {};
    m.set(k2, "dead1");

    let k = {};
    m.set(k, k); /* simple cycle */
  };

  add_elements();

  SpecialPowers.exactGC(function () {
    let keys = SpecialPowers.nondeterministicGetWeakMapKeys(m);
    is(liveKeys.length, 1, "Wrong number of live keys.");
    is(keys.length, 1, "Should have one weak map key.");
    is(m.get(keys[0]), "live1", "live1 should be live");
    SimpleTest.finish();
  });

  SimpleTest.waitForExplicitFinish();
  </script>
</head>
<body>
<p id="display"></p>
</body>
</html>