b3ff390364
The test "assertThrows(builder.instantiate)" threw a TypeError before, which made the test pass, but not because of the feature we wanted to test. This CL fixes the test to call builder.instantiate correctly, and also tests for the correct error message. Drive-by fix: Fix {expected} and {found} parameters in assertThrows. R=ahaas@chromium.org Change-Id: I11c0f63885cc14a36559e637aea60a9da6f1bb8f Reviewed-on: https://chromium-review.googlesource.com/472886 Reviewed-by: Andreas Haas <ahaas@chromium.org> Commit-Queue: Clemens Hammacher <clemensh@chromium.org> Cr-Commit-Position: refs/heads/master@{#44584}
125 lines
3.3 KiB
JavaScript
125 lines
3.3 KiB
JavaScript
// 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");
|
|
load("test/mjsunit/wasm/wasm-module-builder.js");
|
|
|
|
function instantiate(sig, body) {
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
var func = builder.addFunction("", sig)
|
|
.addBody(body);
|
|
|
|
builder.addStart(func.index);
|
|
|
|
return builder.instantiate();
|
|
}
|
|
|
|
function assertVerifies(sig, body) {
|
|
var module = instantiate(sig, body);
|
|
assertFalse(module === undefined);
|
|
assertFalse(module === null);
|
|
assertFalse(module === 0);
|
|
assertEquals("object", typeof module);
|
|
return module;
|
|
}
|
|
|
|
assertVerifies(kSig_v_v, [kExprNop]);
|
|
|
|
// Arguments aren't allow to start functions.
|
|
assertThrows(() => {instantiate(kSig_i_i, [kExprGetLocal, 0]);});
|
|
assertThrows(() => {instantiate(kSig_i_ii, [kExprGetLocal, 0]);});
|
|
assertThrows(() => {instantiate(kSig_i_dd, [kExprGetLocal, 0]);});
|
|
assertThrows(() => {instantiate(kSig_i_v, [kExprI32Const, 0]);});
|
|
|
|
(function testInvalidIndex() {
|
|
print("testInvalidIndex");
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
var func = builder.addFunction("", kSig_v_v)
|
|
.addBody([kExprNop]);
|
|
|
|
builder.addStart(func.index + 1);
|
|
|
|
assertThrows(
|
|
() => builder.instantiate(), WebAssembly.CompileError,
|
|
'WebAssembly.Module(): Wasm decoding failed: ' +
|
|
'function index 1 out of bounds (1 entry) @+20');
|
|
})();
|
|
|
|
|
|
(function testTwoStartFuncs() {
|
|
print("testTwoStartFuncs");
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
var func = builder.addFunction("", kSig_v_v)
|
|
.addBody([kExprNop]);
|
|
|
|
builder.addExplicitSection([kStartSectionCode, 0]);
|
|
builder.addExplicitSection([kStartSectionCode, 0]);
|
|
|
|
assertThrows(
|
|
() => builder.instantiate(), WebAssembly.CompileError,
|
|
'WebAssembly.Module(): Wasm decoding failed: ' +
|
|
'unexpected section: Start @+27');
|
|
})();
|
|
|
|
|
|
(function testRun1() {
|
|
print("testRun1");
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
builder.addMemory(12, 12, true);
|
|
|
|
var func = builder.addFunction("", kSig_v_v)
|
|
.addBody([kExprI32Const, 0, kExprI32Const, 55, kExprI32StoreMem, 0, 0]);
|
|
|
|
builder.addStart(func.index);
|
|
|
|
var module = builder.instantiate();
|
|
var memory = module.exports.memory.buffer;
|
|
var view = new Int8Array(memory);
|
|
assertEquals(55, view[0]);
|
|
})();
|
|
|
|
(function testRun2() {
|
|
print("testRun2");
|
|
var builder = new WasmModuleBuilder();
|
|
|
|
builder.addMemory(12, 12, true);
|
|
|
|
var func = builder.addFunction("", kSig_v_v)
|
|
.addBody([kExprI32Const, 0, kExprI32Const, 22, kExprI32Const, 55, kExprI32Add, kExprI32StoreMem, 0, 0]);
|
|
|
|
builder.addStart(func.index);
|
|
|
|
var module = builder.instantiate();
|
|
var memory = module.exports.memory.buffer;
|
|
var view = new Int8Array(memory);
|
|
assertEquals(77, view[0]);
|
|
})();
|
|
|
|
(function testStartFFI() {
|
|
print("testStartFFI");
|
|
var ranned = false;
|
|
var ffi = {gak: {foo : function() {
|
|
print("we ranned at stert!");
|
|
ranned = true;
|
|
}}};
|
|
|
|
var builder = new WasmModuleBuilder();
|
|
var sig_index = builder.addType(kSig_v_v);
|
|
|
|
builder.addImport("gak", "foo", sig_index);
|
|
var func = builder.addFunction("", sig_index)
|
|
.addBody([kExprCallFunction, 0]);
|
|
|
|
builder.addStart(func.index);
|
|
|
|
var module = builder.instantiate(ffi);
|
|
assertTrue(ranned);
|
|
})();
|