2012-07-05 13:54:20 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2012-07-05 13:54:20 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2012-07-05 13:54:20 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/objects.h"
|
|
|
|
#include "src/transitions-inl.h"
|
|
|
|
#include "src/utils.h"
|
2012-07-05 13:54:20 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
|
2014-04-11 12:13:53 +00:00
|
|
|
Handle<TransitionArray> TransitionArray::Allocate(Isolate* isolate,
|
2014-11-03 16:44:58 +00:00
|
|
|
int number_of_transitions,
|
|
|
|
int slack) {
|
|
|
|
Handle<FixedArray> array = isolate->factory()->NewFixedArray(
|
|
|
|
LengthFor(number_of_transitions + slack));
|
2012-07-10 07:53:00 +00:00
|
|
|
array->set(kPrototypeTransitionsIndex, Smi::FromInt(0));
|
2014-11-03 16:44:58 +00:00
|
|
|
array->set(kTransitionLengthIndex, Smi::FromInt(number_of_transitions));
|
2014-04-11 12:13:53 +00:00
|
|
|
return Handle<TransitionArray>::cast(array);
|
2012-07-05 13:54:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-11 12:13:53 +00:00
|
|
|
Handle<TransitionArray> TransitionArray::AllocateSimple(Isolate* isolate,
|
|
|
|
Handle<Map> target) {
|
|
|
|
Handle<FixedArray> array =
|
|
|
|
isolate->factory()->NewFixedArray(kSimpleTransitionSize);
|
|
|
|
array->set(kSimpleTransitionTarget, *target);
|
|
|
|
return Handle<TransitionArray>::cast(array);
|
2014-04-09 14:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-14 15:10:31 +00:00
|
|
|
void TransitionArray::NoIncrementalWriteBarrierCopyFrom(TransitionArray* origin,
|
|
|
|
int origin_transition,
|
|
|
|
int target_transition) {
|
|
|
|
NoIncrementalWriteBarrierSet(target_transition,
|
|
|
|
origin->GetKey(origin_transition),
|
|
|
|
origin->GetTarget(origin_transition));
|
2012-07-05 13:54:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-04-10 13:06:52 +00:00
|
|
|
Handle<TransitionArray> TransitionArray::NewWith(Handle<Map> map,
|
|
|
|
Handle<Name> name,
|
2014-04-09 14:26:32 +00:00
|
|
|
Handle<Map> target,
|
2014-04-10 13:06:52 +00:00
|
|
|
SimpleTransitionFlag flag) {
|
2014-04-09 14:26:32 +00:00
|
|
|
Handle<TransitionArray> result;
|
2014-04-11 12:13:53 +00:00
|
|
|
Isolate* isolate = name->GetIsolate();
|
2012-09-19 09:54:10 +00:00
|
|
|
|
2014-11-05 09:26:22 +00:00
|
|
|
if (flag == SIMPLE_PROPERTY_TRANSITION) {
|
2014-04-11 12:13:53 +00:00
|
|
|
result = AllocateSimple(isolate, target);
|
2012-09-19 09:54:10 +00:00
|
|
|
} else {
|
2014-04-11 12:13:53 +00:00
|
|
|
result = Allocate(isolate, 1);
|
2014-04-10 13:06:52 +00:00
|
|
|
result->NoIncrementalWriteBarrierSet(0, *name, *target);
|
2012-09-19 09:54:10 +00:00
|
|
|
}
|
2014-04-10 13:06:52 +00:00
|
|
|
result->set_back_pointer_storage(map->GetBackPointer());
|
2012-09-19 09:54:10 +00:00
|
|
|
return result;
|
|
|
|
}
|
2012-07-05 13:54:20 +00:00
|
|
|
|
|
|
|
|
2014-04-11 12:13:53 +00:00
|
|
|
Handle<TransitionArray> TransitionArray::ExtendToFullTransitionArray(
|
2014-04-11 14:25:00 +00:00
|
|
|
Handle<Map> containing_map) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!containing_map->transitions()->IsFullTransitionArray());
|
2014-04-11 14:25:00 +00:00
|
|
|
int nof = containing_map->transitions()->number_of_transitions();
|
2012-09-19 09:54:10 +00:00
|
|
|
|
2014-04-11 14:25:00 +00:00
|
|
|
// A transition array may shrink during GC.
|
|
|
|
Handle<TransitionArray> result = Allocate(containing_map->GetIsolate(), nof);
|
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
int new_nof = containing_map->transitions()->number_of_transitions();
|
|
|
|
if (new_nof != nof) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(new_nof == 0);
|
2014-04-11 14:25:00 +00:00
|
|
|
result->Shrink(ToKeyIndex(0));
|
2014-11-03 16:44:58 +00:00
|
|
|
result->SetNumberOfTransitions(0);
|
2014-04-11 14:25:00 +00:00
|
|
|
} else if (nof == 1) {
|
2014-04-11 12:13:53 +00:00
|
|
|
result->NoIncrementalWriteBarrierCopyFrom(
|
2014-04-11 14:25:00 +00:00
|
|
|
containing_map->transitions(), kSimpleTransitionIndex, 0);
|
2012-09-19 09:54:10 +00:00
|
|
|
}
|
|
|
|
|
2014-04-11 14:25:00 +00:00
|
|
|
result->set_back_pointer_storage(
|
|
|
|
containing_map->transitions()->back_pointer_storage());
|
2012-07-05 13:54:20 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-03 16:44:58 +00:00
|
|
|
Handle<TransitionArray> TransitionArray::Insert(Handle<Map> map,
|
|
|
|
Handle<Name> name,
|
|
|
|
Handle<Map> target,
|
|
|
|
SimpleTransitionFlag flag) {
|
2014-04-10 13:06:52 +00:00
|
|
|
if (!map->HasTransitionArray()) {
|
|
|
|
return TransitionArray::NewWith(map, name, target, flag);
|
|
|
|
}
|
2012-07-05 13:54:20 +00:00
|
|
|
|
2014-04-09 14:26:32 +00:00
|
|
|
int number_of_transitions = map->transitions()->number_of_transitions();
|
2014-11-03 16:44:58 +00:00
|
|
|
int new_nof = number_of_transitions;
|
2012-07-05 13:54:20 +00:00
|
|
|
|
2014-11-05 09:26:22 +00:00
|
|
|
bool is_special_transition = flag == SPECIAL_TRANSITION;
|
|
|
|
DCHECK_EQ(is_special_transition, IsSpecialTransition(*name));
|
|
|
|
PropertyDetails details = is_special_transition
|
|
|
|
? PropertyDetails(NONE, NORMAL, 0)
|
|
|
|
: GetTargetDetails(*name, *target);
|
2014-11-04 09:29:31 +00:00
|
|
|
|
2014-11-05 09:26:22 +00:00
|
|
|
int insertion_index = kNotFound;
|
|
|
|
int index =
|
|
|
|
is_special_transition
|
|
|
|
? map->transitions()->SearchSpecial(Symbol::cast(*name),
|
|
|
|
&insertion_index)
|
|
|
|
: map->transitions()->Search(details.type(), *name,
|
|
|
|
details.attributes(), &insertion_index);
|
2014-11-04 09:29:31 +00:00
|
|
|
if (index == kNotFound) {
|
|
|
|
++new_nof;
|
|
|
|
} else {
|
|
|
|
insertion_index = index;
|
|
|
|
}
|
|
|
|
DCHECK(insertion_index >= 0 && insertion_index <= number_of_transitions);
|
|
|
|
|
2014-11-03 16:44:58 +00:00
|
|
|
CHECK(new_nof <= kMaxNumberOfTransitions);
|
|
|
|
|
|
|
|
if (new_nof <= map->transitions()->number_of_transitions_storage()) {
|
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
TransitionArray* array = map->transitions();
|
|
|
|
|
2014-11-04 09:29:31 +00:00
|
|
|
if (index != kNotFound) {
|
|
|
|
array->SetTarget(index, *target);
|
2014-11-03 16:44:58 +00:00
|
|
|
return handle(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
array->SetNumberOfTransitions(new_nof);
|
2014-11-04 09:29:31 +00:00
|
|
|
for (index = number_of_transitions; index > insertion_index; --index) {
|
|
|
|
Name* key = array->GetKey(index - 1);
|
|
|
|
array->SetKey(index, key);
|
|
|
|
array->SetTarget(index, array->GetTarget(index - 1));
|
2014-11-03 16:44:58 +00:00
|
|
|
}
|
2014-11-04 09:29:31 +00:00
|
|
|
array->SetKey(index, *name);
|
|
|
|
array->SetTarget(index, *target);
|
2014-11-05 09:26:22 +00:00
|
|
|
SLOW_DCHECK(array->IsSortedNoDuplicates());
|
2014-11-03 16:44:58 +00:00
|
|
|
return handle(array);
|
|
|
|
}
|
2012-07-05 13:54:20 +00:00
|
|
|
|
2014-11-03 16:44:58 +00:00
|
|
|
Handle<TransitionArray> result = Allocate(
|
|
|
|
map->GetIsolate(), new_nof,
|
|
|
|
Map::SlackForArraySize(number_of_transitions, kMaxNumberOfTransitions));
|
2014-04-09 14:26:32 +00:00
|
|
|
|
2014-04-14 08:58:00 +00:00
|
|
|
// The map's transition array may grown smaller during the allocation above as
|
|
|
|
// it was weakly traversed, though it is guaranteed not to disappear. Trim the
|
|
|
|
// result copy if needed, and recompute variables.
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(map->HasTransitionArray());
|
2014-04-09 14:26:32 +00:00
|
|
|
DisallowHeapAllocation no_gc;
|
|
|
|
TransitionArray* array = map->transitions();
|
|
|
|
if (array->number_of_transitions() != number_of_transitions) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(array->number_of_transitions() < number_of_transitions);
|
2014-04-09 14:26:32 +00:00
|
|
|
|
|
|
|
number_of_transitions = array->number_of_transitions();
|
2014-11-03 16:44:58 +00:00
|
|
|
new_nof = number_of_transitions;
|
2014-04-09 14:26:32 +00:00
|
|
|
|
2014-11-04 09:29:31 +00:00
|
|
|
insertion_index = kNotFound;
|
2014-11-05 09:26:22 +00:00
|
|
|
index = is_special_transition ? map->transitions()->SearchSpecial(
|
|
|
|
Symbol::cast(*name), &insertion_index)
|
|
|
|
: map->transitions()->Search(
|
|
|
|
details.type(), *name,
|
|
|
|
details.attributes(), &insertion_index);
|
2014-11-04 09:29:31 +00:00
|
|
|
if (index == kNotFound) {
|
|
|
|
++new_nof;
|
|
|
|
} else {
|
|
|
|
insertion_index = index;
|
|
|
|
}
|
|
|
|
DCHECK(insertion_index >= 0 && insertion_index <= number_of_transitions);
|
2012-07-05 13:54:20 +00:00
|
|
|
|
2014-11-03 16:44:58 +00:00
|
|
|
result->Shrink(ToKeyIndex(new_nof));
|
|
|
|
result->SetNumberOfTransitions(new_nof);
|
2014-04-09 14:26:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (array->HasPrototypeTransitions()) {
|
|
|
|
result->SetPrototypeTransitions(array->GetPrototypeTransitions());
|
2012-07-10 07:53:00 +00:00
|
|
|
}
|
|
|
|
|
2014-11-04 09:29:31 +00:00
|
|
|
DCHECK_NE(kNotFound, insertion_index);
|
|
|
|
for (int i = 0; i < insertion_index; ++i) {
|
|
|
|
result->NoIncrementalWriteBarrierCopyFrom(array, i, i);
|
2012-07-05 13:54:20 +00:00
|
|
|
}
|
2014-04-09 14:26:32 +00:00
|
|
|
result->NoIncrementalWriteBarrierSet(insertion_index, *name, *target);
|
2014-11-04 09:29:31 +00:00
|
|
|
for (int i = insertion_index; i < number_of_transitions; ++i) {
|
|
|
|
result->NoIncrementalWriteBarrierCopyFrom(array, i, i + 1);
|
2012-07-05 13:54:20 +00:00
|
|
|
}
|
|
|
|
|
2014-04-09 14:26:32 +00:00
|
|
|
result->set_back_pointer_storage(array->back_pointer_storage());
|
2014-11-05 09:26:22 +00:00
|
|
|
SLOW_DCHECK(result->IsSortedNoDuplicates());
|
2012-07-05 13:54:20 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-05 09:26:22 +00:00
|
|
|
int TransitionArray::SearchDetails(int transition, PropertyType type,
|
|
|
|
PropertyAttributes attributes,
|
|
|
|
int* out_insertion_index) {
|
|
|
|
int nof_transitions = number_of_transitions();
|
|
|
|
DCHECK(transition < nof_transitions);
|
|
|
|
Name* key = GetKey(transition);
|
|
|
|
bool is_data = type == FIELD || type == CONSTANT;
|
|
|
|
for (; transition < nof_transitions && GetKey(transition) == key;
|
|
|
|
transition++) {
|
|
|
|
Map* target = GetTarget(transition);
|
|
|
|
PropertyDetails target_details = GetTargetDetails(key, target);
|
|
|
|
|
|
|
|
bool target_is_data =
|
|
|
|
target_details.type() == FIELD || target_details.type() == CONSTANT;
|
|
|
|
|
|
|
|
int cmp = CompareDetails(is_data, attributes, target_is_data,
|
|
|
|
target_details.attributes());
|
|
|
|
if (cmp == 0) {
|
|
|
|
return transition;
|
|
|
|
} else if (cmp < 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (out_insertion_index != NULL) *out_insertion_index = transition;
|
|
|
|
return kNotFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int TransitionArray::Search(PropertyType type, Name* name,
|
|
|
|
PropertyAttributes attributes,
|
|
|
|
int* out_insertion_index) {
|
|
|
|
int transition = SearchName(name, out_insertion_index);
|
|
|
|
if (transition == kNotFound) {
|
|
|
|
return kNotFound;
|
|
|
|
}
|
|
|
|
return SearchDetails(transition, type, attributes, out_insertion_index);
|
|
|
|
}
|
2012-07-05 13:54:20 +00:00
|
|
|
} } // namespace v8::internal
|