OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 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 --allow-natives-syntax --expose-gc | 5 // Flags: --expose-wasm --allow-natives-syntax --expose-gc |
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 SerializeAndDeserializeModule() { | 10 (function SerializeAndDeserializeModule() { |
(...skipping 19 matching lines...) Expand all Loading... |
30 // writer(mem[i]); | 30 // writer(mem[i]); |
31 // return mem[i] + some_value(); | 31 // return mem[i] + some_value(); |
32 builder.addFunction("_wrap_writer", signature) | 32 builder.addFunction("_wrap_writer", signature) |
33 .addBody([ | 33 .addBody([ |
34 kExprGetLocal, 0, | 34 kExprGetLocal, 0, |
35 kExprCallFunction, 1]); | 35 kExprCallFunction, 1]); |
36 builder.appendToTable([2, 3]); | 36 builder.appendToTable([2, 3]); |
37 | 37 |
38 var wire_bytes = builder.toBuffer(); | 38 var wire_bytes = builder.toBuffer(); |
39 var module = new WebAssembly.Module(wire_bytes); | 39 var module = new WebAssembly.Module(wire_bytes); |
40 var buff = %SerializeWasmModule(module); | |
41 module = null; | |
42 gc(); | |
43 module = %DeserializeWasmModule(buff, wire_bytes); | |
44 | |
45 var mem_1 = new WebAssembly.Memory({initial: 1}); | 40 var mem_1 = new WebAssembly.Memory({initial: 1}); |
46 var view_1 = new Int32Array(mem_1.buffer); | 41 var view_1 = new Int32Array(mem_1.buffer); |
47 | |
48 view_1[0] = 42; | 42 view_1[0] = 42; |
49 | |
50 var outval_1; | 43 var outval_1; |
51 var i1 = new WebAssembly.Instance(module, {"": | 44 var i1 = new WebAssembly.Instance(module, {"": |
52 {some_value: () => 1, | 45 {some_value: () => 1, |
53 writer: (x) => outval_1 = x , | 46 writer: (x) => outval_1 = x , |
54 memory: mem_1} | 47 memory: mem_1} |
55 }); | 48 }); |
56 | 49 |
57 assertEquals(43, i1.exports.main(0)); | 50 assertEquals(43, i1.exports.main(0)); |
58 | 51 |
59 assertEquals(42, outval_1); | 52 assertEquals(42, outval_1); |
| 53 var buff = %SerializeWasmModule(module); |
| 54 module = null; |
| 55 gc(); |
| 56 module = %DeserializeWasmModule(buff, wire_bytes); |
| 57 |
| 58 var mem_2 = new WebAssembly.Memory({initial: 2}); |
| 59 var view_2 = new Int32Array(mem_2.buffer); |
| 60 |
| 61 view_2[0] = 50; |
| 62 |
| 63 var outval_2; |
| 64 var i2 = new WebAssembly.Instance(module, {"": |
| 65 {some_value: () => 1, |
| 66 writer: (x) => outval_2 = x , |
| 67 memory: mem_2} |
| 68 }); |
| 69 |
| 70 assertEquals(51, i2.exports.main(0)); |
| 71 |
| 72 assertEquals(50, outval_2); |
| 73 // The instances don't share memory through deserialization. |
| 74 assertEquals(43, i1.exports.main(0)); |
60 })(); | 75 })(); |
61 | 76 |
62 (function DeserializeInvalidObject() { | 77 (function DeserializeInvalidObject() { |
63 var invalid_buffer = new ArrayBuffer(10); | 78 var invalid_buffer = new ArrayBuffer(10); |
64 | 79 |
65 module = %DeserializeWasmModule(invalid_buffer, invalid_buffer); | 80 module = %DeserializeWasmModule(invalid_buffer, invalid_buffer); |
66 assertEquals(module, undefined); | 81 assertEquals(module, undefined); |
67 })(); | 82 })(); |
68 | 83 |
69 (function RelationBetweenModuleAndClone() { | 84 (function RelationBetweenModuleAndClone() { |
(...skipping 26 matching lines...) Expand all Loading... |
96 var serialized = %SerializeWasmModule(compiled_module); | 111 var serialized = %SerializeWasmModule(compiled_module); |
97 var clone = %DeserializeWasmModule(serialized, wire_bytes); | 112 var clone = %DeserializeWasmModule(serialized, wire_bytes); |
98 | 113 |
99 assertNotNull(clone); | 114 assertNotNull(clone); |
100 assertFalse(clone == undefined); | 115 assertFalse(clone == undefined); |
101 assertFalse(clone == compiled_module); | 116 assertFalse(clone == compiled_module); |
102 assertEquals(clone.constructor, compiled_module.constructor); | 117 assertEquals(clone.constructor, compiled_module.constructor); |
103 var instance3 = new WebAssembly.Instance(clone); | 118 var instance3 = new WebAssembly.Instance(clone); |
104 assertFalse(instance3 == undefined); | 119 assertFalse(instance3 == undefined); |
105 })(); | 120 })(); |
| 121 |
| 122 |
| 123 (function SerializeAfterInstantiationWithMemory() { |
| 124 let builder = new WasmModuleBuilder(); |
| 125 builder.addImportedMemory("", "memory", 1); |
| 126 builder.addFunction("main", kSig_i_v) |
| 127 .addBody([kExprI32Const, 42]) |
| 128 .exportFunc(); |
| 129 |
| 130 var wire_bytes = builder.toBuffer() |
| 131 var compiled_module = new WebAssembly.Module(wire_bytes); |
| 132 var mem_1 = new WebAssembly.Memory({initial: 1}); |
| 133 var ffi = {"":{memory:mem_1}}; |
| 134 var instance1 = new WebAssembly.Instance(compiled_module, ffi); |
| 135 var serialized = %SerializeWasmModule(compiled_module); |
| 136 var clone = %DeserializeWasmModule(serialized, wire_bytes); |
| 137 |
| 138 assertNotNull(clone); |
| 139 assertFalse(clone == undefined); |
| 140 assertFalse(clone == compiled_module); |
| 141 assertEquals(clone.constructor, compiled_module.constructor); |
| 142 var instance2 = new WebAssembly.Instance(clone, ffi); |
| 143 assertFalse(instance2 == undefined); |
| 144 })(); |
| 145 |
| 146 (function GlobalsArePrivateBetweenClones() { |
| 147 var builder = new WasmModuleBuilder(); |
| 148 builder.addGlobal(kWasmI32, true); |
| 149 builder.addFunction("read", kSig_i_v) |
| 150 .addBody([ |
| 151 kExprGetGlobal, 0]) |
| 152 .exportFunc(); |
| 153 |
| 154 builder.addFunction("write", kSig_v_i) |
| 155 .addBody([ |
| 156 kExprGetLocal, 0, |
| 157 kExprSetGlobal, 0]) |
| 158 .exportFunc(); |
| 159 |
| 160 var wire_bytes = builder.toBuffer(); |
| 161 var module = new WebAssembly.Module(wire_bytes); |
| 162 var i1 = new WebAssembly.Instance(module); |
| 163 // serialize and replace module |
| 164 var buff = %SerializeWasmModule(module); |
| 165 var module_clone = %DeserializeWasmModule(buff, wire_bytes); |
| 166 var i2 = new WebAssembly.Instance(module_clone); |
| 167 i1.exports.write(1); |
| 168 i2.exports.write(2); |
| 169 assertEquals(1, i1.exports.read()); |
| 170 assertEquals(2, i2.exports.read()); |
| 171 })(); |
| 172 |
| 173 (function SharedTableTest() { |
| 174 let kTableSize = 3; |
| 175 var sig_index1; |
| 176 |
| 177 function MakeTableExportingModule(constant) { |
| 178 // A module that defines a table and exports it. |
| 179 var builder = new WasmModuleBuilder(); |
| 180 builder.addType(kSig_i_i); |
| 181 builder.addType(kSig_i_ii); |
| 182 sig_index1 = builder.addType(kSig_i_v); |
| 183 var f1 = builder.addFunction("f1", sig_index1) |
| 184 .addBody([kExprI32Const, constant]); |
| 185 |
| 186 builder.addFunction("main", kSig_i_ii) |
| 187 .addBody([ |
| 188 kExprGetLocal, 0, // -- |
| 189 kExprCallIndirect, sig_index1, kTableZero]) // -- |
| 190 .exportAs("main"); |
| 191 |
| 192 builder.setFunctionTableLength(kTableSize); |
| 193 builder.addFunctionTableInit(0, false, [f1.index]); |
| 194 builder.addExportOfKind("table", kExternalTable, 0); |
| 195 |
| 196 return new WebAssembly.Module(builder.toBuffer()); |
| 197 } |
| 198 var m1 = MakeTableExportingModule(11); |
| 199 |
| 200 // Module {m2} imports the table and adds {f2}. |
| 201 var builder = new WasmModuleBuilder(); |
| 202 builder.addType(kSig_i_ii); |
| 203 var sig_index2 = builder.addType(kSig_i_v); |
| 204 var f2 = builder.addFunction("f2", sig_index2) |
| 205 .addBody([kExprI32Const, 22]); |
| 206 |
| 207 builder.addFunction("main", kSig_i_ii) |
| 208 .addBody([ |
| 209 kExprGetLocal, 0, // -- |
| 210 kExprCallIndirect, sig_index2, kTableZero]) // -- |
| 211 .exportAs("main"); |
| 212 |
| 213 builder.addImportedTable("z", "table", kTableSize, kTableSize); |
| 214 builder.addFunctionTableInit(1, false, [f2.index], true); |
| 215 var m2_bytes = builder.toBuffer(); |
| 216 var m2 = new WebAssembly.Module(m2_bytes); |
| 217 |
| 218 assertFalse(sig_index1 == sig_index2); |
| 219 |
| 220 var i1 = new WebAssembly.Instance(m1); |
| 221 var i2 = new WebAssembly.Instance(m2, {z: {table: i1.exports.table}}); |
| 222 |
| 223 var serialized_m2 = %SerializeWasmModule(m2); |
| 224 var m2_clone = %DeserializeWasmModule(serialized_m2, m2_bytes); |
| 225 |
| 226 var m3 = MakeTableExportingModule(33); |
| 227 var i3 = new WebAssembly.Instance(m3); |
| 228 var i2_prime = new WebAssembly.Instance(m2_clone, |
| 229 {z: {table: i3.exports.table}}); |
| 230 |
| 231 assertEquals(11, i1.exports.main(0)); |
| 232 assertEquals(11, i2.exports.main(0)); |
| 233 |
| 234 assertEquals(22, i1.exports.main(1)); |
| 235 assertEquals(22, i2.exports.main(1)); |
| 236 |
| 237 assertEquals(33, i3.exports.main(0)); |
| 238 assertEquals(33, i2_prime.exports.main(0)); |
| 239 |
| 240 assertThrows(() => i1.exports.main(2)); |
| 241 assertThrows(() => i2.exports.main(2)); |
| 242 assertThrows(() => i1.exports.main(3)); |
| 243 assertThrows(() => i2.exports.main(3)); |
| 244 |
| 245 })(); |
OLD | NEW |