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 function EmptyTest() { | 7 function EmptyTest() { |
8 "use asm"; | 8 "use asm"; |
9 function caller() { | 9 function caller() { |
10 empty(); | 10 empty(); |
11 return 11; | 11 return 11; |
12 } | 12 } |
13 function empty() { | 13 function empty() { |
14 } | 14 } |
15 return {caller: caller}; | 15 return {caller: caller}; |
16 } | 16 } |
17 | 17 |
18 assertEquals(11, _WASMEXP_.asmCompileRun(EmptyTest.toString())); | 18 assertEquals(11, _WASMEXP_.instantiateModuleFromAsm( |
| 19 EmptyTest.toString()).caller()); |
19 | 20 |
20 | 21 |
21 function IntTest() { | 22 function IntTest() { |
22 "use asm"; | 23 "use asm"; |
23 function sum(a, b) { | 24 function sum(a, b) { |
24 a = a|0; | 25 a = a|0; |
25 b = b|0; | 26 b = b|0; |
26 var c = (b + 1)|0 | 27 var c = (b + 1)|0 |
27 var d = 3.0; | 28 var d = 3.0; |
28 var e = d | 0; // double conversion | 29 var e = d | 0; // double conversion |
29 return (a + c + 1)|0; | 30 return (a + c + 1)|0; |
30 } | 31 } |
31 | 32 |
32 function caller() { | 33 function caller() { |
33 return sum(77,22) | 0; | 34 return sum(77,22) | 0; |
34 } | 35 } |
35 | 36 |
36 return {caller: caller}; | 37 return {caller: caller}; |
37 } | 38 } |
38 | 39 |
39 assertEquals(101, _WASMEXP_.asmCompileRun(IntTest.toString())); | 40 assertEquals(101, _WASMEXP_.instantiateModuleFromAsm( |
| 41 IntTest.toString()).caller()); |
40 | 42 |
41 | 43 |
42 function Float64Test() { | 44 function Float64Test() { |
43 "use asm"; | 45 "use asm"; |
44 function sum(a, b) { | 46 function sum(a, b) { |
45 a = +a; | 47 a = +a; |
46 b = +b; | 48 b = +b; |
47 return +(a + b); | 49 return +(a + b); |
48 } | 50 } |
49 | 51 |
50 function caller() { | 52 function caller() { |
51 var a = +sum(70.1,10.2); | 53 var a = +sum(70.1,10.2); |
52 var ret = 0|0; | 54 var ret = 0|0; |
53 if (a == 80.3) { | 55 if (a == 80.3) { |
54 ret = 1|0; | 56 ret = 1|0; |
55 } else { | 57 } else { |
56 ret = 0|0; | 58 ret = 0|0; |
57 } | 59 } |
58 return ret|0; | 60 return ret|0; |
59 } | 61 } |
60 | 62 |
61 return {caller: caller}; | 63 return {caller: caller}; |
62 } | 64 } |
63 | 65 |
64 assertEquals(1, _WASMEXP_.asmCompileRun(Float64Test.toString())); | 66 assertEquals(1, _WASMEXP_.instantiateModuleFromAsm( |
| 67 Float64Test.toString()).caller()); |
65 | 68 |
66 | 69 |
67 function BadModule() { | 70 function BadModule() { |
68 "use asm"; | 71 "use asm"; |
69 function caller(a, b) { | 72 function caller(a, b) { |
70 a = a|0; | 73 a = a|0; |
71 b = b+0; | 74 b = b+0; |
72 var c = (b + 1)|0 | 75 var c = (b + 1)|0 |
73 return (a + c + 1)|0; | 76 return (a + c + 1)|0; |
74 } | 77 } |
75 | 78 |
76 function caller() { | 79 function caller() { |
77 return call(1, 2)|0; | 80 return call(1, 2)|0; |
78 } | 81 } |
79 | 82 |
80 return {caller: caller}; | 83 return {caller: caller}; |
81 } | 84 } |
82 | 85 |
83 assertThrows(function() { | 86 assertThrows(function() { |
84 _WASMEXP_.asmCompileRun(BadModule.toString()) | 87 _WASMEXP_.instantiateModuleFromAsm(BadModule.toString()).caller(); |
85 }); | 88 }); |
86 | 89 |
87 | 90 |
88 function TestReturnInBlock() { | 91 function TestReturnInBlock() { |
89 "use asm"; | 92 "use asm"; |
90 | 93 |
91 function caller() { | 94 function caller() { |
92 if(1) { | 95 if(1) { |
93 { | 96 { |
94 { | 97 { |
95 return 1; | 98 return 1; |
96 } | 99 } |
97 } | 100 } |
98 } | 101 } |
99 return 0; | 102 return 0; |
100 } | 103 } |
101 | 104 |
102 return {caller: caller}; | 105 return {caller: caller}; |
103 } | 106 } |
104 | 107 |
105 assertEquals(1, _WASMEXP_.asmCompileRun(TestReturnInBlock.toString())); | 108 assertEquals(1, _WASMEXP_.instantiateModuleFromAsm( |
| 109 TestReturnInBlock.toString()).caller()); |
106 | 110 |
107 | 111 |
108 function TestWhileSimple() { | 112 function TestWhileSimple() { |
109 "use asm"; | 113 "use asm"; |
110 | 114 |
111 function caller() { | 115 function caller() { |
112 var x = 0; | 116 var x = 0; |
113 while(x < 5) { | 117 while(x < 5) { |
114 x = (x + 1)|0; | 118 x = (x + 1)|0; |
115 } | 119 } |
116 return x|0; | 120 return x|0; |
117 } | 121 } |
118 | 122 |
119 return {caller: caller}; | 123 return {caller: caller}; |
120 } | 124 } |
121 | 125 |
122 assertEquals(5, _WASMEXP_.asmCompileRun(TestWhileSimple.toString())); | 126 assertEquals(5, _WASMEXP_.instantiateModuleFromAsm( |
| 127 TestWhileSimple.toString()).caller()); |
123 | 128 |
124 | 129 |
125 function TestWhileWithoutBraces() { | 130 function TestWhileWithoutBraces() { |
126 "use asm"; | 131 "use asm"; |
127 | 132 |
128 function caller() { | 133 function caller() { |
129 var x = 0; | 134 var x = 0; |
130 while(x <= 3) | 135 while(x <= 3) |
131 x = (x + 1)|0; | 136 x = (x + 1)|0; |
132 return x|0; | 137 return x|0; |
133 } | 138 } |
134 | 139 |
135 return {caller: caller}; | 140 return {caller: caller}; |
136 } | 141 } |
137 | 142 |
138 assertEquals(4, _WASMEXP_.asmCompileRun(TestWhileWithoutBraces.toString())); | 143 assertEquals(4, _WASMEXP_.instantiateModuleFromAsm( |
| 144 TestWhileWithoutBraces.toString()).caller()); |
139 | 145 |
140 | 146 |
141 function TestReturnInWhile() { | 147 function TestReturnInWhile() { |
142 "use asm"; | 148 "use asm"; |
143 | 149 |
144 function caller() { | 150 function caller() { |
145 var x = 0; | 151 var x = 0; |
146 while(x < 10) { | 152 while(x < 10) { |
147 x = (x + 6)|0; | 153 x = (x + 6)|0; |
148 return x|0; | 154 return x|0; |
149 } | 155 } |
150 return x|0; | 156 return x|0; |
151 } | 157 } |
152 | 158 |
153 return {caller: caller}; | 159 return {caller: caller}; |
154 } | 160 } |
155 | 161 |
156 assertEquals(6, _WASMEXP_.asmCompileRun(TestReturnInWhile.toString())); | 162 assertEquals(6, _WASMEXP_.instantiateModuleFromAsm( |
| 163 TestReturnInWhile.toString()).caller()); |
157 | 164 |
158 | 165 |
159 function TestReturnInWhileWithoutBraces() { | 166 function TestReturnInWhileWithoutBraces() { |
160 "use asm"; | 167 "use asm"; |
161 | 168 |
162 function caller() { | 169 function caller() { |
163 var x = 0; | 170 var x = 0; |
164 while(x < 5) | 171 while(x < 5) |
165 return 7; | 172 return 7; |
166 return x|0; | 173 return x|0; |
167 } | 174 } |
168 | 175 |
169 return {caller: caller}; | 176 return {caller: caller}; |
170 } | 177 } |
171 | 178 |
172 assertEquals( | 179 assertEquals( |
173 7, _WASMEXP_.asmCompileRun(TestReturnInWhileWithoutBraces.toString())); | 180 7, _WASMEXP_.instantiateModuleFromAsm( |
| 181 TestReturnInWhileWithoutBraces.toString()).caller()); |
174 | 182 |
175 | 183 |
176 function TestBreakInWhile() { | 184 function TestBreakInWhile() { |
177 "use asm"; | 185 "use asm"; |
178 | 186 |
179 function caller() { | 187 function caller() { |
180 while(1) { | 188 while(1) { |
181 break; | 189 break; |
182 } | 190 } |
183 return 8; | 191 return 8; |
184 } | 192 } |
185 | 193 |
186 return {caller: caller}; | 194 return {caller: caller}; |
187 } | 195 } |
188 | 196 |
189 assertEquals(8, _WASMEXP_.asmCompileRun(TestBreakInWhile.toString())); | 197 assertEquals(8, _WASMEXP_.instantiateModuleFromAsm( |
| 198 TestBreakInWhile.toString()).caller()); |
190 | 199 |
191 | 200 |
192 function TestBreakInNestedWhile() { | 201 function TestBreakInNestedWhile() { |
193 "use asm"; | 202 "use asm"; |
194 | 203 |
195 function caller() { | 204 function caller() { |
196 var x = 1.0; | 205 var x = 1.0; |
197 while(x < 1.5) { | 206 while(x < 1.5) { |
198 while(1) | 207 while(1) |
199 break; | 208 break; |
200 x = +(x + 0.25); | 209 x = +(x + 0.25); |
201 } | 210 } |
202 var ret = 0; | 211 var ret = 0; |
203 if (x == 1.5) { | 212 if (x == 1.5) { |
204 ret = 9; | 213 ret = 9; |
205 } | 214 } |
206 return ret|0; | 215 return ret|0; |
207 } | 216 } |
208 | 217 |
209 return {caller: caller}; | 218 return {caller: caller}; |
210 } | 219 } |
211 | 220 |
212 assertEquals(9, _WASMEXP_.asmCompileRun(TestBreakInNestedWhile.toString())); | 221 assertEquals(9, _WASMEXP_.instantiateModuleFromAsm( |
| 222 TestBreakInNestedWhile.toString()).caller()); |
213 | 223 |
214 | 224 |
215 function TestBreakInBlock() { | 225 function TestBreakInBlock() { |
216 "use asm"; | 226 "use asm"; |
217 | 227 |
218 function caller() { | 228 function caller() { |
219 var x = 0; | 229 var x = 0; |
220 abc: { | 230 abc: { |
221 x = 10; | 231 x = 10; |
222 if (x == 10) { | 232 if (x == 10) { |
223 break abc; | 233 break abc; |
224 } | 234 } |
225 x = 20; | 235 x = 20; |
226 } | 236 } |
227 return x|0; | 237 return x|0; |
228 } | 238 } |
229 | 239 |
230 return {caller: caller}; | 240 return {caller: caller}; |
231 } | 241 } |
232 | 242 |
233 assertEquals(10, _WASMEXP_.asmCompileRun(TestBreakInBlock.toString())); | 243 assertEquals(10, _WASMEXP_.instantiateModuleFromAsm( |
| 244 TestBreakInBlock.toString()).caller()); |
234 | 245 |
235 | 246 |
236 function TestBreakInNamedWhile() { | 247 function TestBreakInNamedWhile() { |
237 "use asm"; | 248 "use asm"; |
238 | 249 |
239 function caller() { | 250 function caller() { |
240 var x = 0; | 251 var x = 0; |
241 outer: while (1) { | 252 outer: while (1) { |
242 x = (x + 1)|0; | 253 x = (x + 1)|0; |
243 while (x == 11) { | 254 while (x == 11) { |
244 break outer; | 255 break outer; |
245 } | 256 } |
246 } | 257 } |
247 return x|0; | 258 return x|0; |
248 } | 259 } |
249 | 260 |
250 return {caller: caller}; | 261 return {caller: caller}; |
251 } | 262 } |
252 | 263 |
253 assertEquals(11, _WASMEXP_.asmCompileRun(TestBreakInNamedWhile.toString())); | 264 assertEquals(11, _WASMEXP_.instantiateModuleFromAsm( |
| 265 TestBreakInNamedWhile.toString()).caller()); |
254 | 266 |
255 | 267 |
256 function TestContinue() { | 268 function TestContinue() { |
257 "use asm"; | 269 "use asm"; |
258 | 270 |
259 function caller() { | 271 function caller() { |
260 var x = 5; | 272 var x = 5; |
261 var ret = 0; | 273 var ret = 0; |
262 while (x >= 0) { | 274 while (x >= 0) { |
263 x = (x - 1)|0; | 275 x = (x - 1)|0; |
264 if (x == 2) { | 276 if (x == 2) { |
265 continue; | 277 continue; |
266 } | 278 } |
267 ret = (ret - 1)|0; | 279 ret = (ret - 1)|0; |
268 } | 280 } |
269 return ret|0; | 281 return ret|0; |
270 } | 282 } |
271 | 283 |
272 return {caller: caller}; | 284 return {caller: caller}; |
273 } | 285 } |
274 | 286 |
275 assertEquals(-5, _WASMEXP_.asmCompileRun(TestContinue.toString())); | 287 assertEquals(-5, _WASMEXP_.instantiateModuleFromAsm( |
| 288 TestContinue.toString()).caller()); |
276 | 289 |
277 | 290 |
278 function TestContinueInNamedWhile() { | 291 function TestContinueInNamedWhile() { |
279 "use asm"; | 292 "use asm"; |
280 | 293 |
281 function caller() { | 294 function caller() { |
282 var x = 5; | 295 var x = 5; |
283 var y = 0; | 296 var y = 0; |
284 var ret = 0; | 297 var ret = 0; |
285 outer: while (x > 0) { | 298 outer: while (x > 0) { |
286 x = (x - 1)|0; | 299 x = (x - 1)|0; |
287 y = 0; | 300 y = 0; |
288 while (y < 5) { | 301 while (y < 5) { |
289 if (x == 3) { | 302 if (x == 3) { |
290 continue outer; | 303 continue outer; |
291 } | 304 } |
292 ret = (ret + 1)|0; | 305 ret = (ret + 1)|0; |
293 y = (y + 1)|0; | 306 y = (y + 1)|0; |
294 } | 307 } |
295 } | 308 } |
296 return ret|0; | 309 return ret|0; |
297 } | 310 } |
298 | 311 |
299 return {caller: caller}; | 312 return {caller: caller}; |
300 } | 313 } |
301 | 314 |
302 assertEquals(20, _WASMEXP_.asmCompileRun(TestContinueInNamedWhile.toString())); | 315 assertEquals(20, _WASMEXP_.instantiateModuleFromAsm( |
| 316 TestContinueInNamedWhile.toString()).caller()); |
303 | 317 |
304 | 318 |
305 function TestNot() { | 319 function TestNot() { |
306 "use asm"; | 320 "use asm"; |
307 | 321 |
308 function caller() { | 322 function caller() { |
309 var a = !(2 > 3); | 323 var a = !(2 > 3); |
310 return a | 0; | 324 return a | 0; |
311 } | 325 } |
312 | 326 |
313 return {caller:caller}; | 327 return {caller:caller}; |
314 } | 328 } |
315 | 329 |
316 assertEquals(1, _WASMEXP_.asmCompileRun(TestNot.toString())); | 330 assertEquals(1, _WASMEXP_.instantiateModuleFromAsm( |
| 331 TestNot.toString()).caller()); |
317 | 332 |
318 | 333 |
319 function TestNotEquals() { | 334 function TestNotEquals() { |
320 "use asm"; | 335 "use asm"; |
321 | 336 |
322 function caller() { | 337 function caller() { |
323 var a = 3; | 338 var a = 3; |
324 if (a != 2) { | 339 if (a != 2) { |
325 return 21; | 340 return 21; |
326 } | 341 } |
327 return 0; | 342 return 0; |
328 } | 343 } |
329 | 344 |
330 return {caller:caller}; | 345 return {caller:caller}; |
331 } | 346 } |
332 | 347 |
333 assertEquals(21, _WASMEXP_.asmCompileRun(TestNotEquals.toString())); | 348 assertEquals(21, _WASMEXP_.instantiateModuleFromAsm( |
| 349 TestNotEquals.toString()).caller()); |
334 | 350 |
335 | 351 |
336 function TestUnsignedComparison() { | 352 function TestUnsignedComparison() { |
337 "use asm"; | 353 "use asm"; |
338 | 354 |
339 function caller() { | 355 function caller() { |
340 var a = 0xffffffff; | 356 var a = 0xffffffff; |
341 if ((a>>>0) > (0>>>0)) { | 357 if ((a>>>0) > (0>>>0)) { |
342 return 22; | 358 return 22; |
343 } | 359 } |
344 return 0; | 360 return 0; |
345 } | 361 } |
346 | 362 |
347 return {caller:caller}; | 363 return {caller:caller}; |
348 } | 364 } |
349 | 365 |
350 assertEquals(22, _WASMEXP_.asmCompileRun(TestUnsignedComparison.toString())); | 366 assertEquals(22, _WASMEXP_.instantiateModuleFromAsm( |
| 367 TestUnsignedComparison.toString()).caller()); |
351 | 368 |
352 | 369 |
353 function TestMixedAdd() { | 370 function TestMixedAdd() { |
354 "use asm"; | 371 "use asm"; |
355 | 372 |
356 function caller() { | 373 function caller() { |
357 var a = 0x80000000; | 374 var a = 0x80000000; |
358 var b = 0x7fffffff; | 375 var b = 0x7fffffff; |
359 var c = 0; | 376 var c = 0; |
360 c = ((a>>>0) + b)|0; | 377 c = ((a>>>0) + b)|0; |
361 if ((c >>> 0) > (0>>>0)) { | 378 if ((c >>> 0) > (0>>>0)) { |
362 if (c < 0) { | 379 if (c < 0) { |
363 return 23; | 380 return 23; |
364 } | 381 } |
365 } | 382 } |
366 return 0; | 383 return 0; |
367 } | 384 } |
368 | 385 |
369 return {caller:caller}; | 386 return {caller:caller}; |
370 } | 387 } |
371 | 388 |
372 assertEquals(23, _WASMEXP_.asmCompileRun(TestMixedAdd.toString())); | 389 assertEquals(23, _WASMEXP_.instantiateModuleFromAsm( |
| 390 TestMixedAdd.toString()).caller()); |
373 | 391 |
374 | 392 |
375 function TestInt32HeapAccess(stdlib, foreign, buffer) { | 393 function TestInt32HeapAccess(stdlib, foreign, buffer) { |
376 "use asm"; | 394 "use asm"; |
377 | 395 |
378 var m = new stdlib.Int32Array(buffer); | 396 var m = new stdlib.Int32Array(buffer); |
379 function caller() { | 397 function caller() { |
380 var i = 4; | 398 var i = 4; |
381 | 399 |
382 m[0] = (i + 1) | 0; | 400 m[0] = (i + 1) | 0; |
383 m[i >> 2] = ((m[0]|0) + 1) | 0; | 401 m[i >> 2] = ((m[0]|0) + 1) | 0; |
384 m[2] = ((m[i >> 2]|0) + 1) | 0; | 402 m[2] = ((m[i >> 2]|0) + 1) | 0; |
385 return m[2] | 0; | 403 return m[2] | 0; |
386 } | 404 } |
387 | 405 |
388 return {caller: caller}; | 406 return {caller: caller}; |
389 } | 407 } |
390 | 408 |
391 assertEquals(7, _WASMEXP_.asmCompileRun(TestInt32HeapAccess.toString())); | 409 assertEquals(7, _WASMEXP_.instantiateModuleFromAsm( |
| 410 TestInt32HeapAccess.toString()).caller()); |
392 | 411 |
393 | 412 |
394 function TestInt32HeapAccessExternal() { | 413 function TestInt32HeapAccessExternal() { |
395 var memory = new ArrayBuffer(1024); | 414 var memory = new ArrayBuffer(1024); |
396 var memory_int32 = new Int32Array(memory); | 415 var memory_int32 = new Int32Array(memory); |
397 var module = _WASMEXP_.instantiateModuleFromAsm( | 416 var module = _WASMEXP_.instantiateModuleFromAsm( |
398 TestInt32HeapAccess.toString(), null, memory); | 417 TestInt32HeapAccess.toString(), null, memory); |
399 module.__init__(); | 418 module.__init__(); |
400 assertEquals(7, module.caller()); | 419 assertEquals(7, module.caller()); |
401 assertEquals(7, memory_int32[2]); | 420 assertEquals(7, memory_int32[2]); |
(...skipping 14 matching lines...) Expand all Loading... |
416 for (var i = 0; i < types.length; i++) { | 435 for (var i = 0; i < types.length; i++) { |
417 var code = TestInt32HeapAccess.toString(); | 436 var code = TestInt32HeapAccess.toString(); |
418 code = code.replace('Int32Array', types[i][1]); | 437 code = code.replace('Int32Array', types[i][1]); |
419 code = code.replace(/>> 2/g, types[i][2]); | 438 code = code.replace(/>> 2/g, types[i][2]); |
420 var memory = new ArrayBuffer(1024); | 439 var memory = new ArrayBuffer(1024); |
421 var memory_view = new types[i][0](memory); | 440 var memory_view = new types[i][0](memory); |
422 var module = _WASMEXP_.instantiateModuleFromAsm(code, null, memory); | 441 var module = _WASMEXP_.instantiateModuleFromAsm(code, null, memory); |
423 module.__init__(); | 442 module.__init__(); |
424 assertEquals(7, module.caller()); | 443 assertEquals(7, module.caller()); |
425 assertEquals(7, memory_view[2]); | 444 assertEquals(7, memory_view[2]); |
426 assertEquals(7, _WASMEXP_.asmCompileRun(code)); | 445 assertEquals(7, _WASMEXP_.instantiateModuleFromAsm(code)); |
427 } | 446 } |
428 } | 447 } |
429 | 448 |
430 TestHeapAccessIntTypes(); | 449 TestHeapAccessIntTypes(); |
431 | 450 |
432 | 451 |
433 function TestFloatHeapAccess(stdlib, foreign, buffer) { | 452 function TestFloatHeapAccess(stdlib, foreign, buffer) { |
434 "use asm"; | 453 "use asm"; |
435 | 454 |
436 var f32 = new stdlib.Float32Array(buffer); | 455 var f32 = new stdlib.Float32Array(buffer); |
437 var f64 = new stdlib.Float64Array(buffer); | 456 var f64 = new stdlib.Float64Array(buffer); |
438 var fround = stdlib.Math.fround; | 457 var fround = stdlib.Math.fround; |
439 function caller() { | 458 function caller() { |
440 var i = 8; | 459 var i = 8; |
441 var j = 8; | 460 var j = 8; |
442 var v = 6.0; | 461 var v = 6.0; |
443 | 462 |
444 // TODO(bradnelson): Add float32 when asm-wasm supports it. | 463 // TODO(bradnelson): Add float32 when asm-wasm supports it. |
445 f64[2] = v + 1.0; | 464 f64[2] = v + 1.0; |
446 f64[i >> 3] = +f64[2] + 1.0; | 465 f64[i >> 3] = +f64[2] + 1.0; |
447 f64[j >> 3] = +f64[j >> 3] + 1.0; | 466 f64[j >> 3] = +f64[j >> 3] + 1.0; |
448 i = +f64[i >> 3] == 9.0; | 467 i = +f64[i >> 3] == 9.0; |
449 return i|0; | 468 return i|0; |
450 } | 469 } |
451 | 470 |
452 return {caller: caller}; | 471 return {caller: caller}; |
453 } | 472 } |
454 | 473 |
455 assertEquals(1, _WASMEXP_.asmCompileRun(TestFloatHeapAccess.toString())); | 474 assertEquals(1, _WASMEXP_.instantiateModuleFromAsm( |
| 475 TestFloatHeapAccess.toString()).caller()); |
456 | 476 |
457 | 477 |
458 function TestFloatHeapAccessExternal() { | 478 function TestFloatHeapAccessExternal() { |
459 var memory = new ArrayBuffer(1024); | 479 var memory = new ArrayBuffer(1024); |
460 var memory_float64 = new Float64Array(memory); | 480 var memory_float64 = new Float64Array(memory); |
461 var module = _WASMEXP_.instantiateModuleFromAsm( | 481 var module = _WASMEXP_.instantiateModuleFromAsm( |
462 TestFloatHeapAccess.toString(), null, memory); | 482 TestFloatHeapAccess.toString(), null, memory); |
463 module.__init__(); | 483 module.__init__(); |
464 assertEquals(1, module.caller()); | 484 assertEquals(1, module.caller()); |
465 assertEquals(9.0, memory_float64[1]); | 485 assertEquals(9.0, memory_float64[1]); |
466 } | 486 } |
467 | 487 |
468 TestFloatHeapAccessExternal(); | 488 TestFloatHeapAccessExternal(); |
469 | 489 |
470 | 490 |
471 function TestConvertI32() { | 491 function TestConvertI32() { |
472 "use asm"; | 492 "use asm"; |
473 | 493 |
474 function caller() { | 494 function caller() { |
475 var a = 1.5; | 495 var a = 1.5; |
476 if ((~~(a + a)) == 3) { | 496 if ((~~(a + a)) == 3) { |
477 return 24; | 497 return 24; |
478 } | 498 } |
479 return 0; | 499 return 0; |
480 } | 500 } |
481 | 501 |
482 return {caller:caller}; | 502 return {caller:caller}; |
483 } | 503 } |
484 | 504 |
485 assertEquals(24, _WASMEXP_.asmCompileRun(TestConvertI32.toString())); | 505 assertEquals(24, _WASMEXP_.instantiateModuleFromAsm( |
| 506 TestConvertI32.toString()).caller()); |
486 | 507 |
487 | 508 |
488 function TestConvertF64FromInt() { | 509 function TestConvertF64FromInt() { |
489 "use asm"; | 510 "use asm"; |
490 | 511 |
491 function caller() { | 512 function caller() { |
492 var a = 1; | 513 var a = 1; |
493 if ((+(a + a)) > 1.5) { | 514 if ((+(a + a)) > 1.5) { |
494 return 25; | 515 return 25; |
495 } | 516 } |
496 return 0; | 517 return 0; |
497 } | 518 } |
498 | 519 |
499 return {caller:caller}; | 520 return {caller:caller}; |
500 } | 521 } |
501 | 522 |
502 assertEquals(25, _WASMEXP_.asmCompileRun(TestConvertF64FromInt.toString())); | 523 assertEquals(25, _WASMEXP_.instantiateModuleFromAsm( |
| 524 TestConvertF64FromInt.toString()).caller()); |
503 | 525 |
504 | 526 |
505 function TestConvertF64FromUnsigned() { | 527 function TestConvertF64FromUnsigned() { |
506 "use asm"; | 528 "use asm"; |
507 | 529 |
508 function caller() { | 530 function caller() { |
509 var a = 0xffffffff; | 531 var a = 0xffffffff; |
510 if ((+(a>>>0)) > 0.0) { | 532 if ((+(a>>>0)) > 0.0) { |
511 if((+a) < 0.0) { | 533 if((+a) < 0.0) { |
512 return 26; | 534 return 26; |
513 } | 535 } |
514 } | 536 } |
515 return 0; | 537 return 0; |
516 } | 538 } |
517 | 539 |
518 return {caller:caller}; | 540 return {caller:caller}; |
519 } | 541 } |
520 | 542 |
521 assertEquals(26, _WASMEXP_.asmCompileRun(TestConvertF64FromUnsigned.toString()))
; | 543 assertEquals(26, _WASMEXP_.instantiateModuleFromAsm( |
| 544 TestConvertF64FromUnsigned.toString()).caller()); |
522 | 545 |
523 | 546 |
524 function TestModInt() { | 547 function TestModInt() { |
525 "use asm"; | 548 "use asm"; |
526 | 549 |
527 function caller() { | 550 function caller() { |
528 var a = -83; | 551 var a = -83; |
529 var b = 28; | 552 var b = 28; |
530 return ((a|0)%(b|0))|0; | 553 return ((a|0)%(b|0))|0; |
531 } | 554 } |
532 | 555 |
533 return {caller:caller}; | 556 return {caller:caller}; |
534 } | 557 } |
535 | 558 |
536 assertEquals(-27, _WASMEXP_.asmCompileRun(TestModInt.toString())); | 559 assertEquals(-27, _WASMEXP_.instantiateModuleFromAsm( |
| 560 TestModInt.toString()).caller()); |
537 | 561 |
538 | 562 |
539 function TestModUnsignedInt() { | 563 function TestModUnsignedInt() { |
540 "use asm"; | 564 "use asm"; |
541 | 565 |
542 function caller() { | 566 function caller() { |
543 var a = 0x80000000; //2147483648 | 567 var a = 0x80000000; //2147483648 |
544 var b = 10; | 568 var b = 10; |
545 return ((a>>>0)%(b>>>0))|0; | 569 return ((a>>>0)%(b>>>0))|0; |
546 } | 570 } |
547 | 571 |
548 return {caller:caller}; | 572 return {caller:caller}; |
549 } | 573 } |
550 | 574 |
551 assertEquals(8, _WASMEXP_.asmCompileRun(TestModUnsignedInt.toString())); | 575 assertEquals(8, _WASMEXP_.instantiateModuleFromAsm( |
| 576 TestModUnsignedInt.toString()).caller()); |
552 | 577 |
553 | 578 |
554 function TestModDouble() { | 579 function TestModDouble() { |
555 "use asm"; | 580 "use asm"; |
556 | 581 |
557 function caller() { | 582 function caller() { |
558 var a = 5.25; | 583 var a = 5.25; |
559 var b = 2.5; | 584 var b = 2.5; |
560 if (a%b == 0.25) { | 585 if (a%b == 0.25) { |
561 return 28; | 586 return 28; |
562 } | 587 } |
563 return 0; | 588 return 0; |
564 } | 589 } |
565 | 590 |
566 return {caller:caller}; | 591 return {caller:caller}; |
567 } | 592 } |
568 | 593 |
569 assertEquals(28, _WASMEXP_.asmCompileRun(TestModDouble.toString())); | 594 assertEquals(28, _WASMEXP_.instantiateModuleFromAsm( |
| 595 TestModDouble.toString()).caller()); |
570 | 596 |
571 | 597 |
572 /* | 598 /* |
573 TODO: Fix parsing of negative doubles | 599 TODO: Fix parsing of negative doubles |
574 Fix code to use trunc instead of casts | 600 Fix code to use trunc instead of casts |
575 function TestModDoubleNegative() { | 601 function TestModDoubleNegative() { |
576 "use asm"; | 602 "use asm"; |
577 | 603 |
578 function caller() { | 604 function caller() { |
579 var a = -34359738368.25; | 605 var a = -34359738368.25; |
580 var b = 2.5; | 606 var b = 2.5; |
581 if (a%b == -0.75) { | 607 if (a%b == -0.75) { |
582 return 28; | 608 return 28; |
583 } | 609 } |
584 return 0; | 610 return 0; |
585 } | 611 } |
586 | 612 |
587 return {caller:caller}; | 613 return {caller:caller}; |
588 } | 614 } |
589 | 615 |
590 assertEquals(28, _WASMEXP_.asmCompileRun(TestModDoubleNegative.toString())); | 616 assertEquals(28, _WASMEXP_.instantiateModuleFromAsm( |
| 617 TestModDoubleNegative.toString()).caller()); |
591 */ | 618 */ |
592 | 619 |
593 | 620 |
594 function TestNamedFunctions() { | 621 function TestNamedFunctions() { |
595 "use asm"; | 622 "use asm"; |
596 | 623 |
597 var a = 0.0; | 624 var a = 0.0; |
598 var b = 0.0; | 625 var b = 0.0; |
599 | 626 |
600 function add() { | 627 function add() { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
641 var i = 0; | 668 var i = 0; |
642 for (i = 2; i <= 10; i = (i+1)|0) { | 669 for (i = 2; i <= 10; i = (i+1)|0) { |
643 ret = (ret + i) | 0; | 670 ret = (ret + i) | 0; |
644 } | 671 } |
645 return ret|0; | 672 return ret|0; |
646 } | 673 } |
647 | 674 |
648 return {caller:caller}; | 675 return {caller:caller}; |
649 } | 676 } |
650 | 677 |
651 assertEquals(54, _WASMEXP_.asmCompileRun(TestForLoop.toString())); | 678 assertEquals(54, _WASMEXP_.instantiateModuleFromAsm( |
| 679 TestForLoop.toString()).caller()); |
652 | 680 |
653 | 681 |
654 function TestForLoopWithoutInit() { | 682 function TestForLoopWithoutInit() { |
655 "use asm" | 683 "use asm" |
656 | 684 |
657 function caller() { | 685 function caller() { |
658 var ret = 0; | 686 var ret = 0; |
659 var i = 0; | 687 var i = 0; |
660 for (; i < 10; i = (i+1)|0) { | 688 for (; i < 10; i = (i+1)|0) { |
661 ret = (ret + 10) | 0; | 689 ret = (ret + 10) | 0; |
662 } | 690 } |
663 return ret|0; | 691 return ret|0; |
664 } | 692 } |
665 | 693 |
666 return {caller:caller}; | 694 return {caller:caller}; |
667 } | 695 } |
668 | 696 |
669 assertEquals(100, _WASMEXP_.asmCompileRun(TestForLoopWithoutInit.toString())); | 697 assertEquals(100, _WASMEXP_.instantiateModuleFromAsm( |
| 698 TestForLoopWithoutInit.toString()).caller()); |
670 | 699 |
671 | 700 |
672 function TestForLoopWithoutCondition() { | 701 function TestForLoopWithoutCondition() { |
673 "use asm" | 702 "use asm" |
674 | 703 |
675 function caller() { | 704 function caller() { |
676 var ret = 0; | 705 var ret = 0; |
677 var i = 0; | 706 var i = 0; |
678 for (i=1;; i = (i+1)|0) { | 707 for (i=1;; i = (i+1)|0) { |
679 ret = (ret + i) | 0; | 708 ret = (ret + i) | 0; |
680 if (i == 11) { | 709 if (i == 11) { |
681 break; | 710 break; |
682 } | 711 } |
683 } | 712 } |
684 return ret|0; | 713 return ret|0; |
685 } | 714 } |
686 | 715 |
687 return {caller:caller}; | 716 return {caller:caller}; |
688 } | 717 } |
689 | 718 |
690 assertEquals(66, _WASMEXP_.asmCompileRun(TestForLoopWithoutCondition.toString())
); | 719 assertEquals(66, _WASMEXP_.instantiateModuleFromAsm( |
| 720 TestForLoopWithoutCondition.toString()).caller()); |
691 | 721 |
692 | 722 |
693 function TestForLoopWithoutNext() { | 723 function TestForLoopWithoutNext() { |
694 "use asm" | 724 "use asm" |
695 | 725 |
696 function caller() { | 726 function caller() { |
697 var i = 0; | 727 var i = 0; |
698 for (i=1; i < 41;) { | 728 for (i=1; i < 41;) { |
699 i = (i + 1) | 0; | 729 i = (i + 1) | 0; |
700 } | 730 } |
701 return i|0; | 731 return i|0; |
702 } | 732 } |
703 | 733 |
704 return {caller:caller}; | 734 return {caller:caller}; |
705 } | 735 } |
706 | 736 |
707 assertEquals(41, _WASMEXP_.asmCompileRun(TestForLoopWithoutNext.toString())); | 737 assertEquals(41, _WASMEXP_.instantiateModuleFromAsm( |
| 738 TestForLoopWithoutNext.toString()).caller()); |
708 | 739 |
709 | 740 |
710 function TestForLoopWithoutBody() { | 741 function TestForLoopWithoutBody() { |
711 "use asm" | 742 "use asm" |
712 | 743 |
713 function caller() { | 744 function caller() { |
714 var i = 0; | 745 var i = 0; |
715 for (i=1; i < 45 ; i = (i+1)|0) { | 746 for (i=1; i < 45 ; i = (i+1)|0) { |
716 } | 747 } |
717 return i|0; | 748 return i|0; |
718 } | 749 } |
719 | 750 |
720 return {caller:caller}; | 751 return {caller:caller}; |
721 } | 752 } |
722 | 753 |
723 assertEquals(45, _WASMEXP_.asmCompileRun(TestForLoopWithoutBody.toString())); | 754 assertEquals(45, _WASMEXP_.instantiateModuleFromAsm( |
| 755 TestForLoopWithoutBody.toString()).caller()); |
724 | 756 |
725 | 757 |
726 function TestDoWhile() { | 758 function TestDoWhile() { |
727 "use asm" | 759 "use asm" |
728 | 760 |
729 function caller() { | 761 function caller() { |
730 var i = 0; | 762 var i = 0; |
731 var ret = 21; | 763 var ret = 21; |
732 do { | 764 do { |
733 ret = (ret + ret)|0; | 765 ret = (ret + ret)|0; |
734 i = (i + 1)|0; | 766 i = (i + 1)|0; |
735 } while (i < 2); | 767 } while (i < 2); |
736 return ret|0; | 768 return ret|0; |
737 } | 769 } |
738 | 770 |
739 return {caller:caller}; | 771 return {caller:caller}; |
740 } | 772 } |
741 | 773 |
742 assertEquals(84, _WASMEXP_.asmCompileRun(TestDoWhile.toString())); | 774 assertEquals(84, _WASMEXP_.instantiateModuleFromAsm( |
| 775 TestDoWhile.toString()).caller()); |
743 | 776 |
744 | 777 |
745 function TestConditional() { | 778 function TestConditional() { |
746 "use asm" | 779 "use asm" |
747 | 780 |
748 function caller() { | 781 function caller() { |
749 var x = 1; | 782 var x = 1; |
750 return ((x > 0) ? 41 : 71)|0; | 783 return ((x > 0) ? 41 : 71)|0; |
751 } | 784 } |
752 | 785 |
753 return {caller:caller}; | 786 return {caller:caller}; |
754 } | 787 } |
755 | 788 |
756 assertEquals(41, _WASMEXP_.asmCompileRun(TestConditional.toString())); | 789 assertEquals(41, _WASMEXP_.instantiateModuleFromAsm( |
| 790 TestConditional.toString()).caller()); |
757 | 791 |
758 | 792 |
759 function TestSwitch() { | 793 function TestSwitch() { |
760 "use asm" | 794 "use asm" |
761 | 795 |
762 function caller() { | 796 function caller() { |
763 var ret = 0; | 797 var ret = 0; |
764 var x = 7; | 798 var x = 7; |
765 switch (x) { | 799 switch (x) { |
766 case 1: return 0; | 800 case 1: return 0; |
767 case 7: { | 801 case 7: { |
768 ret = 12; | 802 ret = 12; |
769 break; | 803 break; |
770 } | 804 } |
771 default: return 0; | 805 default: return 0; |
772 } | 806 } |
773 switch (x) { | 807 switch (x) { |
774 case 1: return 0; | 808 case 1: return 0; |
775 case 8: return 0; | 809 case 8: return 0; |
776 default: ret = (ret + 11)|0; | 810 default: ret = (ret + 11)|0; |
777 } | 811 } |
778 return ret|0; | 812 return ret|0; |
779 } | 813 } |
780 | 814 |
781 return {caller:caller}; | 815 return {caller:caller}; |
782 } | 816 } |
783 | 817 |
784 assertEquals(23, _WASMEXP_.asmCompileRun(TestSwitch.toString())); | 818 assertEquals(23, _WASMEXP_.instantiateModuleFromAsm( |
| 819 TestSwitch.toString()).caller()); |
785 | 820 |
786 | 821 |
787 function TestSwitchFallthrough() { | 822 function TestSwitchFallthrough() { |
788 "use asm" | 823 "use asm" |
789 | 824 |
790 function caller() { | 825 function caller() { |
791 var x = 17; | 826 var x = 17; |
792 var ret = 0; | 827 var ret = 0; |
793 switch (x) { | 828 switch (x) { |
794 case 17: | 829 case 17: |
795 case 14: ret = 39; | 830 case 14: ret = 39; |
796 case 1: ret = (ret + 3)|0; | 831 case 1: ret = (ret + 3)|0; |
797 case 4: break; | 832 case 4: break; |
798 default: ret = (ret + 1)|0; | 833 default: ret = (ret + 1)|0; |
799 } | 834 } |
800 return ret|0; | 835 return ret|0; |
801 } | 836 } |
802 | 837 |
803 return {caller:caller}; | 838 return {caller:caller}; |
804 } | 839 } |
805 | 840 |
806 assertEquals(42, _WASMEXP_.asmCompileRun(TestSwitchFallthrough.toString())); | 841 assertEquals(42, _WASMEXP_.instantiateModuleFromAsm( |
| 842 TestSwitchFallthrough.toString()).caller()); |
807 | 843 |
808 | 844 |
809 function TestNestedSwitch() { | 845 function TestNestedSwitch() { |
810 "use asm" | 846 "use asm" |
811 | 847 |
812 function caller() { | 848 function caller() { |
813 var x = 3; | 849 var x = 3; |
814 var y = -13; | 850 var y = -13; |
815 switch (x) { | 851 switch (x) { |
816 case 1: return 0; | 852 case 1: return 0; |
817 case 3: { | 853 case 3: { |
818 switch (y) { | 854 switch (y) { |
819 case 2: return 0; | 855 case 2: return 0; |
820 case -13: return 43; | 856 case -13: return 43; |
821 default: return 0; | 857 default: return 0; |
822 } | 858 } |
823 } | 859 } |
824 default: return 0; | 860 default: return 0; |
825 } | 861 } |
826 return 0; | 862 return 0; |
827 } | 863 } |
828 | 864 |
829 return {caller:caller}; | 865 return {caller:caller}; |
830 } | 866 } |
831 | 867 |
832 assertEquals(43, _WASMEXP_.asmCompileRun(TestNestedSwitch.toString())); | 868 assertEquals(43, _WASMEXP_.instantiateModuleFromAsm( |
| 869 TestNestedSwitch.toString()).caller()); |
833 | 870 |
834 | 871 |
835 function TestInitFunctionWithNoGlobals() { | 872 function TestInitFunctionWithNoGlobals() { |
836 "use asm"; | 873 "use asm"; |
837 function caller() { | 874 function caller() { |
838 return 51; | 875 return 51; |
839 } | 876 } |
840 return {caller}; | 877 return {caller}; |
841 } | 878 } |
842 | 879 |
(...skipping 26 matching lines...) Expand all Loading... |
869 | 906 |
870 function caller() { | 907 function caller() { |
871 return function_table[0&0]() | 0; | 908 return function_table[0&0]() | 0; |
872 } | 909 } |
873 | 910 |
874 var function_table = [dummy] | 911 var function_table = [dummy] |
875 | 912 |
876 return {caller:caller}; | 913 return {caller:caller}; |
877 } | 914 } |
878 | 915 |
879 assertEquals(71, | 916 assertEquals(71, _WASMEXP_.instantiateModuleFromAsm( |
880 _WASMEXP_.asmCompileRun(TestFunctionTableSingleFunction.toString())); | 917 TestFunctionTableSingleFunction.toString()).caller()); |
881 | 918 |
882 | 919 |
883 function TestFunctionTableMultipleFunctions() { | 920 function TestFunctionTableMultipleFunctions() { |
884 "use asm"; | 921 "use asm"; |
885 | 922 |
886 function inc1(x) { | 923 function inc1(x) { |
887 x = x|0; | 924 x = x|0; |
888 return (x+1)|0; | 925 return (x+1)|0; |
889 } | 926 } |
890 | 927 |
891 function inc2(x) { | 928 function inc2(x) { |
892 x = x|0; | 929 x = x|0; |
893 return (x+2)|0; | 930 return (x+2)|0; |
894 } | 931 } |
895 | 932 |
896 function caller() { | 933 function caller() { |
897 if (function_table[0&1](50) == 51) { | 934 if (function_table[0&1](50) == 51) { |
898 if (function_table[1&1](60) == 62) { | 935 if (function_table[1&1](60) == 62) { |
899 return 73; | 936 return 73; |
900 } | 937 } |
901 } | 938 } |
902 return 0; | 939 return 0; |
903 } | 940 } |
904 | 941 |
905 var function_table = [inc1, inc2] | 942 var function_table = [inc1, inc2] |
906 | 943 |
907 return {caller:caller}; | 944 return {caller:caller}; |
908 } | 945 } |
909 | 946 |
910 assertEquals(73, | 947 assertEquals(73, _WASMEXP_.instantiateModuleFromAsm( |
911 _WASMEXP_.asmCompileRun(TestFunctionTableMultipleFunctions.toString())); | 948 TestFunctionTableMultipleFunctions.toString()).caller()); |
912 | 949 |
913 | 950 |
914 function TestFunctionTable() { | 951 function TestFunctionTable() { |
915 "use asm"; | 952 "use asm"; |
916 | 953 |
917 function add(a, b) { | 954 function add(a, b) { |
918 a = a|0; | 955 a = a|0; |
919 b = b|0; | 956 b = b|0; |
920 return (a+b)|0; | 957 return (a+b)|0; |
921 } | 958 } |
(...skipping 29 matching lines...) Expand all Loading... |
951 } | 988 } |
952 | 989 |
953 var module = _WASMEXP_.instantiateModuleFromAsm(TestFunctionTable.toString()); | 990 var module = _WASMEXP_.instantiateModuleFromAsm(TestFunctionTable.toString()); |
954 module.__init__(); | 991 module.__init__(); |
955 assertEquals(55, module.caller(0, 0, 33, 22)); | 992 assertEquals(55, module.caller(0, 0, 33, 22)); |
956 assertEquals(11, module.caller(0, 1, 33, 22)); | 993 assertEquals(11, module.caller(0, 1, 33, 22)); |
957 assertEquals(9, module.caller(0, 2, 54, 45)); | 994 assertEquals(9, module.caller(0, 2, 54, 45)); |
958 assertEquals(99, module.caller(0, 3, 54, 45)); | 995 assertEquals(99, module.caller(0, 3, 54, 45)); |
959 assertEquals(23, module.caller(0, 4, 12, 11)); | 996 assertEquals(23, module.caller(0, 4, 12, 11)); |
960 assertEquals(31, module.caller(1, 0, 30, 11)); | 997 assertEquals(31, module.caller(1, 0, 30, 11)); |
OLD | NEW |