OLD | NEW |
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"); |
(...skipping 15 matching lines...) Expand all Loading... |
26 } | 26 } |
27 | 27 |
28 function builder() { | 28 function builder() { |
29 return new WasmModuleBuilder; | 29 return new WasmModuleBuilder; |
30 } | 30 } |
31 | 31 |
32 function assertCompileError(bytes) { | 32 function assertCompileError(bytes) { |
33 assertThrows(() => module(bytes), WebAssembly.CompileError); | 33 assertThrows(() => module(bytes), WebAssembly.CompileError); |
34 } | 34 } |
35 | 35 |
| 36 function assertTypeError(bytes, imports = {}) { |
| 37 assertThrows(() => instance(bytes, imports), TypeError); |
| 38 } |
| 39 |
36 function assertLinkError(bytes, imports = {}) { | 40 function assertLinkError(bytes, imports = {}) { |
37 assertThrows(() => instance(bytes, imports), TypeError); | 41 assertThrows(() => instance(bytes, imports), WebAssembly.LinkError); |
38 } | 42 } |
39 | 43 |
40 function assertRuntimeError(bytes, imports = {}) { | 44 function assertRuntimeError(bytes, imports = {}) { |
41 assertThrows(() => instance(bytes, imports).exports.run(), | 45 assertThrows(() => instance(bytes, imports).exports.run(), |
42 WebAssembly.RuntimeError); | 46 WebAssembly.RuntimeError); |
43 } | 47 } |
44 | 48 |
45 function assertConversionError(bytes, imports = {}) { | 49 function assertConversionError(bytes, imports = {}) { |
46 assertThrows(() => instance(bytes, imports).exports.run(), TypeError); | 50 assertThrows(() => instance(bytes, imports).exports.run(), TypeError); |
47 } | 51 } |
(...skipping 13 matching lines...) Expand all Loading... |
61 kExprGetLocal, 0 | 65 kExprGetLocal, 0 |
62 ]).end().toBuffer()); | 66 ]).end().toBuffer()); |
63 assertCompileError(builder().addStart(0).toBuffer()); | 67 assertCompileError(builder().addStart(0).toBuffer()); |
64 })(); | 68 })(); |
65 | 69 |
66 (function TestLinkingError() { | 70 (function TestLinkingError() { |
67 let b; | 71 let b; |
68 | 72 |
69 b = builder(); | 73 b = builder(); |
70 b.addImportWithModule("foo", "bar", kSig_v_v); | 74 b.addImportWithModule("foo", "bar", kSig_v_v); |
71 assertLinkError(b.toBuffer(), {}); | 75 assertTypeError(b.toBuffer(), {}); |
72 b = builder(); | 76 b = builder(); |
73 b.addImportWithModule("foo", "bar", kSig_v_v); | 77 b.addImportWithModule("foo", "bar", kSig_v_v); |
74 assertLinkError(b.toBuffer(), {foo: {}}); | 78 assertLinkError(b.toBuffer(), {foo: {}}); |
75 b = builder(); | 79 b = builder(); |
76 b.addImportWithModule("foo", "bar", kSig_v_v); | 80 b.addImportWithModule("foo", "bar", kSig_v_v); |
77 assertLinkError(b.toBuffer(), {foo: {bar: 9}}); | 81 assertLinkError(b.toBuffer(), {foo: {bar: 9}}); |
78 | 82 |
79 b = builder(); | 83 b = builder(); |
80 b.addImportedGlobal("foo", "bar", kAstI32); | 84 b.addImportedGlobal("foo", "bar", kAstI32); |
81 assertLinkError(b.toBuffer(), {}); | 85 assertTypeError(b.toBuffer(), {}); |
82 // TODO(titzer): implement stricter import checks for globals. | 86 b = builder(); |
83 // b = builder(); | 87 b.addImportedGlobal("foo", "bar", kAstI32); |
84 // b.addImportedGlobal("foo", "bar", kAstI32); | 88 assertLinkError(b.toBuffer(), {foo: {}}); |
85 // assertLinkError(b.toBuffer(), {foo: {}}); | 89 b = builder(); |
86 // b = builder(); | 90 b.addImportedGlobal("foo", "bar", kAstI32); |
87 // b.addImportedGlobal("foo", "bar", kAstI32); | 91 assertLinkError(b.toBuffer(), {foo: {bar: ""}}); |
88 // assertLinkError(b.toBuffer(), {foo: {bar: ""}}); | 92 b = builder(); |
89 // b = builder(); | 93 b.addImportedGlobal("foo", "bar", kAstI32); |
90 // b.addImportedGlobal("foo", "bar", kAstI32); | 94 assertLinkError(b.toBuffer(), {foo: {bar: () => 9}}); |
91 // assertLinkError(b.toBuffer(), {foo: {bar: () => 9}}); | |
92 | 95 |
93 b = builder(); | 96 b = builder(); |
94 b.addImportedMemory("foo", "bar"); | 97 b.addImportedMemory("foo", "bar"); |
95 assertLinkError(b.toBuffer(), {}); | 98 assertTypeError(b.toBuffer(), {}); |
96 b = builder(); | 99 b = builder(); |
97 b.addImportedMemory("foo", "bar"); | 100 b.addImportedMemory("foo", "bar"); |
98 assertLinkError(b.toBuffer(), {foo: {}}); | 101 assertLinkError(b.toBuffer(), {foo: {}}); |
99 // TODO(titzer): implement stricter import checks for globals. | 102 b = builder(); |
100 // b = builder(); | 103 b.addImportedMemory("foo", "bar", 1); |
101 // b.addImportedMemory("foo", "bar", 1); | 104 assertLinkError(b.toBuffer(), |
102 // assertLinkError(b.toBuffer(), | 105 {foo: {bar: () => new WebAssembly.Memory({initial: 0})}}); |
103 // {foo: {bar: new WebAssembly.Memory({initial: 0})}}); | 106 |
| 107 b = builder(); |
| 108 b.addFunction("f", kSig_v_v).addBody([ |
| 109 kExprUnreachable, |
| 110 ]).end().addStart(0); |
| 111 assertRuntimeError(b.toBuffer()); |
104 })(); | 112 })(); |
105 | 113 |
106 (function TestTrapError() { | 114 (function TestTrapError() { |
107 assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([ | 115 assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([ |
108 kExprUnreachable | 116 kExprUnreachable |
109 ]).exportFunc().end().toBuffer()); | 117 ]).exportFunc().end().toBuffer()); |
110 | 118 |
111 assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([ | 119 assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([ |
112 kExprI32Const, 1, | 120 kExprI32Const, 1, |
113 kExprI32Const, 0, | 121 kExprI32Const, 0, |
114 kExprI32DivS, | 122 kExprI32DivS, |
115 kExprDrop | 123 kExprDrop |
116 ]).exportFunc().end().toBuffer()); | 124 ]).exportFunc().end().toBuffer()); |
117 | 125 |
118 assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([ | 126 assertRuntimeError(builder().addFunction("run", kSig_v_v).addBody([ |
119 ]).exportFunc().end(). | 127 ]).exportFunc().end(). |
120 addFunction("start", kSig_v_v).addBody([ | 128 addFunction("start", kSig_v_v).addBody([ |
121 kExprUnreachable | 129 kExprUnreachable |
122 ]).end().addStart(1).toBuffer()); | 130 ]).end().addStart(1).toBuffer()); |
123 })(); | 131 })(); |
124 | 132 |
125 (function TestConversionError() { | 133 (function TestConversionError() { |
126 let b = builder(); | 134 let b = builder(); |
127 b.addImportWithModule("foo", "bar", kSig_v_l); | 135 b.addImportWithModule("foo", "bar", kSig_v_l); |
128 assertConversionError(b.addFunction("run", kSig_v_v).addBody([ | 136 assertConversionError(b.addFunction("run", kSig_v_v).addBody([ |
129 kExprI64Const, 0, kExprCallFunction, 0 | 137 kExprI64Const, 0, kExprCallFunction, 0 |
130 ]).exportFunc().end().toBuffer()); | 138 ]).exportFunc().end().toBuffer()); |
| 139 |
131 assertConversionError(builder().addFunction("run", kSig_l_v).addBody([ | 140 assertConversionError(builder().addFunction("run", kSig_l_v).addBody([ |
132 kExprI64Const, 0 | 141 kExprI64Const, 0 |
133 ]).exportFunc().end().toBuffer()); | 142 ]).exportFunc().end().toBuffer()); |
134 })(); | 143 })(); |
OLD | NEW |