Skip to content

Commit ac4cd17

Browse files
authored
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: #779
1 parent 5b609f1 commit ac4cd17

File tree

5 files changed

+29
-26
lines changed

5 files changed

+29
-26
lines changed

gen/function_source.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
#include "quickjs-libc.h"
44

5-
const uint32_t qjsc_function_source_size = 314;
5+
const uint32_t qjsc_function_source_size = 320;
66

7-
const uint8_t qjsc_function_source[314] = {
7+
const uint8_t qjsc_function_source[320] = {
88
0x13, 0x05, 0x01, 0x30, 0x74, 0x65, 0x73, 0x74,
99
0x73, 0x2f, 0x66, 0x75, 0x6e, 0x63, 0x74, 0x69,
1010
0x6f, 0x6e, 0x5f, 0x73, 0x6f, 0x75, 0x72, 0x63,
@@ -42,9 +42,9 @@ const uint8_t qjsc_function_source[314] = {
4242
0x00, 0xb0, 0xeb, 0x0b, 0x39, 0x95, 0x00, 0x00,
4343
0x00, 0x63, 0x02, 0x00, 0xf0, 0x30, 0x69, 0x02,
4444
0x00, 0x69, 0x01, 0x00, 0x06, 0x2f, 0xc0, 0x03,
45-
0x01, 0x01, 0x0e, 0x00, 0x1c, 0x0a, 0x2a, 0x5d,
46-
0x18, 0x00, 0x10, 0x08, 0x25, 0x76, 0x0e, 0x5d,
47-
0x18, 0x00,
45+
0x01, 0x01, 0x14, 0x00, 0x1c, 0x0a, 0x2a, 0x26,
46+
0x03, 0x39, 0x28, 0x00, 0x10, 0x08, 0x27, 0x11,
47+
0x12, 0x67, 0x0d, 0x26, 0x03, 0x39, 0x28, 0x00,
4848
};
4949

5050
static JSContext *JS_NewCustomContext(JSRuntime *rt)

gen/hello_module.c

+10-8
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
#include "quickjs-libc.h"
44

5-
const uint32_t qjsc_fib_module_size = 282;
5+
const uint32_t qjsc_fib_module_size = 290;
66

7-
const uint8_t qjsc_fib_module[282] = {
7+
const uint8_t qjsc_fib_module[290] = {
88
0x13, 0x03, 0x01, 0x2c, 0x65, 0x78, 0x61, 0x6d,
99
0x70, 0x6c, 0x65, 0x73, 0x2f, 0x66, 0x69, 0x62,
1010
0x5f, 0x6d, 0x6f, 0x64, 0x75, 0x6c, 0x65, 0x2e,
@@ -19,8 +19,9 @@ const uint8_t qjsc_fib_module[282] = {
1919
0xb5, 0xa8, 0xeb, 0x03, 0xb5, 0x28, 0xd2, 0xb6,
2020
0xad, 0xeb, 0x03, 0xb6, 0x28, 0xde, 0xd2, 0xb6,
2121
0x9f, 0xf0, 0xde, 0xd2, 0xb7, 0x9f, 0xf0, 0x9e,
22-
0x28, 0xc0, 0x03, 0x02, 0x08, 0x06, 0x00, 0x0f,
23-
0x0e, 0x10, 0x1b, 0x1a, 0x8d, 0x01, 0x66, 0x75,
22+
0x28, 0xc0, 0x03, 0x02, 0x08, 0x0e, 0x09, 0x0c,
23+
0x27, 0x0a, 0x28, 0x02, 0x07, 0x08, 0x11, 0x0a,
24+
0x07, 0x08, 0x07, 0x08, 0x8d, 0x01, 0x66, 0x75,
2425
0x6e, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x20, 0x66,
2526
0x69, 0x62, 0x28, 0x6e, 0x29, 0x0a, 0x7b, 0x0a,
2627
0x20, 0x20, 0x20, 0x20, 0x69, 0x66, 0x20, 0x28,
@@ -43,9 +44,9 @@ const uint8_t qjsc_fib_module[282] = {
4344
0x00, 0x00,
4445
};
4546

46-
const uint32_t qjsc_hello_module_size = 183;
47+
const uint32_t qjsc_hello_module_size = 187;
4748

48-
const uint8_t qjsc_hello_module[183] = {
49+
const uint8_t qjsc_hello_module[187] = {
4950
0x13, 0x07, 0x01, 0x30, 0x65, 0x78, 0x61, 0x6d,
5051
0x70, 0x6c, 0x65, 0x73, 0x2f, 0x68, 0x65, 0x6c,
5152
0x6c, 0x6f, 0x5f, 0x6d, 0x6f, 0x64, 0x75, 0x6c,
@@ -67,8 +68,9 @@ const uint8_t qjsc_hello_module[183] = {
6768
0x00, 0x00, 0x00, 0x43, 0xe4, 0x00, 0x00, 0x00,
6869
0x04, 0xe6, 0x00, 0x00, 0x00, 0x66, 0x00, 0x00,
6970
0xbd, 0x0a, 0xf0, 0x24, 0x02, 0x00, 0x0e, 0x06,
70-
0x2f, 0xc0, 0x03, 0x01, 0x01, 0x08, 0x18, 0x28,
71-
0x36, 0x17, 0x62, 0x00, 0x2a, 0x20, 0x00,
71+
0x2f, 0xc0, 0x03, 0x01, 0x01, 0x0c, 0x00, 0x04,
72+
0x08, 0x00, 0x34, 0x10, 0x30, 0x0f, 0x34, 0x10,
73+
0x2a, 0x20, 0x00,
7274
};
7375

7476
static JSContext *JS_NewCustomContext(JSRuntime *rt)

gen/test_fib.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
#include "quickjs-libc.h"
44

5-
const uint32_t qjsc_test_fib_size = 290;
5+
const uint32_t qjsc_test_fib_size = 294;
66

7-
const uint8_t qjsc_test_fib[290] = {
7+
const uint8_t qjsc_test_fib[294] = {
88
0x13, 0x0e, 0x01, 0x28, 0x65, 0x78, 0x61, 0x6d,
99
0x70, 0x6c, 0x65, 0x73, 0x2f, 0x74, 0x65, 0x73,
1010
0x74, 0x5f, 0x66, 0x69, 0x62, 0x2e, 0x6a, 0x73,
@@ -39,9 +39,9 @@ const uint8_t qjsc_test_fib[290] = {
3939
0xea, 0x00, 0x00, 0x00, 0x43, 0xeb, 0x00, 0x00,
4040
0x00, 0x04, 0xed, 0x00, 0x00, 0x00, 0x66, 0x02,
4141
0x00, 0xbd, 0x0a, 0xf0, 0x24, 0x02, 0x00, 0x0e,
42-
0x06, 0x2f, 0xc0, 0x03, 0x01, 0x01, 0x0a, 0x00,
43-
0x45, 0x08, 0x6c, 0x36, 0x5b, 0x62, 0x00, 0x2a,
44-
0x20, 0x00,
42+
0x06, 0x2f, 0xc0, 0x03, 0x01, 0x01, 0x0e, 0x41,
43+
0x3c, 0x00, 0x39, 0x06, 0x3b, 0x34, 0x10, 0x30,
44+
0x0f, 0x34, 0x10, 0x2a, 0x20, 0x00,
4545
};
4646

4747
static JSContext *JS_NewCustomContext(JSRuntime *rt)

quickjs.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -20414,8 +20414,8 @@ static void emit_source_loc(JSParseState *s)
2041420414
DynBuf *bc = &fd->byte_code;
2041520415

2041620416
dbuf_putc(bc, OP_source_loc);
20417-
dbuf_put_u32(bc, s->last_line_num);
20418-
dbuf_put_u32(bc, s->last_col_num);
20417+
dbuf_put_u32(bc, s->token.line_num);
20418+
dbuf_put_u32(bc, s->token.col_num);
2041920419
}
2042020420

2042120421
static void emit_op(JSParseState *s, uint8_t val)
@@ -24424,6 +24424,7 @@ static __exception int js_parse_expr_binary(JSParseState *s, int level,
2442424424
}
2442524425
if (next_token(s))
2442624426
return -1;
24427+
emit_source_loc(s);
2442724428
if (js_parse_expr_binary(s, level - 1, parse_flags))
2442824429
return -1;
2442924430
emit_op(s, opcode);

tests/test_builtin.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ function test_exception_source_pos()
77
var e;
88

99
try {
10-
throw new Error(""); // line 10, column 15
10+
throw new Error(""); // line 10, column 19
1111
} catch(_e) {
1212
e = _e;
1313
}
1414

15-
assert(e.stack.includes("test_builtin.js:10:15"));
15+
assert(e.stack.includes("test_builtin.js:10:19"));
1616
}
1717

1818
// Keep this at the top; it tests source positions.
@@ -36,7 +36,7 @@ function test_exception_prepare_stack()
3636
};
3737

3838
try {
39-
throw new Error(""); // line 39, column 15
39+
throw new Error(""); // line 39, column 19
4040
} catch(_e) {
4141
e = _e;
4242
}
@@ -48,7 +48,7 @@ function test_exception_prepare_stack()
4848
assert(f.getFunctionName(), 'test_exception_prepare_stack');
4949
assert(f.getFileName().endsWith('test_builtin.js'));
5050
assert(f.getLineNumber(), 39);
51-
assert(f.getColumnNumber(), 15);
51+
assert(f.getColumnNumber(), 19);
5252
assert(!f.isNative());
5353
}
5454

@@ -64,7 +64,7 @@ function test_exception_stack_size_limit()
6464
};
6565

6666
try {
67-
throw new Error(""); // line 67, column 15
67+
throw new Error(""); // line 67, column 19
6868
} catch(_e) {
6969
e = _e;
7070
}
@@ -77,7 +77,7 @@ function test_exception_stack_size_limit()
7777
assert(f.getFunctionName(), 'test_exception_stack_size_limit');
7878
assert(f.getFileName().endsWith('test_builtin.js'));
7979
assert(f.getLineNumber(), 67);
80-
assert(f.getColumnNumber(), 15);
80+
assert(f.getColumnNumber(), 19);
8181
assert(!f.isNative());
8282
}
8383

0 commit comments

Comments
 (0)