OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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: --validate-asm --allow-natives-syntax | 5 // Flags: --validate-asm --allow-natives-syntax |
6 | 6 |
7 function assertValidAsm(func) { | 7 function assertValidAsm(func) { |
8 assertTrue(%IsAsmWasmCode(func)); | 8 assertTrue(%IsAsmWasmCode(func)); |
9 } | 9 } |
10 | 10 |
| 11 (function TestConst() { |
| 12 function Module(s) { |
| 13 "use asm"; |
| 14 var fround = s.Math.fround; |
| 15 // Global constants. These are treated just like numeric literals. |
| 16 const fConst = fround(-3.0); |
| 17 const dConst = -3.0; |
| 18 const iConst = -3; |
| 19 |
| 20 // consts can be used to initialize other consts. |
| 21 const fPrime = fConst; |
| 22 |
| 23 // The following methods verify that return statements with global constants |
| 24 // do not need type annotations. |
| 25 function f() { |
| 26 return fPrime; |
| 27 } |
| 28 function d() { |
| 29 return dConst; |
| 30 } |
| 31 function i() { |
| 32 return iConst; |
| 33 } |
| 34 |
| 35 // The following methods verify that locals initialized with global |
| 36 // constants do not need type annotations. |
| 37 function fVar() { |
| 38 var v = fPrime; |
| 39 return fround(v); |
| 40 } |
| 41 function iVar() { |
| 42 var v = iConst; |
| 43 return v|0; |
| 44 } |
| 45 function dVar() { |
| 46 var v = dConst; |
| 47 return +v; |
| 48 } |
| 49 |
| 50 return { |
| 51 f: f, d: d, i: i, |
| 52 fVar: fVar, dVar: dVar, iVar: iVar, |
| 53 }; |
| 54 } |
| 55 |
| 56 function DisallowAssignToConstGlobal() { |
| 57 const constant = 0; |
| 58 function invalid(i) { |
| 59 i = i|0; |
| 60 constant = i; |
| 61 return constant; |
| 62 } |
| 63 return invalid; |
| 64 } |
| 65 |
| 66 var m = Module(this); |
| 67 assertValidAsm(Module); |
| 68 |
| 69 assertEquals(-3, m.i()); |
| 70 assertEquals(-3.0, m.d()); |
| 71 assertEquals(Math.fround(-3.0), m.f()); |
| 72 |
| 73 assertEquals(-3, m.iVar()); |
| 74 assertEquals(-3.0, m.dVar()); |
| 75 assertEquals(Math.fround(-3.0), m.fVar()); |
| 76 |
| 77 var m = DisallowAssignToConstGlobal(); |
| 78 assertTrue(%IsNotAsmWasmCode(DisallowAssignToConstGlobal)); |
| 79 })(); |
| 80 |
11 (function TestModuleArgs() { | 81 (function TestModuleArgs() { |
12 function Module1(stdlib) { | 82 function Module1(stdlib) { |
13 "use asm"; | 83 "use asm"; |
14 function foo() { } | 84 function foo() { } |
15 return { foo: foo }; | 85 return { foo: foo }; |
16 } | 86 } |
17 function Module2(stdlib, ffi) { | 87 function Module2(stdlib, ffi) { |
18 "use asm"; | 88 "use asm"; |
19 function foo() { } | 89 function foo() { } |
20 return { foo: foo }; | 90 return { foo: foo }; |
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 "use asm"; | 276 "use asm"; |
207 function foo() { return 123; } | 277 function foo() { return 123; } |
208 return { foo: foo }; | 278 return { foo: foo }; |
209 } | 279 } |
210 var heap = new ArrayBuffer(1024 * 1024); | 280 var heap = new ArrayBuffer(1024 * 1024); |
211 var ModuleBound = Module.bind(this, {}, {}, heap); | 281 var ModuleBound = Module.bind(this, {}, {}, heap); |
212 var m = ModuleBound(); | 282 var m = ModuleBound(); |
213 assertValidAsm(Module); | 283 assertValidAsm(Module); |
214 assertEquals(123, m.foo()); | 284 assertEquals(123, m.foo()); |
215 })(); | 285 })(); |
OLD | NEW |