keys.h 5.47 KB
Newer Older
1 2 3 4
// 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.

5 6
#ifndef V8_KEYS_H_
#define V8_KEYS_H_
7 8 9 10 11 12 13

#include "src/isolate.h"
#include "src/objects.h"

namespace v8 {
namespace internal {

14
enum AddKeyConversion { DO_NOT_CONVERT, CONVERT_TO_ARRAY_INDEX };
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33

// 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:
34
  KeyAccumulator(Isolate* isolate, KeyCollectionMode mode,
35
                 PropertyFilter filter)
36
      : isolate_(isolate), mode_(mode), filter_(filter) {}
37 38
  ~KeyAccumulator();

39
  static MaybeHandle<FixedArray> GetKeys(
40 41
      Handle<JSReceiver> object, KeyCollectionMode mode, PropertyFilter filter,
      GetKeysConversion keys_conversion = GetKeysConversion::kKeepNumbers,
42
      bool filter_proxy_keys = true, bool is_for_in = false);
43

44 45
  Handle<FixedArray> GetKeys(
      GetKeysConversion convert = GetKeysConversion::kKeepNumbers);
46 47
  Maybe<bool> CollectKeys(Handle<JSReceiver> receiver,
                          Handle<JSReceiver> object);
48 49 50 51
  Maybe<bool> CollectOwnElementIndices(Handle<JSReceiver> receiver,
                                       Handle<JSObject> object);
  Maybe<bool> CollectOwnPropertyNames(Handle<JSReceiver> receiver,
                                      Handle<JSObject> object);
52 53 54 55

  static Handle<FixedArray> GetEnumPropertyKeys(Isolate* isolate,
                                                Handle<JSObject> object);

56 57
  void AddKey(Object* key, AddKeyConversion convert = DO_NOT_CONVERT);
  void AddKey(Handle<Object> key, AddKeyConversion convert = DO_NOT_CONVERT);
58
  void AddKeys(Handle<FixedArray> array, AddKeyConversion convert);
59
  void AddKeys(Handle<JSObject> array_like, AddKeyConversion convert);
60

61 62
  // Jump to the next level, pushing the current |levelLength_| to
  // |levelLengths_| and adding a new list to |elements_|.
63
  Isolate* isolate() { return isolate_; }
64
  PropertyFilter filter() { return filter_; }
65
  void set_filter_proxy_keys(bool filter) { filter_proxy_keys_ = filter; }
66 67
  void set_is_for_in(bool value) { is_for_in_ = value; }
  void set_skip_indices(bool value) { skip_indices_ = value; }
68 69 70
  void set_last_non_empty_prototype(Handle<JSReceiver> object) {
    last_non_empty_prototype_ = object;
  }
71 72

 private:
73 74
  Maybe<bool> CollectOwnKeys(Handle<JSReceiver> receiver,
                             Handle<JSObject> object);
75 76 77 78 79 80 81 82
  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);

83
  Handle<OrderedHashSet> keys() { return Handle<OrderedHashSet>::cast(keys_); }
84 85

  Isolate* isolate_;
86 87 88
  // keys_ is either an Handle<OrderedHashSet> or in the case of own JSProxy
  // keys a Handle<FixedArray>.
  Handle<FixedArray> keys_;
89
  Handle<JSReceiver> last_non_empty_prototype_;
90
  KeyCollectionMode mode_;
91
  PropertyFilter filter_;
92
  bool filter_proxy_keys_ = true;
93 94
  bool is_for_in_ = false;
  bool skip_indices_ = false;
95 96 97 98

  DISALLOW_COPY_AND_ASSIGN(KeyAccumulator);
};

99 100 101 102 103 104
// The FastKeyAccumulator handles the cases where there are no elements on the
// prototype chain and forwords the complex/slow cases to the normal
// KeyAccumulator.
class FastKeyAccumulator {
 public:
  FastKeyAccumulator(Isolate* isolate, Handle<JSReceiver> receiver,
105 106
                     KeyCollectionMode mode, PropertyFilter filter)
      : isolate_(isolate), receiver_(receiver), mode_(mode), filter_(filter) {
107 108 109 110 111
    Prepare();
  }

  bool is_receiver_simple_enum() { return is_receiver_simple_enum_; }
  bool has_empty_prototype() { return has_empty_prototype_; }
112
  void set_filter_proxy_keys(bool filter) { filter_proxy_keys_ = filter; }
113
  void set_is_for_in(bool value) { is_for_in_ = value; }
114

115 116
  MaybeHandle<FixedArray> GetKeys(
      GetKeysConversion convert = GetKeysConversion::kKeepNumbers);
117 118 119 120 121 122 123 124

 private:
  void Prepare();
  MaybeHandle<FixedArray> GetKeysFast(GetKeysConversion convert);
  MaybeHandle<FixedArray> GetKeysSlow(GetKeysConversion convert);

  Isolate* isolate_;
  Handle<JSReceiver> receiver_;
125
  Handle<JSReceiver> last_non_empty_prototype_;
126
  KeyCollectionMode mode_;
127
  PropertyFilter filter_;
128
  bool filter_proxy_keys_ = true;
129
  bool is_for_in_ = false;
130 131 132 133 134
  bool is_receiver_simple_enum_ = false;
  bool has_empty_prototype_ = false;

  DISALLOW_COPY_AND_ASSIGN(FastKeyAccumulator);
};
135 136 137 138

}  // namespace internal
}  // namespace v8

139
#endif  // V8_KEYS_H_