Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
336 changes: 129 additions & 207 deletions docs/02.API-REFERENCE.md

Large diffs are not rendered by default.

340 changes: 156 additions & 184 deletions jerry-core/api/jerry-snapshot.c

Large diffs are not rendered by default.

7 changes: 1 addition & 6 deletions jerry-core/api/jerry-snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ typedef struct
uint32_t func_offsets[1]; /**< function offsets (lowest bit: global(0) or eval(1) context) */
} jerry_snapshot_header_t;

/**
* Evaluate this function on the top of the scope chain.
*/
#define JERRY_SNAPSHOT_EVAL_CONTEXT 0x1u

/**
* Jerry snapshot magic marker.
*/
Expand All @@ -46,7 +41,7 @@ typedef struct
/**
* Jerry snapshot format version.
*/
#define JERRY_SNAPSHOT_VERSION (10u)
#define JERRY_SNAPSHOT_VERSION (11u)

/**
* Snapshot configuration flags.
Expand Down
29 changes: 18 additions & 11 deletions jerry-core/include/jerryscript-snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,34 @@ extern "C"
* @{
*/

/**
* Flags for jerry_generate_snapshot and jerry_generate_function_snapshot.
*/
typedef enum
{
JERRY_SNAPSHOT_SAVE_STATIC = (1u << 0), /**< static snapshot */
JERRY_SNAPSHOT_SAVE_STRICT = (1u << 1), /**< strict mode code */
} jerry_generate_snapshot_opts_t;

/**
* Snapshot functions.
*/
size_t jerry_parse_and_save_snapshot (const jerry_char_t *source_p, size_t source_size, bool is_for_global,
bool is_strict, uint32_t *buffer_p, size_t buffer_size);
size_t jerry_parse_and_save_static_snapshot (const jerry_char_t *source_p, size_t source_size, bool is_for_global,
bool is_strict, uint32_t *buffer_p, size_t buffer_size);
jerry_value_t jerry_generate_snapshot (const jerry_char_t *resource_name_p, size_t resource_name_length,
const jerry_char_t *source_p, size_t source_size,
uint32_t generate_snapshot_opts, uint32_t *buffer_p, size_t buffer_size);
jerry_value_t jerry_generate_function_snapshot (const jerry_char_t *resource_name_p, size_t resource_name_length,
const jerry_char_t *source_p, size_t source_size,
const jerry_char_t *args_p, size_t args_size,
uint32_t generate_snapshot_opts, uint32_t *buffer_p,
size_t buffer_size);

jerry_value_t jerry_exec_snapshot (const uint32_t *snapshot_p, size_t snapshot_size, bool copy_bytecode);
jerry_value_t jerry_exec_snapshot_at (const uint32_t *snapshot_p, size_t snapshot_size,
size_t func_index, bool copy_bytecode);
size_t jerry_merge_snapshots (const uint32_t **inp_buffers_p, size_t *inp_buffer_sizes_p, size_t number_of_snapshots,
uint32_t *out_buffer_p, size_t out_buffer_size, const char **error_p);
size_t jerry_parse_and_save_literals (const jerry_char_t *source_p, size_t source_size, bool is_strict,
uint32_t *buffer_p, size_t buffer_size, bool is_c_format);

size_t jerry_parse_and_save_function_snapshot (const jerry_char_t *source_p, size_t source_size,
const jerry_char_t *args_p, size_t args_size,
bool is_strict, uint32_t *buffer_p, size_t buffer_size);
size_t jerry_parse_and_save_static_function_snapshot (const jerry_char_t *source_p, size_t source_size,
const jerry_char_t *args_p, size_t args_size,
bool is_strict, uint32_t *buffer_p, size_t buffer_size);
jerry_value_t jerry_load_function_snapshot_at (const uint32_t *function_snapshot_p,
const size_t function_snapshot_size,
size_t func_index,
Expand Down
123 changes: 66 additions & 57 deletions jerry-main/main-unix-snapshot.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* limitations under the License.
*/

#include <assert.h>
#include <string.h>

#include "jerryscript.h"
Expand Down Expand Up @@ -120,14 +121,49 @@ 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
*/
typedef enum
{
OPT_GENERATE_HELP,
OPT_GENERATE_STATIC,
OPT_GENERATE_CONTEXT,
OPT_GENERATE_LITERAL_LIST,
OPT_GENERATE_LITERAL_C,
OPT_GENERATE_SHOW_OP,
Expand All @@ -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)"),
Expand Down Expand Up @@ -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);

Expand All @@ -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:
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
}
Expand All @@ -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;
Expand All @@ -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)
{
Expand Down
14 changes: 7 additions & 7 deletions jerry-main/main-unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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))
{
Expand All @@ -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++;
Expand All @@ -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++;
Expand Down
Loading