blob: 1e87c6f8230fd862b49acfeddbe0bcfe491285d0 [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 Murdoch61f157c2016-09-16 13:49:30 +010013 var sig_index = builder.addType(kSig_i_ii);
Ben Murdochda12d292016-06-02 14:46:10 +010014 builder.addImport("add", sig_index);
15 builder.addFunction("add", sig_index)
16 .addBody([
Ben Murdochc5610432016-08-08 18:44:38 +010017 kExprGetLocal, 0, kExprGetLocal, 1, kExprCallImport, kArity2, 0
Ben Murdochda12d292016-06-02 14:46:10 +010018 ]);
19 builder.addFunction("sub", sig_index)
20 .addBody([
Ben Murdochc5610432016-08-08 18:44:38 +010021 kExprGetLocal, 0, // --
22 kExprGetLocal, 1, // --
23 kExprI32Sub, // --
Ben Murdochda12d292016-06-02 14:46:10 +010024 ]);
Ben Murdochc5610432016-08-08 18:44:38 +010025 builder.addFunction("main", kSig_i_iii)
Ben Murdochda12d292016-06-02 14:46:10 +010026 .addBody([
Ben Murdochda12d292016-06-02 14:46:10 +010027 kExprGetLocal, 0,
28 kExprGetLocal, 1,
Ben Murdochc5610432016-08-08 18:44:38 +010029 kExprGetLocal, 2,
30 kExprCallIndirect, kArity2, sig_index
31 ])
Ben Murdochda12d292016-06-02 14:46:10 +010032 .exportFunc()
Ben Murdoch61f157c2016-09-16 13:49:30 +010033 builder.appendToTable([0, 1, 2]);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000034
Ben Murdochda12d292016-06-02 14:46:10 +010035 return builder.instantiate({add: function(a, b) { return a + b | 0; }});
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000036})();
37
38// Check the module exists.
39assertFalse(module === undefined);
40assertFalse(module === null);
41assertFalse(module === 0);
Ben Murdochda12d292016-06-02 14:46:10 +010042assertEquals("object", typeof module.exports);
43assertEquals("function", typeof module.exports.main);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000044
Ben Murdochda12d292016-06-02 14:46:10 +010045assertEquals(5, module.exports.main(1, 12, 7));
46assertEquals(19, module.exports.main(0, 12, 7));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000047
Ben Murdochda12d292016-06-02 14:46:10 +010048assertTraps(kTrapFuncSigMismatch, "module.exports.main(2, 12, 33)");
49assertTraps(kTrapFuncInvalid, "module.exports.main(3, 12, 33)");