1 // Copyright 2015 the V8 project authors. All rights reserved. 2 // Copyright (C) 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions 6 // are met: 7 // 1. Redistributions of source code must retain the above copyright 8 // notice, this list of conditions and the following disclaimer. 9 // 2. Redistributions in binary form must reproduce the above copyright 10 // notice, this list of conditions and the following disclaimer in the 11 // documentation and/or other materials provided with the distribution. 12 // 13 // THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY 14 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 15 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 // DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY 17 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 20 // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 22 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 24 description('Tests for the descriptors of the properties implicitly defined by ES6 class syntax'); 25 26 function descriptor(object, propertyName) { 27 return Object.getOwnPropertyDescriptor(object, propertyName); 28 } 29 30 function enumeratedProperties(object) { 31 var properties = []; 32 for (var propertyName in object) 33 properties.push(propertyName); 34 return properties; 35 } 36 37 Array.prototype.includes = function(element) { 38 return this.indexOf(element) !== -1; 39 }; 40 41 shouldBeFalse('class A {}; descriptor(A, "prototype").writable'); 42 shouldBe('class A {}; var x = A.prototype; A.prototype = 3; A.prototype', 'x'); 43 shouldBeFalse('class A {}; descriptor(A, "prototype").enumerable'); 44 shouldBeTrue('class A {}; A.foo = "foo"; enumeratedProperties(A).includes("foo")'); 45 shouldBeFalse('class A {}; enumeratedProperties(A).includes("prototype")'); 46 shouldBeFalse('class A {}; descriptor(A, "prototype").configurable'); 47 shouldThrow('class A {}; Object.defineProperty(A, "prototype", {value: "foo"})', '"TypeError: Cannot redefine property: prototype"'); 48 49 shouldBeTrue('class A { static foo() {} }; descriptor(A, "foo").writable'); 50 shouldBe('class A { static foo() {} }; A.foo = 3; A.foo', '3'); 51 shouldBeFalse('class A { static foo() {} }; descriptor(A, "foo").enumerable'); 52 shouldBeFalse('class A { static foo() {} }; enumeratedProperties(A).includes("foo")'); 53 shouldBeTrue('class A { static foo() {} }; descriptor(A, "foo").configurable'); 54 shouldBe('class A { static foo() {} }; Object.defineProperty(A, "foo", {value: "bar"}); A.foo', '"bar"'); 55 56 shouldBe('class A { static get foo() {} }; descriptor(A, "foo").writable', 'undefined'); 57 shouldBe('class A { static get foo() { return 5; } }; A.foo = 3; A.foo', '5'); 58 shouldBeFalse('class A { static get foo() {} }; descriptor(A, "foo").enumerable'); 59 shouldBeFalse('class A { static get foo() {} }; enumeratedProperties(A).includes("foo")'); 60 shouldBeFalse('class A { static get foo() {} }; enumeratedProperties(new A).includes("foo")'); 61 shouldBeTrue('class A { static get foo() {} }; descriptor(A, "foo").configurable'); 62 shouldBe('class A { static get foo() {} }; Object.defineProperty(A, "foo", {value: "bar"}); A.foo', '"bar"'); 63 64 shouldBeTrue('class A { foo() {} }; descriptor(A.prototype, "foo").writable'); 65 shouldBe('class A { foo() {} }; A.prototype.foo = 3; A.prototype.foo', '3'); 66 shouldBeFalse('class A { foo() {} }; descriptor(A.prototype, "foo").enumerable'); 67 shouldBeFalse('class A { foo() {} }; enumeratedProperties(A.prototype).includes("foo")'); 68 shouldBeFalse('class A { foo() {} }; enumeratedProperties(new A).includes("foo")'); 69 shouldBeTrue('class A { foo() {} }; descriptor(A.prototype, "foo").configurable'); 70 shouldBe('class A { foo() {} }; Object.defineProperty(A.prototype, "foo", {value: "bar"}); A.prototype.foo', '"bar"'); 71 72 shouldBe('class A { get foo() {} }; descriptor(A.prototype, "foo").writable', 'undefined'); 73 shouldBe('class A { get foo() { return 5; } }; A.prototype.foo = 3; A.prototype.foo', '5'); 74 shouldBeFalse('class A { get foo() {} }; descriptor(A.prototype, "foo").enumerable'); 75 shouldBeFalse('class A { get foo() {} }; enumeratedProperties(A.prototype).includes("foo")'); 76 shouldBeFalse('class A { get foo() {} }; enumeratedProperties(new A).includes("foo")'); 77 shouldBeTrue('class A { get foo() {} }; descriptor(A.prototype, "foo").configurable'); 78 shouldBe('class A { get foo() {} }; Object.defineProperty(A.prototype, "foo", {value: "bar"}); A.prototype.foo', '"bar"'); 79 80 shouldBeTrue('class A { }; descriptor(A.prototype, "constructor").writable'); 81 shouldBe('class A { }; A.prototype.constructor = 3; A.prototype.constructor', '3'); 82 shouldBeTrue('class A { }; x = {}; A.prototype.constructor = function () { return x; }; (new A) instanceof A'); 83 shouldBeFalse('class A { }; descriptor(A.prototype, "constructor").enumerable'); 84 shouldBeFalse('class A { }; enumeratedProperties(A.prototype).includes("constructor")'); 85 shouldBeFalse('class A { }; enumeratedProperties(new A).includes("constructor")'); 86 shouldBeTrue('class A { }; descriptor(A.prototype, "constructor").configurable'); 87 shouldBe('class A { }; Object.defineProperty(A.prototype, "constructor", {value: "bar"}); A.prototype.constructor', '"bar"'); 88 89 var successfullyParsed = true; 90