Skip to content

Commit 7b226f5

Browse files
authored
Rework snapshot generation API. (#2259)
Also remove eval context support. It provides no practical advantage. JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg [email protected]
1 parent 35926f3 commit 7b226f5

File tree

8 files changed

+474
-566
lines changed

8 files changed

+474
-566
lines changed

docs/02.API-REFERENCE.md

Lines changed: 129 additions & 207 deletions
Large diffs are not rendered by default.

jerry-core/api/jerry-snapshot.c

Lines changed: 156 additions & 184 deletions
Large diffs are not rendered by default.

jerry-core/api/jerry-snapshot.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,6 @@ typedef struct
3333
uint32_t func_offsets[1]; /**< function offsets (lowest bit: global(0) or eval(1) context) */
3434
} jerry_snapshot_header_t;
3535

36-
/**
37-
* Evaluate this function on the top of the scope chain.
38-
*/
39-
#define JERRY_SNAPSHOT_EVAL_CONTEXT 0x1u
40-
4136
/**
4237
* Jerry snapshot magic marker.
4338
*/
@@ -46,7 +41,7 @@ typedef struct
4641
/**
4742
* Jerry snapshot format version.
4843
*/
49-
#define JERRY_SNAPSHOT_VERSION (10u)
44+
#define JERRY_SNAPSHOT_VERSION (11u)
5045

5146
/**
5247
* Snapshot configuration flags.

jerry-core/include/jerryscript-snapshot.h

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,34 @@ extern "C"
2727
* @{
2828
*/
2929

30+
/**
31+
* Flags for jerry_generate_snapshot and jerry_generate_function_snapshot.
32+
*/
33+
typedef enum
34+
{
35+
JERRY_SNAPSHOT_SAVE_STATIC = (1u << 0), /**< static snapshot */
36+
JERRY_SNAPSHOT_SAVE_STRICT = (1u << 1), /**< strict mode code */
37+
} jerry_generate_snapshot_opts_t;
38+
3039
/**
3140
* Snapshot functions.
3241
*/
33-
size_t jerry_parse_and_save_snapshot (const jerry_char_t *source_p, size_t source_size, bool is_for_global,
34-
bool is_strict, uint32_t *buffer_p, size_t buffer_size);
35-
size_t jerry_parse_and_save_static_snapshot (const jerry_char_t *source_p, size_t source_size, bool is_for_global,
36-
bool is_strict, uint32_t *buffer_p, size_t buffer_size);
42+
jerry_value_t jerry_generate_snapshot (const jerry_char_t *resource_name_p, size_t resource_name_length,
43+
const jerry_char_t *source_p, size_t source_size,
44+
uint32_t generate_snapshot_opts, uint32_t *buffer_p, size_t buffer_size);
45+
jerry_value_t jerry_generate_function_snapshot (const jerry_char_t *resource_name_p, size_t resource_name_length,
46+
const jerry_char_t *source_p, size_t source_size,
47+
const jerry_char_t *args_p, size_t args_size,
48+
uint32_t generate_snapshot_opts, uint32_t *buffer_p,
49+
size_t buffer_size);
50+
3751
jerry_value_t jerry_exec_snapshot (const uint32_t *snapshot_p, size_t snapshot_size, bool copy_bytecode);
3852
jerry_value_t jerry_exec_snapshot_at (const uint32_t *snapshot_p, size_t snapshot_size,
3953
size_t func_index, bool copy_bytecode);
4054
size_t jerry_merge_snapshots (const uint32_t **inp_buffers_p, size_t *inp_buffer_sizes_p, size_t number_of_snapshots,
4155
uint32_t *out_buffer_p, size_t out_buffer_size, const char **error_p);
4256
size_t jerry_parse_and_save_literals (const jerry_char_t *source_p, size_t source_size, bool is_strict,
4357
uint32_t *buffer_p, size_t buffer_size, bool is_c_format);
44-
45-
size_t jerry_parse_and_save_function_snapshot (const jerry_char_t *source_p, size_t source_size,
46-
const jerry_char_t *args_p, size_t args_size,
47-
bool is_strict, uint32_t *buffer_p, size_t buffer_size);
48-
size_t jerry_parse_and_save_static_function_snapshot (const jerry_char_t *source_p, size_t source_size,
49-
const jerry_char_t *args_p, size_t args_size,
50-
bool is_strict, uint32_t *buffer_p, size_t buffer_size);
5158
jerry_value_t jerry_load_function_snapshot_at (const uint32_t *function_snapshot_p,
5259
const size_t function_snapshot_size,
5360
size_t func_index,

jerry-main/main-unix-snapshot.c

Lines changed: 66 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* limitations under the License.
1414
*/
1515

16+
#include <assert.h>
1617
#include <string.h>
1718

1819
#include "jerryscript.h"
@@ -120,14 +121,49 @@ read_file (uint8_t *input_pos_p, /**< next position in the input buffer */
120121
return bytes_read;
121122
} /* read_file */
122123

124+
/**
125+
* Print error value
126+
*/
127+
static void
128+
print_unhandled_exception (jerry_value_t error_value) /**< error value */
129+
{
130+
assert (!jerry_value_has_error_flag (error_value));
131+
132+
jerry_value_t err_str_val = jerry_value_to_string (error_value);
133+
134+
if (jerry_value_has_error_flag (err_str_val))
135+
{
136+
/* Avoid recursive error throws. */
137+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Snapshot error: [value cannot be converted to string]\n");
138+
jerry_release_value (err_str_val);
139+
return;
140+
}
141+
142+
jerry_size_t err_str_size = jerry_get_string_size (err_str_val);
143+
144+
if (err_str_size >= 256)
145+
{
146+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Snapshot error: [value cannot be converted to string]\n");
147+
jerry_release_value (err_str_val);
148+
return;
149+
}
150+
151+
jerry_char_t err_str_buf[256];
152+
jerry_size_t string_end = jerry_string_to_char_buffer (err_str_val, err_str_buf, err_str_size);
153+
assert (string_end == err_str_size);
154+
err_str_buf[string_end] = 0;
155+
156+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Snapshot error: %s\n", (char *) err_str_buf);
157+
jerry_release_value (err_str_val);
158+
} /* print_unhandled_exception */
159+
123160
/**
124161
* Generate command line option IDs
125162
*/
126163
typedef enum
127164
{
128165
OPT_GENERATE_HELP,
129166
OPT_GENERATE_STATIC,
130-
OPT_GENERATE_CONTEXT,
131167
OPT_GENERATE_LITERAL_LIST,
132168
OPT_GENERATE_LITERAL_C,
133169
OPT_GENERATE_SHOW_OP,
@@ -143,10 +179,6 @@ static const cli_opt_t generate_opts[] =
143179
.help = "print this help and exit"),
144180
CLI_OPT_DEF (.id = OPT_GENERATE_STATIC, .opt = "s", .longopt = "static",
145181
.help = "generate static snapshot"),
146-
CLI_OPT_DEF (.id = OPT_GENERATE_CONTEXT, .opt = "c", .longopt = "context",
147-
.meta = "MODE",
148-
.help = "specify the execution context of the snapshot: "
149-
"global or eval (default: global)."),
150182
CLI_OPT_DEF (.id = OPT_GENERATE_LITERAL_LIST, .longopt = "save-literals-list-format",
151183
.meta = "FILE",
152184
.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 */
174206
(void) argc;
175207

176208
bool is_save_literals_mode_in_c_format = false;
177-
bool is_snapshot_mode_for_global = true;
209+
uint32_t snapshot_flags = 0;
178210
jerry_init_flag_t flags = JERRY_INIT_EMPTY;
179211

180-
uint32_t number_of_files = 0;
212+
const char *file_name_p = NULL;
181213
uint8_t *source_p = input_buffer;
182214
size_t source_length = 0;
183215
const char *save_literals_file_name_p = NULL;
184-
bool static_snapshot = false;
185216

186217
cli_change_opts (cli_state_p, generate_opts);
187218

@@ -196,31 +227,7 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
196227
}
197228
case OPT_GENERATE_STATIC:
198229
{
199-
static_snapshot = true;
200-
break;
201-
}
202-
case OPT_GENERATE_CONTEXT:
203-
{
204-
const char *mode_str_p = cli_consume_string (cli_state_p);
205-
206-
if (cli_state_p->error != NULL)
207-
{
208-
break;
209-
}
210-
211-
if (!strcmp ("global", mode_str_p))
212-
{
213-
is_snapshot_mode_for_global = true;
214-
}
215-
else if (!strcmp ("eval", mode_str_p))
216-
{
217-
is_snapshot_mode_for_global = false;
218-
}
219-
else
220-
{
221-
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Incorrect argument for context mode: %s\n", mode_str_p);
222-
return JERRY_STANDALONE_EXIT_CODE_FAIL;
223-
}
230+
snapshot_flags |= JERRY_SNAPSHOT_SAVE_STATIC;
224231
break;
225232
}
226233
case OPT_GENERATE_LITERAL_LIST:
@@ -252,7 +259,13 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
252259
}
253260
case CLI_OPT_DEFAULT:
254261
{
255-
const char *file_name_p = cli_consume_string (cli_state_p);
262+
if (file_name_p != NULL)
263+
{
264+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Exactly one input file must be specified\n");
265+
return JERRY_STANDALONE_EXIT_CODE_FAIL;
266+
}
267+
268+
file_name_p = cli_consume_string (cli_state_p);
256269

257270
if (cli_state_p->error == NULL)
258271
{
@@ -263,8 +276,6 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
263276
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Input file is empty\n");
264277
return JERRY_STANDALONE_EXIT_CODE_FAIL;
265278
}
266-
267-
number_of_files++;
268279
}
269280
break;
270281
}
@@ -281,7 +292,7 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
281292
return JERRY_STANDALONE_EXIT_CODE_FAIL;
282293
}
283294

284-
if (number_of_files != 1)
295+
if (file_name_p == NULL)
285296
{
286297
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Exactly one input file must be specified\n");
287298
return JERRY_STANDALONE_EXIT_CODE_FAIL;
@@ -295,33 +306,31 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
295306
return JERRY_STANDALONE_EXIT_CODE_FAIL;
296307
}
297308

298-
size_t snapshot_size;
309+
jerry_value_t snapshot_result;
299310

300-
if (static_snapshot)
301-
{
302-
snapshot_size = jerry_parse_and_save_static_snapshot ((jerry_char_t *) source_p,
303-
source_length,
304-
is_snapshot_mode_for_global,
305-
false,
306-
output_buffer,
307-
sizeof (output_buffer) / sizeof (uint32_t));
308-
}
309-
else
310-
{
311-
snapshot_size = jerry_parse_and_save_snapshot ((jerry_char_t *) source_p,
312-
source_length,
313-
is_snapshot_mode_for_global,
314-
false,
315-
output_buffer,
316-
sizeof (output_buffer) / sizeof (uint32_t));
317-
}
311+
snapshot_result = jerry_generate_snapshot ((jerry_char_t *) file_name_p,
312+
(size_t) strlen (file_name_p),
313+
(jerry_char_t *) source_p,
314+
source_length,
315+
snapshot_flags,
316+
output_buffer,
317+
sizeof (output_buffer) / sizeof (uint32_t));
318318

319-
if (snapshot_size == 0)
319+
if (jerry_value_has_error_flag (snapshot_result))
320320
{
321321
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Error: Generating snapshot failed!\n");
322+
323+
jerry_value_clear_error_flag (&snapshot_result);
324+
325+
print_unhandled_exception (snapshot_result);
326+
327+
jerry_release_value (snapshot_result);
322328
return JERRY_STANDALONE_EXIT_CODE_FAIL;
323329
}
324330

331+
size_t snapshot_size = (size_t) jerry_get_number_value (snapshot_result);
332+
jerry_release_value (snapshot_result);
333+
325334
FILE *snapshot_file_p = fopen (output_file_name_p, "w");
326335
if (snapshot_file_p == NULL)
327336
{

jerry-main/main-unix.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,13 @@ print_unhandled_exception (jerry_value_t error_value) /**< error value */
9797
{
9898
const char msg[] = "[Error message too long]";
9999
err_str_size = sizeof (msg) / sizeof (char) - 1;
100-
memcpy (err_str_buf, msg, err_str_size);
100+
memcpy (err_str_buf, msg, err_str_size + 1);
101101
}
102102
else
103103
{
104-
jerry_size_t sz = jerry_string_to_char_buffer (err_str_val, err_str_buf, err_str_size);
105-
assert (sz == err_str_size);
106-
err_str_buf[err_str_size] = 0;
104+
jerry_size_t string_end = jerry_string_to_char_buffer (err_str_val, err_str_buf, err_str_size);
105+
assert (string_end == err_str_size);
106+
err_str_buf[string_end] = 0;
107107

108108
if (jerry_is_feature_enabled (JERRY_FEATURE_ERROR_MESSAGES)
109109
&& jerry_get_error_type (error_value) == JERRY_ERROR_SYNTAX)
@@ -112,7 +112,7 @@ print_unhandled_exception (jerry_value_t error_value) /**< error value */
112112
unsigned int err_col = 0;
113113

114114
/* 1. parse column and line information */
115-
for (jerry_size_t i = 0; i < sz; i++)
115+
for (jerry_size_t i = 0; i < string_end; i++)
116116
{
117117
if (!strncmp ((char *) (err_str_buf + i), "[line: ", 7))
118118
{
@@ -121,7 +121,7 @@ print_unhandled_exception (jerry_value_t error_value) /**< error value */
121121
char num_str[8];
122122
unsigned int j = 0;
123123

124-
while (i < sz && err_str_buf[i] != ',')
124+
while (i < string_end && err_str_buf[i] != ',')
125125
{
126126
num_str[j] = (char) err_str_buf[i];
127127
j++;
@@ -139,7 +139,7 @@ print_unhandled_exception (jerry_value_t error_value) /**< error value */
139139
i += 10;
140140
j = 0;
141141

142-
while (i < sz && err_str_buf[i] != ']')
142+
while (i < string_end && err_str_buf[i] != ']')
143143
{
144144
num_str[j] = (char) err_str_buf[i];
145145
j++;

0 commit comments

Comments
 (0)