1// Copyright 2015 the V8 project authors. All rights reserved. 2// Redistribution and use in source and binary forms, with or without 3// modification, are permitted provided that the following conditions are 4// met: 5// 6// * Redistributions of source code must retain the above copyright 7// notice, this list of conditions and the following disclaimer. 8// * Redistributions in binary form must reproduce the above 9// copyright notice, this list of conditions and the following 10// disclaimer in the documentation and/or other materials provided 11// with the distribution. 12// * Neither the name of Google Inc. nor the names of its 13// contributors may be used to endorse or promote products derived 14// from this software without specific prior written permission. 15// 16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 28// This file only tests very simple descriptors that always have 29// configurable, enumerable, and writable set to true. 30 31// This is adapted from mjsunit/get-own-property-descriptor.js. 32 33// Flags: --harmony-reflect 34 35function get() { return x; } 36function set(x) { this.x = x; } 37 38var obj = {x: 1}; 39obj.__defineGetter__("accessor", get); 40obj.__defineSetter__("accessor", set); 41var a = new Array(); 42a[1] = 42; 43obj[1] = 42; 44 45var descIsData = Reflect.getOwnPropertyDescriptor(obj, 'x'); 46assertTrue(descIsData.enumerable); 47assertTrue(descIsData.writable); 48assertTrue(descIsData.configurable); 49 50var descIsAccessor = Reflect.getOwnPropertyDescriptor(obj, 'accessor'); 51assertTrue(descIsAccessor.enumerable); 52assertTrue(descIsAccessor.configurable); 53assertTrue(descIsAccessor.get == get); 54assertTrue(descIsAccessor.set == set); 55 56var descIsNotData = Reflect.getOwnPropertyDescriptor(obj, 'not-x'); 57assertTrue(descIsNotData == undefined); 58 59var descIsNotAccessor = Reflect.getOwnPropertyDescriptor(obj, 'not-accessor'); 60assertTrue(descIsNotAccessor == undefined); 61 62var descArray = Reflect.getOwnPropertyDescriptor(a, '1'); 63assertTrue(descArray.enumerable); 64assertTrue(descArray.configurable); 65assertTrue(descArray.writable); 66assertEquals(descArray.value, 42); 67 68var descObjectElement = Reflect.getOwnPropertyDescriptor(obj, '1'); 69assertTrue(descObjectElement.enumerable); 70assertTrue(descObjectElement.configurable); 71assertTrue(descObjectElement.writable); 72assertEquals(descObjectElement.value, 42); 73 74// String objects. 75var a = new String('foobar'); 76for (var i = 0; i < a.length; i++) { 77 var descStringObject = Reflect.getOwnPropertyDescriptor(a, i); 78 assertTrue(descStringObject.enumerable); 79 assertFalse(descStringObject.configurable); 80 assertFalse(descStringObject.writable); 81 assertEquals(descStringObject.value, a.substring(i, i+1)); 82} 83 84// Support for additional attributes on string objects. 85a.x = 42; 86a[10] = 'foo'; 87var descStringProperty = Reflect.getOwnPropertyDescriptor(a, 'x'); 88assertTrue(descStringProperty.enumerable); 89assertTrue(descStringProperty.configurable); 90assertTrue(descStringProperty.writable); 91assertEquals(descStringProperty.value, 42); 92 93var descStringElement = Reflect.getOwnPropertyDescriptor(a, '10'); 94assertTrue(descStringElement.enumerable); 95assertTrue(descStringElement.configurable); 96assertTrue(descStringElement.writable); 97assertEquals(descStringElement.value, 'foo'); 98 99// Test that elements in the prototype chain is not returned. 100var proto = {}; 101proto[10] = 42; 102 103var objWithProto = new Array(); 104objWithProto.prototype = proto; 105objWithProto[0] = 'bar'; 106var descWithProto = Reflect.getOwnPropertyDescriptor(objWithProto, '10'); 107assertEquals(undefined, descWithProto); 108 109// Test elements on global proxy object. 110var global = (function() { return this; })(); 111 112global[42] = 42; 113 114function el_getter() { return 239; }; 115function el_setter() {}; 116Object.defineProperty(global, '239', {get: el_getter, set: el_setter}); 117 118var descRegularElement = Reflect.getOwnPropertyDescriptor(global, '42'); 119assertEquals(42, descRegularElement.value); 120 121var descAccessorElement = Reflect.getOwnPropertyDescriptor(global, '239'); 122assertEquals(el_getter, descAccessorElement.get); 123assertEquals(el_setter, descAccessorElement.set); 124