2015-11-04 14:05:21 +00:00
|
|
|
// Copyright 2013 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2016-03-07 19:25:12 +00:00
|
|
|
#include "src/keys.h"
|
2015-11-04 14:05:21 +00:00
|
|
|
|
2016-05-03 15:29:28 +00:00
|
|
|
#include "src/api-arguments.h"
|
2015-11-04 14:05:21 +00:00
|
|
|
#include "src/elements.h"
|
|
|
|
#include "src/factory.h"
|
2016-05-03 15:29:28 +00:00
|
|
|
#include "src/identity-map.h"
|
2015-12-02 10:19:38 +00:00
|
|
|
#include "src/isolate-inl.h"
|
2015-11-04 14:05:21 +00:00
|
|
|
#include "src/objects-inl.h"
|
2015-12-02 10:19:38 +00:00
|
|
|
#include "src/property-descriptor.h"
|
2016-03-07 19:25:12 +00:00
|
|
|
#include "src/prototype.h"
|
2015-11-04 14:05:21 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
KeyAccumulator::~KeyAccumulator() {
|
|
|
|
}
|
|
|
|
|
2016-05-18 13:41:14 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
static bool ContainsOnlyValidKeys(Handle<FixedArray> array) {
|
|
|
|
int len = array->length();
|
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
|
Object* e = array->get(i);
|
|
|
|
if (!(e->IsName() || e->IsNumber())) return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2016-07-21 09:22:48 +00:00
|
|
|
|
|
|
|
// static
|
2016-05-18 13:41:14 +00:00
|
|
|
MaybeHandle<FixedArray> KeyAccumulator::GetKeys(
|
2016-05-30 15:52:56 +00:00
|
|
|
Handle<JSReceiver> object, KeyCollectionMode mode, PropertyFilter filter,
|
2016-08-01 10:08:54 +00:00
|
|
|
GetKeysConversion keys_conversion, bool is_for_in) {
|
2016-05-18 13:41:14 +00:00
|
|
|
Isolate* isolate = object->GetIsolate();
|
2016-07-21 09:22:48 +00:00
|
|
|
FastKeyAccumulator accumulator(isolate, object, mode, filter);
|
2016-05-25 09:13:31 +00:00
|
|
|
accumulator.set_is_for_in(is_for_in);
|
2016-06-07 08:50:04 +00:00
|
|
|
return accumulator.GetKeys(keys_conversion);
|
2016-05-18 13:41:14 +00:00
|
|
|
}
|
|
|
|
|
2015-11-04 14:05:21 +00:00
|
|
|
Handle<FixedArray> KeyAccumulator::GetKeys(GetKeysConversion convert) {
|
2016-05-25 09:13:31 +00:00
|
|
|
if (keys_.is_null()) {
|
2015-11-04 14:05:21 +00:00
|
|
|
return isolate_->factory()->empty_fixed_array();
|
|
|
|
}
|
2016-05-30 15:52:56 +00:00
|
|
|
if (mode_ == KeyCollectionMode::kOwnOnly &&
|
2016-05-25 09:13:31 +00:00
|
|
|
keys_->map() == isolate_->heap()->fixed_array_map()) {
|
|
|
|
return Handle<FixedArray>::cast(keys_);
|
2016-03-10 15:02:15 +00:00
|
|
|
}
|
2016-06-07 08:50:04 +00:00
|
|
|
USE(ContainsOnlyValidKeys);
|
|
|
|
Handle<FixedArray> result =
|
|
|
|
OrderedHashSet::ConvertToKeysArray(keys(), convert);
|
|
|
|
DCHECK(ContainsOnlyValidKeys(result));
|
|
|
|
return result;
|
2015-11-04 14:05:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 09:13:31 +00:00
|
|
|
void KeyAccumulator::AddKey(Object* key, AddKeyConversion convert) {
|
|
|
|
AddKey(handle(key, isolate_), convert);
|
2015-11-04 14:05:21 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 09:13:31 +00:00
|
|
|
void KeyAccumulator::AddKey(Handle<Object> key, AddKeyConversion convert) {
|
2015-11-04 14:05:21 +00:00
|
|
|
if (key->IsSymbol()) {
|
2016-05-25 09:13:31 +00:00
|
|
|
if (filter_ & SKIP_SYMBOLS) return;
|
|
|
|
if (Handle<Symbol>::cast(key)->is_private()) return;
|
|
|
|
} else if (filter_ & SKIP_STRINGS) {
|
|
|
|
return;
|
2015-11-04 14:05:21 +00:00
|
|
|
}
|
2016-06-28 13:32:18 +00:00
|
|
|
if (IsShadowed(key)) return;
|
2016-05-25 09:13:31 +00:00
|
|
|
if (keys_.is_null()) {
|
|
|
|
keys_ = OrderedHashSet::Allocate(isolate_, 16);
|
2016-05-24 17:35:53 +00:00
|
|
|
}
|
2016-05-25 09:13:31 +00:00
|
|
|
uint32_t index;
|
|
|
|
if (convert == CONVERT_TO_ARRAY_INDEX && key->IsString() &&
|
|
|
|
Handle<String>::cast(key)->AsArrayIndex(&index)) {
|
|
|
|
key = isolate_->factory()->NewNumberFromUint(index);
|
2015-11-04 14:05:21 +00:00
|
|
|
}
|
2016-05-25 09:13:31 +00:00
|
|
|
keys_ = OrderedHashSet::Add(keys(), key);
|
2015-11-04 14:05:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyAccumulator::AddKeys(Handle<FixedArray> array,
|
|
|
|
AddKeyConversion convert) {
|
|
|
|
int add_length = array->length();
|
|
|
|
for (int i = 0; i < add_length; i++) {
|
|
|
|
Handle<Object> current(array->get(i), isolate_);
|
|
|
|
AddKey(current, convert);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyAccumulator::AddKeys(Handle<JSObject> array_like,
|
|
|
|
AddKeyConversion convert) {
|
|
|
|
DCHECK(array_like->IsJSArray() || array_like->HasSloppyArgumentsElements());
|
|
|
|
ElementsAccessor* accessor = array_like->GetElementsAccessor();
|
|
|
|
accessor->AddElementsToKeyAccumulator(array_like, this, convert);
|
|
|
|
}
|
|
|
|
|
2016-06-28 13:32:18 +00:00
|
|
|
MaybeHandle<FixedArray> FilterProxyKeys(KeyAccumulator* accumulator,
|
|
|
|
Handle<JSProxy> owner,
|
2015-12-02 10:19:38 +00:00
|
|
|
Handle<FixedArray> keys,
|
2015-12-02 16:30:06 +00:00
|
|
|
PropertyFilter filter) {
|
|
|
|
if (filter == ALL_PROPERTIES) {
|
2015-12-02 10:19:38 +00:00
|
|
|
// Nothing to do.
|
|
|
|
return keys;
|
|
|
|
}
|
2016-06-28 13:32:18 +00:00
|
|
|
Isolate* isolate = accumulator->isolate();
|
2015-12-02 10:19:38 +00:00
|
|
|
int store_position = 0;
|
|
|
|
for (int i = 0; i < keys->length(); ++i) {
|
|
|
|
Handle<Name> key(Name::cast(keys->get(i)), isolate);
|
2015-12-09 09:40:34 +00:00
|
|
|
if (key->FilterKey(filter)) continue; // Skip this key.
|
2015-12-02 16:30:06 +00:00
|
|
|
if (filter & ONLY_ENUMERABLE) {
|
2015-12-02 10:19:38 +00:00
|
|
|
PropertyDescriptor desc;
|
2015-12-09 18:06:49 +00:00
|
|
|
Maybe<bool> found =
|
2015-12-02 10:19:38 +00:00
|
|
|
JSProxy::GetOwnPropertyDescriptor(isolate, owner, key, &desc);
|
2015-12-09 18:06:49 +00:00
|
|
|
MAYBE_RETURN(found, MaybeHandle<FixedArray>());
|
2016-06-28 13:32:18 +00:00
|
|
|
if (!found.FromJust()) continue;
|
|
|
|
if (!desc.enumerable()) {
|
2016-07-21 11:05:16 +00:00
|
|
|
accumulator->AddShadowingKey(key);
|
2016-06-28 13:32:18 +00:00
|
|
|
continue;
|
|
|
|
}
|
2015-12-02 10:19:38 +00:00
|
|
|
}
|
|
|
|
// Keep this key.
|
|
|
|
if (store_position != i) {
|
|
|
|
keys->set(store_position, *key);
|
|
|
|
}
|
|
|
|
store_position++;
|
|
|
|
}
|
|
|
|
if (store_position == 0) return isolate->factory()->empty_fixed_array();
|
|
|
|
keys->Shrink(store_position);
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
|
2015-12-09 18:06:49 +00:00
|
|
|
// Returns "nothing" in case of exception, "true" on success.
|
2016-05-10 14:26:21 +00:00
|
|
|
Maybe<bool> KeyAccumulator::AddKeysFromJSProxy(Handle<JSProxy> proxy,
|
|
|
|
Handle<FixedArray> keys) {
|
2016-08-01 10:08:54 +00:00
|
|
|
// Postpone the enumerable check for for-in to the ForInFilter step.
|
|
|
|
if (!is_for_in_) {
|
2016-03-23 08:26:35 +00:00
|
|
|
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
|
2016-06-28 13:32:18 +00:00
|
|
|
isolate_, keys, FilterProxyKeys(this, proxy, keys, filter_),
|
2016-03-23 08:26:35 +00:00
|
|
|
Nothing<bool>());
|
2016-08-01 10:08:54 +00:00
|
|
|
if (mode_ == KeyCollectionMode::kOwnOnly) {
|
|
|
|
// If we collect only the keys from a JSProxy do not sort or deduplicate.
|
|
|
|
keys_ = keys;
|
|
|
|
return Just(true);
|
|
|
|
}
|
2016-02-04 17:54:55 +00:00
|
|
|
}
|
2016-05-25 09:13:31 +00:00
|
|
|
AddKeys(keys, is_for_in_ ? CONVERT_TO_ARRAY_INDEX : DO_NOT_CONVERT);
|
2015-12-09 18:06:49 +00:00
|
|
|
return Just(true);
|
2015-11-28 15:02:59 +00:00
|
|
|
}
|
|
|
|
|
2016-05-06 10:50:41 +00:00
|
|
|
Maybe<bool> KeyAccumulator::CollectKeys(Handle<JSReceiver> receiver,
|
|
|
|
Handle<JSReceiver> object) {
|
2016-05-03 15:29:28 +00:00
|
|
|
// Proxies have no hidden prototype and we should not trigger the
|
|
|
|
// [[GetPrototypeOf]] trap on the last iteration when using
|
|
|
|
// AdvanceFollowingProxies.
|
2016-05-30 15:52:56 +00:00
|
|
|
if (mode_ == KeyCollectionMode::kOwnOnly && object->IsJSProxy()) {
|
2016-05-06 10:50:41 +00:00
|
|
|
MAYBE_RETURN(CollectOwnJSProxyKeys(receiver, Handle<JSProxy>::cast(object)),
|
|
|
|
Nothing<bool>());
|
2016-05-03 15:29:28 +00:00
|
|
|
return Just(true);
|
|
|
|
}
|
|
|
|
|
2016-05-30 15:52:56 +00:00
|
|
|
PrototypeIterator::WhereToEnd end = mode_ == KeyCollectionMode::kOwnOnly
|
2016-05-03 15:29:28 +00:00
|
|
|
? PrototypeIterator::END_AT_NON_HIDDEN
|
|
|
|
: PrototypeIterator::END_AT_NULL;
|
2016-06-08 14:43:22 +00:00
|
|
|
for (PrototypeIterator iter(isolate_, object, kStartAtReceiver, end);
|
2016-05-03 15:29:28 +00:00
|
|
|
!iter.IsAtEnd();) {
|
2016-07-21 11:05:16 +00:00
|
|
|
// Start the shadow checks only after the first prototype has added
|
|
|
|
// shadowing keys.
|
|
|
|
if (HasShadowingKeys()) skip_shadow_check_ = false;
|
2016-05-03 15:29:28 +00:00
|
|
|
Handle<JSReceiver> current =
|
|
|
|
PrototypeIterator::GetCurrent<JSReceiver>(iter);
|
|
|
|
Maybe<bool> result = Just(false); // Dummy initialization.
|
|
|
|
if (current->IsJSProxy()) {
|
2016-05-06 10:50:41 +00:00
|
|
|
result = CollectOwnJSProxyKeys(receiver, Handle<JSProxy>::cast(current));
|
2016-05-03 15:29:28 +00:00
|
|
|
} else {
|
|
|
|
DCHECK(current->IsJSObject());
|
2016-05-06 10:50:41 +00:00
|
|
|
result = CollectOwnKeys(receiver, Handle<JSObject>::cast(current));
|
2016-05-03 15:29:28 +00:00
|
|
|
}
|
|
|
|
MAYBE_RETURN(result, Nothing<bool>());
|
|
|
|
if (!result.FromJust()) break; // |false| means "stop iterating".
|
|
|
|
// Iterate through proxies but ignore access checks for the ALL_CAN_READ
|
2016-05-06 10:50:41 +00:00
|
|
|
// case on API objects for OWN_ONLY keys handled in CollectOwnKeys.
|
2016-05-03 15:29:28 +00:00
|
|
|
if (!iter.AdvanceFollowingProxiesIgnoringAccessChecks()) {
|
|
|
|
return Nothing<bool>();
|
|
|
|
}
|
2016-06-07 08:50:04 +00:00
|
|
|
if (!last_non_empty_prototype_.is_null() &&
|
|
|
|
*last_non_empty_prototype_ == *current) {
|
|
|
|
break;
|
|
|
|
}
|
2016-05-03 15:29:28 +00:00
|
|
|
}
|
|
|
|
return Just(true);
|
|
|
|
}
|
|
|
|
|
2016-07-21 11:05:16 +00:00
|
|
|
bool KeyAccumulator::HasShadowingKeys() { return !shadowing_keys_.is_null(); }
|
|
|
|
|
2016-06-28 13:32:18 +00:00
|
|
|
bool KeyAccumulator::IsShadowed(Handle<Object> key) {
|
2016-07-21 11:05:16 +00:00
|
|
|
if (!HasShadowingKeys() || skip_shadow_check_) return false;
|
|
|
|
return shadowing_keys_->Has(isolate_, key);
|
2016-06-28 13:32:18 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 11:05:16 +00:00
|
|
|
void KeyAccumulator::AddShadowingKey(Object* key) {
|
2016-06-28 13:32:18 +00:00
|
|
|
if (mode_ == KeyCollectionMode::kOwnOnly) return;
|
2016-07-21 11:05:16 +00:00
|
|
|
AddShadowingKey(handle(key, isolate_));
|
2016-06-28 13:32:18 +00:00
|
|
|
}
|
2016-07-21 11:05:16 +00:00
|
|
|
void KeyAccumulator::AddShadowingKey(Handle<Object> key) {
|
2016-06-28 13:32:18 +00:00
|
|
|
if (mode_ == KeyCollectionMode::kOwnOnly) return;
|
2016-07-21 11:05:16 +00:00
|
|
|
if (shadowing_keys_.is_null()) {
|
|
|
|
shadowing_keys_ = ObjectHashSet::New(isolate_, 16);
|
2016-06-28 13:32:18 +00:00
|
|
|
}
|
2016-07-21 11:05:16 +00:00
|
|
|
shadowing_keys_ = ObjectHashSet::Add(shadowing_keys_, key);
|
2016-06-28 13:32:18 +00:00
|
|
|
}
|
|
|
|
|
2016-03-07 19:25:12 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
void TrySettingEmptyEnumCache(JSReceiver* object) {
|
|
|
|
Map* map = object->map();
|
|
|
|
DCHECK_EQ(kInvalidEnumCacheSentinel, map->EnumLength());
|
|
|
|
if (!map->OnlyHasSimpleProperties()) return;
|
|
|
|
if (map->IsJSProxyMap()) return;
|
|
|
|
if (map->NumberOfOwnDescriptors() > 0) {
|
|
|
|
int number_of_enumerable_own_properties =
|
|
|
|
map->NumberOfDescribedProperties(OWN_DESCRIPTORS, ENUMERABLE_STRINGS);
|
|
|
|
if (number_of_enumerable_own_properties > 0) return;
|
|
|
|
}
|
|
|
|
DCHECK(object->IsJSObject());
|
|
|
|
map->SetEnumLength(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CheckAndInitalizeSimpleEnumCache(JSReceiver* object) {
|
|
|
|
if (object->map()->EnumLength() == kInvalidEnumCacheSentinel) {
|
|
|
|
TrySettingEmptyEnumCache(object);
|
|
|
|
}
|
|
|
|
if (object->map()->EnumLength() != 0) return false;
|
|
|
|
DCHECK(object->IsJSObject());
|
|
|
|
return !JSObject::cast(object)->HasEnumerableElements();
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void FastKeyAccumulator::Prepare() {
|
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
// Directly go for the fast path for OWN_ONLY keys.
|
2016-05-30 15:52:56 +00:00
|
|
|
if (mode_ == KeyCollectionMode::kOwnOnly) return;
|
2016-03-07 19:25:12 +00:00
|
|
|
// Fully walk the prototype chain and find the last prototype with keys.
|
|
|
|
is_receiver_simple_enum_ = false;
|
|
|
|
has_empty_prototype_ = true;
|
2016-06-07 08:50:04 +00:00
|
|
|
JSReceiver* last_prototype = nullptr;
|
2016-03-07 19:25:12 +00:00
|
|
|
for (PrototypeIterator iter(isolate_, *receiver_); !iter.IsAtEnd();
|
|
|
|
iter.Advance()) {
|
|
|
|
JSReceiver* current = iter.GetCurrent<JSReceiver>();
|
2016-06-07 08:50:04 +00:00
|
|
|
bool has_no_properties = CheckAndInitalizeSimpleEnumCache(current);
|
|
|
|
if (has_no_properties) continue;
|
|
|
|
last_prototype = current;
|
2016-03-07 19:25:12 +00:00
|
|
|
has_empty_prototype_ = false;
|
|
|
|
}
|
2016-06-07 08:50:04 +00:00
|
|
|
if (has_empty_prototype_) {
|
|
|
|
is_receiver_simple_enum_ =
|
|
|
|
receiver_->map()->EnumLength() != kInvalidEnumCacheSentinel &&
|
|
|
|
!JSObject::cast(*receiver_)->HasEnumerableElements();
|
|
|
|
} else if (last_prototype != nullptr) {
|
|
|
|
last_non_empty_prototype_ = handle(last_prototype, isolate_);
|
|
|
|
}
|
2016-03-07 19:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2016-05-03 15:29:28 +00:00
|
|
|
static Handle<FixedArray> ReduceFixedArrayTo(Isolate* isolate,
|
|
|
|
Handle<FixedArray> array,
|
|
|
|
int length) {
|
|
|
|
DCHECK_LE(length, array->length());
|
|
|
|
if (array->length() == length) return array;
|
|
|
|
return isolate->factory()->CopyFixedArrayUpTo(array, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
Handle<FixedArray> GetFastEnumPropertyKeys(Isolate* isolate,
|
|
|
|
Handle<JSObject> object) {
|
|
|
|
Handle<Map> map(object->map());
|
|
|
|
bool cache_enum_length = map->OnlyHasSimpleProperties();
|
|
|
|
|
|
|
|
Handle<DescriptorArray> descs =
|
|
|
|
Handle<DescriptorArray>(map->instance_descriptors(), isolate);
|
|
|
|
int own_property_count = map->EnumLength();
|
|
|
|
// If the enum length of the given map is set to kInvalidEnumCache, this
|
|
|
|
// means that the map itself has never used the present enum cache. The
|
|
|
|
// first step to using the cache is to set the enum length of the map by
|
|
|
|
// counting the number of own descriptors that are ENUMERABLE_STRINGS.
|
|
|
|
if (own_property_count == kInvalidEnumCacheSentinel) {
|
|
|
|
own_property_count =
|
|
|
|
map->NumberOfDescribedProperties(OWN_DESCRIPTORS, ENUMERABLE_STRINGS);
|
|
|
|
} else {
|
|
|
|
DCHECK(
|
|
|
|
own_property_count ==
|
|
|
|
map->NumberOfDescribedProperties(OWN_DESCRIPTORS, ENUMERABLE_STRINGS));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (descs->HasEnumCache()) {
|
|
|
|
Handle<FixedArray> keys(descs->GetEnumCache(), isolate);
|
|
|
|
// In case the number of properties required in the enum are actually
|
|
|
|
// present, we can reuse the enum cache. Otherwise, this means that the
|
|
|
|
// enum cache was generated for a previous (smaller) version of the
|
|
|
|
// Descriptor Array. In that case we regenerate the enum cache.
|
|
|
|
if (own_property_count <= keys->length()) {
|
|
|
|
isolate->counters()->enum_cache_hits()->Increment();
|
|
|
|
if (cache_enum_length) map->SetEnumLength(own_property_count);
|
|
|
|
return ReduceFixedArrayTo(isolate, keys, own_property_count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (descs->IsEmpty()) {
|
|
|
|
isolate->counters()->enum_cache_hits()->Increment();
|
|
|
|
if (cache_enum_length) map->SetEnumLength(0);
|
|
|
|
return isolate->factory()->empty_fixed_array();
|
|
|
|
}
|
|
|
|
|
|
|
|
isolate->counters()->enum_cache_misses()->Increment();
|
|
|
|
|
|
|
|
Handle<FixedArray> storage =
|
|
|
|
isolate->factory()->NewFixedArray(own_property_count);
|
|
|
|
Handle<FixedArray> indices =
|
|
|
|
isolate->factory()->NewFixedArray(own_property_count);
|
|
|
|
|
|
|
|
int size = map->NumberOfOwnDescriptors();
|
|
|
|
int index = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < size; i++) {
|
|
|
|
PropertyDetails details = descs->GetDetails(i);
|
|
|
|
if (details.IsDontEnum()) continue;
|
|
|
|
Object* key = descs->GetKey(i);
|
|
|
|
if (key->IsSymbol()) continue;
|
|
|
|
storage->set(index, key);
|
|
|
|
if (!indices.is_null()) {
|
2017-01-12 16:37:12 +00:00
|
|
|
if (details.location() == kField) {
|
|
|
|
DCHECK_EQ(kData, details.kind());
|
2016-05-03 15:29:28 +00:00
|
|
|
FieldIndex field_index = FieldIndex::ForDescriptor(*map, i);
|
|
|
|
int load_by_field_index = field_index.GetLoadByFieldIndex();
|
|
|
|
indices->set(index, Smi::FromInt(load_by_field_index));
|
2017-01-12 16:37:12 +00:00
|
|
|
} else {
|
|
|
|
indices = Handle<FixedArray>();
|
2016-05-03 15:29:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
DCHECK(index == storage->length());
|
|
|
|
|
|
|
|
DescriptorArray::SetEnumCache(descs, isolate, storage, indices);
|
|
|
|
if (cache_enum_length) {
|
|
|
|
map->SetEnumLength(own_property_count);
|
|
|
|
}
|
|
|
|
return storage;
|
|
|
|
}
|
2016-03-30 13:23:57 +00:00
|
|
|
|
|
|
|
template <bool fast_properties>
|
2016-08-05 12:52:02 +00:00
|
|
|
MaybeHandle<FixedArray> GetOwnKeysWithElements(Isolate* isolate,
|
|
|
|
Handle<JSObject> object,
|
|
|
|
GetKeysConversion convert) {
|
2016-03-30 13:23:57 +00:00
|
|
|
Handle<FixedArray> keys;
|
2016-03-07 19:25:12 +00:00
|
|
|
ElementsAccessor* accessor = object->GetElementsAccessor();
|
2016-03-30 13:23:57 +00:00
|
|
|
if (fast_properties) {
|
2016-05-03 15:29:28 +00:00
|
|
|
keys = GetFastEnumPropertyKeys(isolate, object);
|
2016-03-30 13:23:57 +00:00
|
|
|
} else {
|
|
|
|
// TODO(cbruni): preallocate big enough array to also hold elements.
|
2016-06-28 13:32:18 +00:00
|
|
|
keys = KeyAccumulator::GetOwnEnumPropertyKeys(isolate, object);
|
2016-03-30 13:23:57 +00:00
|
|
|
}
|
2016-08-05 12:52:02 +00:00
|
|
|
MaybeHandle<FixedArray> result =
|
2016-03-10 15:02:15 +00:00
|
|
|
accessor->PrependElementIndices(object, keys, convert, ONLY_ENUMERABLE);
|
|
|
|
|
|
|
|
if (FLAG_trace_for_in_enumerate) {
|
|
|
|
PrintF("| strings=%d symbols=0 elements=%u || prototypes>=1 ||\n",
|
2016-08-05 12:52:02 +00:00
|
|
|
keys->length(), result.ToHandleChecked()->length() - keys->length());
|
2016-03-10 15:02:15 +00:00
|
|
|
}
|
|
|
|
return result;
|
2016-03-07 19:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MaybeHandle<FixedArray> GetOwnKeysWithUninitializedEnumCache(
|
|
|
|
Isolate* isolate, Handle<JSObject> object) {
|
|
|
|
// Uninitalized enum cache
|
|
|
|
Map* map = object->map();
|
|
|
|
if (object->elements() != isolate->heap()->empty_fixed_array() ||
|
|
|
|
object->elements() != isolate->heap()->empty_slow_element_dictionary()) {
|
|
|
|
// Assume that there are elements.
|
|
|
|
return MaybeHandle<FixedArray>();
|
|
|
|
}
|
|
|
|
int number_of_own_descriptors = map->NumberOfOwnDescriptors();
|
|
|
|
if (number_of_own_descriptors == 0) {
|
|
|
|
map->SetEnumLength(0);
|
|
|
|
return isolate->factory()->empty_fixed_array();
|
|
|
|
}
|
|
|
|
// We have no elements but possibly enumerable property keys, hence we can
|
|
|
|
// directly initialize the enum cache.
|
2016-05-03 15:29:28 +00:00
|
|
|
return GetFastEnumPropertyKeys(isolate, object);
|
2016-03-07 19:25:12 +00:00
|
|
|
}
|
|
|
|
|
2016-03-30 13:23:57 +00:00
|
|
|
bool OnlyHasSimpleProperties(Map* map) {
|
|
|
|
return map->instance_type() > LAST_CUSTOM_ELEMENTS_RECEIVER;
|
|
|
|
}
|
|
|
|
|
2016-03-07 19:25:12 +00:00
|
|
|
} // namespace
|
|
|
|
|
2016-06-07 08:50:04 +00:00
|
|
|
MaybeHandle<FixedArray> FastKeyAccumulator::GetKeys(
|
|
|
|
GetKeysConversion keys_conversion) {
|
2016-08-05 12:52:02 +00:00
|
|
|
if (filter_ == ENUMERABLE_STRINGS) {
|
|
|
|
Handle<FixedArray> keys;
|
|
|
|
if (GetKeysFast(keys_conversion).ToHandle(&keys)) {
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
if (isolate_->has_pending_exception()) return MaybeHandle<FixedArray>();
|
2016-03-07 19:25:12 +00:00
|
|
|
}
|
2016-08-05 12:52:02 +00:00
|
|
|
|
2016-06-07 08:50:04 +00:00
|
|
|
return GetKeysSlow(keys_conversion);
|
2016-03-07 19:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MaybeHandle<FixedArray> FastKeyAccumulator::GetKeysFast(
|
2016-06-07 08:50:04 +00:00
|
|
|
GetKeysConversion keys_conversion) {
|
2016-05-30 15:52:56 +00:00
|
|
|
bool own_only = has_empty_prototype_ || mode_ == KeyCollectionMode::kOwnOnly;
|
2016-03-30 13:23:57 +00:00
|
|
|
Map* map = receiver_->map();
|
|
|
|
if (!own_only || !OnlyHasSimpleProperties(map)) {
|
2016-03-07 19:25:12 +00:00
|
|
|
return MaybeHandle<FixedArray>();
|
|
|
|
}
|
|
|
|
|
2016-03-30 13:23:57 +00:00
|
|
|
// From this point on we are certiain to only collect own keys.
|
2016-03-07 19:25:12 +00:00
|
|
|
DCHECK(receiver_->IsJSObject());
|
|
|
|
Handle<JSObject> object = Handle<JSObject>::cast(receiver_);
|
|
|
|
|
2016-03-30 13:23:57 +00:00
|
|
|
// Do not try to use the enum-cache for dict-mode objects.
|
|
|
|
if (map->is_dictionary_map()) {
|
2016-06-07 08:50:04 +00:00
|
|
|
return GetOwnKeysWithElements<false>(isolate_, object, keys_conversion);
|
2016-03-30 13:23:57 +00:00
|
|
|
}
|
2016-03-07 19:25:12 +00:00
|
|
|
int enum_length = receiver_->map()->EnumLength();
|
|
|
|
if (enum_length == kInvalidEnumCacheSentinel) {
|
2016-03-30 13:23:57 +00:00
|
|
|
Handle<FixedArray> keys;
|
2016-03-07 19:25:12 +00:00
|
|
|
// Try initializing the enum cache and return own properties.
|
|
|
|
if (GetOwnKeysWithUninitializedEnumCache(isolate_, object)
|
|
|
|
.ToHandle(&keys)) {
|
2016-03-10 15:02:15 +00:00
|
|
|
if (FLAG_trace_for_in_enumerate) {
|
|
|
|
PrintF("| strings=%d symbols=0 elements=0 || prototypes>=1 ||\n",
|
|
|
|
keys->length());
|
|
|
|
}
|
2016-03-07 19:25:12 +00:00
|
|
|
is_receiver_simple_enum_ =
|
|
|
|
object->map()->EnumLength() != kInvalidEnumCacheSentinel;
|
|
|
|
return keys;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// The properties-only case failed because there were probably elements on the
|
|
|
|
// receiver.
|
2016-06-07 08:50:04 +00:00
|
|
|
return GetOwnKeysWithElements<true>(isolate_, object, keys_conversion);
|
2016-03-07 19:25:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MaybeHandle<FixedArray> FastKeyAccumulator::GetKeysSlow(
|
2016-06-07 08:50:04 +00:00
|
|
|
GetKeysConversion keys_conversion) {
|
|
|
|
KeyAccumulator accumulator(isolate_, mode_, filter_);
|
|
|
|
accumulator.set_is_for_in(is_for_in_);
|
|
|
|
accumulator.set_last_non_empty_prototype(last_non_empty_prototype_);
|
|
|
|
|
|
|
|
MAYBE_RETURN(accumulator.CollectKeys(receiver_, receiver_),
|
|
|
|
MaybeHandle<FixedArray>());
|
|
|
|
return accumulator.GetKeys(keys_conversion);
|
2016-03-07 19:25:12 +00:00
|
|
|
}
|
2015-11-04 14:05:21 +00:00
|
|
|
|
2016-05-25 09:13:31 +00:00
|
|
|
namespace {
|
|
|
|
|
2016-05-03 15:29:28 +00:00
|
|
|
enum IndexedOrNamed { kIndexed, kNamed };
|
|
|
|
|
|
|
|
// Returns |true| on success, |nothing| on exception.
|
2016-06-27 11:48:04 +00:00
|
|
|
template <class Callback, IndexedOrNamed type>
|
|
|
|
Maybe<bool> CollectInterceptorKeysInternal(Handle<JSReceiver> receiver,
|
|
|
|
Handle<JSObject> object,
|
|
|
|
Handle<InterceptorInfo> interceptor,
|
|
|
|
KeyAccumulator* accumulator) {
|
|
|
|
Isolate* isolate = accumulator->isolate();
|
|
|
|
PropertyCallbackArguments args(isolate, interceptor->data(), *receiver,
|
|
|
|
*object, Object::DONT_THROW);
|
|
|
|
Handle<JSObject> result;
|
|
|
|
if (!interceptor->enumerator()->IsUndefined(isolate)) {
|
|
|
|
Callback enum_fun = v8::ToCData<Callback>(interceptor->enumerator());
|
|
|
|
const char* log_tag = type == kIndexed ? "interceptor-indexed-enum"
|
|
|
|
: "interceptor-named-enum";
|
|
|
|
LOG(isolate, ApiObjectAccess(log_tag, *object));
|
|
|
|
result = args.Call(enum_fun);
|
|
|
|
}
|
|
|
|
RETURN_VALUE_IF_SCHEDULED_EXCEPTION(isolate, Nothing<bool>());
|
|
|
|
if (result.is_null()) return Just(true);
|
|
|
|
accumulator->AddKeys(
|
|
|
|
result, type == kIndexed ? CONVERT_TO_ARRAY_INDEX : DO_NOT_CONVERT);
|
|
|
|
return Just(true);
|
|
|
|
}
|
|
|
|
|
2016-05-03 15:29:28 +00:00
|
|
|
template <class Callback, IndexedOrNamed type>
|
2016-06-07 08:50:04 +00:00
|
|
|
Maybe<bool> CollectInterceptorKeys(Handle<JSReceiver> receiver,
|
2016-05-25 09:13:31 +00:00
|
|
|
Handle<JSObject> object,
|
|
|
|
KeyAccumulator* accumulator) {
|
2016-05-03 15:29:28 +00:00
|
|
|
Isolate* isolate = accumulator->isolate();
|
|
|
|
if (type == kIndexed) {
|
|
|
|
if (!object->HasIndexedInterceptor()) return Just(true);
|
|
|
|
} else {
|
|
|
|
if (!object->HasNamedInterceptor()) return Just(true);
|
|
|
|
}
|
|
|
|
Handle<InterceptorInfo> interceptor(type == kIndexed
|
|
|
|
? object->GetIndexedInterceptor()
|
|
|
|
: object->GetNamedInterceptor(),
|
|
|
|
isolate);
|
|
|
|
if ((accumulator->filter() & ONLY_ALL_CAN_READ) &&
|
|
|
|
!interceptor->all_can_read()) {
|
|
|
|
return Just(true);
|
|
|
|
}
|
2016-06-27 11:48:04 +00:00
|
|
|
return CollectInterceptorKeysInternal<Callback, type>(
|
|
|
|
receiver, object, interceptor, accumulator);
|
2016-05-03 15:29:28 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 09:13:31 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
Maybe<bool> KeyAccumulator::CollectOwnElementIndices(
|
|
|
|
Handle<JSReceiver> receiver, Handle<JSObject> object) {
|
|
|
|
if (filter_ & SKIP_STRINGS || skip_indices_) return Just(true);
|
|
|
|
|
2016-05-03 15:29:28 +00:00
|
|
|
ElementsAccessor* accessor = object->GetElementsAccessor();
|
2016-05-06 10:50:41 +00:00
|
|
|
accessor->CollectElementIndices(object, this);
|
2016-05-25 09:13:31 +00:00
|
|
|
|
2016-06-07 08:50:04 +00:00
|
|
|
return CollectInterceptorKeys<v8::IndexedPropertyEnumeratorCallback,
|
2016-05-25 09:13:31 +00:00
|
|
|
kIndexed>(receiver, object, this);
|
2016-05-03 15:29:28 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 09:13:31 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
template <bool skip_symbols>
|
|
|
|
int CollectOwnPropertyNamesInternal(Handle<JSObject> object,
|
|
|
|
KeyAccumulator* keys,
|
|
|
|
Handle<DescriptorArray> descs,
|
|
|
|
int start_index, int limit) {
|
|
|
|
int first_skipped = -1;
|
2016-06-28 13:32:18 +00:00
|
|
|
PropertyFilter filter = keys->filter();
|
|
|
|
KeyCollectionMode mode = keys->mode();
|
2016-05-25 09:13:31 +00:00
|
|
|
for (int i = start_index; i < limit; i++) {
|
2016-06-28 13:32:18 +00:00
|
|
|
bool is_shadowing_key = false;
|
2016-05-25 09:13:31 +00:00
|
|
|
PropertyDetails details = descs->GetDetails(i);
|
2016-06-28 13:32:18 +00:00
|
|
|
|
|
|
|
if ((details.attributes() & filter) != 0) {
|
|
|
|
if (mode == KeyCollectionMode::kIncludePrototypes) {
|
|
|
|
is_shadowing_key = true;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filter & ONLY_ALL_CAN_READ) {
|
2016-05-25 09:13:31 +00:00
|
|
|
if (details.kind() != kAccessor) continue;
|
|
|
|
Object* accessors = descs->GetValue(i);
|
|
|
|
if (!accessors->IsAccessorInfo()) continue;
|
|
|
|
if (!AccessorInfo::cast(accessors)->all_can_read()) continue;
|
2016-05-24 16:40:09 +00:00
|
|
|
}
|
2016-06-28 13:32:18 +00:00
|
|
|
|
2016-05-25 09:13:31 +00:00
|
|
|
Name* key = descs->GetKey(i);
|
|
|
|
if (skip_symbols == key->IsSymbol()) {
|
|
|
|
if (first_skipped == -1) first_skipped = i;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (key->FilterKey(keys->filter())) continue;
|
2016-06-28 13:32:18 +00:00
|
|
|
|
|
|
|
if (is_shadowing_key) {
|
2016-07-21 11:05:16 +00:00
|
|
|
keys->AddShadowingKey(key);
|
2016-06-28 13:32:18 +00:00
|
|
|
} else {
|
|
|
|
keys->AddKey(key, DO_NOT_CONVERT);
|
|
|
|
}
|
2016-05-25 09:13:31 +00:00
|
|
|
}
|
|
|
|
return first_skipped;
|
|
|
|
}
|
|
|
|
|
2016-06-28 13:32:18 +00:00
|
|
|
template <class T>
|
|
|
|
Handle<FixedArray> GetOwnEnumPropertyDictionaryKeys(Isolate* isolate,
|
|
|
|
KeyCollectionMode mode,
|
|
|
|
KeyAccumulator* accumulator,
|
|
|
|
Handle<JSObject> object,
|
|
|
|
T* raw_dictionary) {
|
|
|
|
Handle<T> dictionary(raw_dictionary, isolate);
|
|
|
|
int length = dictionary->NumberOfEnumElements();
|
|
|
|
if (length == 0) {
|
|
|
|
return isolate->factory()->empty_fixed_array();
|
|
|
|
}
|
|
|
|
Handle<FixedArray> storage = isolate->factory()->NewFixedArray(length);
|
|
|
|
T::CopyEnumKeysTo(dictionary, storage, mode, accumulator);
|
|
|
|
return storage;
|
|
|
|
}
|
2016-05-25 09:13:31 +00:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
Maybe<bool> KeyAccumulator::CollectOwnPropertyNames(Handle<JSReceiver> receiver,
|
|
|
|
Handle<JSObject> object) {
|
|
|
|
if (filter_ == ENUMERABLE_STRINGS) {
|
2016-06-28 13:32:18 +00:00
|
|
|
Handle<FixedArray> enum_keys;
|
|
|
|
if (object->HasFastProperties()) {
|
|
|
|
enum_keys = KeyAccumulator::GetOwnEnumPropertyKeys(isolate_, object);
|
|
|
|
// If the number of properties equals the length of enumerable properties
|
|
|
|
// we do not have to filter out non-enumerable ones
|
|
|
|
Map* map = object->map();
|
|
|
|
int nof_descriptors = map->NumberOfOwnDescriptors();
|
|
|
|
if (enum_keys->length() != nof_descriptors) {
|
|
|
|
Handle<DescriptorArray> descs =
|
|
|
|
Handle<DescriptorArray>(map->instance_descriptors(), isolate_);
|
|
|
|
for (int i = 0; i < nof_descriptors; i++) {
|
|
|
|
PropertyDetails details = descs->GetDetails(i);
|
|
|
|
if (!details.IsDontEnum()) continue;
|
|
|
|
Object* key = descs->GetKey(i);
|
2016-07-21 11:05:16 +00:00
|
|
|
this->AddShadowingKey(key);
|
2016-06-28 13:32:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (object->IsJSGlobalObject()) {
|
|
|
|
enum_keys = GetOwnEnumPropertyDictionaryKeys(
|
|
|
|
isolate_, mode_, this, object, object->global_dictionary());
|
|
|
|
} else {
|
|
|
|
enum_keys = GetOwnEnumPropertyDictionaryKeys(
|
|
|
|
isolate_, mode_, this, object, object->property_dictionary());
|
|
|
|
}
|
2016-05-25 09:13:31 +00:00
|
|
|
AddKeys(enum_keys, DO_NOT_CONVERT);
|
2016-05-24 17:35:53 +00:00
|
|
|
} else {
|
2016-05-25 09:13:31 +00:00
|
|
|
if (object->HasFastProperties()) {
|
|
|
|
int limit = object->map()->NumberOfOwnDescriptors();
|
|
|
|
Handle<DescriptorArray> descs(object->map()->instance_descriptors(),
|
|
|
|
isolate_);
|
|
|
|
// First collect the strings,
|
|
|
|
int first_symbol =
|
|
|
|
CollectOwnPropertyNamesInternal<true>(object, this, descs, 0, limit);
|
|
|
|
// then the symbols.
|
|
|
|
if (first_symbol != -1) {
|
|
|
|
CollectOwnPropertyNamesInternal<false>(object, this, descs,
|
|
|
|
first_symbol, limit);
|
|
|
|
}
|
|
|
|
} else if (object->IsJSGlobalObject()) {
|
|
|
|
GlobalDictionary::CollectKeysTo(
|
2016-06-28 13:32:18 +00:00
|
|
|
handle(object->global_dictionary(), isolate_), this);
|
2016-05-25 09:13:31 +00:00
|
|
|
} else {
|
|
|
|
NameDictionary::CollectKeysTo(
|
2016-06-28 13:32:18 +00:00
|
|
|
handle(object->property_dictionary(), isolate_), this);
|
2016-05-25 09:13:31 +00:00
|
|
|
}
|
2016-05-03 15:29:28 +00:00
|
|
|
}
|
2016-05-25 09:13:31 +00:00
|
|
|
// Add the property keys from the interceptor.
|
2016-06-07 08:50:04 +00:00
|
|
|
return CollectInterceptorKeys<v8::GenericNamedPropertyEnumeratorCallback,
|
2016-05-25 09:13:31 +00:00
|
|
|
kNamed>(receiver, object, this);
|
2016-05-03 15:29:28 +00:00
|
|
|
}
|
|
|
|
|
2016-06-27 11:48:04 +00:00
|
|
|
Maybe<bool> KeyAccumulator::CollectAccessCheckInterceptorKeys(
|
|
|
|
Handle<AccessCheckInfo> access_check_info, Handle<JSReceiver> receiver,
|
|
|
|
Handle<JSObject> object) {
|
|
|
|
MAYBE_RETURN(
|
|
|
|
(CollectInterceptorKeysInternal<v8::IndexedPropertyEnumeratorCallback,
|
|
|
|
kIndexed>(
|
|
|
|
receiver, object,
|
|
|
|
handle(
|
|
|
|
InterceptorInfo::cast(access_check_info->indexed_interceptor()),
|
|
|
|
isolate_),
|
|
|
|
this)),
|
|
|
|
Nothing<bool>());
|
|
|
|
MAYBE_RETURN(
|
|
|
|
(CollectInterceptorKeysInternal<
|
|
|
|
v8::GenericNamedPropertyEnumeratorCallback, kNamed>(
|
|
|
|
receiver, object,
|
|
|
|
handle(InterceptorInfo::cast(access_check_info->named_interceptor()),
|
|
|
|
isolate_),
|
|
|
|
this)),
|
|
|
|
Nothing<bool>());
|
|
|
|
return Just(true);
|
|
|
|
}
|
|
|
|
|
2016-05-03 15:29:28 +00:00
|
|
|
// Returns |true| on success, |false| if prototype walking should be stopped,
|
|
|
|
// |nothing| if an exception was thrown.
|
2016-05-06 10:50:41 +00:00
|
|
|
Maybe<bool> KeyAccumulator::CollectOwnKeys(Handle<JSReceiver> receiver,
|
|
|
|
Handle<JSObject> object) {
|
2016-05-03 15:29:28 +00:00
|
|
|
// Check access rights if required.
|
|
|
|
if (object->IsAccessCheckNeeded() &&
|
|
|
|
!isolate_->MayAccess(handle(isolate_->context()), object)) {
|
|
|
|
// The cross-origin spec says that [[Enumerate]] shall return an empty
|
|
|
|
// iterator when it doesn't have access...
|
2016-05-30 15:52:56 +00:00
|
|
|
if (mode_ == KeyCollectionMode::kIncludePrototypes) {
|
2016-05-03 15:29:28 +00:00
|
|
|
return Just(false);
|
|
|
|
}
|
|
|
|
// ...whereas [[OwnPropertyKeys]] shall return whitelisted properties.
|
2016-05-30 15:52:56 +00:00
|
|
|
DCHECK(KeyCollectionMode::kOwnOnly == mode_);
|
2016-06-27 11:48:04 +00:00
|
|
|
Handle<AccessCheckInfo> access_check_info;
|
|
|
|
{
|
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
AccessCheckInfo* maybe_info = AccessCheckInfo::Get(isolate_, object);
|
|
|
|
if (maybe_info) access_check_info = handle(maybe_info, isolate_);
|
|
|
|
}
|
|
|
|
// We always have both kinds of interceptors or none.
|
|
|
|
if (!access_check_info.is_null() &&
|
|
|
|
access_check_info->named_interceptor()) {
|
|
|
|
MAYBE_RETURN(CollectAccessCheckInterceptorKeys(access_check_info,
|
|
|
|
receiver, object),
|
|
|
|
Nothing<bool>());
|
|
|
|
return Just(false);
|
|
|
|
}
|
2016-05-03 15:29:28 +00:00
|
|
|
filter_ = static_cast<PropertyFilter>(filter_ | ONLY_ALL_CAN_READ);
|
|
|
|
}
|
2016-05-25 09:13:31 +00:00
|
|
|
MAYBE_RETURN(CollectOwnElementIndices(receiver, object), Nothing<bool>());
|
|
|
|
MAYBE_RETURN(CollectOwnPropertyNames(receiver, object), Nothing<bool>());
|
2016-05-03 15:29:28 +00:00
|
|
|
return Just(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2016-06-28 13:32:18 +00:00
|
|
|
Handle<FixedArray> KeyAccumulator::GetOwnEnumPropertyKeys(
|
2016-05-03 15:29:28 +00:00
|
|
|
Isolate* isolate, Handle<JSObject> object) {
|
|
|
|
if (object->HasFastProperties()) {
|
|
|
|
return GetFastEnumPropertyKeys(isolate, object);
|
|
|
|
} else if (object->IsJSGlobalObject()) {
|
2016-06-28 13:32:18 +00:00
|
|
|
return GetOwnEnumPropertyDictionaryKeys(
|
|
|
|
isolate, KeyCollectionMode::kOwnOnly, nullptr, object,
|
|
|
|
object->global_dictionary());
|
2016-05-03 15:29:28 +00:00
|
|
|
} else {
|
2016-06-28 13:32:18 +00:00
|
|
|
return GetOwnEnumPropertyDictionaryKeys(
|
|
|
|
isolate, KeyCollectionMode::kOwnOnly, nullptr, object,
|
|
|
|
object->property_dictionary());
|
2016-05-03 15:29:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ES6 9.5.12
|
|
|
|
// Returns |true| on success, |nothing| in case of exception.
|
2016-05-06 10:50:41 +00:00
|
|
|
Maybe<bool> KeyAccumulator::CollectOwnJSProxyKeys(Handle<JSReceiver> receiver,
|
|
|
|
Handle<JSProxy> proxy) {
|
2016-05-03 15:29:28 +00:00
|
|
|
STACK_CHECK(isolate_, Nothing<bool>());
|
|
|
|
// 1. Let handler be the value of the [[ProxyHandler]] internal slot of O.
|
|
|
|
Handle<Object> handler(proxy->handler(), isolate_);
|
|
|
|
// 2. If handler is null, throw a TypeError exception.
|
|
|
|
// 3. Assert: Type(handler) is Object.
|
|
|
|
if (proxy->IsRevoked()) {
|
|
|
|
isolate_->Throw(*isolate_->factory()->NewTypeError(
|
|
|
|
MessageTemplate::kProxyRevoked, isolate_->factory()->ownKeys_string()));
|
|
|
|
return Nothing<bool>();
|
|
|
|
}
|
|
|
|
// 4. Let target be the value of the [[ProxyTarget]] internal slot of O.
|
|
|
|
Handle<JSReceiver> target(proxy->target(), isolate_);
|
|
|
|
// 5. Let trap be ? GetMethod(handler, "ownKeys").
|
|
|
|
Handle<Object> trap;
|
|
|
|
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
|
|
|
|
isolate_, trap, Object::GetMethod(Handle<JSReceiver>::cast(handler),
|
|
|
|
isolate_->factory()->ownKeys_string()),
|
|
|
|
Nothing<bool>());
|
|
|
|
// 6. If trap is undefined, then
|
2016-06-06 12:58:10 +00:00
|
|
|
if (trap->IsUndefined(isolate_)) {
|
2016-05-03 15:29:28 +00:00
|
|
|
// 6a. Return target.[[OwnPropertyKeys]]().
|
2016-05-10 14:26:21 +00:00
|
|
|
return CollectOwnJSProxyTargetKeys(proxy, target);
|
2016-05-03 15:29:28 +00:00
|
|
|
}
|
|
|
|
// 7. Let trapResultArray be Call(trap, handler, «target»).
|
|
|
|
Handle<Object> trap_result_array;
|
|
|
|
Handle<Object> args[] = {target};
|
|
|
|
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
|
|
|
|
isolate_, trap_result_array,
|
|
|
|
Execution::Call(isolate_, trap, handler, arraysize(args), args),
|
|
|
|
Nothing<bool>());
|
|
|
|
// 8. Let trapResult be ? CreateListFromArrayLike(trapResultArray,
|
|
|
|
// «String, Symbol»).
|
|
|
|
Handle<FixedArray> trap_result;
|
|
|
|
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
|
|
|
|
isolate_, trap_result,
|
|
|
|
Object::CreateListFromArrayLike(isolate_, trap_result_array,
|
|
|
|
ElementTypes::kStringAndSymbol),
|
|
|
|
Nothing<bool>());
|
|
|
|
// 9. Let extensibleTarget be ? IsExtensible(target).
|
|
|
|
Maybe<bool> maybe_extensible = JSReceiver::IsExtensible(target);
|
|
|
|
MAYBE_RETURN(maybe_extensible, Nothing<bool>());
|
|
|
|
bool extensible_target = maybe_extensible.FromJust();
|
|
|
|
// 10. Let targetKeys be ? target.[[OwnPropertyKeys]]().
|
|
|
|
Handle<FixedArray> target_keys;
|
|
|
|
ASSIGN_RETURN_ON_EXCEPTION_VALUE(isolate_, target_keys,
|
|
|
|
JSReceiver::OwnPropertyKeys(target),
|
|
|
|
Nothing<bool>());
|
|
|
|
// 11. (Assert)
|
|
|
|
// 12. Let targetConfigurableKeys be an empty List.
|
|
|
|
// To save memory, we're re-using target_keys and will modify it in-place.
|
|
|
|
Handle<FixedArray> target_configurable_keys = target_keys;
|
|
|
|
// 13. Let targetNonconfigurableKeys be an empty List.
|
|
|
|
Handle<FixedArray> target_nonconfigurable_keys =
|
|
|
|
isolate_->factory()->NewFixedArray(target_keys->length());
|
|
|
|
int nonconfigurable_keys_length = 0;
|
|
|
|
// 14. Repeat, for each element key of targetKeys:
|
|
|
|
for (int i = 0; i < target_keys->length(); ++i) {
|
|
|
|
// 14a. Let desc be ? target.[[GetOwnProperty]](key).
|
|
|
|
PropertyDescriptor desc;
|
|
|
|
Maybe<bool> found = JSReceiver::GetOwnPropertyDescriptor(
|
|
|
|
isolate_, target, handle(target_keys->get(i), isolate_), &desc);
|
|
|
|
MAYBE_RETURN(found, Nothing<bool>());
|
|
|
|
// 14b. If desc is not undefined and desc.[[Configurable]] is false, then
|
|
|
|
if (found.FromJust() && !desc.configurable()) {
|
|
|
|
// 14b i. Append key as an element of targetNonconfigurableKeys.
|
|
|
|
target_nonconfigurable_keys->set(nonconfigurable_keys_length,
|
|
|
|
target_keys->get(i));
|
|
|
|
nonconfigurable_keys_length++;
|
|
|
|
// The key was moved, null it out in the original list.
|
2016-10-07 13:05:07 +00:00
|
|
|
target_keys->set(i, Smi::kZero);
|
2016-05-03 15:29:28 +00:00
|
|
|
} else {
|
|
|
|
// 14c. Else,
|
|
|
|
// 14c i. Append key as an element of targetConfigurableKeys.
|
|
|
|
// (No-op, just keep it in |target_keys|.)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 15. If extensibleTarget is true and targetNonconfigurableKeys is empty,
|
|
|
|
// then:
|
|
|
|
if (extensible_target && nonconfigurable_keys_length == 0) {
|
|
|
|
// 15a. Return trapResult.
|
2016-05-10 14:26:21 +00:00
|
|
|
return AddKeysFromJSProxy(proxy, trap_result);
|
2016-05-03 15:29:28 +00:00
|
|
|
}
|
|
|
|
// 16. Let uncheckedResultKeys be a new List which is a copy of trapResult.
|
2016-10-17 12:12:30 +00:00
|
|
|
Zone set_zone(isolate_->allocator(), ZONE_NAME);
|
2016-05-03 15:29:28 +00:00
|
|
|
const int kPresent = 1;
|
|
|
|
const int kGone = 0;
|
|
|
|
IdentityMap<int> unchecked_result_keys(isolate_->heap(), &set_zone);
|
|
|
|
int unchecked_result_keys_size = 0;
|
|
|
|
for (int i = 0; i < trap_result->length(); ++i) {
|
|
|
|
DCHECK(trap_result->get(i)->IsUniqueName());
|
|
|
|
Object* key = trap_result->get(i);
|
|
|
|
int* entry = unchecked_result_keys.Get(key);
|
|
|
|
if (*entry != kPresent) {
|
|
|
|
*entry = kPresent;
|
|
|
|
unchecked_result_keys_size++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// 17. Repeat, for each key that is an element of targetNonconfigurableKeys:
|
|
|
|
for (int i = 0; i < nonconfigurable_keys_length; ++i) {
|
|
|
|
Object* key = target_nonconfigurable_keys->get(i);
|
|
|
|
// 17a. If key is not an element of uncheckedResultKeys, throw a
|
|
|
|
// TypeError exception.
|
|
|
|
int* found = unchecked_result_keys.Find(key);
|
|
|
|
if (found == nullptr || *found == kGone) {
|
|
|
|
isolate_->Throw(*isolate_->factory()->NewTypeError(
|
|
|
|
MessageTemplate::kProxyOwnKeysMissing, handle(key, isolate_)));
|
|
|
|
return Nothing<bool>();
|
|
|
|
}
|
|
|
|
// 17b. Remove key from uncheckedResultKeys.
|
|
|
|
*found = kGone;
|
|
|
|
unchecked_result_keys_size--;
|
|
|
|
}
|
|
|
|
// 18. If extensibleTarget is true, return trapResult.
|
|
|
|
if (extensible_target) {
|
2016-05-10 14:26:21 +00:00
|
|
|
return AddKeysFromJSProxy(proxy, trap_result);
|
2016-05-03 15:29:28 +00:00
|
|
|
}
|
|
|
|
// 19. Repeat, for each key that is an element of targetConfigurableKeys:
|
|
|
|
for (int i = 0; i < target_configurable_keys->length(); ++i) {
|
|
|
|
Object* key = target_configurable_keys->get(i);
|
|
|
|
if (key->IsSmi()) continue; // Zapped entry, was nonconfigurable.
|
|
|
|
// 19a. If key is not an element of uncheckedResultKeys, throw a
|
|
|
|
// TypeError exception.
|
|
|
|
int* found = unchecked_result_keys.Find(key);
|
|
|
|
if (found == nullptr || *found == kGone) {
|
|
|
|
isolate_->Throw(*isolate_->factory()->NewTypeError(
|
|
|
|
MessageTemplate::kProxyOwnKeysMissing, handle(key, isolate_)));
|
|
|
|
return Nothing<bool>();
|
|
|
|
}
|
|
|
|
// 19b. Remove key from uncheckedResultKeys.
|
|
|
|
*found = kGone;
|
|
|
|
unchecked_result_keys_size--;
|
|
|
|
}
|
|
|
|
// 20. If uncheckedResultKeys is not empty, throw a TypeError exception.
|
|
|
|
if (unchecked_result_keys_size != 0) {
|
|
|
|
DCHECK_GT(unchecked_result_keys_size, 0);
|
|
|
|
isolate_->Throw(*isolate_->factory()->NewTypeError(
|
|
|
|
MessageTemplate::kProxyOwnKeysNonExtensible));
|
|
|
|
return Nothing<bool>();
|
|
|
|
}
|
|
|
|
// 21. Return trapResult.
|
2016-05-10 14:26:21 +00:00
|
|
|
return AddKeysFromJSProxy(proxy, trap_result);
|
|
|
|
}
|
|
|
|
|
|
|
|
Maybe<bool> KeyAccumulator::CollectOwnJSProxyTargetKeys(
|
|
|
|
Handle<JSProxy> proxy, Handle<JSReceiver> target) {
|
|
|
|
// TODO(cbruni): avoid creating another KeyAccumulator
|
|
|
|
Handle<FixedArray> keys;
|
|
|
|
ASSIGN_RETURN_ON_EXCEPTION_VALUE(
|
2016-07-11 10:38:33 +00:00
|
|
|
isolate_, keys,
|
|
|
|
KeyAccumulator::GetKeys(target, KeyCollectionMode::kOwnOnly, filter_,
|
2016-08-01 10:08:54 +00:00
|
|
|
GetKeysConversion::kConvertToString, is_for_in_),
|
2016-07-11 10:38:33 +00:00
|
|
|
Nothing<bool>());
|
2016-05-10 14:26:21 +00:00
|
|
|
Maybe<bool> result = AddKeysFromJSProxy(proxy, keys);
|
|
|
|
return result;
|
2016-05-03 15:29:28 +00:00
|
|
|
}
|
|
|
|
|
2015-11-04 14:05:21 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|