Skip to content

Commit 332e216

Browse files
Daniel Ballarerobika
authored andcommitted
Add runtime option "--call-on-exit [FUNCNAME]" to main-unix.c (#3518)
With this option you can call a function after the user script and promises have ran, to be able to do assertions that are executed just before the process would exit. JerryScript-DCO-1.0-Signed-off-by: Daniel Balla dballa@inf.u-szeged.hu
1 parent 4a331b2 commit 332e216

3 files changed

Lines changed: 45 additions & 7 deletions

File tree

jerry-main/main-unix.c

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,8 @@ typedef enum
325325
OPT_EXEC_SNAP,
326326
OPT_EXEC_SNAP_FUNC,
327327
OPT_LOG_LEVEL,
328-
OPT_NO_PROMPT
328+
OPT_NO_PROMPT,
329+
OPT_CALL_ON_EXIT
329330
} main_opt_id_t;
330331

331332
/**
@@ -365,6 +366,8 @@ static const cli_opt_t main_opts[] =
365366
.help = "set log level (0-3)"),
366367
CLI_OPT_DEF (.id = OPT_NO_PROMPT, .longopt = "no-prompt",
367368
.help = "don't print prompt in REPL mode"),
369+
CLI_OPT_DEF (.id = OPT_CALL_ON_EXIT, .longopt = "call-on-exit", .meta = "STRING",
370+
.help = "invoke the specified function when the process is just about to exit"),
368371
CLI_OPT_DEF (.id = CLI_OPT_DEFAULT, .meta = "FILE",
369372
.help = "input JS file(s) (If file is -, read standard input.)")
370373
};
@@ -488,6 +491,8 @@ main (int argc,
488491
bool is_wait_mode = false;
489492
bool no_prompt = false;
490493

494+
const char *exit_cb = NULL;
495+
491496
cli_state_t cli_state = cli_init (main_opts, argc - 1, argv + 1);
492497
for (int id = cli_consume_option (&cli_state); id != CLI_OPT_END; id = cli_consume_option (&cli_state))
493498
{
@@ -530,6 +535,11 @@ main (int argc,
530535
}
531536
break;
532537
}
538+
case OPT_CALL_ON_EXIT:
539+
{
540+
exit_cb = cli_consume_string (&cli_state);
541+
break;
542+
}
533543
case OPT_SHOW_RE_OP:
534544
{
535545
if (check_feature (JERRY_FEATURE_REGEXP_DUMP, cli_state.arg))
@@ -935,6 +945,32 @@ main (int argc,
935945

936946
jerry_release_value (ret_value);
937947

948+
if (exit_cb != NULL)
949+
{
950+
jerry_value_t global = jerry_get_global_object ();
951+
jerry_value_t fn_str = jerry_create_string ((jerry_char_t *) exit_cb);
952+
jerry_value_t callback_fn = jerry_get_property (global, fn_str);
953+
954+
jerry_release_value (global);
955+
jerry_release_value (fn_str);
956+
957+
if (jerry_value_is_function (callback_fn))
958+
{
959+
jerry_value_t ret_val = jerry_call_function (callback_fn, jerry_create_undefined (), NULL, 0);
960+
961+
if (jerry_value_is_error (ret_val))
962+
{
963+
ret_val = jerry_get_value_from_error (ret_val, true);
964+
print_unhandled_exception (ret_val);
965+
ret_code = JERRY_STANDALONE_EXIT_CODE_FAIL;
966+
}
967+
968+
jerry_release_value (ret_val);
969+
}
970+
971+
jerry_release_value (callback_fn);
972+
}
973+
938974
jerry_cleanup ();
939975
#if defined (JERRY_EXTERNAL_CONTEXT) && (JERRY_EXTERNAL_CONTEXT == 1)
940976
free (context_p);

tests/jerry/es2015/regression-test-issue-2544.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
// limitations under the License.
1414

1515
Object.defineProperty(Array.prototype, 0, { get : function () { throw $; } });
16-
var asyncPassed = false;
17-
Promise.race([ , this]).then(Error).catch(function(err) { asyncPassed = (err instanceof ReferenceError); });
18-
new Promise(function() {
19-
throw 5;
20-
}).then(Error).catch(function() { assert(asyncPassed); });
16+
var global_err = undefined;
17+
Promise.race([ , this]).then(Error).catch(function(err) { global_err = err; });
18+
19+
function __checkAsync() {
20+
assert(global_err instanceof ReferenceError);
21+
}

tools/runners/run-test-suite.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def run_normal_tests(args, tests):
127127
test_cmd = get_platform_cmd_prefix()
128128
if args.runtime:
129129
test_cmd.append(args.runtime)
130-
test_cmd.append(args.engine)
130+
test_cmd.extend([args.engine, '--call-on-exit', '__checkAsync'])
131131

132132
total = len(tests)
133133
tested = 0
@@ -161,6 +161,7 @@ def run_snapshot_tests(args, tests):
161161
generate_snapshot_cmd.append(args.runtime)
162162

163163
execute_snapshot_cmd.extend([args.engine, '--exec-snapshot', 'js.snapshot'])
164+
execute_snapshot_cmd.extend(['--call-on-exit', '__checkAsync'])
164165

165166
# engine: jerry[.exe] -> snapshot generator: jerry-snapshot[.exe]
166167
engine = os.path.splitext(args.engine)

0 commit comments

Comments
 (0)