2016-03-07 10:01:24 +00:00
|
|
|
// Copyright 2016 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.
|
|
|
|
|
|
|
|
// Flags: --expose-wasm
|
|
|
|
|
|
|
|
load('test/mjsunit/wasm/wasm-module-builder.js');
|
|
|
|
|
2016-09-27 20:46:10 +00:00
|
|
|
var debug = true;
|
2016-03-07 10:12:52 +00:00
|
|
|
|
2016-10-26 16:58:40 +00:00
|
|
|
function instantiate(buffer, ffi) {
|
2017-11-18 23:20:09 +00:00
|
|
|
return new WebAssembly.Instance(new WebAssembly.Module(buffer), ffi);
|
2016-10-26 16:58:40 +00:00
|
|
|
}
|
|
|
|
|
2016-03-07 10:01:24 +00:00
|
|
|
(function BasicTest() {
|
2019-07-30 11:19:40 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
builder.addMemory(1, 2, false);
|
|
|
|
builder.addFunction('foo', kSig_i_v)
|
|
|
|
.addBody([kExprI32Const, 11])
|
|
|
|
.exportAs('blarg');
|
|
|
|
|
|
|
|
var buffer = builder.toBuffer(debug);
|
|
|
|
var instance = instantiate(buffer);
|
|
|
|
assertEquals(11, instance.exports.blarg());
|
2016-03-07 10:01:24 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function ImportTest() {
|
2019-07-30 11:19:40 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
var index = builder.addImport('', 'print', makeSig_v_x(kWasmI32));
|
|
|
|
builder.addFunction('foo', kSig_v_v)
|
|
|
|
.addBody([kExprI32Const, 13, kExprCallFunction, index])
|
|
|
|
.exportAs('main');
|
|
|
|
|
|
|
|
var buffer = builder.toBuffer(debug);
|
|
|
|
var instance = instantiate(buffer, {'': {print: print}});
|
|
|
|
print('should print 13! ');
|
|
|
|
instance.exports.main();
|
2016-03-07 10:01:24 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function LocalsTest() {
|
2019-07-30 11:19:40 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
builder.addFunction(undefined, kSig_i_i)
|
2020-09-10 12:39:52 +00:00
|
|
|
.addLocals(kWasmI32, 1)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 0, kExprLocalSet, 1, kExprLocalGet, 1])
|
2019-07-30 11:19:40 +00:00
|
|
|
.exportAs('main');
|
|
|
|
|
|
|
|
var buffer = builder.toBuffer(debug);
|
|
|
|
var instance = instantiate(buffer);
|
|
|
|
assertEquals(19, instance.exports.main(19));
|
|
|
|
assertEquals(27777, instance.exports.main(27777));
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function LocalsTest2() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
// TODO(titzer): i64 only works on 64-bit platforms.
|
|
|
|
var types = [
|
2020-09-10 12:39:52 +00:00
|
|
|
{count: 1, type: kWasmI32},
|
|
|
|
{count: 1, type: kWasmF32},
|
|
|
|
{count: 1, type: kWasmF64},
|
2019-07-30 11:19:40 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
for (p of types) {
|
2016-10-26 16:58:40 +00:00
|
|
|
let builder = new WasmModuleBuilder();
|
2019-07-30 11:19:40 +00:00
|
|
|
builder.addFunction(undefined, makeSig_r_x(p.type, p.type))
|
2020-09-10 12:39:52 +00:00
|
|
|
.addLocals(p.type, p.count)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 0, kExprLocalSet, 1, kExprLocalGet, 1])
|
2019-07-30 11:19:40 +00:00
|
|
|
.exportAs('main');
|
2016-03-07 10:01:24 +00:00
|
|
|
|
2016-10-26 16:58:40 +00:00
|
|
|
var buffer = builder.toBuffer(debug);
|
|
|
|
var instance = instantiate(buffer);
|
2016-03-07 10:01:24 +00:00
|
|
|
assertEquals(19, instance.exports.main(19));
|
|
|
|
assertEquals(27777, instance.exports.main(27777));
|
2019-07-30 11:19:40 +00:00
|
|
|
}
|
2016-03-07 21:04:07 +00:00
|
|
|
})();
|
|
|
|
|
2016-03-07 10:01:24 +00:00
|
|
|
(function CallTest() {
|
2019-07-30 11:19:40 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
builder.addFunction('add', kSig_i_ii).addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0, kExprLocalGet, 1, kExprI32Add
|
2019-07-30 11:19:40 +00:00
|
|
|
]);
|
|
|
|
builder.addFunction('main', kSig_i_ii)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 0, kExprLocalGet, 1, kExprCallFunction, 0])
|
2019-07-30 11:19:40 +00:00
|
|
|
.exportAs('main');
|
|
|
|
|
|
|
|
var instance = builder.instantiate();
|
|
|
|
assertEquals(44, instance.exports.main(11, 33));
|
|
|
|
assertEquals(7777, instance.exports.main(2222, 5555));
|
2016-03-07 10:01:24 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function IndirectCallTest() {
|
2019-07-30 11:19:40 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
builder.addFunction('add', kSig_i_ii).addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 0, kExprLocalGet, 1, kExprI32Add
|
2019-07-30 11:19:40 +00:00
|
|
|
]);
|
|
|
|
builder.addFunction('main', kSig_i_iii)
|
|
|
|
.addBody([
|
2019-10-08 12:38:48 +00:00
|
|
|
kExprLocalGet, 1, kExprLocalGet, 2, kExprLocalGet, 0, kExprCallIndirect,
|
2019-07-30 11:19:40 +00:00
|
|
|
0, kTableZero
|
|
|
|
])
|
|
|
|
.exportAs('main');
|
|
|
|
builder.appendToTable([0]);
|
|
|
|
|
|
|
|
var instance = builder.instantiate();
|
|
|
|
assertEquals(44, instance.exports.main(0, 11, 33));
|
|
|
|
assertEquals(7777, instance.exports.main(0, 2222, 5555));
|
|
|
|
assertThrows(() => instance.exports.main(1, 1, 1));
|
2016-03-07 10:01:24 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
(function DataSegmentTest() {
|
2019-07-30 11:19:40 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
builder.addMemory(1, 1, false);
|
|
|
|
builder.addFunction('load', kSig_i_i)
|
2019-10-08 12:38:48 +00:00
|
|
|
.addBody([kExprLocalGet, 0, kExprI32LoadMem, 0, 0])
|
2019-07-30 11:19:40 +00:00
|
|
|
.exportAs('load');
|
|
|
|
builder.addDataSegment(0, [9, 9, 9, 9]);
|
|
|
|
|
|
|
|
var buffer = builder.toBuffer(debug);
|
|
|
|
var instance = instantiate(buffer);
|
|
|
|
assertEquals(151587081, instance.exports.load(0));
|
2016-03-07 10:01:24 +00:00
|
|
|
})();
|
2016-03-07 21:09:22 +00:00
|
|
|
|
|
|
|
(function BasicTestWithUint8Array() {
|
2019-07-30 11:19:40 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
builder.addMemory(1, 2, false);
|
|
|
|
builder.addFunction('foo', kSig_i_v)
|
|
|
|
.addBody([kExprI32Const, 17])
|
|
|
|
.exportAs('blarg');
|
|
|
|
|
|
|
|
var buffer = builder.toBuffer(debug);
|
|
|
|
var array = new Uint8Array(buffer);
|
|
|
|
var instance = instantiate(array);
|
|
|
|
assertEquals(17, instance.exports.blarg());
|
|
|
|
|
|
|
|
var kPad = 5;
|
|
|
|
var buffer2 = new ArrayBuffer(kPad + buffer.byteLength + kPad);
|
|
|
|
var whole = new Uint8Array(buffer2);
|
|
|
|
for (var i = 0; i < whole.byteLength; i++) {
|
|
|
|
whole[i] = 0xff;
|
|
|
|
}
|
|
|
|
var array2 = new Uint8Array(buffer2, kPad, buffer.byteLength);
|
|
|
|
for (var i = 0; i < array2.byteLength; i++) {
|
|
|
|
array2[i] = array[i];
|
|
|
|
}
|
|
|
|
var instance = instantiate(array2);
|
|
|
|
assertEquals(17, instance.exports.blarg());
|
2016-03-07 21:09:22 +00:00
|
|
|
})();
|
2016-03-08 19:55:36 +00:00
|
|
|
|
|
|
|
(function ImportTestTwoLevel() {
|
2019-07-30 11:19:40 +00:00
|
|
|
print(arguments.callee.name);
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
var index = builder.addImport('mod', 'print', makeSig_v_x(kWasmI32));
|
|
|
|
builder.addFunction('foo', kSig_v_v)
|
|
|
|
.addBody([kExprI32Const, 19, kExprCallFunction, index])
|
|
|
|
.exportAs('main');
|
|
|
|
|
|
|
|
var buffer = builder.toBuffer(debug);
|
|
|
|
var instance = instantiate(buffer, {mod: {print: print}});
|
|
|
|
print('should print 19! ');
|
|
|
|
instance.exports.main();
|
|
|
|
})();
|
2016-03-08 19:55:36 +00:00
|
|
|
|
2019-07-30 11:19:40 +00:00
|
|
|
(function TestI32Const() {
|
|
|
|
print(arguments.callee.name);
|
|
|
|
let ints = [
|
|
|
|
// A few negative number of different length.
|
|
|
|
-3 << 28, -20000, -400, -200, -100, -50, -10, -1,
|
|
|
|
// And a few positive number of different length.
|
|
|
|
0, 1, 2, 20, 120, 130, 260, 500, 5000000, 3 << 28
|
|
|
|
];
|
|
|
|
for (let i of ints) {
|
|
|
|
let builder = new WasmModuleBuilder();
|
|
|
|
builder.addFunction('main', kSig_i_v)
|
|
|
|
.addBody([...wasmI32Const(i)])
|
|
|
|
.exportAs('main');
|
|
|
|
let instance = builder.instantiate();
|
|
|
|
assertEquals(i, instance.exports.main());
|
|
|
|
}
|
2016-03-08 19:55:36 +00:00
|
|
|
})();
|