hermes icon indicating copy to clipboard operation
hermes copied to clipboard

Add for await of

Open trossimel-sc opened this issue 1 year ago • 2 comments

Summary

This PR introduces support for the for await of syntax through IR. In detail:

  • The helper function _makeAsyncIterator is used to create an async iterator for a given iterable. It checks if an async iterator method is available; if not, it creates an async iterator from a synchronous iterator by wrapping its results in promises.
  • Added a genAsyncForOfStatement which handles the for await of loop by iterating over the async iterator object. It follows the same rules described here

Test Plan

  • Added 3 unit tests to cover the main behavior of for await of loop

trossimel-sc avatar Nov 04 '24 11:11 trossimel-sc

Hi @trossimel-sc!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

facebook-github-bot avatar Nov 04 '24 11:11 facebook-github-bot

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

facebook-github-bot avatar Jan 17 '25 21:01 facebook-github-bot

@avp Hi! Do you have any feedback on this PR? Thank you 😄

trossimel-sc avatar Mar 18 '25 18:03 trossimel-sc

@avp has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

facebook-github-bot avatar Mar 31 '25 16:03 facebook-github-bot

@avp I just noticed we also need to define Symbol.asyncIterator as it's undefined in hermes.

Edit: Added 👍

trossimel-sc avatar Apr 09 '25 08:04 trossimel-sc

@trossimel-sc has updated the pull request. You must reimport the pull request before landing.

facebook-github-bot avatar Apr 09 '25 09:04 facebook-github-bot

@avp has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

facebook-github-bot avatar Apr 09 '25 16:04 facebook-github-bot

Hi, we've been reviewing this carefully and there's two issues we've identified that need to be handled before this can land.

  1. This needs to handle block scoping (with TDZ, etc). Synchronous for-of handles this, so you should just need to transplant those changes. Here's a diff that adds that as well as a test that it works: https://gist.github.com/avp/798a795f798fb589eb48ae58bd5e51de

  2. Closing the async iterator requires an await. Looks like when an async for-of ends, it needs to invoke the AsyncIteratorClose operation, which in step 4.d. awaits its result. We need to make sure the Promise given back by the return method gets awaited before continuing to execute any code after the loop.

Thanks!

avp avatar Apr 25 '25 23:04 avp

@trossimel-sc has updated the pull request. You must reimport the pull request before landing.

facebook-github-bot avatar May 02 '25 10:05 facebook-github-bot

@trossimel-sc has updated the pull request. You must reimport the pull request before landing.

facebook-github-bot avatar May 02 '25 10:05 facebook-github-bot

Thanks @avp, One last thing: do you know how to address IR errors that occur after adding genYieldOrAwaitExpr as part of the try/catch block in _emitIteratorCloseImpl ? I didn't expect it to fail

trossimel-sc avatar May 02 '25 10:05 trossimel-sc

genYieldOrAwait results in returns when special generator instructions are lowered out, so it's possible the problem is that there's issues with not ending the Try before returning.

avp avatar May 08 '25 16:05 avp

@trossimel-sc Looks like your IRVerifier problems are the result of a slight oversight in emitTryCatchScaffolding's body callback in _emitIteratorCloseImpl.

The old version of the code didn't instantiate a SurroundingTry even though it's technically required to. This just wasn't an observable problem in any way because there was nothing inside to read the SurroundingTry. Now that this PR adds more logic inside that body callback, it may be read so it must be instantiated.

So, much like for your other call to emitTryCatchScaffolding, you must instantiate a SurroundingTry at the top of the body callback and that should fix your IRVerifier errors.

Thanks!

avp avatar May 12 '25 22:05 avp

@trossimel-sc has updated the pull request. You must reimport the pull request before landing.

facebook-github-bot avatar May 13 '25 08:05 facebook-github-bot

@avp Thank you!

trossimel-sc avatar May 13 '25 08:05 trossimel-sc

@trossimel-sc has updated the pull request. You must reimport the pull request before landing.

facebook-github-bot avatar May 14 '25 08:05 facebook-github-bot

@avp has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

facebook-github-bot avatar May 15 '25 23:05 facebook-github-bot