Commit 67e97125 authored by Patrick Thier's avatar Patrick Thier Committed by V8 LUCI CQ

[api] Properly escape RegExp source

Change API RegExp::GetSource to return a string identical to ToString()
and RegExp.prototype.source.

Bug: v8:11693
Change-Id: I3d148883fe6f8a3ff49e552ddd72b1e92f52baf3
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2900737Reviewed-by: 's avatarJakob Gruber <jgruber@chromium.org>
Reviewed-by: 's avatarMaya Lekova <mslekova@chromium.org>
Commit-Queue: Patrick Thier <pthier@chromium.org>
Cr-Commit-Position: refs/heads/master@{#74854}
parent 819d3cb5
...@@ -7003,7 +7003,7 @@ MaybeLocal<v8::RegExp> v8::RegExp::NewWithBacktrackLimit( ...@@ -7003,7 +7003,7 @@ MaybeLocal<v8::RegExp> v8::RegExp::NewWithBacktrackLimit(
Local<v8::String> v8::RegExp::GetSource() const { Local<v8::String> v8::RegExp::GetSource() const {
i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this); i::Handle<i::JSRegExp> obj = Utils::OpenHandle(this);
return Utils::ToLocal( return Utils::ToLocal(
i::Handle<i::String>(obj->Pattern(), obj->GetIsolate())); i::Handle<i::String>(obj->EscapedPattern(), obj->GetIsolate()));
} }
// Assert that the static flags cast in GetFlags is valid. // Assert that the static flags cast in GetFlags is valid.
......
...@@ -73,6 +73,12 @@ String JSRegExp::Pattern() { ...@@ -73,6 +73,12 @@ String JSRegExp::Pattern() {
return pattern; return pattern;
} }
String JSRegExp::EscapedPattern() {
DCHECK(this->source().IsString());
String pattern = String::cast(source());
return pattern;
}
Object JSRegExp::CaptureNameMap() { Object JSRegExp::CaptureNameMap() {
DCHECK(this->data().IsFixedArray()); DCHECK(this->data().IsFixedArray());
DCHECK(TypeSupportsCaptures(TypeTag())); DCHECK(TypeSupportsCaptures(TypeTag()));
......
...@@ -115,6 +115,7 @@ class JSRegExp : public TorqueGeneratedJSRegExp<JSRegExp, JSObject> { ...@@ -115,6 +115,7 @@ class JSRegExp : public TorqueGeneratedJSRegExp<JSRegExp, JSObject> {
inline int MaxRegisterCount() const; inline int MaxRegisterCount() const;
inline Flags GetFlags(); inline Flags GetFlags();
inline String Pattern(); inline String Pattern();
inline String EscapedPattern();
inline Object CaptureNameMap(); inline Object CaptureNameMap();
inline Object DataAt(int index) const; inline Object DataAt(int index) const;
// Set implementation data after the object has been prepared. // Set implementation data after the object has been prepared.
......
...@@ -18941,6 +18941,13 @@ TEST(RegExp) { ...@@ -18941,6 +18941,13 @@ TEST(RegExp) {
CHECK(re->GetSource()->Equals(context.local(), v8_str("foo")).FromJust()); CHECK(re->GetSource()->Equals(context.local(), v8_str("foo")).FromJust());
CHECK_EQ(v8::RegExp::kNone, re->GetFlags()); CHECK_EQ(v8::RegExp::kNone, re->GetFlags());
re = v8::RegExp::New(context.local(), v8_str("foo/bar"), v8::RegExp::kNone)
.ToLocalChecked();
CHECK(re->IsRegExp());
CHECK(
re->GetSource()->Equals(context.local(), v8_str("foo\\/bar")).FromJust());
CHECK_EQ(v8::RegExp::kNone, re->GetFlags());
re = v8::RegExp::New(context.local(), v8_str("bar"), re = v8::RegExp::New(context.local(), v8_str("bar"),
static_cast<v8::RegExp::Flags>(v8::RegExp::kIgnoreCase | static_cast<v8::RegExp::Flags>(v8::RegExp::kIgnoreCase |
v8::RegExp::kGlobal)) v8::RegExp::kGlobal))
...@@ -18973,6 +18980,11 @@ TEST(RegExp) { ...@@ -18973,6 +18980,11 @@ TEST(RegExp) {
CHECK(re->GetSource()->Equals(context.local(), v8_str("quux")).FromJust()); CHECK(re->GetSource()->Equals(context.local(), v8_str("quux")).FromJust());
CHECK_EQ(v8::RegExp::kNone, re->GetFlags()); CHECK_EQ(v8::RegExp::kNone, re->GetFlags());
re = CompileRun("RegExp('qu/ux')").As<v8::RegExp>();
CHECK(re->IsRegExp());
CHECK(re->GetSource()->Equals(context.local(), v8_str("qu\\/ux")).FromJust());
CHECK_EQ(v8::RegExp::kNone, re->GetFlags());
re = CompileRun("/quux/gm").As<v8::RegExp>(); re = CompileRun("/quux/gm").As<v8::RegExp>();
CHECK(re->IsRegExp()); CHECK(re->IsRegExp());
CHECK(re->GetSource()->Equals(context.local(), v8_str("quux")).FromJust()); CHECK(re->GetSource()->Equals(context.local(), v8_str("quux")).FromJust());
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment