Skip to content
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

Consider restricting attribute keys syntax to identifiers and strings again #145

Closed
nicolo-ribaudo opened this issue Jul 17, 2023 · 12 comments

Comments

@nicolo-ribaudo
Copy link
Member

BigInt attribute keys (i.e. import "x" with { 10n: "val" }) need to be converted to strings while parsing, to check for duplicate keys. In SpiderMonkey this would require allocating the bigint at parse time, which is something that does not currently happen (the object { 10n: x } is parsed as { [10n]: x }).

The motivation for allowing number&bigint literals in import attribute keys is for symmetry with object literals.

@mgaudet
Copy link

mgaudet commented Jul 17, 2023

The other thing that I find sort of strange here is that the argument is that we're trying to be symmetric with the object literal version for dynamic import, but the syntax ends up still being a subset of the object literal syntax. For example,

let x = "type"
import "foo" with { [x]: "js"} 

won't parse -- IMO having a tighter simpler rule for the WithClause makes more sense than trying to align it with the dynamic import case.

@nicolo-ribaudo
Copy link
Member Author

It looks like JSC currently allocates bigints while parsing, but they would like to stop doing so:
https://github.com/WebKit/WebKit/blob/4b27f17f5ade44bc02f5d8707c8140a762bb9253/Source/JavaScriptCore/parser/ParserArena.cpp#L98-L112

V8 allocates bigints while parsing without problems

@nicolo-ribaudo
Copy link
Member Author

Also cc @tc39/ecma262-editors, since the keys syntax was relaxed due to feedback from the stage 3 review 🙂

@michaelficarra
Copy link
Member

I don't understand the problem. BigInts are only allocated when someone uses them as keys.

@mgaudet
Copy link

mgaudet commented Jul 18, 2023

The spec requires that duplicate assertion keys are detected and reported as syntax errors; but that means that we need to be able to report that import "f.js" with { 1_000n: "value", 1000n: "value" } are the same. In the arbitrary case of a BigInt, we don't want our parser to have to have a full understanding of BigInt semantics, so we would have to allocate a BigInt.

However, as a design principle we are not allocating GC things during parse; this would be an instance of us having to do so.

@mgaudet
Copy link

mgaudet commented Jul 18, 2023

(for regular numeric values we have specialized machinery to handle this, but since a bigint is arbitrary precision, we would need the full arbitrary precision comparison)

@ljharb
Copy link
Member

ljharb commented Jul 18, 2023

Forgive me if i don't understand some nuance of parsers, but if it's successfully parsed as a bigint, you can't just remove the _s and compare literal source text to see if it's the same?

@michaelficarra
Copy link
Member

michaelficarra commented Jul 18, 2023

@ljharb No, the BigInt grammar is not otherwise canonicalising. 0xFFn === 255n

@ljharb
Copy link
Member

ljharb commented Jul 18, 2023

ah, i forgot about hex literals, fair point

@michaelficarra
Copy link
Member

michaelficarra commented Jul 18, 2023

Regardless, though, I am skeptical of the claim that a parser must instantiate an actual JS BigInt to be able to detect duplicate keys. You don't need to allocate an actual BigInt to compute BigInt::toString.

edit: And this cost, even if excessive, is only paid when the program actually uses this combination of features (import assertions with bigint keys), which will be exceptionally rare.

@bakkot
Copy link
Contributor

bakkot commented Jul 18, 2023

It's definitely possible in principle. Seems like an awful lot of work to ask of implementations for something which isn't ever actually going to be used, though.

@ghost
Copy link

ghost commented Jul 22, 2023

Just implemented an attributes parser, with numeric literal support. I was surprised it was there. Just some thoughts:

  • BigInt support adds no value. If you need a numeric key, you can get it with a standard number or a string.
  • There is precedent in modules of designing the (static) syntax specifically for easy parsing. It could be argued that a more restricted syntax maintains alignment with the established precedent.

I don't have strong feelings. Following this so my parser can stay aligned with whatever happens.

aduh95 added a commit to aduh95/node that referenced this issue Oct 24, 2023
Original commit message:

    [import-attributes] Remove support for numeric keys

    During the 2023-09 TC39 meeting the proposal has been updated to remove support
    for bigint and float literals as import attribute keys, due to implementation
    difficulties in other engines and minimal added value for JS developers.

    GH issue: tc39/proposal-import-attributes#145

    Bug: v8:13856
    Change-Id: I0ede2bb10d6ca338a4b0870a1261ccbcd088c16f
    Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4899760
    Reviewed-by: Shu-yu Guo <[email protected]>
    Commit-Queue: Joyee Cheung <[email protected]>
    Cr-Commit-Position: refs/heads/main@{#90318}

Refs: v8/v8@ea996ad
targos pushed a commit to nodejs/node that referenced this issue Nov 11, 2023
Original commit message:

    [import-attributes] Remove support for numeric keys

    During the 2023-09 TC39 meeting the proposal has been updated to remove support
    for bigint and float literals as import attribute keys, due to implementation
    difficulties in other engines and minimal added value for JS developers.

    GH issue: tc39/proposal-import-attributes#145

    Bug: v8:13856
    Change-Id: I0ede2bb10d6ca338a4b0870a1261ccbcd088c16f
    Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4899760
    Reviewed-by: Shu-yu Guo <[email protected]>
    Commit-Queue: Joyee Cheung <[email protected]>
    Cr-Commit-Position: refs/heads/main@{#90318}

Refs: v8/v8@ea996ad
PR-URL: #50183
Reviewed-By: Geoffrey Booth <[email protected]>
richardlau pushed a commit to nodejs/node that referenced this issue Mar 18, 2024
Original commit message:
    [import-attributes] Remove support for numeric keys

    During the 2023-09 TC39 meeting the proposal has been updated to remove support
    for bigint and float literals as import attribute keys, due to implementation
    difficulties in other engines and minimal added value for JS developers.

    GH issue: tc39/proposal-import-attributes#145

    Bug: v8:13856
    Change-Id: I0ede2bb10d6ca338a4b0870a1261ccbcd088c16f
    Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4899760
    Reviewed-by: Shu-yu Guo <[email protected]>
    Commit-Queue: Joyee Cheung <[email protected]>
    Cr-Commit-Position: refs/heads/main@{#90318}

Refs: v8/v8@ea996ad
PR-URL: #51136
Reviewed-By: Yagiz Nizipli <[email protected]>
abhishekumar-tyagi pushed a commit to abhishekumar-tyagi/node that referenced this issue May 5, 2024
Original commit message:
    [import-attributes] Remove support for numeric keys

    During the 2023-09 TC39 meeting the proposal has been updated to remove support
    for bigint and float literals as import attribute keys, due to implementation
    difficulties in other engines and minimal added value for JS developers.

    GH issue: tc39/proposal-import-attributes#145

    Bug: v8:13856
    Change-Id: I0ede2bb10d6ca338a4b0870a1261ccbcd088c16f
    Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4899760
    Reviewed-by: Shu-yu Guo <[email protected]>
    Commit-Queue: Joyee Cheung <[email protected]>
    Cr-Commit-Position: refs/heads/main@{#90318}

Refs: v8/v8@ea996ad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants