test_webassembly_compile.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <!--
  2. Any copyright is dedicated to the Public Domain.
  3. http://creativecommons.org/publicdomain/zero/1.0/
  4. -->
  5. <html>
  6. <head>
  7. <title>WebAssembly.compile Test</title>
  8. <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  9. <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
  10. </head>
  11. <body>
  12. <script>
  13. const wasmTextToBinary = SpecialPowers.unwrap(SpecialPowers.Cu.getJSTestingFunctions().wasmTextToBinary);
  14. const wasmIsSupported = SpecialPowers.Cu.getJSTestingFunctions().wasmIsSupported
  15. const fooModuleCode = wasmTextToBinary(`(module
  16. (func $foo (result i32) (i32.const 42))
  17. (export "foo" $foo)
  18. )`, 'new-format');
  19. function checkFooModule(m) {
  20. ok(m instanceof WebAssembly.Module, "got a module");
  21. var i = new WebAssembly.Instance(m);
  22. ok(i instanceof WebAssembly.Instance, "got an instance");
  23. ok(i.exports.foo() === 42, "got 42");
  24. }
  25. function checkFooInstance(i) {
  26. ok(i instanceof WebAssembly.Instance, "got a module");
  27. ok(i.exports.foo() === 42, "got 42");
  28. }
  29. function propertiesExist() {
  30. if (!wasmIsSupported()) {
  31. ok(!this["WebAssembly"], "If the device doesn't support, there will be no WebAssembly object");
  32. SimpleTest.finish();
  33. return;
  34. }
  35. ok(WebAssembly, "WebAssembly object should exist");
  36. ok(WebAssembly.compile, "WebAssembly.compile function should exist");
  37. runTest();
  38. }
  39. function compileFail() {
  40. WebAssembly.compile().then(
  41. () => { ok(false, "should have failed"); runTest() }
  42. ).catch(
  43. err => { ok(err instanceof TypeError, "empty compile failed"); runTest() }
  44. );
  45. }
  46. function compileSuccess() {
  47. WebAssembly.compile(fooModuleCode).then(
  48. m => { checkFooModule(m); runTest() }
  49. ).catch(
  50. err => { ok(false, String(err)); runTest() }
  51. );
  52. }
  53. function compileManySuccess() {
  54. const N = 100;
  55. var arr = [];
  56. for (var i = 0; i < N; i++)
  57. arr.push(WebAssembly.compile(fooModuleCode));
  58. SpecialPowers.gc();
  59. Promise.all(arr).then (ms => {
  60. ok(ms.length === N, "got the right number");
  61. for (var i = 0; i < N; i++)
  62. checkFooModule(ms[i]);
  63. runTest();
  64. }).catch(
  65. err => { ok(false, String(err)); runTest() }
  66. );
  67. }
  68. function compileInWorker() {
  69. var w = new Worker(`data:text/plain,
  70. onmessage = e => {
  71. WebAssembly.compile(e.data).then(m => {
  72. var i = new WebAssembly.Instance(m);
  73. if (i.exports.foo() !== 42)
  74. throw "bad i.exports.foo() result";
  75. postMessage("ok");
  76. close();
  77. }).catch(err => { throw err });
  78. }
  79. `);
  80. w.postMessage(fooModuleCode);
  81. w.onmessage = e => {
  82. ok(e.data === "ok", "worker test");
  83. runTest();
  84. }
  85. }
  86. function terminateCompileInWorker() {
  87. var w = new Worker(`data:text/plain,
  88. var fooModuleCode;
  89. function spawnWork() {
  90. const N = 100;
  91. var arr = [];
  92. for (var i = 0; i < N; i++)
  93. arr.push(WebAssembly.compile(fooModuleCode));
  94. Promise.all(arr).then(spawnWork);
  95. }
  96. onmessage = e => {
  97. fooModuleCode = e.data;
  98. spawnWork();
  99. postMessage("ok");
  100. }
  101. `);
  102. w.postMessage(fooModuleCode);
  103. w.onmessage = e => {
  104. ok(e.data === "ok", "worker finished first step");
  105. w.terminate();
  106. runTest();
  107. }
  108. }
  109. function instantiateFail() {
  110. WebAssembly.instantiate().then(
  111. () => { ok(false, "should have failed"); runTest() }
  112. ).catch(
  113. err => { ok(err instanceof TypeError, "empty compile failed"); runTest() }
  114. );
  115. }
  116. function instantiateSuccess() {
  117. WebAssembly.instantiate(fooModuleCode).then(
  118. r => { checkFooModule(r.module); checkFooInstance(r.instance); runTest() }
  119. ).catch(
  120. err => { ok(false, String(err)); runTest() }
  121. );
  122. }
  123. function chainSuccess() {
  124. WebAssembly.compile(fooModuleCode).then(
  125. m => WebAssembly.instantiate(m)
  126. ).then(
  127. i => { checkFooInstance(i); runTest() }
  128. ).catch(
  129. err => { ok(false, String(err)); runTest() }
  130. );
  131. }
  132. var tests = [ propertiesExist,
  133. compileFail,
  134. compileSuccess,
  135. compileManySuccess,
  136. compileInWorker,
  137. terminateCompileInWorker,
  138. instantiateFail,
  139. instantiateSuccess,
  140. chainSuccess
  141. ];
  142. function runTest() {
  143. if (!tests.length) {
  144. SimpleTest.finish();
  145. return;
  146. }
  147. var test = tests.shift();
  148. test();
  149. }
  150. SimpleTest.waitForExplicitFinish();
  151. SpecialPowers.pushPrefEnv({"set": [["javascript.options.wasm", true]]}, runTest);
  152. </script>
  153. </body>
  154. </html>