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
This commit is contained in:
Moe Ghasemi 2019-08-27 13:52:46 -07:00 committed by Robert Fancsik
parent 744a64c6fd
commit 26d48c3e28

View File

@ -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))
{