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 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1743 // from. Global regexps can match any number of times, so we guess | 1744 // from. Global regexps can match any number of times, so we guess |
1744 // conservatively. | 1745 // conservatively. |
1745 int expected_parts = | 1746 int expected_parts = |
1746 (compiled_replacement.parts() + 1) * (is_global ? 4 : 1) + 1; | 1747 (compiled_replacement.parts() + 1) * (is_global ? 4 : 1) + 1; |
1747 ReplacementStringBuilder builder(subject_handle, expected_parts); | 1748 ReplacementStringBuilder builder(subject_handle, expected_parts); |
1748 | 1749 |
1749 // Index of end of last match. | 1750 // Index of end of last match. |
1750 int prev = 0; | 1751 int prev = 0; |
1751 | 1752 |
1752 // Number of parts added by compiled replacement plus preceeding string | 1753 // Number of parts added by compiled replacement plus preceeding string |
1753 // and possibly suffix after last match. | 1754 // and possibly suffix after last match. It is possible for compiled |
1754 const int parts_added_per_loop = compiled_replacement.parts() + 2; | 1755 // replacements to use two elements when encoded as two smis. |
| 1756 const int parts_added_per_loop = compiled_replacement.parts() * 2 + 2; |
1755 bool matched = true; | 1757 bool matched = true; |
1756 do { | 1758 do { |
1757 ASSERT(last_match_info_handle->HasFastElements()); | 1759 ASSERT(last_match_info_handle->HasFastElements()); |
1758 // Increase the capacity of the builder before entering local handle-scope, | 1760 // Increase the capacity of the builder before entering local handle-scope, |
1759 // so its internal buffer can safely allocate a new handle if it grows. | 1761 // so its internal buffer can safely allocate a new handle if it grows. |
1760 builder.EnsureCapacity(parts_added_per_loop); | 1762 builder.EnsureCapacity(parts_added_per_loop); |
1761 | 1763 |
1762 HandleScope loop_scope; | 1764 HandleScope loop_scope; |
1763 int start, end; | 1765 int start, end; |
1764 { | 1766 { |
(...skipping 1994 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3759 | 3761 |
3760 template<typename sinkchar> | 3762 template<typename sinkchar> |
3761 static inline void StringBuilderConcatHelper(String* special, | 3763 static inline void StringBuilderConcatHelper(String* special, |
3762 sinkchar* sink, | 3764 sinkchar* sink, |
3763 FixedArray* fixed_array, | 3765 FixedArray* fixed_array, |
3764 int array_length) { | 3766 int array_length) { |
3765 int position = 0; | 3767 int position = 0; |
3766 for (int i = 0; i < array_length; i++) { | 3768 for (int i = 0; i < array_length; i++) { |
3767 Object* element = fixed_array->get(i); | 3769 Object* element = fixed_array->get(i); |
3768 if (element->IsSmi()) { | 3770 if (element->IsSmi()) { |
| 3771 // Smi encoding of position and length. |
3769 int encoded_slice = Smi::cast(element)->value(); | 3772 int encoded_slice = Smi::cast(element)->value(); |
3770 int pos = StringBuilderSubstringPosition::decode(encoded_slice); | 3773 int pos; |
3771 int len = StringBuilderSubstringLength::decode(encoded_slice); | 3774 int len; |
| 3775 if (encoded_slice > 0) { |
| 3776 // Position and length encoded in one smi. |
| 3777 pos = StringBuilderSubstringPosition::decode(encoded_slice); |
| 3778 len = StringBuilderSubstringLength::decode(encoded_slice); |
| 3779 } else { |
| 3780 // Position and length encoded in two smis. |
| 3781 Object* obj = fixed_array->get(++i); |
| 3782 ASSERT(obj->IsSmi()); |
| 3783 pos = Smi::cast(obj)->value(); |
| 3784 len = -encoded_slice; |
| 3785 } |
3772 String::WriteToFlat(special, | 3786 String::WriteToFlat(special, |
3773 sink + position, | 3787 sink + position, |
3774 pos, | 3788 pos, |
3775 pos + len); | 3789 pos + len); |
3776 position += len; | 3790 position += len; |
3777 } else { | 3791 } else { |
3778 String* string = String::cast(element); | 3792 String* string = String::cast(element); |
3779 int element_length = string->length(); | 3793 int element_length = string->length(); |
3780 String::WriteToFlat(string, sink + position, 0, element_length); | 3794 String::WriteToFlat(string, sink + position, 0, element_length); |
3781 position += element_length; | 3795 position += element_length; |
3782 } | 3796 } |
3783 } | 3797 } |
3784 } | 3798 } |
3785 | 3799 |
3786 | 3800 |
3787 static Object* Runtime_StringBuilderConcat(Arguments args) { | 3801 static Object* Runtime_StringBuilderConcat(Arguments args) { |
3788 NoHandleAllocation ha; | 3802 NoHandleAllocation ha; |
3789 ASSERT(args.length() == 2); | 3803 ASSERT(args.length() == 2); |
3790 CONVERT_CHECKED(JSArray, array, args[0]); | 3804 CONVERT_CHECKED(JSArray, array, args[0]); |
3791 CONVERT_CHECKED(String, special, args[1]); | 3805 CONVERT_CHECKED(String, special, args[1]); |
| 3806 |
| 3807 // This assumption is used by the slice encoding in one or two smis. |
| 3808 ASSERT(Smi::kMaxValue >= String::kMaxLength); |
| 3809 |
3792 int special_length = special->length(); | 3810 int special_length = special->length(); |
3793 Object* smi_array_length = array->length(); | 3811 Object* smi_array_length = array->length(); |
3794 if (!smi_array_length->IsSmi()) { | 3812 if (!smi_array_length->IsSmi()) { |
3795 Top::context()->mark_out_of_memory(); | 3813 Top::context()->mark_out_of_memory(); |
3796 return Failure::OutOfMemoryException(); | 3814 return Failure::OutOfMemoryException(); |
3797 } | 3815 } |
3798 int array_length = Smi::cast(smi_array_length)->value(); | 3816 int array_length = Smi::cast(smi_array_length)->value(); |
3799 if (!array->HasFastElements()) { | 3817 if (!array->HasFastElements()) { |
3800 return Top::Throw(Heap::illegal_argument_symbol()); | 3818 return Top::Throw(Heap::illegal_argument_symbol()); |
3801 } | 3819 } |
3802 FixedArray* fixed_array = FixedArray::cast(array->elements()); | 3820 FixedArray* fixed_array = FixedArray::cast(array->elements()); |
3803 if (fixed_array->length() < array_length) { | 3821 if (fixed_array->length() < array_length) { |
3804 array_length = fixed_array->length(); | 3822 array_length = fixed_array->length(); |
3805 } | 3823 } |
3806 | 3824 |
3807 if (array_length == 0) { | 3825 if (array_length == 0) { |
3808 return Heap::empty_string(); | 3826 return Heap::empty_string(); |
3809 } else if (array_length == 1) { | 3827 } else if (array_length == 1) { |
3810 Object* first = fixed_array->get(0); | 3828 Object* first = fixed_array->get(0); |
3811 if (first->IsString()) return first; | 3829 if (first->IsString()) return first; |
3812 } | 3830 } |
3813 | 3831 |
3814 bool ascii = special->IsAsciiRepresentation(); | 3832 bool ascii = special->IsAsciiRepresentation(); |
3815 int position = 0; | 3833 int position = 0; |
3816 for (int i = 0; i < array_length; i++) { | 3834 for (int i = 0; i < array_length; i++) { |
3817 Object* elt = fixed_array->get(i); | 3835 Object* elt = fixed_array->get(i); |
3818 if (elt->IsSmi()) { | 3836 if (elt->IsSmi()) { |
| 3837 // Smi encoding of position and length. |
3819 int len = Smi::cast(elt)->value(); | 3838 int len = Smi::cast(elt)->value(); |
3820 int pos = len >> 11; | 3839 if (len > 0) { |
3821 len &= 0x7ff; | 3840 // Position and length encoded in one smi. |
3822 if (pos + len > special_length) { | 3841 int pos = len >> 11; |
3823 return Top::Throw(Heap::illegal_argument_symbol()); | 3842 len &= 0x7ff; |
| 3843 if (pos + len > special_length) { |
| 3844 return Top::Throw(Heap::illegal_argument_symbol()); |
| 3845 } |
| 3846 position += len; |
| 3847 } else { |
| 3848 // Position and length encoded in two smis. |
| 3849 position += (-len); |
| 3850 // Get the position and check that it is also a smi. |
| 3851 i++; |
| 3852 if (i >= array_length) { |
| 3853 return Top::Throw(Heap::illegal_argument_symbol()); |
| 3854 } |
| 3855 Object* pos = fixed_array->get(i); |
| 3856 if (!pos->IsSmi()) { |
| 3857 return Top::Throw(Heap::illegal_argument_symbol()); |
| 3858 } |
3824 } | 3859 } |
3825 position += len; | |
3826 } else if (elt->IsString()) { | 3860 } else if (elt->IsString()) { |
3827 String* element = String::cast(elt); | 3861 String* element = String::cast(elt); |
3828 int element_length = element->length(); | 3862 int element_length = element->length(); |
3829 position += element_length; | 3863 position += element_length; |
3830 if (ascii && !element->IsAsciiRepresentation()) { | 3864 if (ascii && !element->IsAsciiRepresentation()) { |
3831 ascii = false; | 3865 ascii = false; |
3832 } | 3866 } |
3833 } else { | 3867 } else { |
3834 return Top::Throw(Heap::illegal_argument_symbol()); | 3868 return Top::Throw(Heap::illegal_argument_symbol()); |
3835 } | 3869 } |
(...skipping 4003 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7839 } else { | 7873 } else { |
7840 // Handle last resort GC and make sure to allow future allocations | 7874 // Handle last resort GC and make sure to allow future allocations |
7841 // to grow the heap without causing GCs (if possible). | 7875 // to grow the heap without causing GCs (if possible). |
7842 Counters::gc_last_resort_from_js.Increment(); | 7876 Counters::gc_last_resort_from_js.Increment(); |
7843 Heap::CollectAllGarbage(false); | 7877 Heap::CollectAllGarbage(false); |
7844 } | 7878 } |
7845 } | 7879 } |
7846 | 7880 |
7847 | 7881 |
7848 } } // namespace v8::internal | 7882 } } // namespace v8::internal |
OLD | NEW |