-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Call return() on the iterator when a for-of loop exits abruptly #2958
Comments
If we wrap the iterator in a wrapper that swallows any exception, we can get it down to try {
for (var iter=$jscomp.makeIterator(y), key=iter.next(); !key.done; key=iter.next()) {
let x = key.value;
...
}
} finally {
iter.close();
} with a little extra runtime library. This only amounts to 10-15 extra gzipped bytes, which is a lot better. |
Any update on this issue? Seems related to #3971. Here's is a simple repro I used to verify: $ npx google-closure-compiler --js test.js --js_output_file test.min.js --language_in ECMASCRIPT_NEXT --language_out ECMASCRIPT5 --assume_function_wrapper --compilation_level ADVANCED --third_party true --formatting PRETTY_PRINT --debug
$ node test.js
> return called: true
$ node test.min.js
> return called: false test.js:function first(xs) { test.min.js:function $$jscomp$arrayIteratorImpl$$($array$jscomp$6$$) { |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This breaks out one part of #2899.
I looked into this briefly today. In order to do it right, we end up bloating our for-of transpilation quite a bit. Currently we transpile
to
which isn't so bad. We can handle
break
,continue
, andreturn
pretty easily by inserting thereturn
call immediately before each, and it doesn't cost anything if you don't use them. Butthrow
can be invisible (i.e. it's likely not coming from aTHROW
node in the block's AST), so the only way to handle it correctly is to wrap the whole thing in a catch:Even this only gets most of the way there - if
return
throws then we end up with the wrong error thrown. To get that correct, we need an additionaltry-finally
around thereturn
. If we're dealing withbreak
s as well, at this point, it probably makes sense to handle those at the same time, rather than mutating the AST inside the body:I estimate that this will add somewhere between 50 and 80 extra gzipped bytes to every for-of loop.
The text was updated successfully, but these errors were encountered: