Skip to content

Commit

Permalink
[Fix] RegExp#toString: should throw when constructed
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Mar 23, 2023
1 parent f3d5fed commit 6c3fe3d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
10 changes: 10 additions & 0 deletions es6-shim.js
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,16 @@
});
if (!regexToStringIsGeneric || !regexToStringSupportsGenericFlags) {
var origRegExpToString = RegExp.prototype.toString;
var origShimProto;
defineProperty(RegExp.prototype, 'toString', function toString() {
try {
toString.prototype = origShimProto;
if (this instanceof toString) {
throw new TypeError('RegExp.prototype.toString is not a constructor');
}
} finally {
toString.prototype = void 0;
}
var R = ES.RequireObjectCoercible(this);
if (Type.regex(R)) {
return _call(origRegExpToString, R);
Expand All @@ -1797,6 +1806,7 @@
return '/' + pattern + '/' + flags;
}, true);
Value.preserveToString(RegExp.prototype.toString, origRegExpToString);
origShimProto = RegExp.prototype.toString.prototype;
RegExp.prototype.toString.prototype = void 0;
}

Expand Down
2 changes: 2 additions & 0 deletions test/regexp.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,8 @@ describe('RegExp', function () {

it('has an undefined prototype', function () {
expect(RegExp.prototype.toString.prototype).to.equal(undefined);

expect(function () { return new RegExp.prototype.toString(); }).to['throw'](TypeError);
});

it('works on regexes', function () {
Expand Down

0 comments on commit 6c3fe3d

Please sign in to comment.