LCOV - code coverage report
Current view: top level - src - keys.h (source / functions) Hit Total Coverage
Test: app.info Lines: 6 6 100.0 %
Date: 2019-04-17 Functions: 0 0 -

          Line data    Source code
       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_KEYS_H_
       6             : #define V8_KEYS_H_
       7             : 
       8             : #include "src/objects.h"
       9             : #include "src/objects/hash-table.h"
      10             : #include "src/objects/js-objects.h"
      11             : 
      12             : namespace v8 {
      13             : namespace internal {
      14             : 
      15             : class JSProxy;
      16             : 
      17             : enum AddKeyConversion { DO_NOT_CONVERT, CONVERT_TO_ARRAY_INDEX };
      18             : 
      19             : // This is a helper class for JSReceiver::GetKeys which collects and sorts keys.
      20             : // GetKeys needs to sort keys per prototype level, first showing the integer
      21             : // indices from elements then the strings from the properties. However, this
      22             : // does not apply to proxies which are in full control of how the keys are
      23             : // sorted.
      24             : //
      25             : // For performance reasons the KeyAccumulator internally separates integer keys
      26             : // in |elements_| into sorted lists per prototype level. String keys are
      27             : // collected in |string_properties_|, a single OrderedHashSet (similar for
      28             : // Symbols in |symbol_properties_|. To separate the keys per level later when
      29             : // assembling the final list, |levelLengths_| keeps track of the number of
      30             : // String and Symbol keys per level.
      31             : //
      32             : // Only unique keys are kept by the KeyAccumulator, strings are stored in a
      33             : // HashSet for inexpensive lookups. Integer keys are kept in sorted lists which
      34             : // are more compact and allow for reasonably fast includes check.
      35             : class KeyAccumulator final {
      36             :  public:
      37             :   KeyAccumulator(Isolate* isolate, KeyCollectionMode mode,
      38             :                  PropertyFilter filter)
      39     1285531 :       : isolate_(isolate), mode_(mode), filter_(filter) {}
      40             :   ~KeyAccumulator() = default;
      41             : 
      42             :   static MaybeHandle<FixedArray> GetKeys(
      43             :       Handle<JSReceiver> object, KeyCollectionMode mode, PropertyFilter filter,
      44             :       GetKeysConversion keys_conversion = GetKeysConversion::kKeepNumbers,
      45             :       bool is_for_in = false, bool skip_indices = false);
      46             : 
      47             :   Handle<FixedArray> GetKeys(
      48             :       GetKeysConversion convert = GetKeysConversion::kKeepNumbers);
      49             :   Maybe<bool> CollectKeys(Handle<JSReceiver> receiver,
      50             :                           Handle<JSReceiver> object);
      51             :   Maybe<bool> CollectOwnElementIndices(Handle<JSReceiver> receiver,
      52             :                                        Handle<JSObject> object);
      53             :   Maybe<bool> CollectOwnPropertyNames(Handle<JSReceiver> receiver,
      54             :                                       Handle<JSObject> object);
      55             :   void CollectPrivateNames(Handle<JSReceiver> receiver,
      56             :                            Handle<JSObject> object);
      57             :   Maybe<bool> CollectAccessCheckInterceptorKeys(
      58             :       Handle<AccessCheckInfo> access_check_info, Handle<JSReceiver> receiver,
      59             :       Handle<JSObject> object);
      60             : 
      61             :   // Might return directly the object's enum_cache, copy the result before using
      62             :   // as an elements backing store for a JSObject.
      63             :   // Does not throw for uninitialized exports in module namespace objects, so
      64             :   // this has to be checked separately.
      65             :   static Handle<FixedArray> GetOwnEnumPropertyKeys(Isolate* isolate,
      66             :                                                    Handle<JSObject> object);
      67             : 
      68             :   void AddKey(Object key, AddKeyConversion convert = DO_NOT_CONVERT);
      69             :   void AddKey(Handle<Object> key, AddKeyConversion convert = DO_NOT_CONVERT);
      70             :   void AddKeys(Handle<FixedArray> array, AddKeyConversion convert);
      71             :   void AddKeys(Handle<JSObject> array_like, AddKeyConversion convert);
      72             : 
      73             :   // Jump to the next level, pushing the current |levelLength_| to
      74             :   // |levelLengths_| and adding a new list to |elements_|.
      75             :   Isolate* isolate() { return isolate_; }
      76             :   // Filter keys based on their property descriptors.
      77             :   PropertyFilter filter() { return filter_; }
      78             :   // The collection mode defines whether we collect the keys from the prototype
      79             :   // chain or only look at the receiver.
      80             :   KeyCollectionMode mode() { return mode_; }
      81             :   // In case of for-in loops we have to treat JSProxy keys differently and
      82             :   // deduplicate them. Additionally we convert JSProxy keys back to array
      83             :   // indices.
      84     1272946 :   void set_is_for_in(bool value) { is_for_in_ = value; }
      85     1279564 :   void set_skip_indices(bool value) { skip_indices_ = value; }
      86             :   // The last_non_empty_prototype is used to limit the prototypes for which
      87             :   // we have to keep track of non-enumerable keys that can shadow keys
      88             :   // repeated on the prototype chain.
      89             :   void set_last_non_empty_prototype(Handle<JSReceiver> object) {
      90     1272946 :     last_non_empty_prototype_ = object;
      91             :   }
      92             :   // Shadowing keys are used to filter keys. This happens when non-enumerable
      93             :   // keys appear again on the prototype chain.
      94             :   void AddShadowingKey(Object key);
      95             :   void AddShadowingKey(Handle<Object> key);
      96             : 
      97             :  private:
      98             :   Maybe<bool> CollectOwnKeys(Handle<JSReceiver> receiver,
      99             :                              Handle<JSObject> object);
     100             :   Maybe<bool> CollectOwnJSProxyKeys(Handle<JSReceiver> receiver,
     101             :                                     Handle<JSProxy> proxy);
     102             :   Maybe<bool> CollectOwnJSProxyTargetKeys(Handle<JSProxy> proxy,
     103             :                                           Handle<JSReceiver> target);
     104             :   Maybe<bool> AddKeysFromJSProxy(Handle<JSProxy> proxy,
     105             :                                  Handle<FixedArray> keys);
     106             :   bool IsShadowed(Handle<Object> key);
     107             :   bool HasShadowingKeys();
     108             :   Handle<OrderedHashSet> keys();
     109             : 
     110             :   Isolate* isolate_;
     111             :   // keys_ is either an Handle<OrderedHashSet> or in the case of own JSProxy
     112             :   // keys a Handle<FixedArray>. The OrderedHashSet is in-place converted to the
     113             :   // result list, a FixedArray containing all collected keys.
     114             :   Handle<FixedArray> keys_;
     115             :   Handle<JSReceiver> last_non_empty_prototype_;
     116             :   Handle<ObjectHashSet> shadowing_keys_;
     117             :   KeyCollectionMode mode_;
     118             :   PropertyFilter filter_;
     119             :   bool is_for_in_ = false;
     120             :   bool skip_indices_ = false;
     121             :   // For all the keys on the first receiver adding a shadowing key we can skip
     122             :   // the shadow check.
     123             :   bool skip_shadow_check_ = true;
     124             : 
     125             :   DISALLOW_COPY_AND_ASSIGN(KeyAccumulator);
     126             : };
     127             : 
     128             : // The FastKeyAccumulator handles the cases where there are no elements on the
     129             : // prototype chain and forwords the complex/slow cases to the normal
     130             : // KeyAccumulator. This significantly speeds up the cases where the OWN_ONLY
     131             : // case where we do not have to walk the prototype chain.
     132             : class FastKeyAccumulator {
     133             :  public:
     134             :   FastKeyAccumulator(Isolate* isolate, Handle<JSReceiver> receiver,
     135             :                      KeyCollectionMode mode, PropertyFilter filter,
     136             :                      bool is_for_in = false, bool skip_indices = false)
     137             :       : isolate_(isolate),
     138             :         receiver_(receiver),
     139             :         mode_(mode),
     140             :         filter_(filter),
     141             :         is_for_in_(is_for_in),
     142     2943858 :         skip_indices_(skip_indices) {
     143     1471929 :     Prepare();
     144             :   }
     145             : 
     146             :   bool is_receiver_simple_enum() { return is_receiver_simple_enum_; }
     147             :   bool has_empty_prototype() { return has_empty_prototype_; }
     148             : 
     149             :   MaybeHandle<FixedArray> GetKeys(
     150             :       GetKeysConversion convert = GetKeysConversion::kKeepNumbers);
     151             : 
     152             :  private:
     153             :   void Prepare();
     154             :   MaybeHandle<FixedArray> GetKeysFast(GetKeysConversion convert);
     155             :   MaybeHandle<FixedArray> GetKeysSlow(GetKeysConversion convert);
     156             : 
     157             :   MaybeHandle<FixedArray> GetOwnKeysWithUninitializedEnumCache();
     158             : 
     159             :   Isolate* isolate_;
     160             :   Handle<JSReceiver> receiver_;
     161             :   Handle<JSReceiver> last_non_empty_prototype_;
     162             :   KeyCollectionMode mode_;
     163             :   PropertyFilter filter_;
     164             :   bool is_for_in_ = false;
     165             :   bool skip_indices_ = false;
     166             :   bool is_receiver_simple_enum_ = false;
     167             :   bool has_empty_prototype_ = false;
     168             : 
     169             :   DISALLOW_COPY_AND_ASSIGN(FastKeyAccumulator);
     170             : };
     171             : 
     172             : }  // namespace internal
     173             : }  // namespace v8
     174             : 
     175             : #endif  // V8_KEYS_H_

Generated by: LCOV version 1.10