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 load("test/mjsunit/wasm/wasm-constants.js"); | 7 load("test/mjsunit/wasm/wasm-constants.js"); |
8 load("test/mjsunit/wasm/wasm-module-builder.js"); | 8 load("test/mjsunit/wasm/wasm-module-builder.js"); |
9 | 9 |
10 (function TestOne() { | 10 (function TestOne() { |
(...skipping 22 matching lines...) Expand all Loading... |
33 let memory = new WebAssembly.Memory({initial: 1}); | 33 let memory = new WebAssembly.Memory({initial: 1}); |
34 let i32 = new Int32Array(memory.buffer); | 34 let i32 = new Int32Array(memory.buffer); |
35 let builder = new WasmModuleBuilder(); | 35 let builder = new WasmModuleBuilder(); |
36 builder.addImportedMemory("garg"); | 36 builder.addImportedMemory("garg"); |
37 builder.exportMemoryAs("daggle"); | 37 builder.exportMemoryAs("daggle"); |
38 | 38 |
39 let instance = builder.instantiate({garg: memory}); | 39 let instance = builder.instantiate({garg: memory}); |
40 assertSame(memory, instance.exports.daggle); | 40 assertSame(memory, instance.exports.daggle); |
41 })(); | 41 })(); |
42 | 42 |
43 | |
44 (function TestImportExport() { | 43 (function TestImportExport() { |
45 print("TestImportExport"); | 44 print("TestImportExport"); |
46 var i1; | 45 var i1; |
47 { | 46 { |
48 let builder = new WasmModuleBuilder(); | 47 let builder = new WasmModuleBuilder(); |
49 builder.addMemory(1, 1, false); | 48 builder.addMemory(1, 1, false); |
50 builder.exportMemoryAs("exported_mem"); | 49 builder.exportMemoryAs("exported_mem"); |
51 builder.addFunction("foo", kSig_i_i) | 50 builder.addFunction("foo", kSig_i_i) |
52 .addBody([ | 51 .addBody([ |
53 kExprGetLocal, 0, | 52 kExprGetLocal, 0, |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
222 .exportFunc(); | 221 .exportFunc(); |
223 instance = builder.instantiate({ | 222 instance = builder.instantiate({ |
224 imported_mem: exp_instance.exports.exported_mem}); | 223 imported_mem: exp_instance.exports.exported_mem}); |
225 } | 224 } |
226 for (var i = initial_size; i < maximum_size; i++) { | 225 for (var i = initial_size; i < maximum_size; i++) { |
227 assertEquals(i, instance.exports.grow(1)); | 226 assertEquals(i, instance.exports.grow(1)); |
228 assertEquals((i+1), instance.exports.mem_size()); | 227 assertEquals((i+1), instance.exports.mem_size()); |
229 } | 228 } |
230 assertEquals(-1, instance.exports.grow(1)); | 229 assertEquals(-1, instance.exports.grow(1)); |
231 })(); | 230 })(); |
| 231 |
| 232 (function TestMemoryGrowWebAssemblyInstances() { |
| 233 print("TestMemoryGrowWebAssemblyInstances"); |
| 234 let memory = new WebAssembly.Memory({initial: 1, maximum: 15}); |
| 235 var builder = new WasmModuleBuilder(); |
| 236 builder.addImportedMemory("imported_mem"); |
| 237 builder.addFunction("mem_size", kSig_i_v) |
| 238 .addBody([kExprMemorySize, kMemoryZero]) |
| 239 .exportAs("mem_size"); |
| 240 builder.addFunction("grow", kSig_i_i) |
| 241 .addBody([kExprGetLocal, 0, kExprGrowMemory, kMemoryZero]) |
| 242 .exportFunc(); |
| 243 var module = new WebAssembly.Module(builder.toBuffer()); |
| 244 var instances = []; |
| 245 for (var i = 0; i < 6; i++) { |
| 246 instances.push(new WebAssembly.Instance(module, {imported_mem: memory})); |
| 247 } |
| 248 function verify_mem_size(expected_pages) { |
| 249 assertEquals(expected_pages*kPageSize, |
| 250 memory.buffer.byteLength); |
| 251 for (var i = 0; i < 6; i++) { |
| 252 assertEquals(expected_pages, instances[i].exports.mem_size()); |
| 253 } |
| 254 } |
| 255 |
| 256 // Verify initial memory size |
| 257 verify_mem_size(1); |
| 258 |
| 259 // Verify memory size with interleaving calls to Memory.grow, |
| 260 // GrowMemory opcode. |
| 261 var current_mem_size = 1; |
| 262 for (var i = 0; i < 5; i++) { |
| 263 function grow(pages) { return instances[i].exports.grow(pages); } |
| 264 assertEquals(current_mem_size, memory.grow(1)); |
| 265 verify_mem_size(++current_mem_size); |
| 266 assertEquals(current_mem_size, instances[i].exports.grow(1)); |
| 267 verify_mem_size(++current_mem_size); |
| 268 } |
| 269 |
| 270 assertThrows(() => memory.grow(5)); |
| 271 })(); |
| 272 |
| 273 (function TestImportedMemoryGrowMultipleInstances() { |
| 274 print("TestImportMemoryMultipleInstances"); |
| 275 let memory = new WebAssembly.Memory({initial: 5, maximum: 100}); |
| 276 var builder = new WasmModuleBuilder(); |
| 277 builder.addImportedMemory("imported_mem"); |
| 278 builder.addFunction("mem_size", kSig_i_v) |
| 279 .addBody([kExprMemorySize, kMemoryZero]) |
| 280 .exportFunc(); |
| 281 builder.addFunction("grow", kSig_i_i) |
| 282 .addBody([kExprGetLocal, 0, kExprGrowMemory, kMemoryZero]) |
| 283 .exportFunc(); |
| 284 var instances = []; |
| 285 for (var i = 0; i < 5; i++) { |
| 286 instances.push(builder.instantiate({imported_mem: memory})); |
| 287 } |
| 288 function grow_instance_0(pages) { return instances[0].exports.grow(pages); } |
| 289 function grow_instance_1(pages) { return instances[1].exports.grow(pages); } |
| 290 function grow_instance_2(pages) { return instances[2].exports.grow(pages); } |
| 291 function grow_instance_3(pages) { return instances[3].exports.grow(pages); } |
| 292 function grow_instance_4(pages) { return instances[4].exports.grow(pages); } |
| 293 |
| 294 function verify_mem_size(expected_pages) { |
| 295 assertEquals(expected_pages*kPageSize, memory.buffer.byteLength); |
| 296 for (var i = 0; i < 5; i++) { |
| 297 assertEquals(expected_pages, instances[i].exports.mem_size()); |
| 298 } |
| 299 } |
| 300 |
| 301 // Verify initial memory size |
| 302 verify_mem_size(5); |
| 303 |
| 304 // Grow instance memory and buffer memory out of order and verify memory is |
| 305 // updated correctly. |
| 306 assertEquals(5, grow_instance_0(7)); |
| 307 verify_mem_size(12); |
| 308 |
| 309 assertEquals(12, memory.grow(4)); |
| 310 verify_mem_size(16); |
| 311 |
| 312 assertEquals(16, grow_instance_4(1)); |
| 313 verify_mem_size(17); |
| 314 |
| 315 assertEquals(17, grow_instance_1(6)); |
| 316 verify_mem_size(23); |
| 317 |
| 318 assertEquals(23, grow_instance_3(2)); |
| 319 verify_mem_size(25); |
| 320 |
| 321 assertEquals(25, memory.grow(10)); |
| 322 verify_mem_size(35); |
| 323 |
| 324 assertEquals(35, grow_instance_2(15)); |
| 325 verify_mem_size(50); |
| 326 assertThrows(() => memory.grow(51)); |
| 327 })(); |
| 328 |
| 329 (function TestExportImportedMemoryGrowMultipleInstances() { |
| 330 // TODO(gdeepti):Exported memory objects currently do not take max_size |
| 331 // into account so this can grow past the maximum specified in the exported |
| 332 // memory object. Assert that growing past maximum for exported objects fails. |
| 333 print("TestExportImportedMemoryGrowMultipleInstances"); |
| 334 var instance; |
| 335 { |
| 336 let builder = new WasmModuleBuilder(); |
| 337 builder.addMemory(1, 11, true); |
| 338 builder.exportMemoryAs("exported_mem"); |
| 339 builder.addFunction("mem_size", kSig_i_v) |
| 340 .addBody([kExprMemorySize, kMemoryZero]) |
| 341 .exportFunc(); |
| 342 instance = builder.instantiate(); |
| 343 } |
| 344 var builder = new WasmModuleBuilder(); |
| 345 builder.addImportedMemory("imported_mem"); |
| 346 builder.addFunction("mem_size", kSig_i_v) |
| 347 .addBody([kExprMemorySize, kMemoryZero]) |
| 348 .exportFunc(); |
| 349 builder.addFunction("grow", kSig_i_i) |
| 350 .addBody([kExprGetLocal, 0, kExprGrowMemory, kMemoryZero]) |
| 351 .exportFunc(); |
| 352 var instances = []; |
| 353 for (var i = 0; i < 10; i++) { |
| 354 instances.push(builder.instantiate({ |
| 355 imported_mem: instance.exports.exported_mem})); |
| 356 } |
| 357 function verify_mem_size(expected_pages) { |
| 358 for (var i = 0; i < 10; i++) { |
| 359 assertEquals(expected_pages, instances[i].exports.mem_size()); |
| 360 } |
| 361 } |
| 362 var current_mem_size = 1; |
| 363 for (var i = 0; i < 10; i++) { |
| 364 function grow(pages) { return instances[i].exports.grow(pages); } |
| 365 assertEquals(current_mem_size, instances[i].exports.grow(1)); |
| 366 verify_mem_size(++current_mem_size); |
| 367 } |
| 368 })(); |
OLD | NEW |