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

Side by Side Diff: src/builtins.cc

Issue 1058003: Revert "More generic version of Array.concat builtin." (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 | src/v8-counters.h » ('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 710 matching lines...) Expand 10 before | Expand all | Expand 10 after
721 } 721 }
722 722
723 // Set the length. 723 // Set the length.
724 array->set_length(Smi::FromInt(new_length)); 724 array->set_length(Smi::FromInt(new_length));
725 725
726 return result_array; 726 return result_array;
727 } 727 }
728 728
729 729
730 BUILTIN(ArrayConcat) { 730 BUILTIN(ArrayConcat) {
731 if (!ArrayPrototypeHasNoElements()) { 731 Counters::array_concat_builtin_total.Increment();
732 if (args.length() != 2) {
733 // Fast case only for concating two arrays.
734 return CallJsBuiltin("ArrayConcat", args);
735 }
736 Counters::array_concat_builtin_two_args.Increment();
737
738 Object* receiver_obj = *args.receiver();
739 FixedArray* receiver_elms = NULL;
740 Object* arg_obj = args[1];
741 FixedArray* arg_elms = NULL;
742 if (!IsJSArrayWithFastElements(receiver_obj, &receiver_elms)
743 || !IsJSArrayWithFastElements(arg_obj, &arg_elms)
744 || !ArrayPrototypeHasNoElements()) {
732 return CallJsBuiltin("ArrayConcat", args); 745 return CallJsBuiltin("ArrayConcat", args);
733 } 746 }
734 747
735 // Iterate through all the arguments performing checks 748 JSArray* receiver_array = JSArray::cast(receiver_obj);
736 // and calculating total length. 749 ASSERT(receiver_array->HasFastElements());
737 int n_arguments = args.length(); 750 JSArray* arg_array = JSArray::cast(arg_obj);
738 int result_len = 0; 751 ASSERT(arg_array->HasFastElements());
739 for (int i = 0; i < n_arguments; i++) {
740 Object* arg = args[i];
741 if (!arg->IsJSArray() || JSArray::cast(arg)->HasFastElements()) {
742 return CallJsBuiltin("ArrayConcat", args);
743 }
744 752
745 int len = Smi::cast(JSArray::cast(arg)->length())->value(); 753 int receiver_len = Smi::cast(receiver_array->length())->value();
754 int arg_len = Smi::cast(arg_array->length())->value();
755 ASSERT(receiver_len <= (Smi::kMaxValue - arg_len));
746 756
747 // We shouldn't overflow when adding another len. 757 int result_len = receiver_len + arg_len;
748 const int kHalfOfMaxInt = 1 << (kBitsPerInt - 2); 758 if (result_len > FixedArray::kMaxSize) {
749 STATIC_ASSERT(FixedArray::kMaxLength < kHalfOfMaxInt); 759 return CallJsBuiltin("ArrayConcat", args);
750 USE(kHalfOfMaxInt);
751 result_len += len;
752 ASSERT(result_len >= 0);
753
754 if (result_len > FixedArray::kMaxLength) {
755 return CallJsBuiltin("ArrayConcat", args);
756 }
757 } 760 }
758
759 if (result_len == 0) { 761 if (result_len == 0) {
760 return AllocateEmptyJSArray(); 762 return AllocateEmptyJSArray();
761 } 763 }
762 764
763 // Allocate result. 765 // Allocate result.
764 Object* result = AllocateJSArray(); 766 Object* result = AllocateJSArray();
765 if (result->IsFailure()) return result; 767 if (result->IsFailure()) return result;
766 JSArray* result_array = JSArray::cast(result); 768 JSArray* result_array = JSArray::cast(result);
767 769
768 result = Heap::AllocateUninitializedFixedArray(result_len); 770 result = Heap::AllocateUninitializedFixedArray(result_len);
769 if (result->IsFailure()) return result; 771 if (result->IsFailure()) return result;
770 FixedArray* result_elms = FixedArray::cast(result); 772 FixedArray* result_elms = FixedArray::cast(result);
771 773
772 // Copy data. 774 // Copy data.
773 AssertNoAllocation no_gc; 775 AssertNoAllocation no_gc;
774 int start_pos = 0; 776 CopyElements(&no_gc, result_elms, 0, receiver_elms, 0, receiver_len);
775 for (int i = 0; i < n_arguments; i++) { 777 CopyElements(&no_gc, result_elms, receiver_len, arg_elms, 0, arg_len);
776 JSArray* array = JSArray::cast(args[i]);
777 FixedArray* elms = FixedArray::cast(array->elements());
778 int len = Smi::cast(array->length())->value();
779 CopyElements(&no_gc, result_elms, start_pos, elms, 0, len);
780 start_pos += len;
781 }
782 ASSERT(start_pos == result_len);
783 778
784 // Set the length and elements. 779 // Set the length and elements.
785 result_array->set_length(Smi::FromInt(result_len)); 780 result_array->set_length(Smi::FromInt(result_len));
786 result_array->set_elements(result_elms); 781 result_array->set_elements(result_elms);
787 782
788 return result_array; 783 return result_array;
789 } 784 }
790 785
791 786
792 // ----------------------------------------------------------------------------- 787 // -----------------------------------------------------------------------------
(...skipping 639 matching lines...) Expand 10 before | Expand all | Expand 10 after
1432 if (entry->contains(pc)) { 1427 if (entry->contains(pc)) {
1433 return names_[i]; 1428 return names_[i];
1434 } 1429 }
1435 } 1430 }
1436 } 1431 }
1437 return NULL; 1432 return NULL;
1438 } 1433 }
1439 1434
1440 1435
1441 } } // namespace v8::internal 1436 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « no previous file | src/v8-counters.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698