Chromium Code Reviews| Index: test/mjsunit/wasm/errors.js |
| diff --git a/test/mjsunit/wasm/errors.js b/test/mjsunit/wasm/errors.js |
| index 7dbd6e5cb6b9f17af0c230359ee0cfd4cd6c147d..e1b7beb51aea318c36a30986cbfb9ca21a710204 100644 |
| --- a/test/mjsunit/wasm/errors.js |
| +++ b/test/mjsunit/wasm/errors.js |
| @@ -25,6 +25,23 @@ function instance(bytes, imports = {}) { |
| return new WebAssembly.Instance(module(bytes), imports); |
| } |
| +// instantiate should succeed but run should fail. |
| +function instantiateAndFailAtRuntime(bytes, imports = {}) { |
| + var instance = undefined; |
| + try { |
| + instance = new WebAssembly.Instance(module(bytes), imports); |
| + } catch(e) { |
| + // If we fail at startup. |
| + if (e instanceof WebAssembly.RuntimeError) { |
| + throw e; |
| + } |
| + // Swallow other instantiation errors because we expect instantiation |
| + // to succeed but runtime to fail. |
| + return; |
| + } |
| + instance.exports.run(); |
| +} |
| + |
| function builder() { |
| return new WasmModuleBuilder; |
| } |
| @@ -33,21 +50,25 @@ function assertCompileError(bytes) { |
| assertThrows(() => module(bytes), WebAssembly.CompileError); |
| } |
| +// default imports to {} so we get LinkError by default, thus allowing us to |
| +// distinguish the TypeError we want to catch |
| function assertTypeError(bytes, imports = {}) { |
| assertThrows(() => instance(bytes, imports), TypeError); |
| } |
| -function assertLinkError(bytes, imports = {}) { |
| +// default imports to null so we get TypeError by default, thus allowing us to |
|
rossberg
2017/01/12 23:59:53
Shouldn't be necessary, since undefined should cau
Mircea Trofin
2017/01/13 03:57:47
Done.
|
| +// distinguish the WebAssembly.LinkError we want to catch |
| +function assertLinkError(bytes, imports = null) { |
| assertThrows(() => instance(bytes, imports), WebAssembly.LinkError); |
| } |
| -function assertRuntimeError(bytes, imports = {}) { |
| - assertThrows(() => instance(bytes, imports).exports.run(), |
| +function assertRuntimeError(bytes, imports) { |
| + assertThrows(() => instantiateAndFailAtRuntime(bytes, imports), |
| WebAssembly.RuntimeError); |
| } |
| -function assertConversionError(bytes, imports = {}) { |
| - assertThrows(() => instance(bytes, imports).exports.run(), TypeError); |
| +function assertConversionError(bytes, imports) { |
| + assertThrows(() => instantiateAndFailAtRuntime(bytes, imports), TypeError); |
| } |
| (function TestDecodingError() { |
| @@ -72,7 +93,7 @@ function assertConversionError(bytes, imports = {}) { |
| b = builder(); |
| b.addImport("foo", "bar", kSig_v_v); |
| - assertTypeError(b.toBuffer(), {}); |
| + assertLinkError(b.toBuffer(), {}); |
| b = builder(); |
| b.addImport("foo", "bar", kSig_v_v); |
| assertLinkError(b.toBuffer(), {foo: {}}); |
| @@ -82,7 +103,7 @@ function assertConversionError(bytes, imports = {}) { |
| b = builder(); |
| b.addImportedGlobal("foo", "bar", kWasmI32); |
| - assertTypeError(b.toBuffer(), {}); |
| + assertLinkError(b.toBuffer(), {}); |
| b = builder(); |
| b.addImportedGlobal("foo", "bar", kWasmI32); |
| assertLinkError(b.toBuffer(), {foo: {}}); |
| @@ -95,7 +116,7 @@ function assertConversionError(bytes, imports = {}) { |
| b = builder(); |
| b.addImportedMemory("foo", "bar"); |
| - assertTypeError(b.toBuffer(), {}); |
| + assertLinkError(b.toBuffer(), {}); |
| b = builder(); |
| b.addImportedMemory("foo", "bar"); |
| assertLinkError(b.toBuffer(), {foo: {}}); |
| @@ -105,7 +126,7 @@ function assertConversionError(bytes, imports = {}) { |
| {foo: {bar: () => new WebAssembly.Memory({initial: 0})}}); |
| b = builder(); |
| - b.addFunction("f", kSig_v_v).addBody([ |
| + b.addFunction("startup", kSig_v_v).addBody([ |
| kExprUnreachable, |
| ]).end().addStart(0); |
| assertRuntimeError(b.toBuffer()); |
| @@ -135,8 +156,9 @@ function assertConversionError(bytes, imports = {}) { |
| b.addImport("foo", "bar", kSig_v_l); |
| assertConversionError(b.addFunction("run", kSig_v_v).addBody([ |
| kExprI64Const, 0, kExprCallFunction, 0 |
| - ]).exportFunc().end().toBuffer()); |
| + ]).exportFunc().end().toBuffer(), {foo:{bar: (l)=>{}}}); |
| + b = builder() |
| assertConversionError(builder().addFunction("run", kSig_l_v).addBody([ |
| kExprI64Const, 0 |
| ]).exportFunc().end().toBuffer()); |