-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Subclassing RegExp makes no progress if the global flag is set. #2277
Comments
This is because your subclass is incorrect. You need to either call super() so the internal slots are set properly, or you need to also override the In other words, a simpler subclass that does what you expect might be: class GlobalRegExp extends RegExp {
constructor(source, flags = 'g') {
super(source, String(flags || '').replace(/g/g, 'g'));
}
} |
So this isn't about building a successful implementation with the spec we have, but rather ensuring the specification is consistent and cohesive around this subclassing type. To review a couple of points:
The inconsistency of use between A bandaid fix for this specific bug would be to change @@match to use [[OriginalFlags]] to be consistent with RegExpBuiltinExec. Having said that, I suspect similar problems exist around the fullUnicode and sticky flags as well, and have not fully audited them. |
It's also causing infinite loop and OOM errors if we try to overwrite the built-in getter: Object.defineProperty(RegExp.prototype, 'global', { value: true });
'foo'.replace(/o/, 'a') |
Consider the following test case:
On the three engines I have tried (SM, V8, JSC), this produces out-of-memory situations unexpectedly.
I've been investigating the subclassing removal proposal. This failure seems to be an error in the implementation of Type IV subclassing of RegExp.
Specifically, the @@match method uses Type IV subclassing to decide if we're a global regexp; and so loops over the input. However, RegExpBuiltinExec does not use Type IV subclassing, and instead uses [[OriginalFlags]]. As such the algorithms are getting confused around
lastIndex
. Specifically, 22.2.5.2.2 Step 15.a of RegExpBuiltinExec isn't run, and so as a result,@@match
simply loops forever with the same lastIndex.It appears that the change to
RegExpBuiltinExec
in order to use [[OriginalFlags]] was done explicitly in 2016, to resolve an issue, but I think that resolution ends up being the root cause of this one.The text was updated successfully, but these errors were encountered: