tor-browser

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

wasm-sourceMappingURL.js (2406B)


      1 // |jit-test| test-also=--wasm-compiler=optimizing; skip-if: !wasmDebuggingEnabled()
      2 
      3 // Tests that wasm module sourceMappingURL section is parsed.
      4 
      5 load(libdir + "asserts.js");
      6 load(libdir + "wasm-binary.js");
      7 
      8 var g = newGlobal({newCompartment: true});
      9 var dbg = new Debugger(g);
     10 
     11 var gotScript;
     12 dbg.allowWasmBinarySource = true;
     13 dbg.onNewScript = (script) => {
     14  gotScript = script;
     15 }
     16 
     17 function toU8(array) {
     18    for (let b of array)
     19        assertEq(b < 256, true);
     20    return Uint8Array.from(array);
     21 }
     22 
     23 function varU32(u32) {
     24    assertEq(u32 >= 0, true);
     25    assertEq(u32 < Math.pow(2,32), true);
     26    var bytes = [];
     27    do {
     28        var byte = u32 & 0x7f;
     29        u32 >>>= 7;
     30        if (u32 != 0)
     31            byte |= 0x80;
     32        bytes.push(byte);
     33    } while (u32 != 0);
     34    return bytes;
     35 }
     36 
     37 function string(name) {
     38    var nameBytes = name.split('').map(c => {
     39        var code = c.charCodeAt(0);
     40        assertEq(code < 128, true); // TODO
     41        return code;
     42    });
     43    return varU32(nameBytes.length).concat(nameBytes);
     44 }
     45 
     46 function appendSourceMappingURL(wasmBytes, url) {
     47    if (!url)
     48        return wasmBytes;
     49    var payload = [...string('sourceMappingURL'), ...string(url)];
     50    return Uint8Array.from([...wasmBytes, userDefinedId, payload.length, ...payload]);
     51 }
     52 g.toWasm = (wast, url) => appendSourceMappingURL(wasmTextToBinary(wast), url);
     53 
     54 // The sourceMappingURL section is not present
     55 g.eval(`o = new WebAssembly.Instance(new WebAssembly.Module(toWasm('(module (func) (export "" (func 0)))')));`);
     56 assertEq(gotScript.format, "wasm");
     57 assertEq(gotScript.source.sourceMapURL, null);
     58 
     59 // The sourceMappingURL section is present
     60 g.eval(`o = new WebAssembly.Instance(new WebAssembly.Module(toWasm('(module (func) (export "a" (func 0)))', 'http://example.org/test')));`);
     61 assertEq(gotScript.format, "wasm");
     62 assertEq(gotScript.source.sourceMapURL, 'http://example.org/test');
     63 
     64 // The sourceMapURL is read-only for wasm
     65 assertThrowsInstanceOf(() => gotScript.source.sourceMapURL = 'foo', Error);
     66 
     67 // The sourceMappingURL section is present, and is still available when wasm
     68 // binary source is disabled.
     69 dbg.allowWasmBinarySource = false;
     70 g.eval(`o = new WebAssembly.Instance(new WebAssembly.Module(toWasm('(module (func) (export "a" (func 0)))', 'http://example.org/test2')));`);
     71 assertEq(gotScript.format, "wasm");
     72 assertEq(gotScript.source.sourceMapURL, 'http://example.org/test2');