module extension: add support for canonical name resolution (#2013)

Before attempting to load a module, each provided resolver must be given an
opportunity to examine the name of the requested module without actually
loading it so as to canonicalize it, in case a module can be referred to by
multiple names.

Then, modules are loaded and cached by their canonical name.

JerryScript-DCO-1.0-Signed-off-by: Gabriel Schulhof gabriel.schulhof@intel.com
This commit is contained in:
Gabriel "_|Nix|_" Schulhof
2017-09-29 13:02:34 +03:00
committed by Zoltan Herczeg
parent e527e41bac
commit 6d53931055
6 changed files with 329 additions and 77 deletions
+30 -9
View File
@@ -30,7 +30,7 @@ typedef jerry_value_t (*jerryx_native_module_on_resolve_t) (void);
typedef struct jerryx_native_module_t
{
const jerry_char_t *name_p; /**< name of the module */
const jerryx_native_module_on_resolve_t on_resolve; /**< function that returns a new instance of the module */
const jerryx_native_module_on_resolve_t on_resolve_p; /**< function that returns a new instance of the module */
struct jerryx_native_module_t *next_p; /**< pointer to next module in the list */
} jerryx_native_module_t;
@@ -58,7 +58,7 @@ typedef struct jerryx_native_module_t
static jerryx_native_module_t _ ## module_name ## _definition = \
{ \
.name_p = (jerry_char_t *) #module_name, \
.on_resolve = (on_resolve_cb), \
.on_resolve_p = (on_resolve_cb), \
.next_p = NULL \
}; \
\
@@ -91,21 +91,42 @@ void jerryx_native_module_register (jerryx_native_module_t *module_p);
*/
void jerryx_native_module_unregister (jerryx_native_module_t *module_p);
/**
* Declare the function pointer type for canonical name resolution.
*/
typedef jerry_value_t (*jerryx_module_get_canonical_name_t) (const jerry_value_t name); /**< The name for which to
* compute the canonical
* name */
/**
* Declare the function pointer type for module resolution.
*/
typedef bool (*jerryx_module_resolve_t) (const jerry_value_t canonical_name, /**< The module's canonical name */
jerry_value_t *result); /**< The resulting module, if the function returns
* true */
/**
* Declare the structure for module resolvers.
*/
typedef struct
{
jerryx_module_get_canonical_name_t get_canonical_name_p; /**< function pointer to establish the canonical name of a
* module */
jerryx_module_resolve_t resolve_p; /**< function pointer to resolve a module */
} jerryx_module_resolver_t;
/**
* Declare the JerryScript module resolver so that it may be added to an array of jerryx_module_resolver_t items and
* thus passed to jerryx_module_resolve.
*/
bool jerryx_module_native_resolver (const jerry_char_t *name, jerry_value_t *result);
/**
* Declare the function pointer type for module resolvers.
*/
typedef bool (*jerryx_module_resolver_t) (const jerry_char_t *name, jerry_value_t *result);
extern jerryx_module_resolver_t jerryx_module_native_resolver;
/**
* Load a copy of a module into the current context using the provided module resolvers, or return one that was already
* loaded if it is found.
*/
jerry_value_t jerryx_module_resolve (const jerry_char_t *name, const jerryx_module_resolver_t *resolvers, size_t count);
jerry_value_t jerryx_module_resolve (const jerry_value_t name,
const jerryx_module_resolver_t **resolvers,
size_t count);
#endif /* !JERRYX_MODULE_H */