tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test-incrementer.js (1873B)


      1 // Based on similar tests in html/infrastructure/safe-passing-of-structured-data/shared-array-buffers/.
      2 //
      3 // This file is simplified from the one there, because it only tests that the
      4 // module can be passed and that functions can be run. The SharedArrayBuffer
      5 // version also tests that the memory is shared between the agents.
      6 
      7 "use strict";
      8 
      9 function createWasmModule() {
     10  return fetch('incrementer.wasm')
     11      .then(response => {
     12        if (!response.ok)
     13          throw new Error(response.statusText);
     14        return response.arrayBuffer();
     15      })
     16      .then(WebAssembly.compile);
     17 }
     18 
     19 function testModule(module) {
     20  let instance = new WebAssembly.Instance(module);
     21  let increment = instance.exports["increment"];
     22  assert_equals(typeof increment, "function", `The type of the increment export should be "function", got ${typeof increment}`);
     23  let result = increment(42);
     24  assert_equals(result, 43, `increment(42) should be 43, got ${result}`);
     25 }
     26 
     27 self.testSharingViaIncrementerScript = (t, whereToListen, whereToListenLabel, whereToSend, whereToSendLabel, origin) => {
     28  return createWasmModule().then(module => {
     29    return new Promise(resolve => {
     30 
     31      whereToListen.onmessage = t.step_func(({ data }) => {
     32        switch (data.message) {
     33          case "module received": {
     34            testModule(data.module);
     35            resolve();
     36            break;
     37          }
     38        }
     39      });
     40 
     41      whereToSend.postMessage({ message: "send module", module }, origin);
     42    });
     43  });
     44 };
     45 
     46 self.setupDestinationIncrementer = (whereToListen, whereToSendBackTo, origin) => {
     47  whereToListen.onmessage = ({ data }) => {
     48    switch (data.message) {
     49      case "send module": {
     50        let module = data.module;
     51        testModule(data.module);
     52        whereToSendBackTo.postMessage({ message: "module received", module }, origin);
     53        break;
     54      }
     55    }
     56  };
     57 };