Home | History | Annotate | Download | only in src
      1 // Copyright 2012 the V8 project authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef V8_KEY_ACCUMULATOR_H_
      6 #define V8_KEY_ACCUMULATOR_H_
      7 
      8 #include "src/isolate.h"
      9 #include "src/objects.h"
     10 
     11 namespace v8 {
     12 namespace internal {
     13 
     14 enum AddKeyConversion { DO_NOT_CONVERT, CONVERT_TO_ARRAY_INDEX, PROXY_MAGIC };
     15 
     16 // This is a helper class for JSReceiver::GetKeys which collects and sorts keys.
     17 // GetKeys needs to sort keys per prototype level, first showing the integer
     18 // indices from elements then the strings from the properties. However, this
     19 // does not apply to proxies which are in full control of how the keys are
     20 // sorted.
     21 //
     22 // For performance reasons the KeyAccumulator internally separates integer keys
     23 // in |elements_| into sorted lists per prototype level. String keys are
     24 // collected in |string_properties_|, a single OrderedHashSet (similar for
     25 // Symbols in |symbol_properties_|. To separate the keys per level later when
     26 // assembling the final list, |levelLengths_| keeps track of the number of
     27 // String and Symbol keys per level.
     28 //
     29 // Only unique keys are kept by the KeyAccumulator, strings are stored in a
     30 // HashSet for inexpensive lookups. Integer keys are kept in sorted lists which
     31 // are more compact and allow for reasonably fast includes check.
     32 class KeyAccumulator final BASE_EMBEDDED {
     33  public:
     34   KeyAccumulator(Isolate* isolate, PropertyFilter filter)
     35       : isolate_(isolate), filter_(filter) {}
     36   ~KeyAccumulator();
     37 
     38   bool AddKey(uint32_t key);
     39   bool AddKey(Object* key, AddKeyConversion convert = DO_NOT_CONVERT);
     40   bool AddKey(Handle<Object> key, AddKeyConversion convert = DO_NOT_CONVERT);
     41   void AddKeys(Handle<FixedArray> array,
     42                AddKeyConversion convert = DO_NOT_CONVERT);
     43   void AddKeys(Handle<JSObject> array,
     44                AddKeyConversion convert = DO_NOT_CONVERT);
     45   void AddKeysFromProxy(Handle<JSObject> array);
     46   Maybe<bool> AddKeysFromProxy(Handle<JSProxy> proxy, Handle<FixedArray> keys);
     47   void AddElementKeysFromInterceptor(Handle<JSObject> array);
     48   // Jump to the next level, pushing the current |levelLength_| to
     49   // |levelLengths_| and adding a new list to |elements_|.
     50   void NextPrototype();
     51   // Sort the integer indices in the last list in |elements_|
     52   void SortCurrentElementsList();
     53   Handle<FixedArray> GetKeys(GetKeysConversion convert = KEEP_NUMBERS);
     54   int length() { return length_; }
     55   Isolate* isolate() { return isolate_; }
     56 
     57  private:
     58   bool AddIntegerKey(uint32_t key);
     59   bool AddStringKey(Handle<Object> key, AddKeyConversion convert);
     60   bool AddSymbolKey(Handle<Object> array);
     61   void SortCurrentElementsListRemoveDuplicates();
     62 
     63   Isolate* isolate_;
     64   PropertyFilter filter_;
     65   // |elements_| contains the sorted element keys (indices) per level.
     66   std::vector<std::vector<uint32_t>*> elements_;
     67   // |protoLengths_| contains the total number of keys (elements + properties)
     68   // per level. Negative values mark counts for a level with keys from a proxy.
     69   std::vector<int> level_lengths_;
     70   // |string_properties_| contains the unique String property keys for all
     71   // levels in insertion order per level.
     72   Handle<OrderedHashSet> string_properties_;
     73   // |symbol_properties_| contains the unique Symbol property keys for all
     74   // levels in insertion order per level.
     75   Handle<OrderedHashSet> symbol_properties_;
     76   // |length_| keeps track of the total number of all element and property keys.
     77   int length_ = 0;
     78   // |levelLength_| keeps track of the number of String keys in the current
     79   // level.
     80   int level_string_length_ = 0;
     81   // |levelSymbolLength_| keeps track of the number of Symbol keys in the
     82   // current level.
     83   int level_symbol_length_ = 0;
     84 
     85   DISALLOW_COPY_AND_ASSIGN(KeyAccumulator);
     86 };
     87 
     88 
     89 }  // namespace internal
     90 }  // namespace v8
     91 
     92 
     93 #endif  // V8_KEY_ACCUMULATOR_H_
     94