From ac4cd17bb5415adf067fe4ac48c6f5eafb7558e5 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sun, 5 Jan 2025 22:20:43 +0100 Subject: [PATCH] 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 --- gen/function_source.c | Bin 2892 -> 2928 bytes gen/hello_module.c | Bin 3961 -> 4035 bytes gen/test_fib.c | Bin 2840 -> 2864 bytes quickjs.c | 5 +++-- tests/test_builtin.js | 12 ++++++------ 5 files changed, 9 insertions(+), 8 deletions(-) diff --git a/gen/function_source.c b/gen/function_source.c index f486ee566f65eeee240fb25ecc3aec3a7b569821..489dfa28a0859d9ef0ff5eea9f43818caeca2b1c 100644 GIT binary patch delta 125 zcmX>j_CahyJCl*Y#7=b}xp9*gJCmWwu#WpPLbYcT+mCSaQkAf|(bj7>lih7dMH QQ5r}DF3z=CoF|DH0N|c69smFU delta 125 zcmX>s|5I*46O)C}GUOd45$H5!A>GBgKCm>Gdcpt+k9d6Jj`BbOqP diff --git a/gen/test_fib.c b/gen/test_fib.c index efa478d0168e4173d275def5fd55d048d66981f3..28265006375d7d97937e33f407fa0a9210c6abb0 100644 GIT binary patch delta 134 zcmbOswn1z{qq2!L7gutAUU7*+X=YxDMSO`uVODW+d`W6?NqkynQnZn!$;Krc*n(1Z y6bvd%40X7GlyNeMV_*OxjV-}!GcY>|Oqzg5Lx`*aSc5?tx`a`p&gMS$E@lADX(iDB delta 110 zcmdlWHbZPeqq2cD7gutAUU7*+X=YxDMSO`uVODW+d`W6?NqkynQnZn!!Nw&U*sKzD u6bvd140O1Fl!+;rV*w(~l0l@g8HhAZ0*e@dNT5<6X_TnLwRstP7c&5gj37J! diff --git a/quickjs.c b/quickjs.c index c534a26..9e53701 100644 --- a/quickjs.c +++ b/quickjs.c @@ -20414,8 +20414,8 @@ static void emit_source_loc(JSParseState *s) DynBuf *bc = &fd->byte_code; dbuf_putc(bc, OP_source_loc); - dbuf_put_u32(bc, s->last_line_num); - dbuf_put_u32(bc, s->last_col_num); + dbuf_put_u32(bc, s->token.line_num); + dbuf_put_u32(bc, s->token.col_num); } 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)) return -1; + emit_source_loc(s); if (js_parse_expr_binary(s, level - 1, parse_flags)) return -1; emit_op(s, opcode); diff --git a/tests/test_builtin.js b/tests/test_builtin.js index 2211f55..c6ea5bb 100644 --- a/tests/test_builtin.js +++ b/tests/test_builtin.js @@ -7,12 +7,12 @@ function test_exception_source_pos() var e; try { - throw new Error(""); // line 10, column 15 + throw new Error(""); // line 10, column 19 } catch(_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. @@ -36,7 +36,7 @@ function test_exception_prepare_stack() }; try { - throw new Error(""); // line 39, column 15 + throw new Error(""); // line 39, column 19 } catch(_e) { e = _e; } @@ -48,7 +48,7 @@ function test_exception_prepare_stack() assert(f.getFunctionName(), 'test_exception_prepare_stack'); assert(f.getFileName().endsWith('test_builtin.js')); assert(f.getLineNumber(), 39); - assert(f.getColumnNumber(), 15); + assert(f.getColumnNumber(), 19); assert(!f.isNative()); } @@ -64,7 +64,7 @@ function test_exception_stack_size_limit() }; try { - throw new Error(""); // line 67, column 15 + throw new Error(""); // line 67, column 19 } catch(_e) { e = _e; } @@ -77,7 +77,7 @@ function test_exception_stack_size_limit() assert(f.getFunctionName(), 'test_exception_stack_size_limit'); assert(f.getFileName().endsWith('test_builtin.js')); assert(f.getLineNumber(), 67); - assert(f.getColumnNumber(), 15); + assert(f.getColumnNumber(), 19); assert(!f.isNative()); }