Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(220)

Side by Side Diff: test/mjsunit/wasm/errors.js

Issue 2629523007: [wasm] JS-API: enable WebAssembly.instantiate tests; fix LinkError (Closed)
Patch Set: fix errors.js Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 the V8 project authors. All rights reserved. 1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Flags: --expose-wasm 5 // Flags: --expose-wasm
6 6
7 'use strict'; 7 'use strict';
8 8
9 load("test/mjsunit/wasm/wasm-constants.js"); 9 load("test/mjsunit/wasm/wasm-constants.js");
10 load("test/mjsunit/wasm/wasm-module-builder.js"); 10 load("test/mjsunit/wasm/wasm-module-builder.js");
11 11
12 function module(bytes) { 12 function module(bytes) {
13 let buffer = bytes; 13 let buffer = bytes;
14 if (typeof buffer === 'string') { 14 if (typeof buffer === 'string') {
15 buffer = new ArrayBuffer(bytes.length); 15 buffer = new ArrayBuffer(bytes.length);
16 let view = new Uint8Array(buffer); 16 let view = new Uint8Array(buffer);
17 for (let i = 0; i < bytes.length; ++i) { 17 for (let i = 0; i < bytes.length; ++i) {
18 view[i] = bytes.charCodeAt(i); 18 view[i] = bytes.charCodeAt(i);
19 } 19 }
20 } 20 }
21 return new WebAssembly.Module(buffer); 21 return new WebAssembly.Module(buffer);
22 } 22 }
23 23
24 function instance(bytes, imports = {}) { 24 function instance(bytes, imports = {}) {
25 return new WebAssembly.Instance(module(bytes), imports); 25 return new WebAssembly.Instance(module(bytes), imports);
26 } 26 }
27 27
28 // instantiate should succeed but run should fail.
29 function instantiateAndFailAtRuntime(bytes, imports = {}) {
30 var instance = undefined;
31 try {
32 instance = new WebAssembly.Instance(module(bytes), imports);
33 } catch(e) {
34 // If we fail at startup.
35 if (e instanceof WebAssembly.RuntimeError) {
36 throw e;
37 }
38 // Swallow other instantiation errors because we expect instantiation
39 // to succeed but runtime to fail.
40 return;
41 }
42 instance.exports.run();
43 }
44
28 function builder() { 45 function builder() {
29 return new WasmModuleBuilder; 46 return new WasmModuleBuilder;
30 } 47 }
31 48
32 function assertCompileError(bytes) { 49 function assertCompileError(bytes) {
33 assertThrows(() => module(bytes), WebAssembly.CompileError); 50 assertThrows(() => module(bytes), WebAssembly.CompileError);
34 } 51 }
35 52
53 // default imports to {} so we get LinkError by default, thus allowing us to
54 // distinguish the TypeError we want to catch
36 function assertTypeError(bytes, imports = {}) { 55 function assertTypeError(bytes, imports = {}) {
37 assertThrows(() => instance(bytes, imports), TypeError); 56 assertThrows(() => instance(bytes, imports), TypeError);
38 } 57 }
39 58
40 function assertLinkError(bytes, imports = {}) { 59 // 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.
60 // distinguish the WebAssembly.LinkError we want to catch
61 function assertLinkError(bytes, imports = null) {
41 assertThrows(() => instance(bytes, imports), WebAssembly.LinkError); 62 assertThrows(() => instance(bytes, imports), WebAssembly.LinkError);
42 } 63 }
43 64
44 function assertRuntimeError(bytes, imports = {}) { 65 function assertRuntimeError(bytes, imports) {
45 assertThrows(() => instance(bytes, imports).exports.run(), 66 assertThrows(() => instantiateAndFailAtRuntime(bytes, imports),
46 WebAssembly.RuntimeError); 67 WebAssembly.RuntimeError);
47 } 68 }
48 69
49 function assertConversionError(bytes, imports = {}) { 70 function assertConversionError(bytes, imports) {
50 assertThrows(() => instance(bytes, imports).exports.run(), TypeError); 71 assertThrows(() => instantiateAndFailAtRuntime(bytes, imports), TypeError);
51 } 72 }
52 73
53 (function TestDecodingError() { 74 (function TestDecodingError() {
54 assertCompileError(""); 75 assertCompileError("");
55 assertCompileError("X"); 76 assertCompileError("X");
56 assertCompileError("\0x00asm"); 77 assertCompileError("\0x00asm");
57 })(); 78 })();
58 79
59 (function TestValidationError() { 80 (function TestValidationError() {
60 assertCompileError(builder().addFunction("f", kSig_i_v).end().toBuffer()); 81 assertCompileError(builder().addFunction("f", kSig_i_v).end().toBuffer());
61 assertCompileError(builder().addFunction("f", kSig_i_v).addBody([ 82 assertCompileError(builder().addFunction("f", kSig_i_v).addBody([
62 kExprReturn 83 kExprReturn
63 ]).end().toBuffer()); 84 ]).end().toBuffer());
64 assertCompileError(builder().addFunction("f", kSig_v_v).addBody([ 85 assertCompileError(builder().addFunction("f", kSig_v_v).addBody([
65 kExprGetLocal, 0 86 kExprGetLocal, 0
66 ]).end().toBuffer()); 87 ]).end().toBuffer());
67 assertCompileError(builder().addStart(0).toBuffer()); 88 assertCompileError(builder().addStart(0).toBuffer());
68 })(); 89 })();
69 90
70 (function TestLinkingError() { 91 (function TestLinkingError() {
71 let b; 92 let b;
72 93
73 b = builder(); 94 b = builder();
74 b.addImport("foo", "bar", kSig_v_v); 95 b.addImport("foo", "bar", kSig_v_v);
75 assertTypeError(b.toBuffer(), {}); 96 assertLinkError(b.toBuffer(), {});
76 b = builder(); 97 b = builder();
77 b.addImport("foo", "bar", kSig_v_v); 98 b.addImport("foo", "bar", kSig_v_v);
78 assertLinkError(b.toBuffer(), {foo: {}}); 99 assertLinkError(b.toBuffer(), {foo: {}});
79 b = builder(); 100 b = builder();
80 b.addImport("foo", "bar", kSig_v_v); 101 b.addImport("foo", "bar", kSig_v_v);
81 assertLinkError(b.toBuffer(), {foo: {bar: 9}}); 102 assertLinkError(b.toBuffer(), {foo: {bar: 9}});
82 103
83 b = builder(); 104 b = builder();
84 b.addImportedGlobal("foo", "bar", kWasmI32); 105 b.addImportedGlobal("foo", "bar", kWasmI32);
85 assertTypeError(b.toBuffer(), {}); 106 assertLinkError(b.toBuffer(), {});
86 b = builder(); 107 b = builder();
87 b.addImportedGlobal("foo", "bar", kWasmI32); 108 b.addImportedGlobal("foo", "bar", kWasmI32);
88 assertLinkError(b.toBuffer(), {foo: {}}); 109 assertLinkError(b.toBuffer(), {foo: {}});
89 b = builder(); 110 b = builder();
90 b.addImportedGlobal("foo", "bar", kWasmI32); 111 b.addImportedGlobal("foo", "bar", kWasmI32);
91 assertLinkError(b.toBuffer(), {foo: {bar: ""}}); 112 assertLinkError(b.toBuffer(), {foo: {bar: ""}});
92 b = builder(); 113 b = builder();
93 b.addImportedGlobal("foo", "bar", kWasmI32); 114 b.addImportedGlobal("foo", "bar", kWasmI32);
94 assertLinkError(b.toBuffer(), {foo: {bar: () => 9}}); 115 assertLinkError(b.toBuffer(), {foo: {bar: () => 9}});
95 116
96 b = builder(); 117 b = builder();
97 b.addImportedMemory("foo", "bar"); 118 b.addImportedMemory("foo", "bar");
98 assertTypeError(b.toBuffer(), {}); 119 assertLinkError(b.toBuffer(), {});
99 b = builder(); 120 b = builder();
100 b.addImportedMemory("foo", "bar"); 121 b.addImportedMemory("foo", "bar");
101 assertLinkError(b.toBuffer(), {foo: {}}); 122 assertLinkError(b.toBuffer(), {foo: {}});
102 b = builder(); 123 b = builder();
103 b.addImportedMemory("foo", "bar", 1); 124 b.addImportedMemory("foo", "bar", 1);
104 assertLinkError(b.toBuffer(), 125 assertLinkError(b.toBuffer(),
105 {foo: {bar: () => new WebAssembly.Memory({initial: 0})}}); 126 {foo: {bar: () => new WebAssembly.Memory({initial: 0})}});
106 127
107 b = builder(); 128 b = builder();
108 b.addFunction("f", kSig_v_v).addBody([ 129 b.addFunction("startup", kSig_v_v).addBody([
109 kExprUnreachable, 130 kExprUnreachable,
110 ]).end().addStart(0); 131 ]).end().addStart(0);
111 assertRuntimeError(b.toBuffer()); 132 assertRuntimeError(b.toBuffer());
112 })(); 133 })();
113 134
114 (function TestTrapError() { 135 (function TestTrapError() {
115 assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([ 136 assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([
116 kExprUnreachable 137 kExprUnreachable
117 ]).exportFunc().end().toBuffer()); 138 ]).exportFunc().end().toBuffer());
118 139
119 assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([ 140 assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([
120 kExprI32Const, 1, 141 kExprI32Const, 1,
121 kExprI32Const, 0, 142 kExprI32Const, 0,
122 kExprI32DivS, 143 kExprI32DivS,
123 kExprDrop 144 kExprDrop
124 ]).exportFunc().end().toBuffer()); 145 ]).exportFunc().end().toBuffer());
125 146
126 assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([ 147 assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([
127 ]).exportFunc().end(). 148 ]).exportFunc().end().
128 addFunction("start", kSig_v_v).addBody([ 149 addFunction("start", kSig_v_v).addBody([
129 kExprUnreachable 150 kExprUnreachable
130 ]).end().addStart(1).toBuffer()); 151 ]).end().addStart(1).toBuffer());
131 })(); 152 })();
132 153
133 (function TestConversionError() { 154 (function TestConversionError() {
134 let b = builder(); 155 let b = builder();
135 b.addImport("foo", "bar", kSig_v_l); 156 b.addImport("foo", "bar", kSig_v_l);
136 assertConversionError(b.addFunction("run", kSig_v_v).addBody([ 157 assertConversionError(b.addFunction("run", kSig_v_v).addBody([
137 kExprI64Const, 0, kExprCallFunction, 0 158 kExprI64Const, 0, kExprCallFunction, 0
138 ]).exportFunc().end().toBuffer()); 159 ]).exportFunc().end().toBuffer(), {foo:{bar: (l)=>{}}});
139 160
161 b = builder()
140 assertConversionError(builder().addFunction("run", kSig_l_v).addBody([ 162 assertConversionError(builder().addFunction("run", kSig_l_v).addBody([
141 kExprI64Const, 0 163 kExprI64Const, 0
142 ]).exportFunc().end().toBuffer()); 164 ]).exportFunc().end().toBuffer());
143 })(); 165 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698