Commit 16fe4263 authored by rossberg's avatar rossberg Committed by Commit bot

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}
parent bb76432f
......@@ -2220,6 +2220,10 @@ void Genesis::InitializeGlobal(Handle<JSGlobalObject> global_object,
InstallError(isolate, dummy, factory->CompileError_string(),
Context::WASM_COMPILE_ERROR_FUNCTION_INDEX);
// -- L i n k E r r o r
InstallError(isolate, dummy, factory->LinkError_string(),
Context::WASM_LINK_ERROR_FUNCTION_INDEX);
// -- R u n t i m e E r r o r
InstallError(isolate, dummy, factory->RuntimeError_string(),
Context::WASM_RUNTIME_ERROR_FUNCTION_INDEX);
......
......@@ -126,6 +126,7 @@ enum ContextLookupFlags {
V(URI_ERROR_FUNCTION_INDEX, JSFunction, uri_error_function) \
V(WASM_COMPILE_ERROR_FUNCTION_INDEX, JSFunction, \
wasm_compile_error_function) \
V(WASM_LINK_ERROR_FUNCTION_INDEX, JSFunction, wasm_link_error_function) \
V(WASM_RUNTIME_ERROR_FUNCTION_INDEX, JSFunction, wasm_runtime_error_function)
#define NATIVE_CONTEXT_JS_ARRAY_ITERATOR_MAPS(V) \
......
......@@ -1370,6 +1370,7 @@ DEFINE_ERROR(ReferenceError, reference_error)
DEFINE_ERROR(SyntaxError, syntax_error)
DEFINE_ERROR(TypeError, type_error)
DEFINE_ERROR(WasmCompileError, wasm_compile_error)
DEFINE_ERROR(WasmLinkError, wasm_link_error)
DEFINE_ERROR(WasmRuntimeError, wasm_runtime_error)
#undef DEFINE_ERROR
......
......@@ -649,6 +649,7 @@ class V8_EXPORT_PRIVATE Factory final {
DECLARE_ERROR(SyntaxError)
DECLARE_ERROR(TypeError)
DECLARE_ERROR(WasmCompileError)
DECLARE_ERROR(WasmLinkError)
DECLARE_ERROR(WasmRuntimeError)
#undef DECLARE_ERROR
......
......@@ -164,6 +164,7 @@
V(TypeError_string, "TypeError") \
V(type_string, "type") \
V(CompileError_string, "CompileError") \
V(LinkError_string, "LinkError") \
V(RuntimeError_string, "RuntimeError") \
V(uint16x8_string, "uint16x8") \
V(Uint16x8_string, "Uint16x8") \
......
......@@ -699,6 +699,10 @@ void WasmJs::InstallWasmConstructors(Isolate* isolate,
isolate->native_context()->wasm_compile_error_function());
JSObject::AddProperty(webassembly, isolate->factory()->CompileError_string(),
compile_error, attributes);
Handle<JSFunction> link_error(
isolate->native_context()->wasm_link_error_function());
JSObject::AddProperty(webassembly, isolate->factory()->LinkError_string(),
link_error, attributes);
Handle<JSFunction> runtime_error(
isolate->native_context()->wasm_runtime_error_function());
JSObject::AddProperty(webassembly, isolate->factory()->RuntimeError_string(),
......
This diff is collapsed.
......@@ -70,6 +70,14 @@ void ErrorThrower::CompileError(const char* format, ...) {
va_end(arguments);
}
void ErrorThrower::LinkError(const char* format, ...) {
if (error()) return;
va_list arguments;
va_start(arguments, format);
Format(isolate_->wasm_link_error_function(), format, arguments);
va_end(arguments);
}
void ErrorThrower::RuntimeError(const char* format, ...) {
if (error()) return;
va_list arguments;
......
......@@ -95,6 +95,7 @@ class V8_EXPORT_PRIVATE ErrorThrower {
PRINTF_FORMAT(2, 3) void TypeError(const char* fmt, ...);
PRINTF_FORMAT(2, 3) void RangeError(const char* fmt, ...);
PRINTF_FORMAT(2, 3) void CompileError(const char* fmt, ...);
PRINTF_FORMAT(2, 3) void LinkError(const char* fmt, ...);
PRINTF_FORMAT(2, 3) void RuntimeError(const char* fmt, ...);
template <typename T>
......
......@@ -33,10 +33,14 @@ function assertCompileError(bytes) {
assertThrows(() => module(bytes), WebAssembly.CompileError);
}
function assertLinkError(bytes, imports = {}) {
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);
......@@ -68,7 +72,7 @@ function assertConversionError(bytes, imports = {}) {
b = builder();
b.addImportWithModule("foo", "bar", kSig_v_v);
assertLinkError(b.toBuffer(), {});
assertTypeError(b.toBuffer(), {});
b = builder();
b.addImportWithModule("foo", "bar", kSig_v_v);
assertLinkError(b.toBuffer(), {foo: {}});
......@@ -78,29 +82,33 @@ function assertConversionError(bytes, imports = {}) {
b = builder();
b.addImportedGlobal("foo", "bar", kAstI32);
assertLinkError(b.toBuffer(), {});
// TODO(titzer): implement stricter import checks for globals.
// 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}});
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");
assertLinkError(b.toBuffer(), {});
assertTypeError(b.toBuffer(), {});
b = builder();
b.addImportedMemory("foo", "bar");
assertLinkError(b.toBuffer(), {foo: {}});
// TODO(titzer): implement stricter import checks for globals.
// b = builder();
// b.addImportedMemory("foo", "bar", 1);
// assertLinkError(b.toBuffer(),
// {foo: {bar: new WebAssembly.Memory({initial: 0})}});
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() {
......@@ -128,6 +136,7 @@ function assertConversionError(bytes, imports = {}) {
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());
......
......@@ -54,6 +54,27 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
assertSame(module.exports.blah, module.exports.foo);
})();
(function testEmptyName() {
print("TestEmptyName...");
var kReturnValue = 93;
var builder = new WasmModuleBuilder();
builder.addFunction("main", kSig_i_v)
.addBody([
kExprI8Const,
kReturnValue,
kExprReturn
])
.exportAs("");
var module = builder.instantiate();
assertEquals("object", typeof module.exports);
assertEquals("function", typeof module.exports[""]);
assertEquals(kReturnValue, module.exports[""]());
})();
(function testNumericName() {
print("TestNumericName...");
......
......@@ -24,7 +24,6 @@ function TestImported(type, val, expected) {
TestImported(kAstI32, 300.1, 300);
TestImported(kAstF32, 87234.87238, Math.fround(87234.87238));
TestImported(kAstF64, 77777.88888, 77777.88888);
TestImported(kAstF64, "89", 89);
function TestExported(type, val, expected) {
......
......@@ -255,7 +255,7 @@ function testCallPrint() {
var main = builder.instantiate({print: print}).exports.main;
for (var i = -9; i < 900; i += 16.125) {
main(i);
main(i);
}
}
......@@ -281,3 +281,23 @@ function testCallImport2(foo, bar, expected) {
}
testCallImport2(function() { return 33; }, function () { return 44; }, 77);
function testImportName(name) {
var builder = new WasmModuleBuilder();
builder.addImportWithModule("M", name, kSig_i_v);
builder.addFunction("main", kSig_i_v)
.addBody([
kExprCallFunction, 0
])
.exportFunc();
let main = builder.instantiate({M: {[name]: () => 42}}).exports.main;
assertEquals(42, main());
}
testImportName("bla");
testImportName("0");
testImportName(" a @#$2 324 ");
// TODO(bradnelson): This should succeed.
// testImportName("");
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment