roots-inl.h 3.84 KB
Newer Older
1 2 3 4 5 6 7 8 9
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef V8_ROOTS_INL_H_
#define V8_ROOTS_INL_H_

#include "src/roots.h"

10
#include "src/feedback-vector.h"
11
#include "src/handles.h"
12
#include "src/isolate.h"
13
#include "src/objects/api-callbacks.h"
14
#include "src/objects/descriptor-array.h"
15
#include "src/objects/heap-number.h"
16 17
#include "src/objects/literal-objects.h"
#include "src/objects/map.h"
18 19
#include "src/objects/oddball.h"
#include "src/objects/property-array.h"
20
#include "src/objects/property-cell.h"
21
#include "src/objects/scope-info.h"
22
#include "src/objects/slots.h"
23
#include "src/objects/string.h"
24 25 26 27

namespace v8 {
namespace internal {

28
V8_INLINE constexpr bool operator<(RootIndex lhs, RootIndex rhs) {
29 30 31 32 33 34 35 36 37 38
  typedef typename std::underlying_type<RootIndex>::type type;
  return static_cast<type>(lhs) < static_cast<type>(rhs);
}

V8_INLINE RootIndex operator++(RootIndex& index) {
  typedef typename std::underlying_type<RootIndex>::type type;
  index = static_cast<RootIndex>(static_cast<type>(index) + 1);
  return index;
}

39 40
bool RootsTable::IsRootHandleLocation(Address* handle_location,
                                      RootIndex* index) const {
41 42 43
  FullObjectSlot location(handle_location);
  FullObjectSlot first_root(&roots_[0]);
  FullObjectSlot last_root(&roots_[kEntriesCount]);
44 45 46 47 48 49
  if (location >= last_root) return false;
  if (location < first_root) return false;
  *index = static_cast<RootIndex>(location - first_root);
  return true;
}

50 51
template <typename T>
bool RootsTable::IsRootHandle(Handle<T> handle, RootIndex* index) const {
52 53 54 55
  // This can't use handle.location() because it is called from places
  // where handle dereferencing is disallowed. Comparing the handle's
  // location against the root handle list is safe though.
  Address* handle_location = reinterpret_cast<Address*>(handle.address());
56 57 58
  return IsRootHandleLocation(handle_location, index);
}

59
ReadOnlyRoots::ReadOnlyRoots(Heap* heap)
60
    : roots_table_(Isolate::FromHeap(heap)->roots_table()) {}
61

62
ReadOnlyRoots::ReadOnlyRoots(Isolate* isolate)
63
    : roots_table_(isolate->roots_table()) {}
64

65 66 67 68 69 70 71 72 73 74 75 76 77
// We use unchecked_cast below because we trust our read-only roots to
// have the right type, and to avoid the heavy #includes that would be
// required for checked casts.

#define ROOT_ACCESSOR(Type, name, CamelName)                     \
  Type ReadOnlyRoots::name() const {                             \
    DCHECK(CheckType(RootIndex::k##CamelName));                  \
    return Type::unchecked_cast(                                 \
        Object(roots_table_[RootIndex::k##CamelName]));          \
  }                                                              \
  Handle<Type> ReadOnlyRoots::name##_handle() const {            \
    DCHECK(CheckType(RootIndex::k##CamelName));                  \
    return Handle<Type>(&roots_table_[RootIndex::k##CamelName]); \
78
  }
79

80
READ_ONLY_ROOT_LIST(ROOT_ACCESSOR)
81
#undef ROOT_ACCESSOR
82

83
Map ReadOnlyRoots::MapForFixedTypedArray(ExternalArrayType array_type) {
84
  RootIndex root_index = RootsTable::RootIndexForFixedTypedArray(array_type);
85 86
  DCHECK(CheckType(root_index));
  return Map::unchecked_cast(Object(roots_table_[root_index]));
87 88
}

89
Map ReadOnlyRoots::MapForFixedTypedArray(ElementsKind elements_kind) {
90
  RootIndex root_index = RootsTable::RootIndexForFixedTypedArray(elements_kind);
91 92
  DCHECK(CheckType(root_index));
  return Map::unchecked_cast(Object(roots_table_[root_index]));
93 94
}

95 96
FixedTypedArrayBase ReadOnlyRoots::EmptyFixedTypedArrayForTypedArray(
    ElementsKind elements_kind) {
97
  RootIndex root_index =
98 99 100
      RootsTable::RootIndexForEmptyFixedTypedArray(elements_kind);
  DCHECK(CheckType(root_index));
  return FixedTypedArrayBase::unchecked_cast(Object(roots_table_[root_index]));
101 102
}

103 104 105 106
}  // namespace internal
}  // namespace v8

#endif  // V8_ROOTS_INL_H_