summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/tests/mochitest/shadow_realm_worker.js
blob: c91c9bc30cd461b4dab2a70883917f4c6cd8f0c6 (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
var sr = new ShadowRealm();
var resolve;

var allSettled = new Promise((resolved) => { resolve = resolved });

self.onmessage = async function (e) {
  try {
    // Test evaluate
    if (e.data === 'evaluate') {
      sr.evaluate("var s = 'PASS set string in realm';")
      var res = sr.evaluate('s');
      postMessage(res);
      return;
    }

    // If Import works in a worker, then it ought to work in a shadow realm
    //
    // See  https://bugzilla.mozilla.org/show_bug.cgi?id=1247687 and
    //      https://bugzilla.mozilla.org/show_bug.cgi?id=1772162
    if (e.data == 'import') {
      var import_worked = false;
      var importValue_worked = false;
      var importNested_worked = false;
      try {
        var module = await import("./shadow_realm_module.js");
        if (module.x != 1) {
          throw "mismatch";
        }
        import_worked = true;
      } catch (e) { }

      try {
        await sr.importValue("./shadow_realm_module.js", 'x').then((x) => {
          if (x == 1) { importValue_worked = true; }
        });
      } catch (e) { }

      try {
        sr.evaluate(`
        var imported = false;
        import("./shadow_realm_module.js").then((module) => {
          if (module.x == 1) {
            imported = true;
          }
        });
        true;
        `);

        importNested_worked = sr.evaluate("imported");
      } catch (e) {

      }

      if (importValue_worked == importNested_worked) {
        postMessage(`PASS: import in workers ${
          import_worked ? "worked" : "failed"
        }. importValue, and nested import all ${
          importValue_worked ? "worked" : "failed"
        } `);
        resolve();
        return;
      }

      postMessage(`FAIL: importValue ${importValue_worked}, import ${import_worked}, importNested ${importNested_worked}`);
      resolve();
      return;
    }


    // Reply back with finish
    if (e.data == 'finish') {
      await allSettled;
      postMessage("finish");
      return;
    }
  } catch (e) {
    postMessage("FAIL: " + e.message);
  }
  postMessage('Unknown message type.');
}