Move EnsureFastWritableElements into the elements accessor.

Additionally clean up the elements accessor api a bit.

BUG=

Review URL: https://codereview.chromium.org/1770793002

Cr-Commit-Position: refs/heads/master@{#34549}
This commit is contained in:
verwaest 2016-03-07 08:25:00 -08:00 committed by Commit bot
parent b954c872aa
commit cf43d2a788
3 changed files with 80 additions and 156 deletions

View File

@ -248,51 +248,25 @@ inline bool EnsureJSArrayWithWritableFastElements(Isolate* isolate,
Handle<JSArray> array = Handle<JSArray>::cast(receiver);
// If there may be elements accessors in the prototype chain, the fast path
// cannot be used if there arguments to add to the array.
Heap* heap = isolate->heap();
if (args != NULL && !IsJSArrayFastElementMovingAllowed(isolate, *array)) {
if (args != nullptr && !IsJSArrayFastElementMovingAllowed(isolate, *array)) {
return false;
}
ElementsKind origin_kind = array->map()->elements_kind();
if (IsDictionaryElementsKind(origin_kind)) return false;
if (array->map()->is_observed()) return false;
if (!array->map()->is_extensible()) return false;
Map* map = array->elements()->map();
if (map == heap->fixed_array_map()) {
if (args == NULL || array->HasFastObjectElements()) {
return true;
}
} else if (map == heap->fixed_cow_array_map()) {
// Use a short-lived HandleScope to avoid creating several copies of the
// elements handle which would cause issues when left-trimming later-on.
HandleScope scope(isolate);
// TODO(jkummerow/verwaest): Move this call (or this entire function?)
// into the ElementsAccessor so it's only done when needed (e.g. ArrayPush
// can skip it because it must grow the backing store anyway).
JSObject::EnsureWritableFastElements(array);
if (args == NULL || array->HasFastObjectElements()) {
return true;
}
} else if (map == heap->fixed_double_array_map()) {
if (args == NULL) {
return true;
}
} else {
return false;
}
if (args == nullptr) return true;
// Adding elements to the array prototype would break code that makes sure
// it has no elements. Handle that elsewhere.
if (isolate->IsAnyInitialArrayPrototype(array)) {
return false;
}
if (isolate->IsAnyInitialArrayPrototype(array)) return false;
// Need to ensure that the arguments passed in args can be contained in
// the array.
int args_length = args->length();
if (first_added_arg >= args_length) {
return true;
}
if (first_added_arg >= args_length) return true;
ElementsKind origin_kind = array->map()->elements_kind();
DCHECK(!IsFastObjectElementsKind(origin_kind));
if (IsFastObjectElementsKind(origin_kind)) return true;
ElementsKind target_kind = origin_kind;
{
DisallowHeapAllocation no_gc;
@ -437,20 +411,20 @@ BUILTIN(ArrayPush) {
return CallJsIntrinsic(isolate, isolate->array_push(), args);
}
// Fast Elements Path
int push_size = args.length() - 1;
int to_add = args.length() - 1;
Handle<JSArray> array = Handle<JSArray>::cast(receiver);
int len = Smi::cast(array->length())->value();
if (push_size == 0) {
return Smi::FromInt(len);
}
DCHECK(push_size > 0);
if (to_add == 0) return Smi::FromInt(len);
// Currently fixed arrays cannot grow too big, so we should never hit this.
DCHECK_LE(to_add, Smi::kMaxValue - Smi::cast(array->length())->value());
if (JSArray::HasReadOnlyLength(array)) {
return CallJsIntrinsic(isolate, isolate->array_push(), args);
}
DCHECK(!array->map()->is_observed());
ElementsAccessor* accessor = array->GetElementsAccessor();
int new_length = accessor->Push(array, handle(array->elements(), isolate),
&args, push_size);
int new_length = accessor->Push(array, &args, to_add);
return Smi::FromInt(new_length);
}
@ -458,7 +432,7 @@ BUILTIN(ArrayPush) {
BUILTIN(ArrayPop) {
HandleScope scope(isolate);
Handle<Object> receiver = args.receiver();
if (!EnsureJSArrayWithWritableFastElements(isolate, receiver, NULL, 0)) {
if (!EnsureJSArrayWithWritableFastElements(isolate, receiver, nullptr, 0)) {
return CallJsIntrinsic(isolate, isolate->array_pop(), args);
}
@ -475,8 +449,7 @@ BUILTIN(ArrayPop) {
Handle<Object> result;
if (IsJSArrayFastElementMovingAllowed(isolate, JSArray::cast(*receiver))) {
// Fast Elements Path
result = array->GetElementsAccessor()->Pop(
array, handle(array->elements(), isolate));
result = array->GetElementsAccessor()->Pop(array);
} else {
// Use Slow Lookup otherwise
uint32_t new_length = len - 1;
@ -492,7 +465,7 @@ BUILTIN(ArrayShift) {
HandleScope scope(isolate);
Heap* heap = isolate->heap();
Handle<Object> receiver = args.receiver();
if (!EnsureJSArrayWithWritableFastElements(isolate, receiver, NULL, 0) ||
if (!EnsureJSArrayWithWritableFastElements(isolate, receiver, nullptr, 0) ||
!IsJSArrayFastElementMovingAllowed(isolate, JSArray::cast(*receiver))) {
return CallJsIntrinsic(isolate, isolate->array_shift(), args);
}
@ -506,8 +479,7 @@ BUILTIN(ArrayShift) {
return CallJsIntrinsic(isolate, isolate->array_shift(), args);
}
Handle<Object> first = array->GetElementsAccessor()->Shift(
array, handle(array->elements(), isolate));
Handle<Object> first = array->GetElementsAccessor()->Shift(array);
return *first;
}
@ -521,20 +493,17 @@ BUILTIN(ArrayUnshift) {
Handle<JSArray> array = Handle<JSArray>::cast(receiver);
DCHECK(!array->map()->is_observed());
int to_add = args.length() - 1;
if (to_add == 0) {
return array->length();
}
// Currently fixed arrays cannot grow too big, so
// we should never hit this case.
DCHECK(to_add <= (Smi::kMaxValue - Smi::cast(array->length())->value()));
if (to_add == 0) return array->length();
if (to_add > 0 && JSArray::HasReadOnlyLength(array)) {
// Currently fixed arrays cannot grow too big, so we should never hit this.
DCHECK_LE(to_add, Smi::kMaxValue - Smi::cast(array->length())->value());
if (JSArray::HasReadOnlyLength(array)) {
return CallJsIntrinsic(isolate, isolate->array_unshift(), args);
}
ElementsAccessor* accessor = array->GetElementsAccessor();
int new_length = accessor->Unshift(array, handle(array->elements(), isolate),
&args, to_add);
int new_length = accessor->Unshift(array, &args, to_add);
return Smi::FromInt(new_length);
}
@ -542,12 +511,9 @@ BUILTIN(ArrayUnshift) {
BUILTIN(ArraySlice) {
HandleScope scope(isolate);
Handle<Object> receiver = args.receiver();
Handle<JSObject> object;
Handle<FixedArrayBase> elms_obj;
int len = -1;
int relative_start = 0;
int relative_end = 0;
bool is_sloppy_arguments = false;
if (receiver->IsJSArray()) {
DisallowHeapAllocation no_gc;
@ -561,22 +527,18 @@ BUILTIN(ArraySlice) {
return CallJsIntrinsic(isolate, isolate->array_slice(), args);
}
len = Smi::cast(array->length())->value();
object = Handle<JSObject>::cast(receiver);
elms_obj = handle(array->elements(), isolate);
} else if (receiver->IsJSObject() &&
GetSloppyArgumentsLength(isolate, Handle<JSObject>::cast(receiver),
&len)) {
DCHECK_EQ(FAST_ELEMENTS, JSObject::cast(*receiver)->GetElementsKind());
// Array.prototype.slice(arguments, ...) is quite a common idiom
// (notably more than 50% of invocations in Web apps).
// Treat it in C++ as well.
is_sloppy_arguments = true;
object = Handle<JSObject>::cast(receiver);
elms_obj = handle(object->elements(), isolate);
} else {
AllowHeapAllocation allow_allocation;
return CallJsIntrinsic(isolate, isolate->array_slice(), args);
}
DCHECK(len >= 0);
DCHECK_LE(0, len);
int argument_count = args.length() - 1;
// Note carefully chosen defaults---if argument is missing,
// it's undefined which gets converted to 0 for relative_start
@ -609,22 +571,9 @@ BUILTIN(ArraySlice) {
uint32_t actual_end =
(relative_end < 0) ? Max(len + relative_end, 0) : Min(relative_end, len);
if (actual_end <= actual_start) {
Handle<JSArray> result_array = isolate->factory()->NewJSArray(
GetPackedElementsKind(object->GetElementsKind()), 0, 0);
return *result_array;
}
Handle<JSObject> object = Handle<JSObject>::cast(receiver);
ElementsAccessor* accessor = object->GetElementsAccessor();
if (is_sloppy_arguments &&
!accessor->IsPacked(object, elms_obj, actual_start, actual_end)) {
// Don't deal with arguments with holes in C++
AllowHeapAllocation allow_allocation;
return CallJsIntrinsic(isolate, isolate->array_slice(), args);
}
Handle<JSArray> result_array =
accessor->Slice(object, elms_obj, actual_start, actual_end);
return *result_array;
return *accessor->Slice(object, actual_start, actual_end);
}
@ -683,9 +632,8 @@ BUILTIN(ArraySplice) {
return CallJsIntrinsic(isolate, isolate->array_splice(), args);
}
ElementsAccessor* accessor = array->GetElementsAccessor();
Handle<JSArray> result_array =
accessor->Splice(array, handle(array->elements(), isolate), actual_start,
actual_delete_count, &args, add_count);
Handle<JSArray> result_array = accessor->Splice(
array, actual_start, actual_delete_count, &args, add_count);
return *result_array;
}

View File

@ -502,12 +502,6 @@ class ElementsAccessorBase : public ElementsAccessor {
ElementsAccessorSubclass::ValidateImpl(holder);
}
bool IsPacked(Handle<JSObject> holder, Handle<FixedArrayBase> backing_store,
uint32_t start, uint32_t end) final {
return ElementsAccessorSubclass::IsPackedImpl(holder, backing_store, start,
end);
}
static bool IsPackedImpl(Handle<JSObject> holder,
Handle<FixedArrayBase> backing_store, uint32_t start,
uint32_t end) {
@ -597,81 +591,67 @@ class ElementsAccessorBase : public ElementsAccessor {
UNREACHABLE();
}
uint32_t Push(Handle<JSArray> receiver, Handle<FixedArrayBase> backing_store,
Arguments* args, uint32_t push_size) final {
return ElementsAccessorSubclass::PushImpl(receiver, backing_store, args,
push_size);
uint32_t Push(Handle<JSArray> receiver, Arguments* args,
uint32_t push_size) final {
return ElementsAccessorSubclass::PushImpl(receiver, args, push_size);
}
static uint32_t PushImpl(Handle<JSArray> receiver,
Handle<FixedArrayBase> elms_obj, Arguments* args,
static uint32_t PushImpl(Handle<JSArray> receiver, Arguments* args,
uint32_t push_sized) {
UNREACHABLE();
return 0;
}
uint32_t Unshift(Handle<JSArray> receiver,
Handle<FixedArrayBase> backing_store, Arguments* args,
uint32_t Unshift(Handle<JSArray> receiver, Arguments* args,
uint32_t unshift_size) final {
return ElementsAccessorSubclass::UnshiftImpl(receiver, backing_store, args,
unshift_size);
return ElementsAccessorSubclass::UnshiftImpl(receiver, args, unshift_size);
}
static uint32_t UnshiftImpl(Handle<JSArray> receiver,
Handle<FixedArrayBase> elms_obj, Arguments* args,
static uint32_t UnshiftImpl(Handle<JSArray> receiver, Arguments* args,
uint32_t unshift_size) {
UNREACHABLE();
return 0;
}
Handle<JSArray> Slice(Handle<JSObject> receiver,
Handle<FixedArrayBase> backing_store, uint32_t start,
Handle<JSArray> Slice(Handle<JSObject> receiver, uint32_t start,
uint32_t end) final {
return ElementsAccessorSubclass::SliceImpl(receiver, backing_store, start,
end);
return ElementsAccessorSubclass::SliceImpl(receiver, start, end);
}
static Handle<JSArray> SliceImpl(Handle<JSObject> receiver,
Handle<FixedArrayBase> backing_store,
uint32_t start, uint32_t end) {
UNREACHABLE();
return Handle<JSArray>();
}
Handle<JSArray> Splice(Handle<JSArray> receiver,
Handle<FixedArrayBase> backing_store, uint32_t start,
Handle<JSArray> Splice(Handle<JSArray> receiver, uint32_t start,
uint32_t delete_count, Arguments* args,
uint32_t add_count) final {
return ElementsAccessorSubclass::SpliceImpl(receiver, backing_store, start,
delete_count, args, add_count);
return ElementsAccessorSubclass::SpliceImpl(receiver, start, delete_count,
args, add_count);
}
static Handle<JSArray> SpliceImpl(Handle<JSArray> receiver,
Handle<FixedArrayBase> backing_store,
uint32_t start, uint32_t delete_count,
Arguments* args, uint32_t add_count) {
UNREACHABLE();
return Handle<JSArray>();
}
Handle<Object> Pop(Handle<JSArray> receiver,
Handle<FixedArrayBase> backing_store) final {
return ElementsAccessorSubclass::PopImpl(receiver, backing_store);
Handle<Object> Pop(Handle<JSArray> receiver) final {
return ElementsAccessorSubclass::PopImpl(receiver);
}
static Handle<Object> PopImpl(Handle<JSArray> receiver,
Handle<FixedArrayBase> backing_store) {
static Handle<Object> PopImpl(Handle<JSArray> receiver) {
UNREACHABLE();
return Handle<Object>();
}
Handle<Object> Shift(Handle<JSArray> receiver,
Handle<FixedArrayBase> backing_store) final {
return ElementsAccessorSubclass::ShiftImpl(receiver, backing_store);
Handle<Object> Shift(Handle<JSArray> receiver) final {
return ElementsAccessorSubclass::ShiftImpl(receiver);
}
static Handle<Object> ShiftImpl(Handle<JSArray> receiver,
Handle<FixedArrayBase> backing_store) {
static Handle<Object> ShiftImpl(Handle<JSArray> receiver) {
UNREACHABLE();
return Handle<Object>();
}
@ -703,7 +683,7 @@ class ElementsAccessorBase : public ElementsAccessor {
if (length == 0) {
array->initialize_elements();
} else if (length <= capacity) {
if (array->HasFastSmiOrObjectElements()) {
if (IsFastSmiOrObjectElementsKind(kind())) {
JSObject::EnsureWritableFastElements(array);
if (array->elements() != *backing_store) {
backing_store = handle(array->elements(), isolate);
@ -1462,28 +1442,24 @@ class FastElementsAccessor
#endif
}
static Handle<Object> PopImpl(Handle<JSArray> receiver,
Handle<FixedArrayBase> backing_store) {
return FastElementsAccessorSubclass::RemoveElement(receiver, backing_store,
AT_END);
static Handle<Object> PopImpl(Handle<JSArray> receiver) {
return FastElementsAccessorSubclass::RemoveElement(receiver, AT_END);
}
static Handle<Object> ShiftImpl(Handle<JSArray> receiver,
Handle<FixedArrayBase> backing_store) {
return FastElementsAccessorSubclass::RemoveElement(receiver, backing_store,
AT_START);
static Handle<Object> ShiftImpl(Handle<JSArray> receiver) {
return FastElementsAccessorSubclass::RemoveElement(receiver, AT_START);
}
static uint32_t PushImpl(Handle<JSArray> receiver,
Handle<FixedArrayBase> backing_store,
Arguments* args, uint32_t push_size) {
Handle<FixedArrayBase> backing_store(receiver->elements());
return FastElementsAccessorSubclass::AddArguments(receiver, backing_store,
args, push_size, AT_END);
}
static uint32_t UnshiftImpl(Handle<JSArray> receiver,
Handle<FixedArrayBase> backing_store,
Arguments* args, uint32_t unshift_size) {
Handle<FixedArrayBase> backing_store(receiver->elements());
return FastElementsAccessorSubclass::AddArguments(
receiver, backing_store, args, unshift_size, AT_START);
}
@ -1496,11 +1472,10 @@ class FastElementsAccessor
}
static Handle<JSArray> SliceImpl(Handle<JSObject> receiver,
Handle<FixedArrayBase> backing_store,
uint32_t start, uint32_t end) {
DCHECK(start < end);
Isolate* isolate = receiver->GetIsolate();
int result_len = end - start;
Handle<FixedArrayBase> backing_store(receiver->elements(), isolate);
int result_len = end < start ? 0u : end - start;
Handle<JSArray> result_array = isolate->factory()->NewJSArray(
KindTraits::Kind, result_len, result_len);
DisallowHeapAllocation no_gc;
@ -1513,7 +1488,6 @@ class FastElementsAccessor
}
static Handle<JSArray> SpliceImpl(Handle<JSArray> receiver,
Handle<FixedArrayBase> backing_store,
uint32_t start, uint32_t delete_count,
Arguments* args, uint32_t add_count) {
Isolate* isolate = receiver->GetIsolate();
@ -1521,6 +1495,15 @@ class FastElementsAccessor
uint32_t length = Smi::cast(receiver->length())->value();
uint32_t new_length = length - delete_count + add_count;
ElementsKind kind = KindTraits::Kind;
if (new_length <= static_cast<uint32_t>(receiver->elements()->length()) &&
IsFastSmiOrObjectElementsKind(kind)) {
HandleScope scope(isolate);
JSObject::EnsureWritableFastElements(receiver);
}
Handle<FixedArrayBase> backing_store(receiver->elements(), isolate);
if (new_length == 0) {
receiver->set_elements(heap->empty_fixed_array());
receiver->set_length(Smi::FromInt(0));
@ -1605,9 +1588,14 @@ class FastElementsAccessor
}
static Handle<Object> RemoveElement(Handle<JSArray> receiver,
Handle<FixedArrayBase> backing_store,
Where remove_position) {
Isolate* isolate = receiver->GetIsolate();
ElementsKind kind = KindTraits::Kind;
if (IsFastSmiOrObjectElementsKind(kind)) {
HandleScope scope(isolate);
JSObject::EnsureWritableFastElements(receiver);
}
Handle<FixedArrayBase> backing_store(receiver->elements(), isolate);
uint32_t length =
static_cast<uint32_t>(Smi::cast(receiver->length())->value());
DCHECK(length > 0);
@ -1622,8 +1610,8 @@ class FastElementsAccessor
FastElementsAccessorSubclass::SetLengthImpl(isolate, receiver, new_length,
backing_store);
if (IsHoleyElementsKind(KindTraits::Kind) && result->IsTheHole()) {
return receiver->GetIsolate()->factory()->undefined_value();
if (IsHoleyElementsKind(kind) && result->IsTheHole()) {
return isolate->factory()->undefined_value();
}
return result;
}
@ -1633,7 +1621,7 @@ class FastElementsAccessor
Arguments* args, uint32_t add_size,
Where remove_position) {
uint32_t length = Smi::cast(receiver->length())->value();
DCHECK(add_size > 0);
DCHECK(0 < add_size);
uint32_t elms_len = backing_store->length();
// Check we do not overflow the new_length.
DCHECK(add_size <= static_cast<uint32_t>(Smi::kMaxValue - length));
@ -2042,8 +2030,7 @@ class TypedElementsAccessor
static void AddElementsToKeyAccumulatorImpl(Handle<JSObject> receiver,
KeyAccumulator* accumulator,
AddKeyConversion convert) {
Handle<FixedArrayBase> elements(receiver->elements(),
receiver->GetIsolate());
Handle<FixedArrayBase> elements(receiver->elements());
uint32_t length = AccessorClass::GetCapacityImpl(*receiver, *elements);
for (uint32_t i = 0; i < length; i++) {
Handle<Object> value = AccessorClass::GetImpl(*elements, i);

View File

@ -52,11 +52,6 @@ class ElementsAccessor {
return HasElement(holder, index, handle(holder->elements()), filter);
}
// Returns true if the backing store is compact in the given range
virtual bool IsPacked(Handle<JSObject> holder,
Handle<FixedArrayBase> backing_store, uint32_t start,
uint32_t end) = 0;
virtual Handle<Object> Get(Handle<JSObject> holder, uint32_t entry) = 0;
virtual PropertyDetails GetDetails(JSObject* holder, uint32_t entry) = 0;
@ -136,28 +131,22 @@ class ElementsAccessor {
static Handle<JSArray> Concat(Isolate* isolate, Arguments* args,
uint32_t concat_size);
virtual uint32_t Push(Handle<JSArray> receiver,
Handle<FixedArrayBase> backing_store, Arguments* args,
virtual uint32_t Push(Handle<JSArray> receiver, Arguments* args,
uint32_t push_size) = 0;
virtual uint32_t Unshift(Handle<JSArray> receiver,
Handle<FixedArrayBase> backing_store,
Arguments* args, uint32_t unshift_size) = 0;
virtual Handle<JSArray> Slice(Handle<JSObject> receiver,
Handle<FixedArrayBase> backing_store,
uint32_t start, uint32_t end) = 0;
virtual Handle<JSArray> Splice(Handle<JSArray> receiver,
Handle<FixedArrayBase> backing_store,
uint32_t start, uint32_t delete_count,
Arguments* args, uint32_t add_count) = 0;
virtual Handle<Object> Pop(Handle<JSArray> receiver,
Handle<FixedArrayBase> backing_store) = 0;
virtual Handle<Object> Pop(Handle<JSArray> receiver) = 0;
virtual Handle<Object> Shift(Handle<JSArray> receiver,
Handle<FixedArrayBase> backing_store) = 0;
virtual Handle<Object> Shift(Handle<JSArray> receiver) = 0;
protected:
friend class LookupIterator;