Skip to content

Commit b22b258

Browse files
littledanCommit bot
authored andcommitted
ES2015 web compat workaround: RegExp.prototype.flags => ""
It turns out that some old polyfill library uses RegExp.prototype.flags as a way of feature testing. It's not clear how widespread this is. For now, as a minimal workaround, we can return undefined from getters like RegExp.prototype.global when the receiver is RegExp.prototype. This patch implements that strategy but omits a UseCounter to make backports easier. R=adamk [email protected] BUG=chromium:581577 LOG=Y CQ_INCLUDE_TRYBOTS=tryserver.chromium.linux:linux_chromium_rel_ng;tryserver.blink:linux_blink_rel Review URL: https://codereview.chromium.org/1640803003 Cr-Commit-Position: refs/heads/master@{#34201}
1 parent 579c010 commit b22b258

6 files changed

Lines changed: 33 additions & 6 deletions

File tree

src/js/harmony-unicode-regexps.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ utils.Import(function(from) {
2424
// ES6 21.2.5.15.
2525
function RegExpGetUnicode() {
2626
if (!IS_REGEXP(this)) {
27+
// TODO(littledan): Remove this RegExp compat workaround
2728
if (this === GlobalRegExpPrototype) {
2829
%IncrementUseCounter(kRegExpPrototypeUnicodeGetter);
30+
return UNDEFINED;
2931
}
3032
throw MakeTypeError(kRegExpNonRegExp, "RegExp.prototype.unicode");
3133
}

src/js/regexp.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,10 @@ function RegExpMakeCaptureGetter(n) {
642642
// ES6 21.2.5.4.
643643
function RegExpGetGlobal() {
644644
if (!IS_REGEXP(this)) {
645+
// TODO(littledan): Remove this RegExp compat workaround
646+
if (this === GlobalRegExpPrototype) {
647+
return UNDEFINED;
648+
}
645649
throw MakeTypeError(kRegExpNonRegExp, "RegExp.prototype.global");
646650
}
647651
return !!REGEXP_GLOBAL(this);
@@ -653,6 +657,10 @@ function RegExpGetGlobal() {
653657
// ES6 21.2.5.5.
654658
function RegExpGetIgnoreCase() {
655659
if (!IS_REGEXP(this)) {
660+
// TODO(littledan): Remove this RegExp compat workaround
661+
if (this === GlobalRegExpPrototype) {
662+
return UNDEFINED;
663+
}
656664
throw MakeTypeError(kRegExpNonRegExp, "RegExp.prototype.ignoreCase");
657665
}
658666
return !!REGEXP_IGNORE_CASE(this);
@@ -664,6 +672,10 @@ function RegExpGetIgnoreCase() {
664672
// ES6 21.2.5.7.
665673
function RegExpGetMultiline() {
666674
if (!IS_REGEXP(this)) {
675+
// TODO(littledan): Remove this RegExp compat workaround
676+
if (this === GlobalRegExpPrototype) {
677+
return UNDEFINED;
678+
}
667679
throw MakeTypeError(kRegExpNonRegExp, "RegExp.prototype.multiline");
668680
}
669681
return !!REGEXP_MULTILINE(this);
@@ -675,6 +687,10 @@ function RegExpGetMultiline() {
675687
// ES6 21.2.5.10.
676688
function RegExpGetSource() {
677689
if (!IS_REGEXP(this)) {
690+
// TODO(littledan): Remove this RegExp compat workaround
691+
if (this === GlobalRegExpPrototype) {
692+
return UNDEFINED;
693+
}
678694
throw MakeTypeError(kRegExpNonRegExp, "RegExp.prototype.source");
679695
}
680696
return REGEXP_SOURCE(this);

test/mjsunit/es6/regexp-flags.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,9 @@ assertEquals(4, get_count);
5050

5151

5252
function testName(name) {
53-
if (name === "sticky") {
54-
assertEquals(undefined, RegExp.prototype[name]);
55-
} else {
56-
assertThrows(() => RegExp.prototype[name], TypeError);
57-
}
53+
// TODO(littledan): For web compatibility, we don't throw an exception,
54+
// but ES2015 expects an exception to be thrown from this getter.
55+
assertEquals(undefined, RegExp.prototype[name]);
5856
assertEquals(
5957
"get " + name,
6058
Object.getOwnPropertyDescriptor(RegExp.prototype, name).get.name);
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Copyright 2015 the V8 project authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
assertEquals("", RegExp.prototype.flags);

test/test262/test262.status

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,12 @@
252252
'built-ins/Object/entries/*': [SKIP],
253253
'built-ins/Object/values/*': [SKIP],
254254

255+
# https://code.google.com/p/chromium/issues/detail?id=581577
256+
'built-ins/RegExp/prototype/source/15.10.7.1-1': [FAIL],
257+
'built-ins/RegExp/prototype/global/15.10.7.2-1': [FAIL],
258+
'built-ins/RegExp/prototype/ignoreCase/15.10.7.3-1': [FAIL],
259+
'built-ins/RegExp/prototype/multiline/15.10.7.4-1': [FAIL],
260+
255261
######################## NEEDS INVESTIGATION ###########################
256262

257263
# These test failures are specific to the intl402 suite and need investigation

test/webkit/fast/regex/toString-expected.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE
2828

2929
PASS RegExp('/').source is "\\/"
3030
PASS RegExp('').source is "(?:)"
31-
FAIL RegExp.prototype.source should be (?:). Threw exception TypeError: RegExp.prototype.source getter called on non-RegExp object
31+
FAIL RegExp.prototype.source should be (?:) (of type string). Was undefined (of type undefined).
3232
PASS RegExp('/').toString() is "/\\//"
3333
PASS RegExp('').toString() is "/(?:)/"
3434
PASS RegExp.prototype.toString() is "/(?:)/"

0 commit comments

Comments
 (0)