summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/chrome/test_discardSystemSource.xhtml
blob: 86a353e5807c7e1f5262935763ae85e63657e353 (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
<?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="chrome://global/skin"?>
<?xml-stylesheet type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=990353
-->
<window title="Mozilla Bug 990353"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
  <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"/>

  <!-- test results are displayed in the html:body -->
  <body xmlns="http://www.w3.org/1999/xhtml">
  <a href="https://bugzilla.mozilla.org/show_bug.cgi?id=990353"
     target="_blank">Mozilla Bug 990353</a>
  </body>

  <!-- test code goes here -->
  <script type="application/javascript">
  <![CDATA[
  /** Test for Bug 990353 **/
  SimpleTest.waitForExplicitFinish();

  function canary() {
    // eslint-disable-next-line no-unused-vars
    var someBitOfSource = 42;
  }

  var gLoadCount = 0;
  function frameLoaded() {
    switch (++gLoadCount) {
      case 1:
        ok(/native code/.test(window[0].canary.toString()), "System function should be sourceless: " + window[0].canary.toString());
        ok(/native code/.test(window[0].onload.toString()), "System event handler should be sourceless: " + window[0].onload.toString());
        var sb = new Cu.Sandbox("https://www.example.com", { discardSource: true });
        Cu.evalInSandbox('function canary() { var someBitOfSource = 42; }', sb);
        ok(/native code/.test(sb.canary.toString()), "Function from sandbox with explicit discarding should be sourceless");
        try {
          window[0].throwSomething();
          ok(false, "should have thrown");
        } catch (e) {
          ok(/some error/.test(e), "Threw exception as expected: " + e);
          ok(/throwSomething/.test(e.stack), "Exception stack trace works: " + e.stack);
        }
        window[0].location = "https://example.org/tests/js/xpconnect/tests/chrome/file_discardSystemSource.html";
        break;
      case 2:
        ok(/someBitOfSource/.test(Cu.waiveXrays(window[0]).canary.toString()), "Content function should have source");
        ok(/someBitOfSource/.test(Cu.waiveXrays(window[0]).onload.toString()), "Content event handler should have source");
        testWorker();
        break;
    }
  }

  function testWorker() {
    var worker = new window[0].wrappedJSObject.Worker('worker_discardSystemSource.js');
    worker.onmessage = function(evt) {
      ok(/someBitOfSource/.test(evt.data), "Non-chrome worker should have source: " + evt.data);
      var chromeWorker = new Worker('worker_discardSystemSource.js');
      chromeWorker.onmessage = function(evt) {
        ok(/native code/.test(evt.data), "Chrome worker should not have source: " + evt.data);
        SimpleTest.finish();
      }
    }
  }

  function go() {
    // We should have our own source, because the pref wasn't enabled when we
    // were loaded.
    ok(/someBitOfSource/.test(canary.toString()), "Should have own source");

    window[0].frameElement.onload = frameLoaded;
    window[0].location = "file_discardSystemSource.html";
  }
  addLoadEvent(function() {
    SpecialPowers.pushPrefEnv({set: [['javascript.options.discardSystemSource', true]]}, go);
  });

  ]]>
  </script>
  <iframe></iframe>
</window>