blob: 325868743143d3e94cd4dcd3d844f2f303ad2782 [file] [log] [blame]
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001// Copyright 2015 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Flags: --expose-wasm
6
7load("test/mjsunit/wasm/wasm-constants.js");
Ben Murdochda12d292016-06-02 14:46:10 +01008load("test/mjsunit/wasm/wasm-module-builder.js");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00009
10var module = (function () {
Ben Murdochda12d292016-06-02 14:46:10 +010011 var builder = new WasmModuleBuilder();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000012
Ben Murdochda12d292016-06-02 14:46:10 +010013 var sig_index = builder.addSignature([kAstI32, kAstI32, kAstI32]);
14 builder.addImport("add", sig_index);
15 builder.addFunction("add", sig_index)
16 .addBody([
17 kExprCallImport, 0, kExprGetLocal, 0, kExprGetLocal, 1
18 ]);
19 builder.addFunction("sub", sig_index)
20 .addBody([
21 kExprI32Sub, kExprGetLocal, 0, kExprGetLocal, 1
22 ]);
23 builder.addFunction("main", [kAstI32, kAstI32, kAstI32, kAstI32])
24 .addBody([
25 kExprCallIndirect, sig_index,
26 kExprGetLocal, 0,
27 kExprGetLocal, 1,
28 kExprGetLocal, 2])
29 .exportFunc()
30 builder.appendToFunctionTable([0, 1, 2]);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000031
Ben Murdochda12d292016-06-02 14:46:10 +010032 return builder.instantiate({add: function(a, b) { return a + b | 0; }});
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000033})();
34
35// Check the module exists.
36assertFalse(module === undefined);
37assertFalse(module === null);
38assertFalse(module === 0);
Ben Murdochda12d292016-06-02 14:46:10 +010039assertEquals("object", typeof module.exports);
40assertEquals("function", typeof module.exports.main);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041
Ben Murdochda12d292016-06-02 14:46:10 +010042assertEquals(5, module.exports.main(1, 12, 7));
43assertEquals(19, module.exports.main(0, 12, 7));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044
Ben Murdochda12d292016-06-02 14:46:10 +010045assertTraps(kTrapFuncSigMismatch, "module.exports.main(2, 12, 33)");
46assertTraps(kTrapFuncInvalid, "module.exports.main(3, 12, 33)");