2016-02-15 08:58:44 +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-constants.js");
|
2016-03-07 19:32:35 +00:00
|
|
|
load("test/mjsunit/wasm/wasm-module-builder.js");
|
2016-02-15 08:58:44 +00:00
|
|
|
|
|
|
|
function instantiate(sig, body) {
|
2016-03-07 19:32:35 +00:00
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
var func = builder.addFunction("", sig)
|
|
|
|
.addBody(body);
|
|
|
|
|
|
|
|
builder.addStart(func.index);
|
|
|
|
|
|
|
|
return builder.instantiate();
|
2016-02-15 08:58:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function assertFails(sig, body) {
|
|
|
|
try {
|
|
|
|
var module = instantiate(sig, body);
|
|
|
|
print("expected failure, but passes");
|
|
|
|
assertFalse(true);
|
|
|
|
} catch (expected) {
|
|
|
|
print("ok: " + expected);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function assertVerifies(sig, body) {
|
|
|
|
var module = instantiate(sig, body);
|
|
|
|
assertFalse(module === undefined);
|
|
|
|
assertFalse(module === null);
|
|
|
|
assertFalse(module === 0);
|
|
|
|
assertEquals("object", typeof module);
|
|
|
|
return module;
|
|
|
|
}
|
|
|
|
|
2016-04-29 09:39:26 +00:00
|
|
|
assertVerifies(kSig_v_v, [kExprNop]);
|
|
|
|
assertVerifies(kSig_i, [kExprI8Const, 0]);
|
2016-02-15 08:58:44 +00:00
|
|
|
|
|
|
|
// Arguments aren't allow to start functions.
|
2016-04-29 09:39:26 +00:00
|
|
|
assertFails(kSig_i_i, [kExprGetLocal, 0]);
|
|
|
|
assertFails(kSig_i_ii, [kExprGetLocal, 0]);
|
|
|
|
assertFails(kSig_i_dd, [kExprGetLocal, 0]);
|
2016-02-15 08:58:44 +00:00
|
|
|
|
|
|
|
(function testInvalidIndex() {
|
2016-03-07 19:32:35 +00:00
|
|
|
print("testInvalidIndex");
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
2016-04-29 09:39:26 +00:00
|
|
|
var func = builder.addFunction("", kSig_v_v)
|
2016-03-07 19:32:35 +00:00
|
|
|
.addBody([kExprNop]);
|
|
|
|
|
|
|
|
builder.addStart(func.index + 1);
|
|
|
|
|
|
|
|
assertThrows(builder.instantiate);
|
2016-02-15 08:58:44 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
|
|
(function testTwoStartFuncs() {
|
2016-03-07 19:32:35 +00:00
|
|
|
print("testTwoStartFuncs");
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
2016-04-29 09:39:26 +00:00
|
|
|
var func = builder.addFunction("", kSig_v_v)
|
2016-03-07 19:32:35 +00:00
|
|
|
.addBody([kExprNop]);
|
|
|
|
|
2016-06-21 19:47:51 +00:00
|
|
|
builder.addExplicitSection([kDeclStart, 0]);
|
|
|
|
builder.addExplicitSection([kDeclStart, 0]);
|
2016-03-07 19:32:35 +00:00
|
|
|
|
|
|
|
assertThrows(builder.instantiate);
|
2016-02-15 08:58:44 +00:00
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
|
|
(function testRun() {
|
2016-03-07 19:32:35 +00:00
|
|
|
print("testRun");
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
|
|
|
|
builder.addMemory(12, 12, true);
|
|
|
|
|
2016-04-29 09:39:26 +00:00
|
|
|
var func = builder.addFunction("", kSig_v_v)
|
2016-04-29 09:15:26 +00:00
|
|
|
.addBody([kExprI8Const, 0, kExprI8Const, 77, kExprI32StoreMem, 0, 0]);
|
2016-03-07 19:32:35 +00:00
|
|
|
|
|
|
|
builder.addStart(func.index);
|
|
|
|
|
|
|
|
var module = builder.instantiate();
|
2016-03-09 13:31:23 +00:00
|
|
|
var memory = module.exports.memory;
|
2016-02-15 08:58:44 +00:00
|
|
|
var view = new Int8Array(memory);
|
|
|
|
assertEquals(77, view[0]);
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function testStartFFI() {
|
2016-03-07 19:32:35 +00:00
|
|
|
print("testStartFFI");
|
2016-02-15 08:58:44 +00:00
|
|
|
var ranned = false;
|
2016-03-07 19:32:35 +00:00
|
|
|
var ffi = { foo : function() {
|
2016-02-15 08:58:44 +00:00
|
|
|
print("we ranned at stert!");
|
|
|
|
ranned = true;
|
2016-03-07 19:32:35 +00:00
|
|
|
}};
|
|
|
|
|
|
|
|
var builder = new WasmModuleBuilder();
|
2016-06-21 19:47:51 +00:00
|
|
|
var sig_index = builder.addType(kSig_v_v);
|
2016-03-07 19:32:35 +00:00
|
|
|
|
|
|
|
builder.addImport("foo", sig_index);
|
|
|
|
var func = builder.addFunction("", sig_index)
|
2016-04-29 09:15:26 +00:00
|
|
|
.addBody([kExprCallImport, kArity0, 0]);
|
2016-03-07 19:32:35 +00:00
|
|
|
|
|
|
|
builder.addStart(func.index);
|
|
|
|
|
|
|
|
var module = builder.instantiate(ffi);
|
2016-02-15 08:58:44 +00:00
|
|
|
assertTrue(ranned);
|
|
|
|
})();
|