tor-browser

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

streaming.js (3886B)


      1 // |jit-test| skip-if: !wasmStreamingEnabled()
      2 
      3 load(libdir + "wasm-binary.js");
      4 
      5 function testInstantiate(source, importObj, exportName, expectedValue) {
      6    var result;
      7    WebAssembly.instantiateStreaming(code, importObj).then(r => { result = r });
      8    drainJobQueue();
      9    assertEq(result !== undefined, true);
     10    assertEq(result.module instanceof WebAssembly.Module, true);
     11    assertEq(result.instance instanceof WebAssembly.Instance, true);
     12    assertEq(result.instance.exports[exportName](), expectedValue);
     13 }
     14 function testBoth(source, exportName, expectedValue) {
     15    var module;
     16    WebAssembly.compileStreaming(code).then(m => { module = m });
     17    drainJobQueue();
     18    assertEq(module !== undefined, true);
     19    assertEq(module instanceof WebAssembly.Module, true);
     20    assertEq(new WebAssembly.Instance(module).exports[exportName](), expectedValue);
     21 
     22    testInstantiate(source, undefined, exportName, expectedValue);
     23 }
     24 
     25 function testFailInstantiate(source, importObj, error) {
     26    var caught = false;
     27    WebAssembly.instantiateStreaming(source).catch(err => {
     28        assertEq(err instanceof error, true);
     29        caught = true;
     30    });
     31    drainJobQueue();
     32    assertEq(caught, true);
     33 }
     34 function testFailBoth(source, error) {
     35    var caught = false;
     36    WebAssembly.compileStreaming(source).catch(err => {
     37        assertEq(err instanceof error, true);
     38        caught = true;
     39    });
     40    drainJobQueue();
     41    assertEq(caught, true);
     42 
     43    testFailInstantiate(source, undefined, error);
     44 }
     45 
     46 var code = wasmTextToBinary('(module (func (export "run") (result i32) i32.const 42))');
     47 testBoth(code, 'run', 42);
     48 testFailBoth(42, TypeError);
     49 testBoth(Promise.resolve(code), 'run', 42);
     50 testFailBoth(Promise.resolve(42), TypeError);
     51 testFailBoth(Promise.reject(new String("fail")), String);
     52 testBoth({then(resolve) { resolve(code) }}, 'run', 42);
     53 testFailBoth({then(resolve) { resolve(42) }}, TypeError);
     54 testFailBoth(new Promise((resolve, reject) => { reject(new Error("hi")) }), Error);
     55 testFailBoth(new Promise((resolve, reject) => { reject(new String("hi")) }), String);
     56 
     57 var code = wasmTextToBinary('(module (func $im (import "js" "foo") (result i32)) (func (export "run") (result i32) (i32.add (i32.const 1) (call $im))))');
     58 testInstantiate(code, {js:{foo() { return 42 }}}, 'run', 43);
     59 testFailInstantiate(code, null, TypeError);
     60 testFailInstantiate(code, {js:42}, TypeError);
     61 testFailInstantiate(code, {js:{foo:42}}, TypeError);
     62 
     63 var text = `(module\n`;
     64 text += ` (func (result i32) i32.const 0)\n`;
     65 for (var i = 1; i <= 200; i++)
     66    text += ` (func (result i32) (i32.add (i32.const ${i}) (call ${i-1})))\n`;
     67 text += ` (func (export "run") (result i32) call 100)\n`;
     68 text += `)`;
     69 var code = wasmTextToBinary(text);
     70 
     71 // fuzzing-safe disables setBufferStreamParams
     72 if (typeof setBufferStreamParams == 'function') {
     73    assertEq(code.length > 1000, true);
     74    for ([delayMillis, chunkSize] of [[0, 10], [1, 10], [0, 100], [1, 100], [0, 1000], [1, 1000], [10, 1000]]) {
     75        setBufferStreamParams(delayMillis, chunkSize);
     76        testBoth(code, 'run', 5050);
     77    }
     78 
     79    setBufferStreamParams(1, 100);
     80    var arr = [];
     81    for (var i = 0; i < 10; i++)
     82        arr.push(WebAssembly.instantiateStreaming(code));
     83    var results;
     84    Promise.all(arr).then(r => results = r);
     85    drainJobQueue();
     86    assertEq(results.length === 10, true);
     87    for (var i = 0; i < 10; i++)
     88        assertEq(results[i].instance.exports.run(), 5050);
     89 }
     90 
     91 // No code section, but data section:
     92 var code = wasmTextToBinary('(module (memory (import "js" "mem") 1) (data (i32.const 0) "a"))');
     93 var mem = new WebAssembly.Memory({initial:1});
     94 WebAssembly.instantiateStreaming(code, {js:{mem}});
     95 drainJobQueue();
     96 assertEq(new Uint8Array(mem.buffer)[0], 97);
     97 
     98 // Junk section before code section.
     99 testFailBoth(moduleWithSections([{name: 100, body: [1, 2, 3]}, bodySection([])]), WebAssembly.CompileError);