OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 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/regexp/jsregexp.h" | 5 #include "src/regexp/jsregexp.h" |
6 | 6 |
7 #include "src/ast.h" | 7 #include "src/ast.h" |
8 #include "src/base/platform/platform.h" | 8 #include "src/base/platform/platform.h" |
9 #include "src/compilation-cache.h" | 9 #include "src/compilation-cache.h" |
10 #include "src/compiler.h" | 10 #include "src/compiler.h" |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 Handle<Object> result; | 139 Handle<Object> result; |
140 if (in_cache) { | 140 if (in_cache) { |
141 re->set_data(*cached); | 141 re->set_data(*cached); |
142 return re; | 142 return re; |
143 } | 143 } |
144 pattern = String::Flatten(pattern); | 144 pattern = String::Flatten(pattern); |
145 PostponeInterruptsScope postpone(isolate); | 145 PostponeInterruptsScope postpone(isolate); |
146 RegExpCompileData parse_result; | 146 RegExpCompileData parse_result; |
147 FlatStringReader reader(isolate, pattern); | 147 FlatStringReader reader(isolate, pattern); |
148 if (!RegExpParser::ParseRegExp(re->GetIsolate(), &zone, &reader, | 148 if (!RegExpParser::ParseRegExp(re->GetIsolate(), &zone, &reader, |
149 flags.is_multiline(), flags.is_unicode(), | 149 flags & JSRegExp::kMultiline, |
150 &parse_result)) { | 150 flags & JSRegExp::kUnicode, &parse_result)) { |
151 // Throw an exception if we fail to parse the pattern. | 151 // Throw an exception if we fail to parse the pattern. |
152 return ThrowRegExpException(re, pattern, parse_result.error); | 152 return ThrowRegExpException(re, pattern, parse_result.error); |
153 } | 153 } |
154 | 154 |
155 bool has_been_compiled = false; | 155 bool has_been_compiled = false; |
156 | 156 |
157 if (parse_result.simple && | 157 if (parse_result.simple && !(flags & JSRegExp::kIgnoreCase) && |
158 !flags.is_ignore_case() && | 158 !(flags & JSRegExp::kSticky) && !HasFewDifferentCharacters(pattern)) { |
159 !flags.is_sticky() && | |
160 !HasFewDifferentCharacters(pattern)) { | |
161 // Parse-tree is a single atom that is equal to the pattern. | 159 // Parse-tree is a single atom that is equal to the pattern. |
162 AtomCompile(re, pattern, flags, pattern); | 160 AtomCompile(re, pattern, flags, pattern); |
163 has_been_compiled = true; | 161 has_been_compiled = true; |
164 } else if (parse_result.tree->IsAtom() && | 162 } else if (parse_result.tree->IsAtom() && !(flags & JSRegExp::kIgnoreCase) && |
165 !flags.is_ignore_case() && | 163 !(flags & JSRegExp::kSticky) && parse_result.capture_count == 0) { |
166 !flags.is_sticky() && | |
167 parse_result.capture_count == 0) { | |
168 RegExpAtom* atom = parse_result.tree->AsAtom(); | 164 RegExpAtom* atom = parse_result.tree->AsAtom(); |
169 Vector<const uc16> atom_pattern = atom->data(); | 165 Vector<const uc16> atom_pattern = atom->data(); |
170 Handle<String> atom_string; | 166 Handle<String> atom_string; |
171 ASSIGN_RETURN_ON_EXCEPTION( | 167 ASSIGN_RETURN_ON_EXCEPTION( |
172 isolate, atom_string, | 168 isolate, atom_string, |
173 isolate->factory()->NewStringFromTwoByte(atom_pattern), | 169 isolate->factory()->NewStringFromTwoByte(atom_pattern), |
174 Object); | 170 Object); |
175 if (!HasFewDifferentCharacters(atom_string)) { | 171 if (!HasFewDifferentCharacters(atom_string)) { |
176 AtomCompile(re, pattern, flags, atom_string); | 172 AtomCompile(re, pattern, flags, atom_string); |
177 has_been_compiled = true; | 173 has_been_compiled = true; |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
368 ThrowRegExpException(re, error_message); | 364 ThrowRegExpException(re, error_message); |
369 return false; | 365 return false; |
370 } | 366 } |
371 | 367 |
372 JSRegExp::Flags flags = re->GetFlags(); | 368 JSRegExp::Flags flags = re->GetFlags(); |
373 | 369 |
374 Handle<String> pattern(re->Pattern()); | 370 Handle<String> pattern(re->Pattern()); |
375 pattern = String::Flatten(pattern); | 371 pattern = String::Flatten(pattern); |
376 RegExpCompileData compile_data; | 372 RegExpCompileData compile_data; |
377 FlatStringReader reader(isolate, pattern); | 373 FlatStringReader reader(isolate, pattern); |
378 if (!RegExpParser::ParseRegExp(isolate, &zone, &reader, flags.is_multiline(), | 374 if (!RegExpParser::ParseRegExp(isolate, &zone, &reader, |
379 flags.is_unicode(), &compile_data)) { | 375 flags & JSRegExp::kMultiline, |
| 376 flags & JSRegExp::kUnicode, &compile_data)) { |
380 // Throw an exception if we fail to parse the pattern. | 377 // Throw an exception if we fail to parse the pattern. |
381 // THIS SHOULD NOT HAPPEN. We already pre-parsed it successfully once. | 378 // THIS SHOULD NOT HAPPEN. We already pre-parsed it successfully once. |
382 USE(ThrowRegExpException(re, pattern, compile_data.error)); | 379 USE(ThrowRegExpException(re, pattern, compile_data.error)); |
383 return false; | 380 return false; |
384 } | 381 } |
385 RegExpEngine::CompilationResult result = RegExpEngine::Compile( | 382 RegExpEngine::CompilationResult result = RegExpEngine::Compile( |
386 isolate, &zone, &compile_data, flags.is_ignore_case(), flags.is_global(), | 383 isolate, &zone, &compile_data, flags & JSRegExp::kIgnoreCase, |
387 flags.is_multiline(), flags.is_sticky(), pattern, sample_subject, | 384 flags & JSRegExp::kGlobal, flags & JSRegExp::kMultiline, |
388 is_one_byte); | 385 flags & JSRegExp::kSticky, pattern, sample_subject, is_one_byte); |
389 if (result.error_message != NULL) { | 386 if (result.error_message != NULL) { |
390 // Unable to compile regexp. | 387 // Unable to compile regexp. |
391 Handle<String> error_message = isolate->factory()->NewStringFromUtf8( | 388 Handle<String> error_message = isolate->factory()->NewStringFromUtf8( |
392 CStrVector(result.error_message)).ToHandleChecked(); | 389 CStrVector(result.error_message)).ToHandleChecked(); |
393 ThrowRegExpException(re, error_message); | 390 ThrowRegExpException(re, error_message); |
394 return false; | 391 return false; |
395 } | 392 } |
396 | 393 |
397 Handle<FixedArray> data = Handle<FixedArray>(FixedArray::cast(re->data())); | 394 Handle<FixedArray> data = Handle<FixedArray>(FixedArray::cast(re->data())); |
398 data->set(JSRegExp::code_index(is_one_byte), result.code); | 395 data->set(JSRegExp::code_index(is_one_byte), result.code); |
(...skipping 6103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
6502 | 6499 |
6503 | 6500 |
6504 void RegExpResultsCache::Clear(FixedArray* cache) { | 6501 void RegExpResultsCache::Clear(FixedArray* cache) { |
6505 for (int i = 0; i < kRegExpResultsCacheSize; i++) { | 6502 for (int i = 0; i < kRegExpResultsCacheSize; i++) { |
6506 cache->set(i, Smi::FromInt(0)); | 6503 cache->set(i, Smi::FromInt(0)); |
6507 } | 6504 } |
6508 } | 6505 } |
6509 | 6506 |
6510 } // namespace internal | 6507 } // namespace internal |
6511 } // namespace v8 | 6508 } // namespace v8 |
OLD | NEW |