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

Close for-of iterator when the loop body throws #3734

Merged
merged 1 commit into from
Mar 12, 2024
Merged

Conversation

raskad
Copy link
Member

@raskad raskad commented Mar 12, 2024

Currently the iterator used in a for-of loop is not closed when the loop body throws. This PR fixes that by widening the scope of an exception handler that was already in place for some of the for-of assignment code paths.

To make this work I had to adjust the iterator close opcodes somewhat. The reason is that we may run into a situation when we attempt to close an iterator two times. See the following example:

var iterable = {};
iterable[Symbol.iterator] = function() {
  return {
    next: function() {
      return { done: false, value: null };
    },
    return: function() {
      throw "iter-close-error";
    }
  };
};

function f() {
  for (var x of iterable) {
    return;
  }
}
f();

In this example we close the iterator when we return from f inside of the loop body. The iterator close code is injected during the compilation of the return statement. The problem is that if the iterable.return function throws, like in the example, we start running the exception handler code that also attempts to close the iterator. To avoid this I adjusted the iterator close opcodes.

@raskad raskad added bug Something isn't working execution Issues or PRs related to code execution labels Mar 12, 2024
@raskad raskad added this to the v0.18.1 milestone Mar 12, 2024
Copy link

Test262 conformance changes

Test result main count PR count difference
Total 50,268 50,268 0
Passed 42,769 42,771 +2
Ignored 1,391 1,391 0
Failed 6,108 6,106 -2
Panics 0 0 0
Conformance 85.08% 85.09% +0.00%
Fixed tests (2):
test/language/statements/for-of/generator-close-via-throw.js (previously Failed)
test/language/statements/for-of/iterator-close-via-throw.js (previously Failed)

@raskad raskad requested a review from a team March 12, 2024 02:07
Copy link
Member

@HalidOdat HalidOdat left a comment

Choose a reason for hiding this comment

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

Nice catch! :)

@HalidOdat HalidOdat requested a review from a team March 12, 2024 16:08
@jedel1043 jedel1043 added this pull request to the merge queue Mar 12, 2024
Merged via the queue into main with commit 861010e Mar 12, 2024
13 checks passed
@raskad raskad deleted the fix-for-of-throw branch March 12, 2024 22:53
@raskad raskad modified the milestones: v0.18.1, v0.19.0 Jul 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working execution Issues or PRs related to code execution
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants