V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
roots-inl.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_ROOTS_INL_H_
6 #define V8_ROOTS_INL_H_
7 
8 #include "src/roots.h"
9 
10 #include "src/feedback-vector.h"
11 #include "src/handles.h"
12 #include "src/heap/heap-inl.h"
13 #include "src/objects/api-callbacks.h"
14 #include "src/objects/descriptor-array.h"
15 #include "src/objects/literal-objects.h"
16 #include "src/objects/map.h"
17 #include "src/objects/scope-info.h"
18 #include "src/objects/slots.h"
19 
20 namespace v8 {
21 namespace internal {
22 
23 V8_INLINE constexpr bool operator<(RootIndex lhs, RootIndex rhs) {
24  typedef typename std::underlying_type<RootIndex>::type type;
25  return static_cast<type>(lhs) < static_cast<type>(rhs);
26 }
27 
28 V8_INLINE RootIndex operator++(RootIndex& index) {
29  typedef typename std::underlying_type<RootIndex>::type type;
30  index = static_cast<RootIndex>(static_cast<type>(index) + 1);
31  return index;
32 }
33 
34 bool RootsTable::IsRootHandleLocation(Address* handle_location,
35  RootIndex* index) const {
36  ObjectSlot location(handle_location);
37  ObjectSlot first_root(&roots_[0]);
38  ObjectSlot last_root(&roots_[kEntriesCount]);
39  if (location >= last_root) return false;
40  if (location < first_root) return false;
41  *index = static_cast<RootIndex>(location - first_root);
42  return true;
43 }
44 
45 template <typename T>
46 bool RootsTable::IsRootHandle(Handle<T> handle, RootIndex* index) const {
47  // This can't use handle.location() because it is called from places
48  // where handle dereferencing is disallowed. Comparing the handle's
49  // location against the root handle list is safe though.
50  Address* handle_location = reinterpret_cast<Address*>(handle.address());
51  return IsRootHandleLocation(handle_location, index);
52 }
53 
54 ReadOnlyRoots::ReadOnlyRoots(Heap* heap)
55  : roots_table_(heap->isolate()->roots_table()) {}
56 
57 ReadOnlyRoots::ReadOnlyRoots(Isolate* isolate)
58  : roots_table_(isolate->roots_table()) {}
59 
60 // TODO(jkummerow): Drop std::remove_pointer after the migration to ObjectPtr.
61 #define ROOT_ACCESSOR(Type, name, CamelName) \
62  Type ReadOnlyRoots::name() const { \
63  return std::remove_pointer<Type>::type::cast( \
64  roots_table_[RootIndex::k##CamelName]); \
65  } \
66  Handle<std::remove_pointer<Type>::type> ReadOnlyRoots::name##_handle() \
67  const { \
68  return Handle<std::remove_pointer<Type>::type>( \
69  bit_cast<Address*>(&roots_table_[RootIndex::k##CamelName])); \
70  }
71 
72 READ_ONLY_ROOT_LIST(ROOT_ACCESSOR)
73 #undef ROOT_ACCESSOR
74 
75 Map ReadOnlyRoots::MapForFixedTypedArray(ExternalArrayType array_type) {
76  RootIndex root_index = RootsTable::RootIndexForFixedTypedArray(array_type);
77  return Map::cast(roots_table_[root_index]);
78 }
79 
80 Map ReadOnlyRoots::MapForFixedTypedArray(ElementsKind elements_kind) {
81  RootIndex root_index = RootsTable::RootIndexForFixedTypedArray(elements_kind);
82  return Map::cast(roots_table_[root_index]);
83 }
84 
85 FixedTypedArrayBase ReadOnlyRoots::EmptyFixedTypedArrayForMap(const Map map) {
86  RootIndex root_index =
87  RootsTable::RootIndexForEmptyFixedTypedArray(map->elements_kind());
88  return FixedTypedArrayBase::cast(roots_table_[root_index]);
89 }
90 
91 } // namespace internal
92 } // namespace v8
93 
94 #endif // V8_ROOTS_INL_H_
Definition: libplatform.h:13