OLD | NEW |
1 // Copyright 2016 the V8 project authors. All rights reserved. | 1 // Copyright 2016 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/regexp-utils.h" | 5 #include "src/regexp/regexp-utils.h" |
6 | 6 |
7 #include "src/factory.h" | 7 #include "src/factory.h" |
8 #include "src/isolate.h" | 8 #include "src/isolate.h" |
9 #include "src/objects-inl.h" | 9 #include "src/objects-inl.h" |
10 #include "src/regexp/jsregexp.h" | 10 #include "src/regexp/jsregexp.h" |
11 | 11 |
12 namespace v8 { | 12 namespace v8 { |
13 namespace internal { | 13 namespace internal { |
14 | 14 |
15 // Constants for accessing RegExpLastMatchInfo. | 15 Handle<String> RegExpUtils::GenericCaptureGetter( |
16 // TODO(jgruber): Currently, RegExpLastMatchInfo is still a JSObject maintained | 16 Isolate* isolate, Handle<RegExpMatchInfo> match_info, int capture, |
17 // and accessed from JS. This is a crutch until all RegExp logic is ported, then | 17 bool* ok) { |
18 // we can take care of RegExpLastMatchInfo. | |
19 | |
20 Handle<Object> RegExpUtils::GetLastMatchField(Isolate* isolate, | |
21 Handle<JSObject> match_info, | |
22 int index) { | |
23 return JSReceiver::GetElement(isolate, match_info, index).ToHandleChecked(); | |
24 } | |
25 | |
26 void RegExpUtils::SetLastMatchField(Isolate* isolate, | |
27 Handle<JSObject> match_info, int index, | |
28 Handle<Object> value) { | |
29 JSReceiver::SetElement(isolate, match_info, index, value, SLOPPY) | |
30 .ToHandleChecked(); | |
31 } | |
32 | |
33 int RegExpUtils::GetLastMatchNumberOfCaptures(Isolate* isolate, | |
34 Handle<JSObject> match_info) { | |
35 Handle<Object> obj = | |
36 GetLastMatchField(isolate, match_info, RegExpImpl::kLastCaptureCount); | |
37 return Handle<Smi>::cast(obj)->value(); | |
38 } | |
39 | |
40 Handle<String> RegExpUtils::GetLastMatchSubject(Isolate* isolate, | |
41 Handle<JSObject> match_info) { | |
42 return Handle<String>::cast( | |
43 GetLastMatchField(isolate, match_info, RegExpImpl::kLastSubject)); | |
44 } | |
45 | |
46 Handle<Object> RegExpUtils::GetLastMatchInput(Isolate* isolate, | |
47 Handle<JSObject> match_info) { | |
48 return GetLastMatchField(isolate, match_info, RegExpImpl::kLastInput); | |
49 } | |
50 | |
51 int RegExpUtils::GetLastMatchCapture(Isolate* isolate, | |
52 Handle<JSObject> match_info, int i) { | |
53 Handle<Object> obj = | |
54 GetLastMatchField(isolate, match_info, RegExpImpl::kFirstCapture + i); | |
55 return Handle<Smi>::cast(obj)->value(); | |
56 } | |
57 | |
58 Handle<String> RegExpUtils::GenericCaptureGetter(Isolate* isolate, | |
59 Handle<JSObject> match_info, | |
60 int capture, bool* ok) { | |
61 const int index = capture * 2; | 18 const int index = capture * 2; |
62 if (index >= GetLastMatchNumberOfCaptures(isolate, match_info)) { | 19 if (index >= match_info->NumberOfCaptureRegisters()) { |
63 if (ok != nullptr) *ok = false; | 20 if (ok != nullptr) *ok = false; |
64 return isolate->factory()->empty_string(); | 21 return isolate->factory()->empty_string(); |
65 } | 22 } |
66 | 23 |
67 const int match_start = GetLastMatchCapture(isolate, match_info, index); | 24 const int match_start = match_info->Capture(index); |
68 const int match_end = GetLastMatchCapture(isolate, match_info, index + 1); | 25 const int match_end = match_info->Capture(index + 1); |
69 if (match_start == -1 || match_end == -1) { | 26 if (match_start == -1 || match_end == -1) { |
70 if (ok != nullptr) *ok = false; | 27 if (ok != nullptr) *ok = false; |
71 return isolate->factory()->empty_string(); | 28 return isolate->factory()->empty_string(); |
72 } | 29 } |
73 | 30 |
74 if (ok != nullptr) *ok = true; | 31 if (ok != nullptr) *ok = true; |
75 Handle<String> last_subject = GetLastMatchSubject(isolate, match_info); | 32 Handle<String> last_subject(match_info->LastSubject()); |
76 return isolate->factory()->NewSubString(last_subject, match_start, match_end); | 33 return isolate->factory()->NewSubString(last_subject, match_start, match_end); |
77 } | 34 } |
78 | 35 |
79 namespace { | 36 namespace { |
80 | 37 |
81 V8_INLINE bool HasInitialRegExpMap(Isolate* isolate, Handle<JSReceiver> recv) { | 38 V8_INLINE bool HasInitialRegExpMap(Isolate* isolate, Handle<JSReceiver> recv) { |
82 return recv->map() == isolate->regexp_function()->initial_map(); | 39 return recv->map() == isolate->regexp_function()->initial_map(); |
83 } | 40 } |
84 | 41 |
85 } // namespace | 42 } // namespace |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
221 | 178 |
222 const int last_index = Handle<Smi>::cast(last_index_obj)->value(); | 179 const int last_index = Handle<Smi>::cast(last_index_obj)->value(); |
223 const int new_last_index = | 180 const int new_last_index = |
224 last_index + AdvanceStringIndex(isolate, string, last_index, unicode); | 181 last_index + AdvanceStringIndex(isolate, string, last_index, unicode); |
225 | 182 |
226 return SetLastIndex(isolate, regexp, new_last_index); | 183 return SetLastIndex(isolate, regexp, new_last_index); |
227 } | 184 } |
228 | 185 |
229 } // namespace internal | 186 } // namespace internal |
230 } // namespace v8 | 187 } // namespace v8 |
OLD | NEW |