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

Await nullish async disposable #16408

Merged
merged 5 commits into from Apr 4, 2024

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Apr 3, 2024

Q                       A
Fixed Issues? When an async-disposable is nullish, Babel does not await its disposing process (REPL)
Patch: Bug Fix? Yes
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

In the REPL example,

(async function () {
  const log = [];

  function disposable(name) {
    return {
      async [Symbol.dispose || Symbol.for("Symbol.dispose")]() {
        log.push(name);
      },
    };
  }

  async function f() {
    using y = disposable("y");
    await using x = null;
    log.push("f body");
  }

  const promise = f();

  log.push("body");

  await promise;

  console.log(log)
})()

It should log "f body", "body", "y", but currently it logs "f body", "y", "body".

Per spec 7.5.7 Dispose, if hint is async-dispose, the result should be awaited, even if it is nullish. Therefore the disposing of the next resource y should follow log.push("body").

@JLHwung JLHwung added PR: Bug Fix 🐛 A type of pull request used for our changelog categories Spec: Explicit Resource Management labels Apr 3, 2024
@babel-bot
Copy link
Collaborator

babel-bot commented Apr 3, 2024

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/56669

@@ -39,6 +39,8 @@ export default function _usingCtx() {
throw new TypeError(`Property [Symbol.dispose] is not a function.`);
}
stack.push({ v: value, d: dispose, a: isAwait });
} else if (isAwait) {
stack.push({ a: isAwait });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explicitly set d to undefined, given that we read it later?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or set it to false to save a few bytes?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably use value here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything works for me as long as it's something :)

@JLHwung JLHwung force-pushed the await-nullish-async-disposable branch from 6ef1fe9 to 6600aec Compare April 4, 2024 14:15
@@ -39,6 +39,9 @@ export default function _usingCtx() {
throw new TypeError(`Property [Symbol.dispose] is not a function.`);
}
stack.push({ v: value, d: dispose, a: isAwait });
} else if (isAwait) {
// provide the nullish `value` as `d` for minification gain
stack.push({ d: value, a: isAwait });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside: currently await using x = document.all will go to this branch, we probably should reject it. I will check the spec and open a new PR to fix that.

@JLHwung JLHwung merged commit 3e96f03 into babel:main Apr 4, 2024
51 checks passed
@JLHwung JLHwung deleted the await-nullish-async-disposable branch April 4, 2024 14:37
@nicolo-ribaudo
Copy link
Member

There will be some changes related to this in the proposal: tc39/proposal-explicit-resource-management#219

We will have a conclusion at the end of the week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
PR: Bug Fix 🐛 A type of pull request used for our changelog categories Spec: Explicit Resource Management
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants