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

How should using const interact with Script? #66

Closed
rbuckton opened this issue Sep 20, 2021 · 8 comments
Closed

How should using const interact with Script? #66

rbuckton opened this issue Sep 20, 2021 · 8 comments

Comments

@rbuckton
Copy link
Collaborator

Per ScriptEvaluation, scripts are evaluated with the LexicalEnvironment set to globalEnv. That means that all using const bindings at the top level of a script would also be tracked in globalEnv, which in turn means that they would persist as a value between on script and the next. This leaves us with three options:

  1. Dispose tracked resources at the end of ScriptEvaluation.
  2. Dispose tracked resources after all scripts are evaluated (its not clear in the spec if/where this happens, however).
  3. Disallow using const at the top level of a Script (i.e., when not inside of a Block or other new lexical environment).

(1) seems awkward, because the value persists beyond its scoped lifetime. (2) seems ambiguous and possibly not feasible. (3) seems unfortunate, but probably the safest option.

CC: @wycats, @waldemarhorwat as the currently signed on reviewers.

@ljharb
Copy link
Member

ljharb commented Sep 20, 2021

Why couldn't using const declarations only be in scope for that Script? iow, why can't they be different than const in that scenario?

@rbuckton
Copy link
Collaborator Author

I've been looking at ScriptEvaluation and its not clear that const is per-script:

image

I was under the impression that let/const was per-script, but cannot find where that occurs in the spec.

@rbuckton rbuckton changed the title How should using const interact with _Script_? How should using const interact with Script? Sep 21, 2021
@jridgewell
Copy link
Member

The two Runtime Semantics are different:

var BindingIdentifier = Initializer
…
5. Return ? PutValue(lhs, value).

PutValue falls back to putting the reference into the globalObj. This is why variables are accessible between scripts.

let BindingIdentifier = Initializer
…
5. Return InitializeReferencedBinding(lhs, value).

InitializeReferencedBinding only inserts into the environment record.

@rbuckton
Copy link
Collaborator Author

However the same environment record is used for successive calls to ScriptEvaluation in the same Realm, per the steps above. If a const was per-script, I would expect a per-script Environment Record to be set for the LexicalEnvironment of scriptContext.

Other than by way of a closure, in { using const a = ...; }, a is unreachable outside of the block. In a Module, the declaration is at least scoped to the module body. For a Script, the resource would be addressable from subsequent Script evaluations in the same Realm, with access to a (potentially disposed) resource.

@rbuckton

This comment has been minimized.

@rbuckton
Copy link
Collaborator Author

@ljharb, my apologies, I misread.

Why couldn't using const declarations only be in scope for that Script? iow, why can't they be different than const in that scenario?

(emphasis added)

I'm concerned it would require a lot of additional machinery in the spec to introduce using const as something script-local, when const itself isn't. This seems like an artificial distinction that could cause more confusion than it's worth.

@rbuckton
Copy link
Collaborator Author

I'm currently leaning towards "Ban at the top level of Script" as the safest bet, since a using const requires some kind of block scope and a Script has no implicit block scope (only global scope). Its also something we could chose to relax later, vs. introducing semantics that could be wrong or confusing that we can't change later.

@rbuckton
Copy link
Collaborator Author

rbuckton commented Sep 7, 2022

I discussed this at the October, 2021 TC39 meeting, with the proposed semantics being to ban using at the top level of a Script. There did not seem to be an objection at that time, so I am considering this issue closed.

@rbuckton rbuckton closed this as completed Sep 7, 2022
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

3 participants