Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion tests/unit-ext/module/jerry-module-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include "test-common.h"

/* Load a module. */
const char eval_string1[] = "require ('my_custom_module');";
const char eval_string1[] = "require ('my_custom_module').number_value;";

/* Load a module using a different resolver. */
const char eval_string2[] = "require ('differently-handled-module');";
Expand Down Expand Up @@ -74,6 +74,15 @@ const char eval_string7[] = "(function() {"
" return x !== y ? 1 : 0;"
"}) ();";

/* Make sure the entire cache is cleared. */
const char eval_string8[] =
"(function() {"
" var custom_module = require ('my_custom_module');"
" custom_module.call_function_with_callback(function(){"
" throw '12312391238219423914832091480921834028130948213904812093849023814902183490218394082190348'"
" });"
"}) ();";

/*
* Define a resolver for a module named "differently-handled-module" to check that custom resolvers work.
*/
Expand Down Expand Up @@ -155,6 +164,12 @@ assert_number (jerry_value_t js_value, double expected_result)
TEST_ASSERT (jerry_value_as_number (js_value) == expected_result);
} /* assert_number */

static jerry_value_t
eval_string (const char *the_string)
{
return jerry_eval ((const jerry_char_t *) the_string, strlen (the_string), JERRY_PARSE_STRICT_MODE);
} /* eval_string */

static void
eval_one (const char *the_string, double expected_result)
{
Expand Down Expand Up @@ -214,5 +229,9 @@ main (int argc, char **argv)
eval_one (eval_string6, 1);
eval_one (eval_string7, 1);

jerry_value_t val_err = eval_string (eval_string8);
TEST_ASSERT (jerry_value_is_exception (val_err));
jerry_value_free (val_err);

jerry_cleanup ();
} /* main */
30 changes: 29 additions & 1 deletion tests/unit-ext/module/my-custom-module.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,38 @@

#define MODULE_NAME my_custom_module

static void
jobject_set_property_jval (jerry_value_t jobj, const char *name, jerry_value_t value)
{
jerry_value_t prop_name = jerry_string_sz (name);
jerry_value_t ret_val = jerry_object_set (jobj, prop_name, value);
jerry_value_free (prop_name);
jerry_value_free (ret_val);
} /* jobject_set_property_jval */

static jerry_value_t
call_function_with_callback (const jerry_call_info_t *call_info_p,
const jerry_value_t jargv[],
const jerry_length_t jargc)
{
(void) jargc;
jerry_value_t jval_func = jargv[0];
return jerry_call (jval_func, call_info_p->this_value, NULL, 0);
} /* call_function_with_callback */

static jerry_value_t
my_custom_module_on_resolve (void)
{
return jerry_number (42);
jerry_value_t mymodule = jerry_object ();
jerry_value_t val = jerry_number (42);
jobject_set_property_jval (mymodule, "number_value", val);
jerry_value_free (val);

jerry_value_t jfunc = jerry_function_external (call_function_with_callback);
jobject_set_property_jval (mymodule, "call_function_with_callback", jfunc);
jerry_value_free (jfunc);

return mymodule;
} /* my_custom_module_on_resolve */

JERRYX_NATIVE_MODULE (MODULE_NAME, my_custom_module_on_resolve)
Loading