From a869f9ba6c8366cfb8794241b9f52043c8c34568 Mon Sep 17 00:00:00 2001 From: Yonggang Luo Date: Sat, 6 Feb 2021 09:44:33 -0800 Subject: [PATCH] Add testcase that cleanup error throw from javascript properly. JerryScript-DCO-1.0-Signed-off-by: Yonggang Luo luoyonggang@gmail.com --- tests/unit-ext/module/jerry-module-test.c | 21 +++++++++++++++- tests/unit-ext/module/my-custom-module.c | 30 ++++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/tests/unit-ext/module/jerry-module-test.c b/tests/unit-ext/module/jerry-module-test.c index f2ebc027fe..d8352ed84c 100644 --- a/tests/unit-ext/module/jerry-module-test.c +++ b/tests/unit-ext/module/jerry-module-test.c @@ -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');"; @@ -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. */ @@ -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) { @@ -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 */ diff --git a/tests/unit-ext/module/my-custom-module.c b/tests/unit-ext/module/my-custom-module.c index f5a3e7d218..2395bfcf00 100644 --- a/tests/unit-ext/module/my-custom-module.c +++ b/tests/unit-ext/module/my-custom-module.c @@ -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)