summaryrefslogtreecommitdiff
path: root/test/binaryen.js/test.js
blob: 3b6d9471ba141a528f5697e593fbe6bc74e97fab (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

Binaryen = Binaryen(); // instantiate the module

var input =
  '(module\n' +
  '  (export "add" $add)\n' +
  '  (func $add (param $x f64) (param $y f64) (result f64)\n' +
  '    (f64.add\n' +
  '      (get_local $x)\n' +
  '      (get_local $y)\n' +
  '    )\n' +
  '  )\n' +
  ')\n';

console.log('input:');
console.log(input);
console.log('================');

var module = new Binaryen.Module();
var parser = new Binaryen.SExpressionParser(input);

console.log('s-expr dump:');
parser.get_root().dump();
var s_module = parser.get_root().getChild(0);
console.log('================');

var builder = new Binaryen.SExpressionWasmBuilder(module, s_module);

console.log('module:');
Binaryen.WasmPrinter.prototype.printModule(module);
console.log('================');

var interface_ = new Binaryen.ShellExternalInterface();
var instance = new Binaryen.ModuleInstance(module, interface_);

var name = new Binaryen.Name('add');
console.log('name: ' + name.c_str());

var args = new Binaryen.LiteralList();
args.push_back(new Binaryen.Literal(40));
args.push_back(new Binaryen.Literal(2));

console.log('answer is ' + instance.callExport(name, args).getf64() + '.');