Home | History | Annotate | Download | only in objects
      1 // Copyright 2018 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_OBJECTS_PROTOTYPE_INFO_H_
      6 #define V8_OBJECTS_PROTOTYPE_INFO_H_
      7 
      8 #include "src/objects.h"
      9 #include "src/objects/fixed-array.h"
     10 
     11 // Has to be the last include (doesn't have include guards):
     12 #include "src/objects/object-macros.h"
     13 
     14 namespace v8 {
     15 namespace internal {
     16 
     17 // Container for metadata stored on each prototype map.
     18 class PrototypeInfo : public Struct {
     19  public:
     20   static const int UNREGISTERED = -1;
     21 
     22   // [module_namespace]: A backpointer to JSModuleNamespace from its
     23   // PrototypeInfo (or undefined). This field is only used for JSModuleNamespace
     24   // maps.  TODO(jkummerow): Figure out if there's a way to store the namespace
     25   // pointer elsewhere to save memory.
     26   DECL_ACCESSORS(module_namespace, Object)
     27 
     28   // [prototype_users]: WeakArrayList containing weak references to maps using
     29   // this prototype, or Smi(0) if uninitialized.
     30   DECL_ACCESSORS(prototype_users, Object)
     31 
     32   // [object_create_map]: A field caching the map for Object.create(prototype).
     33   static inline void SetObjectCreateMap(Handle<PrototypeInfo> info,
     34                                         Handle<Map> map);
     35   inline Map* ObjectCreateMap();
     36   inline bool HasObjectCreateMap();
     37 
     38   // [registry_slot]: Slot in prototype's user registry where this user
     39   // is stored. Returns UNREGISTERED if this prototype has not been registered.
     40   inline int registry_slot() const;
     41   inline void set_registry_slot(int slot);
     42 
     43   // [bit_field]
     44   inline int bit_field() const;
     45   inline void set_bit_field(int bit_field);
     46 
     47   DECL_BOOLEAN_ACCESSORS(should_be_fast_map)
     48 
     49   DECL_CAST(PrototypeInfo)
     50 
     51   // Dispatched behavior.
     52   DECL_PRINTER(PrototypeInfo)
     53   DECL_VERIFIER(PrototypeInfo)
     54 
     55   static const int kJSModuleNamespaceOffset = HeapObject::kHeaderSize;
     56   static const int kPrototypeUsersOffset =
     57       kJSModuleNamespaceOffset + kPointerSize;
     58   static const int kRegistrySlotOffset = kPrototypeUsersOffset + kPointerSize;
     59   static const int kValidityCellOffset = kRegistrySlotOffset + kPointerSize;
     60   static const int kObjectCreateMapOffset = kValidityCellOffset + kPointerSize;
     61   static const int kBitFieldOffset = kObjectCreateMapOffset + kPointerSize;
     62   static const int kSize = kBitFieldOffset + kPointerSize;
     63 
     64   // Bit field usage.
     65   static const int kShouldBeFastBit = 0;
     66 
     67   class BodyDescriptor;
     68 
     69  private:
     70   DECL_ACCESSORS(object_create_map, MaybeObject)
     71 
     72   DISALLOW_IMPLICIT_CONSTRUCTORS(PrototypeInfo);
     73 };
     74 
     75 // A growing array with an additional API for marking slots "empty". When adding
     76 // new elements, we reuse the empty slots instead of growing the array.
     77 class PrototypeUsers : public WeakArrayList {
     78  public:
     79   static Handle<WeakArrayList> Add(Isolate* isolate,
     80                                    Handle<WeakArrayList> array,
     81                                    Handle<Map> value, int* assigned_index);
     82 
     83   static inline void MarkSlotEmpty(WeakArrayList* array, int index);
     84 
     85   // The callback is called when a weak pointer to HeapObject "object" is moved
     86   // from index "from_index" to index "to_index" during compaction. The callback
     87   // must not cause GC.
     88   typedef void (*CompactionCallback)(HeapObject* object, int from_index,
     89                                      int to_index);
     90   static WeakArrayList* Compact(Handle<WeakArrayList> array, Heap* heap,
     91                                 CompactionCallback callback,
     92                                 PretenureFlag pretenure = NOT_TENURED);
     93 
     94 #ifdef VERIFY_HEAP
     95   static void Verify(WeakArrayList* array);
     96 #endif  // VERIFY_HEAP
     97 
     98   static const int kEmptySlotIndex = 0;
     99   static const int kFirstIndex = 1;
    100 
    101   static const int kNoEmptySlotsMarker = 0;
    102 
    103  private:
    104   static inline Smi* empty_slot_index(WeakArrayList* array);
    105   static inline void set_empty_slot_index(WeakArrayList* array, int index);
    106 
    107   static void IsSlotEmpty(WeakArrayList* array, int index);
    108 
    109   DISALLOW_IMPLICIT_CONSTRUCTORS(PrototypeUsers);
    110 };
    111 
    112 }  // namespace internal
    113 }  // namespace v8
    114 
    115 #include "src/objects/object-macros-undef.h"
    116 
    117 #endif  // V8_OBJECTS_PROTOTYPE_INFO_H_
    118