Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(202)

Side by Side Diff: src/runtime/runtime-regexp.cc

Issue 719403005: One instead of two runtime calls when initializing regexp. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: addressed comments Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/runtime/runtime.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/v8.h" 5 #include "src/v8.h"
6 6
7 #include "src/arguments.h" 7 #include "src/arguments.h"
8 #include "src/jsregexp-inl.h" 8 #include "src/jsregexp-inl.h"
9 #include "src/jsregexp.h" 9 #include "src/jsregexp.h"
10 #include "src/runtime/runtime-utils.h" 10 #include "src/runtime/runtime-utils.h"
(...skipping 741 matching lines...) Expand 10 before | Expand all | Expand 10 after
752 if (result->HasFastObjectElements()) { 752 if (result->HasFastObjectElements()) {
753 RegExpResultsCache::Enter(isolate, subject, pattern, elements, 753 RegExpResultsCache::Enter(isolate, subject, pattern, elements,
754 RegExpResultsCache::STRING_SPLIT_SUBSTRINGS); 754 RegExpResultsCache::STRING_SPLIT_SUBSTRINGS);
755 } 755 }
756 } 756 }
757 757
758 return *result; 758 return *result;
759 } 759 }
760 760
761 761
762 RUNTIME_FUNCTION(Runtime_RegExpCompile) {
763 HandleScope scope(isolate);
764 DCHECK(args.length() == 3);
765 CONVERT_ARG_HANDLE_CHECKED(JSRegExp, re, 0);
766 CONVERT_ARG_HANDLE_CHECKED(String, pattern, 1);
767 CONVERT_ARG_HANDLE_CHECKED(String, flags, 2);
768 Handle<Object> result;
769 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, result,
770 RegExpImpl::Compile(re, pattern, flags));
771 return *result;
772 }
773
774
775 RUNTIME_FUNCTION(Runtime_RegExpExecRT) { 762 RUNTIME_FUNCTION(Runtime_RegExpExecRT) {
776 HandleScope scope(isolate); 763 HandleScope scope(isolate);
777 DCHECK(args.length() == 4); 764 DCHECK(args.length() == 4);
778 CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 0); 765 CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 0);
779 CONVERT_ARG_HANDLE_CHECKED(String, subject, 1); 766 CONVERT_ARG_HANDLE_CHECKED(String, subject, 1);
780 CONVERT_INT32_ARG_CHECKED(index, 2); 767 CONVERT_INT32_ARG_CHECKED(index, 2);
781 CONVERT_ARG_HANDLE_CHECKED(JSArray, last_match_info, 3); 768 CONVERT_ARG_HANDLE_CHECKED(JSArray, last_match_info, 3);
782 // Due to the way the JS calls are constructed this must be less than the 769 // Due to the way the JS calls are constructed this must be less than the
783 // length of a string, i.e. it is always a Smi. We check anyway for security. 770 // length of a string, i.e. it is always a Smi. We check anyway for security.
784 RUNTIME_ASSERT(index >= 0); 771 RUNTIME_ASSERT(index >= 0);
(...skipping 21 matching lines...) Expand all
806 Handle<JSArray> array = Handle<JSArray>::cast(object); 793 Handle<JSArray> array = Handle<JSArray>::cast(object);
807 array->set_elements(*elements); 794 array->set_elements(*elements);
808 array->set_length(Smi::FromInt(size)); 795 array->set_length(Smi::FromInt(size));
809 // Write in-object properties after the length of the array. 796 // Write in-object properties after the length of the array.
810 array->InObjectPropertyAtPut(JSRegExpResult::kIndexIndex, *index); 797 array->InObjectPropertyAtPut(JSRegExpResult::kIndexIndex, *index);
811 array->InObjectPropertyAtPut(JSRegExpResult::kInputIndex, *input); 798 array->InObjectPropertyAtPut(JSRegExpResult::kInputIndex, *input);
812 return *array; 799 return *array;
813 } 800 }
814 801
815 802
816 RUNTIME_FUNCTION(Runtime_RegExpInitializeObject) { 803 static JSRegExp::Flags RegExpFlagsFromString(Handle<String> flags,
804 bool* success) {
805 uint32_t value = JSRegExp::NONE;
806 int length = flags->length();
807 // A longer flags string cannot be valid.
808 if (length > 4) return JSRegExp::Flags(0);
809 for (int i = 0; i < length; i++) {
810 uint32_t flag = JSRegExp::NONE;
811 switch (flags->Get(i)) {
812 case 'g':
813 flag = JSRegExp::GLOBAL;
814 break;
815 case 'i':
816 flag = JSRegExp::IGNORE_CASE;
817 break;
818 case 'm':
819 flag = JSRegExp::MULTILINE;
820 break;
821 case 'y':
822 if (!FLAG_harmony_regexps) return JSRegExp::Flags(0);
823 flag = JSRegExp::STICKY;
824 break;
825 default:
826 return JSRegExp::Flags(0);
827 }
828 // Duplicate flag.
829 if (value & flag) return JSRegExp::Flags(0);
830 value |= flag;
831 }
832 *success = true;
833 return JSRegExp::Flags(value);
834 }
835
836
837 RUNTIME_FUNCTION(Runtime_RegExpInitializeAndCompile) {
817 HandleScope scope(isolate); 838 HandleScope scope(isolate);
818 DCHECK(args.length() == 6); 839 DCHECK(args.length() == 3);
819 CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 0); 840 CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 0);
820 CONVERT_ARG_HANDLE_CHECKED(String, source, 1); 841 CONVERT_ARG_HANDLE_CHECKED(String, source, 1);
842 CONVERT_ARG_HANDLE_CHECKED(String, flags_string, 2);
843 Factory* factory = isolate->factory();
821 // If source is the empty string we set it to "(?:)" instead as 844 // If source is the empty string we set it to "(?:)" instead as
822 // suggested by ECMA-262, 5th, section 15.10.4.1. 845 // suggested by ECMA-262, 5th, section 15.10.4.1.
823 if (source->length() == 0) source = isolate->factory()->query_colon_string(); 846 if (source->length() == 0) source = factory->query_colon_string();
824 847
825 CONVERT_ARG_HANDLE_CHECKED(Object, global, 2); 848 bool success = false;
826 if (!global->IsTrue()) global = isolate->factory()->false_value(); 849 JSRegExp::Flags flags = RegExpFlagsFromString(flags_string, &success);
850 if (!success) {
851 Handle<FixedArray> element = factory->NewFixedArray(1);
852 element->set(0, *flags_string);
853 Handle<JSArray> args = factory->NewJSArrayWithElements(element);
854 THROW_NEW_ERROR_RETURN_FAILURE(
855 isolate, NewSyntaxError("invalid_regexp_flags", args));
856 }
827 857
828 CONVERT_ARG_HANDLE_CHECKED(Object, ignoreCase, 3); 858 Handle<Object> global = factory->ToBoolean(flags.is_global());
829 if (!ignoreCase->IsTrue()) ignoreCase = isolate->factory()->false_value(); 859 Handle<Object> ignore_case = factory->ToBoolean(flags.is_ignore_case());
830 860 Handle<Object> multiline = factory->ToBoolean(flags.is_multiline());
831 CONVERT_ARG_HANDLE_CHECKED(Object, multiline, 4); 861 Handle<Object> sticky = factory->ToBoolean(flags.is_sticky());
832 if (!multiline->IsTrue()) multiline = isolate->factory()->false_value();
833
834 CONVERT_ARG_HANDLE_CHECKED(Object, sticky, 5);
835 if (!sticky->IsTrue()) sticky = isolate->factory()->false_value();
836 862
837 Map* map = regexp->map(); 863 Map* map = regexp->map();
838 Object* constructor = map->constructor(); 864 Object* constructor = map->constructor();
839 if (!FLAG_harmony_regexps && constructor->IsJSFunction() && 865 if (!FLAG_harmony_regexps && constructor->IsJSFunction() &&
840 JSFunction::cast(constructor)->initial_map() == map) { 866 JSFunction::cast(constructor)->initial_map() == map) {
841 // If we still have the original map, set in-object properties directly. 867 // If we still have the original map, set in-object properties directly.
842 regexp->InObjectPropertyAtPut(JSRegExp::kSourceFieldIndex, *source); 868 regexp->InObjectPropertyAtPut(JSRegExp::kSourceFieldIndex, *source);
843 // Both true and false are immovable immortal objects so no need for write 869 // Both true and false are immovable immortal objects so no need for write
844 // barrier. 870 // barrier.
845 regexp->InObjectPropertyAtPut(JSRegExp::kGlobalFieldIndex, *global, 871 regexp->InObjectPropertyAtPut(JSRegExp::kGlobalFieldIndex, *global,
846 SKIP_WRITE_BARRIER); 872 SKIP_WRITE_BARRIER);
847 regexp->InObjectPropertyAtPut(JSRegExp::kIgnoreCaseFieldIndex, *ignoreCase, 873 regexp->InObjectPropertyAtPut(JSRegExp::kIgnoreCaseFieldIndex, *ignore_case,
848 SKIP_WRITE_BARRIER); 874 SKIP_WRITE_BARRIER);
849 regexp->InObjectPropertyAtPut(JSRegExp::kMultilineFieldIndex, *multiline, 875 regexp->InObjectPropertyAtPut(JSRegExp::kMultilineFieldIndex, *multiline,
850 SKIP_WRITE_BARRIER); 876 SKIP_WRITE_BARRIER);
851 regexp->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex, 877 regexp->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex,
852 Smi::FromInt(0), SKIP_WRITE_BARRIER); 878 Smi::FromInt(0), SKIP_WRITE_BARRIER);
853 return *regexp; 879 } else {
880 // Map has changed, so use generic, but slower, method. We also end here if
881 // the --harmony-regexp flag is set, because the initial map does not have
882 // space for the 'sticky' flag, since it is from the snapshot, but must work
883 // both with and without --harmony-regexp. When sticky comes out from under
884 // the flag, we will be able to use the fast initial map.
885 PropertyAttributes final =
886 static_cast<PropertyAttributes>(READ_ONLY | DONT_ENUM | DONT_DELETE);
887 PropertyAttributes writable =
888 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
889 Handle<Object> zero(Smi::FromInt(0), isolate);
890 JSObject::SetOwnPropertyIgnoreAttributes(regexp, factory->source_string(),
891 source, final).Check();
892 JSObject::SetOwnPropertyIgnoreAttributes(regexp, factory->global_string(),
893 global, final).Check();
894 JSObject::SetOwnPropertyIgnoreAttributes(
895 regexp, factory->ignore_case_string(), ignore_case, final).Check();
896 JSObject::SetOwnPropertyIgnoreAttributes(
897 regexp, factory->multiline_string(), multiline, final).Check();
898 if (FLAG_harmony_regexps) {
899 JSObject::SetOwnPropertyIgnoreAttributes(regexp, factory->sticky_string(),
900 sticky, final).Check();
901 }
902 JSObject::SetOwnPropertyIgnoreAttributes(
903 regexp, factory->last_index_string(), zero, writable).Check();
854 } 904 }
855 905
856 // Map has changed, so use generic, but slower, method. We also end here if 906 Handle<Object> result;
857 // the --harmony-regexp flag is set, because the initial map does not have 907 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
858 // space for the 'sticky' flag, since it is from the snapshot, but must work 908 isolate, result, RegExpImpl::Compile(regexp, source, flags));
859 // both with and without --harmony-regexp. When sticky comes out from under 909 return *result;
860 // the flag, we will be able to use the fast initial map.
861 PropertyAttributes final =
862 static_cast<PropertyAttributes>(READ_ONLY | DONT_ENUM | DONT_DELETE);
863 PropertyAttributes writable =
864 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
865 Handle<Object> zero(Smi::FromInt(0), isolate);
866 Factory* factory = isolate->factory();
867 JSObject::SetOwnPropertyIgnoreAttributes(regexp, factory->source_string(),
868 source, final).Check();
869 JSObject::SetOwnPropertyIgnoreAttributes(regexp, factory->global_string(),
870 global, final).Check();
871 JSObject::SetOwnPropertyIgnoreAttributes(
872 regexp, factory->ignore_case_string(), ignoreCase, final).Check();
873 JSObject::SetOwnPropertyIgnoreAttributes(regexp, factory->multiline_string(),
874 multiline, final).Check();
875 if (FLAG_harmony_regexps) {
876 JSObject::SetOwnPropertyIgnoreAttributes(regexp, factory->sticky_string(),
877 sticky, final).Check();
878 }
879 JSObject::SetOwnPropertyIgnoreAttributes(regexp, factory->last_index_string(),
880 zero, writable).Check();
881 return *regexp;
882 } 910 }
883 911
884 912
885 RUNTIME_FUNCTION(Runtime_MaterializeRegExpLiteral) { 913 RUNTIME_FUNCTION(Runtime_MaterializeRegExpLiteral) {
886 HandleScope scope(isolate); 914 HandleScope scope(isolate);
887 DCHECK(args.length() == 4); 915 DCHECK(args.length() == 4);
888 CONVERT_ARG_HANDLE_CHECKED(FixedArray, literals, 0); 916 CONVERT_ARG_HANDLE_CHECKED(FixedArray, literals, 0);
889 CONVERT_SMI_ARG_CHECKED(index, 1); 917 CONVERT_SMI_ARG_CHECKED(index, 1);
890 CONVERT_ARG_HANDLE_CHECKED(String, pattern, 2); 918 CONVERT_ARG_HANDLE_CHECKED(String, pattern, 2);
891 CONVERT_ARG_HANDLE_CHECKED(String, flags, 3); 919 CONVERT_ARG_HANDLE_CHECKED(String, flags, 3);
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
1088 1116
1089 1117
1090 RUNTIME_FUNCTION(RuntimeReference_IsRegExp) { 1118 RUNTIME_FUNCTION(RuntimeReference_IsRegExp) {
1091 SealHandleScope shs(isolate); 1119 SealHandleScope shs(isolate);
1092 DCHECK(args.length() == 1); 1120 DCHECK(args.length() == 1);
1093 CONVERT_ARG_CHECKED(Object, obj, 0); 1121 CONVERT_ARG_CHECKED(Object, obj, 0);
1094 return isolate->heap()->ToBoolean(obj->IsJSRegExp()); 1122 return isolate->heap()->ToBoolean(obj->IsJSRegExp());
1095 } 1123 }
1096 } 1124 }
1097 } // namespace v8::internal 1125 } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/runtime/runtime.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698