Index: test/mjsunit/wasm/import-memory.js |
diff --git a/test/mjsunit/wasm/import-memory.js b/test/mjsunit/wasm/import-memory.js |
index 845da91d1d17bca6545d322edaeb05ef6ed9f49e..3b70332f97bc5eb95d2dbd81135df3a89204d0c2 100644 |
--- a/test/mjsunit/wasm/import-memory.js |
+++ b/test/mjsunit/wasm/import-memory.js |
@@ -40,7 +40,6 @@ load("test/mjsunit/wasm/wasm-module-builder.js"); |
assertSame(memory, instance.exports.daggle); |
})(); |
- |
(function TestImportExport() { |
print("TestImportExport"); |
var i1; |
@@ -229,3 +228,141 @@ load("test/mjsunit/wasm/wasm-module-builder.js"); |
} |
assertEquals(-1, instance.exports.grow(1)); |
})(); |
+ |
+(function TestMemoryGrowWebAssemblyInstances() { |
+ print("TestMemoryGrowWebAssemblyInstances"); |
+ let memory = new WebAssembly.Memory({initial: 1, maximum: 15}); |
+ var builder = new WasmModuleBuilder(); |
+ builder.addImportedMemory("imported_mem"); |
+ builder.addFunction("mem_size", kSig_i_v) |
+ .addBody([kExprMemorySize, kMemoryZero]) |
+ .exportAs("mem_size"); |
+ builder.addFunction("grow", kSig_i_i) |
+ .addBody([kExprGetLocal, 0, kExprGrowMemory, kMemoryZero]) |
+ .exportFunc(); |
+ var module = new WebAssembly.Module(builder.toBuffer()); |
+ var instances = []; |
+ for (var i = 0; i < 6; i++) { |
+ instances.push(new WebAssembly.Instance(module, {imported_mem: memory})); |
+ } |
+ function verify_mem_size(expected_pages) { |
+ assertEquals(expected_pages*kPageSize, |
+ memory.buffer.byteLength); |
+ for (var i = 0; i < 6; i++) { |
+ assertEquals(expected_pages, instances[i].exports.mem_size()); |
+ } |
+ } |
+ |
+ // Verify initial memory size |
+ verify_mem_size(1); |
+ |
+ // Verify memory size with interleaving calls to Memory.grow, |
+ // GrowMemory opcode. |
+ var current_mem_size = 1; |
+ for (var i = 0; i < 5; i++) { |
+ function grow(pages) { return instances[i].exports.grow(pages); } |
+ assertEquals(current_mem_size, memory.grow(1)); |
+ verify_mem_size(++current_mem_size); |
+ assertEquals(current_mem_size, instances[i].exports.grow(1)); |
+ verify_mem_size(++current_mem_size); |
+ } |
+ |
+ assertThrows(() => memory.grow(5)); |
+})(); |
+ |
+(function TestImportedMemoryGrowMultipleInstances() { |
+ print("TestImportMemoryMultipleInstances"); |
+ let memory = new WebAssembly.Memory({initial: 5, maximum: 100}); |
+ var builder = new WasmModuleBuilder(); |
+ builder.addImportedMemory("imported_mem"); |
+ builder.addFunction("mem_size", kSig_i_v) |
+ .addBody([kExprMemorySize, kMemoryZero]) |
+ .exportFunc(); |
+ builder.addFunction("grow", kSig_i_i) |
+ .addBody([kExprGetLocal, 0, kExprGrowMemory, kMemoryZero]) |
+ .exportFunc(); |
+ var instances = []; |
+ for (var i = 0; i < 5; i++) { |
+ instances.push(builder.instantiate({imported_mem: memory})); |
+ } |
+ function grow_instance_0(pages) { return instances[0].exports.grow(pages); } |
+ function grow_instance_1(pages) { return instances[1].exports.grow(pages); } |
+ function grow_instance_2(pages) { return instances[2].exports.grow(pages); } |
+ function grow_instance_3(pages) { return instances[3].exports.grow(pages); } |
+ function grow_instance_4(pages) { return instances[4].exports.grow(pages); } |
+ |
+ function verify_mem_size(expected_pages) { |
+ assertEquals(expected_pages*kPageSize, memory.buffer.byteLength); |
+ for (var i = 0; i < 5; i++) { |
+ assertEquals(expected_pages, instances[i].exports.mem_size()); |
+ } |
+ } |
+ |
+ // Verify initial memory size |
+ verify_mem_size(5); |
+ |
+ // Grow instance memory and buffer memory out of order and verify memory is |
+ // updated correctly. |
+ assertEquals(5, grow_instance_0(7)); |
+ verify_mem_size(12); |
+ |
+ assertEquals(12, memory.grow(4)); |
+ verify_mem_size(16); |
+ |
+ assertEquals(16, grow_instance_4(1)); |
+ verify_mem_size(17); |
+ |
+ assertEquals(17, grow_instance_1(6)); |
+ verify_mem_size(23); |
+ |
+ assertEquals(23, grow_instance_3(2)); |
+ verify_mem_size(25); |
+ |
+ assertEquals(25, memory.grow(10)); |
+ verify_mem_size(35); |
+ |
+ assertEquals(35, grow_instance_2(15)); |
+ verify_mem_size(50); |
+ assertThrows(() => memory.grow(51)); |
+})(); |
+ |
+(function TestExportImportedMemoryGrowMultipleInstances() { |
+ // TODO(gdeepti):Exported memory objects currently do not take max_size |
+ // into account so this can grow past the maximum specified in the exported |
+ // memory object. Assert that growing past maximum for exported objects fails. |
+ print("TestExportImportedMemoryGrowMultipleInstances"); |
+ var instance; |
+ { |
+ let builder = new WasmModuleBuilder(); |
+ builder.addMemory(1, 11, true); |
+ builder.exportMemoryAs("exported_mem"); |
+ builder.addFunction("mem_size", kSig_i_v) |
+ .addBody([kExprMemorySize, kMemoryZero]) |
+ .exportFunc(); |
+ instance = builder.instantiate(); |
+ } |
+ var builder = new WasmModuleBuilder(); |
+ builder.addImportedMemory("imported_mem"); |
+ builder.addFunction("mem_size", kSig_i_v) |
+ .addBody([kExprMemorySize, kMemoryZero]) |
+ .exportFunc(); |
+ builder.addFunction("grow", kSig_i_i) |
+ .addBody([kExprGetLocal, 0, kExprGrowMemory, kMemoryZero]) |
+ .exportFunc(); |
+ var instances = []; |
+ for (var i = 0; i < 10; i++) { |
+ instances.push(builder.instantiate({ |
+ imported_mem: instance.exports.exported_mem})); |
+ } |
+ function verify_mem_size(expected_pages) { |
+ for (var i = 0; i < 10; i++) { |
+ assertEquals(expected_pages, instances[i].exports.mem_size()); |
+ } |
+ } |
+ var current_mem_size = 1; |
+ for (var i = 0; i < 10; i++) { |
+ function grow(pages) { return instances[i].exports.grow(pages); } |
+ assertEquals(current_mem_size, instances[i].exports.grow(1)); |
+ verify_mem_size(++current_mem_size); |
+ } |
+})(); |