V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
heap-object.h
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_HEAP_OBJECT_H_
6 #define V8_OBJECTS_HEAP_OBJECT_H_
7 
8 #include "src/globals.h"
9 
10 #include "src/objects.h"
11 
12 // Has to be the last include (doesn't have include guards):
13 #include "src/objects/object-macros.h"
14 
15 namespace v8 {
16 namespace internal {
17 
18 // This is the new way to represent the Object class. It is temporarily
19 // separate to allow an incremental transition.
20 // For a design overview, see https://goo.gl/Ph4CGz.
21 class ObjectPtr {
22  public:
23  constexpr ObjectPtr() : ptr_(kNullAddress) {}
24  explicit constexpr ObjectPtr(Address ptr) : ptr_(ptr) {}
25 
26  // Enable incremental transition.
27  operator Object*() const { return reinterpret_cast<Object*>(ptr()); }
28 
29  bool operator==(const ObjectPtr other) const {
30  return this->ptr() == other.ptr();
31  }
32  bool operator!=(const ObjectPtr other) const {
33  return this->ptr() != other.ptr();
34  }
35  // Usage in std::set requires operator<.
36  bool operator<(const ObjectPtr other) const {
37  return this->ptr() < other.ptr();
38  }
39 
40  // Returns the tagged "(heap) object pointer" representation of this object.
41  constexpr Address ptr() const { return ptr_; }
42 
43  ObjectPtr* operator->() { return this; }
44  const ObjectPtr* operator->() const { return this; }
45 
46 #define IS_TYPE_FUNCTION_DECL(Type) V8_INLINE bool Is##Type() const;
47  OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
48  HEAP_OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
49 #undef IS_TYPE_FUNCTION_DECL
50 #define DECL_STRUCT_PREDICATE(NAME, Name, name) V8_INLINE bool Is##Name() const;
51  STRUCT_LIST(DECL_STRUCT_PREDICATE)
52 #undef DECL_STRUCT_PREDICATE
53 #define IS_TYPE_FUNCTION_DECL(Type, Value) \
54  V8_INLINE bool Is##Type(Isolate* isolate) const; \
55  V8_INLINE bool Is##Type(ReadOnlyRoots roots) const; \
56  V8_INLINE bool Is##Type() const;
57  ODDBALL_LIST(IS_TYPE_FUNCTION_DECL)
58 #undef IS_TYPE_FUNCTION_DECL
59  inline bool IsHashTableBase() const;
60  V8_INLINE bool IsSmallOrderedHashTable() const;
61 
62  inline bool IsObject() const { return true; }
63  inline double Number() const;
64  inline bool ToInt32(int32_t* value) const;
65  inline bool ToUint32(uint32_t* value) const;
66 
67  // ECMA-262 9.2.
68  bool BooleanValue(Isolate* isolate);
69 
70  inline bool FilterKey(PropertyFilter filter);
71 
72  // Returns the permanent hash code associated with this object. May return
73  // undefined if not yet created.
74  inline Object* GetHash();
75 
76  // Returns the permanent hash code associated with this object depending on
77  // the actual object type. May create and store a hash code if needed and none
78  // exists.
79  Smi GetOrCreateHash(Isolate* isolate);
80 
81  // Checks whether this object has the same value as the given one. This
82  // function is implemented according to ES5, section 9.12 and can be used
83  // to implement the Object.is function.
84  V8_EXPORT_PRIVATE bool SameValue(Object* other);
85 
86  // Tries to convert an object to an array index. Returns true and sets the
87  // output parameter if it succeeds. Equivalent to ToArrayLength, but does not
88  // allow kMaxUInt32.
89  V8_WARN_UNUSED_RESULT inline bool ToArrayIndex(uint32_t* index) const;
90 
91 #ifdef VERIFY_HEAP
92  void ObjectVerify(Isolate* isolate) {
93  reinterpret_cast<Object*>(ptr())->ObjectVerify(isolate);
94  }
95  // Verify a pointer is a valid object pointer.
96  static void VerifyPointer(Isolate* isolate, Object* p);
97 #endif
98 
99  inline void ShortPrint(FILE* out = stdout);
100  void ShortPrint(std::ostream& os); // NOLINT
101  inline void Print();
102  inline void Print(std::ostream& os);
103 
104  // For use with std::unordered_set.
105  struct Hasher {
106  size_t operator()(const ObjectPtr o) const {
107  return std::hash<v8::internal::Address>{}(o.ptr());
108  }
109  };
110 
111  private:
112  friend class ObjectSlot;
113  Address ptr_;
114 };
115 
116 // In heap-objects.h to be usable without heap-objects-inl.h inclusion.
117 bool ObjectPtr::IsSmi() const { return HAS_SMI_TAG(ptr()); }
118 bool ObjectPtr::IsHeapObject() const {
119  DCHECK_EQ(!IsSmi(), Internals::HasHeapObjectTag(ptr()));
120  return !IsSmi();
121 }
122 
123 // Replacement for HeapObject; temporarily separate for incremental transition:
124 class HeapObjectPtr : public ObjectPtr {
125  public:
126  inline Map map() const;
127  inline void set_map(Map value);
128  inline void set_map_after_allocation(
129  Map value, WriteBarrierMode mode = UPDATE_WRITE_BARRIER);
130  // The no-write-barrier version. This is OK if the object is white and in
131  // new space, or if the value is an immortal immutable object, like the maps
132  // of primitive (non-JS) objects like strings, heap numbers etc.
133  inline void set_map_no_write_barrier(Map value);
134 
135  inline ObjectSlot map_slot();
136  inline MapWord map_word() const;
137  inline void set_map_word(MapWord map_word);
138 
139  // Set the map using release store
140  inline void synchronized_set_map(Map value);
141  inline void synchronized_set_map_word(MapWord map_word);
142 
143  inline WriteBarrierMode GetWriteBarrierMode(
144  const DisallowHeapAllocation& promise);
145 
146  // Enable incremental transition.
147  operator HeapObject*() { return reinterpret_cast<HeapObject*>(ptr()); }
148  operator const HeapObject*() const {
149  return reinterpret_cast<const HeapObject*>(ptr());
150  }
151 
152  bool is_null() const { return ptr() == kNullAddress; }
153 
154  bool IsHeapObjectPtr() const { return IsHeapObject(); }
155 
156  inline ReadOnlyRoots GetReadOnlyRoots() const;
157 
158 #define IS_TYPE_FUNCTION_DECL(Type) V8_INLINE bool Is##Type() const;
159  HEAP_OBJECT_TYPE_LIST(IS_TYPE_FUNCTION_DECL)
160 #undef IS_TYPE_FUNCTION_DECL
161 
162  // Untagged aligned address.
163  inline Address address() const { return ptr() - kHeapObjectTag; }
164 
165  inline int Size() const;
166  inline int SizeFromMap(Map map) const;
167 
168  inline ObjectSlot RawField(int byte_offset) const;
169  inline MaybeObjectSlot RawMaybeWeakField(int byte_offset) const;
170 
171 #ifdef OBJECT_PRINT
172  void PrintHeader(std::ostream& os, const char* id); // NOLINT
173 #endif
174  void HeapObjectVerify(Isolate* isolate);
175 #ifdef VERIFY_HEAP
176  static void VerifyHeapPointer(Isolate* isolate, Object* p);
177 #endif
178 
179 #ifdef VERIFY_HEAP
180  inline void VerifyObjectField(Isolate* isolate, int offset);
181  inline void VerifySmiField(int offset);
182  inline void VerifyMaybeObjectField(Isolate* isolate, int offset);
183 #endif
184 
185  static const int kMapOffset = HeapObject::kMapOffset;
186  static const int kHeaderSize = HeapObject::kHeaderSize;
187 
188  inline Address GetFieldAddress(int field_offset) const;
189 
190  protected:
191  // Special-purpose constructor for subclasses that have fast paths where
192  // their ptr() is a Smi.
193  enum class AllowInlineSmiStorage { kRequireHeapObjectTag, kAllowBeingASmi };
194  inline HeapObjectPtr(Address ptr, AllowInlineSmiStorage allow_smi);
195 
196  OBJECT_CONSTRUCTORS(HeapObjectPtr, ObjectPtr)
197 };
198 
199 // Replacement for NeverReadOnlySpaceObject, temporarily separate for
200 // incremental transition.
201 // Helper class for objects that can never be in RO space.
202 // TODO(leszeks): Add checks in the factory that we never allocate these objects
203 // in RO space.
204 // TODO(3770): Get rid of the duplication.
206  public:
207  // The Heap the object was allocated in. Used also to access Isolate.
208  static inline Heap* GetHeap(const HeapObjectPtr object);
209 
210  // Convenience method to get current isolate.
211  static inline Isolate* GetIsolate(const HeapObjectPtr object);
212 };
213 
214 } // namespace internal
215 } // namespace v8
216 
217 #include "src/objects/object-macros-undef.h"
218 
219 #endif // V8_OBJECTS_HEAP_OBJECT_H_
Definition: libplatform.h:13