summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/unit/test_private_field_xrays.js
blob: bd37eb44c792b03c7376850ee8db2e6082185d78 (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
'use strict'

ChromeUtils.importESModule("resource://gre/modules/Preferences.sys.mjs");

add_task(async function () {
  let webnav = Services.appShell.createWindowlessBrowser(false);

  let docShell = webnav.docShell;

  docShell.createAboutBlankContentViewer(null, null);

  let window = webnav.document.defaultView;

  let iframe = window.eval(`
    iframe = document.createElement("iframe");
    iframe.id = "iframe"
    document.body.appendChild(iframe)
    iframe`);


  let unwrapped = Cu.waiveXrays(iframe);


  class Base {
    constructor(o) {
      return o;
    }
  };


  class A extends Base {
    #x = 12;
    static gx(o) {
      return o.#x;
    }

    static sx(o, v) {
      o.#x = v;
    }
  };

  new A(iframe);
  Assert.equal(A.gx(iframe), 12);
  A.sx(iframe, 'wrapped');

  // Shouldn't tunnel past xray.
  Assert.throws(() => A.gx(unwrapped), TypeError);
  Assert.throws(() => A.sx(unwrapped, 'unwrapped'), TypeError);

  new A(unwrapped);
  Assert.equal(A.gx(unwrapped), 12);
  Assert.equal(A.gx(iframe), 'wrapped');

  A.sx(iframe, 'modified');
  Assert.equal(A.gx(unwrapped), 12);
  A.sx(unwrapped, 16);
  Assert.equal(A.gx(iframe), 'modified');
});