Resolve module paths relative to the current module (#2976)

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 <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:
Dániel Bátyai
2019-07-17 14:44:18 +02:00
committed by GitHub
parent 6c441091b4
commit ec11a7b4e9
47 changed files with 233 additions and 129 deletions
+50 -8
View File
@@ -13,6 +13,9 @@
* limitations under the License.
*/
#if !defined (WIN32)
#include <libgen.h>
#endif /* !defined (WIN32) */
#include <limits.h>
#include <stdarg.h>
#include <stdlib.h>
@@ -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)
{