Index: test/mjsunit/harmony/destructuring.js |
diff --git a/test/mjsunit/harmony/destructuring.js b/test/mjsunit/harmony/destructuring.js |
index 83d0563b5ccd091d75a1106d6394fd3f6b0607fb..3e1726fdc2d6ab99b152548c32f401b2701a6072 100644 |
--- a/test/mjsunit/harmony/destructuring.js |
+++ b/test/mjsunit/harmony/destructuring.js |
@@ -14,7 +14,7 @@ |
var sum = 0; |
- for(var {z} = { z : 3 }; z != 0; z--) { |
+ for (var {z} = { z : 3 }; z != 0; z--) { |
sum += z; |
} |
assertEquals(6, sum); |
@@ -41,6 +41,79 @@ |
}()); |
+(function TestObjectLiteralPatternInitializers() { |
+ var { x : x, y : y = 2 } = { x : 1 }; |
+ assertEquals(1, x); |
+ assertEquals(2, y); |
+ |
+ var {z = 3} = {}; |
+ assertEquals(3, z); |
+ |
+ var sum = 0; |
+ for (var {z = 3} = {}; z != 0; z--) { |
+ sum += z; |
+ } |
+ assertEquals(6, sum); |
+ |
+ var log = []; |
+ var o = { |
+ get x() { |
+ log.push("x"); |
+ return undefined; |
+ }, |
+ get y() { |
+ log.push("y"); |
+ return { |
+ get z() { log.push("z"); return undefined; } |
+ } |
+ } |
+ }; |
+ var { x : x0 = 0, y : { z : z1 = 1}, x : x1 = 0} = o; |
+ assertSame(0, x0); |
+ assertSame(1, z1); |
+ assertSame(0, x1); |
+ assertArrayEquals(["x", "y", "z", "x"], log); |
+}()); |
+ |
+ |
+(function TestObjectLiteralPatternLexicalInitializers() { |
+ 'use strict'; |
+ let { x : x, y : y = 2 } = { x : 1 }; |
+ assertEquals(1, x); |
+ assertEquals(2, y); |
+ |
+ let {z = 3} = {}; |
+ assertEquals(3, z); |
+ |
+ let log = []; |
+ let o = { |
+ get x() { |
+ log.push("x"); |
+ return undefined; |
+ }, |
+ get y() { |
+ log.push("y"); |
+ return { |
+ get z() { log.push("z"); return undefined; } |
+ } |
+ } |
+ }; |
+ |
+ let { x : x0 = 0, y : { z : z1 = 1 }, x : x1 = 5} = o; |
+ assertSame(0, x0); |
+ assertSame(1, z1); |
+ assertSame(5, x1); |
+ assertArrayEquals(["x", "y", "z", "x"], log); |
+ |
+ let sum = 0; |
+ for (let {x = 0, z = 3} = {}; z != 0; z--) { |
+ assertEquals(0, x); |
+ sum += z; |
+ } |
+ assertEquals(6, sum); |
+}()); |
+ |
+ |
(function TestObjectLiteralPatternLexical() { |
'use strict'; |
let { x : x, y : y } = { x : 1, y : 2 }; |
@@ -70,7 +143,7 @@ |
assertArrayEquals(["x", "y", "z", "x"], log); |
let sum = 0; |
- for(let {x, z} = { x : 0, z : 3 }; z != 0; z--) { |
+ for (let {x, z} = { x : 0, z : 3 }; z != 0; z--) { |
assertEquals(0, x); |
sum += z; |
} |
@@ -90,8 +163,7 @@ |
const {z} = { z : 3 }; |
assertEquals(3, z); |
- |
- for(const {x, z} = { x : 0, z : 3 }; z != 3 || x != 0;) { |
+ for (const {x, z} = { x : 0, z : 3 }; z != 3 || x != 0;) { |
assertTrue(false); |
} |
}()); |
@@ -130,6 +202,87 @@ |
}()); |
+(function TestTDZInIntializers() { |
+ 'use strict'; |
+ { |
+ let {x, y = x} = {x : 42, y : 27}; |
+ assertSame(42, x); |
+ assertSame(27, y); |
+ } |
+ |
+ { |
+ let {x, y = x + 1} = { x : 42 }; |
+ assertSame(42, x); |
+ assertSame(43, y); |
+ } |
+ assertThrows(function() { |
+ let {x = y, y} = { y : 42 }; |
+ }, ReferenceError); |
+ |
+ { |
+ let {x, y = eval("x+1")} = {x:42}; |
+ assertEquals(42, x); |
+ assertEquals(43, y); |
+ } |
+ |
+ { |
+ let {x = function() {return y+1;}, y} = {y:42}; |
+ assertEquals(43, x()); |
+ assertEquals(42, y); |
+ } |
+ { |
+ let {x = function() {return eval("y+1");}, y} = {y:42}; |
+ assertEquals(43, x()); |
+ assertEquals(42, y); |
+ } |
+}()); |
+ |
+ |
+(function TestSideEffectsInInitializers() { |
+ var callCount = 0; |
+ function f(v) { callCount++; return v; } |
+ |
+ callCount = 0; |
+ var { x = f(42) } = { x : 27 }; |
+ assertSame(27, x); |
+ assertEquals(0, callCount); |
+ |
+ callCount = 0; |
+ var { x = f(42) } = {}; |
+ assertSame(42, x); |
+ assertEquals(1, callCount); |
+}()); |
+ |
+ |
+(function TestMultipleAccesses() { |
+ assertThrows( |
+ "'use strict';"+ |
+ "const {x,x} = {x:1};", |
+ SyntaxError); |
+ |
+ assertThrows( |
+ "'use strict';"+ |
+ "let {x,x} = {x:1};", |
+ SyntaxError); |
+ |
+ (function() { |
+ var {x,x = 2} = {x : 1}; |
+ assertSame(1, x); |
+ }()); |
+ |
+ assertThrows(function () { |
+ 'use strict'; |
+ let {x = (function() { x = 2; }())} = {}; |
+ }, ReferenceError); |
+ |
+ (function() { |
+ 'use strict'; |
+ let {x = (function() { x = 2; }())} = {x:1}; |
+ assertSame(1, x); |
+ }()); |
+}()); |
+ |
+ |
(function TestExceptions() { |
for (var val of [null, undefined]) { |
assertThrows(function() { var {} = val; }, TypeError); |