From c304b9a38abb0f8efedafe8b97af689ca6823fea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20B=C3=A1tyai?= Date: Thu, 11 Jul 2019 12:33:02 +0200 Subject: [PATCH] Include file path in Syntax error messages (#2941) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu --- jerry-core/api/jerry.c | 30 +++++-- jerry-core/ecma/base/ecma-module.c | 15 ++++ .../builtin-objects/ecma-builtin-function.c | 10 +-- jerry-core/ecma/operations/ecma-eval.c | 6 +- jerry-core/jcontext/jcontext.h | 4 +- jerry-core/lit/lit-magic-strings.inc.h | 14 +++- jerry-core/lit/lit-magic-strings.ini | 2 + jerry-core/parser/js/js-parser.c | 3 +- jerry-main/main-unix.c | 57 +++++++------- targets/nuttx-stm32f4/jerry_main.c | 78 +++++++++---------- .../apps/jerryscript/jerry_main.c | 64 +++++++-------- tests/unit-core/test-api.c | 44 ++++++++++- 12 files changed, 204 insertions(+), 123 deletions(-) diff --git a/jerry-core/api/jerry.c b/jerry-core/api/jerry.c index 90f895aea..d7a980ed3 100644 --- a/jerry-core/api/jerry.c +++ b/jerry-core/api/jerry.c @@ -396,10 +396,17 @@ jerry_parse (const jerry_char_t *resource_name_p, /**< resource name (usually a #if ENABLED (JERRY_PARSER) jerry_assert_api_available (); -#if ENABLED (JERRY_LINE_INFO) - JERRY_CONTEXT (resource_name) = ecma_find_or_create_literal_string (resource_name_p, - (lit_utf8_size_t) resource_name_length); -#endif /* ENABLED (JERRY_LINE_INFO) */ +#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) + if (resource_name_length == 0) + { + JERRY_CONTEXT (resource_name) = ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_ANON); + } + else + { + JERRY_CONTEXT (resource_name) = ecma_find_or_create_literal_string (resource_name_p, + (lit_utf8_size_t) resource_name_length); + } +#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) */ ecma_compiled_code_t *bytecode_data_p; ecma_value_t parse_status; @@ -468,10 +475,17 @@ jerry_parse_function (const jerry_char_t *resource_name_p, /**< resource name (u ecma_compiled_code_t *bytecode_data_p; ecma_value_t parse_status; -#if ENABLED (JERRY_LINE_INFO) - JERRY_CONTEXT (resource_name) = ecma_find_or_create_literal_string (resource_name_p, - (lit_utf8_size_t) resource_name_length); -#endif /* ENABLED (JERRY_LINE_INFO) */ +#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) + if (resource_name_length == 0) + { + JERRY_CONTEXT (resource_name) = ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_ANON); + } + else + { + JERRY_CONTEXT (resource_name) = ecma_find_or_create_literal_string (resource_name_p, + (lit_utf8_size_t) resource_name_length); + } +#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) */ if (arg_list_p == NULL) { diff --git a/jerry-core/ecma/base/ecma-module.c b/jerry-core/ecma/base/ecma-module.c index b43d5d6d4..fe308055e 100644 --- a/jerry-core/ecma/base/ecma-module.c +++ b/jerry-core/ecma/base/ecma-module.c @@ -695,6 +695,21 @@ ecma_module_parse (ecma_module_t *module_p) /**< module */ module_p->context_p->parent_p = JERRY_CONTEXT (module_top_context_p); JERRY_CONTEXT (module_top_context_p) = module_p->context_p; +#if ENABLED (JERRY_DEBUGGER) && ENABLED (JERRY_PARSER) + if (JERRY_CONTEXT (debugger_flags) & JERRY_DEBUGGER_CONNECTED) + { + jerry_debugger_send_string (JERRY_DEBUGGER_SOURCE_CODE_NAME, + JERRY_DEBUGGER_NO_SUBTYPE, + script_path_p, + script_path_size - 1); + } +#endif /* ENABLED (JERRY_DEBUGGER) && ENABLED (JERRY_PARSER) */ + +#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) + JERRY_CONTEXT (resource_name) = ecma_make_string_value (ecma_new_ecma_string_from_utf8 (script_path_p, + script_path_size - 1)); +#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) */ + ecma_compiled_code_t *bytecode_data_p; ecma_value_t ret_value = parser_parse_script (NULL, 0, diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-function.c b/jerry-core/ecma/builtin-objects/ecma-builtin-function.c index f72ee7983..22e46ef63 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-function.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-function.c @@ -24,9 +24,9 @@ #include "js-parser.h" #include "lit-magic-strings.h" -#if ENABLED (JERRY_LINE_INFO) +#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) #include "jcontext.h" -#endif /* ENABLED (JERRY_LINE_INFO) */ +#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) */ #define ECMA_BUILTINS_INTERNAL #include "ecma-builtins-internal.h" @@ -159,9 +159,9 @@ ecma_builtin_function_dispatch_construct (const ecma_value_t *arguments_list_p, ECMA_STRING_TO_UTF8_STRING (arguments_str_p, arguments_buffer_p, arguments_buffer_size); ECMA_STRING_TO_UTF8_STRING (function_body_str_p, function_body_buffer_p, function_body_buffer_size); -#if ENABLED (JERRY_LINE_INFO) - JERRY_CONTEXT (resource_name) = ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY); -#endif /* ENABLED (JERRY_LINE_INFO) */ +#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) + JERRY_CONTEXT (resource_name) = ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_ANON); +#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) */ ecma_compiled_code_t *bytecode_data_p = NULL; diff --git a/jerry-core/ecma/operations/ecma-eval.c b/jerry-core/ecma/operations/ecma-eval.c index 3a34df961..90da28901 100644 --- a/jerry-core/ecma/operations/ecma-eval.c +++ b/jerry-core/ecma/operations/ecma-eval.c @@ -91,9 +91,9 @@ ecma_op_eval_chars_buffer (const lit_utf8_byte_t *code_p, /**< code characters b parse_opts &= (uint32_t) ~ECMA_PARSE_STRICT_MODE; } -#if ENABLED (JERRY_LINE_INFO) - JERRY_CONTEXT (resource_name) = ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY); -#endif /* ENABLED (JERRY_LINE_INFO) */ +#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) + JERRY_CONTEXT (resource_name) = ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_EVAL); +#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) */ #if ENABLED (JERRY_ES2015_CLASS) ECMA_CLEAR_SUPER_EVAL_PARSER_OPTS (); diff --git a/jerry-core/jcontext/jcontext.h b/jerry-core/jcontext/jcontext.h index 5b027a65a..733342794 100644 --- a/jerry-core/jcontext/jcontext.h +++ b/jerry-core/jcontext/jcontext.h @@ -191,9 +191,9 @@ struct jerry_context_t uint8_t debugger_max_receive_size; /**< maximum amount of data that can be received */ #endif /* ENABLED (JERRY_DEBUGGER) */ -#if ENABLED (JERRY_LINE_INFO) +#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) ecma_value_t resource_name; /**< resource name (usually a file name) */ -#endif /* ENABLED (JERRY_LINE_INFO) */ +#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) */ #if ENABLED (JERRY_MEM_STATS) jmem_heap_stats_t jmem_heap_stats; /**< heap's memory usage statistics */ diff --git a/jerry-core/lit/lit-magic-strings.inc.h b/jerry-core/lit/lit-magic-strings.inc.h index 1c0bdfe9d..763700e0b 100644 --- a/jerry-core/lit/lit-magic-strings.inc.h +++ b/jerry-core/lit/lit-magic-strings.inc.h @@ -239,6 +239,9 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SLICE, "slice") LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SPLIT, "split") #endif LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_VALUE, "value") +#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) && ENABLED (JERRY_PARSER) +LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_RESOURCE_EVAL, "") +#endif #if ENABLED (JERRY_BUILTIN_MATH) LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_LOG10E_U, "LOG10E") #endif @@ -566,6 +569,9 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SET_UTC_DATE_UL, "setUTCDate") #if ENABLED (JERRY_BUILTIN_STRING) && ENABLED (JERRY_ES2015_BUILTIN) LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_STARTS_WITH, "startsWith") #endif +#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) +LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_RESOURCE_ANON, "") +#endif #if ENABLED (JERRY_ES2015_BUILTIN_DATAVIEW) \ || ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_ARRAY_BUFFER_UL, "ArrayBuffer") @@ -757,7 +763,9 @@ LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (4, LIT_MAGIC_STRING_EMPTY_NON_CAPTURE_G LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (4, LIT_MAGIC_STRING_DATE_UL) #endif LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (5, LIT_MAGIC_STRING_ARRAY_UL) -#if ENABLED (JERRY_BUILTIN_MATH) +#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) && ENABLED (JERRY_PARSER) +LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (6, LIT_MAGIC_STRING_RESOURCE_EVAL) +#elif ENABLED (JERRY_BUILTIN_MATH) LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (6, LIT_MAGIC_STRING_LOG10E_U) #else LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (6, LIT_MAGIC_STRING_NUMBER_UL) @@ -783,7 +791,9 @@ LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (10, LIT_MAGIC_STRING_CHAR_CODE_AT_UL) #else LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (10, LIT_MAGIC_STRING_ENUMERABLE) #endif -#if ENABLED (JERRY_ES2015_BUILTIN_DATAVIEW) \ +#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) +LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (11, LIT_MAGIC_STRING_RESOURCE_ANON) +#elif ENABLED (JERRY_ES2015_BUILTIN_DATAVIEW) \ || ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (11, LIT_MAGIC_STRING_ARRAY_BUFFER_UL) #elif ENABLED (JERRY_BUILTIN_ERRORS) diff --git a/jerry-core/lit/lit-magic-strings.ini b/jerry-core/lit/lit-magic-strings.ini index fbf8b37ea..57e521562 100644 --- a/jerry-core/lit/lit-magic-strings.ini +++ b/jerry-core/lit/lit-magic-strings.ini @@ -112,6 +112,7 @@ LIT_MAGIC_STRING_SHIFT = "shift" LIT_MAGIC_STRING_SLICE = "slice" LIT_MAGIC_STRING_SPLIT = "split" LIT_MAGIC_STRING_VALUE = "value" +LIT_MAGIC_STRING_RESOURCE_EVAL = "" LIT_MAGIC_STRING_LOG10E_U = "LOG10E" LIT_MAGIC_STRING_NUMBER_UL = "Number" LIT_MAGIC_STRING_OBJECT_UL = "Object" @@ -247,6 +248,7 @@ LIT_MAGIC_STRING_SET_MINUTES_UL = "setMinutes" LIT_MAGIC_STRING_SET_SECONDS_UL = "setSeconds" LIT_MAGIC_STRING_SET_UTC_DATE_UL = "setUTCDate" LIT_MAGIC_STRING_STARTS_WITH = "startsWith" +LIT_MAGIC_STRING_RESOURCE_ANON = "" LIT_MAGIC_STRING_ARRAY_BUFFER_UL = "ArrayBuffer" LIT_MAGIC_STRING_SYNTAX_ERROR_UL = "SyntaxError" LIT_MAGIC_STRING_UINT16_ARRAY_UL = "Uint16Array" diff --git a/jerry-core/parser/js/js-parser.c b/jerry-core/parser/js/js-parser.c index e2fc2b9e9..1a83e6fb9 100644 --- a/jerry-core/parser/js/js-parser.c +++ b/jerry-core/parser/js/js-parser.c @@ -2962,8 +2962,9 @@ parser_parse_script (const uint8_t *arg_list_p, /**< function argument list */ ecma_value_t col_str_val = ecma_make_uint32_value (parser_error.column); ecma_value_t error_value = ecma_raise_standard_error_with_format (ECMA_ERROR_SYNTAX, - "% [line: %, column: %]", + "% [%:%:%]", err_str_val, + JERRY_CONTEXT (resource_name), line_str_val, col_str_val); diff --git a/jerry-main/main-unix.c b/jerry-main/main-unix.c index 3afc0bc03..2a3cd9117 100644 --- a/jerry-main/main-unix.c +++ b/jerry-main/main-unix.c @@ -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') { diff --git a/targets/nuttx-stm32f4/jerry_main.c b/targets/nuttx-stm32f4/jerry_main.c index 0a98a5944..af293653f 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; + current_p++; - char num_str[8]; - uint32_t j = 0; - - while (i < sz && err_str_buf[i] != ',') + if (*current_p == '<') { - num_str[j] = (char) err_str_buf[i]; - j++; - i++; - } - num_str[j] = '\0'; - - err_line = str_to_uint (num_str); - - if (strncmp ((char *) (err_str_buf + i), ", column: ", 10)) - { - break; /* wrong position info format */ + break; } - i += 10; - j = 0; - - while (i < sz && 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 = str_to_uint (num_str); + path_str_end_p = (char *) current_p++; + + err_line = str_to_uint ((char *) current_p, (char **) ¤t_p); + + 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 3967dba8d..746edb652 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++; - } - 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 < sz && 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 */ @@ -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 (); } diff --git a/tests/unit-core/test-api.c b/tests/unit-core/test-api.c index 11070a2ac..ad6ef636f 100644 --- a/tests/unit-core/test-api.c +++ b/tests/unit-core/test-api.c @@ -827,7 +827,49 @@ main (void) jerry_release_value (err_str_val); jerry_release_value (parsed_code_val); TEST_ASSERT (!strcmp ((char *) err_str_buf, - "SyntaxError: Primary expression expected. [line: 2, column: 10]")); + "SyntaxError: Primary expression expected. [:2:10]")); + + const jerry_char_t file_str[] = "filename.js"; + parsed_code_val = jerry_parse (file_str, + sizeof (file_str) - 1, + parser_err_src, + sizeof (parser_err_src) - 1, + JERRY_PARSE_NO_OPTS); + TEST_ASSERT (jerry_value_is_error (parsed_code_val)); + parsed_code_val = jerry_get_value_from_error (parsed_code_val, true); + err_str_val = jerry_value_to_string (parsed_code_val); + err_str_size = jerry_get_string_size (err_str_val); + + sz = jerry_string_to_char_buffer (err_str_val, err_str_buf, err_str_size); + err_str_buf[sz] = 0; + + jerry_release_value (err_str_val); + jerry_release_value (parsed_code_val); + TEST_ASSERT (!strcmp ((char *) err_str_buf, + "SyntaxError: Primary expression expected. [filename.js:2:10]")); + + const jerry_char_t eval_err_src[] = "eval(\"var b;\\nfor (,); \");"; + parsed_code_val = jerry_parse (file_str, + sizeof (file_str), + eval_err_src, + sizeof (eval_err_src) - 1, + JERRY_PARSE_NO_OPTS); + TEST_ASSERT (!jerry_value_is_error (parsed_code_val)); + + res = jerry_run (parsed_code_val); + TEST_ASSERT (jerry_value_is_error (res)); + res = jerry_get_value_from_error (res, true); + err_str_val = jerry_value_to_string (res); + err_str_size = jerry_get_string_size (err_str_val); + + sz = jerry_string_to_char_buffer (err_str_val, err_str_buf, err_str_size); + err_str_buf[sz] = 0; + + jerry_release_value (err_str_val); + jerry_release_value (parsed_code_val); + jerry_release_value (res); + TEST_ASSERT (!strcmp ((char *) err_str_buf, + "SyntaxError: Primary expression expected. [:2:6]")); jerry_cleanup (); }