Rework snapshot generation API. (#2259)

Also remove eval context support. It provides no practical advantage.

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2018-04-05 13:43:55 +02:00
committed by GitHub
parent 35926f3f85
commit 7b226f53e0
8 changed files with 477 additions and 569 deletions
+66 -57
View File
@@ -13,6 +13,7 @@
* limitations under the License.
*/
#include <assert.h>
#include <string.h>
#include "jerryscript.h"
@@ -120,6 +121,42 @@ read_file (uint8_t *input_pos_p, /**< next position in the input buffer */
return bytes_read;
} /* read_file */
/**
* Print error value
*/
static void
print_unhandled_exception (jerry_value_t error_value) /**< error value */
{
assert (!jerry_value_has_error_flag (error_value));
jerry_value_t err_str_val = jerry_value_to_string (error_value);
if (jerry_value_has_error_flag (err_str_val))
{
/* Avoid recursive error throws. */
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Snapshot error: [value cannot be converted to string]\n");
jerry_release_value (err_str_val);
return;
}
jerry_size_t err_str_size = jerry_get_string_size (err_str_val);
if (err_str_size >= 256)
{
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Snapshot error: [value cannot be converted to string]\n");
jerry_release_value (err_str_val);
return;
}
jerry_char_t err_str_buf[256];
jerry_size_t string_end = jerry_string_to_char_buffer (err_str_val, err_str_buf, err_str_size);
assert (string_end == err_str_size);
err_str_buf[string_end] = 0;
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Snapshot error: %s\n", (char *) err_str_buf);
jerry_release_value (err_str_val);
} /* print_unhandled_exception */
/**
* Generate command line option IDs
*/
@@ -127,7 +164,6 @@ typedef enum
{
OPT_GENERATE_HELP,
OPT_GENERATE_STATIC,
OPT_GENERATE_CONTEXT,
OPT_GENERATE_LITERAL_LIST,
OPT_GENERATE_LITERAL_C,
OPT_GENERATE_SHOW_OP,
@@ -143,10 +179,6 @@ 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_CONTEXT, .opt = "c", .longopt = "context",
.meta = "MODE",
.help = "specify the execution context of the snapshot: "
"global or eval (default: global)."),
CLI_OPT_DEF (.id = OPT_GENERATE_LITERAL_LIST, .longopt = "save-literals-list-format",
.meta = "FILE",
.help = "export literals found in parsed JS input (in list format)"),
@@ -174,14 +206,13 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
(void) argc;
bool is_save_literals_mode_in_c_format = false;
bool is_snapshot_mode_for_global = true;
uint32_t snapshot_flags = 0;
jerry_init_flag_t flags = JERRY_INIT_EMPTY;
uint32_t number_of_files = 0;
const char *file_name_p = NULL;
uint8_t *source_p = input_buffer;
size_t source_length = 0;
const char *save_literals_file_name_p = NULL;
bool static_snapshot = false;
cli_change_opts (cli_state_p, generate_opts);
@@ -196,31 +227,7 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
}
case OPT_GENERATE_STATIC:
{
static_snapshot = true;
break;
}
case OPT_GENERATE_CONTEXT:
{
const char *mode_str_p = cli_consume_string (cli_state_p);
if (cli_state_p->error != NULL)
{
break;
}
if (!strcmp ("global", mode_str_p))
{
is_snapshot_mode_for_global = true;
}
else if (!strcmp ("eval", mode_str_p))
{
is_snapshot_mode_for_global = false;
}
else
{
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Incorrect argument for context mode: %s\n", mode_str_p);
return JERRY_STANDALONE_EXIT_CODE_FAIL;
}
snapshot_flags |= JERRY_SNAPSHOT_SAVE_STATIC;
break;
}
case OPT_GENERATE_LITERAL_LIST:
@@ -252,7 +259,13 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
}
case CLI_OPT_DEFAULT:
{
const char *file_name_p = cli_consume_string (cli_state_p);
if (file_name_p != NULL)
{
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Exactly one input file must be specified\n");
return JERRY_STANDALONE_EXIT_CODE_FAIL;
}
file_name_p = cli_consume_string (cli_state_p);
if (cli_state_p->error == NULL)
{
@@ -263,8 +276,6 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Input file is empty\n");
return JERRY_STANDALONE_EXIT_CODE_FAIL;
}
number_of_files++;
}
break;
}
@@ -281,7 +292,7 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
return JERRY_STANDALONE_EXIT_CODE_FAIL;
}
if (number_of_files != 1)
if (file_name_p == NULL)
{
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Exactly one input file must be specified\n");
return JERRY_STANDALONE_EXIT_CODE_FAIL;
@@ -295,33 +306,31 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
return JERRY_STANDALONE_EXIT_CODE_FAIL;
}
size_t snapshot_size;
jerry_value_t snapshot_result;
if (static_snapshot)
{
snapshot_size = jerry_parse_and_save_static_snapshot ((jerry_char_t *) source_p,
source_length,
is_snapshot_mode_for_global,
false,
output_buffer,
sizeof (output_buffer) / sizeof (uint32_t));
}
else
{
snapshot_size = jerry_parse_and_save_snapshot ((jerry_char_t *) source_p,
source_length,
is_snapshot_mode_for_global,
false,
output_buffer,
sizeof (output_buffer) / sizeof (uint32_t));
}
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 (snapshot_size == 0)
if (jerry_value_has_error_flag (snapshot_result))
{
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Generating snapshot failed!\n");
jerry_value_clear_error_flag (&snapshot_result);
print_unhandled_exception (snapshot_result);
jerry_release_value (snapshot_result);
return JERRY_STANDALONE_EXIT_CODE_FAIL;
}
size_t snapshot_size = (size_t) jerry_get_number_value (snapshot_result);
jerry_release_value (snapshot_result);
FILE *snapshot_file_p = fopen (output_file_name_p, "w");
if (snapshot_file_p == NULL)
{
+7 -7
View File
@@ -97,13 +97,13 @@ print_unhandled_exception (jerry_value_t error_value) /**< error value */
{
const char msg[] = "[Error message too long]";
err_str_size = sizeof (msg) / sizeof (char) - 1;
memcpy (err_str_buf, msg, err_str_size);
memcpy (err_str_buf, msg, err_str_size + 1);
}
else
{
jerry_size_t sz = jerry_string_to_char_buffer (err_str_val, err_str_buf, err_str_size);
assert (sz == err_str_size);
err_str_buf[err_str_size] = 0;
jerry_size_t string_end = jerry_string_to_char_buffer (err_str_val, err_str_buf, err_str_size);
assert (string_end == err_str_size);
err_str_buf[string_end] = 0;
if (jerry_is_feature_enabled (JERRY_FEATURE_ERROR_MESSAGES)
&& jerry_get_error_type (error_value) == JERRY_ERROR_SYNTAX)
@@ -112,7 +112,7 @@ print_unhandled_exception (jerry_value_t error_value) /**< error value */
unsigned int err_col = 0;
/* 1. parse column and line information */
for (jerry_size_t i = 0; i < sz; i++)
for (jerry_size_t i = 0; i < string_end; i++)
{
if (!strncmp ((char *) (err_str_buf + i), "[line: ", 7))
{
@@ -121,7 +121,7 @@ print_unhandled_exception (jerry_value_t error_value) /**< error value */
char num_str[8];
unsigned int j = 0;
while (i < sz && err_str_buf[i] != ',')
while (i < string_end && err_str_buf[i] != ',')
{
num_str[j] = (char) err_str_buf[i];
j++;
@@ -139,7 +139,7 @@ print_unhandled_exception (jerry_value_t error_value) /**< error value */
i += 10;
j = 0;
while (i < sz && err_str_buf[i] != ']')
while (i < string_end && err_str_buf[i] != ']')
{
num_str[j] = (char) err_str_buf[i];
j++;