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 | 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 let kReturnValue = 17; | 10 let kReturnValue = 17; |
(...skipping 11 matching lines...) Expand all Loading... |
22 function CheckInstance(instance) { | 22 function CheckInstance(instance) { |
23 assertFalse(instance === undefined); | 23 assertFalse(instance === undefined); |
24 assertFalse(instance === null); | 24 assertFalse(instance === null); |
25 assertFalse(instance === 0); | 25 assertFalse(instance === 0); |
26 assertEquals("object", typeof instance); | 26 assertEquals("object", typeof instance); |
27 | 27 |
28 // Check the exports object is frozen. | 28 // Check the exports object is frozen. |
29 assertFalse(Object.isExtensible(instance.exports)); | 29 assertFalse(Object.isExtensible(instance.exports)); |
30 assertTrue(Object.isFrozen(instance.exports)); | 30 assertTrue(Object.isFrozen(instance.exports)); |
31 | 31 |
32 // Check the memory is an ArrayBuffer. | 32 // Check the memory is WebAssembly.Memory. |
33 var mem = instance.exports.memory; | 33 var mem = instance.exports.memory; |
34 assertFalse(mem === undefined); | 34 assertFalse(mem === undefined); |
35 assertFalse(mem === null); | 35 assertFalse(mem === null); |
36 assertFalse(mem === 0); | 36 assertFalse(mem === 0); |
37 assertEquals("object", typeof mem); | 37 assertEquals("object", typeof mem); |
38 assertTrue(mem instanceof WebAssembly.Memory); | 38 assertTrue(mem instanceof WebAssembly.Memory); |
39 var buf = mem.buffer; | 39 var buf = mem.buffer; |
40 assertTrue(buf instanceof ArrayBuffer); | 40 assertTrue(buf instanceof ArrayBuffer); |
41 assertEquals(65536, buf.byteLength); | 41 assertEquals(65536, buf.byteLength); |
42 for (var i = 0; i < 4; i++) { | 42 for (var i = 0; i < 4; i++) { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 promise.then(compiled_module => { | 110 promise.then(compiled_module => { |
111 let instance_1 = new WebAssembly.Instance(compiled_module); | 111 let instance_1 = new WebAssembly.Instance(compiled_module); |
112 let instance_2 = new WebAssembly.Instance(compiled_module); | 112 let instance_2 = new WebAssembly.Instance(compiled_module); |
113 assertTrue(instance_1 != instance_2); | 113 assertTrue(instance_1 != instance_2); |
114 }); | 114 }); |
115 })(); | 115 })(); |
116 | 116 |
117 (function InstancesAreIsolatedFromEachother() { | 117 (function InstancesAreIsolatedFromEachother() { |
118 print("InstancesAreIsolatedFromEachother..."); | 118 print("InstancesAreIsolatedFromEachother..."); |
119 var builder = new WasmModuleBuilder(); | 119 var builder = new WasmModuleBuilder(); |
120 builder.addMemory(1,1, true); | 120 builder.addImportedMemory("", "memory", 1,1); |
121 var kSig_v_i = makeSig([kWasmI32], []); | 121 var kSig_v_i = makeSig([kWasmI32], []); |
122 var signature = builder.addType(kSig_v_i); | 122 var signature = builder.addType(kSig_v_i); |
123 builder.addImport("m", "some_value", kSig_i_v); | 123 builder.addImport("m", "some_value", kSig_i_v); |
124 builder.addImport("m", "writer", signature); | 124 builder.addImport("m", "writer", signature); |
125 | 125 |
126 builder.addFunction("main", kSig_i_i) | 126 builder.addFunction("main", kSig_i_i) |
127 .addBody([ | 127 .addBody([ |
128 kExprGetLocal, 0, | 128 kExprGetLocal, 0, |
129 kExprI32LoadMem, 0, 0, | 129 kExprI32LoadMem, 0, 0, |
130 kExprI32Const, 1, | 130 kExprI32Const, 1, |
(...skipping 18 matching lines...) Expand all Loading... |
149 var mem_2 = new WebAssembly.Memory({initial: 1}); | 149 var mem_2 = new WebAssembly.Memory({initial: 1}); |
150 var view_1 = new Int32Array(mem_1.buffer); | 150 var view_1 = new Int32Array(mem_1.buffer); |
151 var view_2 = new Int32Array(mem_2.buffer); | 151 var view_2 = new Int32Array(mem_2.buffer); |
152 | 152 |
153 view_1[0] = 42; | 153 view_1[0] = 42; |
154 view_2[0] = 1000; | 154 view_2[0] = 1000; |
155 | 155 |
156 var outval_1; | 156 var outval_1; |
157 var outval_2; | 157 var outval_2; |
158 var i1 = new WebAssembly.Instance(module, {m: {some_value: () => 1, | 158 var i1 = new WebAssembly.Instance(module, {m: {some_value: () => 1, |
159 writer: (x)=>outval_1 = x }}, | 159 writer: (x)=>outval_1 = x }, |
160 mem_1); | 160 "": {memory: mem_1}}); |
| 161 |
161 var i2 = new WebAssembly.Instance(module, {m: {some_value: () => 2, | 162 var i2 = new WebAssembly.Instance(module, {m: {some_value: () => 2, |
162 writer: (x)=>outval_2 = x }}, | 163 writer: (x)=>outval_2 = x }, |
163 mem_2); | 164 "": {memory: mem_2}}); |
164 | 165 |
165 assertEquals(43, i1.exports.main(0)); | 166 assertEquals(43, i1.exports.main(0)); |
166 assertEquals(1002, i2.exports.main(0)); | 167 assertEquals(1002, i2.exports.main(0)); |
167 | 168 |
168 assertEquals(42, outval_1); | 169 assertEquals(42, outval_1); |
169 assertEquals(1000, outval_2); | 170 assertEquals(1000, outval_2); |
170 })(); | 171 })(); |
171 | 172 |
172 (function GlobalsArePrivateToTheInstance() { | 173 (function GlobalsArePrivateToTheInstance() { |
173 print("GlobalsArePrivateToTheInstance..."); | 174 print("GlobalsArePrivateToTheInstance..."); |
(...skipping 16 matching lines...) Expand all Loading... |
190 i1.exports.write(1); | 191 i1.exports.write(1); |
191 i2.exports.write(2); | 192 i2.exports.write(2); |
192 assertEquals(1, i1.exports.read()); | 193 assertEquals(1, i1.exports.read()); |
193 assertEquals(2, i2.exports.read()); | 194 assertEquals(2, i2.exports.read()); |
194 })(); | 195 })(); |
195 | 196 |
196 | 197 |
197 (function InstanceMemoryIsIsolated() { | 198 (function InstanceMemoryIsIsolated() { |
198 print("InstanceMemoryIsIsolated..."); | 199 print("InstanceMemoryIsIsolated..."); |
199 var builder = new WasmModuleBuilder(); | 200 var builder = new WasmModuleBuilder(); |
200 builder.addMemory(1,1, true); | 201 builder.addImportedMemory("", "memory", 1,1); |
201 | 202 |
202 builder.addFunction("f", kSig_i_v) | 203 builder.addFunction("f", kSig_i_v) |
203 .addBody([ | 204 .addBody([ |
204 kExprI32Const, 0, | 205 kExprI32Const, 0, |
205 kExprI32LoadMem, 0, 0 | 206 kExprI32LoadMem, 0, 0 |
206 ]).exportFunc(); | 207 ]).exportFunc(); |
207 | 208 |
208 var mem_1 = new WebAssembly.Memory({initial: 1}); | 209 var mem_1 = new WebAssembly.Memory({initial: 1}); |
209 var mem_2 = new WebAssembly.Memory({initial: 1}); | 210 var mem_2 = new WebAssembly.Memory({initial: 1}); |
210 var view_1 = new Int32Array(mem_1.buffer); | 211 var view_1 = new Int32Array(mem_1.buffer); |
211 var view_2 = new Int32Array(mem_2.buffer); | 212 var view_2 = new Int32Array(mem_2.buffer); |
212 view_1[0] = 1; | 213 view_1[0] = 1; |
213 view_2[0] = 1000; | 214 view_2[0] = 1000; |
214 | 215 |
215 var module = new WebAssembly.Module(builder.toBuffer()); | 216 var module = new WebAssembly.Module(builder.toBuffer()); |
216 var i1 = new WebAssembly.Instance(module, null, mem_1); | 217 var i1 = new WebAssembly.Instance(module, {"":{memory:mem_1}}); |
217 var i2 = new WebAssembly.Instance(module, null, mem_2); | 218 var i2 = new WebAssembly.Instance(module, {"":{memory:mem_2}}); |
218 | 219 |
219 assertEquals(1, i1.exports.f()); | 220 assertEquals(1, i1.exports.f()); |
220 assertEquals(1000, i2.exports.f()); | 221 assertEquals(1000, i2.exports.f()); |
221 })(); | 222 })(); |
222 | 223 |
223 (function MustBeMemory() { | 224 (function MustBeMemory() { |
224 print("MustBeMemory..."); | 225 print("MustBeMemory..."); |
225 var memory = new ArrayBuffer(65536); | 226 var memory = new ArrayBuffer(65536); |
226 var module = new WebAssembly.Module(buffer); | 227 let builder = new WasmModuleBuilder(); |
227 assertThrows(() => new WebAssembly.Instance(module, null, memory), TypeError); | 228 builder.addImportedMemory("", "memory"); |
| 229 |
| 230 let module = new WebAssembly.Module(builder.toBuffer()); |
| 231 |
| 232 |
| 233 assertThrows(() => new WebAssembly.Instance(module, {"":{memory:memory}}), Web
Assembly.LinkError); |
228 })(); | 234 })(); |
OLD | NEW |