Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(264)

Side by Side Diff: src/objects.cc

Issue 203333004: Handlification of JSArray::SetElementsLength(). (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Addressing review notes Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/objects.h ('k') | test/cctest/test-heap.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 11421 matching lines...) Expand 10 before | Expand all | Expand 10 after
11432 11432
11433 bool threw; 11433 bool threw;
11434 Execution::Call(isolate, 11434 Execution::Call(isolate,
11435 Handle<JSFunction>(isolate->observers_end_perform_splice()), 11435 Handle<JSFunction>(isolate->observers_end_perform_splice()),
11436 isolate->factory()->undefined_value(), ARRAY_SIZE(args), args, 11436 isolate->factory()->undefined_value(), ARRAY_SIZE(args), args,
11437 &threw); 11437 &threw);
11438 ASSERT(!threw); 11438 ASSERT(!threw);
11439 } 11439 }
11440 11440
11441 11441
11442 // TODO(ishell): Temporary wrapper until handlified.
11443 // static 11442 // static
Yang 2014/03/19 13:54:20 I think you can remove that comment too.
Igor Sheludko 2014/03/19 14:14:23 Done.
11444 Handle<Object> JSArray::SetElementsLength(Handle<JSArray> array, 11443 Handle<Object> JSArray::SetElementsLength(Handle<JSArray> array,
11445 Handle<Object> length) { 11444 Handle<Object> length) {
11446 CALL_HEAP_FUNCTION(array->GetIsolate(), 11445 // We should never end in here with a pixel or external array.
11447 array->SetElementsLength(*length), 11446 ASSERT(array->AllowsSetElementsLength());
11448 Object); 11447 if (!array->map()->is_observed()) {
11449 } 11448 return array->GetElementsAccessor()->SetLength(array, length);
11449 }
11450 11450
11451 11451 Isolate* isolate = array->GetIsolate();
11452 MaybeObject* JSArray::SetElementsLength(Object* len) {
11453 // We should never end in here with a pixel or external array.
11454 ASSERT(AllowsSetElementsLength());
11455 if (!map()->is_observed())
11456 return GetElementsAccessor()->SetLength(this, len);
11457
11458 Isolate* isolate = GetIsolate();
11459 HandleScope scope(isolate);
11460 Handle<JSArray> self(this);
11461 List<uint32_t> indices; 11452 List<uint32_t> indices;
11462 List<Handle<Object> > old_values; 11453 List<Handle<Object> > old_values;
11463 Handle<Object> old_length_handle(self->length(), isolate); 11454 Handle<Object> old_length_handle(array->length(), isolate);
11464 Handle<Object> new_length_handle(len, isolate); 11455 Handle<Object> new_length_handle = length;
Yang 2014/03/19 13:54:20 Can we either rename the argument or rename the us
Igor Sheludko 2014/03/19 14:14:23 Done.
11465 uint32_t old_length = 0; 11456 uint32_t old_length = 0;
11466 CHECK(old_length_handle->ToArrayIndex(&old_length)); 11457 CHECK(old_length_handle->ToArrayIndex(&old_length));
11467 uint32_t new_length = 0; 11458 uint32_t new_length = 0;
11468 if (!new_length_handle->ToArrayIndex(&new_length)) 11459 CHECK(new_length_handle->ToArrayIndex(&new_length));
11469 return Failure::InternalError();
11470 11460
11471 static const PropertyAttributes kNoAttrFilter = NONE; 11461 static const PropertyAttributes kNoAttrFilter = NONE;
11472 int num_elements = self->NumberOfLocalElements(kNoAttrFilter); 11462 int num_elements = array->NumberOfLocalElements(kNoAttrFilter);
11473 if (num_elements > 0) { 11463 if (num_elements > 0) {
11474 if (old_length == static_cast<uint32_t>(num_elements)) { 11464 if (old_length == static_cast<uint32_t>(num_elements)) {
11475 // Simple case for arrays without holes. 11465 // Simple case for arrays without holes.
11476 for (uint32_t i = old_length - 1; i + 1 > new_length; --i) { 11466 for (uint32_t i = old_length - 1; i + 1 > new_length; --i) {
11477 if (!GetOldValue(isolate, self, i, &old_values, &indices)) break; 11467 if (!GetOldValue(isolate, array, i, &old_values, &indices)) break;
11478 } 11468 }
11479 } else { 11469 } else {
11480 // For sparse arrays, only iterate over existing elements. 11470 // For sparse arrays, only iterate over existing elements.
11481 // TODO(rafaelw): For fast, sparse arrays, we can avoid iterating over 11471 // TODO(rafaelw): For fast, sparse arrays, we can avoid iterating over
11482 // the to-be-removed indices twice. 11472 // the to-be-removed indices twice.
11483 Handle<FixedArray> keys = isolate->factory()->NewFixedArray(num_elements); 11473 Handle<FixedArray> keys = isolate->factory()->NewFixedArray(num_elements);
11484 self->GetLocalElementKeys(*keys, kNoAttrFilter); 11474 array->GetLocalElementKeys(*keys, kNoAttrFilter);
11485 while (num_elements-- > 0) { 11475 while (num_elements-- > 0) {
11486 uint32_t index = NumberToUint32(keys->get(num_elements)); 11476 uint32_t index = NumberToUint32(keys->get(num_elements));
11487 if (index < new_length) break; 11477 if (index < new_length) break;
11488 if (!GetOldValue(isolate, self, index, &old_values, &indices)) break; 11478 if (!GetOldValue(isolate, array, index, &old_values, &indices)) break;
11489 } 11479 }
11490 } 11480 }
11491 } 11481 }
11492 11482
11493 MaybeObject* result = 11483 Handle<Object> hresult =
11494 self->GetElementsAccessor()->SetLength(*self, *new_length_handle); 11484 array->GetElementsAccessor()->SetLength(array, new_length_handle);
11495 Handle<Object> hresult; 11485 RETURN_IF_EMPTY_HANDLE_VALUE(isolate, hresult, hresult);
11496 if (!result->ToHandle(&hresult, isolate)) return result;
11497 11486
11498 CHECK(self->length()->ToArrayIndex(&new_length)); 11487 CHECK(array->length()->ToArrayIndex(&new_length));
11499 if (old_length == new_length) return *hresult; 11488 if (old_length == new_length) return hresult;
11500 11489
11501 BeginPerformSplice(self); 11490 BeginPerformSplice(array);
11502 11491
11503 for (int i = 0; i < indices.length(); ++i) { 11492 for (int i = 0; i < indices.length(); ++i) {
11504 JSObject::EnqueueChangeRecord( 11493 JSObject::EnqueueChangeRecord(
11505 self, "delete", isolate->factory()->Uint32ToString(indices[i]), 11494 array, "delete", isolate->factory()->Uint32ToString(indices[i]),
11506 old_values[i]); 11495 old_values[i]);
11507 } 11496 }
11508 JSObject::EnqueueChangeRecord( 11497 JSObject::EnqueueChangeRecord(
11509 self, "update", isolate->factory()->length_string(), 11498 array, "update", isolate->factory()->length_string(),
11510 old_length_handle); 11499 old_length_handle);
11511 11500
11512 EndPerformSplice(self); 11501 EndPerformSplice(array);
11513 11502
11514 uint32_t index = Min(old_length, new_length); 11503 uint32_t index = Min(old_length, new_length);
11515 uint32_t add_count = new_length > old_length ? new_length - old_length : 0; 11504 uint32_t add_count = new_length > old_length ? new_length - old_length : 0;
11516 uint32_t delete_count = new_length < old_length ? old_length - new_length : 0; 11505 uint32_t delete_count = new_length < old_length ? old_length - new_length : 0;
11517 Handle<JSArray> deleted = isolate->factory()->NewJSArray(0); 11506 Handle<JSArray> deleted = isolate->factory()->NewJSArray(0);
11518 if (delete_count > 0) { 11507 if (delete_count > 0) {
11519 for (int i = indices.length() - 1; i >= 0; i--) { 11508 for (int i = indices.length() - 1; i >= 0; i--) {
11520 JSObject::SetElement(deleted, indices[i] - index, old_values[i], NONE, 11509 JSObject::SetElement(deleted, indices[i] - index, old_values[i], NONE,
11521 SLOPPY); 11510 SLOPPY);
11522 } 11511 }
11523 11512
11524 SetProperty(deleted, isolate->factory()->length_string(), 11513 SetProperty(deleted, isolate->factory()->length_string(),
11525 isolate->factory()->NewNumberFromUint(delete_count), 11514 isolate->factory()->NewNumberFromUint(delete_count),
11526 NONE, SLOPPY); 11515 NONE, SLOPPY);
11527 } 11516 }
11528 11517
11529 EnqueueSpliceRecord(self, index, deleted, add_count); 11518 EnqueueSpliceRecord(array, index, deleted, add_count);
11530 11519
11531 return *hresult; 11520 return hresult;
11532 } 11521 }
11533 11522
11534 11523
11535 Handle<Map> Map::GetPrototypeTransition(Handle<Map> map, 11524 Handle<Map> Map::GetPrototypeTransition(Handle<Map> map,
11536 Handle<Object> prototype) { 11525 Handle<Object> prototype) {
11537 FixedArray* cache = map->GetPrototypeTransitions(); 11526 FixedArray* cache = map->GetPrototypeTransitions();
11538 int number_of_transitions = map->NumberOfProtoTransitions(); 11527 int number_of_transitions = map->NumberOfProtoTransitions();
11539 const int proto_offset = 11528 const int proto_offset =
11540 kProtoTransitionHeaderSize + kProtoTransitionPrototypeOffset; 11529 kProtoTransitionHeaderSize + kProtoTransitionPrototypeOffset;
11541 const int map_offset = kProtoTransitionHeaderSize + kProtoTransitionMapOffset; 11530 const int map_offset = kProtoTransitionHeaderSize + kProtoTransitionMapOffset;
(...skipping 4987 matching lines...) Expand 10 before | Expand all | Expand 10 after
16529 #define ERROR_MESSAGES_TEXTS(C, T) T, 16518 #define ERROR_MESSAGES_TEXTS(C, T) T,
16530 static const char* error_messages_[] = { 16519 static const char* error_messages_[] = {
16531 ERROR_MESSAGES_LIST(ERROR_MESSAGES_TEXTS) 16520 ERROR_MESSAGES_LIST(ERROR_MESSAGES_TEXTS)
16532 }; 16521 };
16533 #undef ERROR_MESSAGES_TEXTS 16522 #undef ERROR_MESSAGES_TEXTS
16534 return error_messages_[reason]; 16523 return error_messages_[reason];
16535 } 16524 }
16536 16525
16537 16526
16538 } } // namespace v8::internal 16527 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/objects.h ('k') | test/cctest/test-heap.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698