blob: 14567d3844595ee896ea58c0a4b2b7f50abe2628 [file] [log] [blame]
Ben Murdoch61f157c2016-09-16 13:49:30 +01001// Copyright 2015 the V8 project authors. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5
6// Flags: --expose-wasm
7// Flags: --wasm-jit-prototype
8
9load("test/mjsunit/wasm/wasm-constants.js");
10load("test/mjsunit/wasm/wasm-module-builder.js");
11
12var module = (function () {
13 var builder = new WasmModuleBuilder();
14
15 var sig_index = builder.addType(kSig_i_ii);
16 builder.addPadFunctionTable(512);
17 builder.addImport("add", sig_index);
18 builder.addFunction("add", sig_index)
19 .addBody([
20 kExprGetLocal, 0, kExprGetLocal, 1, kExprCallImport, kArity2, 0
21 ]);
22 builder.addFunction("sub", sig_index)
23 .addBody([
24 kExprGetLocal, 0, // --
25 kExprGetLocal, 1, // --
26 kExprI32Sub, // --
27 ]);
28 builder.addFunction("main", kSig_i_iii)
29 .addBody([
30 kExprGetLocal, 0,
31 kExprGetLocal, 1,
32 kExprGetLocal, 2,
33 kExprCallIndirect, kArity2, sig_index
34 ])
35 .exportFunc()
36 builder.appendToTable([0, 1, 2]);
37
38 return builder.instantiate({add: function(a, b) { return a + b | 0; }});
39})();
40
41// Check the module exists.
42assertFalse(module === undefined);
43assertFalse(module === null);
44assertFalse(module === 0);
45assertEquals("object", typeof module.exports);
46assertEquals("function", typeof module.exports.main);
47
48assertEquals(5, module.exports.main(1, 12, 7));
49assertEquals(19, module.exports.main(0, 12, 7));
50
51assertTraps(kTrapFuncSigMismatch, "module.exports.main(2, 12, 33)");
52assertTraps(kTrapFuncSigMismatch, "module.exports.main(4, 12, 33)");
53assertTraps(kTrapFuncSigMismatch, "module.exports.main(511, 12, 33)");
54assertTraps(kTrapFuncInvalid, "module.exports.main(512, 12, 33)");
55assertTraps(kTrapFuncInvalid, "module.exports.main(1025, 12, 33)");
56assertTraps(kTrapFuncInvalid, "module.exports.main(-1, 12, 33)");