mirror of
https://github.com/jerryscript-project/jerryscript.git
synced 2025-12-15 16:29:21 +00:00
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
288 lines
7.6 KiB
C
288 lines
7.6 KiB
C
/* Copyright JS Foundation and other contributors, http://js.foundation
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
#if !defined (WIN32)
|
|
#include <libgen.h>
|
|
#endif /* !defined (WIN32) */
|
|
#include <limits.h>
|
|
#include <stdarg.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#include "jerryscript-port.h"
|
|
#include "jerryscript-port-default.h"
|
|
#include "jerryscript-debugger.h"
|
|
|
|
#ifndef DISABLE_EXTRA_API
|
|
|
|
/**
|
|
* Actual log level
|
|
*/
|
|
static jerry_log_level_t jerry_port_default_log_level = JERRY_LOG_LEVEL_ERROR;
|
|
|
|
#define JERRY_PORT_DEFAULT_LOG_LEVEL jerry_port_default_log_level
|
|
|
|
/**
|
|
* Get the log level
|
|
*
|
|
* @return current log level
|
|
*
|
|
* Note:
|
|
* This function is only available if the port implementation library is
|
|
* compiled without the DISABLE_EXTRA_API macro.
|
|
*/
|
|
jerry_log_level_t
|
|
jerry_port_default_get_log_level (void)
|
|
{
|
|
return jerry_port_default_log_level;
|
|
} /* jerry_port_default_get_log_level */
|
|
|
|
/**
|
|
* Set the log level
|
|
*
|
|
* Note:
|
|
* This function is only available if the port implementation library is
|
|
* compiled without the DISABLE_EXTRA_API macro.
|
|
*/
|
|
void
|
|
jerry_port_default_set_log_level (jerry_log_level_t level) /**< log level */
|
|
{
|
|
jerry_port_default_log_level = level;
|
|
} /* jerry_port_default_set_log_level */
|
|
|
|
#else /* DISABLE_EXTRA_API */
|
|
#define JERRY_PORT_DEFAULT_LOG_LEVEL JERRY_LOG_LEVEL_ERROR
|
|
#endif /* !DISABLE_EXTRA_API */
|
|
|
|
/**
|
|
* Default implementation of jerry_port_log. Prints log message to the standard
|
|
* error with 'vfprintf' if message log level is less than or equal to the
|
|
* current log level.
|
|
*
|
|
* If debugger support is enabled, printing happens first to an in-memory buffer,
|
|
* which is then sent both to the standard error and to the debugger client.
|
|
*
|
|
* Note:
|
|
* Changing the log level from JERRY_LOG_LEVEL_ERROR is only possible if
|
|
* the port implementation library is compiled without the
|
|
* DISABLE_EXTRA_API macro.
|
|
*/
|
|
void
|
|
jerry_port_log (jerry_log_level_t level, /**< message log level */
|
|
const char *format, /**< format string */
|
|
...) /**< parameters */
|
|
{
|
|
if (level <= JERRY_PORT_DEFAULT_LOG_LEVEL)
|
|
{
|
|
va_list args;
|
|
va_start (args, format);
|
|
#if defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1)
|
|
int length = vsnprintf (NULL, 0, format, args);
|
|
va_end (args);
|
|
va_start (args, format);
|
|
|
|
JERRY_VLA (char, buffer, length + 1);
|
|
vsnprintf (buffer, (size_t) length + 1, format, args);
|
|
|
|
fprintf (stderr, "%s", buffer);
|
|
jerry_debugger_send_log (level, (jerry_char_t *) buffer, (jerry_size_t) length);
|
|
#else /* If jerry-debugger isn't defined, libc is turned on */
|
|
vfprintf (stderr, format, args);
|
|
#endif /* defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) */
|
|
va_end (args);
|
|
}
|
|
} /* jerry_port_log */
|
|
|
|
#if defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1)
|
|
|
|
#define DEBUG_BUFFER_SIZE (256)
|
|
static char debug_buffer[DEBUG_BUFFER_SIZE];
|
|
static int debug_buffer_index = 0;
|
|
|
|
#endif /* defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) */
|
|
|
|
/**
|
|
* Default implementation of jerry_port_print_char. Uses 'putchar' to
|
|
* print a single character to standard output.
|
|
*/
|
|
void
|
|
jerry_port_print_char (char c) /**< the character to print */
|
|
{
|
|
putchar (c);
|
|
|
|
#if defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1)
|
|
debug_buffer[debug_buffer_index++] = c;
|
|
|
|
if ((debug_buffer_index == DEBUG_BUFFER_SIZE) || (c == '\n'))
|
|
{
|
|
jerry_debugger_send_output ((jerry_char_t *) debug_buffer, (jerry_size_t) debug_buffer_index);
|
|
debug_buffer_index = 0;
|
|
}
|
|
#endif /* defined (JERRY_DEBUGGER) && (JERRY_DEBUGGER == 1) */
|
|
} /* jerry_port_print_char */
|
|
|
|
/**
|
|
* Determines the size of the given file.
|
|
* @return size of the file
|
|
*/
|
|
static size_t
|
|
jerry_port_get_file_size (FILE *file_p) /**< opened file */
|
|
{
|
|
fseek (file_p, 0, SEEK_END);
|
|
long size = ftell (file_p);
|
|
fseek (file_p, 0, SEEK_SET);
|
|
|
|
return (size_t) size;
|
|
} /* jerry_port_get_file_size */
|
|
|
|
/**
|
|
* Opens file with the given path and reads its source.
|
|
* @return the source of the file
|
|
*/
|
|
uint8_t *
|
|
jerry_port_read_source (const char *file_name_p, /**< file name */
|
|
size_t *out_size_p) /**< [out] read bytes */
|
|
{
|
|
FILE *file_p = fopen (file_name_p, "rb");
|
|
|
|
if (file_p == NULL)
|
|
{
|
|
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to open file: %s\n", file_name_p);
|
|
return NULL;
|
|
}
|
|
|
|
size_t file_size = jerry_port_get_file_size (file_p);
|
|
uint8_t *buffer_p = (uint8_t *) malloc (file_size);
|
|
|
|
if (buffer_p == NULL)
|
|
{
|
|
fclose (file_p);
|
|
|
|
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to allocate memory for module");
|
|
return NULL;
|
|
}
|
|
|
|
size_t bytes_read = fread (buffer_p, 1u, file_size, file_p);
|
|
|
|
if (!bytes_read)
|
|
{
|
|
fclose (file_p);
|
|
free (buffer_p);
|
|
|
|
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: failed to read file: %s\n", file_name_p);
|
|
return NULL;
|
|
}
|
|
|
|
fclose (file_p);
|
|
*out_size_p = bytes_read;
|
|
|
|
return buffer_p;
|
|
} /* jerry_port_read_source */
|
|
|
|
/**
|
|
* Release the previously opened file's content.
|
|
*/
|
|
void
|
|
jerry_port_release_source (uint8_t *buffer_p) /**< buffer to free */
|
|
{
|
|
free (buffer_p);
|
|
} /* jerry_port_release_source */
|
|
|
|
/**
|
|
* Normalize a file path
|
|
*
|
|
* @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 */
|
|
char *base_file_p) /**< base file path */
|
|
{
|
|
size_t ret = 0;
|
|
|
|
#if defined (WIN32)
|
|
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__)
|
|
#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)
|
|
{
|
|
const size_t len = strnlen (norm_p, out_buf_size);
|
|
if (len < out_buf_size)
|
|
{
|
|
strncpy (out_buf_p, norm_p, out_buf_size);
|
|
ret = len;
|
|
}
|
|
}
|
|
|
|
free (buffer_p);
|
|
#undef MAX_JERRY_PATH_SIZE
|
|
#else
|
|
(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)
|
|
{
|
|
strncpy (out_buf_p, in_path_p, out_buf_size);
|
|
ret = len;
|
|
}
|
|
#endif
|
|
|
|
return ret;
|
|
} /* jerry_port_normalize_path */
|