aboutsummaryrefslogtreecommitdiff
path: root/targets
diff options
context:
space:
mode:
authorDániel Bátyai <dbatyai@inf.u-szeged.hu>2019-07-11 12:33:02 +0200
committerGitHub <noreply@github.com>2019-07-11 12:33:02 +0200
commitc304b9a38abb0f8efedafe8b97af689ca6823fea (patch)
treefcd230304370c8fb3b5b3683823da885b68c8a70 /targets
parent3f47e1b0aa4073417aa6f0552d9a99d38f0f9987 (diff)
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
Diffstat (limited to 'targets')
-rw-r--r--targets/nuttx-stm32f4/jerry_main.c74
-rw-r--r--targets/tizenrt-artik053/apps/jerryscript/jerry_main.c60
2 files changed, 65 insertions, 69 deletions
diff --git a/targets/nuttx-stm32f4/jerry_main.c b/targets/nuttx-stm32f4/jerry_main.c
index 0a98a594..af293653 100644
--- a/targets/nuttx-stm32f4/jerry_main.c
+++ b/targets/nuttx-stm32f4/jerry_main.c
@@ -128,21 +128,25 @@ read_file (const char *file_name, /**< source code */
* @return converted number
*/
static uint32_t
-str_to_uint (const char *num_str_p) /**< string to convert */
+str_to_uint (const char *num_str_p, /**< string to convert */
+ char **out_p) /**< [out] end of the number */
{
assert (jerry_is_feature_enabled (JERRY_FEATURE_ERROR_MESSAGES));
uint32_t result = 0;
- while (*num_str_p != '\0')
+ while (*num_str_p >= '0' && *num_str_p <= '9')
{
- assert (*num_str_p >= '0' && *num_str_p <= '9');
-
result *= 10;
result += (uint32_t) (*num_str_p - '0');
num_str_p++;
}
+ if (out_p != NULL)
+ {
+ *out_p = num_str_p;
+ }
+
return result;
} /* str_to_uint */
@@ -150,8 +154,7 @@ str_to_uint (const char *num_str_p) /**< string to convert */
* Print error value
*/
static void
-print_unhandled_exception (jerry_value_t error_value, /**< error value */
- const jerry_char_t *source_p) /**< source_p */
+print_unhandled_exception (jerry_value_t error_value) /**< error value */
{
assert (jerry_value_is_error (error_value));
@@ -177,46 +180,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 + sz;
uint32_t err_line = 0;
uint32_t err_col = 0;
+ char *path_str_p = NULL;
+ char *path_str_end_p = NULL;
/* 1. parse column and line information */
- for (uint32_t i = 0; i < sz; 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;
-
- char num_str[8];
- uint32_t j = 0;
+ current_p++;
- while (i < sz && err_str_buf[i] != ',')
+ if (*current_p == '<')
{
- num_str[j] = (char) err_str_buf[i];
- j++;
- i++;
+ break;
}
- num_str[j] = '\0';
-
- err_line = str_to_uint (num_str);
- if (strncmp ((char *) (err_str_buf + i), ", column: ", 10))
+ path_str_p = (char *) current_p;
+ while (current_p < string_end_p && *current_p != ':')
{
- break; /* wrong position info format */
+ current_p++;
}
- i += 10;
- j = 0;
+ path_str_end_p = (char *) current_p++;
- while (i < sz && err_str_buf[i] != ']')
- {
- num_str[j] = (char) err_str_buf[i];
- j++;
- i++;
- }
- num_str[j] = '\0';
+ err_line = str_to_uint ((char *) current_p, (char **) &current_p);
- err_col = str_to_uint (num_str);
+ current_p++;
+
+ err_col = str_to_uint ((char *) current_p, NULL);
break;
}
} /* for */
@@ -228,6 +222,15 @@ print_unhandled_exception (jerry_value_t error_value, /**< error value */
bool is_printing_context = false;
uint32_t pos = 0;
+ /* Temporarily modify the error message, so we can use the path. */
+ *path_str_end_p = '\0';
+
+ size_t source_size;
+ const jerry_char_t *source_p = read_file (path_str_p, &source_size);
+
+ /* Revert the error message. */
+ *path_str_end_p = ':';
+
/* 2. seek and print */
while (source_p[pos] != '\0')
{
@@ -361,7 +364,7 @@ int jerry_main (int argc, char *argv[])
{
if (++i < argc)
{
- debug_port = str_to_uint (argv[i]);
+ debug_port = str_to_uint (argv[i], NULL);
}
else
{
@@ -419,6 +422,7 @@ int jerry_main (int argc, char *argv[])
source_p,
source_size,
JERRY_PARSE_NO_OPTS);
+ free ((void*) source_p);
if (!jerry_value_is_error (ret_value))
{
@@ -429,14 +433,10 @@ int jerry_main (int argc, char *argv[])
if (jerry_value_is_error (ret_value))
{
- print_unhandled_exception (ret_value, source_p);
- free ((void*) source_p);
-
+ print_unhandled_exception (ret_value);
break;
}
- free ((void*) source_p);
-
jerry_release_value (ret_value);
ret_value = jerry_create_undefined ();
}
diff --git a/targets/tizenrt-artik053/apps/jerryscript/jerry_main.c b/targets/tizenrt-artik053/apps/jerryscript/jerry_main.c
index 3967dba8..746edb65 100644
--- a/targets/tizenrt-artik053/apps/jerryscript/jerry_main.c
+++ b/targets/tizenrt-artik053/apps/jerryscript/jerry_main.c
@@ -126,8 +126,7 @@ read_file (const char *file_name, /**< source code */
* Print error value
*/
static void
-print_unhandled_exception (jerry_value_t error_value, /**< error value */
- const jerry_char_t *source_p) /**< source_p */
+print_unhandled_exception (jerry_value_t error_value) /**< error value */
{
assert (jerry_value_is_error (error_value));
@@ -153,46 +152,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 + sz;
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 < sz; 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 < sz && err_str_buf[i] != ',')
+ if (*current_p == '<')
{
- num_str[j] = (char) err_str_buf[i];
- j++;
- i++;
+ break;
}
- num_str[j] = '\0';
- err_line = (unsigned int) strtol (num_str, NULL, 10);
-
- if (strncmp ((char *) (err_str_buf + i), ", column: ", 10))
+ path_str_p = (char *) current_p;
+ while (current_p < string_end_p && *current_p != ':')
{
- break; /* wrong position info format */
+ current_p++;
}
- i += 10;
- j = 0;
+ path_str_end_p = (char *) current_p++;
- while (i < sz && err_str_buf[i] != ']')
- {
- num_str[j] = (char) err_str_buf[i];
- j++;
- i++;
- }
- num_str[j] = '\0';
+ err_line = (unsigned int) strtol ((char *) current_p, (char **) &current_p, 10);
+
+ current_p++;
- err_col = (unsigned int) strtol (num_str, NULL, 10);
+ err_col = (unsigned int) strtol ((char *) current_p, NULL, 10);
break;
}
} /* for */
@@ -204,6 +194,15 @@ print_unhandled_exception (jerry_value_t error_value, /**< error value */
bool is_printing_context = false;
unsigned int pos = 0;
+ /* Temporarily modify the error message, so we can use the path. */
+ *path_str_end_p = '\0';
+
+ size_t source_size;
+ const jerry_char_t *source_p = read_file (path_str_p, &source_size);
+
+ /* Revert the error message. */
+ *path_str_end_p = ':';
+
/* 2. seek and print */
while (source_p[pos] != '\0')
{
@@ -397,6 +396,7 @@ jerry_cmd_main (int argc, char *argv[])
source_p,
source_size,
JERRY_PARSE_NO_OPTS);
+ free ((void*) source_p);
if (!jerry_value_is_error (ret_value))
{
@@ -407,14 +407,10 @@ jerry_cmd_main (int argc, char *argv[])
if (jerry_value_is_error (ret_value))
{
- print_unhandled_exception (ret_value, source_p);
- free ((void*) source_p);
-
+ print_unhandled_exception (ret_value);
break;
}
- free ((void*) source_p);
-
jerry_release_value (ret_value);
ret_value = jerry_create_undefined ();
}