Commit 98dfe024 authored by neis's avatar neis Committed by Commit bot

[es6] Partially implement Reflect.getOwnPropertyDescriptor.

Proxies are not properly supported yet.

R=rossberg, jkummerow@chromium.org
BUG=

Review URL: https://codereview.chromium.org/1408163005

Cr-Commit-Position: refs/heads/master@{#31674}
parent e8f752ce
......@@ -2197,6 +2197,8 @@ void Genesis::InitializeGlobal_harmony_reflect() {
Builtins::kReflectDeleteProperty, 2, true);
SimpleInstallFunction(reflect, "get",
Builtins::kReflectGet, 3, false);
SimpleInstallFunction(reflect, "getOwnPropertyDescriptor",
Builtins::kReflectGetOwnPropertyDescriptor, 2, true);
SimpleInstallFunction(reflect, "getPrototypeOf",
Builtins::kReflectGetPrototypeOf, 1, true);
SimpleInstallFunction(reflect, "has",
......
......@@ -1533,6 +1533,33 @@ BUILTIN(ReflectGet) {
}
// ES6 section 26.1.7 Reflect.getOwnPropertyDescriptor
BUILTIN(ReflectGetOwnPropertyDescriptor) {
HandleScope scope(isolate);
DCHECK_EQ(3, args.length());
Handle<Object> target = args.at<Object>(1);
Handle<Object> key = args.at<Object>(2);
if (!target->IsJSReceiver()) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kCalledOnNonObject,
isolate->factory()->NewStringFromAsciiChecked(
"Reflect.getOwnPropertyDescriptor")));
}
Handle<Name> name;
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, name,
Object::ToName(isolate, key));
PropertyDescriptor desc;
bool found = JSReceiver::GetOwnPropertyDescriptor(
isolate, Handle<JSReceiver>::cast(target), name, &desc);
if (isolate->has_pending_exception()) return isolate->heap()->exception();
if (!found) return isolate->heap()->undefined_value();
return *desc.ToObject(isolate);
}
// ES6 section 26.1.8 Reflect.getPrototypeOf
BUILTIN(ReflectGetPrototypeOf) {
HandleScope scope(isolate);
......
......@@ -62,6 +62,7 @@ enum BuiltinExtraArguments {
V(ReflectDefineProperty, NO_EXTRA_ARGUMENTS) \
V(ReflectDeleteProperty, NO_EXTRA_ARGUMENTS) \
V(ReflectGet, NO_EXTRA_ARGUMENTS) \
V(ReflectGetOwnPropertyDescriptor, NO_EXTRA_ARGUMENTS) \
V(ReflectGetPrototypeOf, NO_EXTRA_ARGUMENTS) \
V(ReflectHas, NO_EXTRA_ARGUMENTS) \
V(ReflectIsExtensible, NO_EXTRA_ARGUMENTS) \
......
......@@ -6608,7 +6608,7 @@ bool JSArray::ArraySetLength(Isolate* isolate, Handle<JSArray> a,
// static
bool JSReceiver::GetOwnPropertyDescriptor(Isolate* isolate,
Handle<JSObject> object,
Handle<JSReceiver> object,
Handle<Object> key,
PropertyDescriptor* desc) {
bool success = false;
......@@ -6672,6 +6672,8 @@ bool JSReceiver::GetOwnPropertyDescriptor(LookupIterator* it,
// 8. Set D.[[Configurable]] to the value of X's [[Configurable]] attribute.
desc->set_configurable((attrs & DONT_DELETE) == 0);
// 9. Return D.
DCHECK(PropertyDescriptor::IsAccessorDescriptor(desc) !=
PropertyDescriptor::IsDataDescriptor(desc));
return true;
}
......
......@@ -1893,7 +1893,7 @@ class JSReceiver: public HeapObject {
ShouldThrow should_throw);
static bool GetOwnPropertyDescriptor(Isolate* isolate,
Handle<JSObject> object,
Handle<JSReceiver> object,
Handle<Object> key,
PropertyDescriptor* desc);
static bool GetOwnPropertyDescriptor(LookupIterator* it,
......
......@@ -101,6 +101,45 @@ bool ToPropertyDescriptorFastPath(Isolate* isolate, Handle<Object> obj,
}
static void CreateDataProperty(Isolate* isolate, Handle<JSObject> object,
Handle<String> name, Handle<Object> value) {
LookupIterator it(object, name);
Maybe<bool> result = JSObject::CreateDataProperty(&it, value);
CHECK(result.IsJust() && result.FromJust());
}
// ES6 6.2.4.4 "FromPropertyDescriptor"
Handle<Object> PropertyDescriptor::ToObject(Isolate* isolate) {
DCHECK(!(PropertyDescriptor::IsAccessorDescriptor(this) &&
PropertyDescriptor::IsDataDescriptor(this)));
Factory* factory = isolate->factory();
Handle<JSObject> result = factory->NewJSObject(isolate->object_function());
if (has_value()) {
CreateDataProperty(isolate, result, factory->value_string(), value());
}
if (has_writable()) {
CreateDataProperty(isolate, result, factory->writable_string(),
factory->ToBoolean(writable()));
}
if (has_get()) {
CreateDataProperty(isolate, result, factory->get_string(), get());
}
if (has_set()) {
CreateDataProperty(isolate, result, factory->set_string(), set());
}
if (has_enumerable()) {
CreateDataProperty(isolate, result, factory->enumerable_string(),
factory->ToBoolean(enumerable()));
}
if (has_configurable()) {
CreateDataProperty(isolate, result, factory->configurable_string(),
factory->ToBoolean(configurable()));
}
return result;
}
// ES6 6.2.4.5
// Returns false in case of exception.
// static
......
......@@ -89,6 +89,8 @@ class PropertyDescriptor {
(has_writable() && !writable() ? READ_ONLY : NONE));
}
Handle<Object> ToObject(Isolate* isolate);
static bool ToPropertyDescriptor(Isolate* isolate, Handle<Object> obj,
PropertyDescriptor* desc);
......
// Copyright 2015 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following
// disclaimer in the documentation and/or other materials provided
// with the distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// This file only tests very simple descriptors that always have
// configurable, enumerable, and writable set to true.
// This is adapted from mjsunit/get-own-property-descriptor.js.
// Flags: --harmony-reflect
function get() { return x; }
function set(x) { this.x = x; }
var obj = {x: 1};
obj.__defineGetter__("accessor", get);
obj.__defineSetter__("accessor", set);
var a = new Array();
a[1] = 42;
obj[1] = 42;
var descIsData = Reflect.getOwnPropertyDescriptor(obj, 'x');
assertTrue(descIsData.enumerable);
assertTrue(descIsData.writable);
assertTrue(descIsData.configurable);
var descIsAccessor = Reflect.getOwnPropertyDescriptor(obj, 'accessor');
assertTrue(descIsAccessor.enumerable);
assertTrue(descIsAccessor.configurable);
assertTrue(descIsAccessor.get == get);
assertTrue(descIsAccessor.set == set);
var descIsNotData = Reflect.getOwnPropertyDescriptor(obj, 'not-x');
assertTrue(descIsNotData == undefined);
var descIsNotAccessor = Reflect.getOwnPropertyDescriptor(obj, 'not-accessor');
assertTrue(descIsNotAccessor == undefined);
var descArray = Reflect.getOwnPropertyDescriptor(a, '1');
assertTrue(descArray.enumerable);
assertTrue(descArray.configurable);
assertTrue(descArray.writable);
assertEquals(descArray.value, 42);
var descObjectElement = Reflect.getOwnPropertyDescriptor(obj, '1');
assertTrue(descObjectElement.enumerable);
assertTrue(descObjectElement.configurable);
assertTrue(descObjectElement.writable);
assertEquals(descObjectElement.value, 42);
// String objects.
var a = new String('foobar');
for (var i = 0; i < a.length; i++) {
var descStringObject = Reflect.getOwnPropertyDescriptor(a, i);
assertTrue(descStringObject.enumerable);
assertFalse(descStringObject.configurable);
assertFalse(descStringObject.writable);
assertEquals(descStringObject.value, a.substring(i, i+1));
}
// Support for additional attributes on string objects.
a.x = 42;
a[10] = 'foo';
var descStringProperty = Reflect.getOwnPropertyDescriptor(a, 'x');
assertTrue(descStringProperty.enumerable);
assertTrue(descStringProperty.configurable);
assertTrue(descStringProperty.writable);
assertEquals(descStringProperty.value, 42);
var descStringElement = Reflect.getOwnPropertyDescriptor(a, '10');
assertTrue(descStringElement.enumerable);
assertTrue(descStringElement.configurable);
assertTrue(descStringElement.writable);
assertEquals(descStringElement.value, 'foo');
// Test that elements in the prototype chain is not returned.
var proto = {};
proto[10] = 42;
var objWithProto = new Array();
objWithProto.prototype = proto;
objWithProto[0] = 'bar';
var descWithProto = Reflect.getOwnPropertyDescriptor(objWithProto, '10');
assertEquals(undefined, descWithProto);
// Test elements on global proxy object.
var global = (function() { return this; })();
global[42] = 42;
function el_getter() { return 239; };
function el_setter() {};
Object.defineProperty(global, '239', {get: el_getter, set: el_setter});
var descRegularElement = Reflect.getOwnPropertyDescriptor(global, '42');
assertEquals(42, descRegularElement.value);
var descAccessorElement = Reflect.getOwnPropertyDescriptor(global, '239');
assertEquals(el_getter, descAccessorElement.get);
assertEquals(el_setter, descAccessorElement.set);
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment