Rework module linking (#4632)

The module linking process from jerry_parse is moved out into
a new jerry_module_link function, and jerry_parse is limited to
create unlinked modules.

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2021-03-30 15:40:09 +02:00
committed by GitHub
parent 874a6a49d5
commit 6c484f3529
22 changed files with 1574 additions and 789 deletions
+285 -74
View File
@@ -13,9 +13,6 @@
* limitations under the License.
*/
#if !defined (_WIN32)
#include <libgen.h>
#endif /* !defined (_WIN32) */
#include <limits.h>
#include <stdarg.h>
#include <stdlib.h>
@@ -104,102 +101,316 @@ jerry_port_release_source (uint8_t *buffer_p) /**< buffer to free */
} /* jerry_port_release_source */
/**
* Normalize a file path
* Computes the end of the directory part of a path.
*
* @return length of the path written to the output buffer
* @return end of the directory part of a path.
*/
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 */
static size_t
jerry_port_get_directory_end (const jerry_char_t *path_p) /**< path */
{
size_t ret = 0;
const jerry_char_t *end_p = path_p + strlen ((const char *) path_p);
while (end_p > path_p)
{
#if defined (_WIN32)
size_t base_drive_dir_len;
const size_t in_path_len = strnlen (in_path_p, _MAX_PATH);
if (end_p[-1] == '/' || end_p[-1] == '\\')
{
return (size_t) (end_p - path_p);
}
#else /* !_WIN32 */
if (end_p[-1] == '/')
{
return (size_t) (end_p - path_p);
}
#endif /* _WIN32 */
end_p--;
}
return 0;
} /* jerry_port_get_directory_end */
/**
* Normalize a file path.
*
* @return a newly allocated buffer with the normalized path if the operation is successful,
* NULL otherwise
*/
static jerry_char_t *
jerry_port_normalize_path (const jerry_char_t *in_path_p, /**< path to the referenced module */
size_t in_path_length, /**< length of the path */
const jerry_char_t *base_path_p, /**< base path */
size_t base_path_length) /**< length of the base path */
{
char *path_p;
if (base_file_p != NULL)
if (base_path_length > 0)
{
char drive[_MAX_DRIVE];
char *dir_p = (char *) malloc (_MAX_DIR);
path_p = (char *) malloc (base_path_length + in_path_length + 1);
_splitpath_s (base_file_p, drive, _MAX_DRIVE, dir_p, _MAX_DIR, NULL, 0, NULL, 0);
const size_t drive_len = strnlen (drive, _MAX_DRIVE);
const size_t dir_len = strnlen (dir_p, _MAX_DIR);
base_drive_dir_len = drive_len + dir_len;
path_p = (char *) malloc (base_drive_dir_len + in_path_len + 1);
if (path_p == NULL)
{
return NULL;
}
memcpy (path_p, &drive, drive_len);
memcpy (path_p + drive_len, dir_p, dir_len);
free (dir_p);
memcpy (path_p, base_path_p, base_path_length);
memcpy (path_p + base_path_length, in_path_p, in_path_length);
path_p[base_path_length + in_path_length] = '\0';
}
else
{
base_drive_dir_len = 0;
path_p = (char *) malloc (in_path_len + 1);
}
path_p = (char *) malloc (in_path_length + 1);
memcpy (path_p + base_drive_dir_len, in_path_p, in_path_len + 1);
char *norm_p = _fullpath (out_buf_p, path_p, out_buf_size);
free (path_p);
if (norm_p != NULL)
{
ret = strnlen (norm_p, out_buf_size);
}
#elif defined (__unix__) || defined (__APPLE__)
char *base_dir_p = dirname (base_file_p);
const size_t base_dir_len = strnlen (base_dir_p, PATH_MAX);
const size_t in_path_len = strnlen (in_path_p, PATH_MAX);
char *path_p = (char *) malloc (base_dir_len + 1 + in_path_len + 1);
memcpy (path_p, base_dir_p, base_dir_len);
memcpy (path_p + base_dir_len, "/", 1);
memcpy (path_p + base_dir_len + 1, in_path_p, in_path_len + 1);
char *norm_p = realpath (path_p, NULL);
free (path_p);
if (norm_p != NULL)
{
const size_t norm_len = strnlen (norm_p, out_buf_size);
if (norm_len < out_buf_size)
if (path_p == NULL)
{
memcpy (out_buf_p, norm_p, norm_len + 1);
ret = norm_len;
return NULL;
}
free (norm_p);
memcpy (path_p, in_path_p, in_path_length);
path_p[in_path_length] = '\0';
}
#else
(void) base_file_p; /* unused */
/* Do nothing, just copy the input. */
const size_t in_path_len = strnlen (in_path_p, out_buf_size);
if (in_path_len < out_buf_size)
#if defined (_WIN32)
char full_path[_MAX_PATH];
if (_fullpath (full_path, path_p, _MAX_PATH) != NULL)
{
memcpy (out_buf_p, in_path_p, in_path_len + 1);
ret = in_path_len;
}
#endif
free (path_p);
return ret;
size_t full_path_len = strlen (full_path);
path_p = (char *) malloc (full_path_len + 1);
if (path_p == NULL)
{
return NULL;
}
memcpy (path_p, full_path, full_path_len + 1);
}
#elif defined (__unix__) || defined (__APPLE__)
char *norm_p = realpath (path_p, NULL);
if (norm_p != NULL)
{
free (path_p);
path_p = norm_p;
}
#endif /* _WIN32 */
return (jerry_char_t *) path_p;
} /* jerry_port_normalize_path */
/**
* Get the module object of a native module.
* A module descriptor.
*/
typedef struct jerry_port_module_t
{
struct jerry_port_module_t *next_p; /**< next_module */
jerry_char_t *path_p; /**< path to the module */
size_t base_path_length; /**< base path length for relative difference */
jerry_value_t realm; /**< the realm of the module */
jerry_value_t module; /**< the module itself */
} jerry_port_module_t;
/**
* Native info descriptor for modules.
*/
static const jerry_object_native_info_t jerry_port_module_native_info =
{
.free_cb = NULL,
};
/**
* Default module manager.
*/
typedef struct
{
jerry_port_module_t *module_head_p; /**< first module */
} jerry_port_module_manager_t;
/**
* Release known modules.
*/
static void
jerry_port_module_free (jerry_port_module_manager_t *manager_p, /**< module manager */
const jerry_value_t realm) /**< if this argument is object, release only those modules,
* which realm value is equal to this argument. */
{
jerry_port_module_t *module_p = manager_p->module_head_p;
bool release_all = !jerry_value_is_object (realm);
jerry_port_module_t *prev_p = NULL;
while (module_p != NULL)
{
jerry_port_module_t *next_p = module_p->next_p;
if (release_all || module_p->realm == realm)
{
free (module_p->path_p);
jerry_release_value (module_p->realm);
jerry_release_value (module_p->module);
free (module_p);
if (prev_p == NULL)
{
manager_p->module_head_p = next_p;
}
else
{
prev_p->next_p = next_p;
}
}
else
{
prev_p = module_p;
}
module_p = next_p;
}
} /* jerry_port_module_free */
/**
* Initialize the default module manager.
*/
static void
jerry_port_module_manager_init (void *user_data_p)
{
((jerry_port_module_manager_t *) user_data_p)->module_head_p = NULL;
} /* jerry_port_module_manager_init */
/**
* Deinitialize the default module manager.
*/
static void
jerry_port_module_manager_deinit (void *user_data_p) /**< context pointer to deinitialize */
{
jerry_value_t undef = jerry_create_undefined ();
jerry_port_module_free ((jerry_port_module_manager_t *) user_data_p, undef);
jerry_release_value (undef);
} /* jerry_port_module_manager_deinit */
/**
* Declare the context data manager for modules.
*/
static const jerry_context_data_manager_t jerry_port_module_manager =
{
.init_cb = jerry_port_module_manager_init,
.deinit_cb = jerry_port_module_manager_deinit,
.bytes_needed = sizeof (jerry_port_module_manager_t)
};
/**
* Default module resolver.
*
* @return Undefined, if 'name' is not a native module
* jerry_value_t containing the module object, otherwise
* @return a module object if resolving is successful, an error otherwise
*/
jerry_value_t
jerry_port_get_native_module (jerry_value_t name) /**< module specifier */
jerry_port_module_resolve (const jerry_value_t specifier, /**< module specifier string */
const jerry_value_t referrer, /**< parent module */
void *user_p) /**< user data */
{
(void) name;
return jerry_create_undefined ();
} /* jerry_port_get_native_module */
(void) user_p;
jerry_port_module_t *module_p;
const jerry_char_t *base_path_p = NULL;
size_t base_path_length = 0;
if (jerry_get_object_native_pointer (referrer, (void **) &module_p, &jerry_port_module_native_info))
{
base_path_p = module_p->path_p;
base_path_length = module_p->base_path_length;
}
jerry_size_t in_path_length = jerry_get_utf8_string_size (specifier);
jerry_char_t *in_path_p = (jerry_char_t *) malloc (in_path_length + 1);
jerry_string_to_utf8_char_buffer (specifier, in_path_p, in_path_length);
in_path_p[in_path_length] = '\0';
jerry_char_t *path_p = jerry_port_normalize_path (in_path_p, in_path_length, base_path_p, base_path_length);
if (path_p == NULL)
{
return jerry_create_error (JERRY_ERROR_COMMON, (const jerry_char_t *) "Out of memory");
}
jerry_value_t realm = jerry_get_global_object ();
jerry_port_module_manager_t *manager_p;
manager_p = (jerry_port_module_manager_t *) jerry_get_context_data (&jerry_port_module_manager);
module_p = manager_p->module_head_p;
while (module_p != NULL)
{
if (module_p->realm == realm
&& strcmp ((const char *) module_p->path_p, (const char *) path_p) == 0)
{
free (path_p);
free (in_path_p);
jerry_release_value (realm);
return jerry_acquire_value (module_p->module);
}
module_p = module_p->next_p;
}
size_t source_size;
uint8_t *source_p = jerry_port_read_source ((const char *) path_p, &source_size);
if (source_p == NULL)
{
free (path_p);
free (in_path_p);
jerry_release_value (realm);
/* TODO: This is incorrect, but makes test262 module tests pass
* (they should throw SyntaxError, but not because the module cannot be found). */
return jerry_create_error (JERRY_ERROR_SYNTAX, (const jerry_char_t *) "Module file not found");
}
jerry_parse_options_t parse_options;
parse_options.options = JERRY_PARSE_MODULE | JERRY_PARSE_HAS_RESOURCE;
parse_options.resource_name_p = (jerry_char_t *) in_path_p;
parse_options.resource_name_length = (size_t) in_path_length;
jerry_value_t ret_value = jerry_parse (source_p,
source_size,
&parse_options);
jerry_port_release_source (source_p);
free (in_path_p);
if (jerry_value_is_error (ret_value))
{
free (path_p);
jerry_release_value (realm);
return ret_value;
}
module_p = (jerry_port_module_t *) malloc (sizeof (jerry_port_module_t));
module_p->next_p = manager_p->module_head_p;
module_p->path_p = path_p;
module_p->base_path_length = jerry_port_get_directory_end (module_p->path_p);
module_p->realm = realm;
module_p->module = jerry_acquire_value (ret_value);
jerry_set_object_native_pointer (ret_value, module_p, &jerry_port_module_native_info);
manager_p->module_head_p = module_p;
return ret_value;
} /* jerry_port_module_resolve */
/**
* Release known modules.
*/
void
jerry_port_module_release (const jerry_value_t realm) /**< if this argument is object, release only those modules,
* which realm value is equal to this argument. */
{
jerry_port_module_free ((jerry_port_module_manager_t *) jerry_get_context_data (&jerry_port_module_manager),
realm);
} /* jerry_port_module_release */