Add for await of
Summary
This PR introduces support for the for await of syntax through IR. In detail:
- The helper function
_makeAsyncIteratoris 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
genAsyncForOfStatementwhich 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 ofloop
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!
Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!
@avp Hi! Do you have any feedback on this PR? Thank you 😄
@avp has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.
@avp I just noticed we also need to define Symbol.asyncIterator as it's undefined in hermes.
Edit: Added 👍
@trossimel-sc has updated the pull request. You must reimport the pull request before landing.
@avp has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.
Hi, we've been reviewing this carefully and there's two issues we've identified that need to be handled before this can land.
-
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
-
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 thereturnmethod gets awaited before continuing to execute any code after the loop.
Thanks!
@trossimel-sc has updated the pull request. You must reimport the pull request before landing.
@trossimel-sc has updated the pull request. You must reimport the pull request before landing.
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
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.
@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!
@trossimel-sc has updated the pull request. You must reimport the pull request before landing.
@avp Thank you!
@trossimel-sc has updated the pull request. You must reimport the pull request before landing.
@avp has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.