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 = 117; | 10 let kReturnValue = 117; |
(...skipping 13 matching lines...) Expand all Loading... |
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 memory is an ArrayBuffer. | 28 // Check the memory is an ArrayBuffer. |
29 var mem = instance.exports.memory; | 29 var mem = instance.exports.memory; |
30 assertFalse(mem === undefined); | 30 assertFalse(mem === undefined); |
31 assertFalse(mem === null); | 31 assertFalse(mem === null); |
32 assertFalse(mem === 0); | 32 assertFalse(mem === 0); |
33 assertEquals("object", typeof mem); | 33 assertEquals("object", typeof mem); |
34 assertTrue(mem instanceof WebAssembly.Memory); | 34 assertTrue(mem instanceof ArrayBuffer); |
35 var buf = mem.buffer; | 35 for (let i = 0; i < 4; i++) { |
36 assertTrue(buf instanceof ArrayBuffer); | |
37 assertEquals(65536, buf.byteLength); | |
38 for (var i = 0; i < 4; i++) { | |
39 instance.exports.memory = 0; // should be ignored | 36 instance.exports.memory = 0; // should be ignored |
40 mem.buffer = 0; // should be ignored | |
41 assertSame(mem, instance.exports.memory); | 37 assertSame(mem, instance.exports.memory); |
42 assertSame(buf, mem.buffer); | |
43 } | 38 } |
44 | 39 |
| 40 assertEquals(65536, instance.exports.memory.byteLength); |
| 41 |
45 // Check the properties of the main function. | 42 // Check the properties of the main function. |
46 let main = instance.exports.main; | 43 let main = instance.exports.main; |
47 assertFalse(main === undefined); | 44 assertFalse(main === undefined); |
48 assertFalse(main === null); | 45 assertFalse(main === null); |
49 assertFalse(main === 0); | 46 assertFalse(main === 0); |
50 assertEquals("function", typeof main); | 47 assertEquals("function", typeof main); |
51 | 48 |
52 assertEquals(kReturnValue, main()); | 49 assertEquals(kReturnValue, main()); |
53 } | 50 } |
54 | 51 |
55 // Deprecated experimental API. | 52 // Deprecated experimental API. |
56 CheckInstance(Wasm.instantiateModule(buffer)); | 53 CheckInstance(Wasm.instantiateModule(buffer)); |
57 | 54 |
58 // Official API | 55 // Official API |
59 let module = new WebAssembly.Module(buffer); | 56 let module = new WebAssembly.Module(buffer); |
60 CheckInstance(new WebAssembly.Instance(module)); | 57 CheckInstance(new WebAssembly.Instance(module)); |
61 | 58 |
62 let promise = WebAssembly.compile(buffer); | 59 let promise = WebAssembly.compile(buffer); |
63 promise.then(module => CheckInstance(new WebAssembly.Instance(module))); | 60 promise.then(module => CheckInstance(new WebAssembly.Instance(module))); |
64 | 61 |
65 // Negative tests. | 62 // Negative tests. |
66 (function InvalidModules() { | 63 (function InvalidModules() { |
67 print("InvalidModules..."); | |
68 let invalid_cases = [undefined, 1, "", "a", {some:1, obj: "b"}]; | 64 let invalid_cases = [undefined, 1, "", "a", {some:1, obj: "b"}]; |
69 let len = invalid_cases.length; | 65 let len = invalid_cases.length; |
70 for (var i = 0; i < len; ++i) { | 66 for (var i = 0; i < len; ++i) { |
71 try { | 67 try { |
72 let instance = new WebAssembly.Instance(1); | 68 let instance = new WebAssembly.Instance(1); |
73 assertUnreachable("should not be able to instantiate invalid modules."); | 69 assertUnreachable("should not be able to instantiate invalid modules."); |
74 } catch (e) { | 70 } catch (e) { |
75 assertContains("Argument 0", e.toString()); | 71 assertContains("Argument 0", e.toString()); |
76 } | 72 } |
77 } | 73 } |
78 })(); | 74 })(); |
79 | 75 |
80 // Compile async an invalid blob. | 76 // Compile async an invalid blob. |
81 (function InvalidBinaryAsyncCompilation() { | 77 (function InvalidBinaryAsyncCompilation() { |
82 print("InvalidBinaryAsyncCompilation..."); | |
83 let builder = new WasmModuleBuilder(); | 78 let builder = new WasmModuleBuilder(); |
84 builder.addFunction("f", kSig_i_i) | 79 builder.addFunction("f", kSig_i_i) |
85 .addBody([kExprCallFunction, 0]); | 80 .addBody([kExprCallImport, kArity0, 0]); |
86 let promise = WebAssembly.compile(builder.toBuffer()); | 81 let promise = WebAssembly.compile(builder.toBuffer()); |
87 promise | 82 promise |
88 .then(compiled => | 83 .then(compiled => |
89 assertUnreachable("should not be able to compile invalid blob.")) | 84 assertUnreachable("should not be able to compile invalid blob.")) |
90 .catch(e => assertContains("invalid signature index", e.toString())); | 85 .catch(e => assertContains("invalid signature index", e.toString())); |
91 })(); | 86 })(); |
92 | 87 |
93 // Multiple instances tests. | 88 // Multiple instances tests. |
94 (function ManyInstances() { | 89 (function ManyInstances() { |
95 print("ManyInstances..."); | |
96 let compiled_module = new WebAssembly.Module(buffer); | 90 let compiled_module = new WebAssembly.Module(buffer); |
97 let instance_1 = new WebAssembly.Instance(compiled_module); | 91 let instance_1 = new WebAssembly.Instance(compiled_module); |
98 let instance_2 = new WebAssembly.Instance(compiled_module); | 92 let instance_2 = new WebAssembly.Instance(compiled_module); |
99 assertTrue(instance_1 != instance_2); | 93 assertTrue(instance_1 != instance_2); |
100 })(); | 94 })(); |
101 | 95 |
102 (function ManyInstancesAsync() { | 96 (function ManyInstancesAsync() { |
103 print("ManyInstancesAsync..."); | |
104 let promise = WebAssembly.compile(buffer); | 97 let promise = WebAssembly.compile(buffer); |
105 promise.then(compiled_module => { | 98 promise.then(compiled_module => { |
106 let instance_1 = new WebAssembly.Instance(compiled_module); | 99 let instance_1 = new WebAssembly.Instance(compiled_module); |
107 let instance_2 = new WebAssembly.Instance(compiled_module); | 100 let instance_2 = new WebAssembly.Instance(compiled_module); |
108 assertTrue(instance_1 != instance_2); | 101 assertTrue(instance_1 != instance_2); |
109 }); | 102 }); |
110 })(); | 103 })(); |
111 | 104 |
112 (function InstancesAreIsolatedFromEachother() { | 105 (function InstancesAreIsolatedFromEachother() { |
113 print("InstancesAreIsolatedFromEachother..."); | |
114 var builder = new WasmModuleBuilder(); | 106 var builder = new WasmModuleBuilder(); |
115 builder.addMemory(1,1, true); | 107 builder.addMemory(1,1, true); |
116 var kSig_v_i = makeSig([kAstI32], []); | 108 var kSig_v_i = makeSig([kAstI32], []); |
117 var signature = builder.addType(kSig_v_i); | 109 var signature = builder.addType(kSig_v_i); |
118 builder.addImport("some_value", kSig_i); | 110 builder.addImport("some_value", kSig_i); |
119 builder.addImport("writer", signature); | 111 builder.addImport("writer", signature); |
120 | 112 |
121 builder.addFunction("main", kSig_i_i) | 113 builder.addFunction("main", kSig_i_i) |
122 .addBody([ | 114 .addBody([ |
| 115 kExprI32Const, 1, |
123 kExprGetLocal, 0, | 116 kExprGetLocal, 0, |
124 kExprI32LoadMem, 0, 0, | 117 kExprI32LoadMem, 0, 0, |
125 kExprI32Const, 1, | 118 kExprCallIndirect, kArity1, signature, |
126 kExprCallIndirect, signature, | |
127 kExprGetLocal,0, | 119 kExprGetLocal,0, |
128 kExprI32LoadMem,0, 0, | 120 kExprI32LoadMem,0, 0, |
129 kExprCallFunction, 0, | 121 kExprCallImport, kArity0, 0, |
130 kExprI32Add | 122 kExprI32Add |
131 ]).exportFunc(); | 123 ]).exportFunc(); |
132 | 124 |
133 // writer(mem[i]); | 125 // writer(mem[i]); |
134 // return mem[i] + some_value(); | 126 // return mem[i] + some_value(); |
135 builder.addFunction("_wrap_writer", signature) | 127 builder.addFunction("_wrap_writer", signature) |
136 .addBody([ | 128 .addBody([ |
137 kExprGetLocal, 0, | 129 kExprGetLocal, 0, |
138 kExprCallFunction, 1]); | 130 kExprCallImport, kArity1, 1]); |
139 builder.appendToTable([2, 3]); | 131 builder.appendToTable([0, 1]); |
140 | 132 |
141 | 133 |
142 var module = new WebAssembly.Module(builder.toBuffer()); | 134 var module = new WebAssembly.Module(builder.toBuffer()); |
143 var mem_1 = new ArrayBuffer(4); | 135 var mem_1 = new ArrayBuffer(4); |
144 var mem_2 = new ArrayBuffer(4); | 136 var mem_2 = new ArrayBuffer(4); |
145 var view_1 = new Int32Array(mem_1); | 137 var view_1 = new Int32Array(mem_1); |
146 var view_2 = new Int32Array(mem_2); | 138 var view_2 = new Int32Array(mem_2); |
147 | 139 |
148 view_1[0] = 42; | 140 view_1[0] = 42; |
149 view_2[0] = 1000; | 141 view_2[0] = 1000; |
150 | 142 |
151 var outval_1; | 143 var outval_1; |
152 var outval_2; | 144 var outval_2; |
153 var i1 = new WebAssembly.Instance(module, {some_value: () => 1, | 145 var i1 = new WebAssembly.Instance(module, {some_value: () => 1, |
154 writer: (x)=>outval_1 = x }, mem_1); | 146 writer: (x)=>outval_1 = x }, mem_1); |
155 var i2 = new WebAssembly.Instance(module, {some_value: () => 2, | 147 var i2 = new WebAssembly.Instance(module, {some_value: () => 2, |
156 writer: (x)=>outval_2 = x }, mem_2); | 148 writer: (x)=>outval_2 = x }, mem_2); |
157 | 149 |
158 assertEquals(43, i1.exports.main(0)); | 150 assertEquals(43, i1.exports.main(0)); |
159 assertEquals(1002, i2.exports.main(0)); | 151 assertEquals(1002, i2.exports.main(0)); |
160 | 152 |
161 assertEquals(42, outval_1); | 153 assertEquals(42, outval_1); |
162 assertEquals(1000, outval_2); | 154 assertEquals(1000, outval_2); |
163 })(); | 155 })(); |
164 | 156 |
165 (function GlobalsArePrivateToTheInstance() { | 157 (function GlobalsArePrivateToTheInstance() { |
166 print("GlobalsArePrivateToTheInstance..."); | |
167 var builder = new WasmModuleBuilder(); | 158 var builder = new WasmModuleBuilder(); |
168 builder.addGlobal(kAstI32); | 159 builder.addGlobal(kAstI32); |
169 builder.addFunction("read", kSig_i_v) | 160 builder.addFunction("read", kSig_i_v) |
170 .addBody([ | 161 .addBody([ |
171 kExprGetGlobal, 0]) | 162 kExprGetGlobal, 0]) |
172 .exportFunc(); | 163 .exportFunc(); |
173 | 164 |
174 builder.addFunction("write", kSig_v_i) | 165 builder.addFunction("write", kSig_v_i) |
175 .addBody([ | 166 .addBody([ |
176 kExprGetLocal, 0, | 167 kExprGetLocal, 0, |
177 kExprSetGlobal, 0]) | 168 kExprSetGlobal, 0]) |
178 .exportFunc(); | 169 .exportFunc(); |
179 | 170 |
180 var module = new WebAssembly.Module(builder.toBuffer()); | 171 var module = new WebAssembly.Module(builder.toBuffer()); |
181 var i1 = new WebAssembly.Instance(module); | 172 var i1 = new WebAssembly.Instance(module); |
182 var i2 = new WebAssembly.Instance(module); | 173 var i2 = new WebAssembly.Instance(module); |
183 i1.exports.write(1); | 174 i1.exports.write(1); |
184 i2.exports.write(2); | 175 i2.exports.write(2); |
185 assertEquals(1, i1.exports.read()); | 176 assertEquals(1, i1.exports.read()); |
186 assertEquals(2, i2.exports.read()); | 177 assertEquals(2, i2.exports.read()); |
187 })(); | 178 })(); |
188 | 179 |
189 | 180 |
190 (function InstanceMemoryIsIsolated() { | 181 (function InstanceMemoryIsIsolated() { |
191 print("InstanceMemoryIsIsolated..."); | |
192 var builder = new WasmModuleBuilder(); | 182 var builder = new WasmModuleBuilder(); |
193 builder.addMemory(1,1, true); | 183 builder.addMemory(1,1, true); |
194 | 184 |
195 builder.addFunction("f", kSig_i) | 185 builder.addFunction("f", kSig_i) |
196 .addBody([ | 186 .addBody([ |
197 kExprI32Const, 0, | 187 kExprI32Const, 0, |
198 kExprI32LoadMem, 0, 0 | 188 kExprI32LoadMem, 0, 0 |
199 ]).exportFunc(); | 189 ]).exportFunc(); |
200 | 190 |
201 var mem_1 = new ArrayBuffer(65536); | 191 var mem_1 = new ArrayBuffer(65536); |
202 var mem_2 = new ArrayBuffer(65536); | 192 var mem_2 = new ArrayBuffer(65536); |
203 var view_1 = new Int32Array(mem_1); | 193 var view_1 = new Int32Array(mem_1); |
204 var view_2 = new Int32Array(mem_2); | 194 var view_2 = new Int32Array(mem_2); |
205 view_1[0] = 1; | 195 view_1[0] = 1; |
206 view_2[0] = 1000; | 196 view_2[0] = 1000; |
207 | 197 |
208 var module = new WebAssembly.Module(builder.toBuffer()); | 198 var module = new WebAssembly.Module(builder.toBuffer()); |
209 var i1 = new WebAssembly.Instance(module, null, mem_1); | 199 var i1 = new WebAssembly.Instance(module, null, mem_1); |
210 var i2 = new WebAssembly.Instance(module, null, mem_2); | 200 var i2 = new WebAssembly.Instance(module, null, mem_2); |
211 | 201 |
212 assertEquals(1, i1.exports.f()); | 202 assertEquals(1, i1.exports.f()); |
213 assertEquals(1000, i2.exports.f()); | 203 assertEquals(1000, i2.exports.f()); |
214 })(); | 204 })(); |
OLD | NEW |