Skip to content

Normative: Don't call well-known Symbol methods for RegExp on primitive values #3009

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

petamoriken
Copy link

Fixed that methods for RegExp, such as @@split, are not called on primitive values.

Background

The internal code of runtimes such as Node.js and Deno are implemented that they work without problems even if they are polluted with prototypes. With this normative change in place, those runtimes don't need to worry if changes to String.prototype[Symbol.split] are polluted.

On the other hand, if this normative change is not approved, the following workaround will have to be provided:

// primordial
const StringPrototypeSplit = Function.call.bind(String.prototype.split);

// safe wrapper
export const SafeStringPrototypeSplit = (str, separator, limit) => {
  if (typeof separator === "string") {
    return StringPrototypeSplit(str, {
      __proto__: null,
      toString() { return separator },
    }, limit);
  }
  return StringPrototypeSplit(str, separator, limit);
};

This approach could cause performance issue.

downstream: denoland/deno#17473

@ljharb ljharb added normative change Affects behavior required to correctly evaluate some ECMAScript source text needs consensus This needs committee consensus before it can be eligible to be merged. needs test262 tests The proposal should specify how to test an implementation. Ideally via github.com/tc39/test262 labels Feb 5, 2023
@ljharb
Copy link
Member

ljharb commented Feb 5, 2023

@petamoriken can someone from deno add this to the March meeting agenda?

@petamoriken
Copy link
Author

@lucacasonato Would you work on this issue?

@petamoriken petamoriken force-pushed the normative/dont-call-regexp-@@method-on-primitives branch from ca31671 to 25424af Compare March 2, 2023 08:05
@bathos
Copy link
Contributor

bathos commented Mar 2, 2023

I can see that this change would cause this to no longer evaluate to “surprise”:

String.prototype[Symbol.split] = () => "surprise";
"a.b.c".split(".");

I see no obvious downside to the proposed change in a vacuum, but given it’s a breaking change, I’m curious whether there’s been research yet on whether it’s web compatible in practice?

(It seems plausible that it could be safe, but it also seems plausible that faithful polyfill implementations, e.g. @zloirock’s core-js, could be impacted).

@zloirock
Copy link

zloirock commented Mar 2, 2023

core-js does not detect delegation to @@methods for primitives, only for objects, so it's enough safe.

@petamoriken petamoriken force-pushed the normative/dont-call-regexp-@@method-on-primitives branch from 25424af to 5942961 Compare April 5, 2023 08:55
@petamoriken petamoriken force-pushed the normative/dont-call-regexp-@@method-on-primitives branch from 5942961 to f9d3b02 Compare May 4, 2023 12:46
@petamoriken petamoriken force-pushed the normative/dont-call-regexp-@@method-on-primitives branch from f9d3b02 to c45ae45 Compare November 26, 2024 16:24
@petamoriken petamoriken changed the title Normative: Don't call @@method for RegExp on primitive values Normative: Don't call well-known Symbol methods for RegExp on primitive values Nov 26, 2024
@erights
Copy link

erights commented Feb 13, 2025

The (typeof separator === "string") in the expository shim should instead be
(!isObject(separator)) assuming

const isObject = v => Object(v) === v;

@petamoriken petamoriken force-pushed the normative/dont-call-regexp-@@method-on-primitives branch from 9b254aa to cd6e94f Compare February 21, 2025 11:57
@nicolo-ribaudo nicolo-ribaudo added has consensus This has committee consensus. and removed needs consensus This needs committee consensus before it can be eligible to be merged. labels Feb 21, 2025
@ptomato
Copy link
Contributor

ptomato commented Mar 12, 2025

Test262 PR has landed.

@ptomato ptomato added has test262 tests and removed needs test262 tests The proposal should specify how to test an implementation. Ideally via github.com/tc39/test262 labels Mar 12, 2025
sosukesuzuki added a commit to sosukesuzuki/WebKit that referenced this pull request Mar 29, 2025
https://bugs.webkit.org/show_bug.cgi?id=290684

Reviewed by NOBODY (OOPS!).

This patch implements tc39/ecma262#3009.
test262 has been updated for it already[1].

[1]: tc39/test262@abc22b5

* JSTests/test262/expectations.yaml:
* Source/JavaScriptCore/builtins/StringPrototype.js:
(match):
(matchAll):
(intrinsic.StringPrototypeReplaceIntrinsic.replace):
(intrinsic.StringPrototypeReplaceAllIntrinsic.replaceAll):
(search):
(split):
sosukesuzuki added a commit to sosukesuzuki/WebKit that referenced this pull request Mar 29, 2025
https://bugs.webkit.org/show_bug.cgi?id=290684

Reviewed by NOBODY (OOPS!).

This patch implements tc39/ecma262#3009.
test262 has been updated for it already[1].

[1]: tc39/test262@abc22b5

* JSTests/test262/expectations.yaml:
* Source/JavaScriptCore/builtins/StringPrototype.js:
(match):
(matchAll):
(intrinsic.StringPrototypeReplaceIntrinsic.replace):
(intrinsic.StringPrototypeReplaceAllIntrinsic.replaceAll):
(search):
(split):
webkit-commit-queue pushed a commit to sosukesuzuki/WebKit that referenced this pull request Mar 30, 2025
https://bugs.webkit.org/show_bug.cgi?id=290684

Reviewed by Yusuke Suzuki.

This patch implements tc39/ecma262#3009.
test262 has been updated for it already[1].

[1]: tc39/test262@abc22b5

* JSTests/test262/expectations.yaml:
* Source/JavaScriptCore/builtins/StringPrototype.js:
(match):
(matchAll):
(intrinsic.StringPrototypeReplaceIntrinsic.replace):
(intrinsic.StringPrototypeReplaceAllIntrinsic.replaceAll):
(search):
(split):

Canonical link: https://commits.webkit.org/292913@main
moz-v2v-gh pushed a commit to mozilla/gecko-dev that referenced this pull request Apr 25, 2025
… on primitives r=dminor,iain

This patch adds telemetry for tc39/ecma262#3009.

That normative PR has already been approved by TC39, but we need to
verify that it's web compatible.

Differential Revision: https://phabricator.services.mozilla.com/D241305
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has consensus This has committee consensus. has test262 tests needs implementations normative change Affects behavior required to correctly evaluate some ECMAScript source text
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants