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

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

Issue 2629523007: [wasm] JS-API: enable WebAssembly.instantiate tests; fix LinkError (Closed)
Patch Set: initialize module_ upfront 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 function assertLinkError(bytes, imports) {
41 assertThrows(() => instance(bytes, imports), WebAssembly.LinkError); 60 assertThrows(() => instance(bytes, imports), WebAssembly.LinkError);
42 } 61 }
43 62
44 function assertRuntimeError(bytes, imports = {}) { 63 function assertRuntimeError(bytes, imports) {
45 assertThrows(() => instance(bytes, imports).exports.run(), 64 assertThrows(() => instantiateAndFailAtRuntime(bytes, imports),
46 WebAssembly.RuntimeError); 65 WebAssembly.RuntimeError);
47 } 66 }
48 67
49 function assertConversionError(bytes, imports = {}) { 68 function assertConversionError(bytes, imports) {
50 assertThrows(() => instance(bytes, imports).exports.run(), TypeError); 69 assertThrows(() => instantiateAndFailAtRuntime(bytes, imports), TypeError);
51 } 70 }
52 71
53 (function TestDecodingError() { 72 (function TestDecodingError() {
54 assertCompileError(""); 73 assertCompileError("");
55 assertCompileError("X"); 74 assertCompileError("X");
56 assertCompileError("\0x00asm"); 75 assertCompileError("\0x00asm");
57 })(); 76 })();
58 77
59 (function TestValidationError() { 78 (function TestValidationError() {
60 assertCompileError(builder().addFunction("f", kSig_i_v).end().toBuffer()); 79 assertCompileError(builder().addFunction("f", kSig_i_v).end().toBuffer());
61 assertCompileError(builder().addFunction("f", kSig_i_v).addBody([ 80 assertCompileError(builder().addFunction("f", kSig_i_v).addBody([
62 kExprReturn 81 kExprReturn
63 ]).end().toBuffer()); 82 ]).end().toBuffer());
64 assertCompileError(builder().addFunction("f", kSig_v_v).addBody([ 83 assertCompileError(builder().addFunction("f", kSig_v_v).addBody([
65 kExprGetLocal, 0 84 kExprGetLocal, 0
66 ]).end().toBuffer()); 85 ]).end().toBuffer());
67 assertCompileError(builder().addStart(0).toBuffer()); 86 assertCompileError(builder().addStart(0).toBuffer());
68 })(); 87 })();
69 88
70 (function TestLinkingError() { 89 (function TestLinkingError() {
71 let b; 90 let b;
72 91
73 b = builder(); 92 b = builder();
74 b.addImport("foo", "bar", kSig_v_v); 93 b.addImport("foo", "bar", kSig_v_v);
75 assertTypeError(b.toBuffer(), {}); 94 assertLinkError(b.toBuffer(), {});
76 b = builder(); 95 b = builder();
77 b.addImport("foo", "bar", kSig_v_v); 96 b.addImport("foo", "bar", kSig_v_v);
78 assertLinkError(b.toBuffer(), {foo: {}}); 97 assertLinkError(b.toBuffer(), {foo: {}});
79 b = builder(); 98 b = builder();
80 b.addImport("foo", "bar", kSig_v_v); 99 b.addImport("foo", "bar", kSig_v_v);
81 assertLinkError(b.toBuffer(), {foo: {bar: 9}}); 100 assertLinkError(b.toBuffer(), {foo: {bar: 9}});
82 101
83 b = builder(); 102 b = builder();
84 b.addImportedGlobal("foo", "bar", kWasmI32); 103 b.addImportedGlobal("foo", "bar", kWasmI32);
85 assertTypeError(b.toBuffer(), {}); 104 assertLinkError(b.toBuffer(), {});
86 b = builder(); 105 b = builder();
87 b.addImportedGlobal("foo", "bar", kWasmI32); 106 b.addImportedGlobal("foo", "bar", kWasmI32);
88 assertLinkError(b.toBuffer(), {foo: {}}); 107 assertLinkError(b.toBuffer(), {foo: {}});
89 b = builder(); 108 b = builder();
90 b.addImportedGlobal("foo", "bar", kWasmI32); 109 b.addImportedGlobal("foo", "bar", kWasmI32);
91 assertLinkError(b.toBuffer(), {foo: {bar: ""}}); 110 assertLinkError(b.toBuffer(), {foo: {bar: ""}});
92 b = builder(); 111 b = builder();
93 b.addImportedGlobal("foo", "bar", kWasmI32); 112 b.addImportedGlobal("foo", "bar", kWasmI32);
94 assertLinkError(b.toBuffer(), {foo: {bar: () => 9}}); 113 assertLinkError(b.toBuffer(), {foo: {bar: () => 9}});
95 114
96 b = builder(); 115 b = builder();
97 b.addImportedMemory("foo", "bar"); 116 b.addImportedMemory("foo", "bar");
98 assertTypeError(b.toBuffer(), {}); 117 assertLinkError(b.toBuffer(), {});
99 b = builder(); 118 b = builder();
100 b.addImportedMemory("foo", "bar"); 119 b.addImportedMemory("foo", "bar");
101 assertLinkError(b.toBuffer(), {foo: {}}); 120 assertLinkError(b.toBuffer(), {foo: {}});
102 b = builder(); 121 b = builder();
103 b.addImportedMemory("foo", "bar", 1); 122 b.addImportedMemory("foo", "bar", 1);
104 assertLinkError(b.toBuffer(), 123 assertLinkError(b.toBuffer(),
105 {foo: {bar: () => new WebAssembly.Memory({initial: 0})}}); 124 {foo: {bar: () => new WebAssembly.Memory({initial: 0})}});
106 125
107 b = builder(); 126 b = builder();
108 b.addFunction("f", kSig_v_v).addBody([ 127 b.addFunction("startup", kSig_v_v).addBody([
109 kExprUnreachable, 128 kExprUnreachable,
110 ]).end().addStart(0); 129 ]).end().addStart(0);
111 assertRuntimeError(b.toBuffer()); 130 assertRuntimeError(b.toBuffer());
112 })(); 131 })();
113 132
114 (function TestTrapError() { 133 (function TestTrapError() {
115 assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([ 134 assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([
116 kExprUnreachable 135 kExprUnreachable
117 ]).exportFunc().end().toBuffer()); 136 ]).exportFunc().end().toBuffer());
118 137
119 assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([ 138 assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([
120 kExprI32Const, 1, 139 kExprI32Const, 1,
121 kExprI32Const, 0, 140 kExprI32Const, 0,
122 kExprI32DivS, 141 kExprI32DivS,
123 kExprDrop 142 kExprDrop
124 ]).exportFunc().end().toBuffer()); 143 ]).exportFunc().end().toBuffer());
125 144
126 assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([ 145 assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([
127 ]).exportFunc().end(). 146 ]).exportFunc().end().
128 addFunction("start", kSig_v_v).addBody([ 147 addFunction("start", kSig_v_v).addBody([
129 kExprUnreachable 148 kExprUnreachable
130 ]).end().addStart(1).toBuffer()); 149 ]).end().addStart(1).toBuffer());
131 })(); 150 })();
132 151
133 (function TestConversionError() { 152 (function TestConversionError() {
134 let b = builder(); 153 let b = builder();
135 b.addImport("foo", "bar", kSig_v_l); 154 b.addImport("foo", "bar", kSig_v_l);
136 assertConversionError(b.addFunction("run", kSig_v_v).addBody([ 155 assertConversionError(b.addFunction("run", kSig_v_v).addBody([
137 kExprI64Const, 0, kExprCallFunction, 0 156 kExprI64Const, 0, kExprCallFunction, 0
138 ]).exportFunc().end().toBuffer()); 157 ]).exportFunc().end().toBuffer(), {foo:{bar: (l)=>{}}});
139 158
159 b = builder()
140 assertConversionError(builder().addFunction("run", kSig_l_v).addBody([ 160 assertConversionError(builder().addFunction("run", kSig_l_v).addBody([
141 kExprI64Const, 0 161 kExprI64Const, 0
142 ]).exportFunc().end().toBuffer()); 162 ]).exportFunc().end().toBuffer());
143 })(); 163 })();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698