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'; | |
8 | |
9 load("test/mjsunit/wasm/wasm-constants.js"); | |
10 load("test/mjsunit/wasm/wasm-module-builder.js"); | |
11 | |
7 // Basic tests. | 12 // Basic tests. |
8 | 13 |
9 var outOfUint32RangeValue = 1e12; | 14 var outOfUint32RangeValue = 1e12; |
10 var int32ButOob = 1073741824; | 15 var int32ButOob = 1073741824; |
11 | 16 |
12 function assertTableIsValid(table) { | 17 function assertTableIsValid(table) { |
13 assertSame(WebAssembly.Table.prototype, table.__proto__); | 18 assertSame(WebAssembly.Table.prototype, table.__proto__); |
14 assertSame(WebAssembly.Table, table.constructor); | 19 assertSame(WebAssembly.Table, table.constructor); |
15 assertTrue(table instanceof Object); | 20 assertTrue(table instanceof Object); |
16 assertTrue(table instanceof WebAssembly.Table); | 21 assertTrue(table instanceof WebAssembly.Table); |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
124 assertThrows(() => WebAssembly.Table.prototype.length.call([]), TypeError); | 129 assertThrows(() => WebAssembly.Table.prototype.length.call([]), TypeError); |
125 })(); | 130 })(); |
126 | 131 |
127 (function TestGet() { | 132 (function TestGet() { |
128 let table = new WebAssembly.Table({element: "anyfunc", initial: 10}); | 133 let table = new WebAssembly.Table({element: "anyfunc", initial: 10}); |
129 | 134 |
130 for (let i = 0; i < 10; ++i) { | 135 for (let i = 0; i < 10; ++i) { |
131 assertEquals(null, table.get(i)); | 136 assertEquals(null, table.get(i)); |
132 assertEquals(null, table.get(String(i))); | 137 assertEquals(null, table.get(String(i))); |
133 } | 138 } |
134 assertEquals(null, table.get("")); | 139 for (let key of [0.4, "", NaN, {}, [], () => {}]) { |
135 assertEquals(null, table.get(NaN)); | 140 assertEquals(null, table.get(key)); |
136 assertEquals(null, table.get({})); | 141 } |
137 assertEquals(null, table.get([])); | 142 for (let key of [10, -1]) { |
138 assertEquals(null, table.get(() => {})); | 143 assertEquals(undefined, table.get(key)); |
139 | 144 } |
140 assertEquals(undefined, table.get(10)); | |
141 assertEquals(undefined, table.get(-1)); | |
142 | 145 |
143 assertThrows(() => table.get(Symbol()), TypeError); | 146 assertThrows(() => table.get(Symbol()), TypeError); |
144 assertThrows(() => WebAssembly.Table.prototype.get.call([], 0), TypeError); | 147 assertThrows(() => WebAssembly.Table.prototype.get.call([], 0), TypeError); |
145 })(); | 148 })(); |
149 | |
150 (function TestSet() { | |
151 let builder = new WasmModuleBuilder; | |
152 builder.addExport("wasm", builder.addFunction("", kSig_v_v)); | |
153 builder.addExport("host", builder.addImportWithModule("test", "f", kSig_v_v)); | |
154 let {wasm, host} = builder.instantiate({test: {f() {}}}).exports; | |
155 | |
156 let table = new WebAssembly.Table({element: "anyfunc", initial: 10}); | |
157 | |
158 for (let f of [null, wasm, host]) { | |
159 for (let i = 0; i < 10; ++i) table.set(i, f); | |
160 for (let i = 0; i < 10; ++i) assertSame(f, table.get(i)); | |
161 for (let i = 0; i < 10; ++i) table.set(String(i), f); | |
ahaas
2016/10/13 14:25:09
Could you do the last two for loops in a separate
rossberg
2016/10/13 14:43:46
Done.
| |
162 for (let i = 0; i < 10; ++i) assertSame(f, table.get(i)); | |
163 | |
ahaas
2016/10/13 14:25:09
Could you also add a test where you check that the
rossberg
2016/10/13 14:43:46
Done.
| |
164 for (let key of [0.4, "", NaN, {}, [], () => {}]) { | |
165 table.set(0, null); | |
166 table.set(key, f); | |
167 assertSame(f, table.get(0)); | |
168 } | |
169 | |
170 for (let key of [10, -1]) { | |
ahaas
2016/10/13 14:25:09
According to the spec a RangeError should be throw
rossberg
2016/10/13 14:43:46
Done.
| |
171 table.set(key, f); // ignored | |
ahaas
2016/10/13 14:25:09
Should we adjust the spec to specify what should h
rossberg
2016/10/13 14:43:46
Yes, probably. I changed the implementation to thr
| |
172 } | |
173 | |
174 assertThrows(() => table.set(0), TypeError); | |
175 assertThrows(() => table.set(0, undefined), TypeError); | |
176 assertThrows(() => table.set(0, 0), TypeError); | |
177 assertThrows(() => table.set(0, ""), TypeError); | |
178 assertThrows(() => table.set(0, {}), TypeError); | |
179 assertThrows(() => table.set(0, () => {}), TypeError); | |
180 | |
181 assertThrows(() => table.set(Symbol(), f), TypeError); | |
182 assertThrows(() => WebAssembly.Table.prototype.set.call([], 0, f), | |
183 TypeError); | |
184 } | |
ahaas
2016/10/13 14:25:09
Do you check somewhere already that table.set retu
rossberg
2016/10/13 14:43:46
No, done.
| |
185 })(); | |
186 | |
187 (function TestGrow() { | |
188 let builder = new WasmModuleBuilder; | |
189 builder.addExport("wasm", builder.addFunction("", kSig_v_v)); | |
190 builder.addExport("host", builder.addImportWithModule("test", "f", kSig_v_v)); | |
191 let {wasm, host} = builder.instantiate({test: {f() {}}}).exports; | |
192 | |
193 function init(table) { | |
194 for (let i = 0; i < 5; ++i) table.set(i, wasm); | |
195 for (let i = 15; i < 20; ++i) table.set(i, host); | |
196 } | |
197 function check(table) { | |
198 for (let i = 0; i < 5; ++i) assertSame(wasm, table.get(i)); | |
199 for (let i = 6; i < 15; ++i) assertSame(null, table.get(i)); | |
200 for (let i = 15; i < 20; ++i) assertSame(host, table.get(i)); | |
201 for (let i = 21; i < table.length; ++i) assertSame(null, table.get(i)); | |
202 } | |
203 | |
204 let table = new WebAssembly.Table({element: "anyfunc", initial: 20}); | |
205 init(table); | |
206 check(table); | |
207 table.grow(20); | |
208 check(table); | |
209 table.grow(30); | |
210 check(table); | |
211 assertThrows(() => table.grow(10), RangeError); | |
212 assertThrows(() => table.grow(20), RangeError); | |
213 | |
214 table = new WebAssembly.Table({element: "anyfunc", initial: 20, maximum: 25}); | |
215 init(table); | |
216 check(table); | |
217 table.grow(20); | |
218 check(table); | |
219 table.grow(25); | |
220 check(table); | |
221 table.grow(25); | |
222 check(table); | |
223 assertThrows(() => table.grow(10), RangeError); | |
224 assertThrows(() => table.grow(20), RangeError); | |
225 assertThrows(() => table.grow(26), RangeError); | |
226 | |
227 assertThrows(() => WebAssembly.Table.prototype.grow.call([], 20), TypeError); | |
228 })(); | |
OLD | NEW |