From 26d48c3e28d8e49dd6593971e91392a30feffae6 Mon Sep 17 00:00:00 2001 From: Moe Ghasemi Date: Tue, 27 Aug 2019 13:52:46 -0700 Subject: [PATCH] Adding a switch to the jerry-snapshot tool to load a function (#3024) The -f and --generate-function-snapshot switch are added to the jerry-snapshot command line tool so the entire javascript input can be interpeted and loaded as a function snapshot. The function arguments must be given in the command line. Example: echo "return a + b;" > myfunc.js ./jerry-snapshot generate -f "a, b" myfunc.js JerryScript-DCO-1.0-Signed-off-by: Moe Ghasemi mghasemi@sierrawireless.com --- jerry-main/main-unix-snapshot.c | 40 +++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/jerry-main/main-unix-snapshot.c b/jerry-main/main-unix-snapshot.c index af66e2626..244f7ee62 100644 --- a/jerry-main/main-unix-snapshot.c +++ b/jerry-main/main-unix-snapshot.c @@ -198,6 +198,7 @@ typedef enum OPT_GENERATE_HELP, OPT_GENERATE_STATIC, OPT_GENERATE_SHOW_OP, + OPT_GENERATE_FUNCTION, OPT_GENERATE_OUT, OPT_IMPORT_LITERAL_LIST } generate_opt_id_t; @@ -211,6 +212,9 @@ static const cli_opt_t generate_opts[] = .help = "print this help and exit"), CLI_OPT_DEF (.id = OPT_GENERATE_STATIC, .opt = "s", .longopt = "static", .help = "generate static snapshot"), + CLI_OPT_DEF (.id = OPT_GENERATE_FUNCTION, .opt = "f", .longopt = "generate-function-snapshot", + .meta = "ARGUMENTS", + .help = "generate function snapshot with given arguments"), CLI_OPT_DEF (.id = OPT_IMPORT_LITERAL_LIST, .longopt = "load-literals-list-format", .meta = "FILE", .help = "import literals from list format (for static snapshots)"), @@ -241,6 +245,7 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */ uint8_t *source_p = input_buffer; size_t source_length = 0; const char *literals_file_name_p = NULL; + const char *function_args_p = NULL; cli_change_opts (cli_state_p, generate_opts); @@ -258,6 +263,11 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */ snapshot_flags |= JERRY_SNAPSHOT_SAVE_STATIC; break; } + case OPT_GENERATE_FUNCTION: + { + function_args_p = cli_consume_string (cli_state_p); + break; + } case OPT_IMPORT_LITERAL_LIST: { literals_file_name_p = cli_consume_string (cli_state_p); @@ -365,13 +375,29 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */ } jerry_value_t snapshot_result; - snapshot_result = jerry_generate_snapshot ((jerry_char_t *) file_name_p, - (size_t) strlen (file_name_p), - (jerry_char_t *) source_p, - source_length, - snapshot_flags, - output_buffer, - sizeof (output_buffer) / sizeof (uint32_t)); + + if (function_args_p != NULL) + { + snapshot_result = jerry_generate_function_snapshot ((jerry_char_t *) file_name_p, + (size_t) strlen (file_name_p), + (jerry_char_t *) source_p, + source_length, + (const jerry_char_t *) function_args_p, + strlen (function_args_p), + snapshot_flags, + output_buffer, + sizeof (output_buffer) / sizeof (uint32_t)); + } + else + { + snapshot_result = jerry_generate_snapshot ((jerry_char_t *) file_name_p, + (size_t) strlen (file_name_p), + (jerry_char_t *) source_p, + source_length, + snapshot_flags, + output_buffer, + sizeof (output_buffer) / sizeof (uint32_t)); + } if (jerry_value_is_error (snapshot_result)) {