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

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: 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 JSRegExp::Flags RegExpFlagsFromString(Handle<String> flags, bool* success) {
aandrey 2014/11/19 13:16:54 static?
804 uint32_t value = JSRegExp::NONE;
805 int length = flags->length();
806 // A longer flags string cannot be valid.
807 if (length > 4) return JSRegExp::Flags(0);
808 for (int i = 0; i < flags->length(); i++) {
aandrey 2014/11/19 13:16:54 flags->length() -> length ?
809 uint32_t flag = JSRegExp::NONE;
810 switch (flags->Get(i)) {
811 case 'g':
812 flag = JSRegExp::GLOBAL;
813 break;
814 case 'i':
815 flag = JSRegExp::IGNORE_CASE;
816 break;
817 case 'm':
818 flag = JSRegExp::MULTILINE;
819 break;
820 case 'y':
821 if (!FLAG_harmony_regexps) return JSRegExp::Flags(0);
822 flag = JSRegExp::STICKY;
823 break;
824 default:
825 return JSRegExp::Flags(0);
826 }
827 // Duplicate flag.
828 if (value & flag) return JSRegExp::Flags(0);
829 value |= flag;
830 }
831 *success = true;
832 return JSRegExp::Flags(value);
833 }
834
835
836 RUNTIME_FUNCTION(Runtime_RegExpInitializeAndCompile) {
817 HandleScope scope(isolate); 837 HandleScope scope(isolate);
818 DCHECK(args.length() == 6); 838 DCHECK(args.length() == 3);
819 CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 0); 839 CONVERT_ARG_HANDLE_CHECKED(JSRegExp, regexp, 0);
820 CONVERT_ARG_HANDLE_CHECKED(String, source, 1); 840 CONVERT_ARG_HANDLE_CHECKED(String, source, 1);
841 CONVERT_ARG_HANDLE_CHECKED(String, flags_string, 2);
842 Factory* factory = isolate->factory();
821 // If source is the empty string we set it to "(?:)" instead as 843 // If source is the empty string we set it to "(?:)" instead as
822 // suggested by ECMA-262, 5th, section 15.10.4.1. 844 // suggested by ECMA-262, 5th, section 15.10.4.1.
823 if (source->length() == 0) source = isolate->factory()->query_colon_string(); 845 if (source->length() == 0) source = factory->query_colon_string();
824 846
825 CONVERT_ARG_HANDLE_CHECKED(Object, global, 2); 847 bool success = false;
826 if (!global->IsTrue()) global = isolate->factory()->false_value(); 848 JSRegExp::Flags flags = RegExpFlagsFromString(flags_string, &success);
849 if (!success) {
850 Handle<FixedArray> element = factory->NewFixedArray(1);
851 element->set(0, *flags_string);
852 Handle<JSArray> args = factory->NewJSArrayWithElements(element);
853 THROW_NEW_ERROR_RETURN_FAILURE(
854 isolate, NewSyntaxError("invalid_regexp_flags", args));
855 }
827 856
828 CONVERT_ARG_HANDLE_CHECKED(Object, ignoreCase, 3); 857 Handle<Object> global = factory->ToBoolean(flags.is_global());
829 if (!ignoreCase->IsTrue()) ignoreCase = isolate->factory()->false_value(); 858 Handle<Object> ignore_case = factory->ToBoolean(flags.is_ignore_case());
830 859 Handle<Object> multiline = factory->ToBoolean(flags.is_multiline());
831 CONVERT_ARG_HANDLE_CHECKED(Object, multiline, 4); 860 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 861
837 Map* map = regexp->map(); 862 Map* map = regexp->map();
838 Object* constructor = map->constructor(); 863 Object* constructor = map->constructor();
839 if (!FLAG_harmony_regexps && constructor->IsJSFunction() && 864 if (!FLAG_harmony_regexps && constructor->IsJSFunction() &&
840 JSFunction::cast(constructor)->initial_map() == map) { 865 JSFunction::cast(constructor)->initial_map() == map) {
841 // If we still have the original map, set in-object properties directly. 866 // If we still have the original map, set in-object properties directly.
842 regexp->InObjectPropertyAtPut(JSRegExp::kSourceFieldIndex, *source); 867 regexp->InObjectPropertyAtPut(JSRegExp::kSourceFieldIndex, *source);
843 // Both true and false are immovable immortal objects so no need for write 868 // Both true and false are immovable immortal objects so no need for write
844 // barrier. 869 // barrier.
845 regexp->InObjectPropertyAtPut(JSRegExp::kGlobalFieldIndex, *global, 870 regexp->InObjectPropertyAtPut(JSRegExp::kGlobalFieldIndex, *global,
846 SKIP_WRITE_BARRIER); 871 SKIP_WRITE_BARRIER);
847 regexp->InObjectPropertyAtPut(JSRegExp::kIgnoreCaseFieldIndex, *ignoreCase, 872 regexp->InObjectPropertyAtPut(JSRegExp::kIgnoreCaseFieldIndex, *ignore_case,
848 SKIP_WRITE_BARRIER); 873 SKIP_WRITE_BARRIER);
849 regexp->InObjectPropertyAtPut(JSRegExp::kMultilineFieldIndex, *multiline, 874 regexp->InObjectPropertyAtPut(JSRegExp::kMultilineFieldIndex, *multiline,
850 SKIP_WRITE_BARRIER); 875 SKIP_WRITE_BARRIER);
851 regexp->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex, 876 regexp->InObjectPropertyAtPut(JSRegExp::kLastIndexFieldIndex,
852 Smi::FromInt(0), SKIP_WRITE_BARRIER); 877 Smi::FromInt(0), SKIP_WRITE_BARRIER);
853 return *regexp; 878 } else {
879 // Map has changed, so use generic, but slower, method. We also end here if
880 // the --harmony-regexp flag is set, because the initial map does not have
881 // space for the 'sticky' flag, since it is from the snapshot, but must work
882 // both with and without --harmony-regexp. When sticky comes out from under
883 // the flag, we will be able to use the fast initial map.
884 PropertyAttributes final =
885 static_cast<PropertyAttributes>(READ_ONLY | DONT_ENUM | DONT_DELETE);
886 PropertyAttributes writable =
887 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE);
888 Handle<Object> zero(Smi::FromInt(0), isolate);
889 JSObject::SetOwnPropertyIgnoreAttributes(regexp, factory->source_string(),
890 source, final).Check();
891 JSObject::SetOwnPropertyIgnoreAttributes(regexp, factory->global_string(),
892 global, final).Check();
893 JSObject::SetOwnPropertyIgnoreAttributes(
894 regexp, factory->ignore_case_string(), ignore_case, final).Check();
895 JSObject::SetOwnPropertyIgnoreAttributes(
896 regexp, factory->multiline_string(), multiline, final).Check();
897 if (FLAG_harmony_regexps) {
898 JSObject::SetOwnPropertyIgnoreAttributes(regexp, factory->sticky_string(),
899 sticky, final).Check();
900 }
901 JSObject::SetOwnPropertyIgnoreAttributes(
902 regexp, factory->last_index_string(), zero, writable).Check();
854 } 903 }
855 904
856 // Map has changed, so use generic, but slower, method. We also end here if 905 Handle<Object> result;
857 // the --harmony-regexp flag is set, because the initial map does not have 906 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
858 // space for the 'sticky' flag, since it is from the snapshot, but must work 907 isolate, result, RegExpImpl::Compile(regexp, source, flags));
859 // both with and without --harmony-regexp. When sticky comes out from under 908 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 } 909 }
883 910
884 911
885 RUNTIME_FUNCTION(Runtime_MaterializeRegExpLiteral) { 912 RUNTIME_FUNCTION(Runtime_MaterializeRegExpLiteral) {
886 HandleScope scope(isolate); 913 HandleScope scope(isolate);
887 DCHECK(args.length() == 4); 914 DCHECK(args.length() == 4);
888 CONVERT_ARG_HANDLE_CHECKED(FixedArray, literals, 0); 915 CONVERT_ARG_HANDLE_CHECKED(FixedArray, literals, 0);
889 CONVERT_SMI_ARG_CHECKED(index, 1); 916 CONVERT_SMI_ARG_CHECKED(index, 1);
890 CONVERT_ARG_HANDLE_CHECKED(String, pattern, 2); 917 CONVERT_ARG_HANDLE_CHECKED(String, pattern, 2);
891 CONVERT_ARG_HANDLE_CHECKED(String, flags, 3); 918 CONVERT_ARG_HANDLE_CHECKED(String, flags, 3);
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
1088 1115
1089 1116
1090 RUNTIME_FUNCTION(RuntimeReference_IsRegExp) { 1117 RUNTIME_FUNCTION(RuntimeReference_IsRegExp) {
1091 SealHandleScope shs(isolate); 1118 SealHandleScope shs(isolate);
1092 DCHECK(args.length() == 1); 1119 DCHECK(args.length() == 1);
1093 CONVERT_ARG_CHECKED(Object, obj, 0); 1120 CONVERT_ARG_CHECKED(Object, obj, 0);
1094 return isolate->heap()->ToBoolean(obj->IsJSRegExp()); 1121 return isolate->heap()->ToBoolean(obj->IsJSRegExp());
1095 } 1122 }
1096 } 1123 }
1097 } // namespace v8::internal 1124 } // 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