Skip to content

Record the source location of import() calls - #1755

Merged
bnoordhuis merged 2 commits into
quickjs-ng:masterfrom
yspbwx2010:import-source-location
Oct 6, 2026
Merged

bnoordhuis merged 2 commits into
quickjs-ng:masterfrom
yspbwx2010:import-source-location

Conversation

@yspbwx2010

Copy link
Copy Markdown
Contributor

The parser emitted OP_import with no OP_source_loc before it, so the pc of an import() call mapped to the last location recorded before it. The pc of an await directly after it mapped to the same place. That location could be the start of the statement, an earlier call, or the function itself.

Two kinds of errors showed it. The first is code that import() runs synchronously, such as the specifier's toString() or a getter on the options object. It captures the caller's frame at the import() call. The second is when import() rejects without running user code, for example because the options argument or its with property is not an object, or an attribute value is not a string. That TypeError has no stack until the caller's await rethrows it.

This change saves the position of the import keyword and emits it right before OP_import. It follows the same pattern as the for-of iterator close fix in #1473. import.meta is not affected.

examples/test_fib.js uses await import(), so make codegen now adds a pc2line entry to gen/test_fib.c. The regenerated file is included so the codegen check stays clean.

The new test, tests/test_dynamic_import_line_numbers.js, covers four cases:

  1. The import() is in a declaration at the start of the function. It used to report the function header.
  2. The import() follows a call on the previous line. It used to report that call.
  3. The import() is in an expression statement. It used to report the column of await.
  4. A getter on the options object throws, with no await involved. It used to report the call on the previous line.

Testing: make test passes with the change applied. Without the quickjs.c change the new test fails with got |11,1|, expected |13,21|.

Fixes: #1752

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 quickjs-ng#1473 did for the for-of
iterator close. gen/test_fib.c is regenerated because
examples/test_fib.js uses await import().

Fixes: quickjs-ng#1752

Co-Authored-By: Claude Opus 5.5 <[email protected]>

@bnoordhuis bnoordhuis left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM modulo a comment about a comment.

Comment on lines +3 to +10
/* A stack frame in a function that calls import() must point at the
import() call, not at the start of the statement, an earlier call in the
same function or the function itself. import() can run user code
synchronously, such as a getter on the options object, which captures the
frame while import() executes. It can also reject without a stack, for
example on a bad options argument, and the TypeError then gets its stack
when the await rethrows it. */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
/* A stack frame in a function that calls import() must point at the
import() call, not at the start of the statement, an earlier call in the
same function or the function itself. import() can run user code
synchronously, such as a getter on the options object, which captures the
frame while import() executes. It can also reject without a stack, for
example on a bad options argument, and the TypeError then gets its stack
when the await rethrows it. */

Superfluous LLM comment, please remove.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, removed, and shifted the expected line numbers to match.

The expected line numbers move up by eight to match.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@bnoordhuis
bnoordhuis merged commit a6b82a3 into quickjs-ng:master Oct 6, 2026
128 checks passed
@yspbwx2010
yspbwx2010 deleted the import-source-location branch October 9, 2026 07:41
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.

import() calls record no source location, so errors point at an earlier line or the function header

2 participants