Skip to content

Commit 1dbce7d

Browse files
committed
Align SyntaxError reporting with other JS engines.
Previously, "in <file>:<file>" was a part of the error message. Now the file name is reported as "stack" property. This fixes #1005 issue on Github.
1 parent 0b9cf98 commit 1dbce7d

File tree

5 files changed

+430
-412
lines changed

5 files changed

+430
-412
lines changed

src/njs_error.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,34 @@ njs_error_stack_attach(njs_vm_t *vm, njs_value_t value, njs_uint_t skip)
217217
}
218218

219219

220+
void
221+
njs_error_stack_set(njs_vm_t *vm, njs_value_t *value, njs_str_t *file,
222+
njs_int_t line)
223+
{
224+
njs_chb_t chain;
225+
njs_value_t *stackval;
226+
227+
if (njs_slow_path(!vm->options.backtrace
228+
|| !njs_is_error(value))
229+
|| njs_object(value)->stack_attached)
230+
{
231+
return;
232+
}
233+
234+
NJS_CHB_MP_INIT(&chain, vm->mem_pool);
235+
236+
njs_chb_sprintf(&chain, njs_length(" at :\n") + NJS_INT_T_LEN
237+
+ file->length,
238+
" at %V:%uD\n", file, line);
239+
240+
stackval = njs_object_value(value);
241+
242+
(void) njs_string_create_chb(vm, stackval, &chain);
243+
244+
njs_chb_destroy(&chain);
245+
}
246+
247+
220248
njs_int_t
221249
njs_error_stack(njs_vm_t *vm, njs_value_t *value, njs_value_t *stack)
222250
{

src/njs_error.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ njs_int_t njs_error_to_string(njs_vm_t *vm, njs_value_t *retval,
4444
const njs_value_t *error);
4545
njs_int_t njs_error_stack(njs_vm_t *vm, njs_value_t *value, njs_value_t *stack);
4646
void njs_error_stack_attach(njs_vm_t *vm, njs_value_t value, njs_uint_t skip);
47+
void njs_error_stack_set(njs_vm_t *vm, njs_value_t *value, njs_str_t *file,
48+
njs_int_t line);
4749

4850

4951
extern const njs_object_type_init_t njs_error_type_init;

src/njs_parser.c

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9205,11 +9205,9 @@ static void
92059205
njs_parser_error(njs_vm_t *vm, njs_object_type_t type, njs_str_t *file,
92069206
uint32_t line, const char *fmt, va_list args)
92079207
{
9208-
size_t width;
92099208
u_char msg[NJS_MAX_ERROR_STR];
92109209
u_char *p, *end;
9211-
njs_int_t ret;
9212-
njs_value_t value, error;
9210+
njs_value_t error;
92139211

92149212
if (njs_slow_path(vm->top_frame == NULL)) {
92159213
njs_vm_runtime_init(vm);
@@ -9220,29 +9218,10 @@ njs_parser_error(njs_vm_t *vm, njs_object_type_t type, njs_str_t *file,
92209218

92219219
p = njs_vsprintf(p, end, fmt, args);
92229220

9223-
width = njs_length(" in ") + file->length + NJS_INT_T_LEN;
9224-
9225-
if (p > end - width) {
9226-
p = end - width;
9227-
}
9228-
9229-
if (file->length != 0 && !vm->options.quiet) {
9230-
p = njs_sprintf(p, end, " in %V:%uD", file, line);
9231-
9232-
} else {
9233-
p = njs_sprintf(p, end, " in %uD", line);
9234-
}
9235-
92369221
njs_error_new(vm, &error, njs_vm_proto(vm, type), msg, p - msg);
92379222

9238-
njs_set_number(&value, line);
9239-
njs_value_property_set(vm, &error, NJS_ATOM_STRING_lineNumber, &value);
9240-
9241-
if (file->length != 0) {
9242-
ret = njs_string_create(vm, &value, file->start, file->length);
9243-
if (ret == NJS_OK) {
9244-
njs_value_property_set(vm, &error, NJS_ATOM_STRING_fileName, &value);
9245-
}
9223+
if (!vm->options.quiet && file->length != 0) {
9224+
njs_error_stack_set(vm, &error, file, line);
92469225
}
92479226

92489227
njs_vm_throw(vm, &error);

0 commit comments

Comments
 (0)