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

Side by Side Diff: src/builtins.cc

Issue 223743003: Maybehandlification of EnsureJSArrayWithWritableFastElements(). (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 8 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 | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 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 276 matching lines...) Expand 10 before | Expand all | Expand 10 after
287 if (proto == heap->null_value()) return false; 287 if (proto == heap->null_value()) return false;
288 array_proto = JSObject::cast(proto); 288 array_proto = JSObject::cast(proto);
289 if (array_proto != native_context->initial_object_prototype()) return false; 289 if (array_proto != native_context->initial_object_prototype()) return false;
290 if (array_proto->elements() != heap->empty_fixed_array()) return false; 290 if (array_proto->elements() != heap->empty_fixed_array()) return false;
291 return array_proto->GetPrototype()->IsNull(); 291 return array_proto->GetPrototype()->IsNull();
292 } 292 }
293 293
294 294
295 // Returns empty handle if not applicable. 295 // Returns empty handle if not applicable.
296 MUST_USE_RESULT 296 MUST_USE_RESULT
297 static inline Handle<FixedArrayBase> EnsureJSArrayWithWritableFastElements( 297 static inline MaybeHandle<FixedArrayBase> EnsureJSArrayWithWritableFastElements(
298 Isolate* isolate, 298 Isolate* isolate,
299 Handle<Object> receiver, 299 Handle<Object> receiver,
300 Arguments* args, 300 Arguments* args,
301 int first_added_arg) { 301 int first_added_arg) {
302 if (!receiver->IsJSArray()) return Handle<FixedArrayBase>::null(); 302 if (!receiver->IsJSArray()) return MaybeHandle<FixedArrayBase>();
303 Handle<JSArray> array = Handle<JSArray>::cast(receiver); 303 Handle<JSArray> array = Handle<JSArray>::cast(receiver);
304 if (array->map()->is_observed()) return Handle<FixedArrayBase>::null(); 304 if (array->map()->is_observed()) return MaybeHandle<FixedArrayBase>();
305 if (!array->map()->is_extensible()) return Handle<FixedArrayBase>::null(); 305 if (!array->map()->is_extensible()) return MaybeHandle<FixedArrayBase>();
306 Handle<FixedArrayBase> elms(array->elements(), isolate); 306 Handle<FixedArrayBase> elms(array->elements(), isolate);
307 Heap* heap = isolate->heap(); 307 Heap* heap = isolate->heap();
308 Map* map = elms->map(); 308 Map* map = elms->map();
309 if (map == heap->fixed_array_map()) { 309 if (map == heap->fixed_array_map()) {
310 if (args == NULL || array->HasFastObjectElements()) return elms; 310 if (args == NULL || array->HasFastObjectElements()) return elms;
311 } else if (map == heap->fixed_cow_array_map()) { 311 } else if (map == heap->fixed_cow_array_map()) {
312 elms = JSObject::EnsureWritableFastElements(array); 312 elms = JSObject::EnsureWritableFastElements(array);
313 if (args == NULL || array->HasFastObjectElements()) return elms; 313 if (args == NULL || array->HasFastObjectElements()) return elms;
314 } else if (map == heap->fixed_double_array_map()) { 314 } else if (map == heap->fixed_double_array_map()) {
315 if (args == NULL) return elms; 315 if (args == NULL) return elms;
316 } else { 316 } else {
317 return Handle<FixedArrayBase>::null(); 317 return MaybeHandle<FixedArrayBase>();
318 } 318 }
319 319
320 // Need to ensure that the arguments passed in args can be contained in 320 // Need to ensure that the arguments passed in args can be contained in
321 // the array. 321 // the array.
322 int args_length = args->length(); 322 int args_length = args->length();
323 if (first_added_arg >= args_length) return handle(array->elements(), isolate); 323 if (first_added_arg >= args_length) return handle(array->elements(), isolate);
324 324
325 ElementsKind origin_kind = array->map()->elements_kind(); 325 ElementsKind origin_kind = array->map()->elements_kind();
326 ASSERT(!IsFastObjectElementsKind(origin_kind)); 326 ASSERT(!IsFastObjectElementsKind(origin_kind));
327 ElementsKind target_kind = origin_kind; 327 ElementsKind target_kind = origin_kind;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
384 argv.start(), 384 argv.start(),
385 &pending_exception); 385 &pending_exception);
386 if (pending_exception) return Failure::Exception(); 386 if (pending_exception) return Failure::Exception();
387 return *result; 387 return *result;
388 } 388 }
389 389
390 390
391 BUILTIN(ArrayPush) { 391 BUILTIN(ArrayPush) {
392 HandleScope scope(isolate); 392 HandleScope scope(isolate);
393 Handle<Object> receiver = args.receiver(); 393 Handle<Object> receiver = args.receiver();
394 Handle<FixedArrayBase> elms_obj = 394 MaybeHandle<FixedArrayBase> maybe_elms_obj =
395 EnsureJSArrayWithWritableFastElements(isolate, receiver, &args, 1); 395 EnsureJSArrayWithWritableFastElements(isolate, receiver, &args, 1);
396 if (elms_obj.is_null()) return CallJsBuiltin(isolate, "ArrayPush", args); 396 if (maybe_elms_obj.is_null()) {
397 return CallJsBuiltin(isolate, "ArrayPush", args);
398 }
399 Handle<FixedArrayBase> elms_obj = maybe_elms_obj.ToHandleChecked();
Yang 2014/04/03 15:04:04 That would be a duplicate check. Why not use ToHan
Igor Sheludko 2014/04/03 15:17:06 Good point, done.
397 400
398 Handle<JSArray> array = Handle<JSArray>::cast(receiver); 401 Handle<JSArray> array = Handle<JSArray>::cast(receiver);
399 ASSERT(!array->map()->is_observed()); 402 ASSERT(!array->map()->is_observed());
400 403
401 ElementsKind kind = array->GetElementsKind(); 404 ElementsKind kind = array->GetElementsKind();
402 405
403 if (IsFastSmiOrObjectElementsKind(kind)) { 406 if (IsFastSmiOrObjectElementsKind(kind)) {
404 Handle<FixedArray> elms = Handle<FixedArray>::cast(elms_obj); 407 Handle<FixedArray> elms = Handle<FixedArray>::cast(elms_obj);
405 408
406 int len = Smi::cast(array->length())->value(); 409 int len = Smi::cast(array->length())->value();
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
489 // Set the length. 492 // Set the length.
490 array->set_length(Smi::FromInt(new_length)); 493 array->set_length(Smi::FromInt(new_length));
491 return Smi::FromInt(new_length); 494 return Smi::FromInt(new_length);
492 } 495 }
493 } 496 }
494 497
495 498
496 BUILTIN(ArrayPop) { 499 BUILTIN(ArrayPop) {
497 HandleScope scope(isolate); 500 HandleScope scope(isolate);
498 Handle<Object> receiver = args.receiver(); 501 Handle<Object> receiver = args.receiver();
499 Handle<FixedArrayBase> elms_obj = 502 MaybeHandle<FixedArrayBase> maybe_elms_obj =
500 EnsureJSArrayWithWritableFastElements(isolate, receiver, NULL, 0); 503 EnsureJSArrayWithWritableFastElements(isolate, receiver, NULL, 0);
501 if (elms_obj.is_null()) return CallJsBuiltin(isolate, "ArrayPop", args); 504 if (maybe_elms_obj.is_null()) return CallJsBuiltin(isolate, "ArrayPop", args);
505 Handle<FixedArrayBase> elms_obj = maybe_elms_obj.ToHandleChecked();
502 506
503 Handle<JSArray> array = Handle<JSArray>::cast(receiver); 507 Handle<JSArray> array = Handle<JSArray>::cast(receiver);
504 ASSERT(!array->map()->is_observed()); 508 ASSERT(!array->map()->is_observed());
505 509
506 int len = Smi::cast(array->length())->value(); 510 int len = Smi::cast(array->length())->value();
507 if (len == 0) return isolate->heap()->undefined_value(); 511 if (len == 0) return isolate->heap()->undefined_value();
508 512
509 ElementsAccessor* accessor = array->GetElementsAccessor(); 513 ElementsAccessor* accessor = array->GetElementsAccessor();
510 int new_length = len - 1; 514 int new_length = len - 1;
511 Handle<Object> element; 515 Handle<Object> element;
512 if (accessor->HasElement(*array, *array, new_length, *elms_obj)) { 516 if (accessor->HasElement(*array, *array, new_length, *elms_obj)) {
513 element = accessor->Get( 517 element = accessor->Get(
514 array, array, new_length, elms_obj); 518 array, array, new_length, elms_obj);
515 } else { 519 } else {
516 Handle<Object> proto(array->GetPrototype(), isolate); 520 Handle<Object> proto(array->GetPrototype(), isolate);
517 element = Object::GetElement(isolate, proto, len - 1); 521 element = Object::GetElement(isolate, proto, len - 1);
518 } 522 }
519 RETURN_IF_EMPTY_HANDLE(isolate, element); 523 RETURN_IF_EMPTY_HANDLE(isolate, element);
520 RETURN_IF_EMPTY_HANDLE(isolate, 524 RETURN_IF_EMPTY_HANDLE(isolate,
521 accessor->SetLength( 525 accessor->SetLength(
522 array, handle(Smi::FromInt(new_length), isolate))); 526 array, handle(Smi::FromInt(new_length), isolate)));
523 return *element; 527 return *element;
524 } 528 }
525 529
526 530
527 BUILTIN(ArrayShift) { 531 BUILTIN(ArrayShift) {
528 HandleScope scope(isolate); 532 HandleScope scope(isolate);
529 Heap* heap = isolate->heap(); 533 Heap* heap = isolate->heap();
530 Handle<Object> receiver = args.receiver(); 534 Handle<Object> receiver = args.receiver();
531 Handle<FixedArrayBase> elms_obj = 535 MaybeHandle<FixedArrayBase> maybe_elms_obj =
532 EnsureJSArrayWithWritableFastElements(isolate, receiver, NULL, 0); 536 EnsureJSArrayWithWritableFastElements(isolate, receiver, NULL, 0);
533 if (elms_obj.is_null() || 537 if (maybe_elms_obj.is_null() ||
534 !IsJSArrayFastElementMovingAllowed(heap, 538 !IsJSArrayFastElementMovingAllowed(heap,
535 *Handle<JSArray>::cast(receiver))) { 539 *Handle<JSArray>::cast(receiver))) {
536 return CallJsBuiltin(isolate, "ArrayShift", args); 540 return CallJsBuiltin(isolate, "ArrayShift", args);
537 } 541 }
538 Handle<JSArray> array = Handle<JSArray>::cast(receiver); 542 Handle<JSArray> array = Handle<JSArray>::cast(receiver);
539 ASSERT(!array->map()->is_observed()); 543 ASSERT(!array->map()->is_observed());
540 544
541 int len = Smi::cast(array->length())->value(); 545 int len = Smi::cast(array->length())->value();
542 if (len == 0) return heap->undefined_value(); 546 if (len == 0) return heap->undefined_value();
543 547
548 Handle<FixedArrayBase> elms_obj = maybe_elms_obj.ToHandleChecked();
549
544 // Get first element 550 // Get first element
545 ElementsAccessor* accessor = array->GetElementsAccessor(); 551 ElementsAccessor* accessor = array->GetElementsAccessor();
546 Handle<Object> first = accessor->Get(receiver, array, 0, elms_obj); 552 Handle<Object> first = accessor->Get(receiver, array, 0, elms_obj);
547 RETURN_IF_EMPTY_HANDLE(isolate, first); 553 RETURN_IF_EMPTY_HANDLE(isolate, first);
548 if (first->IsTheHole()) { 554 if (first->IsTheHole()) {
549 first = isolate->factory()->undefined_value(); 555 first = isolate->factory()->undefined_value();
550 } 556 }
551 557
552 if (heap->CanMoveObjectStart(*elms_obj)) { 558 if (heap->CanMoveObjectStart(*elms_obj)) {
553 array->set_elements(LeftTrimFixedArray(heap, *elms_obj, 1)); 559 array->set_elements(LeftTrimFixedArray(heap, *elms_obj, 1));
(...skipping 15 matching lines...) Expand all
569 array->set_length(Smi::FromInt(len - 1)); 575 array->set_length(Smi::FromInt(len - 1));
570 576
571 return *first; 577 return *first;
572 } 578 }
573 579
574 580
575 BUILTIN(ArrayUnshift) { 581 BUILTIN(ArrayUnshift) {
576 HandleScope scope(isolate); 582 HandleScope scope(isolate);
577 Heap* heap = isolate->heap(); 583 Heap* heap = isolate->heap();
578 Handle<Object> receiver = args.receiver(); 584 Handle<Object> receiver = args.receiver();
579 Handle<FixedArrayBase> elms_obj = 585 MaybeHandle<FixedArrayBase> maybe_elms_obj =
580 EnsureJSArrayWithWritableFastElements(isolate, receiver, NULL, 0); 586 EnsureJSArrayWithWritableFastElements(isolate, receiver, NULL, 0);
581 if (elms_obj.is_null() || 587 if (maybe_elms_obj.is_null() ||
582 !IsJSArrayFastElementMovingAllowed(heap, 588 !IsJSArrayFastElementMovingAllowed(heap,
583 *Handle<JSArray>::cast(receiver))) { 589 *Handle<JSArray>::cast(receiver))) {
584 return CallJsBuiltin(isolate, "ArrayUnshift", args); 590 return CallJsBuiltin(isolate, "ArrayUnshift", args);
585 } 591 }
586 Handle<JSArray> array = Handle<JSArray>::cast(receiver); 592 Handle<JSArray> array = Handle<JSArray>::cast(receiver);
587 ASSERT(!array->map()->is_observed()); 593 ASSERT(!array->map()->is_observed());
588 if (!array->HasFastSmiOrObjectElements()) { 594 if (!array->HasFastSmiOrObjectElements()) {
589 return CallJsBuiltin(isolate, "ArrayUnshift", args); 595 return CallJsBuiltin(isolate, "ArrayUnshift", args);
590 } 596 }
591 Handle<FixedArray> elms = Handle<FixedArray>::cast(elms_obj); 597 Handle<FixedArray> elms =
598 Handle<FixedArray>::cast(maybe_elms_obj.ToHandleChecked());
592 599
593 int len = Smi::cast(array->length())->value(); 600 int len = Smi::cast(array->length())->value();
594 int to_add = args.length() - 1; 601 int to_add = args.length() - 1;
595 int new_length = len + to_add; 602 int new_length = len + to_add;
596 // Currently fixed arrays cannot grow too big, so 603 // Currently fixed arrays cannot grow too big, so
597 // we should never hit this case. 604 // we should never hit this case.
598 ASSERT(to_add <= (Smi::kMaxValue - len)); 605 ASSERT(to_add <= (Smi::kMaxValue - len));
599 606
600 JSObject::EnsureCanContainElements(array, &args, 1, to_add, 607 JSObject::EnsureCanContainElements(array, &args, 1, to_add,
601 DONT_ALLOW_DOUBLE_ELEMENTS); 608 DONT_ALLOW_DOUBLE_ELEMENTS);
(...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after
771 accessor->CopyElements( 778 accessor->CopyElements(
772 elms, k, kind, handle(result_array->elements(), isolate), 0, result_len); 779 elms, k, kind, handle(result_array->elements(), isolate), 0, result_len);
773 return *result_array; 780 return *result_array;
774 } 781 }
775 782
776 783
777 BUILTIN(ArraySplice) { 784 BUILTIN(ArraySplice) {
778 HandleScope scope(isolate); 785 HandleScope scope(isolate);
779 Heap* heap = isolate->heap(); 786 Heap* heap = isolate->heap();
780 Handle<Object> receiver = args.receiver(); 787 Handle<Object> receiver = args.receiver();
781 Handle<FixedArrayBase> elms_obj = 788 MaybeHandle<FixedArrayBase> maybe_elms_obj =
782 EnsureJSArrayWithWritableFastElements(isolate, receiver, &args, 3); 789 EnsureJSArrayWithWritableFastElements(isolate, receiver, &args, 3);
783 if (elms_obj.is_null() || 790 if (maybe_elms_obj.is_null() ||
784 !IsJSArrayFastElementMovingAllowed(heap, 791 !IsJSArrayFastElementMovingAllowed(heap,
785 *Handle<JSArray>::cast(receiver))) { 792 *Handle<JSArray>::cast(receiver))) {
786 return CallJsBuiltin(isolate, "ArraySplice", args); 793 return CallJsBuiltin(isolate, "ArraySplice", args);
787 } 794 }
788 Handle<JSArray> array = Handle<JSArray>::cast(receiver); 795 Handle<JSArray> array = Handle<JSArray>::cast(receiver);
789 ASSERT(!array->map()->is_observed()); 796 ASSERT(!array->map()->is_observed());
790 797
791 int len = Smi::cast(array->length())->value(); 798 int len = Smi::cast(array->length())->value();
792 799
793 int n_arguments = args.length() - 1; 800 int n_arguments = args.length() - 1;
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
840 ElementsKind elements_kind = array->GetElementsKind(); 847 ElementsKind elements_kind = array->GetElementsKind();
841 848
842 int item_count = (n_arguments > 1) ? (n_arguments - 2) : 0; 849 int item_count = (n_arguments > 1) ? (n_arguments - 2) : 0;
843 int new_length = len - actual_delete_count + item_count; 850 int new_length = len - actual_delete_count + item_count;
844 851
845 // For double mode we do not support changing the length. 852 // For double mode we do not support changing the length.
846 if (new_length > len && IsFastDoubleElementsKind(elements_kind)) { 853 if (new_length > len && IsFastDoubleElementsKind(elements_kind)) {
847 return CallJsBuiltin(isolate, "ArraySplice", args); 854 return CallJsBuiltin(isolate, "ArraySplice", args);
848 } 855 }
849 856
857 Handle<FixedArrayBase> elms_obj = maybe_elms_obj.ToHandleChecked();
858
850 if (new_length == 0) { 859 if (new_length == 0) {
851 Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements( 860 Handle<JSArray> result = isolate->factory()->NewJSArrayWithElements(
852 elms_obj, elements_kind, actual_delete_count); 861 elms_obj, elements_kind, actual_delete_count);
853 array->set_elements(heap->empty_fixed_array()); 862 array->set_elements(heap->empty_fixed_array());
854 array->set_length(Smi::FromInt(0)); 863 array->set_length(Smi::FromInt(0));
855 return *result; 864 return *result;
856 } 865 }
857 866
858 Handle<JSArray> result_array = 867 Handle<JSArray> result_array =
859 isolate->factory()->NewJSArray(elements_kind, 868 isolate->factory()->NewJSArray(elements_kind,
(...skipping 875 matching lines...) Expand 10 before | Expand all | Expand 10 after
1735 } 1744 }
1736 BUILTIN_LIST_C(DEFINE_BUILTIN_ACCESSOR_C) 1745 BUILTIN_LIST_C(DEFINE_BUILTIN_ACCESSOR_C)
1737 BUILTIN_LIST_A(DEFINE_BUILTIN_ACCESSOR_A) 1746 BUILTIN_LIST_A(DEFINE_BUILTIN_ACCESSOR_A)
1738 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H) 1747 BUILTIN_LIST_H(DEFINE_BUILTIN_ACCESSOR_H)
1739 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A) 1748 BUILTIN_LIST_DEBUG_A(DEFINE_BUILTIN_ACCESSOR_A)
1740 #undef DEFINE_BUILTIN_ACCESSOR_C 1749 #undef DEFINE_BUILTIN_ACCESSOR_C
1741 #undef DEFINE_BUILTIN_ACCESSOR_A 1750 #undef DEFINE_BUILTIN_ACCESSOR_A
1742 1751
1743 1752
1744 } } // namespace v8::internal 1753 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698