2015-11-04 14:05:21 +00:00
|
|
|
// Copyright 2012 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-05-03 15:29:28 +00:00
|
|
|
#ifndef V8_KEYS_H_
|
|
|
|
#define V8_KEYS_H_
|
2015-11-04 14:05:21 +00:00
|
|
|
|
|
|
|
#include "src/isolate.h"
|
|
|
|
#include "src/objects.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2016-05-25 09:13:31 +00:00
|
|
|
enum AddKeyConversion { DO_NOT_CONVERT, CONVERT_TO_ARRAY_INDEX };
|
2015-11-04 14:05:21 +00:00
|
|
|
|
|
|
|
// This is a helper class for JSReceiver::GetKeys which collects and sorts keys.
|
|
|
|
// GetKeys needs to sort keys per prototype level, first showing the integer
|
|
|
|
// indices from elements then the strings from the properties. However, this
|
|
|
|
// does not apply to proxies which are in full control of how the keys are
|
|
|
|
// sorted.
|
|
|
|
//
|
|
|
|
// For performance reasons the KeyAccumulator internally separates integer keys
|
|
|
|
// in |elements_| into sorted lists per prototype level. String keys are
|
|
|
|
// collected in |string_properties_|, a single OrderedHashSet (similar for
|
|
|
|
// Symbols in |symbol_properties_|. To separate the keys per level later when
|
|
|
|
// assembling the final list, |levelLengths_| keeps track of the number of
|
|
|
|
// String and Symbol keys per level.
|
|
|
|
//
|
|
|
|
// Only unique keys are kept by the KeyAccumulator, strings are stored in a
|
|
|
|
// HashSet for inexpensive lookups. Integer keys are kept in sorted lists which
|
|
|
|
// are more compact and allow for reasonably fast includes check.
|
|
|
|
class KeyAccumulator final BASE_EMBEDDED {
|
|
|
|
public:
|
2016-05-30 15:52:56 +00:00
|
|
|
KeyAccumulator(Isolate* isolate, KeyCollectionMode mode,
|
2016-02-04 17:54:55 +00:00
|
|
|
PropertyFilter filter)
|
2016-05-30 15:52:56 +00:00
|
|
|
: isolate_(isolate), mode_(mode), filter_(filter) {}
|
2015-11-04 14:05:21 +00:00
|
|
|
~KeyAccumulator();
|
|
|
|
|
2016-05-25 09:13:31 +00:00
|
|
|
static MaybeHandle<FixedArray> GetKeys(
|
2016-05-30 15:52:56 +00:00
|
|
|
Handle<JSReceiver> object, KeyCollectionMode mode, PropertyFilter filter,
|
|
|
|
GetKeysConversion keys_conversion = GetKeysConversion::kKeepNumbers,
|
2016-08-01 10:08:54 +00:00
|
|
|
bool is_for_in = false);
|
2016-06-07 08:50:04 +00:00
|
|
|
|
2016-05-30 15:52:56 +00:00
|
|
|
Handle<FixedArray> GetKeys(
|
|
|
|
GetKeysConversion convert = GetKeysConversion::kKeepNumbers);
|
2016-05-06 10:50:41 +00:00
|
|
|
Maybe<bool> CollectKeys(Handle<JSReceiver> receiver,
|
|
|
|
Handle<JSReceiver> object);
|
2016-05-25 09:13:31 +00:00
|
|
|
Maybe<bool> CollectOwnElementIndices(Handle<JSReceiver> receiver,
|
|
|
|
Handle<JSObject> object);
|
|
|
|
Maybe<bool> CollectOwnPropertyNames(Handle<JSReceiver> receiver,
|
|
|
|
Handle<JSObject> object);
|
2016-06-27 11:48:04 +00:00
|
|
|
Maybe<bool> CollectAccessCheckInterceptorKeys(
|
|
|
|
Handle<AccessCheckInfo> access_check_info, Handle<JSReceiver> receiver,
|
|
|
|
Handle<JSObject> object);
|
2016-05-06 10:50:41 +00:00
|
|
|
|
2016-06-28 13:32:18 +00:00
|
|
|
static Handle<FixedArray> GetOwnEnumPropertyKeys(Isolate* isolate,
|
|
|
|
Handle<JSObject> object);
|
2016-05-06 10:50:41 +00:00
|
|
|
|
2016-05-25 09:13:31 +00:00
|
|
|
void AddKey(Object* key, AddKeyConversion convert = DO_NOT_CONVERT);
|
|
|
|
void AddKey(Handle<Object> key, AddKeyConversion convert = DO_NOT_CONVERT);
|
2016-01-29 18:57:26 +00:00
|
|
|
void AddKeys(Handle<FixedArray> array, AddKeyConversion convert);
|
2016-05-25 09:13:31 +00:00
|
|
|
void AddKeys(Handle<JSObject> array_like, AddKeyConversion convert);
|
2016-05-06 10:50:41 +00:00
|
|
|
|
2015-11-04 14:05:21 +00:00
|
|
|
// Jump to the next level, pushing the current |levelLength_| to
|
|
|
|
// |levelLengths_| and adding a new list to |elements_|.
|
2015-11-18 00:06:26 +00:00
|
|
|
Isolate* isolate() { return isolate_; }
|
2016-06-28 13:32:18 +00:00
|
|
|
// Filter keys based on their property descriptors.
|
2016-05-03 15:29:28 +00:00
|
|
|
PropertyFilter filter() { return filter_; }
|
2016-06-28 13:32:18 +00:00
|
|
|
// The collection mode defines whether we collect the keys from the prototype
|
|
|
|
// chain or only look at the receiver.
|
|
|
|
KeyCollectionMode mode() { return mode_; }
|
|
|
|
// In case of for-in loops we have to treat JSProxy keys differently and
|
|
|
|
// deduplicate them. Additionally we convert JSProxy keys back to array
|
|
|
|
// indices.
|
2016-05-25 09:13:31 +00:00
|
|
|
void set_is_for_in(bool value) { is_for_in_ = value; }
|
|
|
|
void set_skip_indices(bool value) { skip_indices_ = value; }
|
2016-06-28 13:32:18 +00:00
|
|
|
// The last_non_empty_prototype is used to limit the prototypes for which
|
|
|
|
// we have to keep track of non-enumerable keys that can shadow keys
|
|
|
|
// repeated on the prototype chain.
|
2016-06-07 08:50:04 +00:00
|
|
|
void set_last_non_empty_prototype(Handle<JSReceiver> object) {
|
|
|
|
last_non_empty_prototype_ = object;
|
|
|
|
}
|
2016-06-28 13:32:18 +00:00
|
|
|
// Shadowing keys are used to filter keys. This happens when non-enumerable
|
|
|
|
// keys appear again on the prototype chain.
|
2016-07-21 11:05:16 +00:00
|
|
|
void AddShadowingKey(Object* key);
|
|
|
|
void AddShadowingKey(Handle<Object> key);
|
2015-11-04 14:05:21 +00:00
|
|
|
|
|
|
|
private:
|
2016-05-06 10:50:41 +00:00
|
|
|
Maybe<bool> CollectOwnKeys(Handle<JSReceiver> receiver,
|
|
|
|
Handle<JSObject> object);
|
2016-05-10 14:26:21 +00:00
|
|
|
Maybe<bool> CollectOwnJSProxyKeys(Handle<JSReceiver> receiver,
|
|
|
|
Handle<JSProxy> proxy);
|
|
|
|
Maybe<bool> CollectOwnJSProxyTargetKeys(Handle<JSProxy> proxy,
|
|
|
|
Handle<JSReceiver> target);
|
|
|
|
Maybe<bool> AddKeysFromJSProxy(Handle<JSProxy> proxy,
|
|
|
|
Handle<FixedArray> keys);
|
2016-06-28 13:32:18 +00:00
|
|
|
bool IsShadowed(Handle<Object> key);
|
2016-07-21 11:05:16 +00:00
|
|
|
bool HasShadowingKeys();
|
2016-05-25 09:13:31 +00:00
|
|
|
Handle<OrderedHashSet> keys() { return Handle<OrderedHashSet>::cast(keys_); }
|
2015-11-04 14:05:21 +00:00
|
|
|
|
|
|
|
Isolate* isolate_;
|
2016-05-25 09:13:31 +00:00
|
|
|
// keys_ is either an Handle<OrderedHashSet> or in the case of own JSProxy
|
2016-06-28 13:32:18 +00:00
|
|
|
// keys a Handle<FixedArray>. The OrderedHashSet is in-place converted to the
|
|
|
|
// result list, a FixedArray containing all collected keys.
|
2016-05-25 09:13:31 +00:00
|
|
|
Handle<FixedArray> keys_;
|
2016-06-07 08:50:04 +00:00
|
|
|
Handle<JSReceiver> last_non_empty_prototype_;
|
2016-07-21 11:05:16 +00:00
|
|
|
Handle<ObjectHashSet> shadowing_keys_;
|
2016-05-30 15:52:56 +00:00
|
|
|
KeyCollectionMode mode_;
|
2015-12-02 16:30:06 +00:00
|
|
|
PropertyFilter filter_;
|
2016-05-25 09:13:31 +00:00
|
|
|
bool is_for_in_ = false;
|
|
|
|
bool skip_indices_ = false;
|
2016-07-21 11:05:16 +00:00
|
|
|
// For all the keys on the first receiver adding a shadowing key we can skip
|
|
|
|
// the shadow check.
|
|
|
|
bool skip_shadow_check_ = true;
|
2015-11-04 14:05:21 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(KeyAccumulator);
|
|
|
|
};
|
|
|
|
|
2016-03-07 19:25:12 +00:00
|
|
|
// The FastKeyAccumulator handles the cases where there are no elements on the
|
|
|
|
// prototype chain and forwords the complex/slow cases to the normal
|
2016-06-28 13:32:18 +00:00
|
|
|
// KeyAccumulator. This significantly speeds up the cases where the OWN_ONLY
|
|
|
|
// case where we do not have to walk the prototype chain.
|
2016-03-07 19:25:12 +00:00
|
|
|
class FastKeyAccumulator {
|
|
|
|
public:
|
|
|
|
FastKeyAccumulator(Isolate* isolate, Handle<JSReceiver> receiver,
|
2016-05-30 15:52:56 +00:00
|
|
|
KeyCollectionMode mode, PropertyFilter filter)
|
|
|
|
: isolate_(isolate), receiver_(receiver), mode_(mode), filter_(filter) {
|
2016-03-07 19:25:12 +00:00
|
|
|
Prepare();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_receiver_simple_enum() { return is_receiver_simple_enum_; }
|
|
|
|
bool has_empty_prototype() { return has_empty_prototype_; }
|
2016-05-25 09:13:31 +00:00
|
|
|
void set_is_for_in(bool value) { is_for_in_ = value; }
|
2016-03-07 19:25:12 +00:00
|
|
|
|
2016-05-30 15:52:56 +00:00
|
|
|
MaybeHandle<FixedArray> GetKeys(
|
|
|
|
GetKeysConversion convert = GetKeysConversion::kKeepNumbers);
|
2016-03-07 19:25:12 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
void Prepare();
|
|
|
|
MaybeHandle<FixedArray> GetKeysFast(GetKeysConversion convert);
|
|
|
|
MaybeHandle<FixedArray> GetKeysSlow(GetKeysConversion convert);
|
|
|
|
|
|
|
|
Isolate* isolate_;
|
|
|
|
Handle<JSReceiver> receiver_;
|
2016-06-07 08:50:04 +00:00
|
|
|
Handle<JSReceiver> last_non_empty_prototype_;
|
2016-05-30 15:52:56 +00:00
|
|
|
KeyCollectionMode mode_;
|
2016-03-07 19:25:12 +00:00
|
|
|
PropertyFilter filter_;
|
2016-05-25 09:13:31 +00:00
|
|
|
bool is_for_in_ = false;
|
2016-03-07 19:25:12 +00:00
|
|
|
bool is_receiver_simple_enum_ = false;
|
|
|
|
bool has_empty_prototype_ = false;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(FastKeyAccumulator);
|
|
|
|
};
|
2015-11-04 14:05:21 +00:00
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|
|
|
|
|
2016-05-03 15:29:28 +00:00
|
|
|
#endif // V8_KEYS_H_
|