Index: test/mjsunit/harmony/do-expressions.js |
diff --git a/test/mjsunit/harmony/do-expressions.js b/test/mjsunit/harmony/do-expressions.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..8e09b7c7132b1d429a02d563a658e67b180b1b16 |
--- /dev/null |
+++ b/test/mjsunit/harmony/do-expressions.js |
@@ -0,0 +1,110 @@ |
+// Copyright 2015 the V8 project authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+// Flags: --harmony-do-expressions --harmony-sloppy-let --allow-natives-syntax |
+ |
+function returnValue(v) { return v; } |
+function MyError() {} |
+ |
+function TestBasic() { |
+ // Looping and lexical declarations |
+ assertEquals(512, returnValue(do { |
+ let n = 2; |
+ for (let i = 0; i < 4; i++) n <<= 2; |
+ })); |
+ |
+ // Strings do the right thing |
+ assertEquals("spooky halloween", returnValue(do { |
+ "happy halloween".replace('happy', 'spooky'); |
+ })); |
+ |
+ // Do expressions with no completion produce an undefined value |
+ assertEquals(undefined, returnValue(do {})); |
+ assertEquals(undefined, returnValue(do { var x = 99; })); |
+ assertEquals(undefined, returnValue(do { function f() {}; })); |
+ assertEquals(undefined, returnValue(do { let z = 33; })); |
+ |
+ // Propagation of exception |
+ assertThrows(function() { |
+ (do { |
+ throw new MyError(); |
+ "potatoes"; |
+ }); |
+ }, MyError); |
+ |
+ // Real-world use case for desugaring |
+ var array = [1, 2, 3, 4, 5], iterable = [6, 7, 8,9]; |
+ assertEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], do { |
+ for (var element of iterable) array.push(element); |
+ array; |
+ }); |
+ |
+ // Nested do-expressions |
+ assertEquals(125, do { (do { (do { 5 * 5 * 5 }) }) }); |
+ |
+ // Directives are not honoured |
+ (do { |
+ "use strict"; |
+ foo = 80; |
+ assertEquals(foo, 80); |
+ }); |
+ |
+ // Non-empty operand stack testing |
+ var O = { |
+ method1() { |
+ let x = 256; |
+ return x + do { |
+ for (var i = 0; i < 4; ++i) x += i; |
+ } + 17; |
+ }, |
+ method2() { |
+ let x = 256; |
+ this.reset(); |
+ return x + do { |
+ for (var i = 0; i < this.length(); ++i) x += this.index() * 2; |
+ }; |
+ }, |
+ _index: 0, |
+ index() { |
+ return ++this._index; |
+ }, |
+ _length: 4, |
+ length() { return this._length; }, |
+ reset() { this._index = 0; } |
+ }; |
+ assertEquals(535, O["method" + do { 1 } + ""]()); |
+ assertEquals(532, O["method" + do { ({ valueOf() { return "2"; } }); }]()); |
+ assertEquals(532, O[ |
+ do { let s = ""; for (let c of "method") s += c; } + "2"]()); |
+} |
+TestBasic(); |
+ |
+ |
+function TestDeoptimization1() { |
+ function f(v) { |
+ return 88 + do { |
+ v.a * v.b + v.c; |
+ }; |
+ } |
+ |
+ var o1 = {}; |
+ o1.a = 10; |
+ o1.b = 5; |
+ o1.c = 50; |
+ |
+ var o2 = {}; |
+ o2.c = 100; |
+ o2.a = 10; |
+ o2.b = 10; |
+ |
+ assertEquals(188, f(o1)); |
+ assertEquals(188, f(o1)); |
+ %OptimizeFunctionOnNextCall(f); |
+ assertEquals(188, f(o1)); |
+ assertOptimized(f); |
+ assertEquals(288, f(o2)); |
+ assertUnoptimized(f); |
+ assertEquals(288, f(o2)); |
+} |
+TestDeoptimization1(); |