Skip to content

Commit 99c9a22

Browse files
LaszloLangoakosthekiss
authored andcommitted
Improve API of literal save and the snapshot (command line) tool (#2507)
Removed 'jerry_parse_and_save_literals' and introduced 'jerry_get_literals_from_snapshot' instead which works on snapshot buffers rather than source code. Added literal saving feature to snapshot merge in the snapshot command line tool. Also added missing 'jerry_cleanup()' calls to the snapshot tool. Improved the console messages of the snapshot tool. Based on previous work of Tamas Zakor <[email protected]> JerryScript-DCO-1.0-Signed-off-by: László Langó [email protected]
1 parent 054717f commit 99c9a22

File tree

5 files changed

+354
-180
lines changed

5 files changed

+354
-180
lines changed

docs/02.API-REFERENCE.md

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -678,7 +678,7 @@ main (void)
678678

679679
- [jerry_init](#jerry_init)
680680
- [jerry_cleanup](#jerry_cleanup)
681-
- [jerry_parse_and_save_literals](#jerry_parse_and_save_literals)
681+
- [jerry_get_literals_from_snapshot](#jerry_get_literals_from_snapshot)
682682

683683

684684
## jerry_get_memory_stats
@@ -5349,30 +5349,28 @@ main (void)
53495349
- [jerry_parse_and_save_function_snapshot](#jerry_parse_and_save_function_snapshot)
53505350

53515351

5352-
## jerry_parse_and_save_literals
5352+
## jerry_get_literals_from_snapshot
53535353

53545354
**Summary**
53555355

5356-
Collect the used literals from the given source code and save them into a specific file in a list or C format.
5357-
These literals are generated by the parser, they are valid identifiers and none of them are magic string.
5356+
Collect the used literals from the given snapshot and save them into a buffer in list or C format.
5357+
None of these literals are magic strings. In C format only valid identifiers are collected.
53585358

53595359
**Prototype**
53605360

53615361
```c
53625362
size_t
5363-
jerry_parse_and_save_literals (const jerry_char_t *source_p,
5364-
size_t source_size,
5365-
bool is_strict,
5366-
uint32_t *buffer_p,
5367-
size_t buffer_size,
5368-
bool is_c_format);
5363+
jerry_get_literals_from_snapshot (const uint32_t *snapshot_p,
5364+
size_t snapshot_size,
5365+
jerry_char_t *lit_buf_p,
5366+
size_t lit_buf_size,
5367+
bool is_c_format);
53695368
```
53705369

5371-
- `source_p` - script source, it must be a valid utf8 string.
5372-
- `source_size` - script source size, in bytes.
5373-
- `is_strict` - strict mode.
5374-
- `buffer_p` - buffer to save literals to.
5375-
- `buffer_size` - the buffer's size.
5370+
- `snapshot_p` - input snapshot buffer.
5371+
- `snapshot_size` - snapshot size, in bytes.
5372+
- `lit_buf_p` - buffer to save literals to.
5373+
- `lit_buf_size` - the buffer's size.
53765374
- `is_c_format` - the output format would be C-style (true) or a simple list (false).
53775375
- return value
53785376
- the size of the literal-list, if it was generated succesfully (i.e. the list of literals isn't empty,
@@ -5393,20 +5391,31 @@ main (void)
53935391
{
53945392
jerry_init (JERRY_INIT_EMPTY);
53955393

5396-
static uint32_t save_literal_buffer[256];
5394+
static jerry_char_t literal_buffer[256];
5395+
static uint32_t snapshot_buffer[256];
53975396
const jerry_char_t *code_for_literal_save_p = (const jerry_char_t *) "var obj = { a:'aa', bb:'Bb' }";
5397+
size_t code_for_literal_save_size = strlen ((const char *) code_for_literal_save_p);
5398+
5399+
jerry_value_t generate_result = jerry_generate_snapshot (NULL,
5400+
0,
5401+
code_for_literal_save_p,
5402+
code_for_literal_save_size,
5403+
0,
5404+
snapshot_buffer,
5405+
256);
5406+
size_t snapshot_size = (size_t) jerry_get_number_value (generate_result);
5407+
jerry_release_value (generate_result);
53985408

5399-
size_t literal_sizes = jerry_parse_and_save_literals (code_for_literal_save_p,
5400-
strlen ((const char *) code_for_literal_save_p),
5401-
false,
5402-
save_literal_buffer,
5403-
sizeof (save_literal_buffer) / sizeof (uint32_t),
5404-
true);
5409+
const size_t literal_size = jerry_get_literals_from_snapshot (snapshot_buffer,
5410+
snapshot_size,
5411+
literal_buffer,
5412+
256,
5413+
true);
54055414

5406-
if (literal_sizes != 0)
5415+
if (literal_size != 0)
54075416
{
5408-
FILE *literal_file_p = fopen ("literals.txt", "w");
5409-
fwrite (save_literal_buffer, sizeof (uint8_t), literal_sizes, literal_file_p);
5417+
FILE *literal_file_p = fopen ("literals.h", "wb");
5418+
fwrite (literal_buffer, sizeof (uint8_t), literal_size, literal_file_p);
54105419
fclose (literal_file_p);
54115420
}
54125421

jerry-core/api/jerry-snapshot.c

Lines changed: 66 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,7 @@ jerry_merge_snapshots (const uint32_t **inp_buffers_p, /**< array of (pointers t
12591259
functions_size += header_p->lit_table_offset - start_offset;
12601260

12611261
scan_snapshot_functions (data_p + start_offset,
1262-
data_p + header_p->lit_table_offset,
1262+
literal_base_p,
12631263
lit_pool_p,
12641264
literal_base_p);
12651265
}
@@ -1582,48 +1582,44 @@ ecma_string_is_valid_identifier (const ecma_string_t *string_p)
15821582
#endif /* JERRY_ENABLE_SNAPSHOT_SAVE */
15831583

15841584
/**
1585-
* Copy certain string literals into the given buffer in a specified format,
1586-
* which are valid identifiers and none of them are magic string.
1585+
* Get the literals from a snapshot. Copies certain string literals into the given
1586+
* buffer in a specified format.
1587+
*
1588+
* Note:
1589+
* Only valid identifiers are saved in C format.
15871590
*
15881591
* @return size of the literal-list in bytes, at most equal to the buffer size,
1589-
* if the source parsed successfully and the list of the literals isn't empty,
1592+
* if the list of the literals isn't empty,
15901593
* 0 - otherwise.
15911594
*/
15921595
size_t
1593-
jerry_parse_and_save_literals (const jerry_char_t *source_p, /**< script source */
1594-
size_t source_size, /**< script source size */
1595-
bool is_strict, /**< strict mode */
1596-
uint32_t *buffer_p, /**< [out] buffer to save literals to */
1597-
size_t buffer_size, /**< the buffer's size */
1598-
bool is_c_format) /**< format-flag */
1596+
jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, /**< input snapshot buffer */
1597+
size_t snapshot_size, /**< size of the input snapshot buffer */
1598+
jerry_char_t *lit_buf_p, /**< [out] buffer to save literals to */
1599+
size_t lit_buf_size, /**< the buffer's size */
1600+
bool is_c_format) /**< format-flag */
15991601
{
16001602
#ifdef JERRY_ENABLE_SNAPSHOT_SAVE
1601-
ecma_value_t parse_status;
1602-
ecma_compiled_code_t *bytecode_data_p;
1603-
1604-
#ifdef JERRY_ENABLE_LINE_INFO
1605-
JERRY_CONTEXT (resource_name) = ECMA_VALUE_UNDEFINED;
1606-
#endif /* JERRY_ENABLE_LINE_INFO */
1607-
1608-
parse_status = parser_parse_script (NULL,
1609-
0,
1610-
source_p,
1611-
source_size,
1612-
is_strict,
1613-
&bytecode_data_p);
1603+
const uint8_t *snapshot_data_p = (uint8_t *) snapshot_p;
1604+
const jerry_snapshot_header_t *header_p = (const jerry_snapshot_header_t *) snapshot_data_p;
16141605

1615-
if (ECMA_IS_VALUE_ERROR (parse_status))
1606+
if (snapshot_size <= sizeof (jerry_snapshot_header_t)
1607+
|| header_p->magic != JERRY_SNAPSHOT_MAGIC
1608+
|| header_p->version != JERRY_SNAPSHOT_VERSION
1609+
|| !snapshot_check_global_flags (header_p->global_flags))
16161610
{
1617-
ecma_free_value (JERRY_CONTEXT (error_value));
1611+
/* Invalid snapshot format */
16181612
return 0;
16191613
}
16201614

1621-
ecma_free_value (parse_status);
1615+
JERRY_ASSERT ((header_p->lit_table_offset % sizeof (uint32_t)) == 0);
1616+
const uint8_t *literal_base_p = snapshot_data_p + header_p->lit_table_offset;
16221617

16231618
ecma_collection_header_t *lit_pool_p = ecma_new_values_collection ();
1624-
ecma_save_literals_add_compiled_code (bytecode_data_p, lit_pool_p);
1625-
1626-
ecma_bytecode_deref (bytecode_data_p);
1619+
scan_snapshot_functions (snapshot_data_p + header_p->func_offsets[0],
1620+
literal_base_p,
1621+
lit_pool_p,
1622+
literal_base_p);
16271623

16281624
lit_utf8_size_t literal_count = 0;
16291625
ecma_value_t *iterator_p = ecma_collection_iterator_init (lit_pool_p);
@@ -1657,10 +1653,8 @@ jerry_parse_and_save_literals (const jerry_char_t *source_p, /**< script source
16571653
return 0;
16581654
}
16591655

1660-
uint8_t *destination_p = (uint8_t *) buffer_p;
1661-
1662-
uint8_t *const buffer_start_p = destination_p;
1663-
uint8_t *const buffer_end_p = destination_p + buffer_size;
1656+
jerry_char_t *const buffer_start_p = lit_buf_p;
1657+
jerry_char_t *const buffer_end_p = lit_buf_p + lit_buf_size;
16641658

16651659
JMEM_DEFINE_LOCAL_ARRAY (literal_array, literal_count, ecma_string_t *);
16661660
lit_utf8_size_t literal_idx = 0;
@@ -1697,43 +1691,43 @@ jerry_parse_and_save_literals (const jerry_char_t *source_p, /**< script source
16971691
if (is_c_format)
16981692
{
16991693
/* Save literal count. */
1700-
destination_p = jerry_append_chars_to_buffer (destination_p,
1701-
buffer_end_p,
1702-
"jerry_length_t literal_count = ",
1703-
0);
1694+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p,
1695+
buffer_end_p,
1696+
"jerry_length_t literal_count = ",
1697+
0);
17041698

1705-
destination_p = jerry_append_number_to_buffer (destination_p, buffer_end_p, literal_count);
1699+
lit_buf_p = jerry_append_number_to_buffer (lit_buf_p, buffer_end_p, literal_count);
17061700

17071701
/* Save the array of literals. */
1708-
destination_p = jerry_append_chars_to_buffer (destination_p,
1709-
buffer_end_p,
1710-
";\n\njerry_char_t *literals[",
1711-
0);
1702+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p,
1703+
buffer_end_p,
1704+
";\n\njerry_char_t *literals[",
1705+
0);
17121706

1713-
destination_p = jerry_append_number_to_buffer (destination_p, buffer_end_p, literal_count);
1714-
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, "] =\n{\n", 0);
1707+
lit_buf_p = jerry_append_number_to_buffer (lit_buf_p, buffer_end_p, literal_count);
1708+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "] =\n{\n", 0);
17151709

17161710
for (lit_utf8_size_t i = 0; i < literal_count; i++)
17171711
{
1718-
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, " \"", 0);
1719-
destination_p = jerry_append_ecma_string_to_buffer (destination_p, buffer_end_p, literal_array[i]);
1720-
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, "\"", 0);
1712+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " \"", 0);
1713+
lit_buf_p = jerry_append_ecma_string_to_buffer (lit_buf_p, buffer_end_p, literal_array[i]);
1714+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\"", 0);
17211715

17221716
if (i < literal_count - 1)
17231717
{
1724-
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, ",", 0);
1718+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, ",", 0);
17251719
}
17261720

1727-
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, "\n", 0);
1721+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\n", 0);
17281722
}
17291723

1730-
destination_p = jerry_append_chars_to_buffer (destination_p,
1731-
buffer_end_p,
1732-
"};\n\njerry_length_t literal_sizes[",
1733-
0);
1724+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p,
1725+
buffer_end_p,
1726+
"};\n\njerry_length_t literal_sizes[",
1727+
0);
17341728

1735-
destination_p = jerry_append_number_to_buffer (destination_p, buffer_end_p, literal_count);
1736-
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, "] =\n{\n", 0);
1729+
lit_buf_p = jerry_append_number_to_buffer (lit_buf_p, buffer_end_p, literal_count);
1730+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "] =\n{\n", 0);
17371731
}
17381732

17391733
/* Save the literal sizes respectively. */
@@ -1743,51 +1737,51 @@ jerry_parse_and_save_literals (const jerry_char_t *source_p, /**< script source
17431737

17441738
if (is_c_format)
17451739
{
1746-
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, " ", 0);
1740+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " ", 0);
17471741
}
17481742

1749-
destination_p = jerry_append_number_to_buffer (destination_p, buffer_end_p, str_size);
1750-
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, " ", 0);
1743+
lit_buf_p = jerry_append_number_to_buffer (lit_buf_p, buffer_end_p, str_size);
1744+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " ", 0);
17511745

17521746
if (is_c_format)
17531747
{
17541748
/* Show the given string as a comment. */
1755-
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, "/* ", 0);
1756-
destination_p = jerry_append_ecma_string_to_buffer (destination_p, buffer_end_p, literal_array[i]);
1757-
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, " */", 0);
1749+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "/* ", 0);
1750+
lit_buf_p = jerry_append_ecma_string_to_buffer (lit_buf_p, buffer_end_p, literal_array[i]);
1751+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, " */", 0);
17581752

17591753
if (i < literal_count - 1)
17601754
{
1761-
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, ",", 0);
1755+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, ",", 0);
17621756
}
17631757
}
17641758
else
17651759
{
1766-
destination_p = jerry_append_ecma_string_to_buffer (destination_p, buffer_end_p, literal_array[i]);
1760+
lit_buf_p = jerry_append_ecma_string_to_buffer (lit_buf_p, buffer_end_p, literal_array[i]);
17671761
}
17681762

1769-
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, "\n", 0);
1763+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "\n", 0);
17701764
}
17711765

17721766
if (is_c_format)
17731767
{
1774-
destination_p = jerry_append_chars_to_buffer (destination_p, buffer_end_p, "};\n", 0);
1768+
lit_buf_p = jerry_append_chars_to_buffer (lit_buf_p, buffer_end_p, "};\n", 0);
17751769
}
17761770

17771771
JMEM_FINALIZE_LOCAL_ARRAY (literal_array);
17781772

1779-
return destination_p <= buffer_end_p ? (size_t) (destination_p - buffer_start_p) : 0;
1773+
return lit_buf_p <= buffer_end_p ? (size_t) (lit_buf_p - buffer_start_p) : 0;
17801774
#else /* !JERRY_ENABLE_SNAPSHOT_SAVE */
1781-
JERRY_UNUSED (source_p);
1782-
JERRY_UNUSED (source_size);
1783-
JERRY_UNUSED (is_strict);
1784-
JERRY_UNUSED (buffer_p);
1785-
JERRY_UNUSED (buffer_size);
1775+
JERRY_UNUSED (snapshot_p);
1776+
JERRY_UNUSED (snapshot_size);
1777+
JERRY_UNUSED (lit_buf_p);
1778+
JERRY_UNUSED (lit_buf_size);
17861779
JERRY_UNUSED (is_c_format);
17871780

17881781
return 0;
17891782
#endif /* JERRY_ENABLE_SNAPSHOT_SAVE */
1790-
} /* jerry_parse_and_save_literals */
1783+
} /* jerry_get_literals_from_snapshot */
1784+
17911785

17921786
/**
17931787
* Generate snapshot function from specified source and arguments

jerry-core/include/jerryscript-snapshot.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ jerry_value_t jerry_load_function_snapshot (const uint32_t *function_snapshot_p,
6565

6666
size_t jerry_merge_snapshots (const uint32_t **inp_buffers_p, size_t *inp_buffer_sizes_p, size_t number_of_snapshots,
6767
uint32_t *out_buffer_p, size_t out_buffer_size, const char **error_p);
68-
size_t jerry_parse_and_save_literals (const jerry_char_t *source_p, size_t source_size, bool is_strict,
69-
uint32_t *buffer_p, size_t buffer_size, bool is_c_format);
68+
size_t jerry_get_literals_from_snapshot (const uint32_t *snapshot_p, size_t snapshot_size,
69+
jerry_char_t *lit_buf_p, size_t lit_buf_size, bool is_c_format);
7070
/**
7171
* @}
7272
*/

0 commit comments

Comments
 (0)