ECMAScript proposal at stage 3 of the process, see https://github.com/tc39/proposals
This is a specification draft for the legacy (deprecated) RegExp features in JavaScript, i.e., static properties of the constructor like RegExp.$1
as well as the RegExp.prototype.compile
method.
This does not reflect what the implementations do, but what the editor thinks to be the least bad thing they ought to do in order to maintain web compatibility.
RegExp static properties (currently not part of ECMA 262,see tc39/ecma262#137) are specified such that:
- The values returned by those properties are updated each time a successful match is done.
- They may be deleted. (This is important for secured environments that want to avoid global side-effects.)
The proposal includes another feature that needs consensus and implementation experience before being specced:
- RegExp legacy static properties as well as RegExp.prototype.compile are disabled for instances of proper subclasses of RegExp as well as for cross-realm regexps. See the detailed motivation here.
We have attempted to identify potential risks induced by the the backward-compatibility break introduced by that feature.
See also the differences between this spec and the current implementations.
The amendments are relative to the last ECMAScript specification draft found at: https://tc39.github.io/ecma262/ Changes relative to existing algorithms are marked in bold.
All the amendments are part of Annex B, including those that modify objects or algorithm defined in other parts of the spec.
The %RegExp% intrinsic object, which is the builtin RegExp constructor, has the following additional internal slots:
- [[RegExpInput]]
- [[RegExpLastMatch]]
- [[RegExpLastParen]]
- [[RegExpLeftContext]]
- [[RegExpRightContext]]
- [[RegExpParen1]]
- [[RegExpParen2]]
- [[RegExpParen3]]
- [[RegExpParen4]]
- [[RegExpParen5]]
- [[RegExpParen6]]
- [[RegExpParen7]]
- [[RegExpParen8]]
- [[RegExpParen9]]
The initial value of all these internal slots is the empty String.
RegExp instances have an additional slot which optionally keeps a reference to its constructor. It is used for deciding whether a nonstandard legacy feature is enabled for that regexp. The RegExpAlloc abstract operation is modified as follows:
- Let obj be ? OrdinaryCreateFromConstructor(newTarget, "%RegExpPrototype%", «[[RegExpMatcher]], [[OriginalSource]], [[OriginalFlags]], [[Realm]], [[LegacyFeaturesEnabled]]»).
- Let thisRealm be the current Realm Record.
- Set the value of obj’s [[Realm]] internal slot to thisRealm.
- If SameValue(newTarget, thisRealm.[[Intrinsics]].[[%RegExp%]]) is true, then
- Set the value of obj’s [[LegacyFeaturesEnabled]] internal slot to true.
- Else,
- Set the value of obj’s [[LegacyFeaturesEnabled]] internal slot to false.
- Perform ! DefinePropertyOrThrow(obj, "lastIndex", PropertyDescriptor {[[Writable]]: true, [[Enumerable]]: false, [[Configurable]]: false}).
- Return obj.
In the RegExpBuiltInExec abstract operation, a hook is added for updating the static properties of %RegExp% after a successful match. The last three steps of the algorithm are modified as follows:
- ...
- (current step 23) Perform ! CreateDataProperty(A, "0", matchedSubstr).
- Let capturedValues be an new empty List.
- (current step 24) For each integer i such that i > 0 and i ≤ n
- ...
- (current step 24.e) Perform ! CreateDataProperty(A, ToString(i) , capturedValue).
- Append capturedValue to the end of capturedValues.
- Let thisRealm be the current Realm Record.
- Let rRealm be the value of R’s [[Realm]] internal slot.
- If SameValue(thisRealm, rRealm) is true, then
- If the value of R’s [[LegacyFeaturesEnabled]] internal slot is true, then
- Perform UpdateLegacyRegExpStaticProperties(%RegExp%, S, lastIndex, e, capturedValues).
- Else,
- Perform InvalidateLegacyRegExpStaticProperties(%RegExp%).
- If the value of R’s [[LegacyFeaturesEnabled]] internal slot is true, then
- (current step 25) Return A.
The abstract operation UpdateLegacyRegExpStaticProperties updates the values of the static properties of %RegExp% after a successful match.
- Assert: C is an Object that has a [[RegExpInput]] internal slot.
- Assert: Type(S) is String.
- Let len be the number of code units in S.
- Assert: startIndex and endIndex are integers such that 0 ≤ startIndex ≤ endIndex ≤ len.
- Assert: capturedValues is a List of Strings.
- Let n be the number of elements in capturedValues.
- Set the value of C’s [[RegExpInput]] internal slot to S.
- Set the value of C’s [[RegExpLastMatch]] internal slot to a String whose length is endIndex - startIndex and containing the code units from S with indices startIndex through endIndex - 1, in ascending order.
- If n > 0, set the value of C’s [[RegExpLastParen]] internal slot to the last element of capturedValues.
- Else, set the value of C’s [[RegExpLastParen]] internal slot to the empty String.
- Set the value of C’s [[RegExpLeftContext]] internal slot to a String whose length is startIndex and containing the code units from S with indices 0 through startIndex - 1, in ascending order.
- Set the value of C’s [[RegExpRightContext]] internal slot to a String whose length is len - endIndex and containing the code units from S with indices endIndex through len - 1, in ascending order.
- For each integer i such that 1 ≤ i ≤ 9
- If i ≤ n, set the value of C’s [[RegExpPareni]] internal slot to the ith element of capturedValues.
- Else, set the value of C’s [[RegExpPareni]] internal slot to the empty String.
The abstract operation InvalidateLegacyRegExpStaticProperties marks the values of the static properties of %RegExp% as non-available.
- Assert: C is an Object that has a [[RegExpInput]] internal slot.
- Set the value of the following internal slots of C to empty:
- [[RegExpInput]]
- [[RegExpLastMatch]]
- [[RegExpLastParen]]
- [[RegExpLeftContext]]
- [[RegExpRightContext]]
- [[RegExpParen1]]
- [[RegExpParen2]]
- [[RegExpParen3]]
- [[RegExpParen4]]
- [[RegExpParen5]]
- [[RegExpParen6]]
- [[RegExpParen7]]
- [[RegExpParen8]]
- [[RegExpParen9]]
All the below properties are accessor properties who have the attributes { [[Enumerable]]: false, [[Configurable]]: true }. Moreover, for the properties whose setter is not explicitely defined, the [[Set]] attribute is set to undefined.
The accessors check for their this value, so that the properties do not appear to be inherited by subclasses.
The abstract operation GetLegacyRegExpStaticProperty is used when retrieving a value from a legacy RegExp static property.
- Assert C is an object that has an internal slot named internalSlotName.
- If SameValue(C, thisValue) is false, throw a TypeError exception.
- Let val be the value of the internal slot of C named internalSlotName.
- If val is empty, throw a TypeError exception.
- Return val.
The abstract operation SetLegacyRegExpStaticProperty is used when assigning a value to a legacy RegExp static property.
- Assert C is an object that has an internal slot named internalSlotName.
- If SameValue(C, thisValue) is false, throw a TypeError exception.
- Let strVal be ? ToString(val).
- Set the value of the internal slot of C named internalSlotName to strVal.
- Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpInput]]).
- Perform ? SetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpInput]], val).
- Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpInput]]).
- Perform ? SetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpInput]], val).
- Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpLastMatch]]).
- Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpLastMatch]]).
- Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpLastParen]]).
- Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpLastParen]]).
- Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpLeftContext]]).
- Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpLeftContext]]).
- Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpRightContext]]).
- Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpRightContext]]).
- Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpParen1]]).
- Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpParen2]]).
- Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpParen3]]).
- Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpParen4]]).
- Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpParen5]]).
- Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpParen6]]).
- Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpParen7]]).
- Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpParen8]]).
- Return ? GetLegacyRegExpStaticProperty(%RegExp%, this value, [[RegExpParen9]]).
The modification below will disable RegExp.prototype.compile for objects that are not direct instances of RegExp as well as in case of mismatch between realms.
- Let O be the this value.
- If Type(O) is not Object or Type(O) is Object and O does not have a [[RegExpMatcher]] internal slot, then
- Throw a TypeError exception.
- Let thisRealm be the current Realm Record.
- Let oRealm be the value of O’s [[Realm]] internal slot.
- If SameValue(thisRealm, oRealm) is false, throw a TypeError exception.
- If the value of R’s [[LegacyFeaturesEnabled]] internal slot is false, throw a TypeError exception.
- If Type(pattern) is Object and pattern has a [[RegExpMatcher]] internal slot, then
- If flags is not undefined, throw a TypeError exception.
- Let P be the value of pattern’s [[OriginalSource]] internal slot.
- Let F be the value of pattern’s [[OriginalFlags]] internal slot.
- Else,
- Let P be pattern.
- Let F be flags.
- Return ? RegExpInitialize(O, P, F).