summaryrefslogtreecommitdiffstats
path: root/dom/asmjscache/test/test_cachingBasic.html
blob: e84fdba8b317e7591610de50fd2d98c2a402a4d5 (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
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=929236
-->
<head>
  <meta charset="utf-8">
  <title>asm.js browser tests</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
</head>
<body>
  <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=929236">asm.js browser tests</a>
  <p id="display"></p>
  <div id="content" style="display: none"></div>
  <pre id="test"></pre>

  <script>
  var jsFuns = SpecialPowers.Cu.getJSTestingFunctions();

  var code = "function f() { 'use asm';\n";
  for (var i = 0; i < 5000; i++)
    code += "function g" + i + "() { return " + i + "}\n";
  code += "return g42 }\n";
  code += "ok(jsFuns.isAsmJSModule(f), 'f is an asm.js module')\n";
  code += "var g42 = f();\n";
  code += "ok(jsFuns.isAsmJSFunction(g42), 'g42 is an asm.js function')\n";
  code += "ok(g42() === 42, 'g42 returns the correct result')\n";
  code += "finishedEvalAsync(f);";
  ok(code.length > 100000, "code is long enough to definitely hit the cache");

  function evalAsync(code) {
    var blob = new Blob([code], {type:"application/javascript"});
    var script = document.createElement('script');
    script.src = URL.createObjectURL(blob);
    document.body.appendChild(script);
  }

  var state = 0;
  function finishedEvalAsync(module) {
    switch (state) {
      case 0:
        state++;
        evalAsync(code);
        break;
      case 1:
        ok(!jsFuns.isAsmJSModuleLoadedFromCache(module), 'module not loaded from cache');
        SimpleTest.finish();
        break;
      default:
        throw "huh?";
    }
  }

  function runTest() {
      // generate a big ol asm.js module and compile it async so that we can hit
      // the asm.js cache.
      SimpleTest.waitForExplicitFinish();
      evalAsync(code);
  }

  if (!jsFuns.isAsmJSCompilationAvailable()) {
      ok(true, "isAsmJSCompilationAvailable is false, skipping this test!");
  } else {
      runTest();
  }
  </script>

</body>
</html>