OLD | NEW |
1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2009 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 1339 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1350 int length = to - from; | 1350 int length = to - from; |
1351 ASSERT(length > 0); | 1351 ASSERT(length > 0); |
1352 // Can we encode the slice in 11 bits for length and 19 bits for | 1352 // Can we encode the slice in 11 bits for length and 19 bits for |
1353 // start position - as used by StringBuilderConcatHelper? | 1353 // start position - as used by StringBuilderConcatHelper? |
1354 if (StringBuilderSubstringLength::is_valid(length) && | 1354 if (StringBuilderSubstringLength::is_valid(length) && |
1355 StringBuilderSubstringPosition::is_valid(from)) { | 1355 StringBuilderSubstringPosition::is_valid(from)) { |
1356 int encoded_slice = StringBuilderSubstringLength::encode(length) | | 1356 int encoded_slice = StringBuilderSubstringLength::encode(length) | |
1357 StringBuilderSubstringPosition::encode(from); | 1357 StringBuilderSubstringPosition::encode(from); |
1358 AddElement(Smi::FromInt(encoded_slice)); | 1358 AddElement(Smi::FromInt(encoded_slice)); |
1359 } else { | 1359 } else { |
1360 // Otherwise encode as two smis. | 1360 Handle<String> slice = Factory::NewStringSlice(subject_, from, to); |
1361 AddElement(Smi::FromInt(-length)); | 1361 AddElement(*slice); |
1362 AddElement(Smi::FromInt(from)); | |
1363 } | 1362 } |
1364 IncrementCharacterCount(length); | 1363 IncrementCharacterCount(length); |
1365 } | 1364 } |
1366 | 1365 |
1367 | 1366 |
1368 void AddString(Handle<String> string) { | 1367 void AddString(Handle<String> string) { |
1369 int length = string->length(); | 1368 int length = string->length(); |
1370 ASSERT(length > 0); | 1369 ASSERT(length > 0); |
1371 AddElement(*string); | 1370 AddElement(*string); |
1372 if (!string->IsAsciiRepresentation()) { | 1371 if (!string->IsAsciiRepresentation()) { |
(...skipping 2387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3760 | 3759 |
3761 template<typename sinkchar> | 3760 template<typename sinkchar> |
3762 static inline void StringBuilderConcatHelper(String* special, | 3761 static inline void StringBuilderConcatHelper(String* special, |
3763 sinkchar* sink, | 3762 sinkchar* sink, |
3764 FixedArray* fixed_array, | 3763 FixedArray* fixed_array, |
3765 int array_length) { | 3764 int array_length) { |
3766 int position = 0; | 3765 int position = 0; |
3767 for (int i = 0; i < array_length; i++) { | 3766 for (int i = 0; i < array_length; i++) { |
3768 Object* element = fixed_array->get(i); | 3767 Object* element = fixed_array->get(i); |
3769 if (element->IsSmi()) { | 3768 if (element->IsSmi()) { |
3770 // Smi encoding of position and length. | |
3771 int encoded_slice = Smi::cast(element)->value(); | 3769 int encoded_slice = Smi::cast(element)->value(); |
3772 int pos; | 3770 int pos = StringBuilderSubstringPosition::decode(encoded_slice); |
3773 int len; | 3771 int len = StringBuilderSubstringLength::decode(encoded_slice); |
3774 if (encoded_slice > 0) { | |
3775 // Position and length encoded in one smi. | |
3776 pos = StringBuilderSubstringPosition::decode(encoded_slice); | |
3777 len = StringBuilderSubstringLength::decode(encoded_slice); | |
3778 } else { | |
3779 // Position and length encoded in two smis. | |
3780 Object* obj = fixed_array->get(++i); | |
3781 ASSERT(obj->IsSmi()); | |
3782 pos = Smi::cast(obj)->value(); | |
3783 len = -encoded_slice; | |
3784 } | |
3785 String::WriteToFlat(special, | 3772 String::WriteToFlat(special, |
3786 sink + position, | 3773 sink + position, |
3787 pos, | 3774 pos, |
3788 pos + len); | 3775 pos + len); |
3789 position += len; | 3776 position += len; |
3790 } else { | 3777 } else { |
3791 String* string = String::cast(element); | 3778 String* string = String::cast(element); |
3792 int element_length = string->length(); | 3779 int element_length = string->length(); |
3793 String::WriteToFlat(string, sink + position, 0, element_length); | 3780 String::WriteToFlat(string, sink + position, 0, element_length); |
3794 position += element_length; | 3781 position += element_length; |
3795 } | 3782 } |
3796 } | 3783 } |
3797 } | 3784 } |
3798 | 3785 |
3799 | 3786 |
3800 static Object* Runtime_StringBuilderConcat(Arguments args) { | 3787 static Object* Runtime_StringBuilderConcat(Arguments args) { |
3801 NoHandleAllocation ha; | 3788 NoHandleAllocation ha; |
3802 ASSERT(args.length() == 2); | 3789 ASSERT(args.length() == 2); |
3803 CONVERT_CHECKED(JSArray, array, args[0]); | 3790 CONVERT_CHECKED(JSArray, array, args[0]); |
3804 CONVERT_CHECKED(String, special, args[1]); | 3791 CONVERT_CHECKED(String, special, args[1]); |
3805 | |
3806 // This assumption is used by the slice encoding in one or two smis. | |
3807 ASSERT(Smi::kMaxValue >= String::kMaxLength); | |
3808 | |
3809 int special_length = special->length(); | 3792 int special_length = special->length(); |
3810 Object* smi_array_length = array->length(); | 3793 Object* smi_array_length = array->length(); |
3811 if (!smi_array_length->IsSmi()) { | 3794 if (!smi_array_length->IsSmi()) { |
3812 Top::context()->mark_out_of_memory(); | 3795 Top::context()->mark_out_of_memory(); |
3813 return Failure::OutOfMemoryException(); | 3796 return Failure::OutOfMemoryException(); |
3814 } | 3797 } |
3815 int array_length = Smi::cast(smi_array_length)->value(); | 3798 int array_length = Smi::cast(smi_array_length)->value(); |
3816 if (!array->HasFastElements()) { | 3799 if (!array->HasFastElements()) { |
3817 return Top::Throw(Heap::illegal_argument_symbol()); | 3800 return Top::Throw(Heap::illegal_argument_symbol()); |
3818 } | 3801 } |
3819 FixedArray* fixed_array = FixedArray::cast(array->elements()); | 3802 FixedArray* fixed_array = FixedArray::cast(array->elements()); |
3820 if (fixed_array->length() < array_length) { | 3803 if (fixed_array->length() < array_length) { |
3821 array_length = fixed_array->length(); | 3804 array_length = fixed_array->length(); |
3822 } | 3805 } |
3823 | 3806 |
3824 if (array_length == 0) { | 3807 if (array_length == 0) { |
3825 return Heap::empty_string(); | 3808 return Heap::empty_string(); |
3826 } else if (array_length == 1) { | 3809 } else if (array_length == 1) { |
3827 Object* first = fixed_array->get(0); | 3810 Object* first = fixed_array->get(0); |
3828 if (first->IsString()) return first; | 3811 if (first->IsString()) return first; |
3829 } | 3812 } |
3830 | 3813 |
3831 bool ascii = special->IsAsciiRepresentation(); | 3814 bool ascii = special->IsAsciiRepresentation(); |
3832 int position = 0; | 3815 int position = 0; |
3833 for (int i = 0; i < array_length; i++) { | 3816 for (int i = 0; i < array_length; i++) { |
3834 Object* elt = fixed_array->get(i); | 3817 Object* elt = fixed_array->get(i); |
3835 if (elt->IsSmi()) { | 3818 if (elt->IsSmi()) { |
3836 // Smi encoding of position and length. | |
3837 int len = Smi::cast(elt)->value(); | 3819 int len = Smi::cast(elt)->value(); |
3838 if (len > 0) { | 3820 int pos = len >> 11; |
3839 // Position and length encoded in one smi. | 3821 len &= 0x7ff; |
3840 int pos = len >> 11; | 3822 if (pos + len > special_length) { |
3841 len &= 0x7ff; | 3823 return Top::Throw(Heap::illegal_argument_symbol()); |
3842 if (pos + len > special_length) { | |
3843 return Top::Throw(Heap::illegal_argument_symbol()); | |
3844 } | |
3845 position += len; | |
3846 } else { | |
3847 // Position and length encoded in two smis. | |
3848 position += (-len); | |
3849 // Get the position and check that it is also a smi. | |
3850 i++; | |
3851 if (i >= array_length) { | |
3852 return Top::Throw(Heap::illegal_argument_symbol()); | |
3853 } | |
3854 Object* pos = fixed_array->get(i); | |
3855 if (!pos->IsSmi()) { | |
3856 return Top::Throw(Heap::illegal_argument_symbol()); | |
3857 } | |
3858 } | 3824 } |
| 3825 position += len; |
3859 } else if (elt->IsString()) { | 3826 } else if (elt->IsString()) { |
3860 String* element = String::cast(elt); | 3827 String* element = String::cast(elt); |
3861 int element_length = element->length(); | 3828 int element_length = element->length(); |
3862 position += element_length; | 3829 position += element_length; |
3863 if (ascii && !element->IsAsciiRepresentation()) { | 3830 if (ascii && !element->IsAsciiRepresentation()) { |
3864 ascii = false; | 3831 ascii = false; |
3865 } | 3832 } |
3866 } else { | 3833 } else { |
3867 return Top::Throw(Heap::illegal_argument_symbol()); | 3834 return Top::Throw(Heap::illegal_argument_symbol()); |
3868 } | 3835 } |
(...skipping 4016 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7885 } else { | 7852 } else { |
7886 // Handle last resort GC and make sure to allow future allocations | 7853 // Handle last resort GC and make sure to allow future allocations |
7887 // to grow the heap without causing GCs (if possible). | 7854 // to grow the heap without causing GCs (if possible). |
7888 Counters::gc_last_resort_from_js.Increment(); | 7855 Counters::gc_last_resort_from_js.Increment(); |
7889 Heap::CollectAllGarbage(false); | 7856 Heap::CollectAllGarbage(false); |
7890 } | 7857 } |
7891 } | 7858 } |
7892 | 7859 |
7893 | 7860 |
7894 } } // namespace v8::internal | 7861 } } // namespace v8::internal |
OLD | NEW |