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/builtins/builtins-utils.h" | 5 #include "src/builtins/builtins-utils.h" |
6 #include "src/builtins/builtins.h" | 6 #include "src/builtins/builtins.h" |
7 | 7 |
8 #include "src/code-factory.h" | 8 #include "src/code-factory.h" |
9 #include "src/regexp/jsregexp.h" | 9 #include "src/regexp/jsregexp.h" |
10 | 10 |
11 namespace v8 { | 11 namespace v8 { |
12 namespace internal { | 12 namespace internal { |
13 | 13 |
14 // ----------------------------------------------------------------------------- | 14 // ----------------------------------------------------------------------------- |
15 // ES6 section 21.2 RegExp Objects | 15 // ES6 section 21.2 RegExp Objects |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 // ES#sec-isregexp IsRegExp ( argument ) | |
20 Maybe<bool> IsRegExp(Isolate* isolate, Handle<Object> object) { | |
21 if (!object->IsJSReceiver()) return Just(false); | |
22 | |
23 Handle<JSReceiver> receiver = Handle<JSReceiver>::cast(object); | |
24 | |
25 if (isolate->regexp_function()->initial_map() == receiver->map()) { | |
26 // Fast-path for unmodified JSRegExp instances. | |
27 return Just(true); | |
28 } | |
29 | |
30 Handle<Object> match; | |
31 ASSIGN_RETURN_ON_EXCEPTION_VALUE( | |
32 isolate, match, | |
33 JSObject::GetProperty(receiver, isolate->factory()->match_symbol()), | |
34 Nothing<bool>()); | |
35 | |
36 if (!match->IsUndefined(isolate)) return Just(match->BooleanValue()); | |
37 return Just(object->IsJSRegExp()); | |
38 } | |
39 | |
40 Handle<String> PatternFlags(Isolate* isolate, Handle<JSRegExp> regexp) { | 19 Handle<String> PatternFlags(Isolate* isolate, Handle<JSRegExp> regexp) { |
41 static const int kMaxFlagsLength = 5 + 1; // 5 flags and '\0'; | 20 static const int kMaxFlagsLength = 5 + 1; // 5 flags and '\0'; |
42 char flags_string[kMaxFlagsLength]; | 21 char flags_string[kMaxFlagsLength]; |
43 int i = 0; | 22 int i = 0; |
44 | 23 |
45 const JSRegExp::Flags flags = regexp->GetFlags(); | 24 const JSRegExp::Flags flags = regexp->GetFlags(); |
46 | 25 |
47 if ((flags & JSRegExp::kGlobal) != 0) flags_string[i++] = 'g'; | 26 if ((flags & JSRegExp::kGlobal) != 0) flags_string[i++] = 'g'; |
48 if ((flags & JSRegExp::kIgnoreCase) != 0) flags_string[i++] = 'i'; | 27 if ((flags & JSRegExp::kIgnoreCase) != 0) flags_string[i++] = 'i'; |
49 if ((flags & JSRegExp::kMultiline) != 0) flags_string[i++] = 'm'; | 28 if ((flags & JSRegExp::kMultiline) != 0) flags_string[i++] = 'm'; |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 HandleScope scope(isolate); | 71 HandleScope scope(isolate); |
93 | 72 |
94 Handle<HeapObject> new_target = args.new_target(); | 73 Handle<HeapObject> new_target = args.new_target(); |
95 Handle<Object> pattern = args.atOrUndefined(isolate, 1); | 74 Handle<Object> pattern = args.atOrUndefined(isolate, 1); |
96 Handle<Object> flags = args.atOrUndefined(isolate, 2); | 75 Handle<Object> flags = args.atOrUndefined(isolate, 2); |
97 | 76 |
98 Handle<JSFunction> target = isolate->regexp_function(); | 77 Handle<JSFunction> target = isolate->regexp_function(); |
99 | 78 |
100 bool pattern_is_regexp; | 79 bool pattern_is_regexp; |
101 { | 80 { |
102 Maybe<bool> maybe_pattern_is_regexp = IsRegExp(isolate, pattern); | 81 Maybe<bool> maybe_pattern_is_regexp = Object::IsRegExp(isolate, pattern); |
103 if (maybe_pattern_is_regexp.IsNothing()) { | 82 if (maybe_pattern_is_regexp.IsNothing()) { |
104 DCHECK(isolate->has_pending_exception()); | 83 DCHECK(isolate->has_pending_exception()); |
105 return isolate->heap()->exception(); | 84 return isolate->heap()->exception(); |
106 } | 85 } |
107 pattern_is_regexp = maybe_pattern_is_regexp.FromJust(); | 86 pattern_is_regexp = maybe_pattern_is_regexp.FromJust(); |
108 } | 87 } |
109 | 88 |
110 if (new_target->IsUndefined(isolate)) { | 89 if (new_target->IsUndefined(isolate)) { |
111 new_target = target; | 90 new_target = target; |
112 | 91 |
(...skipping 790 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
903 BUILTIN(RegExpRightContextGetter) { | 882 BUILTIN(RegExpRightContextGetter) { |
904 HandleScope scope(isolate); | 883 HandleScope scope(isolate); |
905 const int start_index = GetLastMatchCapture(isolate, 1); | 884 const int start_index = GetLastMatchCapture(isolate, 1); |
906 Handle<String> last_subject = GetLastMatchSubject(isolate); | 885 Handle<String> last_subject = GetLastMatchSubject(isolate); |
907 const int len = last_subject->length(); | 886 const int len = last_subject->length(); |
908 return *isolate->factory()->NewSubString(last_subject, start_index, len); | 887 return *isolate->factory()->NewSubString(last_subject, start_index, len); |
909 } | 888 } |
910 | 889 |
911 } // namespace internal | 890 } // namespace internal |
912 } // namespace v8 | 891 } // namespace v8 |
OLD | NEW |