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

Unified Diff: src/builtins/builtins-regexp.cc

Issue 2302773002: [regexp] Port RegExpConstructor (Closed)
Patch Set: Tweaks Created 4 years, 3 months 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/builtins/builtins.h ('k') | src/heap-symbols.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/builtins/builtins-regexp.cc
diff --git a/src/builtins/builtins-regexp.cc b/src/builtins/builtins-regexp.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f1e458860c7e96487ad9e79cf19ff0785d820e48
--- /dev/null
+++ b/src/builtins/builtins-regexp.cc
@@ -0,0 +1,152 @@
+// Copyright 2016 the V8 project authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "src/builtins/builtins.h"
+#include "src/builtins/builtins-utils.h"
+
+#include "src/string-builder.h"
+
+namespace v8 {
+namespace internal {
+
+// -----------------------------------------------------------------------------
+// ES6 section 21.2 RegExp Objects
+
+namespace {
+
+// ES#sec-isregexp IsRegExp ( argument )
+Maybe<bool> IsRegExp(Isolate* isolate, Handle<Object> object) {
+ if (!object->IsJSReceiver()) return Just(false);
+
+ Handle<JSReceiver> receiver = Handle<JSReceiver>::cast(object);
+
+ Handle<Object> match;
+ ASSIGN_RETURN_ON_EXCEPTION_VALUE(
+ isolate, match,
+ JSObject::GetProperty(receiver, isolate->factory()->match_symbol()),
+ Nothing<bool>());
+
+ if (!match->IsUndefined(isolate)) return Just(match->BooleanValue());
+ return Just(object->IsJSRegExp());
+}
+
+Handle<String> PatternFlags(Isolate* isolate, Handle<JSRegExp> regexp) {
+ IncrementalStringBuilder builder(isolate);
+ const JSRegExp::Flags flags = regexp->GetFlags();
+
+ if ((flags & JSRegExp::kGlobal) != 0) builder.AppendCharacter('g');
+ if ((flags & JSRegExp::kIgnoreCase) != 0) builder.AppendCharacter('i');
+ if ((flags & JSRegExp::kMultiline) != 0) builder.AppendCharacter('m');
+ if ((flags & JSRegExp::kUnicode) != 0) builder.AppendCharacter('u');
+ if ((flags & JSRegExp::kSticky) != 0) builder.AppendCharacter('y');
+
+ return builder.Finish().ToHandleChecked();
+}
+
+// ES#sec-regexpinitialize
+// Runtime Semantics: RegExpInitialize ( obj, pattern, flags )
+MaybeHandle<JSRegExp> RegExpInitialize(Isolate* isolate,
+ Handle<JSRegExp> regexp,
+ Handle<Object> pattern,
+ Handle<Object> flags) {
+ Handle<String> pattern_string;
+ if (pattern->IsUndefined(isolate)) {
+ pattern_string = isolate->factory()->empty_string();
+ } else {
+ ASSIGN_RETURN_ON_EXCEPTION(isolate, pattern_string,
+ Object::ToString(isolate, pattern), JSRegExp);
+ }
+
+ Handle<String> flags_string;
+ if (flags->IsUndefined(isolate)) {
+ flags_string = isolate->factory()->empty_string();
+ } else {
+ ASSIGN_RETURN_ON_EXCEPTION(isolate, flags_string,
+ Object::ToString(isolate, flags), JSRegExp);
+ }
+
+ // TODO(jgruber): We could avoid the flags back and forth conversions.
+ RETURN_RESULT(isolate,
+ JSRegExp::Initialize(regexp, pattern_string, flags_string),
+ JSRegExp);
+}
+
+} // namespace
+
+// ES#sec-regexp-pattern-flags
+// RegExp ( pattern, flags )
+BUILTIN(RegExpConstructor) {
+ HandleScope scope(isolate);
+
+ Handle<HeapObject> new_target = args.new_target();
+ Handle<Object> pattern = args.atOrUndefined(isolate, 1);
+ Handle<Object> flags = args.atOrUndefined(isolate, 2);
+
+ Handle<JSFunction> target =
+ handle(isolate->native_context()->regexp_function(), isolate);
+
+ bool pattern_is_regexp;
+ {
+ Maybe<bool> maybe_pattern_is_regexp = IsRegExp(isolate, pattern);
+ if (maybe_pattern_is_regexp.IsNothing()) {
+ DCHECK(isolate->has_pending_exception());
+ return isolate->heap()->exception();
+ }
+ pattern_is_regexp = maybe_pattern_is_regexp.FromJust();
+ }
+
+ if (new_target->IsUndefined(isolate)) {
+ new_target = target;
+
+ // ES6 section 21.2.3.1 step 3.b
+ if (pattern_is_regexp && flags->IsUndefined(isolate)) {
+ Handle<Object> pattern_constructor;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, pattern_constructor,
+ Object::GetProperty(pattern,
+ isolate->factory()->constructor_string()));
+
+ if (*pattern_constructor == *new_target) {
+ return *pattern;
+ }
+ }
+ } else if (!new_target->IsJSReceiver()) {
+ // TODO(jgruber): Better error message.
+ THROW_NEW_ERROR_RETURN_FAILURE(
+ isolate, NewTypeError(MessageTemplate::kCalledNonCallable, new_target));
+ }
+ Handle<JSReceiver> new_target_receiver = Handle<JSReceiver>::cast(new_target);
+
+ if (pattern->IsJSRegExp()) {
+ Handle<JSRegExp> regexp_pattern = Handle<JSRegExp>::cast(pattern);
+
+ if (flags->IsUndefined(isolate)) {
+ flags = PatternFlags(isolate, regexp_pattern);
+ }
+ pattern = handle(regexp_pattern->source(), isolate);
+ } else if (pattern_is_regexp) {
+ Handle<Object> pattern_source;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, pattern_source,
+ Object::GetProperty(pattern, isolate->factory()->source_string()));
+
+ if (flags->IsUndefined(isolate)) {
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, flags,
+ Object::GetProperty(pattern, isolate->factory()->flags_string()));
+ }
+ pattern = pattern_source;
+ }
+
+ Handle<JSObject> object;
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, object, JSObject::New(target, new_target_receiver));
+ Handle<JSRegExp> regexp = Handle<JSRegExp>::cast(object);
+
+ RETURN_RESULT_OR_FAILURE(isolate,
+ RegExpInitialize(isolate, regexp, pattern, flags));
+}
+
+} // namespace internal
+} // namespace v8
« no previous file with comments | « src/builtins/builtins.h ('k') | src/heap-symbols.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698