summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/debug/wasm-sourceMappingURL.js
blob: 7f7f245faf928cde4b3d07043248324bc83c9521 (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
71
72
// |jit-test| test-also=--wasm-compiler=optimizing; skip-if: !wasmDebuggingEnabled()

// Tests that wasm module sourceMappingURL section is parsed.

load(libdir + "asserts.js");
load(libdir + "wasm-binary.js");

var g = newGlobal({newCompartment: true});
var dbg = new Debugger(g);

var gotScript;
dbg.allowWasmBinarySource = true;
dbg.onNewScript = (script) => {
  gotScript = script;
}

function toU8(array) {
    for (let b of array)
        assertEq(b < 256, true);
    return Uint8Array.from(array);
}

function varU32(u32) {
    assertEq(u32 >= 0, true);
    assertEq(u32 < Math.pow(2,32), true);
    var bytes = [];
    do {
        var byte = u32 & 0x7f;
        u32 >>>= 7;
        if (u32 != 0)
            byte |= 0x80;
        bytes.push(byte);
    } while (u32 != 0);
    return bytes;
}

function string(name) {
    var nameBytes = name.split('').map(c => {
        var code = c.charCodeAt(0);
        assertEq(code < 128, true); // TODO
        return code;
    });
    return varU32(nameBytes.length).concat(nameBytes);
}

function appendSourceMappingURL(wasmBytes, url) {
    if (!url)
        return wasmBytes;
    var payload = [...string('sourceMappingURL'), ...string(url)];
    return Uint8Array.from([...wasmBytes, userDefinedId, payload.length, ...payload]);
}
g.toWasm = (wast, url) => appendSourceMappingURL(wasmTextToBinary(wast), url);

// The sourceMappingURL section is not present
g.eval(`o = new WebAssembly.Instance(new WebAssembly.Module(toWasm('(module (func) (export "" (func 0)))')));`);
assertEq(gotScript.format, "wasm");
assertEq(gotScript.source.sourceMapURL, null);

// The sourceMappingURL section is present
g.eval(`o = new WebAssembly.Instance(new WebAssembly.Module(toWasm('(module (func) (export "a" (func 0)))', 'http://example.org/test')));`);
assertEq(gotScript.format, "wasm");
assertEq(gotScript.source.sourceMapURL, 'http://example.org/test');

// The sourceMapURL is read-only for wasm
assertThrowsInstanceOf(() => gotScript.source.sourceMapURL = 'foo', Error);

// The sourceMappingURL section is present, and is still available when wasm
// binary source is disabled.
dbg.allowWasmBinarySource = false;
g.eval(`o = new WebAssembly.Instance(new WebAssembly.Module(toWasm('(module (func) (export "a" (func 0)))', 'http://example.org/test2')));`);
assertEq(gotScript.format, "wasm");
assertEq(gotScript.source.sourceMapURL, 'http://example.org/test2');