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