Track line and column numbers for expressions (#781)

Commit 73cc00e reduced the number of emitted source locations a great
deal but it resulted in at least one observable regression:

    export default async function f() {
         return "abc" + x
    }
    f() // ReferenceError should point to 2:20 but pointed to 1:1

Emit source locations for expressions again. Increases the average
number of source locations by about 15%. Non-scientifically tested
by counting source locations emitted when parsing the test suite
before and after.

No test because we currently cannot easily test stack traces coming
from module imports.

Fixes: https://github.com/quickjs-ng/quickjs/issues/779
This commit is contained in:
Ben Noordhuis 2025-01-05 22:20:43 +01:00 committed by GitHub
parent 5b609f15af
commit ac4cd17bb5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 9 additions and 8 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -20414,8 +20414,8 @@ static void emit_source_loc(JSParseState *s)
DynBuf *bc = &fd->byte_code; DynBuf *bc = &fd->byte_code;
dbuf_putc(bc, OP_source_loc); dbuf_putc(bc, OP_source_loc);
dbuf_put_u32(bc, s->last_line_num); dbuf_put_u32(bc, s->token.line_num);
dbuf_put_u32(bc, s->last_col_num); dbuf_put_u32(bc, s->token.col_num);
} }
static void emit_op(JSParseState *s, uint8_t val) static void emit_op(JSParseState *s, uint8_t val)
@ -24424,6 +24424,7 @@ static __exception int js_parse_expr_binary(JSParseState *s, int level,
} }
if (next_token(s)) if (next_token(s))
return -1; return -1;
emit_source_loc(s);
if (js_parse_expr_binary(s, level - 1, parse_flags)) if (js_parse_expr_binary(s, level - 1, parse_flags))
return -1; return -1;
emit_op(s, opcode); emit_op(s, opcode);

View file

@ -7,12 +7,12 @@ function test_exception_source_pos()
var e; var e;
try { try {
throw new Error(""); // line 10, column 15 throw new Error(""); // line 10, column 19
} catch(_e) { } catch(_e) {
e = _e; e = _e;
} }
assert(e.stack.includes("test_builtin.js:10:15")); assert(e.stack.includes("test_builtin.js:10:19"));
} }
// Keep this at the top; it tests source positions. // Keep this at the top; it tests source positions.
@ -36,7 +36,7 @@ function test_exception_prepare_stack()
}; };
try { try {
throw new Error(""); // line 39, column 15 throw new Error(""); // line 39, column 19
} catch(_e) { } catch(_e) {
e = _e; e = _e;
} }
@ -48,7 +48,7 @@ function test_exception_prepare_stack()
assert(f.getFunctionName(), 'test_exception_prepare_stack'); assert(f.getFunctionName(), 'test_exception_prepare_stack');
assert(f.getFileName().endsWith('test_builtin.js')); assert(f.getFileName().endsWith('test_builtin.js'));
assert(f.getLineNumber(), 39); assert(f.getLineNumber(), 39);
assert(f.getColumnNumber(), 15); assert(f.getColumnNumber(), 19);
assert(!f.isNative()); assert(!f.isNative());
} }
@ -64,7 +64,7 @@ function test_exception_stack_size_limit()
}; };
try { try {
throw new Error(""); // line 67, column 15 throw new Error(""); // line 67, column 19
} catch(_e) { } catch(_e) {
e = _e; e = _e;
} }
@ -77,7 +77,7 @@ function test_exception_stack_size_limit()
assert(f.getFunctionName(), 'test_exception_stack_size_limit'); assert(f.getFunctionName(), 'test_exception_stack_size_limit');
assert(f.getFileName().endsWith('test_builtin.js')); assert(f.getFileName().endsWith('test_builtin.js'));
assert(f.getLineNumber(), 67); assert(f.getLineNumber(), 67);
assert(f.getColumnNumber(), 15); assert(f.getColumnNumber(), 19);
assert(!f.isNative()); assert(!f.isNative());
} }