OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/assert.h" | 5 #include "platform/assert.h" |
6 #include "vm/bootstrap_natives.h" | 6 #include "vm/bootstrap_natives.h" |
7 #include "vm/exceptions.h" | 7 #include "vm/exceptions.h" |
8 #include "vm/native_entry.h" | 8 #include "vm/native_entry.h" |
9 #include "vm/object.h" | 9 #include "vm/object.h" |
10 #include "vm/regexp_parser.h" | 10 #include "vm/regexp_parser.h" |
11 | 11 |
12 #include "lib/regexp_jsc.h" | 12 #include "lib/regexp_jsc.h" |
13 | 13 |
14 namespace dart { | 14 namespace dart { |
15 | 15 |
16 DECLARE_FLAG(bool, trace_irregexp); | |
17 DEFINE_FLAG(bool, use_jscre, false, | |
18 "Use JSCRE instead of the irregexp engine."); | |
19 | |
16 DEFINE_NATIVE_ENTRY(JSSyntaxRegExp_factory, 4) { | 20 DEFINE_NATIVE_ENTRY(JSSyntaxRegExp_factory, 4) { |
17 ASSERT(TypeArguments::CheckedHandle(arguments->NativeArgAt(0)).IsNull()); | 21 ASSERT(TypeArguments::CheckedHandle(arguments->NativeArgAt(0)).IsNull()); |
18 GET_NON_NULL_NATIVE_ARGUMENT(String, pattern, arguments->NativeArgAt(1)); | 22 GET_NON_NULL_NATIVE_ARGUMENT(String, pattern, arguments->NativeArgAt(1)); |
19 GET_NON_NULL_NATIVE_ARGUMENT( | 23 GET_NON_NULL_NATIVE_ARGUMENT( |
20 Instance, handle_multi_line, arguments->NativeArgAt(2)); | 24 Instance, handle_multi_line, arguments->NativeArgAt(2)); |
21 GET_NON_NULL_NATIVE_ARGUMENT( | 25 GET_NON_NULL_NATIVE_ARGUMENT( |
22 Instance, handle_case_sensitive, arguments->NativeArgAt(3)); | 26 Instance, handle_case_sensitive, arguments->NativeArgAt(3)); |
23 bool ignore_case = handle_case_sensitive.raw() != Bool::True().raw(); | 27 bool ignore_case = handle_case_sensitive.raw() != Bool::True().raw(); |
24 bool multi_line = handle_multi_line.raw() == Bool::True().raw(); | 28 bool multi_line = handle_multi_line.raw() == Bool::True().raw(); |
25 return Jscre::Compile(pattern, multi_line, ignore_case); | 29 |
30 if (FLAG_use_jscre) { | |
31 return Jscre::Compile(pattern, multi_line, ignore_case); | |
32 } else { | |
33 // Parse the pattern once in order to throw any format exceptions within | |
34 // the factory constructor. It is parsed again upon compilation. | |
35 RegExpCompileData compileData; | |
36 if (!RegExpParser::ParseRegExp(pattern, multi_line, &compileData)) { | |
37 // Parsing failures throw an exception. | |
38 UNREACHABLE(); | |
39 } | |
40 | |
41 // Create a JSRegExp object containing only the initial parameters. | |
42 // Compilation is done upon regexp execution. | |
43 return RegExpEngine::New(pattern, multi_line, ignore_case); | |
44 } | |
26 } | 45 } |
27 | 46 |
28 | 47 |
29 DEFINE_NATIVE_ENTRY(JSSyntaxRegExp_getPattern, 1) { | 48 DEFINE_NATIVE_ENTRY(JSSyntaxRegExp_getPattern, 1) { |
30 const JSRegExp& regexp = JSRegExp::CheckedHandle(arguments->NativeArgAt(0)); | 49 const JSRegExp& regexp = JSRegExp::CheckedHandle(arguments->NativeArgAt(0)); |
31 ASSERT(!regexp.IsNull()); | 50 ASSERT(!regexp.IsNull()); |
32 return regexp.pattern(); | 51 return regexp.pattern(); |
33 } | 52 } |
34 | 53 |
35 | 54 |
(...skipping 22 matching lines...) Expand all Loading... | |
58 String::New("Regular expression is not initialized yet. ")); | 77 String::New("Regular expression is not initialized yet. ")); |
59 const String& message = String::Handle(String::Concat(errmsg, pattern)); | 78 const String& message = String::Handle(String::Concat(errmsg, pattern)); |
60 const Array& args = Array::Handle(Array::New(1)); | 79 const Array& args = Array::Handle(Array::New(1)); |
61 args.SetAt(0, message); | 80 args.SetAt(0, message); |
62 Exceptions::ThrowByType(Exceptions::kFormat, args); | 81 Exceptions::ThrowByType(Exceptions::kFormat, args); |
63 return Object::null(); | 82 return Object::null(); |
64 } | 83 } |
65 | 84 |
66 | 85 |
67 DEFINE_NATIVE_ENTRY(JSSyntaxRegExp_ExecuteMatch, 3) { | 86 DEFINE_NATIVE_ENTRY(JSSyntaxRegExp_ExecuteMatch, 3) { |
68 const JSRegExp& regexp = JSRegExp::CheckedHandle(arguments->NativeArgAt(0)); | 87 JSRegExp& regexp = JSRegExp::CheckedHandle(arguments->NativeArgAt(0)); |
69 ASSERT(!regexp.IsNull()); | 88 ASSERT(!regexp.IsNull()); |
70 GET_NON_NULL_NATIVE_ARGUMENT(String, str, arguments->NativeArgAt(1)); | 89 GET_NON_NULL_NATIVE_ARGUMENT(String, str, arguments->NativeArgAt(1)); |
71 GET_NON_NULL_NATIVE_ARGUMENT(Smi, start_index, arguments->NativeArgAt(2)); | 90 GET_NON_NULL_NATIVE_ARGUMENT(Smi, start_index, arguments->NativeArgAt(2)); |
72 | 91 |
73 // The irregexp parser runs alongside the jscre parser while the port is | 92 if (FLAG_use_jscre) { |
74 // still in progress. When done, it will replace jscre completely. | 93 return Jscre::Execute(regexp, str, start_index.Value()); |
75 const String& pattern = String::Handle(regexp.pattern()); | 94 } else { |
76 RegExpCompileData compileData; | 95 const bool is_one_byte_string = str.IsOneByteString(); |
Florian Schneider
2014/09/16 11:11:48
Could be also ExternalOneByteString.
jgruber1
2014/09/22 18:58:03
The generated code is now specialized for all four
| |
77 if (RegExpParser::ParseRegExp(pattern, regexp.is_multi_line(), | 96 |
78 &compileData)) { | 97 // Retrieve the cached function. |
79 RegExpEngine::Compile(&compileData, | 98 Function& fn = Function::Handle(is_one_byte_string ? |
80 regexp.is_ignore_case(), | 99 regexp.one_byte_function() : regexp.two_byte_function()); |
81 regexp.is_global(), // A Dart regexp is always global | 100 |
82 regexp.is_multi_line(), | 101 // Compile if needed. |
83 pattern, | 102 if (fn.IsNull()) { |
84 str, | 103 // Parse the pattern. |
85 str.IsOneByteString()); | 104 const String& pattern = String::Handle(regexp.pattern()); |
86 RegExpEngine::DotPrint(pattern.ToCString(), | 105 RegExpCompileData compileData; |
87 compileData.node, | 106 if (!RegExpParser::ParseRegExp(pattern, regexp.is_multi_line(), |
88 regexp.is_ignore_case()); | 107 &compileData)) { |
108 // Parsing failures are handled in the factory constructor. | |
109 UNREACHABLE(); | |
110 } | |
111 | |
112 // Compile it. | |
113 RegExpEngine::CompilationResult result = | |
114 RegExpEngine::Compile( | |
115 &compileData, | |
116 regexp.is_ignore_case(), | |
117 regexp.is_global(), // A Dart regexp is always global | |
118 regexp.is_multi_line(), | |
119 pattern, | |
120 str, | |
121 str.IsOneByteString()); | |
122 if (result.function.IsNull()) { | |
123 // Should never fail. | |
124 UNREACHABLE(); | |
125 } | |
126 | |
127 // Debug output of the generated state machine. | |
128 if (FLAG_trace_irregexp) { | |
129 #ifdef DEBUG | |
130 RegExpEngine::DotPrint(pattern.ToCString(), | |
131 compileData.node, | |
132 regexp.is_ignore_case()); | |
133 #endif | |
134 } | |
135 | |
136 // Cache the result. | |
137 if (is_one_byte_string) { | |
138 regexp.set_one_byte_function(result.function); | |
139 } else { | |
140 regexp.set_two_byte_function(result.function); | |
141 } | |
142 regexp.set_num_bracket_expressions(compileData.capture_count); | |
143 | |
144 fn = result.function.raw(); | |
145 } | |
146 | |
147 ASSERT(!fn.IsNull()); | |
148 | |
149 // Create the output array. | |
Florian Schneider
2014/09/16 11:11:47
Creating the output array should be part of the ge
jgruber1
2014/09/22 18:58:03
Done.
| |
150 const Smi& num_bracket_exprs = | |
151 Smi::Handle(regexp.num_bracket_expressions()); | |
152 intptr_t num_bracket_expressions = num_bracket_exprs.Value(); | |
153 const intptr_t kMatchPair = 2; | |
154 Array& matches = Array::Handle( | |
155 Array::New(kMatchPair * (num_bracket_expressions + 1))); | |
156 | |
157 // And finally call the generated code. | |
158 IRRegExpMacroAssembler::Result result = | |
159 IRRegExpMacroAssembler::Execute(fn, str, start_index, | |
160 &matches, isolate); | |
161 | |
162 if (result == IRRegExpMacroAssembler::SUCCESS) { | |
163 return matches.raw(); | |
164 } else { | |
165 return Array::null(); | |
166 } | |
89 } | 167 } |
90 | 168 |
91 return Jscre::Execute(regexp, str, start_index.Value()); | 169 UNREACHABLE(); |
170 return Array::null(); | |
92 } | 171 } |
93 | 172 |
94 } // namespace dart | 173 } // namespace dart |
OLD | NEW |