| 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 1264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1275 typedef BitField<int, 0, 11> StringBuilderSubstringLength; | 1275 typedef BitField<int, 0, 11> StringBuilderSubstringLength; |
| 1276 typedef BitField<int, 11, 19> StringBuilderSubstringPosition; | 1276 typedef BitField<int, 11, 19> StringBuilderSubstringPosition; |
| 1277 | 1277 |
| 1278 class ReplacementStringBuilder { | 1278 class ReplacementStringBuilder { |
| 1279 public: | 1279 public: |
| 1280 ReplacementStringBuilder(Handle<String> subject, int estimated_part_count) | 1280 ReplacementStringBuilder(Handle<String> subject, int estimated_part_count) |
| 1281 : subject_(subject), | 1281 : subject_(subject), |
| 1282 parts_(Factory::NewFixedArray(estimated_part_count)), | 1282 parts_(Factory::NewFixedArray(estimated_part_count)), |
| 1283 part_count_(0), | 1283 part_count_(0), |
| 1284 character_count_(0), | 1284 character_count_(0), |
| 1285 is_ascii_(StringShape(*subject).IsAsciiRepresentation()) { | 1285 is_ascii_(subject->IsAsciiRepresentation()) { |
| 1286 // Require a non-zero initial size. Ensures that doubling the size to | 1286 // Require a non-zero initial size. Ensures that doubling the size to |
| 1287 // extend the array will work. | 1287 // extend the array will work. |
| 1288 ASSERT(estimated_part_count > 0); | 1288 ASSERT(estimated_part_count > 0); |
| 1289 } | 1289 } |
| 1290 | 1290 |
| 1291 void EnsureCapacity(int elements) { | 1291 void EnsureCapacity(int elements) { |
| 1292 int length = parts_->length(); | 1292 int length = parts_->length(); |
| 1293 int required_length = part_count_ + elements; | 1293 int required_length = part_count_ + elements; |
| 1294 if (length < required_length) { | 1294 if (length < required_length) { |
| 1295 int new_length = length; | 1295 int new_length = length; |
| (...skipping 23 matching lines...) Expand all Loading... |
| 1319 AddElement(*slice); | 1319 AddElement(*slice); |
| 1320 } | 1320 } |
| 1321 IncrementCharacterCount(length); | 1321 IncrementCharacterCount(length); |
| 1322 } | 1322 } |
| 1323 | 1323 |
| 1324 | 1324 |
| 1325 void AddString(Handle<String> string) { | 1325 void AddString(Handle<String> string) { |
| 1326 int length = string->length(); | 1326 int length = string->length(); |
| 1327 ASSERT(length > 0); | 1327 ASSERT(length > 0); |
| 1328 AddElement(*string); | 1328 AddElement(*string); |
| 1329 if (!StringShape(*string).IsAsciiRepresentation()) { | 1329 if (!string->IsAsciiRepresentation()) { |
| 1330 is_ascii_ = false; | 1330 is_ascii_ = false; |
| 1331 } | 1331 } |
| 1332 IncrementCharacterCount(length); | 1332 IncrementCharacterCount(length); |
| 1333 } | 1333 } |
| 1334 | 1334 |
| 1335 | 1335 |
| 1336 Handle<String> ToString() { | 1336 Handle<String> ToString() { |
| 1337 if (part_count_ == 0) { | 1337 if (part_count_ == 0) { |
| 1338 return Factory::empty_string(); | 1338 return Factory::empty_string(); |
| 1339 } | 1339 } |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1576 | 1576 |
| 1577 ZoneList<ReplacementPart> parts_; | 1577 ZoneList<ReplacementPart> parts_; |
| 1578 ZoneList<Handle<String> > replacement_substrings_; | 1578 ZoneList<Handle<String> > replacement_substrings_; |
| 1579 }; | 1579 }; |
| 1580 | 1580 |
| 1581 | 1581 |
| 1582 void CompiledReplacement::Compile(Handle<String> replacement, | 1582 void CompiledReplacement::Compile(Handle<String> replacement, |
| 1583 int capture_count, | 1583 int capture_count, |
| 1584 int subject_length) { | 1584 int subject_length) { |
| 1585 ASSERT(replacement->IsFlat()); | 1585 ASSERT(replacement->IsFlat()); |
| 1586 if (StringShape(*replacement).IsAsciiRepresentation()) { | 1586 if (replacement->IsAsciiRepresentation()) { |
| 1587 AssertNoAllocation no_alloc; | 1587 AssertNoAllocation no_alloc; |
| 1588 ParseReplacementPattern(&parts_, | 1588 ParseReplacementPattern(&parts_, |
| 1589 replacement->ToAsciiVector(), | 1589 replacement->ToAsciiVector(), |
| 1590 capture_count, | 1590 capture_count, |
| 1591 subject_length); | 1591 subject_length); |
| 1592 } else { | 1592 } else { |
| 1593 ASSERT(StringShape(*replacement).IsTwoByteRepresentation()); | 1593 ASSERT(replacement->IsTwoByteRepresentation()); |
| 1594 AssertNoAllocation no_alloc; | 1594 AssertNoAllocation no_alloc; |
| 1595 | 1595 |
| 1596 ParseReplacementPattern(&parts_, | 1596 ParseReplacementPattern(&parts_, |
| 1597 replacement->ToUC16Vector(), | 1597 replacement->ToUC16Vector(), |
| 1598 capture_count, | 1598 capture_count, |
| 1599 subject_length); | 1599 subject_length); |
| 1600 } | 1600 } |
| 1601 // Find substrings of replacement string and create them as String objects.. | 1601 // Find substrings of replacement string and create them as String objects.. |
| 1602 int substring_index = 0; | 1602 int substring_index = 0; |
| 1603 for (int i = 0, n = parts_.length(); i < n; i++) { | 1603 for (int i = 0, n = parts_.length(); i < n; i++) { |
| (...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2158 if (start_index + pattern_length > subject_length) return -1; | 2158 if (start_index + pattern_length > subject_length) return -1; |
| 2159 | 2159 |
| 2160 if (!sub->IsFlat()) { | 2160 if (!sub->IsFlat()) { |
| 2161 FlattenString(sub); | 2161 FlattenString(sub); |
| 2162 } | 2162 } |
| 2163 // Searching for one specific character is common. For one | 2163 // Searching for one specific character is common. For one |
| 2164 // character patterns linear search is necessary, so any smart | 2164 // character patterns linear search is necessary, so any smart |
| 2165 // algorithm is unnecessary overhead. | 2165 // algorithm is unnecessary overhead. |
| 2166 if (pattern_length == 1) { | 2166 if (pattern_length == 1) { |
| 2167 AssertNoAllocation no_heap_allocation; // ensure vectors stay valid | 2167 AssertNoAllocation no_heap_allocation; // ensure vectors stay valid |
| 2168 if (StringShape(*sub).IsAsciiRepresentation()) { | 2168 if (sub->IsAsciiRepresentation()) { |
| 2169 uc16 pchar = pat->Get(0); | 2169 uc16 pchar = pat->Get(0); |
| 2170 if (pchar > String::kMaxAsciiCharCode) { | 2170 if (pchar > String::kMaxAsciiCharCode) { |
| 2171 return -1; | 2171 return -1; |
| 2172 } | 2172 } |
| 2173 Vector<const char> ascii_vector = | 2173 Vector<const char> ascii_vector = |
| 2174 sub->ToAsciiVector().SubVector(start_index, subject_length); | 2174 sub->ToAsciiVector().SubVector(start_index, subject_length); |
| 2175 const void* pos = memchr(ascii_vector.start(), | 2175 const void* pos = memchr(ascii_vector.start(), |
| 2176 static_cast<const char>(pchar), | 2176 static_cast<const char>(pchar), |
| 2177 static_cast<size_t>(ascii_vector.length())); | 2177 static_cast<size_t>(ascii_vector.length())); |
| 2178 if (pos == NULL) { | 2178 if (pos == NULL) { |
| 2179 return -1; | 2179 return -1; |
| 2180 } | 2180 } |
| 2181 return reinterpret_cast<const char*>(pos) - ascii_vector.start() | 2181 return reinterpret_cast<const char*>(pos) - ascii_vector.start() |
| 2182 + start_index; | 2182 + start_index; |
| 2183 } | 2183 } |
| 2184 return SingleCharIndexOf(sub->ToUC16Vector(), pat->Get(0), start_index); | 2184 return SingleCharIndexOf(sub->ToUC16Vector(), pat->Get(0), start_index); |
| 2185 } | 2185 } |
| 2186 | 2186 |
| 2187 if (!pat->IsFlat()) { | 2187 if (!pat->IsFlat()) { |
| 2188 FlattenString(pat); | 2188 FlattenString(pat); |
| 2189 } | 2189 } |
| 2190 | 2190 |
| 2191 AssertNoAllocation no_heap_allocation; // ensure vectors stay valid | 2191 AssertNoAllocation no_heap_allocation; // ensure vectors stay valid |
| 2192 // dispatch on type of strings | 2192 // dispatch on type of strings |
| 2193 if (StringShape(*pat).IsAsciiRepresentation()) { | 2193 if (pat->IsAsciiRepresentation()) { |
| 2194 Vector<const char> pat_vector = pat->ToAsciiVector(); | 2194 Vector<const char> pat_vector = pat->ToAsciiVector(); |
| 2195 if (StringShape(*sub).IsAsciiRepresentation()) { | 2195 if (sub->IsAsciiRepresentation()) { |
| 2196 return StringMatchStrategy(sub->ToAsciiVector(), pat_vector, start_index); | 2196 return StringMatchStrategy(sub->ToAsciiVector(), pat_vector, start_index); |
| 2197 } | 2197 } |
| 2198 return StringMatchStrategy(sub->ToUC16Vector(), pat_vector, start_index); | 2198 return StringMatchStrategy(sub->ToUC16Vector(), pat_vector, start_index); |
| 2199 } | 2199 } |
| 2200 Vector<const uc16> pat_vector = pat->ToUC16Vector(); | 2200 Vector<const uc16> pat_vector = pat->ToUC16Vector(); |
| 2201 if (StringShape(*sub).IsAsciiRepresentation()) { | 2201 if (sub->IsAsciiRepresentation()) { |
| 2202 return StringMatchStrategy(sub->ToAsciiVector(), pat_vector, start_index); | 2202 return StringMatchStrategy(sub->ToAsciiVector(), pat_vector, start_index); |
| 2203 } | 2203 } |
| 2204 return StringMatchStrategy(sub->ToUC16Vector(), pat_vector, start_index); | 2204 return StringMatchStrategy(sub->ToUC16Vector(), pat_vector, start_index); |
| 2205 } | 2205 } |
| 2206 | 2206 |
| 2207 | 2207 |
| 2208 static Object* Runtime_StringIndexOf(Arguments args) { | 2208 static Object* Runtime_StringIndexOf(Arguments args) { |
| 2209 HandleScope scope; // create a new handle scope | 2209 HandleScope scope; // create a new handle scope |
| 2210 ASSERT(args.length() == 3); | 2210 ASSERT(args.length() == 3); |
| 2211 | 2211 |
| (...skipping 1110 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3322 // than the input and, if that assumption breaks, again with the exact | 3322 // than the input and, if that assumption breaks, again with the exact |
| 3323 // length. This may not be pretty, but it is nicer than what was here before | 3323 // length. This may not be pretty, but it is nicer than what was here before |
| 3324 // and I hereby claim my vaffel-is. | 3324 // and I hereby claim my vaffel-is. |
| 3325 // | 3325 // |
| 3326 // Allocate the resulting string. | 3326 // Allocate the resulting string. |
| 3327 // | 3327 // |
| 3328 // NOTE: This assumes that the upper/lower case of an ascii | 3328 // NOTE: This assumes that the upper/lower case of an ascii |
| 3329 // character is also ascii. This is currently the case, but it | 3329 // character is also ascii. This is currently the case, but it |
| 3330 // might break in the future if we implement more context and locale | 3330 // might break in the future if we implement more context and locale |
| 3331 // dependent upper/lower conversions. | 3331 // dependent upper/lower conversions. |
| 3332 Object* o = StringShape(s).IsAsciiRepresentation() | 3332 Object* o = s->IsAsciiRepresentation() |
| 3333 ? Heap::AllocateRawAsciiString(length) | 3333 ? Heap::AllocateRawAsciiString(length) |
| 3334 : Heap::AllocateRawTwoByteString(length); | 3334 : Heap::AllocateRawTwoByteString(length); |
| 3335 if (o->IsFailure()) return o; | 3335 if (o->IsFailure()) return o; |
| 3336 String* result = String::cast(o); | 3336 String* result = String::cast(o); |
| 3337 bool has_changed_character = false; | 3337 bool has_changed_character = false; |
| 3338 | 3338 |
| 3339 // Convert all characters to upper case, assuming that they will fit | 3339 // Convert all characters to upper case, assuming that they will fit |
| 3340 // in the buffer | 3340 // in the buffer |
| 3341 Access<StringInputBuffer> buffer(&runtime_string_input_buffer); | 3341 Access<StringInputBuffer> buffer(&runtime_string_input_buffer); |
| 3342 buffer->Reset(s); | 3342 buffer->Reset(s); |
| (...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3673 array_length = fixed_array->length(); | 3673 array_length = fixed_array->length(); |
| 3674 } | 3674 } |
| 3675 | 3675 |
| 3676 if (array_length == 0) { | 3676 if (array_length == 0) { |
| 3677 return Heap::empty_string(); | 3677 return Heap::empty_string(); |
| 3678 } else if (array_length == 1) { | 3678 } else if (array_length == 1) { |
| 3679 Object* first = fixed_array->get(0); | 3679 Object* first = fixed_array->get(0); |
| 3680 if (first->IsString()) return first; | 3680 if (first->IsString()) return first; |
| 3681 } | 3681 } |
| 3682 | 3682 |
| 3683 bool ascii = StringShape(special).IsAsciiRepresentation(); | 3683 bool ascii = special->IsAsciiRepresentation(); |
| 3684 int position = 0; | 3684 int position = 0; |
| 3685 for (int i = 0; i < array_length; i++) { | 3685 for (int i = 0; i < array_length; i++) { |
| 3686 Object* elt = fixed_array->get(i); | 3686 Object* elt = fixed_array->get(i); |
| 3687 if (elt->IsSmi()) { | 3687 if (elt->IsSmi()) { |
| 3688 int len = Smi::cast(elt)->value(); | 3688 int len = Smi::cast(elt)->value(); |
| 3689 int pos = len >> 11; | 3689 int pos = len >> 11; |
| 3690 len &= 0x7ff; | 3690 len &= 0x7ff; |
| 3691 if (pos + len > special_length) { | 3691 if (pos + len > special_length) { |
| 3692 return Top::Throw(Heap::illegal_argument_symbol()); | 3692 return Top::Throw(Heap::illegal_argument_symbol()); |
| 3693 } | 3693 } |
| 3694 position += len; | 3694 position += len; |
| 3695 } else if (elt->IsString()) { | 3695 } else if (elt->IsString()) { |
| 3696 String* element = String::cast(elt); | 3696 String* element = String::cast(elt); |
| 3697 int element_length = element->length(); | 3697 int element_length = element->length(); |
| 3698 if (!Smi::IsValid(element_length + position)) { | 3698 if (!Smi::IsValid(element_length + position)) { |
| 3699 Top::context()->mark_out_of_memory(); | 3699 Top::context()->mark_out_of_memory(); |
| 3700 return Failure::OutOfMemoryException(); | 3700 return Failure::OutOfMemoryException(); |
| 3701 } | 3701 } |
| 3702 position += element_length; | 3702 position += element_length; |
| 3703 if (ascii && !StringShape(element).IsAsciiRepresentation()) { | 3703 if (ascii && !element->IsAsciiRepresentation()) { |
| 3704 ascii = false; | 3704 ascii = false; |
| 3705 } | 3705 } |
| 3706 } else { | 3706 } else { |
| 3707 return Top::Throw(Heap::illegal_argument_symbol()); | 3707 return Top::Throw(Heap::illegal_argument_symbol()); |
| 3708 } | 3708 } |
| 3709 } | 3709 } |
| 3710 | 3710 |
| 3711 int length = position; | 3711 int length = position; |
| 3712 Object* object; | 3712 Object* object; |
| 3713 | 3713 |
| (...skipping 1036 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4750 FlattenString(str); | 4750 FlattenString(str); |
| 4751 | 4751 |
| 4752 CONVERT_ARG_CHECKED(JSArray, output, 1); | 4752 CONVERT_ARG_CHECKED(JSArray, output, 1); |
| 4753 RUNTIME_ASSERT(output->HasFastElements()); | 4753 RUNTIME_ASSERT(output->HasFastElements()); |
| 4754 | 4754 |
| 4755 AssertNoAllocation no_allocation; | 4755 AssertNoAllocation no_allocation; |
| 4756 | 4756 |
| 4757 FixedArray* output_array = output->elements(); | 4757 FixedArray* output_array = output->elements(); |
| 4758 RUNTIME_ASSERT(output_array->length() >= DateParser::OUTPUT_SIZE); | 4758 RUNTIME_ASSERT(output_array->length() >= DateParser::OUTPUT_SIZE); |
| 4759 bool result; | 4759 bool result; |
| 4760 if (StringShape(*str).IsAsciiRepresentation()) { | 4760 if (str->IsAsciiRepresentation()) { |
| 4761 result = DateParser::Parse(str->ToAsciiVector(), output_array); | 4761 result = DateParser::Parse(str->ToAsciiVector(), output_array); |
| 4762 } else { | 4762 } else { |
| 4763 ASSERT(StringShape(*str).IsTwoByteRepresentation()); | 4763 ASSERT(str->IsTwoByteRepresentation()); |
| 4764 result = DateParser::Parse(str->ToUC16Vector(), output_array); | 4764 result = DateParser::Parse(str->ToUC16Vector(), output_array); |
| 4765 } | 4765 } |
| 4766 | 4766 |
| 4767 if (result) { | 4767 if (result) { |
| 4768 return *output; | 4768 return *output; |
| 4769 } else { | 4769 } else { |
| 4770 return Heap::null_value(); | 4770 return Heap::null_value(); |
| 4771 } | 4771 } |
| 4772 } | 4772 } |
| 4773 | 4773 |
| (...skipping 1792 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 6566 return *result; | 6566 return *result; |
| 6567 } | 6567 } |
| 6568 | 6568 |
| 6569 | 6569 |
| 6570 // If an object given is an external string, check that the underlying | 6570 // If an object given is an external string, check that the underlying |
| 6571 // resource is accessible. For other kinds of objects, always return true. | 6571 // resource is accessible. For other kinds of objects, always return true. |
| 6572 static bool IsExternalStringValid(Object* str) { | 6572 static bool IsExternalStringValid(Object* str) { |
| 6573 if (!str->IsString() || !StringShape(String::cast(str)).IsExternal()) { | 6573 if (!str->IsString() || !StringShape(String::cast(str)).IsExternal()) { |
| 6574 return true; | 6574 return true; |
| 6575 } | 6575 } |
| 6576 if (StringShape(String::cast(str)).IsAsciiRepresentation()) { | 6576 if (String::cast(str)->IsAsciiRepresentation()) { |
| 6577 return ExternalAsciiString::cast(str)->resource() != NULL; | 6577 return ExternalAsciiString::cast(str)->resource() != NULL; |
| 6578 } else if (StringShape(String::cast(str)).IsTwoByteRepresentation()) { | 6578 } else if (String::cast(str)->IsTwoByteRepresentation()) { |
| 6579 return ExternalTwoByteString::cast(str)->resource() != NULL; | 6579 return ExternalTwoByteString::cast(str)->resource() != NULL; |
| 6580 } else { | 6580 } else { |
| 6581 return true; | 6581 return true; |
| 6582 } | 6582 } |
| 6583 } | 6583 } |
| 6584 | 6584 |
| 6585 | 6585 |
| 6586 // Helper function used by Runtime_DebugGetLoadedScripts below. | 6586 // Helper function used by Runtime_DebugGetLoadedScripts below. |
| 6587 static int DebugGetLoadedScripts(FixedArray* instances, int instances_size) { | 6587 static int DebugGetLoadedScripts(FixedArray* instances, int instances_size) { |
| 6588 NoHandleAllocation ha; | 6588 NoHandleAllocation ha; |
| (...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 7003 } else { | 7003 } else { |
| 7004 // Handle last resort GC and make sure to allow future allocations | 7004 // Handle last resort GC and make sure to allow future allocations |
| 7005 // to grow the heap without causing GCs (if possible). | 7005 // to grow the heap without causing GCs (if possible). |
| 7006 Counters::gc_last_resort_from_js.Increment(); | 7006 Counters::gc_last_resort_from_js.Increment(); |
| 7007 Heap::CollectAllGarbage(); | 7007 Heap::CollectAllGarbage(); |
| 7008 } | 7008 } |
| 7009 } | 7009 } |
| 7010 | 7010 |
| 7011 | 7011 |
| 7012 } } // namespace v8::internal | 7012 } } // namespace v8::internal |
| OLD | NEW |