Skip to content

Call return() on the iterator when a for-of loop exits abruptly #2958

Open
@shicks

Description

@shicks

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

for (var x of y) {
  ...
}

to

for (var iter=$jscomp.makeIterator(y), key=iter.next(); !key.done; key=iter.next()) {
  let x = key.value;
  ...
}

which isn't so bad. We can handle break, continue, and return pretty easily by inserting the return call immediately before each, and it doesn't cost anything if you don't use them. But throw can be invisible (i.e. it's likely not coming from a THROW node in the block's AST), so the only way to handle it correctly is to wrap the whole thing in a catch:

for (var iter=$jscomp.makeIterator(y), key=iter.next(); !key.done; key=iter.next()) {
  let x = key.value;
  try {
    ...
  } catch (e) {
    if (iter.return) iter.return();
    throw e;
  }
}

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 additional try-finally around the return. If we're dealing with breaks as well, at this point, it probably makes sense to handle those at the same time, rather than mutating the AST inside the body:

try {
  for (var iter=$jscomp.makeIterator(y), key=iter.next(); !key.done; key=iter.next()) {
    let x = key.value;
    ...
  }
} catch (e) {
  key.thrown = {thrown: e};
} finally {
  try {
    if (!key.done && iter.return) iter.return();
  } finally {
    if (key.thrown) throw key.thrown.thrown;
  }
}

I estimate that this will add somewhere between 50 and 80 extra gzipped bytes to every for-of loop.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions