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

#ifndef V8_TRANSITIONS_INL_H_
#define V8_TRANSITIONS_INL_H_

8
#include "src/transitions.h"
9

10
#include "src/ic/handler-configuration-inl.h"
11 12
#include "src/objects/fixed-array-inl.h"
#include "src/objects/maybe-object-inl.h"
13

14 15 16
// Has to be the last include (doesn't have include guards):
#include "src/objects/object-macros.h"

17 18 19
namespace v8 {
namespace internal {

20 21
TransitionArray* TransitionsAccessor::transitions() {
  DCHECK_EQ(kFullTransitionArray, encoding());
22
  return TransitionArray::cast(raw_transitions_->ToStrongHeapObject());
23
}
24

25
CAST_ACCESSOR(TransitionArray)
26

27
bool TransitionArray::HasPrototypeTransitions() {
28
  return Get(kPrototypeTransitionsIndex) != MaybeObject::FromSmi(Smi::kZero);
29 30
}

31
WeakFixedArray* TransitionArray::GetPrototypeTransitions() {
32
  DCHECK(HasPrototypeTransitions());  // Callers must check first.
33 34
  Object* prototype_transitions =
      Get(kPrototypeTransitionsIndex)->ToStrongHeapObject();
35
  return WeakFixedArray::cast(prototype_transitions);
36 37
}

38 39 40 41 42
HeapObjectReference** TransitionArray::GetKeySlot(int transition_number) {
  DCHECK(transition_number < number_of_transitions());
  return reinterpret_cast<HeapObjectReference**>(
      RawFieldOfElementAt(ToKeyIndex(transition_number)));
}
43

44 45
void TransitionArray::SetPrototypeTransitions(WeakFixedArray* transitions) {
  DCHECK(transitions->IsWeakFixedArray());
46 47
  WeakFixedArray::Set(kPrototypeTransitionsIndex,
                      HeapObjectReference::Strong(transitions));
48 49
}

50 51 52 53 54 55 56
int TransitionArray::NumberOfPrototypeTransitions(
    WeakFixedArray* proto_transitions) {
  if (proto_transitions->length() == 0) return 0;
  MaybeObject* raw =
      proto_transitions->Get(kProtoTransitionNumberOfEntriesOffset);
  return Smi::ToInt(raw->ToSmi());
}
57

58
Name* TransitionArray::GetKey(int transition_number) {
59
  DCHECK(transition_number < number_of_transitions());
60
  return Name::cast(Get(ToKeyIndex(transition_number))->ToStrongHeapObject());
61 62
}

63 64 65 66 67 68
Name* TransitionsAccessor::GetKey(int transition_number) {
  switch (encoding()) {
    case kPrototypeInfo:
    case kUninitialized:
      UNREACHABLE();
      return nullptr;
69 70 71
    case kWeakRef: {
      Map* map = Map::cast(raw_transitions_->ToWeakHeapObject());
      return GetSimpleTransitionKey(map);
72
    }
73 74
    case kFullTransitionArray:
      return transitions()->GetKey(transition_number);
75
  }
76
  UNREACHABLE();
77 78
}

79
void TransitionArray::SetKey(int transition_number, Name* key) {
80
  DCHECK(transition_number < number_of_transitions());
81 82
  WeakFixedArray::Set(ToKeyIndex(transition_number),
                      HeapObjectReference::Strong(key));
83 84
}

85
HeapObjectReference** TransitionArray::GetTargetSlot(int transition_number) {
86
  DCHECK(transition_number < number_of_transitions());
87 88
  return reinterpret_cast<HeapObjectReference**>(
      RawFieldOfElementAt(ToTargetIndex(transition_number)));
89 90
}

91
// static
92 93
PropertyDetails TransitionsAccessor::GetTargetDetails(Name* name, Map* target) {
  DCHECK(!IsSpecialTransition(name->GetReadOnlyRoots(), name));
94 95 96 97 98 99
  int descriptor = target->LastAdded();
  DescriptorArray* descriptors = target->instance_descriptors();
  // Transitions are allowed only for the last added property.
  DCHECK(descriptors->GetKey(descriptor)->Equals(name));
  return descriptors->GetDetails(descriptor);
}
100

101
// static
102 103
Map* TransitionsAccessor::GetTargetFromRaw(MaybeObject* raw) {
  return Map::cast(raw->ToWeakHeapObject());
104 105
}

106
MaybeObject* TransitionArray::GetRawTarget(int transition_number) {
107
  DCHECK(transition_number < number_of_transitions());
108
  return Get(ToTargetIndex(transition_number));
109
}
110

111
Map* TransitionArray::GetTarget(int transition_number) {
112
  MaybeObject* raw = GetRawTarget(transition_number);
113 114 115 116 117 118 119 120 121
  return TransitionsAccessor::GetTargetFromRaw(raw);
}

Map* TransitionsAccessor::GetTarget(int transition_number) {
  switch (encoding()) {
    case kPrototypeInfo:
    case kUninitialized:
      UNREACHABLE();
      return nullptr;
122 123
    case kWeakRef:
      return Map::cast(raw_transitions_->ToWeakHeapObject());
124 125 126
    case kFullTransitionArray:
      return transitions()->GetTarget(transition_number);
  }
127
  UNREACHABLE();
128 129
}

130
void TransitionArray::SetRawTarget(int transition_number, MaybeObject* value) {
131
  DCHECK(transition_number < number_of_transitions());
132 133
  DCHECK(value->IsWeakHeapObject() && value->ToWeakHeapObject()->IsMap());
  WeakFixedArray::Set(ToTargetIndex(transition_number), value);
134 135
}

136 137
bool TransitionArray::GetTargetIfExists(int transition_number, Isolate* isolate,
                                        Map** target) {
138 139 140 141
  MaybeObject* raw = GetRawTarget(transition_number);
  HeapObject* heap_object;
  if (raw->ToStrongHeapObject(&heap_object) &&
      heap_object->IsUndefined(isolate)) {
142 143 144 145 146
    return false;
  }
  *target = TransitionsAccessor::GetTargetFromRaw(raw);
  return true;
}
147

148
int TransitionArray::SearchName(Name* name, int* out_insertion_index) {
149
  DCHECK(name->IsUniqueName());
150 151
  return internal::Search<ALL_ENTRIES>(this, name, number_of_entries(),
                                       out_insertion_index);
152 153
}

154 155 156 157 158
int TransitionArray::number_of_transitions() const {
  if (length() < kFirstIndex) return 0;
  return Smi::ToInt(Get(kTransitionLengthIndex)->ToSmi());
}

159
int TransitionArray::CompareKeys(Name* key1, uint32_t hash1, PropertyKind kind1,
160
                                 PropertyAttributes attributes1, Name* key2,
161
                                 uint32_t hash2, PropertyKind kind2,
162 163 164 165
                                 PropertyAttributes attributes2) {
  int cmp = CompareNames(key1, hash1, key2, hash2);
  if (cmp != 0) return cmp;

166
  return CompareDetails(kind1, attributes1, kind2, attributes2);
167 168 169 170 171 172 173 174 175 176 177 178
}

int TransitionArray::CompareNames(Name* key1, uint32_t hash1, Name* key2,
                                  uint32_t hash2) {
  if (key1 != key2) {
    // In case of hash collisions key1 is always "less" than key2.
    return hash1 <= hash2 ? -1 : 1;
  }

  return 0;
}

179
int TransitionArray::CompareDetails(PropertyKind kind1,
180
                                    PropertyAttributes attributes1,
181
                                    PropertyKind kind2,
182
                                    PropertyAttributes attributes2) {
183 184
  if (kind1 != kind2) {
    return static_cast<int>(kind1) < static_cast<int>(kind2) ? -1 : 1;
185 186 187 188 189 190 191 192 193 194
  }

  if (attributes1 != attributes2) {
    return static_cast<int>(attributes1) < static_cast<int>(attributes2) ? -1
                                                                         : 1;
  }

  return 0;
}

195 196 197 198 199
void TransitionArray::Set(int transition_number, Name* key,
                          MaybeObject* target) {
  WeakFixedArray::Set(ToKeyIndex(transition_number),
                      MaybeObject::FromObject(key));
  WeakFixedArray::Set(ToTargetIndex(transition_number), target);
200 201
}

202 203
int TransitionArray::Capacity() {
  if (length() <= kFirstIndex) return 0;
204
  return (length() - kFirstIndex) / kEntrySize;
205
}
206

207
void TransitionArray::SetNumberOfTransitions(int number_of_transitions) {
208
  DCHECK(number_of_transitions <= Capacity());
209 210 211
  WeakFixedArray::Set(
      kTransitionLengthIndex,
      MaybeObject::FromSmi(Smi::FromInt(number_of_transitions)));
212 213
}

214 215
}  // namespace internal
}  // namespace v8
216

217 218
#include "src/objects/object-macros-undef.h"

219
#endif  // V8_TRANSITIONS_INL_H_