Skip to content

fix: emit source_loc around iterator_close so for/of return() stack traces report correct line - #1473

Merged
saghul merged 1 commit into
quickjs-ng:masterfrom
mvanhorn:fix/1266-emit-source-loc-around-iterator-close-so-for-of-re
May 19, 2026
Merged

saghul merged 1 commit into
quickjs-ng:masterfrom
mvanhorn:fix/1266-emit-source-loc-around-iterator-close-so-for-of-re

Conversation

@mvanhorn

Copy link
Copy Markdown
Contributor

Summary

When a for/of loop early-exits (break/return inside the body) saghul's iterator_close op runs to release the iterator. Any error thrown from the iterator's return() method was reported with the source_loc of the bytecode AFTER the loop, not the line of the actual for/of. Fix is to emit a source_loc op around iterator_close so the stack trace points at the loop.

Why this matters

Reporter pinpointed the exact OP and gave a minimal repro. The misleading line number made these errors very hard to diagnose, especially in generators where the iterator's return() can fail far from the call site.

Changes

  • quickjs.c::js_for_of_close_callback (and the bytecode emitter) - bracket iterator_close with source_loc ops carrying the line of the originating for/of.

Testing

make all && ./run-test262 -c tests/test262.conf clean. Added a script case under tests/bugs that asserts the stack trace line matches the for/of source line.

Fixes #1266

@saghul
saghul merged commit bcac5c2 into quickjs-ng:master May 19, 2026
124 checks passed
bnoordhuis pushed a commit that referenced this pull request Oct 6, 2026
The parser emitted OP_import with no OP_source_loc before it, so the pc
of an import() call, and of an await directly after it, mapped to the
last location recorded before the call. Save the position of the import
keyword and emit it right before OP_import, as #1473 did for the for-of
iterator close. gen/test_fib.c is regenerated because
examples/test_fib.js uses await import().

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Fixes: #1752
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Line numbers are off in for/of

3 participants