OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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 3326 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3337 | 3337 |
3338 class ReplacementStringBuilder { | 3338 class ReplacementStringBuilder { |
3339 public: | 3339 public: |
3340 ReplacementStringBuilder(Heap* heap, | 3340 ReplacementStringBuilder(Heap* heap, |
3341 Handle<String> subject, | 3341 Handle<String> subject, |
3342 int estimated_part_count) | 3342 int estimated_part_count) |
3343 : heap_(heap), | 3343 : heap_(heap), |
3344 array_builder_(heap->isolate(), estimated_part_count), | 3344 array_builder_(heap->isolate(), estimated_part_count), |
3345 subject_(subject), | 3345 subject_(subject), |
3346 character_count_(0), | 3346 character_count_(0), |
3347 is_ascii_(subject->IsOneByteRepresentation()), | 3347 is_ascii_(subject->IsOneByteRepresentation()) { |
3348 overflowed_(false) { | |
3349 // Require a non-zero initial size. Ensures that doubling the size to | 3348 // Require a non-zero initial size. Ensures that doubling the size to |
3350 // extend the array will work. | 3349 // extend the array will work. |
3351 ASSERT(estimated_part_count > 0); | 3350 ASSERT(estimated_part_count > 0); |
3352 } | 3351 } |
3353 | 3352 |
3354 static inline void AddSubjectSlice(FixedArrayBuilder* builder, | 3353 static inline void AddSubjectSlice(FixedArrayBuilder* builder, |
3355 int from, | 3354 int from, |
3356 int to) { | 3355 int to) { |
3357 ASSERT(from >= 0); | 3356 ASSERT(from >= 0); |
3358 int length = to - from; | 3357 int length = to - from; |
(...skipping 27 matching lines...) Expand all Loading... |
3386 ASSERT(length > 0); | 3385 ASSERT(length > 0); |
3387 AddElement(*string); | 3386 AddElement(*string); |
3388 if (!string->IsOneByteRepresentation()) { | 3387 if (!string->IsOneByteRepresentation()) { |
3389 is_ascii_ = false; | 3388 is_ascii_ = false; |
3390 } | 3389 } |
3391 IncrementCharacterCount(length); | 3390 IncrementCharacterCount(length); |
3392 } | 3391 } |
3393 | 3392 |
3394 | 3393 |
3395 Handle<String> ToString() { | 3394 Handle<String> ToString() { |
3396 if (overflowed_) { | |
3397 heap_->isolate()->ThrowInvalidStringLength(); | |
3398 return Handle<String>(); | |
3399 } | |
3400 | |
3401 if (array_builder_.length() == 0) { | 3395 if (array_builder_.length() == 0) { |
3402 return heap_->isolate()->factory()->empty_string(); | 3396 return heap_->isolate()->factory()->empty_string(); |
3403 } | 3397 } |
3404 | 3398 |
3405 Handle<String> joined_string; | 3399 Handle<String> joined_string; |
3406 if (is_ascii_) { | 3400 if (is_ascii_) { |
3407 Handle<SeqOneByteString> seq = NewRawOneByteString(character_count_); | 3401 Handle<SeqOneByteString> seq = NewRawOneByteString(character_count_); |
| 3402 RETURN_IF_EMPTY_HANDLE_VALUE(heap_->isolate(), seq, Handle<String>()); |
3408 DisallowHeapAllocation no_gc; | 3403 DisallowHeapAllocation no_gc; |
3409 uint8_t* char_buffer = seq->GetChars(); | 3404 uint8_t* char_buffer = seq->GetChars(); |
3410 StringBuilderConcatHelper(*subject_, | 3405 StringBuilderConcatHelper(*subject_, |
3411 char_buffer, | 3406 char_buffer, |
3412 *array_builder_.array(), | 3407 *array_builder_.array(), |
3413 array_builder_.length()); | 3408 array_builder_.length()); |
3414 joined_string = Handle<String>::cast(seq); | 3409 joined_string = Handle<String>::cast(seq); |
3415 } else { | 3410 } else { |
3416 // Non-ASCII. | 3411 // Non-ASCII. |
3417 Handle<SeqTwoByteString> seq = NewRawTwoByteString(character_count_); | 3412 Handle<SeqTwoByteString> seq = NewRawTwoByteString(character_count_); |
| 3413 RETURN_IF_EMPTY_HANDLE_VALUE(heap_->isolate(), seq, Handle<String>()); |
3418 DisallowHeapAllocation no_gc; | 3414 DisallowHeapAllocation no_gc; |
3419 uc16* char_buffer = seq->GetChars(); | 3415 uc16* char_buffer = seq->GetChars(); |
3420 StringBuilderConcatHelper(*subject_, | 3416 StringBuilderConcatHelper(*subject_, |
3421 char_buffer, | 3417 char_buffer, |
3422 *array_builder_.array(), | 3418 *array_builder_.array(), |
3423 array_builder_.length()); | 3419 array_builder_.length()); |
3424 joined_string = Handle<String>::cast(seq); | 3420 joined_string = Handle<String>::cast(seq); |
3425 } | 3421 } |
3426 return joined_string; | 3422 return joined_string; |
3427 } | 3423 } |
3428 | 3424 |
3429 | 3425 |
3430 void IncrementCharacterCount(int by) { | 3426 void IncrementCharacterCount(int by) { |
3431 if (character_count_ > String::kMaxLength - by) { | 3427 if (character_count_ > String::kMaxLength - by) { |
3432 overflowed_ = true; | 3428 STATIC_ASSERT(String::kMaxLength < kMaxInt); |
| 3429 character_count_ = kMaxInt; |
| 3430 } else { |
| 3431 character_count_ += by; |
3433 } | 3432 } |
3434 character_count_ += by; | |
3435 } | 3433 } |
3436 | 3434 |
3437 private: | 3435 private: |
3438 Handle<SeqOneByteString> NewRawOneByteString(int length) { | 3436 Handle<SeqOneByteString> NewRawOneByteString(int length) { |
3439 return heap_->isolate()->factory()->NewRawOneByteString(length); | 3437 return heap_->isolate()->factory()->NewRawOneByteString(length); |
3440 } | 3438 } |
3441 | 3439 |
3442 | 3440 |
3443 Handle<SeqTwoByteString> NewRawTwoByteString(int length) { | 3441 Handle<SeqTwoByteString> NewRawTwoByteString(int length) { |
3444 return heap_->isolate()->factory()->NewRawTwoByteString(length); | 3442 return heap_->isolate()->factory()->NewRawTwoByteString(length); |
3445 } | 3443 } |
3446 | 3444 |
3447 | 3445 |
3448 void AddElement(Object* element) { | 3446 void AddElement(Object* element) { |
3449 ASSERT(element->IsSmi() || element->IsString()); | 3447 ASSERT(element->IsSmi() || element->IsString()); |
3450 ASSERT(array_builder_.capacity() > array_builder_.length()); | 3448 ASSERT(array_builder_.capacity() > array_builder_.length()); |
3451 array_builder_.Add(element); | 3449 array_builder_.Add(element); |
3452 } | 3450 } |
3453 | 3451 |
3454 Heap* heap_; | 3452 Heap* heap_; |
3455 FixedArrayBuilder array_builder_; | 3453 FixedArrayBuilder array_builder_; |
3456 Handle<String> subject_; | 3454 Handle<String> subject_; |
3457 int character_count_; | 3455 int character_count_; |
3458 bool is_ascii_; | 3456 bool is_ascii_; |
3459 bool overflowed_; | |
3460 }; | 3457 }; |
3461 | 3458 |
3462 | 3459 |
3463 class CompiledReplacement { | 3460 class CompiledReplacement { |
3464 public: | 3461 public: |
3465 explicit CompiledReplacement(Zone* zone) | 3462 explicit CompiledReplacement(Zone* zone) |
3466 : parts_(1, zone), replacement_substrings_(0, zone), zone_(zone) {} | 3463 : parts_(1, zone), replacement_substrings_(0, zone), zone_(zone) {} |
3467 | 3464 |
3468 // Return whether the replacement is simple. | 3465 // Return whether the replacement is simple. |
3469 bool Compile(Handle<String> replacement, | 3466 bool Compile(Handle<String> replacement, |
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
3906 | 3903 |
3907 int matches = indices.length(); | 3904 int matches = indices.length(); |
3908 if (matches == 0) return *subject; | 3905 if (matches == 0) return *subject; |
3909 | 3906 |
3910 // Detect integer overflow. | 3907 // Detect integer overflow. |
3911 int64_t result_len_64 = | 3908 int64_t result_len_64 = |
3912 (static_cast<int64_t>(replacement_len) - | 3909 (static_cast<int64_t>(replacement_len) - |
3913 static_cast<int64_t>(pattern_len)) * | 3910 static_cast<int64_t>(pattern_len)) * |
3914 static_cast<int64_t>(matches) + | 3911 static_cast<int64_t>(matches) + |
3915 static_cast<int64_t>(subject_len); | 3912 static_cast<int64_t>(subject_len); |
3916 if (result_len_64 > INT_MAX) { | 3913 int result_len; |
3917 v8::internal::Heap::FatalProcessOutOfMemory("invalid string length", true); | 3914 if (result_len_64 > static_cast<int64_t>(String::kMaxLength)) { |
| 3915 STATIC_ASSERT(String::kMaxLength < kMaxInt); |
| 3916 result_len = kMaxInt; // Provoke exception. |
| 3917 } else { |
| 3918 result_len = static_cast<int>(result_len_64); |
3918 } | 3919 } |
3919 int result_len = static_cast<int>(result_len_64); | |
3920 | 3920 |
3921 int subject_pos = 0; | 3921 int subject_pos = 0; |
3922 int result_pos = 0; | 3922 int result_pos = 0; |
3923 | 3923 |
3924 Handle<ResultSeqString> result; | 3924 Handle<ResultSeqString> result; |
3925 if (ResultSeqString::kHasAsciiEncoding) { | 3925 if (ResultSeqString::kHasAsciiEncoding) { |
3926 result = Handle<ResultSeqString>::cast( | 3926 result = Handle<ResultSeqString>::cast( |
3927 isolate->factory()->NewRawOneByteString(result_len)); | 3927 isolate->factory()->NewRawOneByteString(result_len)); |
3928 } else { | 3928 } else { |
3929 result = Handle<ResultSeqString>::cast( | 3929 result = Handle<ResultSeqString>::cast( |
3930 isolate->factory()->NewRawTwoByteString(result_len)); | 3930 isolate->factory()->NewRawTwoByteString(result_len)); |
3931 } | 3931 } |
| 3932 RETURN_IF_EMPTY_HANDLE(isolate, result); |
3932 | 3933 |
3933 for (int i = 0; i < matches; i++) { | 3934 for (int i = 0; i < matches; i++) { |
3934 // Copy non-matched subject content. | 3935 // Copy non-matched subject content. |
3935 if (subject_pos < indices.at(i)) { | 3936 if (subject_pos < indices.at(i)) { |
3936 String::WriteToFlat(*subject, | 3937 String::WriteToFlat(*subject, |
3937 result->GetChars() + result_pos, | 3938 result->GetChars() + result_pos, |
3938 subject_pos, | 3939 subject_pos, |
3939 indices.at(i)); | 3940 indices.at(i)); |
3940 result_pos += indices.at(i) - subject_pos; | 3941 result_pos += indices.at(i) - subject_pos; |
3941 } | 3942 } |
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4101 if (new_length == 0) return isolate->heap()->empty_string(); | 4102 if (new_length == 0) return isolate->heap()->empty_string(); |
4102 | 4103 |
4103 Handle<ResultSeqString> answer; | 4104 Handle<ResultSeqString> answer; |
4104 if (ResultSeqString::kHasAsciiEncoding) { | 4105 if (ResultSeqString::kHasAsciiEncoding) { |
4105 answer = Handle<ResultSeqString>::cast( | 4106 answer = Handle<ResultSeqString>::cast( |
4106 isolate->factory()->NewRawOneByteString(new_length)); | 4107 isolate->factory()->NewRawOneByteString(new_length)); |
4107 } else { | 4108 } else { |
4108 answer = Handle<ResultSeqString>::cast( | 4109 answer = Handle<ResultSeqString>::cast( |
4109 isolate->factory()->NewRawTwoByteString(new_length)); | 4110 isolate->factory()->NewRawTwoByteString(new_length)); |
4110 } | 4111 } |
| 4112 ASSERT(!answer.is_null()); |
4111 | 4113 |
4112 int prev = 0; | 4114 int prev = 0; |
4113 int position = 0; | 4115 int position = 0; |
4114 | 4116 |
4115 do { | 4117 do { |
4116 start = current_match[0]; | 4118 start = current_match[0]; |
4117 end = current_match[1]; | 4119 end = current_match[1]; |
4118 if (prev < start) { | 4120 if (prev < start) { |
4119 // Add substring subject[prev;start] to answer string. | 4121 // Add substring subject[prev;start] to answer string. |
4120 String::WriteToFlat(*subject, answer->GetChars() + position, prev, start); | 4122 String::WriteToFlat(*subject, answer->GetChars() + position, prev, start); |
(...skipping 2430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6551 | 6553 |
6552 // Simpler handling of ASCII strings. | 6554 // Simpler handling of ASCII strings. |
6553 // | 6555 // |
6554 // NOTE: This assumes that the upper/lower case of an ASCII | 6556 // NOTE: This assumes that the upper/lower case of an ASCII |
6555 // character is also ASCII. This is currently the case, but it | 6557 // character is also ASCII. This is currently the case, but it |
6556 // might break in the future if we implement more context and locale | 6558 // might break in the future if we implement more context and locale |
6557 // dependent upper/lower conversions. | 6559 // dependent upper/lower conversions. |
6558 if (s->IsOneByteRepresentationUnderneath()) { | 6560 if (s->IsOneByteRepresentationUnderneath()) { |
6559 Handle<SeqOneByteString> result = | 6561 Handle<SeqOneByteString> result = |
6560 isolate->factory()->NewRawOneByteString(length); | 6562 isolate->factory()->NewRawOneByteString(length); |
6561 | 6563 ASSERT(!result.is_null()); // Same length as input. |
6562 DisallowHeapAllocation no_gc; | 6564 DisallowHeapAllocation no_gc; |
6563 String::FlatContent flat_content = s->GetFlatContent(); | 6565 String::FlatContent flat_content = s->GetFlatContent(); |
6564 ASSERT(flat_content.IsFlat()); | 6566 ASSERT(flat_content.IsFlat()); |
6565 bool has_changed_character = false; | 6567 bool has_changed_character = false; |
6566 bool is_ascii = FastAsciiConvert<Converter>( | 6568 bool is_ascii = FastAsciiConvert<Converter>( |
6567 reinterpret_cast<char*>(result->GetChars()), | 6569 reinterpret_cast<char*>(result->GetChars()), |
6568 reinterpret_cast<const char*>(flat_content.ToOneByteVector().start()), | 6570 reinterpret_cast<const char*>(flat_content.ToOneByteVector().start()), |
6569 length, | 6571 length, |
6570 &has_changed_character); | 6572 &has_changed_character); |
6571 // If not ASCII, we discard the result and take the 2 byte path. | 6573 // If not ASCII, we discard the result and take the 2 byte path. |
6572 if (is_ascii) return has_changed_character ? *result : *s; | 6574 if (is_ascii) return has_changed_character ? *result : *s; |
6573 } | 6575 } |
6574 | 6576 |
6575 Handle<SeqString> result; | 6577 Handle<SeqString> result; |
6576 if (s->IsOneByteRepresentation()) { | 6578 if (s->IsOneByteRepresentation()) { |
6577 result = isolate->factory()->NewRawOneByteString(length); | 6579 result = isolate->factory()->NewRawOneByteString(length); |
6578 } else { | 6580 } else { |
6579 result = isolate->factory()->NewRawTwoByteString(length); | 6581 result = isolate->factory()->NewRawTwoByteString(length); |
6580 } | 6582 } |
| 6583 ASSERT(!result.is_null()); // Same length as input. |
| 6584 |
6581 MaybeObject* maybe = ConvertCaseHelper(isolate, *s, *result, length, mapping); | 6585 MaybeObject* maybe = ConvertCaseHelper(isolate, *s, *result, length, mapping); |
6582 Object* answer; | 6586 Object* answer; |
6583 if (!maybe->ToObject(&answer)) return maybe; | 6587 if (!maybe->ToObject(&answer)) return maybe; |
6584 if (answer->IsString()) return answer; | 6588 if (answer->IsString()) return answer; |
6585 | 6589 |
6586 ASSERT(answer->IsSmi()); | 6590 ASSERT(answer->IsSmi()); |
6587 length = Smi::cast(answer)->value(); | 6591 length = Smi::cast(answer)->value(); |
6588 if (s->IsOneByteRepresentation() && length > 0) { | 6592 if (s->IsOneByteRepresentation() && length > 0) { |
6589 result = isolate->factory()->NewRawOneByteString(length); | 6593 result = isolate->factory()->NewRawOneByteString(length); |
6590 } else { | 6594 } else { |
6591 if (length < 0) length = -length; | 6595 if (length < 0) length = -length; |
6592 result = isolate->factory()->NewRawTwoByteString(length); | 6596 result = isolate->factory()->NewRawTwoByteString(length); |
6593 } | 6597 } |
| 6598 RETURN_IF_EMPTY_HANDLE(isolate, result); |
6594 return ConvertCaseHelper(isolate, *s, *result, length, mapping); | 6599 return ConvertCaseHelper(isolate, *s, *result, length, mapping); |
6595 } | 6600 } |
6596 | 6601 |
6597 | 6602 |
6598 RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToLowerCase) { | 6603 RUNTIME_FUNCTION(MaybeObject*, Runtime_StringToLowerCase) { |
6599 return ConvertCase( | 6604 return ConvertCase( |
6600 args, isolate, isolate->runtime_state()->to_lower_mapping()); | 6605 args, isolate, isolate->runtime_state()->to_lower_mapping()); |
6601 } | 6606 } |
6602 | 6607 |
6603 | 6608 |
(...skipping 624 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
7228 if (max_nof_separators < (array_length - 1)) { | 7233 if (max_nof_separators < (array_length - 1)) { |
7229 return isolate->ThrowInvalidStringLength(); | 7234 return isolate->ThrowInvalidStringLength(); |
7230 } | 7235 } |
7231 int length = (array_length - 1) * separator_length; | 7236 int length = (array_length - 1) * separator_length; |
7232 for (int i = 0; i < array_length; i++) { | 7237 for (int i = 0; i < array_length; i++) { |
7233 Object* element_obj = fixed_array->get(i); | 7238 Object* element_obj = fixed_array->get(i); |
7234 RUNTIME_ASSERT(element_obj->IsString()); | 7239 RUNTIME_ASSERT(element_obj->IsString()); |
7235 String* element = String::cast(element_obj); | 7240 String* element = String::cast(element_obj); |
7236 int increment = element->length(); | 7241 int increment = element->length(); |
7237 if (increment > String::kMaxLength - length) { | 7242 if (increment > String::kMaxLength - length) { |
7238 return isolate->ThrowInvalidStringLength(); | 7243 STATIC_ASSERT(String::kMaxLength < kMaxInt); |
| 7244 length = kMaxInt; // Provoke exception; |
| 7245 break; |
7239 } | 7246 } |
7240 length += increment; | 7247 length += increment; |
7241 } | 7248 } |
7242 | 7249 |
7243 Handle<SeqTwoByteString> answer = | 7250 Handle<SeqTwoByteString> answer = |
7244 isolate->factory()->NewRawTwoByteString(length); | 7251 isolate->factory()->NewRawTwoByteString(length); |
| 7252 RETURN_IF_EMPTY_HANDLE(isolate, answer); |
7245 | 7253 |
7246 DisallowHeapAllocation no_gc; | 7254 DisallowHeapAllocation no_gc; |
7247 | 7255 |
7248 uc16* sink = answer->GetChars(); | 7256 uc16* sink = answer->GetChars(); |
7249 #ifdef DEBUG | 7257 #ifdef DEBUG |
7250 uc16* end = sink + length; | 7258 uc16* end = sink + length; |
7251 #endif | 7259 #endif |
7252 | 7260 |
7253 String* first = String::cast(fixed_array->get(0)); | 7261 String* first = String::cast(fixed_array->get(0)); |
7254 String* seperator_raw = *separator; | 7262 String* seperator_raw = *separator; |
(...skipping 2224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9479 "not_date_object", HandleVector<Object>(NULL, 0))); | 9487 "not_date_object", HandleVector<Object>(NULL, 0))); |
9480 } | 9488 } |
9481 | 9489 |
9482 | 9490 |
9483 RUNTIME_FUNCTION(MaybeObject*, Runtime_ThrowMessage) { | 9491 RUNTIME_FUNCTION(MaybeObject*, Runtime_ThrowMessage) { |
9484 HandleScope scope(isolate); | 9492 HandleScope scope(isolate); |
9485 ASSERT(args.length() == 1); | 9493 ASSERT(args.length() == 1); |
9486 CONVERT_SMI_ARG_CHECKED(message_id, 0); | 9494 CONVERT_SMI_ARG_CHECKED(message_id, 0); |
9487 const char* message = GetBailoutReason( | 9495 const char* message = GetBailoutReason( |
9488 static_cast<BailoutReason>(message_id)); | 9496 static_cast<BailoutReason>(message_id)); |
9489 Handle<Name> message_handle = | 9497 Handle<String> message_handle = |
9490 isolate->factory()->NewStringFromAscii(CStrVector(message)); | 9498 isolate->factory()->NewStringFromAscii(CStrVector(message)); |
| 9499 RETURN_IF_EMPTY_HANDLE(isolate, message_handle); |
9491 return isolate->Throw(*message_handle); | 9500 return isolate->Throw(*message_handle); |
9492 } | 9501 } |
9493 | 9502 |
9494 | 9503 |
9495 RUNTIME_FUNCTION(MaybeObject*, Runtime_StackGuard) { | 9504 RUNTIME_FUNCTION(MaybeObject*, Runtime_StackGuard) { |
9496 SealHandleScope shs(isolate); | 9505 SealHandleScope shs(isolate); |
9497 ASSERT(args.length() == 0); | 9506 ASSERT(args.length() == 0); |
9498 | 9507 |
9499 // First check if this is a real stack overflow. | 9508 // First check if this is a real stack overflow. |
9500 if (isolate->stack_guard()->IsStackOverflow()) { | 9509 if (isolate->stack_guard()->IsStackOverflow()) { |
(...skipping 5561 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
15062 // Handle last resort GC and make sure to allow future allocations | 15071 // Handle last resort GC and make sure to allow future allocations |
15063 // to grow the heap without causing GCs (if possible). | 15072 // to grow the heap without causing GCs (if possible). |
15064 isolate->counters()->gc_last_resort_from_js()->Increment(); | 15073 isolate->counters()->gc_last_resort_from_js()->Increment(); |
15065 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, | 15074 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, |
15066 "Runtime::PerformGC"); | 15075 "Runtime::PerformGC"); |
15067 } | 15076 } |
15068 } | 15077 } |
15069 | 15078 |
15070 | 15079 |
15071 } } // namespace v8::internal | 15080 } } // namespace v8::internal |
OLD | NEW |