2015-12-11 12:26:16 +00:00
|
|
|
// Copyright 2015 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2015-12-17 10:54:08 +00:00
|
|
|
// Flags: --expose-wasm
|
|
|
|
|
2015-12-11 12:26:16 +00:00
|
|
|
load("test/mjsunit/wasm/wasm-constants.js");
|
2016-03-07 19:32:35 +00:00
|
|
|
load("test/mjsunit/wasm/wasm-module-builder.js");
|
2015-12-11 12:26:16 +00:00
|
|
|
|
|
|
|
var module = (function () {
|
2016-03-07 19:32:35 +00:00
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
2016-04-29 09:39:26 +00:00
|
|
|
var sig_index = builder.addSignature(kSig_i_ii);
|
2016-03-07 19:32:35 +00:00
|
|
|
builder.addImport("add", sig_index);
|
|
|
|
builder.addFunction("add", sig_index)
|
|
|
|
.addBody([
|
2016-04-29 09:15:26 +00:00
|
|
|
kExprGetLocal, 0, kExprGetLocal, 1, kExprCallImport, kArity2, 0
|
2016-03-07 19:32:35 +00:00
|
|
|
]);
|
|
|
|
builder.addFunction("sub", sig_index)
|
|
|
|
.addBody([
|
2016-04-29 09:15:26 +00:00
|
|
|
kExprGetLocal, 0, // --
|
|
|
|
kExprGetLocal, 1, // --
|
|
|
|
kExprI32Sub, // --
|
2016-03-07 19:32:35 +00:00
|
|
|
]);
|
2016-04-29 09:39:26 +00:00
|
|
|
builder.addFunction("main", kSig_i_iii)
|
2016-03-07 19:32:35 +00:00
|
|
|
.addBody([
|
|
|
|
kExprGetLocal, 0,
|
|
|
|
kExprGetLocal, 1,
|
2016-04-29 09:15:26 +00:00
|
|
|
kExprGetLocal, 2,
|
|
|
|
kExprCallIndirect, kArity2, sig_index
|
|
|
|
])
|
2016-03-07 19:32:35 +00:00
|
|
|
.exportFunc()
|
|
|
|
builder.appendToFunctionTable([0, 1, 2]);
|
|
|
|
|
|
|
|
return builder.instantiate({add: function(a, b) { return a + b | 0; }});
|
2015-12-11 12:26:16 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
// Check the module exists.
|
|
|
|
assertFalse(module === undefined);
|
|
|
|
assertFalse(module === null);
|
|
|
|
assertFalse(module === 0);
|
2016-03-07 19:32:35 +00:00
|
|
|
assertEquals("object", typeof module.exports);
|
|
|
|
assertEquals("function", typeof module.exports.main);
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-03-07 19:32:35 +00:00
|
|
|
assertEquals(5, module.exports.main(1, 12, 7));
|
|
|
|
assertEquals(19, module.exports.main(0, 12, 7));
|
2015-12-11 12:26:16 +00:00
|
|
|
|
2016-03-07 19:32:35 +00:00
|
|
|
assertTraps(kTrapFuncSigMismatch, "module.exports.main(2, 12, 33)");
|
|
|
|
assertTraps(kTrapFuncInvalid, "module.exports.main(3, 12, 33)");
|