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

Side by Side Diff: src/builtins.cc

Issue 669101: Fix invalid fast return in splice when returned array is empty. (Closed)
Patch Set: Created 10 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
« no previous file with comments | « no previous file | test/mjsunit/array-splice.js » ('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 2006-2008 the V8 project authors. All rights reserved. 1 // Copyright 2006-2008 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 574 matching lines...) Expand 10 before | Expand all | Expand 10 after
585 585
586 // SpiderMonkey and JSC return undefined in the case where no 586 // SpiderMonkey and JSC return undefined in the case where no
587 // arguments are given instead of using the implicit undefined 587 // arguments are given instead of using the implicit undefined
588 // arguments. This does not follow ECMA-262, but we do the same for 588 // arguments. This does not follow ECMA-262, but we do the same for
589 // compatibility. 589 // compatibility.
590 // TraceMonkey follows ECMA-262 though. 590 // TraceMonkey follows ECMA-262 though.
591 if (n_arguments == 0) { 591 if (n_arguments == 0) {
592 return Heap::undefined_value(); 592 return Heap::undefined_value();
593 } 593 }
594 594
595 int relativeStart = 0; 595 int relativeStart = 0;
Kasper Lund 2010/03/05 07:08:14 Don't use camelCase for local variables.
596 Object* arg1 = args[1]; 596 Object* arg1 = args[1];
597 if (arg1->IsSmi()) { 597 if (arg1->IsSmi()) {
598 relativeStart = Smi::cast(arg1)->value(); 598 relativeStart = Smi::cast(arg1)->value();
599 } else if (!arg1->IsUndefined()) { 599 } else if (!arg1->IsUndefined()) {
600 return CallJsBuiltin("ArraySplice", args); 600 return CallJsBuiltin("ArraySplice", args);
601 } 601 }
602 int actualStart = (relativeStart < 0) ? Max(len + relativeStart, 0) 602 int actualStart = (relativeStart < 0) ? Max(len + relativeStart, 0)
Kasper Lund 2010/03/05 07:08:14 Don't use camelCase for local variables.
603 : Min(relativeStart, len); 603 : Min(relativeStart, len);
604 604
605 // SpiderMonkey, TraceMonkey and JSC treat the case where no delete count is 605 // SpiderMonkey, TraceMonkey and JSC treat the case where no delete count is
606 // given differently from when an undefined delete count is given. 606 // given differently from when an undefined delete count is given.
607 // This does not follow ECMA-262, but we do the same for 607 // This does not follow ECMA-262, but we do the same for
608 // compatibility. 608 // compatibility.
609 int deleteCount = len; 609 int deleteCount = len;
Kasper Lund 2010/03/05 07:08:14 Don't use camelCase for local variables.
610 if (n_arguments > 1) { 610 if (n_arguments > 1) {
611 Object* arg2 = args[2]; 611 Object* arg2 = args[2];
612 if (arg2->IsSmi()) { 612 if (arg2->IsSmi()) {
613 deleteCount = Smi::cast(arg2)->value(); 613 deleteCount = Smi::cast(arg2)->value();
614 } else { 614 } else {
615 return CallJsBuiltin("ArraySplice", args); 615 return CallJsBuiltin("ArraySplice", args);
616 } 616 }
617 } 617 }
618 int actualDeleteCount = Min(Max(deleteCount, 0), len - actualStart); 618 int actualDeleteCount = Min(Max(deleteCount, 0), len - actualStart);
Kasper Lund 2010/03/05 07:08:14 Don't use camelCase for local variables.
619 if (actualDeleteCount == 0) {
620 return AllocateEmptyJSArray();
621 }
622
623 // Allocate result array.
624 Object* result = AllocateJSArray();
625 if (result->IsFailure()) return result;
626 JSArray* result_array = JSArray::cast(result);
627
628 result = Heap::AllocateUninitializedFixedArray(actualDeleteCount);
629 if (result->IsFailure()) return result;
630 FixedArray* result_elms = FixedArray::cast(result);
631 619
632 FixedArray* elms = FixedArray::cast(array->elements()); 620 FixedArray* elms = FixedArray::cast(array->elements());
633 621
634 AssertNoAllocation no_gc; 622 JSArray* result_array = NULL;
635 // Fill newly created array. 623 if (actualDeleteCount == 0) {
636 CopyElements(&no_gc, result_elms, 0, elms, actualStart, actualDeleteCount); 624 Object* result = AllocateEmptyJSArray();
625 if (result->IsFailure()) return result;
626 result_array = JSArray::cast(result);
627 } else {
628 // Allocate result array.
629 Object* result = AllocateJSArray();
630 if (result->IsFailure()) return result;
631 result_array = JSArray::cast(result);
637 632
638 // Set elements. 633 result = Heap::AllocateUninitializedFixedArray(actualDeleteCount);
639 result_array->set_elements(result_elms); 634 if (result->IsFailure()) return result;
635 FixedArray* result_elms = FixedArray::cast(result);
640 636
641 // Set the length. 637 AssertNoAllocation no_gc;
642 result_array->set_length(Smi::FromInt(actualDeleteCount)); 638 // Fill newly created array.
639 CopyElements(&no_gc, result_elms, 0, elms, actualStart, actualDeleteCount);
640
641 // Set elements.
642 result_array->set_elements(result_elms);
643
644 // Set the length.
645 result_array->set_length(Smi::FromInt(actualDeleteCount));
646 }
643 647
644 int itemCount = (n_arguments > 1) ? (n_arguments - 2) : 0; 648 int itemCount = (n_arguments > 1) ? (n_arguments - 2) : 0;
Kasper Lund 2010/03/05 07:08:14 Don't use camelCase for local variables.
645 649
646 int new_length = len - actualDeleteCount + itemCount; 650 int new_length = len - actualDeleteCount + itemCount;
647 651
648 if (itemCount < actualDeleteCount) { 652 if (itemCount < actualDeleteCount) {
649 // Shrink the array. 653 // Shrink the array.
654 AssertNoAllocation no_gc;
650 MoveElements(&no_gc, 655 MoveElements(&no_gc,
651 elms, actualStart + itemCount, 656 elms, actualStart + itemCount,
652 elms, actualStart + actualDeleteCount, 657 elms, actualStart + actualDeleteCount,
653 (len - actualDeleteCount - actualStart)); 658 (len - actualDeleteCount - actualStart));
654 FillWithHoles(elms, new_length, len); 659 FillWithHoles(elms, new_length, len);
655 } else if (itemCount > actualDeleteCount) { 660 } else if (itemCount > actualDeleteCount) {
656 // Currently fixed arrays cannot grow too big, so 661 // Currently fixed arrays cannot grow too big, so
657 // we should never hit this case. 662 // we should never hit this case.
658 ASSERT((itemCount - actualDeleteCount) <= (Smi::kMaxValue - len)); 663 ASSERT((itemCount - actualDeleteCount) <= (Smi::kMaxValue - len));
659 664
660 FixedArray* source_elms = elms; 665 FixedArray* source_elms = elms;
661 666
662 // Check if array need to grow. 667 // Check if array need to grow.
663 if (new_length > elms->length()) { 668 if (new_length > elms->length()) {
664 // New backing storage is needed. 669 // New backing storage is needed.
665 int capacity = new_length + (new_length >> 1) + 16; 670 int capacity = new_length + (new_length >> 1) + 16;
666 Object* obj = Heap::AllocateUninitializedFixedArray(capacity); 671 Object* obj = Heap::AllocateUninitializedFixedArray(capacity);
667 if (obj->IsFailure()) return obj; 672 if (obj->IsFailure()) return obj;
668 FixedArray* new_elms = FixedArray::cast(obj); 673 FixedArray* new_elms = FixedArray::cast(obj);
669 674
675 AssertNoAllocation no_gc;
670 // Copy the part before actualStart as is. 676 // Copy the part before actualStart as is.
671 CopyElements(&no_gc, new_elms, 0, elms, 0, actualStart); 677 CopyElements(&no_gc, new_elms, 0, elms, 0, actualStart);
672 FillWithHoles(new_elms, new_length, capacity); 678 FillWithHoles(new_elms, new_length, capacity);
673 679
674 source_elms = elms; 680 source_elms = elms;
675 elms = new_elms; 681 elms = new_elms;
676 array->set_elements(elms); 682 array->set_elements(elms);
677 } 683 }
678 684
685 AssertNoAllocation no_gc;
679 MoveElements(&no_gc, 686 MoveElements(&no_gc,
680 elms, actualStart + itemCount, 687 elms, actualStart + itemCount,
681 source_elms, actualStart + actualDeleteCount, 688 source_elms, actualStart + actualDeleteCount,
682 (len - actualDeleteCount - actualStart)); 689 (len - actualDeleteCount - actualStart));
683 } 690 }
684 691
692 AssertNoAllocation no_gc;
685 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc); 693 WriteBarrierMode mode = elms->GetWriteBarrierMode(no_gc);
686 for (int k = actualStart; k < actualStart + itemCount; k++) { 694 for (int k = actualStart; k < actualStart + itemCount; k++) {
687 elms->set(k, args[3 + k - actualStart], mode); 695 elms->set(k, args[3 + k - actualStart], mode);
688 } 696 }
689 697
690 // Set the length. 698 // Set the length.
691 array->set_length(Smi::FromInt(new_length)); 699 array->set_length(Smi::FromInt(new_length));
692 700
693 return result_array; 701 return result_array;
694 } 702 }
(...skipping 643 matching lines...) Expand 10 before | Expand all | Expand 10 after
1338 if (entry->contains(pc)) { 1346 if (entry->contains(pc)) {
1339 return names_[i]; 1347 return names_[i];
1340 } 1348 }
1341 } 1349 }
1342 } 1350 }
1343 return NULL; 1351 return NULL;
1344 } 1352 }
1345 1353
1346 1354
1347 } } // namespace v8::internal 1355 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | test/mjsunit/array-splice.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698