Include file path in Syntax error messages (#2941)
When using ES6 modules it was not possible to identify which module an error originates from. This PR changes the error message to also include the file path using the file:line:column format, and updates the source context printing for unhandled exceptions to use the correct file. Co-authored-by: Marko Fabo <mfabo@inf.u-szeged.hu> JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu
This commit is contained in:
+29
-28
@@ -158,46 +158,37 @@ print_unhandled_exception (jerry_value_t error_value) /**< error value */
|
||||
if (jerry_is_feature_enabled (JERRY_FEATURE_ERROR_MESSAGES)
|
||||
&& jerry_get_error_type (error_value) == JERRY_ERROR_SYNTAX)
|
||||
{
|
||||
jerry_char_t *string_end_p = err_str_buf + string_end;
|
||||
unsigned int err_line = 0;
|
||||
unsigned int err_col = 0;
|
||||
char *path_str_p = NULL;
|
||||
char *path_str_end_p = NULL;
|
||||
|
||||
/* 1. parse column and line information */
|
||||
for (jerry_size_t i = 0; i < string_end; i++)
|
||||
for (jerry_char_t *current_p = err_str_buf; current_p < string_end_p; current_p++)
|
||||
{
|
||||
if (!strncmp ((char *) (err_str_buf + i), "[line: ", 7))
|
||||
if (*current_p == '[')
|
||||
{
|
||||
i += 7;
|
||||
current_p++;
|
||||
|
||||
char num_str[8];
|
||||
unsigned int j = 0;
|
||||
|
||||
while (i < string_end && err_str_buf[i] != ',')
|
||||
if (*current_p == '<')
|
||||
{
|
||||
num_str[j] = (char) err_str_buf[i];
|
||||
j++;
|
||||
i++;
|
||||
}
|
||||
num_str[j] = '\0';
|
||||
|
||||
err_line = (unsigned int) strtol (num_str, NULL, 10);
|
||||
|
||||
if (strncmp ((char *) (err_str_buf + i), ", column: ", 10))
|
||||
{
|
||||
break; /* wrong position info format */
|
||||
break;
|
||||
}
|
||||
|
||||
i += 10;
|
||||
j = 0;
|
||||
|
||||
while (i < string_end && err_str_buf[i] != ']')
|
||||
path_str_p = (char *) current_p;
|
||||
while (current_p < string_end_p && *current_p != ':')
|
||||
{
|
||||
num_str[j] = (char) err_str_buf[i];
|
||||
j++;
|
||||
i++;
|
||||
current_p++;
|
||||
}
|
||||
num_str[j] = '\0';
|
||||
|
||||
err_col = (unsigned int) strtol (num_str, NULL, 10);
|
||||
path_str_end_p = (char *) current_p++;
|
||||
|
||||
err_line = (unsigned int) strtol ((char *) current_p, (char **) ¤t_p, 10);
|
||||
|
||||
current_p++;
|
||||
|
||||
err_col = (unsigned int) strtol ((char *) current_p, NULL, 10);
|
||||
break;
|
||||
}
|
||||
} /* for */
|
||||
@@ -209,8 +200,18 @@ print_unhandled_exception (jerry_value_t error_value) /**< error value */
|
||||
bool is_printing_context = false;
|
||||
unsigned int pos = 0;
|
||||
|
||||
size_t source_size;
|
||||
|
||||
/* Temporarily modify the error message, so we can use the path. */
|
||||
*path_str_end_p = '\0';
|
||||
|
||||
read_file (path_str_p, &source_size);
|
||||
|
||||
/* Revert the error message. */
|
||||
*path_str_end_p = ':';
|
||||
|
||||
/* 2. seek and print */
|
||||
while ((pos < JERRY_BUFFER_SIZE) && (buffer[pos] != '\0'))
|
||||
while ((pos < source_size) && (buffer[pos] != '\0'))
|
||||
{
|
||||
if (buffer[pos] == '\n')
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user