Commit 50be01b2 authored by Mathias Bynens's avatar Mathias Bynens Committed by Commit Bot

[inspector] Improve regular expression printing

Prior to this patch, `new RegExp('a/b')` logs the following in
the DevTools Console:

    /a/b/

This is syntactically invalid.

This patch fixes this while simplifying regular expression printing
in general by leveraging `RegExp#toString`, instead of duplicating
the logic on the inspector side. This is possible thanks to the recent
work on making `RegExp#toString` more robust (v8:1982).

Bug: chromium:1202013, v8:1982
Change-Id: I14ccc1892f4a99361ad170fea608ace630740991
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2848463
Commit-Queue: Mathias Bynens <mathias@chromium.org>
Reviewed-by: 's avatarYang Guo <yangguo@chromium.org>
Cr-Commit-Position: refs/heads/master@{#74169}
parent 0cd8a913
...@@ -195,22 +195,19 @@ String16 descriptionForPrimitiveType(v8::Local<v8::Context> context, ...@@ -195,22 +195,19 @@ String16 descriptionForPrimitiveType(v8::Local<v8::Context> context,
return String16(); return String16();
} }
String16 descriptionForRegExp(v8::Isolate* isolate, String16 descriptionForObject(v8::Isolate* isolate,
v8::Local<v8::Object> object) {
return toProtocolString(isolate, object->GetConstructorName());
}
String16 descriptionForRegExp(v8::Local<v8::Context> context,
v8::Local<v8::RegExp> value) { v8::Local<v8::RegExp> value) {
String16Builder description; v8::Isolate* isolate = context->GetIsolate();
description.append('/'); v8::Local<v8::String> description;
description.append(toProtocolString(isolate, value->GetSource())); if (!value->ToString(context).ToLocal(&description)) {
description.append('/'); return descriptionForObject(isolate, value);
v8::RegExp::Flags flags = value->GetFlags(); }
if (flags & v8::RegExp::Flags::kHasIndices) description.append('d'); return toProtocolString(isolate, description);
if (flags & v8::RegExp::Flags::kGlobal) description.append('g');
if (flags & v8::RegExp::Flags::kIgnoreCase) description.append('i');
if (flags & v8::RegExp::Flags::kLinear) description.append('l');
if (flags & v8::RegExp::Flags::kMultiline) description.append('m');
if (flags & v8::RegExp::Flags::kDotAll) description.append('s');
if (flags & v8::RegExp::Flags::kUnicode) description.append('u');
if (flags & v8::RegExp::Flags::kSticky) description.append('y');
return description.toString();
} }
enum class ErrorType { kNative, kClient }; enum class ErrorType { kNative, kClient };
...@@ -268,11 +265,6 @@ String16 descriptionForError(v8::Local<v8::Context> context, ...@@ -268,11 +265,6 @@ String16 descriptionForError(v8::Local<v8::Context> context,
return description + stackWithoutMessage; return description + stackWithoutMessage;
} }
String16 descriptionForObject(v8::Isolate* isolate,
v8::Local<v8::Object> object) {
return toProtocolString(isolate, object->GetConstructorName());
}
String16 descriptionForDate(v8::Local<v8::Context> context, String16 descriptionForDate(v8::Local<v8::Context> context,
v8::Local<v8::Date> date) { v8::Local<v8::Date> date) {
v8::Isolate* isolate = context->GetIsolate(); v8::Isolate* isolate = context->GetIsolate();
...@@ -1605,7 +1597,7 @@ std::unique_ptr<ValueMirror> ValueMirror::create(v8::Local<v8::Context> context, ...@@ -1605,7 +1597,7 @@ std::unique_ptr<ValueMirror> ValueMirror::create(v8::Local<v8::Context> context,
if (value->IsRegExp()) { if (value->IsRegExp()) {
return std::make_unique<ObjectMirror>( return std::make_unique<ObjectMirror>(
value, RemoteObject::SubtypeEnum::Regexp, value, RemoteObject::SubtypeEnum::Regexp,
descriptionForRegExp(isolate, value.As<v8::RegExp>())); descriptionForRegExp(context, value.As<v8::RegExp>()));
} }
if (value->IsProxy()) { if (value->IsProxy()) {
return std::make_unique<ObjectMirror>( return std::make_unique<ObjectMirror>(
......
...@@ -487,6 +487,16 @@ Running test: testRegExp ...@@ -487,6 +487,16 @@ Running test: testRegExp
type : object type : object
} }
} }
'new RegExp('foo/bar')', returnByValue: false, generatePreview: false
{
result : {
className : RegExp
description : /foo\/bar/
objectId : <objectId>
subtype : regexp
type : object
}
}
'var re = new RegExp('\w+', 'g'); 'var re = new RegExp('\w+', 'g');
re.prop = 32; re.prop = 32;
re', returnByValue: false, generatePreview: true re', returnByValue: false, generatePreview: true
......
...@@ -227,6 +227,9 @@ InspectorTest.runAsyncTestSuite([ ...@@ -227,6 +227,9 @@ InspectorTest.runAsyncTestSuite([
InspectorTest.logMessage((await evaluate({ InspectorTest.logMessage((await evaluate({
expression: `new RegExp('\\w+', 'g')`, expression: `new RegExp('\\w+', 'g')`,
})).result); })).result);
InspectorTest.logMessage((await evaluate({
expression: `new RegExp('foo/bar')`
})).result);
InspectorTest.logMessage((await evaluate({ InspectorTest.logMessage((await evaluate({
expression: `var re = new RegExp('\\w+', 'g'); expression: `var re = new RegExp('\\w+', 'g');
re.prop = 32; re.prop = 32;
......
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