Skip to content

Commit e656451

Browse files
committed
feat: simplify some of the column number logic
1 parent db745cf commit e656451

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

src/core/parser.c

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9543,16 +9543,6 @@ static __exception int resolve_variables(JSContext *ctx, JSFunctionDef *s)
95439543

95449544
case OP_column_num:
95459545
column_num = get_u32(bc_buf + pos + 1);
9546-
while(
9547-
bc_buf[pos_next] == OP_column_num
9548-
&& column_num == get_u32(bc_buf + pos_next + 1)
9549-
) {
9550-
pos = pos_next;
9551-
op = bc_buf[pos];
9552-
len = opcode_info[op].size;
9553-
pos_next = pos + len;
9554-
}
9555-
95569546
s->column_number_size++;
95579547
goto no_change;
95589548
case OP_eval: /* convert scope index to adjusted variable index */
@@ -9861,11 +9851,13 @@ static void add_pc2col_info(JSFunctionDef *s, uint32_t pc, int column_num)
98619851
{
98629852
if(s->column_number_slots != NULL
98639853
&& s->column_number_count < s->column_number_size
9864-
&& pc >= s->column_number_last_pc) {
9854+
&& pc >= s->column_number_last_pc
9855+
&& column_num != s->column_number_last) {
98659856
s->column_number_slots[s->column_number_count].pc = pc;
98669857
s->column_number_slots[s->column_number_count].column_num = column_num;
98679858
s->column_number_count++;
98689859
s->column_number_last_pc = pc;
9860+
s->column_number_last = column_num;
98699861
}
98709862
}
98719863

@@ -10149,6 +10141,7 @@ static __exception int resolve_labels(JSContext *ctx, JSFunctionDef *s)
1014910141
s->column_number_slots = js_mallocz(s->ctx, sizeof(*s->column_number_slots) * s->column_number_size);
1015010142
if(s->column_number_slots == NULL)
1015110143
return -1;
10144+
s->column_number_last = s->column_num;
1015210145
s->column_number_last_pc = 0;
1015310146
}
1015410147

src/core/parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ typedef struct JSFunctionDef {
319319
int column_number_size;
320320
int column_number_count;
321321
int column_number_last_pc;
322+
int column_number_last;
322323

323324
/* pc2line table */
324325
JSAtom filename;

src/core/runtime.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1180,18 +1180,17 @@ void build_backtrace(JSContext* ctx, JSValueConst error_obj, const char* filenam
11801180
if (b->has_debug) {
11811181
/* find line and column, default to 1:1 */
11821182
line_num = find_line_num(ctx, b, sf->cur_pc - b->byte_code_buf - 1);
1183-
line_num = line_num == -1 ? 1 : line_num;
1183+
line_num = line_num == -1 ? b->debug.line_num : line_num;
11841184
column_num = find_column_num(ctx, b, sf->cur_pc - b->byte_code_buf - 1);
1185-
column_num = column_num == -1 ? 1 : column_num + 1;
1185+
column_num = column_num == -1 ? b->debug.column_num : column_num + 1;
11861186
atom_str = JS_AtomToCString(ctx, b->debug.filename);
11871187
dbuf_printf(&dbuf, " (%s", atom_str ? atom_str : "<null>");
11881188
JS_FreeCString(ctx, atom_str);
1189-
if (line_num != -1){
1189+
if (line_num != -1) {
11901190
dbuf_printf(&dbuf, ":%d", line_num);
1191-
}
1192-
1193-
if (column_num != -1){
1194-
dbuf_printf(&dbuf, ":%d", column_num);
1191+
if (column_num != -1) {
1192+
dbuf_printf(&dbuf, ":%d", column_num);
1193+
}
11951194
}
11961195

11971196
dbuf_putc(&dbuf, ')');
@@ -1216,8 +1215,13 @@ void build_backtrace(JSContext* ctx, JSValueConst error_obj, const char* filenam
12161215

12171216
dbuf_free(&dbuf);
12181217
JS_DefinePropertyValue(ctx, error_obj, JS_ATOM_stack, str, JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
1219-
JS_DefinePropertyValue(ctx, error_obj, JS_ATOM_lineNumber, JS_NewInt32(ctx, line_num), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
1220-
JS_DefinePropertyValue(ctx, error_obj, JS_ATOM_columnNumber, JS_NewInt32(ctx, column_num), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
1218+
1219+
if (line_num != -1) {
1220+
JS_DefinePropertyValue(ctx, error_obj, JS_ATOM_lineNumber, JS_NewInt32(ctx, line_num), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
1221+
if (column_num != -1) {
1222+
JS_DefinePropertyValue(ctx, error_obj, JS_ATOM_columnNumber, JS_NewInt32(ctx, column_num), JS_PROP_WRITABLE | JS_PROP_CONFIGURABLE);
1223+
}
1224+
}
12211225
}
12221226

12231227
/* Note: it is important that no exception is returned by this function */

0 commit comments

Comments
 (0)