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

function cleanInfo(info) {
  var ret = {};
  for (var x in info) {
    if (x !== 'name' && x !== 'body' && x !== 'type') {
      ret[x] = info[x];
    }
  }
  return ret;
}

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

  var func = module.addFunction("a-function", Binaryen.none, Binaryen.i32, [],
    module.i32.add(
      module.i32.const(1),
      module.i32.const(2)
    )
  );

  console.log("GetFunction is equal: " + (func === module.getFunction("a-function")));

  module.runPassesOnFunction(func, ["precompute"]);

  var funcInfo = Binaryen.getFunctionInfo(func);
  console.log("getFunctionInfo=" + JSON.stringify(cleanInfo(funcInfo)));
  var expInfo = Binaryen.getExpressionInfo(funcInfo.body);
  console.log("getExpressionInfo(body)=" + JSON.stringify(cleanInfo(expInfo)));
  console.log(Binaryen.emitText(funcInfo.body));

  module.removeFunction("a-function");

  assert(module.validate());

  console.log(module.emitText());
}

Binaryen.ready.then(test);