From ec11a7b4e94d849c04c6ed0875771e4814fd0374 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20B=C3=A1tyai?= Date: Wed, 17 Jul 2019 14:44:18 +0200 Subject: [PATCH] Resolve module paths relative to the current module (#2976) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The current module implementation resolves module paths relative to the current working directory, but paths should be resolved relative to the currently evaluated module/source. This requires a change in the jerry_port_normalize_path port API function, so that it also takes the current module path as an argument. On the engine side, we now also create a module object for the main script, so that we can properly identify the base path for other modules. Co-authored-by: Marko Fabo JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai@inf.u-szeged.hu --- docs/05.PORT-API.md | 4 +- jerry-core/api/jerry.c | 8 +- jerry-core/ecma/base/ecma-module.c | 101 ++++++++++-------- .../builtin-objects/ecma-builtin-function.c | 8 +- jerry-core/include/jerryscript-port.h | 7 +- jerry-core/jcontext/jcontext.h | 4 +- jerry-core/lit/lit-magic-strings.inc.h | 4 +- jerry-core/parser/js/js-parser-internal.h | 2 +- jerry-core/parser/js/js-parser-module.c | 42 +++++++- jerry-core/parser/js/js-parser-statm.c | 4 +- jerry-main/main-unix.c | 2 +- jerry-port/default/default-io.c | 58 ++++++++-- targets/nuttx-stm32f4/jerry_main.c | 2 +- targets/nuttx-stm32f4/jerry_port.c | 9 +- .../apps/jerryscript/jerry_main.c | 11 +- tests/jerry/es2015/module-export-02.js | 8 +- tests/jerry/es2015/module-export-03.js | 2 +- tests/jerry/es2015/module-export-05.js | 4 +- tests/jerry/es2015/module-export-06.js | 4 +- tests/jerry/es2015/module-import-01.js | 14 +-- tests/jerry/es2015/module-import-02.js | 4 +- tests/jerry/es2015/module-import-03.js | 2 +- tests/jerry/es2015/module-import-04.js | 8 +- tests/jerry/fail/module-001.js | 2 +- tests/jerry/fail/module-002.js | 2 +- tests/jerry/fail/module-003.js | 2 +- tests/jerry/fail/module-004.js | 2 +- tests/jerry/fail/module-005.js | 2 +- tests/jerry/fail/module-007.js | 2 +- tests/jerry/fail/module-010.js | 2 +- tests/jerry/fail/module-014.js | 2 +- tests/jerry/fail/module-015.js | 2 +- tests/jerry/fail/module-016.js | 2 +- tests/jerry/fail/module-017.js | 2 +- tests/jerry/fail/module-018.js | 2 +- tests/jerry/fail/module-020.js | 2 +- tests/jerry/fail/module-021.js | 4 +- tests/jerry/fail/module-022.js | 2 +- tests/jerry/fail/module-023.js | 2 +- tests/jerry/fail/module-024.js | 2 +- tests/jerry/fail/module-025.js | 2 +- tests/jerry/fail/module-026.js | 2 +- tests/jerry/fail/module-027.js | 2 +- tests/jerry/fail/module-028.js | 2 +- tests/jerry/fail/module-029.js | 2 +- tests/jerry/fail/module-030.js | 2 +- .../jerry/fail/regression-test-issue-2896.js | 2 +- 47 files changed, 233 insertions(+), 129 deletions(-) diff --git a/docs/05.PORT-API.md b/docs/05.PORT-API.md index b0c950fd4..62525d29b 100644 --- a/docs/05.PORT-API.md +++ b/docs/05.PORT-API.md @@ -118,9 +118,11 @@ jerry_port_release_source (uint8_t *buffer_p) /**< buffer to free */ size_t jerry_port_normalize_path (const char *in_path_p, /**< input file path */ char *out_buf_p, /**< output buffer */ - size_t out_buf_size) /**< size of output buffer */ + size_t out_buf_size, /**< size of output buffer */ + char *base_file_p) /**< base file path */ { // normalize in_path_p by expanding relative paths etc. + // if base_file_p is not NULL, in_path_p is relative to that file // write to out_buf_p the normalized path // return length of written path } /* jerry_port_normalize_path */ diff --git a/jerry-core/api/jerry.c b/jerry-core/api/jerry.c index 1ce685638..709e4ee9d 100644 --- a/jerry-core/api/jerry.c +++ b/jerry-core/api/jerry.c @@ -402,7 +402,7 @@ 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) || ENABLED (JERRY_ERROR_MESSAGES) +#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) if (resource_name_length == 0) { JERRY_CONTEXT (resource_name) = ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_ANON); @@ -412,7 +412,7 @@ jerry_parse (const jerry_char_t *resource_name_p, /**< resource name (usually a 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) */ +#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) */ ecma_compiled_code_t *bytecode_data_p; ecma_value_t parse_status; @@ -481,7 +481,7 @@ 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) || ENABLED (JERRY_ERROR_MESSAGES) +#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) if (resource_name_length == 0) { JERRY_CONTEXT (resource_name) = ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_ANON); @@ -491,7 +491,7 @@ jerry_parse_function (const jerry_char_t *resource_name_p, /**< resource name (u 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) */ +#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) */ if (arg_list_p == NULL) { diff --git a/jerry-core/ecma/base/ecma-module.c b/jerry-core/ecma/base/ecma-module.c index fe308055e..9007c3ee7 100644 --- a/jerry-core/ecma/base/ecma-module.c +++ b/jerry-core/ecma/base/ecma-module.c @@ -41,32 +41,57 @@ ecma_module_create_normalized_path (const uint8_t *char_p, /**< module specifier prop_length_t size) /**< size of module specifier */ { JERRY_ASSERT (size > 0); - ecma_string_t *ret_p; + ecma_string_t *ret_p = NULL; - /* Zero terminate the string. */ - uint8_t *temp_p = (uint8_t *) jmem_heap_alloc_block (size + 1u); - memcpy (temp_p, char_p, size); - temp_p[size] = LIT_CHAR_NULL; + /* The module specifier is cesu8 encoded, we need to convert is to utf8, and zero terminate it, + * so that OS level functions can handle it. */ + lit_utf8_byte_t *path_p = (lit_utf8_byte_t *) jmem_heap_alloc_block (size + 1u); - uint8_t *normalized_p = (uint8_t *) jmem_heap_alloc_block (ECMA_MODULE_MAX_PATH); - size_t new_size = jerry_port_normalize_path ((const char *) temp_p, - (char *) normalized_p, - ECMA_MODULE_MAX_PATH); + lit_utf8_size_t utf8_size; + utf8_size = lit_convert_cesu8_string_to_utf8_string (char_p, + size, + path_p, + size); + path_p[utf8_size] = LIT_CHAR_NULL; + lit_utf8_byte_t *module_path_p = NULL; + lit_utf8_size_t module_path_size = 0; - if (new_size == 0) + /* Check if we have a current module, and use its path as the base path. */ + JERRY_ASSERT (JERRY_CONTEXT (module_top_context_p) != NULL); + if (JERRY_CONTEXT (module_top_context_p)->module_p != NULL) { - /* Failed to normalize path, use original. */ - ret_p = ecma_new_ecma_string_from_utf8 (temp_p, (lit_utf8_size_t) (size + 1u)); - } - else - { - /* Copy the trailing \0 into the string as well, to make it more convenient to use later. */ - ret_p = ecma_new_ecma_string_from_utf8 (normalized_p, (lit_utf8_size_t) (new_size + 1u)); + JERRY_ASSERT (JERRY_CONTEXT (module_top_context_p)->module_p->path_p != NULL); + module_path_size = ecma_string_get_size (JERRY_CONTEXT (module_top_context_p)->module_p->path_p); + module_path_p = (lit_utf8_byte_t *) jmem_heap_alloc_block (module_path_size + 1); + + lit_utf8_size_t module_utf8_size; + module_utf8_size = ecma_string_copy_to_utf8_buffer (JERRY_CONTEXT (module_top_context_p)->module_p->path_p, + module_path_p, + module_path_size); + + module_path_p[module_utf8_size] = LIT_CHAR_NULL; } - jmem_heap_free_block (temp_p, size + 1u); - jmem_heap_free_block (normalized_p, ECMA_MODULE_MAX_PATH); + lit_utf8_byte_t *normalized_out_p = (lit_utf8_byte_t *) jmem_heap_alloc_block (ECMA_MODULE_MAX_PATH); + size_t normalized_size = jerry_port_normalize_path ((const char *) path_p, + (char *) normalized_out_p, + ECMA_MODULE_MAX_PATH, + (char *) module_path_p); + + + if (normalized_size > 0) + { + /* Convert the normalized path to cesu8. */ + ret_p = ecma_new_ecma_string_from_utf8_converted_to_cesu8 (normalized_out_p, (lit_utf8_size_t) (normalized_size)); + } + + jmem_heap_free_block (path_p, size + 1u); + jmem_heap_free_block (normalized_out_p, ECMA_MODULE_MAX_PATH); + if (module_path_p != NULL) + { + jmem_heap_free_block (module_path_p, module_path_size + 1); + } return ret_p; } /* ecma_module_create_normalized_path */ @@ -573,17 +598,7 @@ ecma_module_connect_imports (void) { ecma_module_context_t *current_context_p = JERRY_CONTEXT (module_top_context_p); - ecma_object_t *local_env_p; - - if (current_context_p->module_p != NULL) - { - local_env_p = current_context_p->module_p->scope_p; - } - else - { - /* This is the root context. */ - local_env_p = ecma_get_global_environment (); - } + ecma_object_t *local_env_p = current_context_p->module_p->scope_p; JERRY_ASSERT (ecma_is_lexical_environment (local_env_p)); ecma_module_node_t *import_node_p = current_context_p->imports_p; @@ -677,14 +692,18 @@ ecma_module_parse (ecma_module_t *module_p) /**< module */ module_p->state = ECMA_MODULE_STATE_PARSING; module_p->context_p = ecma_module_create_module_context (); - lit_utf8_size_t script_path_size; - uint8_t flags = ECMA_STRING_FLAG_EMPTY; - const lit_utf8_byte_t *script_path_p = ecma_string_get_chars (module_p->path_p, - &script_path_size, - &flags); + lit_utf8_size_t module_path_size = ecma_string_get_size (module_p->path_p); + lit_utf8_byte_t *module_path_p = (lit_utf8_byte_t *) jmem_heap_alloc_block (module_path_size + 1); + + lit_utf8_size_t module_path_utf8_size; + module_path_utf8_size = ecma_string_copy_to_utf8_buffer (module_p->path_p, + module_path_p, + module_path_size); + module_path_p[module_path_utf8_size] = LIT_CHAR_NULL; size_t source_size = 0; - uint8_t *source_p = jerry_port_read_source ((const char *) script_path_p, &source_size); + uint8_t *source_p = jerry_port_read_source ((const char *) module_path_p, &source_size); + jmem_heap_free_block (module_path_p, module_path_size + 1); if (source_p == NULL) { @@ -700,15 +719,12 @@ ecma_module_parse (ecma_module_t *module_p) /**< module */ { jerry_debugger_send_string (JERRY_DEBUGGER_SOURCE_CODE_NAME, JERRY_DEBUGGER_NO_SUBTYPE, - script_path_p, - script_path_size - 1); + module_path_p, + module_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) */ + JERRY_CONTEXT (resource_name) = ecma_make_string_value (module_p->path_p); ecma_compiled_code_t *bytecode_data_p; ecma_value_t ret_value = parser_parse_script (NULL, @@ -900,7 +916,6 @@ ecma_module_cleanup (void) current_p = next_p; } - ecma_module_release_module_context (JERRY_CONTEXT (module_top_context_p)); JERRY_CONTEXT (module_top_context_p) = NULL; } /* ecma_module_cleanup */ diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-function.c b/jerry-core/ecma/builtin-objects/ecma-builtin-function.c index 22e46ef63..1553aa65f 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) || ENABLED (JERRY_ERROR_MESSAGES) +#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) #include "jcontext.h" -#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) */ +#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) */ #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) || ENABLED (JERRY_ERROR_MESSAGES) +#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) JERRY_CONTEXT (resource_name) = ecma_make_magic_string_value (LIT_MAGIC_STRING_RESOURCE_ANON); -#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) */ +#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) */ ecma_compiled_code_t *bytecode_data_p = NULL; diff --git a/jerry-core/include/jerryscript-port.h b/jerry-core/include/jerryscript-port.h index ab5b90c77..1e24fd74d 100644 --- a/jerry-core/include/jerryscript-port.h +++ b/jerry-core/include/jerryscript-port.h @@ -225,11 +225,16 @@ void jerry_port_release_source (uint8_t *buffer_p); * @param in_path_p Input path as a zero terminated string. * @param out_buf_p Pointer to the output buffer where the normalized path should be written. * @param out_buf_size Size of the output buffer. + * @param base_file_p A file path that 'in_path_p' is relative to, usually the current module file. + * A NULL value represents that 'in_path_p' is relative to the current working directory. * * @return length of the string written to the output buffer * zero, if the buffer was not sufficient or an error occured */ -size_t jerry_port_normalize_path (const char *in_path_p, char *out_buf_p, size_t out_buf_size); +size_t jerry_port_normalize_path (const char *in_path_p, + char *out_buf_p, + size_t out_buf_size, + char *base_file_p); /** * @} diff --git a/jerry-core/jcontext/jcontext.h b/jerry-core/jcontext/jcontext.h index e339b5194..86cc5b540 100644 --- a/jerry-core/jcontext/jcontext.h +++ b/jerry-core/jcontext/jcontext.h @@ -190,9 +190,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) || ENABLED (JERRY_ERROR_MESSAGES) +#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) ecma_value_t resource_name; /**< resource name (usually a file name) */ -#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) */ +#endif /* ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) */ #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 17ae3967a..df0265d12 100644 --- a/jerry-core/lit/lit-magic-strings.inc.h +++ b/jerry-core/lit/lit-magic-strings.inc.h @@ -575,7 +575,7 @@ 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) +#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_RESOURCE_ANON, "") #endif #if ENABLED (JERRY_ES2015_BUILTIN_DATAVIEW) \ @@ -797,7 +797,7 @@ 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_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) +#if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) || ENABLED (JERRY_ES2015_MODULE_SYSTEM) LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (11, LIT_MAGIC_STRING_RESOURCE_ANON) #elif ENABLED (JERRY_ES2015_BUILTIN_DATAVIEW) \ || ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY) diff --git a/jerry-core/parser/js/js-parser-internal.h b/jerry-core/parser/js/js-parser-internal.h index 16a5c6da0..6a28225d2 100644 --- a/jerry-core/parser/js/js-parser-internal.h +++ b/jerry-core/parser/js/js-parser-internal.h @@ -572,7 +572,7 @@ extern const lexer_lit_location_t lexer_default_literal; void parser_module_add_export_node_to_context (parser_context_t *context_p); void parser_module_add_import_node_to_context (parser_context_t *context_p); void parser_module_check_request_place (parser_context_t *context_p); -void parser_module_context_init (parser_context_t *context_p); +void parser_module_context_init (void); void parser_module_handle_module_specifier (parser_context_t *context_p); void parser_module_handle_requests (parser_context_t *context_p); void parser_module_parse_export_clause (parser_context_t *context_p); diff --git a/jerry-core/parser/js/js-parser-module.c b/jerry-core/parser/js/js-parser-module.c index bb21375cf..026906f72 100644 --- a/jerry-core/parser/js/js-parser-module.c +++ b/jerry-core/parser/js/js-parser-module.c @@ -259,16 +259,45 @@ parser_module_add_names_to_node (parser_context_t *context_p, /**< parser contex * Create module context if needed. */ void -parser_module_context_init (parser_context_t *context_p) /**< parser context */ +parser_module_context_init (void) { if (JERRY_CONTEXT (module_top_context_p) == NULL) { ecma_module_context_t *module_context_p; - module_context_p = (ecma_module_context_t *) parser_malloc (context_p, - sizeof (ecma_module_context_t)); - + module_context_p = (ecma_module_context_t *) jmem_heap_alloc_block (sizeof (ecma_module_context_t)); memset (module_context_p, 0, sizeof (ecma_module_context_t)); JERRY_CONTEXT (module_top_context_p) = module_context_p; + + ecma_string_t *path_str_p = ecma_get_string_from_value (JERRY_CONTEXT (resource_name)); + + lit_utf8_size_t path_str_size; + uint8_t flags = ECMA_STRING_FLAG_EMPTY; + + const lit_utf8_byte_t *path_str_chars_p = ecma_string_get_chars (path_str_p, + &path_str_size, + &flags); + + ecma_string_t *path_p = ecma_module_create_normalized_path (path_str_chars_p, + (prop_length_t) path_str_size); + + if (path_p == NULL) + { + path_p = path_str_p; + } + + ecma_module_t *module_p = ecma_module_find_or_create_module (path_p); + + if (path_p != path_str_p) + { + ecma_deref_ecma_string (path_p); + } + + module_p->state = ECMA_MODULE_STATE_EVALUATED; + module_p->scope_p = ecma_get_global_environment (); + ecma_ref_object (module_p->scope_p); + + module_p->context_p = module_context_p; + module_context_p->module_p = module_p; } } /* parser_module_context_init */ @@ -518,6 +547,11 @@ parser_module_handle_module_specifier (parser_context_t *context_p) /**< parser ecma_string_t *path_p = ecma_module_create_normalized_path (context_p->lit_object.literal_p->u.char_p, context_p->lit_object.literal_p->prop.length); + if (path_p == NULL) + { + parser_raise_error (context_p, PARSER_ERR_FILE_NOT_FOUND); + } + ecma_module_t *module_p = ecma_module_find_or_create_module (path_p); ecma_deref_ecma_string (path_p); diff --git a/jerry-core/parser/js/js-parser-statm.c b/jerry-core/parser/js/js-parser-statm.c index 33cb0fd73..0f5920727 100644 --- a/jerry-core/parser/js/js-parser-statm.c +++ b/jerry-core/parser/js/js-parser-statm.c @@ -1843,7 +1843,7 @@ parser_parse_import_statement (parser_context_t *context_p) /**< parser context JERRY_ASSERT (context_p->token.type == LEXER_KEYW_IMPORT); parser_module_check_request_place (context_p); - parser_module_context_init (context_p); + parser_module_context_init (); ecma_module_node_t module_node; memset (&module_node, 0, sizeof (ecma_module_node_t)); @@ -1964,7 +1964,7 @@ parser_parse_export_statement (parser_context_t *context_p) /**< context */ JERRY_ASSERT (context_p->token.type == LEXER_KEYW_EXPORT); parser_module_check_request_place (context_p); - parser_module_context_init (context_p); + parser_module_context_init (); ecma_module_node_t module_node; memset (&module_node, 0, sizeof (ecma_module_node_t)); diff --git a/jerry-main/main-unix.c b/jerry-main/main-unix.c index 2a3cd9117..cc91b60cd 100644 --- a/jerry-main/main-unix.c +++ b/jerry-main/main-unix.c @@ -151,7 +151,7 @@ print_unhandled_exception (jerry_value_t error_value) /**< error value */ } else { - jerry_size_t string_end = jerry_string_to_char_buffer (err_str_val, err_str_buf, err_str_size); + jerry_size_t string_end = jerry_string_to_utf8_char_buffer (err_str_val, err_str_buf, err_str_size); assert (string_end == err_str_size); err_str_buf[string_end] = 0; diff --git a/jerry-port/default/default-io.c b/jerry-port/default/default-io.c index 1d54356af..90c1b1205 100644 --- a/jerry-port/default/default-io.c +++ b/jerry-port/default/default-io.c @@ -13,6 +13,9 @@ * limitations under the License. */ +#if !defined (WIN32) +#include +#endif /* !defined (WIN32) */ #include #include #include @@ -203,22 +206,58 @@ jerry_port_release_source (uint8_t *buffer_p) /**< buffer to free */ * @return length of the path written to the output buffer */ size_t -jerry_port_normalize_path (const char *in_path_p, /**< input file path */ - char *out_buf_p, /**< output buffer */ - size_t out_buf_size) /**< size of output buffer */ +jerry_port_normalize_path (const char *in_path_p, /**< input file path */ + char *out_buf_p, /**< output buffer */ + size_t out_buf_size, /**< size of output buffer */ + char *base_file_p) /**< base file path */ { size_t ret = 0; #if defined (WIN32) - char *norm_p = _fullpath (out_buf_p, in_path_p, out_buf_size); + char drive[_MAX_DRIVE]; + char *dir_p = (char *) malloc (_MAX_DIR); + + char *path_p = (char *) malloc (_MAX_PATH * 2); + *path_p = '\0'; + + if (base_file_p != NULL) + { + _splitpath_s (base_file_p, + &drive, + _MAX_DRIVE, + dir_p, + _MAX_DIR, + NULL, + 0, + NULL, + 0); + strncat (path_p, &drive, _MAX_DRIVE); + strncat (path_p, dir_p, _MAX_DIR); + } + + strncat (path_p, in_path_p, _MAX_PATH); + + char *norm_p = _fullpath (out_buf_p, path_p, out_buf_size); + + free (path_p); + free (dir_p); if (norm_p != NULL) { ret = strnlen (norm_p, out_buf_size); } #elif defined (__unix__) || defined (__APPLE__) - char *temp_p = (char *) malloc (PATH_MAX); - char *norm_p = realpath (in_path_p, temp_p); +#define MAX_JERRY_PATH_SIZE 256 + char *buffer_p = (char *) malloc (PATH_MAX); + char *path_p = (char *) malloc (PATH_MAX); + + char *base_p = dirname (base_file_p); + strncpy (path_p, base_p, MAX_JERRY_PATH_SIZE); + strncat (path_p, "/", 1); + strncat (path_p, in_path_p, MAX_JERRY_PATH_SIZE); + + char *norm_p = realpath (path_p, buffer_p); + free (path_p); if (norm_p != NULL) { @@ -230,9 +269,12 @@ jerry_port_normalize_path (const char *in_path_p, /**< input file path */ } } - free (temp_p); + free (buffer_p); +#undef MAX_JERRY_PATH_SIZE #else - /* Do nothing. */ + (void) base_file_p; + + /* Do nothing, just copy the input. */ const size_t len = strnlen (in_path_p, out_buf_size); if (len < out_buf_size) { diff --git a/targets/nuttx-stm32f4/jerry_main.c b/targets/nuttx-stm32f4/jerry_main.c index af293653f..36d10f38a 100644 --- a/targets/nuttx-stm32f4/jerry_main.c +++ b/targets/nuttx-stm32f4/jerry_main.c @@ -173,7 +173,7 @@ print_unhandled_exception (jerry_value_t error_value) /**< error value */ } else { - jerry_size_t sz = jerry_string_to_char_buffer (err_str_val, err_str_buf, err_str_size); + jerry_size_t sz = jerry_string_to_utf8_char_buffer (err_str_val, err_str_buf, err_str_size); assert (sz == err_str_size); err_str_buf[err_str_size] = 0; diff --git a/targets/nuttx-stm32f4/jerry_port.c b/targets/nuttx-stm32f4/jerry_port.c index 4715da78a..2cf86089e 100644 --- a/targets/nuttx-stm32f4/jerry_port.c +++ b/targets/nuttx-stm32f4/jerry_port.c @@ -132,10 +132,13 @@ jerry_port_release_source (uint8_t *buffer_p) /**< buffer to free */ * @return length of the path written to the output buffer */ size_t -jerry_port_normalize_path (const char *in_path_p, /**< input file path */ - char *out_buf_p, /**< output buffer */ - size_t out_buf_size) /**< size of output buffer */ +jerry_port_normalize_path (const char *in_path_p, /**< input file path */ + char *out_buf_p, /**< output buffer */ + size_t out_buf_size, /**< size of output buffer */ + char *base_file_p) /**< base file path */ { + (void) base_file_p; + size_t len = strlen (in_path_p); if (len + 1 > out_buf_size) { diff --git a/targets/tizenrt-artik053/apps/jerryscript/jerry_main.c b/targets/tizenrt-artik053/apps/jerryscript/jerry_main.c index 746edb652..b407f2d19 100644 --- a/targets/tizenrt-artik053/apps/jerryscript/jerry_main.c +++ b/targets/tizenrt-artik053/apps/jerryscript/jerry_main.c @@ -145,7 +145,7 @@ print_unhandled_exception (jerry_value_t error_value) /**< error value */ } else { - jerry_size_t sz = jerry_string_to_char_buffer (err_str_val, err_str_buf, err_str_size); + jerry_size_t sz = jerry_string_to_utf8_char_buffer (err_str_val, err_str_buf, err_str_size); assert (sz == err_str_size); err_str_buf[err_str_size] = 0; @@ -575,10 +575,13 @@ jerry_port_release_source (uint8_t *buffer_p) /**< buffer to free */ * @return length of the path written to the output buffer */ size_t -jerry_port_normalize_path (const char *in_path_p, /**< input file path */ - char *out_buf_p, /**< output buffer */ - size_t out_buf_size) /**< size of output buffer */ +jerry_port_normalize_path (const char *in_path_p, /**< input file path */ + char *out_buf_p, /**< output buffer */ + size_t out_buf_size, /**< size of output buffer */ + char *base_file_p) /**< base file path */ { + (void) base_file_p; + size_t len = strlen (in_path_p); if (len + 1 > out_buf_size) { diff --git a/tests/jerry/es2015/module-export-02.js b/tests/jerry/es2015/module-export-02.js index 584b76126..6fddc5f09 100644 --- a/tests/jerry/es2015/module-export-02.js +++ b/tests/jerry/es2015/module-export-02.js @@ -13,8 +13,8 @@ * limitations under the License. */ -export {} from "tests/jerry/es2015/module-export-01.js"; -export {aa,} from "tests/jerry/es2015/module-export-01.js"; -export {bb as b_, cc as c_} from "tests/jerry/es2015/module-export-01.js"; -export * from "tests/jerry/es2015/module-export-01.js"; +export {} from "module-export-01.js"; +export {aa,} from "module-export-01.js"; +export {bb as b_, cc as c_} from "module-export-01.js"; +export * from "module-export-01.js"; export default function () {return "default"}; diff --git a/tests/jerry/es2015/module-export-03.js b/tests/jerry/es2015/module-export-03.js index afdd70b6f..eb8850c36 100644 --- a/tests/jerry/es2015/module-export-03.js +++ b/tests/jerry/es2015/module-export-03.js @@ -23,4 +23,4 @@ export default class { } } -export * from "tests/jerry/es2015/module-export-02.js" +export * from "module-export-02.js" diff --git a/tests/jerry/es2015/module-export-05.js b/tests/jerry/es2015/module-export-05.js index 2184128e0..5a6349c02 100644 --- a/tests/jerry/es2015/module-export-05.js +++ b/tests/jerry/es2015/module-export-05.js @@ -13,6 +13,6 @@ * limitations under the License. */ -export * from "tests/jerry/es2015/module-export-01.js"; -export * from "tests/jerry/es2015/module-export-04.js"; +export * from "module-export-01.js"; +export * from "module-export-04.js"; export default a = "str" diff --git a/tests/jerry/es2015/module-export-06.js b/tests/jerry/es2015/module-export-06.js index 06d3d7db4..8c614a490 100644 --- a/tests/jerry/es2015/module-export-06.js +++ b/tests/jerry/es2015/module-export-06.js @@ -14,6 +14,6 @@ */ export {} -export {} from "tests/jerry/es2015/module-export-01.js"; +export {} from "module-export-01.js"; export {}; -export {} from "tests/jerry/es2015/module-export-04.js" +export {} from "module-export-04.js" diff --git a/tests/jerry/es2015/module-import-01.js b/tests/jerry/es2015/module-import-01.js index 6f738366a..54f6826f5 100644 --- a/tests/jerry/es2015/module-import-01.js +++ b/tests/jerry/es2015/module-import-01.js @@ -13,13 +13,13 @@ * limitations under the License. */ -import "tests/jerry/es2015/module-export-01.js"; -import def from "tests/jerry/es2015/module-export-01.js"; -import {} from "tests/jerry/es2015/module-export-01.js"; -import {aa as a,} from "tests/jerry/es2015/module-export-01.js"; -import {bb as b, cc as c} from "tests/jerry/es2015/module-export-01.js"; -import {x} from "tests/jerry/es2015/module-export-01.js"; -import * as mod from "tests/jerry/es2015/module-export-01.js"; +import "./module-export-01.js"; +import def from "module-export-01.js"; +import {} from "module-export-01.js"; +import {aa as a,} from "module-export-01.js"; +import {bb as b, cc as c} from "module-export-01.js"; +import {x} from "module-export-01.js"; +import * as mod from "module-export-01.js"; assert (def === "default"); assert (a === "a"); diff --git a/tests/jerry/es2015/module-import-02.js b/tests/jerry/es2015/module-import-02.js index 67fe0d3f5..669c7a49e 100644 --- a/tests/jerry/es2015/module-import-02.js +++ b/tests/jerry/es2015/module-import-02.js @@ -13,8 +13,8 @@ * limitations under the License. */ -import def, * as mod from "tests/jerry/es2015/module-export-02.js"; -import {b_, c_,} from "tests/jerry/es2015/module-export-02.js"; +import def, * as mod from "module-export-02.js"; +import {b_, c_,} from "module-export-02.js"; assert (def() === "default") assert (mod.aa === "a") diff --git a/tests/jerry/es2015/module-import-03.js b/tests/jerry/es2015/module-import-03.js index b874983ba..11214a9e6 100644 --- a/tests/jerry/es2015/module-import-03.js +++ b/tests/jerry/es2015/module-import-03.js @@ -13,7 +13,7 @@ * limitations under the License. */ -import incrementer, {aa, c_, x,} from "tests/jerry/es2015/module-export-03.js" +import incrementer, {aa, c_, x,} from "module-export-03.js" var i = new incrementer(3); assert(i.incr() === 4); assert(i.incr() === 5); diff --git a/tests/jerry/es2015/module-import-04.js b/tests/jerry/es2015/module-import-04.js index 34a6e693e..f0a5181a1 100644 --- a/tests/jerry/es2015/module-import-04.js +++ b/tests/jerry/es2015/module-import-04.js @@ -13,7 +13,7 @@ * limitations under the License. */ -import "tests/jerry/es2015/module-import-01.js" -import "tests/jerry/es2015/module-export-05.js" -import "tests/jerry/es2015/module-export-06.js" -import "tests/jerry/es2015/module-export-07.js" +import "module-import-01.js"; +import "module-export-05.js"; +import "module-export-06.js"; +import "module-export-07.js"; diff --git a/tests/jerry/fail/module-001.js b/tests/jerry/fail/module-001.js index 21f09a4fd..7dba7a828 100644 --- a/tests/jerry/fail/module-001.js +++ b/tests/jerry/fail/module-001.js @@ -14,4 +14,4 @@ */ // File does not exist. -import b from "tests/jerry/fail/module-exports.js" +import b from "module-exports.js" diff --git a/tests/jerry/fail/module-002.js b/tests/jerry/fail/module-002.js index 6b407bd53..52b6d0086 100644 --- a/tests/jerry/fail/module-002.js +++ b/tests/jerry/fail/module-002.js @@ -13,4 +13,4 @@ * limitations under the License. */ -import { , as b } from "tests/jerry/es2015/module-export-01.js"; +import { , as b } from "../es2015/module-export-01.js"; diff --git a/tests/jerry/fail/module-003.js b/tests/jerry/fail/module-003.js index eadde5efa..f7f8a2fd8 100644 --- a/tests/jerry/fail/module-003.js +++ b/tests/jerry/fail/module-003.js @@ -13,4 +13,4 @@ * limitations under the License. */ -import , as b from "tests/jerry/es2015/module-export-01.js"; +import , as b from "../es2015/module-export-01.js"; diff --git a/tests/jerry/fail/module-004.js b/tests/jerry/fail/module-004.js index 6786d051a..cc3345b74 100644 --- a/tests/jerry/fail/module-004.js +++ b/tests/jerry/fail/module-004.js @@ -13,4 +13,4 @@ * limitations under the License. */ -import { b as , } from "tests/jerry/es2015/module-export-01.js"; +import { b as , } from "../es2015/module-export-01.js"; diff --git a/tests/jerry/fail/module-005.js b/tests/jerry/fail/module-005.js index 62fbe501a..dd668ddc5 100644 --- a/tests/jerry/fail/module-005.js +++ b/tests/jerry/fail/module-005.js @@ -14,4 +14,4 @@ */ /* Named imports must be in a NamedImports block. */ -import b as , from "tests/jerry/es2015/module-export-01.js"; +import b as , from "../es2015/module-export-01.js"; diff --git a/tests/jerry/fail/module-007.js b/tests/jerry/fail/module-007.js index 77c36fd43..5a0ca65f9 100644 --- a/tests/jerry/fail/module-007.js +++ b/tests/jerry/fail/module-007.js @@ -14,4 +14,4 @@ */ /* Module requests must always be evaluated. */ -import "tests/jerry/fail/module-sideeffect.js" +import "./module-sideeffect.js" diff --git a/tests/jerry/fail/module-010.js b/tests/jerry/fail/module-010.js index ef3b923ce..7c9721e74 100644 --- a/tests/jerry/fail/module-010.js +++ b/tests/jerry/fail/module-010.js @@ -14,4 +14,4 @@ */ /* Can't have duplicate local bindings */ -import { c as a, d as a } from "tests/jerry/es2015/module-export-01.js"; +import { c as a, d as a } from "../es2015/module-export-01.js"; diff --git a/tests/jerry/fail/module-014.js b/tests/jerry/fail/module-014.js index f422fab97..77cd07179 100644 --- a/tests/jerry/fail/module-014.js +++ b/tests/jerry/fail/module-014.js @@ -15,5 +15,5 @@ /* Import/export statements must be in the global scope. */ if (true) { - import { c } from "tests/jerry/es2015/module-export-01.js"; + import { c } from "../es2015/module-export-01.js"; } diff --git a/tests/jerry/fail/module-015.js b/tests/jerry/fail/module-015.js index e176eda70..a3be641c5 100644 --- a/tests/jerry/fail/module-015.js +++ b/tests/jerry/fail/module-015.js @@ -15,5 +15,5 @@ /* Import/export statements must be in the global scope. */ function someFunction() { - import { c } from "tests/jerry/es2015/module-export-01.js"; + import { c } from "../es2015/module-export-01.js"; } diff --git a/tests/jerry/fail/module-016.js b/tests/jerry/fail/module-016.js index 1e2a84876..794b4d16d 100644 --- a/tests/jerry/fail/module-016.js +++ b/tests/jerry/fail/module-016.js @@ -14,4 +14,4 @@ */ /* Import/export statements must be in the global scope. */ -eval ('import { c } from "tests/jerry/es2015/module-export-01.js";'); +eval ('import { c } from "../es2015/module-export-01.js";'); diff --git a/tests/jerry/fail/module-017.js b/tests/jerry/fail/module-017.js index f873d3a45..fb29f841e 100644 --- a/tests/jerry/fail/module-017.js +++ b/tests/jerry/fail/module-017.js @@ -14,4 +14,4 @@ */ /* NamedImports must always be followed by a FromClause. */ -import { b }, from "tests/jerry/es2015/module-export-01.js" +import { b }, from "../es2015/module-export-01.js" diff --git a/tests/jerry/fail/module-018.js b/tests/jerry/fail/module-018.js index 1ccfd1cd5..9bb0e9709 100644 --- a/tests/jerry/fail/module-018.js +++ b/tests/jerry/fail/module-018.js @@ -14,4 +14,4 @@ */ /* An import statement can have either a NameSpaceImport or NamedIpmorts */ -import * as mod, { b } from "tests/jerry/es2015/module-export-01.js" +import * as mod, { b } from "../es2015/module-export-01.js" diff --git a/tests/jerry/fail/module-020.js b/tests/jerry/fail/module-020.js index 241b2b642..dd66dd0d3 100644 --- a/tests/jerry/fail/module-020.js +++ b/tests/jerry/fail/module-020.js @@ -14,4 +14,4 @@ */ /* '*' is not valid inside NamedImports. */ -import { *, d } from "tests/jerry/es2015/module-imported-01.js" +import { *, d } from "../es2015/module-imported-01.js" diff --git a/tests/jerry/fail/module-021.js b/tests/jerry/fail/module-021.js index 2b44e962a..9dd2ff318 100644 --- a/tests/jerry/fail/module-021.js +++ b/tests/jerry/fail/module-021.js @@ -14,5 +14,5 @@ */ /* Can't have duplicated local bindings. */ -import { b } from "tests/jerry/es2015/module-export-01.js" -import { b } from "tests/jerry/es2015/module-export-02.js" +import { b } from "../es2015/module-export-01.js" +import { b } from "../es2015/module-export-02.js" diff --git a/tests/jerry/fail/module-022.js b/tests/jerry/fail/module-022.js index afc7ff43b..2accda4c5 100644 --- a/tests/jerry/fail/module-022.js +++ b/tests/jerry/fail/module-022.js @@ -14,4 +14,4 @@ */ /* FromClause must follow an ImportClause. */ -import from "tests/jerry/es2015/module-export-02.js" +import from "../es2015/module-export-02.js" diff --git a/tests/jerry/fail/module-023.js b/tests/jerry/fail/module-023.js index 4d09473df..97d78a434 100644 --- a/tests/jerry/fail/module-023.js +++ b/tests/jerry/fail/module-023.js @@ -14,4 +14,4 @@ */ /* Namespace imports must have a local name. */ -import * from "tests/jerry/es2015/module-export-01.js" +import * from "../es2015/module-export-01.js" diff --git a/tests/jerry/fail/module-024.js b/tests/jerry/fail/module-024.js index 7bfb069c9..6e5081f61 100644 --- a/tests/jerry/fail/module-024.js +++ b/tests/jerry/fail/module-024.js @@ -14,4 +14,4 @@ */ /* Star exports can't have an export name. */ -export * as star from "tests/jerry/es2015/module-export-01.js" +export * as star from "../es2015/module-export-01.js" diff --git a/tests/jerry/fail/module-025.js b/tests/jerry/fail/module-025.js index 7095030c3..0765b062d 100644 --- a/tests/jerry/fail/module-025.js +++ b/tests/jerry/fail/module-025.js @@ -14,4 +14,4 @@ */ /* Indirect exports must be checked if they are resolvable. */ -export { l } from "tests/jerry/es2015/module-export-01.js" +export { l } from "../es2015/module-export-01.js" diff --git a/tests/jerry/fail/module-026.js b/tests/jerry/fail/module-026.js index 2e3e2c87f..b4eef17ba 100644 --- a/tests/jerry/fail/module-026.js +++ b/tests/jerry/fail/module-026.js @@ -14,4 +14,4 @@ */ /* Can't have circular imports/exports. */ -export { b } from "tests/jerry/fail/module-027.js" +export { b } from "./module-027.js" diff --git a/tests/jerry/fail/module-027.js b/tests/jerry/fail/module-027.js index 88083813a..87b62a9da 100644 --- a/tests/jerry/fail/module-027.js +++ b/tests/jerry/fail/module-027.js @@ -14,4 +14,4 @@ */ /* Can't have circular imports/exports. */ -export { b } from "tests/jerry/fail/module-026.js" +export { b } from "./module-026.js" diff --git a/tests/jerry/fail/module-028.js b/tests/jerry/fail/module-028.js index 4eb2ae1c1..908dc0a99 100644 --- a/tests/jerry/fail/module-028.js +++ b/tests/jerry/fail/module-028.js @@ -14,4 +14,4 @@ */ /* Ambiguous import */ -import { x } from "tests/jerry/es2015/module-export-05.js" +import { x } from "../es2015/module-export-05.js" diff --git a/tests/jerry/fail/module-029.js b/tests/jerry/fail/module-029.js index 0e22465f4..bd55c1ab6 100644 --- a/tests/jerry/fail/module-029.js +++ b/tests/jerry/fail/module-029.js @@ -14,4 +14,4 @@ */ /* Import/export statements must be in the global scope. */ -Function('','import { c } from "tests/jerry/es2015/module-export-01.js";') +Function('','import { c } from "../es2015/module-export-01.js";') diff --git a/tests/jerry/fail/module-030.js b/tests/jerry/fail/module-030.js index 961513f27..77f2528a4 100644 --- a/tests/jerry/fail/module-030.js +++ b/tests/jerry/fail/module-030.js @@ -14,4 +14,4 @@ */ /* No default export found. */ -import def from "tests/jerry/es2015/module-export-06.js" +import def from "../es2015/module-export-06.js" diff --git a/tests/jerry/fail/regression-test-issue-2896.js b/tests/jerry/fail/regression-test-issue-2896.js index 0e4b06f02..415bc49d6 100644 --- a/tests/jerry/fail/regression-test-issue-2896.js +++ b/tests/jerry/fail/regression-test-issue-2896.js @@ -13,4 +13,4 @@ // limitations under the License. export {} from "dummy.js"; -export {} from "tests/jerry/es2015/module-export-04.js"; +export {} from "../es2015/module-export-04.js";