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

Consistent way to represent name, length, and "is a subclass" for constructors #3053

Open
Josh-Cena opened this issue Apr 29, 2023 · 4 comments

Comments

@Josh-Cena
Copy link
Contributor

I'm trying to parse the ES spec for the prototype/length of constructors, and I found some annoying inconsistencies.

  • AsyncFunction
    • Says "has a "name" property whose value is "AsyncFunction"" in "Properties of the AsyncFunction Constructor"
    • Has a separate clause for AsyncFunction.length under "Properties of the AsyncFunction Constructor"
    • Says "is a subclass of Function" in "The AsyncFunction Constructor"
  • Date
    • No mention of its name
    • Says "has a "length" property whose value is 7𝔽" in "The Date Constructor"
  • RegExp
    • No mention of its name/length
  • TypedArray
    • Says "has a "name" property whose value is..." in "Properties of the TypedArray Constructors"
    • Says "has a "length" property whose value is 3𝔽" in "The TypedArray Constructors"
    • No mention that they are subclasses of %TypedArray% (probably okay? Inconsistent with Function subclasses, though)
  • AggregateError
    • No mention of its name/length
    • No mention that it is a subclass of Error (again, inconsistent with Function)

As summarized above, only Function subclasses explicitly say that they are subclasses. Furthermore, they are the only ones that have a separate entry for length instead of either being omitted (since they can be inferred anyway) or as a bullet under "The X Constructor". Maybe this is to highlight that this length and name are unrelated to the same-named properties on their instances, but this can be done in an editorial note as well.

As a bonus, I wonder if the mention of "length" can be moved to the "Properties of the X Constructor" section, since "length" and "name" are both, well, properties of the constructor.

Lastly, I hope that all subclasses (errors, functions, typed arrays) can mention that they are subclasses.

@bathos
Copy link
Contributor

bathos commented Apr 29, 2023

The derivation of both of these properties is described at ECMAScript Standard Buillt-in Objects. It does seem inconsistent that %AsyncFunction% and %TypedArray% [this was actually about the %TypedArray% constructors] redundantly declare their names in those preambles while e.g. %AsyncGeneratorFunction% doesn’t; it might be for emphasis given those WKIOs aren’t exposed by global properties, but it’s not done across the board. I would not expect %Date%, %RegExp%, %AggregateError% to mention it. %Date% specifies its length explicitly because it cannot be derived from the signature.

The “is a subclass of Function” stuff looks odd to me. Usually in the spec, “subclass” is used to refer to ES code functions where [[ClassKind]] = "derived". GeneratorFunction etc doesn’t fit that description.

@ljharb
Copy link
Member

ljharb commented Apr 29, 2023

Named and lengths should only be mentioned when they don’t match the implicit rules in the spec.

@Josh-Cena
Copy link
Contributor Author

To be clear, I'm not saying every function should mention its name/length, just that those that do should do it consistently, and those whose name can be inferred should probably not mention it.

@jmdyck
Copy link
Collaborator

jmdyck commented Apr 29, 2023

"name" property:

  • The AsyncFunction Constructor needs to specify the value of its "name" property because there isn't an "AsyncFunction" property on the global object, so the default "name" rule (specifically, the "functions that are specified as properties of objects" sentence) either doesn't apply or would give the wrong result. (Note that the relevant prose in the status quo is vague, but PR Editorial: Specify _prefix_ for non-specific uses of CreateBuiltinFunction #2833 will clarify it.)

    (And similarly for %Function.prototype%, %TypedArray%, %GeneratorFunction%, %AsyncGeneratorFunction%.)

  • For the TypedArray constructors, there are corresponding properties of the global object, so you could maybe argue that the default rules would work. However, I think this case (defining multiple constructors all at once) is weird enough that it's worth being more explicit about their "name" properties.

    (And similarly for NativeError constructors.)

  • Moreover, for all of the above, the has a "name" property sentence is a bullet in the "Properties of [whatever]" section, so that's fairly consistent.


"length" property:

I think the spec is pretty consistent in 'declaring' a "length" property only when necessary (i.e., when either its existence or value can't be inferred from general rules). The one exception I can see is %TypedArray% ( )

However, you're correct that these declarations take multiple forms. I'll propose a PR to make these consistent unless someone else is already working on one.


"is a subclass":

The spec has 3 statements that "X is a subclass of Y", namely that

  • the GeneratorFunction constructor,
  • the AsyncGeneratorFunction constructor, and
  • the AsyncFunction constructor

is each a subclass of Function.

It's unclear what this phrase means. If it just means that each of these 3 constructors has a [[Prototype]] whose value is %Function%, then we already say that in the respective Properties of the Foo Constructor sections, and I'd be inclined to drop the "is a subclass" bullet.

If it means something else, then we should probably define it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants