summaryrefslogtreecommitdiff
path: root/test/binaryen.js/sourcemap.js
blob: f8deecf3fa991206b80a19e57efd81b6d79fff3d (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
function assert(x) {
  if (!x) throw 'error!';
}

function test() {
  var module = new Binaryen.Module();

  var fileIndex = module.addDebugInfoFileName("module.c");

  console.log(module.getDebugInfoFileName(fileIndex));
  console.log();

  var expr = module.i32.const(1);
  var body = module.block("", [
    expr
  ], Binaryen.i32);

  var func = module.addFunction("main", Binaryen.none, Binaryen.i32, [], body);

  module.setDebugLocation(func, expr, fileIndex, 1, 2);
  module.setDebugLocation(func, body, fileIndex, 0, 3);

  var output = module.emitBinary("module.wasm.map");
  assert(module.validate());

  function dumpBinary(buffer) {
    var hex = [], o, b, h;
    for (var i = 0; i < buffer.length; ++i) {
      o = i.toString(16);
      while (o.length < 3) o = "0" + o;
      if ((i & 15) === 0) hex.push((i ? "\n" : "") + o + ":");
      if ((b = buffer[i]) >= 0x21 && b <= 0x7e)
        h = String.fromCharCode(b) + ' ';
      else if ((h = b.toString(16)).length < 2)
        h = "0" + h;
      hex.push(h);
    }
    console.log(hex.join(" "));
  }

  dumpBinary(output.binary);
  console.log();
  console.log(output.sourceMap);
}

Binaryen.ready.then(test);