| OLD | NEW |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/runtime/runtime-utils.h" | 5 #include "src/runtime/runtime-utils.h" |
| 6 | 6 |
| 7 #include "src/arguments.h" | 7 #include "src/arguments.h" |
| 8 #include "src/conversions-inl.h" | 8 #include "src/conversions-inl.h" |
| 9 #include "src/isolate-inl.h" | 9 #include "src/isolate-inl.h" |
| 10 #include "src/messages.h" | 10 #include "src/messages.h" |
| (...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 limit, zone); | 382 limit, zone); |
| 383 } | 383 } |
| 384 } | 384 } |
| 385 } | 385 } |
| 386 } | 386 } |
| 387 } | 387 } |
| 388 | 388 |
| 389 template <typename ResultSeqString> | 389 template <typename ResultSeqString> |
| 390 MUST_USE_RESULT static Object* StringReplaceGlobalAtomRegExpWithString( | 390 MUST_USE_RESULT static Object* StringReplaceGlobalAtomRegExpWithString( |
| 391 Isolate* isolate, Handle<String> subject, Handle<JSRegExp> pattern_regexp, | 391 Isolate* isolate, Handle<String> subject, Handle<JSRegExp> pattern_regexp, |
| 392 Handle<String> replacement, Handle<JSObject> last_match_info) { | 392 Handle<String> replacement, Handle<JSObject> last_match_info, Zone* zone) { |
| 393 DCHECK(subject->IsFlat()); | 393 DCHECK(subject->IsFlat()); |
| 394 DCHECK(replacement->IsFlat()); | 394 DCHECK(replacement->IsFlat()); |
| 395 | 395 |
| 396 ZoneScope zone_scope(isolate->runtime_zone()); | 396 ZoneList<int> indices(8, zone); |
| 397 ZoneList<int> indices(8, zone_scope.zone()); | |
| 398 DCHECK_EQ(JSRegExp::ATOM, pattern_regexp->TypeTag()); | 397 DCHECK_EQ(JSRegExp::ATOM, pattern_regexp->TypeTag()); |
| 399 String* pattern = | 398 String* pattern = |
| 400 String::cast(pattern_regexp->DataAt(JSRegExp::kAtomPatternIndex)); | 399 String::cast(pattern_regexp->DataAt(JSRegExp::kAtomPatternIndex)); |
| 401 int subject_len = subject->length(); | 400 int subject_len = subject->length(); |
| 402 int pattern_len = pattern->length(); | 401 int pattern_len = pattern->length(); |
| 403 int replacement_len = replacement->length(); | 402 int replacement_len = replacement->length(); |
| 404 | 403 |
| 405 FindStringIndicesDispatch(isolate, *subject, pattern, &indices, 0xffffffff, | 404 FindStringIndicesDispatch(isolate, *subject, pattern, &indices, 0xffffffff, |
| 406 zone_scope.zone()); | 405 zone); |
| 407 | 406 |
| 408 int matches = indices.length(); | 407 int matches = indices.length(); |
| 409 if (matches == 0) return *subject; | 408 if (matches == 0) return *subject; |
| 410 | 409 |
| 411 // Detect integer overflow. | 410 // Detect integer overflow. |
| 412 int64_t result_len_64 = (static_cast<int64_t>(replacement_len) - | 411 int64_t result_len_64 = (static_cast<int64_t>(replacement_len) - |
| 413 static_cast<int64_t>(pattern_len)) * | 412 static_cast<int64_t>(pattern_len)) * |
| 414 static_cast<int64_t>(matches) + | 413 static_cast<int64_t>(matches) + |
| 415 static_cast<int64_t>(subject_len); | 414 static_cast<int64_t>(subject_len); |
| 416 int result_len; | 415 int result_len; |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 // CompiledReplacement uses zone allocation. | 475 // CompiledReplacement uses zone allocation. |
| 477 ZoneScope zone_scope(isolate->runtime_zone()); | 476 ZoneScope zone_scope(isolate->runtime_zone()); |
| 478 CompiledReplacement compiled_replacement(zone_scope.zone()); | 477 CompiledReplacement compiled_replacement(zone_scope.zone()); |
| 479 bool simple_replace = | 478 bool simple_replace = |
| 480 compiled_replacement.Compile(replacement, capture_count, subject_length); | 479 compiled_replacement.Compile(replacement, capture_count, subject_length); |
| 481 | 480 |
| 482 // Shortcut for simple non-regexp global replacements | 481 // Shortcut for simple non-regexp global replacements |
| 483 if (regexp->TypeTag() == JSRegExp::ATOM && simple_replace) { | 482 if (regexp->TypeTag() == JSRegExp::ATOM && simple_replace) { |
| 484 if (subject->HasOnlyOneByteChars() && replacement->HasOnlyOneByteChars()) { | 483 if (subject->HasOnlyOneByteChars() && replacement->HasOnlyOneByteChars()) { |
| 485 return StringReplaceGlobalAtomRegExpWithString<SeqOneByteString>( | 484 return StringReplaceGlobalAtomRegExpWithString<SeqOneByteString>( |
| 486 isolate, subject, regexp, replacement, last_match_info); | 485 isolate, subject, regexp, replacement, last_match_info, |
| 486 zone_scope.zone()); |
| 487 } else { | 487 } else { |
| 488 return StringReplaceGlobalAtomRegExpWithString<SeqTwoByteString>( | 488 return StringReplaceGlobalAtomRegExpWithString<SeqTwoByteString>( |
| 489 isolate, subject, regexp, replacement, last_match_info); | 489 isolate, subject, regexp, replacement, last_match_info, |
| 490 zone_scope.zone()); |
| 490 } | 491 } |
| 491 } | 492 } |
| 492 | 493 |
| 493 RegExpImpl::GlobalCache global_cache(regexp, subject, isolate); | 494 RegExpImpl::GlobalCache global_cache(regexp, subject, isolate); |
| 494 if (global_cache.HasException()) return isolate->heap()->exception(); | 495 if (global_cache.HasException()) return isolate->heap()->exception(); |
| 495 | 496 |
| 496 int32_t* current_match = global_cache.FetchNext(); | 497 int32_t* current_match = global_cache.FetchNext(); |
| 497 if (current_match == NULL) { | 498 if (current_match == NULL) { |
| 498 if (global_cache.HasException()) return isolate->heap()->exception(); | 499 if (global_cache.HasException()) return isolate->heap()->exception(); |
| 499 return *subject; | 500 return *subject; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 541 | 542 |
| 542 RegExpImpl::SetLastMatchInfo(last_match_info, subject, capture_count, | 543 RegExpImpl::SetLastMatchInfo(last_match_info, subject, capture_count, |
| 543 global_cache.LastSuccessfulMatch()); | 544 global_cache.LastSuccessfulMatch()); |
| 544 | 545 |
| 545 RETURN_RESULT_OR_FAILURE(isolate, builder.ToString()); | 546 RETURN_RESULT_OR_FAILURE(isolate, builder.ToString()); |
| 546 } | 547 } |
| 547 | 548 |
| 548 template <typename ResultSeqString> | 549 template <typename ResultSeqString> |
| 549 MUST_USE_RESULT static Object* StringReplaceGlobalRegExpWithEmptyString( | 550 MUST_USE_RESULT static Object* StringReplaceGlobalRegExpWithEmptyString( |
| 550 Isolate* isolate, Handle<String> subject, Handle<JSRegExp> regexp, | 551 Isolate* isolate, Handle<String> subject, Handle<JSRegExp> regexp, |
| 551 Handle<JSObject> last_match_info) { | 552 Handle<JSObject> last_match_info, Zone* zone) { |
| 552 DCHECK(subject->IsFlat()); | 553 DCHECK(subject->IsFlat()); |
| 553 | 554 |
| 554 // Shortcut for simple non-regexp global replacements | 555 // Shortcut for simple non-regexp global replacements |
| 555 if (regexp->TypeTag() == JSRegExp::ATOM) { | 556 if (regexp->TypeTag() == JSRegExp::ATOM) { |
| 556 Handle<String> empty_string = isolate->factory()->empty_string(); | 557 Handle<String> empty_string = isolate->factory()->empty_string(); |
| 557 if (subject->IsOneByteRepresentation()) { | 558 if (subject->IsOneByteRepresentation()) { |
| 558 return StringReplaceGlobalAtomRegExpWithString<SeqOneByteString>( | 559 return StringReplaceGlobalAtomRegExpWithString<SeqOneByteString>( |
| 559 isolate, subject, regexp, empty_string, last_match_info); | 560 isolate, subject, regexp, empty_string, last_match_info, zone); |
| 560 } else { | 561 } else { |
| 561 return StringReplaceGlobalAtomRegExpWithString<SeqTwoByteString>( | 562 return StringReplaceGlobalAtomRegExpWithString<SeqTwoByteString>( |
| 562 isolate, subject, regexp, empty_string, last_match_info); | 563 isolate, subject, regexp, empty_string, last_match_info, zone); |
| 563 } | 564 } |
| 564 } | 565 } |
| 565 | 566 |
| 566 RegExpImpl::GlobalCache global_cache(regexp, subject, isolate); | 567 RegExpImpl::GlobalCache global_cache(regexp, subject, isolate); |
| 567 if (global_cache.HasException()) return isolate->heap()->exception(); | 568 if (global_cache.HasException()) return isolate->heap()->exception(); |
| 568 | 569 |
| 569 int32_t* current_match = global_cache.FetchNext(); | 570 int32_t* current_match = global_cache.FetchNext(); |
| 570 if (current_match == NULL) { | 571 if (current_match == NULL) { |
| 571 if (global_cache.HasException()) return isolate->heap()->exception(); | 572 if (global_cache.HasException()) return isolate->heap()->exception(); |
| 572 return *subject; | 573 return *subject; |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 651 CONVERT_ARG_HANDLE_CHECKED(String, subject, 0); | 652 CONVERT_ARG_HANDLE_CHECKED(String, subject, 0); |
| 652 CONVERT_ARG_HANDLE_CHECKED(String, replacement, 2); | 653 CONVERT_ARG_HANDLE_CHECKED(String, replacement, 2); |
| 653 CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 1); | 654 CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 1); |
| 654 CONVERT_ARG_HANDLE_CHECKED(JSObject, last_match_info, 3); | 655 CONVERT_ARG_HANDLE_CHECKED(JSObject, last_match_info, 3); |
| 655 | 656 |
| 656 CHECK(regexp->GetFlags() & JSRegExp::kGlobal); | 657 CHECK(regexp->GetFlags() & JSRegExp::kGlobal); |
| 657 CHECK(last_match_info->HasFastObjectElements()); | 658 CHECK(last_match_info->HasFastObjectElements()); |
| 658 | 659 |
| 659 subject = String::Flatten(subject); | 660 subject = String::Flatten(subject); |
| 660 | 661 |
| 662 ZoneScope zone_scope(isolate->runtime_zone()); |
| 663 |
| 661 if (replacement->length() == 0) { | 664 if (replacement->length() == 0) { |
| 662 if (subject->HasOnlyOneByteChars()) { | 665 if (subject->HasOnlyOneByteChars()) { |
| 663 return StringReplaceGlobalRegExpWithEmptyString<SeqOneByteString>( | 666 return StringReplaceGlobalRegExpWithEmptyString<SeqOneByteString>( |
| 664 isolate, subject, regexp, last_match_info); | 667 isolate, subject, regexp, last_match_info, zone_scope.zone()); |
| 665 } else { | 668 } else { |
| 666 return StringReplaceGlobalRegExpWithEmptyString<SeqTwoByteString>( | 669 return StringReplaceGlobalRegExpWithEmptyString<SeqTwoByteString>( |
| 667 isolate, subject, regexp, last_match_info); | 670 isolate, subject, regexp, last_match_info, zone_scope.zone()); |
| 668 } | 671 } |
| 669 } | 672 } |
| 670 | 673 |
| 671 replacement = String::Flatten(replacement); | 674 replacement = String::Flatten(replacement); |
| 672 | 675 |
| 673 return StringReplaceGlobalRegExpWithString(isolate, subject, regexp, | 676 return StringReplaceGlobalRegExpWithString(isolate, subject, regexp, |
| 674 replacement, last_match_info); | 677 replacement, last_match_info); |
| 675 } | 678 } |
| 676 | 679 |
| 677 | 680 |
| (...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1013 | 1016 |
| 1014 | 1017 |
| 1015 RUNTIME_FUNCTION(Runtime_IsRegExp) { | 1018 RUNTIME_FUNCTION(Runtime_IsRegExp) { |
| 1016 SealHandleScope shs(isolate); | 1019 SealHandleScope shs(isolate); |
| 1017 DCHECK(args.length() == 1); | 1020 DCHECK(args.length() == 1); |
| 1018 CONVERT_ARG_CHECKED(Object, obj, 0); | 1021 CONVERT_ARG_CHECKED(Object, obj, 0); |
| 1019 return isolate->heap()->ToBoolean(obj->IsJSRegExp()); | 1022 return isolate->heap()->ToBoolean(obj->IsJSRegExp()); |
| 1020 } | 1023 } |
| 1021 } // namespace internal | 1024 } // namespace internal |
| 1022 } // namespace v8 | 1025 } // namespace v8 |
| OLD | NEW |