From 23ac60915fd372d5ab77b72dec14de9c2d7cf5c9 Mon Sep 17 00:00:00 2001 From: Robert Sipka Date: Fri, 17 Mar 2017 01:01:59 +0100 Subject: [PATCH] Modify the snapshot API functions to expect a 32-bit aligned buffer pointer (#1655) JerryScript-DCO-1.0-Signed-off-by: Robert Sipka rsipka.uszeged@partner.samsung.com --- docs/02.API-REFERENCE.md | 18 ++-- jerry-core/ecma/base/ecma-literal-storage.c | 47 +++++----- jerry-core/ecma/base/ecma-literal-storage.h | 4 +- jerry-core/jerry-snapshot.c | 97 +++++++++++---------- jerry-core/jerryscript.h | 6 +- jerry-main/main-unix.c | 23 +++-- tests/unit/test-api.c | 22 +++-- 7 files changed, 120 insertions(+), 97 deletions(-) diff --git a/docs/02.API-REFERENCE.md b/docs/02.API-REFERENCE.md index 825f48fda..b3d7145cc 100644 --- a/docs/02.API-REFERENCE.md +++ b/docs/02.API-REFERENCE.md @@ -3419,7 +3419,7 @@ jerry_parse_and_save_snapshot (const jerry_char_t *source_p, size_t source_size, bool is_for_global, bool is_strict, - uint8_t *buffer_p, + uint32_t *buffer_p, size_t buffer_size); ``` @@ -3441,7 +3441,7 @@ jerry_parse_and_save_snapshot (const jerry_char_t *source_p, { jerry_init (JERRY_INIT_EMPTY); - static uint8_t global_mode_snapshot_buffer[1024]; + static uint32_t global_mode_snapshot_buffer[256]; const jerry_char_t *code_to_snapshot_p = "(function () { return 'string from snapshot'; }) ();"; size_t global_mode_snapshot_size = jerry_parse_and_save_snapshot (code_to_snapshot_p, @@ -3449,7 +3449,7 @@ jerry_parse_and_save_snapshot (const jerry_char_t *source_p, true, false, global_mode_snapshot_buffer, - sizeof (global_mode_snapshot_buffer)); + sizeof (global_mode_snapshot_buffer) / sizeof (uint32_t)); jerry_cleanup (); } @@ -3475,7 +3475,7 @@ is no longer needed. ```c jerry_value_t -jerry_exec_snapshot (const void *snapshot_p, +jerry_exec_snapshot (const uint32_t *snapshot_p, size_t snapshot_size, bool copy_bytecode); ``` @@ -3495,7 +3495,7 @@ jerry_exec_snapshot (const void *snapshot_p, ```c { jerry_value_t res; - static uint8_t global_mode_snapshot_buffer[1024]; + static uint32_t global_mode_snapshot_buffer[256]; const jerry_char_t *code_to_snapshot_p = "(function () { return 'string from snapshot'; }) ();"; jerry_init (JERRY_INIT_EMPTY); @@ -3504,7 +3504,7 @@ jerry_exec_snapshot (const void *snapshot_p, true, false, global_mode_snapshot_buffer, - sizeof (global_mode_snapshot_buffer)); + sizeof (global_mode_snapshot_buffer) / sizeof (uint32_t)); jerry_cleanup (); jerry_init (JERRY_INIT_EMPTY); @@ -3538,7 +3538,7 @@ size_t jerry_parse_and_save_literals (const jerry_char_t *source_p, size_t source_size, bool is_strict, - uint8_t *buffer_p, + uint32_t *buffer_p, size_t buffer_size, bool is_c_format); ``` @@ -3560,14 +3560,14 @@ jerry_parse_and_save_literals (const jerry_char_t *source_p, { jerry_init (JERRY_INIT_EMPTY); - static uint8_t save_literal_buffer[1024]; + static uint32_t save_literal_buffer[256]; const jerry_char_t *code_for_literal_save_p = "var obj = { a:'aa', bb:'Bb' }"; size_t literal_sizes = jerry_parse_and_save_literals (code_for_literal_save_p, strlen ((const char *) code_for_literal_save_p), false, save_literal_buffer, - sizeof (save_literal_buffer), + sizeof (save_literal_buffer) / sizeof (uint32_t), true); if (literal_sizes != 0) diff --git a/jerry-core/ecma/base/ecma-literal-storage.c b/jerry-core/ecma/base/ecma-literal-storage.c index c799e4fc1..b3aff18ac 100644 --- a/jerry-core/ecma/base/ecma-literal-storage.c +++ b/jerry-core/ecma/base/ecma-literal-storage.c @@ -226,7 +226,7 @@ ecma_find_or_create_literal_number (ecma_number_t number_arg) /**< number to be * false - otherwise. */ bool -ecma_save_literals_for_snapshot (uint8_t *buffer_p, /**< [out] output snapshot buffer */ +ecma_save_literals_for_snapshot (uint32_t *buffer_p, /**< [out] output snapshot buffer */ size_t buffer_size, /**< size of the buffer */ size_t *in_out_buffer_offset_p, /**< [in,out] write position in the buffer */ lit_mem_to_snapshot_id_map_entry_t **out_map_p, /**< [out] map from literal identifiers @@ -294,7 +294,8 @@ ecma_save_literals_for_snapshot (uint8_t *buffer_p, /**< [out] output snapshot b map_p = jmem_heap_alloc_block (total_count * sizeof (lit_mem_to_snapshot_id_map_entry_t)); /* Set return values (no error is possible from here). */ - buffer_p += *in_out_buffer_offset_p; + JERRY_ASSERT ((*in_out_buffer_offset_p % sizeof (uint32_t)) == 0); + buffer_p += *in_out_buffer_offset_p / sizeof (uint32_t); *in_out_buffer_offset_p += lit_table_size; *out_map_p = map_p; *out_map_len_p = total_count; @@ -306,12 +307,14 @@ ecma_save_literals_for_snapshot (uint8_t *buffer_p, /**< [out] output snapshot b * constant so the first literal must have offset one. */ uint32_t literal_offset = JERRY_SNAPSHOT_LITERAL_ALIGNMENT; - ((uint32_t *) buffer_p)[0] = string_count; - ((uint32_t *) buffer_p)[1] = number_count; - buffer_p += 2 * sizeof (uint32_t); + buffer_p[0] = string_count; + buffer_p[1] = number_count; + buffer_p += 2; string_list_p = JERRY_CONTEXT (string_list_first_p); + uint16_t *destination_p = (uint16_t *) buffer_p; + while (string_list_p != NULL) { for (int i = 0; i < ECMA_LIT_STORAGE_VALUE_COUNT; i++) @@ -327,13 +330,14 @@ ecma_save_literals_for_snapshot (uint8_t *buffer_p, /**< [out] output snapshot b ecma_length_t length = ecma_string_get_size (string_p); - *((uint16_t *) buffer_p) = (uint16_t) length; - ecma_string_to_utf8_bytes (string_p, buffer_p + sizeof (uint16_t), length); + *destination_p = (uint16_t) length; + ecma_string_to_utf8_bytes (string_p, ((lit_utf8_byte_t *) destination_p) + sizeof (uint16_t), length); length = JERRY_ALIGNUP (sizeof (uint16_t) + length, JERRY_SNAPSHOT_LITERAL_ALIGNMENT); - buffer_p += length; + JERRY_ASSERT ((length % sizeof (uint16_t)) == 0); + destination_p += length / sizeof (uint16_t); literal_offset += length; } } @@ -359,12 +363,13 @@ ecma_save_literals_for_snapshot (uint8_t *buffer_p, /**< [out] output snapshot b JERRY_ASSERT (ECMA_STRING_GET_CONTAINER (value_p) == ECMA_STRING_LITERAL_NUMBER); ecma_number_t num = ecma_get_number_from_value (value_p->u.lit_number); - memcpy (buffer_p, &num, sizeof (ecma_number_t)); + memcpy (destination_p, &num, sizeof (ecma_number_t)); ecma_length_t length = JERRY_ALIGNUP (sizeof (ecma_number_t), JERRY_SNAPSHOT_LITERAL_ALIGNMENT); - buffer_p += length; + JERRY_ASSERT ((length % sizeof (uint16_t)) == 0); + destination_p += length / sizeof (uint16_t); literal_offset += length; } } @@ -388,7 +393,7 @@ ecma_save_literals_for_snapshot (uint8_t *buffer_p, /**< [out] output snapshot b * false - otherwise (i.e. buffer length is incorrect). */ static inline bool __attr_always_inline___ -ecma_load_literals_from_buffer (const uint8_t *buffer_p, /**< buffer with literal table in snapshot */ +ecma_load_literals_from_buffer (const uint16_t *buffer_p, /**< buffer with literal table in snapshot */ uint32_t lit_table_size, /**< size of literal table in snapshot */ lit_mem_to_snapshot_id_map_entry_t *map_p, /**< literal map */ uint32_t string_count, /**< number of strings */ @@ -407,7 +412,7 @@ ecma_load_literals_from_buffer (const uint8_t *buffer_p, /**< buffer with litera return false; } - lit_utf8_size_t length = *((uint16_t *) buffer_p); + lit_utf8_size_t length = *buffer_p; lit_utf8_size_t aligned_length = JERRY_ALIGNUP (sizeof (uint16_t) + length, JERRY_SNAPSHOT_LITERAL_ALIGNMENT); @@ -417,11 +422,12 @@ ecma_load_literals_from_buffer (const uint8_t *buffer_p, /**< buffer with litera return false; } - map_p->literal_id = ecma_find_or_create_literal_string (buffer_p + sizeof (uint16_t), length); + map_p->literal_id = ecma_find_or_create_literal_string (((lit_utf8_byte_t *) buffer_p) + sizeof (uint16_t), length); map_p->literal_offset = (jmem_cpointer_t) (literal_offset >> JERRY_SNAPSHOT_LITERAL_ALIGNMENT_LOG); map_p++; - buffer_p += aligned_length; + JERRY_ASSERT ((aligned_length % sizeof (uint16_t)) == 0); + buffer_p += aligned_length / sizeof (uint16_t); literal_offset += aligned_length; string_count--; @@ -446,7 +452,8 @@ ecma_load_literals_from_buffer (const uint8_t *buffer_p, /**< buffer with litera ecma_length_t length = JERRY_ALIGNUP (sizeof (ecma_number_t), JERRY_SNAPSHOT_LITERAL_ALIGNMENT); - buffer_p += length; + JERRY_ASSERT ((length % sizeof (uint16_t)) == 0); + buffer_p += length / sizeof (uint16_t); literal_offset += length; number_count--; @@ -462,7 +469,7 @@ ecma_load_literals_from_buffer (const uint8_t *buffer_p, /**< buffer with litera * false - otherwise (i.e. snapshot is incorrect). */ bool -ecma_load_literals_from_snapshot (const uint8_t *buffer_p, /**< buffer with literal table in snapshot */ +ecma_load_literals_from_snapshot (const uint32_t *buffer_p, /**< buffer with literal table in snapshot */ uint32_t lit_table_size, /**< size of literal table in snapshot */ lit_mem_to_snapshot_id_map_entry_t **out_map_p, /**< [out] map from literal offsets * in snapshot to identifiers @@ -478,9 +485,9 @@ ecma_load_literals_from_snapshot (const uint8_t *buffer_p, /**< buffer with lite return false; } - uint32_t string_count = ((uint32_t *) buffer_p)[0]; - uint32_t number_count = ((uint32_t *) buffer_p)[1]; - buffer_p += 2 * sizeof (uint32_t); + uint32_t string_count = buffer_p[0]; + uint32_t number_count = buffer_p[1]; + buffer_p += 2; uint32_t total_count = string_count + number_count; lit_mem_to_snapshot_id_map_entry_t *map_p; @@ -495,7 +502,7 @@ ecma_load_literals_from_snapshot (const uint8_t *buffer_p, /**< buffer with lite map_p = jmem_heap_alloc_block (total_count * sizeof (lit_mem_to_snapshot_id_map_entry_t)); *out_map_p = map_p; - if (ecma_load_literals_from_buffer (buffer_p, lit_table_size, map_p, string_count, number_count)) + if (ecma_load_literals_from_buffer ((uint16_t *) buffer_p, lit_table_size, map_p, string_count, number_count)) { return true; } diff --git a/jerry-core/ecma/base/ecma-literal-storage.h b/jerry-core/ecma/base/ecma-literal-storage.h index 67310133a..e9e1ec660 100644 --- a/jerry-core/ecma/base/ecma-literal-storage.h +++ b/jerry-core/ecma/base/ecma-literal-storage.h @@ -43,13 +43,13 @@ jmem_cpointer_t ecma_find_or_create_literal_number (ecma_number_t number_arg); #ifdef JERRY_ENABLE_SNAPSHOT_SAVE bool -ecma_save_literals_for_snapshot (uint8_t *, size_t, size_t *, +ecma_save_literals_for_snapshot (uint32_t *, size_t, size_t *, lit_mem_to_snapshot_id_map_entry_t **, uint32_t *, uint32_t *); #endif /* JERRY_ENABLE_SNAPSHOT_SAVE */ #ifdef JERRY_ENABLE_SNAPSHOT_EXEC bool -ecma_load_literals_from_snapshot (const uint8_t *, uint32_t, +ecma_load_literals_from_snapshot (const uint32_t *, uint32_t, lit_mem_to_snapshot_id_map_entry_t **, uint32_t *); #endif /* JERRY_ENABLE_SNAPSHOT_EXEC */ diff --git a/jerry-core/jerry-snapshot.c b/jerry-core/jerry-snapshot.c index 9c39ef522..c8dd02958 100644 --- a/jerry-core/jerry-snapshot.c +++ b/jerry-core/jerry-snapshot.c @@ -208,7 +208,7 @@ snapshot_add_compiled_code (ecma_compiled_code_t *compiled_code_p, /**< compiled * Set the uint16_t offsets in the code area. */ static void -jerry_snapshot_set_offsets (uint8_t *buffer_p, /**< buffer */ +jerry_snapshot_set_offsets (uint32_t *buffer_p, /**< buffer */ uint32_t size, /**< buffer size */ lit_mem_to_snapshot_id_map_entry_t *lit_map_p) /**< literal map */ { @@ -228,7 +228,7 @@ jerry_snapshot_set_offsets (uint8_t *buffer_p, /**< buffer */ if (bytecode_p->status_flags & CBC_CODE_FLAGS_UINT16_ARGUMENTS) { - literal_start_p = (jmem_cpointer_t *) (buffer_p + sizeof (cbc_uint16_arguments_t)); + literal_start_p = (jmem_cpointer_t *) (((uint8_t *) buffer_p) + sizeof (cbc_uint16_arguments_t)); cbc_uint16_arguments_t *args_p = (cbc_uint16_arguments_t *) buffer_p; argument_end = args_p->argument_end; @@ -237,7 +237,7 @@ jerry_snapshot_set_offsets (uint8_t *buffer_p, /**< buffer */ } else { - literal_start_p = (jmem_cpointer_t *) (buffer_p + sizeof (cbc_uint8_arguments_t)); + literal_start_p = (jmem_cpointer_t *) (((uint8_t *) buffer_p) + sizeof (cbc_uint8_arguments_t)); cbc_uint8_arguments_t *args_p = (cbc_uint8_arguments_t *) buffer_p; argument_end = args_p->argument_end; @@ -292,7 +292,8 @@ jerry_snapshot_set_offsets (uint8_t *buffer_p, /**< buffer */ bytecode_p->refs = 1; } - buffer_p += code_size; + JERRY_ASSERT ((code_size % sizeof (uint32_t)) == 0); + buffer_p += code_size / sizeof (uint32_t); size -= code_size; } while (size > 0); @@ -458,7 +459,7 @@ jerry_parse_and_save_snapshot (const jerry_char_t *source_p, /**< script source bool is_for_global, /**< snapshot would be executed as global (true) * or eval (false) */ bool is_strict, /**< strict mode */ - uint8_t *buffer_p, /**< buffer to save snapshot to */ + uint32_t *buffer_p, /**< buffer to save snapshot to */ size_t buffer_size) /**< the buffer's size */ { #ifdef JERRY_ENABLE_SNAPSHOT_SAVE @@ -481,7 +482,7 @@ jerry_parse_and_save_snapshot (const jerry_char_t *source_p, /**< script source return 0; } - snapshot_add_compiled_code (bytecode_data_p, buffer_p, buffer_size, &globals); + snapshot_add_compiled_code (bytecode_data_p, (uint8_t *) buffer_p, buffer_size, &globals); if (globals.snapshot_error_occured) { @@ -507,13 +508,14 @@ jerry_parse_and_save_snapshot (const jerry_char_t *source_p, /**< script source return 0; } - jerry_snapshot_set_offsets (buffer_p + JERRY_ALIGNUP (sizeof (jerry_snapshot_header_t), JMEM_ALIGNMENT), + jerry_snapshot_set_offsets (buffer_p + (JERRY_ALIGNUP (sizeof (jerry_snapshot_header_t), + JMEM_ALIGNMENT) / sizeof (uint32_t)), (uint32_t) (header.lit_table_offset - sizeof (jerry_snapshot_header_t)), lit_map_p); size_t header_offset = 0; - snapshot_write_to_buffer_by_offset (buffer_p, + snapshot_write_to_buffer_by_offset ((uint8_t *) buffer_p, buffer_size, &header_offset, &header, @@ -549,7 +551,7 @@ jerry_parse_and_save_snapshot (const jerry_char_t *source_p, /**< script source * thrown error - otherwise */ jerry_value_t -jerry_exec_snapshot (const void *snapshot_p, /**< snapshot */ +jerry_exec_snapshot (const uint32_t *snapshot_p, /**< snapshot */ size_t snapshot_size, /**< size of snapshot */ bool copy_bytecode) /**< flag, indicating whether the passed snapshot * buffer should be copied to the engine's memory. @@ -586,7 +588,8 @@ jerry_exec_snapshot (const void *snapshot_p, /**< snapshot */ return ecma_raise_type_error (invalid_version_error_p); } - if (!ecma_load_literals_from_snapshot (snapshot_data_p + header_p->lit_table_offset, + JERRY_ASSERT ((header_p->lit_table_offset % sizeof (uint32_t)) == 0); + if (!ecma_load_literals_from_snapshot ((uint32_t *) (snapshot_data_p + header_p->lit_table_offset), header_p->lit_table_size, &lit_map_p, &literals_num)) @@ -882,7 +885,7 @@ size_t jerry_parse_and_save_literals (const jerry_char_t *source_p, /**< script source */ size_t source_size, /**< script source size */ bool is_strict, /**< strict mode */ - uint8_t *buffer_p, /**< [out] buffer to save literals to */ + uint32_t *buffer_p, /**< [out] buffer to save literals to */ size_t buffer_size, /**< the buffer's size */ bool is_c_format) /**< format-flag */ { @@ -934,8 +937,10 @@ jerry_parse_and_save_literals (const jerry_char_t *source_p, /**< script source return 0; } - uint8_t * const buffer_start_p = buffer_p; - uint8_t * const buffer_end_p = buffer_p + buffer_size; + uint8_t *destination_p = (uint8_t *) buffer_p; + + uint8_t *const buffer_start_p = destination_p; + uint8_t *const buffer_end_p = destination_p + buffer_size; JMEM_DEFINE_LOCAL_ARRAY (literal_array, literal_count, ecma_string_t *); lit_utf8_size_t literal_idx = 0; @@ -968,43 +973,43 @@ jerry_parse_and_save_literals (const jerry_char_t *source_p, /**< script source if (is_c_format) { /* Save literal count. */ - buffer_p = jerry_append_chars_to_buffer (buffer_p, - buffer_end_p, - (const char *) "jerry_length_t literal_count = ", - 0); + destination_p = jerry_append_chars_to_buffer (destination_p, + buffer_end_p, + (const char *) "jerry_length_t literal_count = ", + 0); - buffer_p = jerry_append_number_to_buffer (buffer_p, buffer_end_p, literal_count); + destination_p = jerry_append_number_to_buffer (destination_p, buffer_end_p, literal_count); /* Save the array of literals. */ - buffer_p = jerry_append_chars_to_buffer (buffer_p, - buffer_end_p, - ";\n\njerry_char_ptr_t literals[", - 0); + destination_p = jerry_append_chars_to_buffer (destination_p, + buffer_end_p, + ";\n\njerry_char_ptr_t literals[", + 0); - buffer_p = jerry_append_number_to_buffer (buffer_p, buffer_end_p, literal_count); - buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, "] =\n{\n", 0); + destination_p = jerry_append_number_to_buffer (destination_p, buffer_end_p, literal_count); + destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, "] =\n{\n", 0); for (lit_utf8_size_t i = 0; i < literal_count; i++) { - buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, " \"", 0); - buffer_p = jerry_append_ecma_string_to_buffer (buffer_p, buffer_end_p, literal_array[i]); - buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, "\"", 0); + destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, " \"", 0); + destination_p = jerry_append_ecma_string_to_buffer (destination_p, buffer_end_p, literal_array[i]); + destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, "\"", 0); if (i < literal_count - 1) { - buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, ",", 0); + destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, ",", 0); } - buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, "\n", 0); + destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, "\n", 0); } - buffer_p = jerry_append_chars_to_buffer (buffer_p, - buffer_end_p, - (const char *) "};\n\njerry_length_t literal_sizes[", - 0); + destination_p = jerry_append_chars_to_buffer (destination_p, + buffer_end_p, + (const char *) "};\n\njerry_length_t literal_sizes[", + 0); - buffer_p = jerry_append_number_to_buffer (buffer_p, buffer_end_p, literal_count); - buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, "] =\n{\n", 0); + destination_p = jerry_append_number_to_buffer (destination_p, buffer_end_p, literal_count); + destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, "] =\n{\n", 0); } /* Save the literal sizes respectively. */ @@ -1014,40 +1019,40 @@ jerry_parse_and_save_literals (const jerry_char_t *source_p, /**< script source if (is_c_format) { - buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, " ", 0); + destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, " ", 0); } - buffer_p = jerry_append_number_to_buffer (buffer_p, buffer_end_p, str_size); - buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, " ", 0); + destination_p = jerry_append_number_to_buffer (destination_p, buffer_end_p, str_size); + destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, " ", 0); if (is_c_format) { /* Show the given string as a comment. */ - buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, "/* ", 0); - buffer_p = jerry_append_ecma_string_to_buffer (buffer_p, buffer_end_p, literal_array[i]); - buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, " */", 0); + destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, "/* ", 0); + destination_p = jerry_append_ecma_string_to_buffer (destination_p, buffer_end_p, literal_array[i]); + destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, " */", 0); if (i < literal_count - 1) { - buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, ",", 0); + destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, ",", 0); } } else { - buffer_p = jerry_append_ecma_string_to_buffer (buffer_p, buffer_end_p, literal_array[i]); + destination_p = jerry_append_ecma_string_to_buffer (destination_p, buffer_end_p, literal_array[i]); } - buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, "\n", 0); + destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, "\n", 0); } if (is_c_format) { - buffer_p = jerry_append_chars_to_buffer (buffer_p, buffer_end_p, (const char *) "};\n", 0); + destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, (const char *) "};\n", 0); } JMEM_FINALIZE_LOCAL_ARRAY (literal_array); - return buffer_p <= buffer_end_p ? (size_t) (buffer_p - buffer_start_p) : 0; + return destination_p <= buffer_end_p ? (size_t) (destination_p - buffer_start_p) : 0; #else /* !JERRY_ENABLE_SNAPSHOT_SAVE */ JERRY_UNUSED (source_p); JERRY_UNUSED (source_size); diff --git a/jerry-core/jerryscript.h b/jerry-core/jerryscript.h index b2688b761..77c53dd12 100644 --- a/jerry-core/jerryscript.h +++ b/jerry-core/jerryscript.h @@ -339,10 +339,10 @@ bool jerry_is_valid_cesu8_string (const jerry_char_t *cesu8_buf_p, jerry_size_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, uint8_t *buffer_p, size_t buffer_size); -jerry_value_t jerry_exec_snapshot (const void *snapshot_p, size_t snapshot_size, bool copy_bytecode); + bool is_strict, 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); size_t jerry_parse_and_save_literals (const jerry_char_t *source_p, size_t source_size, bool is_strict, - uint8_t *buffer_p, size_t buffer_size, bool is_c_format); + uint32_t *buffer_p, size_t buffer_size, bool is_c_format); /** * @} diff --git a/jerry-main/main-unix.c b/jerry-main/main-unix.c index b9998eb01..0059914b6 100644 --- a/jerry-main/main-unix.c +++ b/jerry-main/main-unix.c @@ -28,10 +28,15 @@ #define JERRY_MAX_COMMAND_LINE_ARGS (64) /** - * Maximum size of source code / snapshots buffer + * Maximum size of source code */ #define JERRY_BUFFER_SIZE (1048576) +/** + * Maximum size of snapshots buffer + */ +#define JERRY_SNAPSHOT_BUFFER_SIZE (JERRY_BUFFER_SIZE / sizeof (uint32_t)) + /** * Standalone Jerry exit codes */ @@ -45,7 +50,7 @@ static uint8_t buffer[ JERRY_BUFFER_SIZE ]; -static const uint8_t * +static const uint32_t * read_file (const char *file_name, size_t *out_size_p) { @@ -75,7 +80,7 @@ read_file (const char *file_name, fclose (file); *out_size_p = bytes_read; - return (const uint8_t *) buffer; + return (const uint32_t *) buffer; } /* read_file */ /** @@ -629,7 +634,7 @@ main (int argc, for (int i = 0; i < exec_snapshots_count; i++) { size_t snapshot_size; - const uint8_t *snapshot_p = read_file (exec_snapshot_file_names[i], &snapshot_size); + const uint32_t *snapshot_p = read_file (exec_snapshot_file_names[i], &snapshot_size); if (snapshot_p == NULL) { @@ -637,7 +642,7 @@ main (int argc, } else { - ret_value = jerry_exec_snapshot ((void *) snapshot_p, + ret_value = jerry_exec_snapshot (snapshot_p, snapshot_size, true); } @@ -654,7 +659,7 @@ main (int argc, for (int i = 0; i < files_counter; i++) { size_t source_size; - const jerry_char_t *source_p = read_file (file_names[i], &source_size); + const jerry_char_t *source_p = (jerry_char_t *) read_file (file_names[i], &source_size); if (source_p == NULL) { @@ -670,7 +675,7 @@ main (int argc, if (jerry_is_feature_enabled (JERRY_FEATURE_SNAPSHOT_SAVE) && (is_save_snapshot_mode || is_save_literals_mode)) { - static uint8_t snapshot_save_buffer[ JERRY_BUFFER_SIZE ]; + static uint32_t snapshot_save_buffer[ JERRY_SNAPSHOT_BUFFER_SIZE ]; if (is_save_snapshot_mode) { @@ -679,7 +684,7 @@ main (int argc, is_save_snapshot_mode_for_global_or_eval, false, snapshot_save_buffer, - JERRY_BUFFER_SIZE); + JERRY_SNAPSHOT_BUFFER_SIZE); if (snapshot_size == 0) { ret_value = jerry_create_error (JERRY_ERROR_COMMON, (jerry_char_t *) "Snapshot saving failed!"); @@ -698,7 +703,7 @@ main (int argc, source_size, false, snapshot_save_buffer, - JERRY_BUFFER_SIZE, + JERRY_SNAPSHOT_BUFFER_SIZE, is_save_literals_mode_in_c_format_or_list); if (literal_buffer_size == 0) { diff --git a/tests/unit/test-api.c b/tests/unit/test-api.c index 63aaf1af9..314ff55e7 100644 --- a/tests/unit/test-api.c +++ b/tests/unit/test-api.c @@ -18,6 +18,11 @@ #include "test-common.h" +/** + * Maximum size of snapshots buffer + */ +#define SNAPSHOT_BUFFER_SIZE (256) + const char *test_source = ( "function assert (arg) { " " if (!arg) { " @@ -1026,8 +1031,8 @@ main (void) if (jerry_is_feature_enabled (JERRY_FEATURE_SNAPSHOT_SAVE) && jerry_is_feature_enabled (JERRY_FEATURE_SNAPSHOT_EXEC)) { - static uint8_t global_mode_snapshot_buffer[1024]; - static uint8_t eval_mode_snapshot_buffer[1024]; + static uint32_t global_mode_snapshot_buffer[SNAPSHOT_BUFFER_SIZE]; + static uint32_t eval_mode_snapshot_buffer[SNAPSHOT_BUFFER_SIZE]; const char *code_to_snapshot_p = "(function () { return 'string from snapshot'; }) ();"; @@ -1037,7 +1042,7 @@ main (void) true, false, global_mode_snapshot_buffer, - sizeof (global_mode_snapshot_buffer)); + SNAPSHOT_BUFFER_SIZE); TEST_ASSERT (global_mode_snapshot_size != 0); jerry_cleanup (); @@ -1047,7 +1052,7 @@ main (void) false, false, eval_mode_snapshot_buffer, - sizeof (eval_mode_snapshot_buffer)); + SNAPSHOT_BUFFER_SIZE); TEST_ASSERT (eval_mode_snapshot_size != 0); jerry_cleanup (); @@ -1088,14 +1093,14 @@ main (void) /* C format generation */ jerry_init (JERRY_INIT_EMPTY); - static jerry_char_t literal_buffer_c[256]; + static uint32_t literal_buffer_c[SNAPSHOT_BUFFER_SIZE]; static const char *code_for_c_format_p = "var object = { aa:'fo o', Bb:'max', aaa:'xzy0' };"; size_t literal_sizes_c_format = jerry_parse_and_save_literals ((jerry_char_t *) code_for_c_format_p, strlen (code_for_c_format_p), false, literal_buffer_c, - sizeof (literal_buffer_c), + SNAPSHOT_BUFFER_SIZE, true); TEST_ASSERT (literal_sizes_c_format == 203); @@ -1123,15 +1128,16 @@ main (void) /* List format generation */ jerry_init (JERRY_INIT_EMPTY); - static jerry_char_t literal_buffer_list[256]; + static uint32_t literal_buffer_list[SNAPSHOT_BUFFER_SIZE]; static const char *code_for_list_format_p = "var obj = { a:'aa', bb:'Bb' };"; size_t literal_sizes_list_format = jerry_parse_and_save_literals ((jerry_char_t *) code_for_list_format_p, strlen (code_for_list_format_p), false, literal_buffer_list, - sizeof (literal_buffer_list), + SNAPSHOT_BUFFER_SIZE, false); + TEST_ASSERT (literal_sizes_list_format == 25); TEST_ASSERT (!strncmp ((char *) literal_buffer_list, "1 a\n2 Bb\n2 aa\n2 bb\n3 obj\n", literal_sizes_list_format));