OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Flags: --harmony-do-expressions --harmony-sloppy-let --allow-natives-syntax |
| 6 |
| 7 function returnValue(v) { return v; } |
| 8 function MyError() {} |
| 9 |
| 10 function TestBasic() { |
| 11 // Looping and lexical declarations |
| 12 assertEquals(512, returnValue(do { |
| 13 let n = 2; |
| 14 for (let i = 0; i < 4; i++) n <<= 2; |
| 15 })); |
| 16 |
| 17 // Strings do the right thing |
| 18 assertEquals("spooky halloween", returnValue(do { |
| 19 "happy halloween".replace('happy', 'spooky'); |
| 20 })); |
| 21 |
| 22 // Do expressions with no completion produce an undefined value |
| 23 assertEquals(undefined, returnValue(do {})); |
| 24 assertEquals(undefined, returnValue(do { var x = 99; })); |
| 25 assertEquals(undefined, returnValue(do { function f() {}; })); |
| 26 assertEquals(undefined, returnValue(do { let z = 33; })); |
| 27 |
| 28 // Propagation of exception |
| 29 assertThrows(function() { |
| 30 (do { |
| 31 throw new MyError(); |
| 32 "potatoes"; |
| 33 }); |
| 34 }, MyError); |
| 35 |
| 36 // Real-world use case for desugaring |
| 37 var array = [1, 2, 3, 4, 5], iterable = [6, 7, 8,9]; |
| 38 assertEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], do { |
| 39 for (var element of iterable) array.push(element); |
| 40 array; |
| 41 }); |
| 42 |
| 43 // Nested do-expressions |
| 44 assertEquals(125, do { (do { (do { 5 * 5 * 5 }) }) }); |
| 45 |
| 46 // Directives are not honoured |
| 47 (do { |
| 48 "use strict"; |
| 49 foo = 80; |
| 50 assertEquals(foo, 80); |
| 51 }); |
| 52 |
| 53 // Non-empty operand stack testing |
| 54 var O = { |
| 55 method1() { |
| 56 let x = 256; |
| 57 return x + do { |
| 58 for (var i = 0; i < 4; ++i) x += i; |
| 59 } + 17; |
| 60 }, |
| 61 method2() { |
| 62 let x = 256; |
| 63 this.reset(); |
| 64 return x + do { |
| 65 for (var i = 0; i < this.length(); ++i) x += this.index() * 2; |
| 66 }; |
| 67 }, |
| 68 _index: 0, |
| 69 index() { |
| 70 return ++this._index; |
| 71 }, |
| 72 _length: 4, |
| 73 length() { return this._length; }, |
| 74 reset() { this._index = 0; } |
| 75 }; |
| 76 assertEquals(535, O["method" + do { 1 } + ""]()); |
| 77 assertEquals(532, O["method" + do { ({ valueOf() { return "2"; } }); }]()); |
| 78 assertEquals(532, O[ |
| 79 do { let s = ""; for (let c of "method") s += c; } + "2"]()); |
| 80 } |
| 81 TestBasic(); |
| 82 |
| 83 |
| 84 function TestDeoptimization1() { |
| 85 function f(v) { |
| 86 return 88 + do { |
| 87 v.a * v.b + v.c; |
| 88 }; |
| 89 } |
| 90 |
| 91 var o1 = {}; |
| 92 o1.a = 10; |
| 93 o1.b = 5; |
| 94 o1.c = 50; |
| 95 |
| 96 var o2 = {}; |
| 97 o2.c = 100; |
| 98 o2.a = 10; |
| 99 o2.b = 10; |
| 100 |
| 101 assertEquals(188, f(o1)); |
| 102 assertEquals(188, f(o1)); |
| 103 %OptimizeFunctionOnNextCall(f); |
| 104 assertEquals(188, f(o1)); |
| 105 assertOptimized(f); |
| 106 assertEquals(288, f(o2)); |
| 107 assertUnoptimized(f); |
| 108 assertEquals(288, f(o2)); |
| 109 } |
| 110 TestDeoptimization1(); |
OLD | NEW |