v8/test/mjsunit/wasm/errors.js
rossberg 16fe426320 Implement LinkError; import tweaks
- Implement new WebAssembly.LinkError exception
- Implement stricter checks for glboal imports
- Add tests
- Refactor handling of import names
- Add TODOs for empty import names

R=titzer@chromium.org
BUG=

Review-Url: https://codereview.chromium.org/2584843002
Cr-Commit-Position: refs/heads/master@{#41764}
2016-12-16 14:23:35 +00:00

144 lines
4.0 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
'use strict';
load("test/mjsunit/wasm/wasm-constants.js");
load("test/mjsunit/wasm/wasm-module-builder.js");
function module(bytes) {
let buffer = bytes;
if (typeof buffer === 'string') {
buffer = new ArrayBuffer(bytes.length);
let view = new Uint8Array(buffer);
for (let i = 0; i < bytes.length; ++i) {
view[i] = bytes.charCodeAt(i);
}
}
return new WebAssembly.Module(buffer);
}
function instance(bytes, imports = {}) {
return new WebAssembly.Instance(module(bytes), imports);
}
function builder() {
return new WasmModuleBuilder;
}
function assertCompileError(bytes) {
assertThrows(() => module(bytes), WebAssembly.CompileError);
}
function assertTypeError(bytes, imports = {}) {
assertThrows(() => instance(bytes, imports), TypeError);
}
function assertLinkError(bytes, imports = {}) {
assertThrows(() => instance(bytes, imports), WebAssembly.LinkError);
}
function assertRuntimeError(bytes, imports = {}) {
assertThrows(() => instance(bytes, imports).exports.run(),
WebAssembly.RuntimeError);
}
function assertConversionError(bytes, imports = {}) {
assertThrows(() => instance(bytes, imports).exports.run(), TypeError);
}
(function TestDecodingError() {
assertCompileError("");
assertCompileError("X");
assertCompileError("\0x00asm");
})();
(function TestValidationError() {
assertCompileError(builder().addFunction("f", kSig_i_v).end().toBuffer());
assertCompileError(builder().addFunction("f", kSig_i_v).addBody([
kExprReturn
]).end().toBuffer());
assertCompileError(builder().addFunction("f", kSig_v_v).addBody([
kExprGetLocal, 0
]).end().toBuffer());
assertCompileError(builder().addStart(0).toBuffer());
})();
(function TestLinkingError() {
let b;
b = builder();
b.addImportWithModule("foo", "bar", kSig_v_v);
assertTypeError(b.toBuffer(), {});
b = builder();
b.addImportWithModule("foo", "bar", kSig_v_v);
assertLinkError(b.toBuffer(), {foo: {}});
b = builder();
b.addImportWithModule("foo", "bar", kSig_v_v);
assertLinkError(b.toBuffer(), {foo: {bar: 9}});
b = builder();
b.addImportedGlobal("foo", "bar", kAstI32);
assertTypeError(b.toBuffer(), {});
b = builder();
b.addImportedGlobal("foo", "bar", kAstI32);
assertLinkError(b.toBuffer(), {foo: {}});
b = builder();
b.addImportedGlobal("foo", "bar", kAstI32);
assertLinkError(b.toBuffer(), {foo: {bar: ""}});
b = builder();
b.addImportedGlobal("foo", "bar", kAstI32);
assertLinkError(b.toBuffer(), {foo: {bar: () => 9}});
b = builder();
b.addImportedMemory("foo", "bar");
assertTypeError(b.toBuffer(), {});
b = builder();
b.addImportedMemory("foo", "bar");
assertLinkError(b.toBuffer(), {foo: {}});
b = builder();
b.addImportedMemory("foo", "bar", 1);
assertLinkError(b.toBuffer(),
{foo: {bar: () => new WebAssembly.Memory({initial: 0})}});
b = builder();
b.addFunction("f", kSig_v_v).addBody([
kExprUnreachable,
]).end().addStart(0);
assertRuntimeError(b.toBuffer());
})();
(function TestTrapError() {
assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([
kExprUnreachable
]).exportFunc().end().toBuffer());
assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([
kExprI32Const, 1,
kExprI32Const, 0,
kExprI32DivS,
kExprDrop
]).exportFunc().end().toBuffer());
assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([
]).exportFunc().end().
addFunction("start", kSig_v_v).addBody([
kExprUnreachable
]).end().addStart(1).toBuffer());
})();
(function TestConversionError() {
let b = builder();
b.addImportWithModule("foo", "bar", kSig_v_l);
assertConversionError(b.addFunction("run", kSig_v_v).addBody([
kExprI64Const, 0, kExprCallFunction, 0
]).exportFunc().end().toBuffer());
assertConversionError(builder().addFunction("run", kSig_l_v).addBody([
kExprI64Const, 0
]).exportFunc().end().toBuffer());
})();