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 module = %DeserializeWasmModule(buff, wire_bytes); | |
bradnelson
2017/03/29 20:49:07
Any reason to drop the link to the old one here? I
Mircea Trofin
2017/03/29 20:56:30
Done.
| |
166 var i2 = new WebAssembly.Instance(module); | |
167 i1.exports.write(1); | |
168 i2.exports.write(2); | |
169 assertEquals(1, i1.exports.read()); | |
170 assertEquals(2, i2.exports.read()); | |
171 })(); | |
172 | |
bradnelson
2017/03/29 20:49:07
You're inconsistent with lines skipped between tes
Mircea Trofin
2017/03/29 20:56:30
Done.
| |
173 | |
174 (function SharedTableTest() { | |
175 let kTableSize = 3; | |
176 var sig_index1; | |
177 | |
178 function MakeTableExportingModule(constant) { | |
bradnelson
2017/03/29 20:49:07
Indent weird here.
Mircea Trofin
2017/03/29 20:56:30
Done.
| |
179 // A module that defines a table and exports it. | |
180 var builder = new WasmModuleBuilder(); | |
181 builder.addType(kSig_i_i); | |
182 builder.addType(kSig_i_ii); | |
183 sig_index1 = builder.addType(kSig_i_v); | |
184 var f1 = builder.addFunction("f1", sig_index1) | |
185 .addBody([kExprI32Const, constant]); | |
186 | |
187 builder.addFunction("main", kSig_i_ii) | |
188 .addBody([ | |
189 kExprGetLocal, 0, // -- | |
190 kExprCallIndirect, sig_index1, kTableZero]) // -- | |
191 .exportAs("main"); | |
192 | |
193 builder.setFunctionTableLength(kTableSize); | |
194 builder.addFunctionTableInit(0, false, [f1.index]); | |
195 builder.addExportOfKind("table", kExternalTable, 0); | |
196 | |
197 return new WebAssembly.Module(builder.toBuffer()); | |
198 } | |
199 var m1 = MakeTableExportingModule(11); | |
200 | |
201 // Module {m2} imports the table and adds {f2}. | |
202 var builder = new WasmModuleBuilder(); | |
203 builder.addType(kSig_i_ii); | |
204 var sig_index2 = builder.addType(kSig_i_v); | |
205 var f2 = builder.addFunction("f2", sig_index2) | |
206 .addBody([kExprI32Const, 22]); | |
207 | |
208 builder.addFunction("main", kSig_i_ii) | |
209 .addBody([ | |
210 kExprGetLocal, 0, // -- | |
211 kExprCallIndirect, sig_index2, kTableZero]) // -- | |
212 .exportAs("main"); | |
213 | |
214 builder.addImportedTable("z", "table", kTableSize, kTableSize); | |
215 builder.addFunctionTableInit(1, false, [f2.index], true); | |
216 var m2_bytes = builder.toBuffer(); | |
217 var m2 = new WebAssembly.Module(m2_bytes); | |
218 | |
219 assertFalse(sig_index1 == sig_index2); | |
220 | |
221 var i1 = new WebAssembly.Instance(m1); | |
222 var i2 = new WebAssembly.Instance(m2, {z: {table: i1.exports.table}}); | |
223 | |
224 var serialized_m2 = %SerializeWasmModule(m2); | |
225 var m2_clone = %DeserializeWasmModule(serialized_m2, m2_bytes); | |
bradnelson
2017/03/29 20:49:07
Worth a variant where you force a GC to ensure lin
Mircea Trofin
2017/03/29 20:56:30
We test that we execute instance-specific function
| |
226 | |
227 var m3 = MakeTableExportingModule(33); | |
228 var i3 = new WebAssembly.Instance(m3); | |
229 var i2_prime = new WebAssembly.Instance(m2_clone, {z: {table: i3.exports.table }}); | |
bradnelson
2017/03/29 20:49:07
>80
Mircea Trofin
2017/03/29 20:56:30
Done.
| |
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 |