Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 4 | |
| 5 | (function testExpressionTypes() { |
| 6 | "use strict"; |
| 7 | ((x, y = x) => assertEquals(42, y))(42); |
| 8 | |
| 9 | ((x, y = (x)) => assertEquals(42, y))(42); |
| 10 | ((x, y = `${x}`) => assertEquals("42", y))(42); |
| 11 | ((x, y = x = x + 1) => assertEquals(43, y))(42); |
| 12 | ((x, y = x()) => assertEquals(42, y))(() => 42); |
| 13 | ((x, y = new x()) => assertEquals(42, y.z))(function() { this.z = 42 }); |
| 14 | ((x, y = -x) => assertEquals(-42, y))(42); |
| 15 | ((x, y = ++x) => assertEquals(43, y))(42); |
| 16 | ((x, y = x === 42) => assertTrue(y))(42); |
| 17 | ((x, y = (x == 42 ? x : 0)) => assertEquals(42, y))(42); |
| 18 | |
| 19 | ((x, y = function() { return x }) => assertEquals(42, y()))(42); |
| 20 | ((x, y = () => x) => assertEquals(42, y()))(42); |
| 21 | |
| 22 | // Literals |
| 23 | ((x, y = {z: x}) => assertEquals(42, y.z))(42); |
| 24 | ((x, y = {[x]: x}) => assertEquals(42, y[42]))(42); |
| 25 | ((x, y = [x]) => assertEquals(42, y[0]))(42); |
| 26 | ((x, y = [...x]) => assertEquals(42, y[0]))([42]); |
| 27 | |
| 28 | ((x, y = class { |
| 29 | static [x]() { return x } |
| 30 | }) => assertEquals(42, y[42]()))(42); |
| 31 | ((x, y = (new class { |
| 32 | z() { return x } |
| 33 | })) => assertEquals(42, y.z()))(42); |
| 34 | |
| 35 | ((x, y = (new class Y { |
| 36 | static [x]() { return x } |
| 37 | z() { return Y[42]() } |
| 38 | })) => assertEquals(42, y.z()))(42); |
| 39 | |
| 40 | ((x, y = (new class { |
| 41 | constructor() { this.z = x } |
| 42 | })) => assertEquals(42, y.z))(42); |
| 43 | ((x, y = (new class Y { |
| 44 | constructor() { this.z = x } |
| 45 | })) => assertEquals(42, y.z))(42); |
| 46 | |
| 47 | ((x, y = (new class extends x { |
| 48 | })) => assertEquals(42, y.z()))(class { z() { return 42 } }); |
| 49 | |
| 50 | // Defaults inside destructuring |
| 51 | ((x, {y = x}) => assertEquals(42, y))(42, {}); |
| 52 | ((x, [y = x]) => assertEquals(42, y))(42, []); |
| 53 | })(); |
| 54 | |
| 55 | |
| 56 | (function testMultiScopeCapture() { |
| 57 | "use strict"; |
| 58 | var x = 1; |
| 59 | { |
| 60 | let y = 2; |
| 61 | ((x, y, a = x, b = y) => { |
| 62 | assertEquals(3, x); |
| 63 | assertEquals(3, a); |
| 64 | assertEquals(4, y); |
| 65 | assertEquals(4, b); |
| 66 | })(3, 4); |
| 67 | } |
| 68 | })(); |
| 69 | |
| 70 | |
| 71 | (function testSuper() { |
| 72 | "use strict"; |
| 73 | class A { |
| 74 | x() { return 42; } |
| 75 | } |
| 76 | |
| 77 | class B extends A { |
| 78 | y() { |
| 79 | ((q = super.x()) => assertEquals(42, q))(); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | new B().y(); |
| 84 | |
| 85 | class C { |
| 86 | constructor() { return { prop: 42 } } |
| 87 | } |
| 88 | |
| 89 | class D extends C{ |
| 90 | constructor() { |
| 91 | ((q = super()) => assertEquals(42, q.prop))(); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | new D(); |
| 96 | })(); |
| 97 | |
| 98 | |
| 99 | (function testScopeFlags() { |
| 100 | ((x, y = eval('x')) => assertEquals(42, y))(42); |
| 101 | ((x, {y = eval('x')}) => assertEquals(42, y))(42, {}); |
| 102 | })(); |