13
13
* limitations under the License.
14
14
*/
15
15
16
+ #include <assert.h>
16
17
#include <string.h>
17
18
18
19
#include "jerryscript.h"
@@ -120,14 +121,49 @@ read_file (uint8_t *input_pos_p, /**< next position in the input buffer */
120
121
return bytes_read ;
121
122
} /* read_file */
122
123
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
+
123
160
/**
124
161
* Generate command line option IDs
125
162
*/
126
163
typedef enum
127
164
{
128
165
OPT_GENERATE_HELP ,
129
166
OPT_GENERATE_STATIC ,
130
- OPT_GENERATE_CONTEXT ,
131
167
OPT_GENERATE_LITERAL_LIST ,
132
168
OPT_GENERATE_LITERAL_C ,
133
169
OPT_GENERATE_SHOW_OP ,
@@ -143,10 +179,6 @@ static const cli_opt_t generate_opts[] =
143
179
.help = "print this help and exit" ),
144
180
CLI_OPT_DEF (.id = OPT_GENERATE_STATIC , .opt = "s" , .longopt = "static" ,
145
181
.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)." ),
150
182
CLI_OPT_DEF (.id = OPT_GENERATE_LITERAL_LIST , .longopt = "save-literals-list-format" ,
151
183
.meta = "FILE" ,
152
184
.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 */
174
206
(void ) argc ;
175
207
176
208
bool is_save_literals_mode_in_c_format = false;
177
- bool is_snapshot_mode_for_global = true ;
209
+ uint32_t snapshot_flags = 0 ;
178
210
jerry_init_flag_t flags = JERRY_INIT_EMPTY ;
179
211
180
- uint32_t number_of_files = 0 ;
212
+ const char * file_name_p = NULL ;
181
213
uint8_t * source_p = input_buffer ;
182
214
size_t source_length = 0 ;
183
215
const char * save_literals_file_name_p = NULL ;
184
- bool static_snapshot = false;
185
216
186
217
cli_change_opts (cli_state_p , generate_opts );
187
218
@@ -196,31 +227,7 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
196
227
}
197
228
case OPT_GENERATE_STATIC :
198
229
{
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 ;
224
231
break ;
225
232
}
226
233
case OPT_GENERATE_LITERAL_LIST :
@@ -252,7 +259,13 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
252
259
}
253
260
case CLI_OPT_DEFAULT :
254
261
{
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 );
256
269
257
270
if (cli_state_p -> error == NULL )
258
271
{
@@ -263,8 +276,6 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
263
276
jerry_port_log (JERRY_LOG_LEVEL_ERROR , "Input file is empty\n" );
264
277
return JERRY_STANDALONE_EXIT_CODE_FAIL ;
265
278
}
266
-
267
- number_of_files ++ ;
268
279
}
269
280
break ;
270
281
}
@@ -281,7 +292,7 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
281
292
return JERRY_STANDALONE_EXIT_CODE_FAIL ;
282
293
}
283
294
284
- if (number_of_files != 1 )
295
+ if (file_name_p == NULL )
285
296
{
286
297
jerry_port_log (JERRY_LOG_LEVEL_ERROR , "Error: Exactly one input file must be specified\n" );
287
298
return JERRY_STANDALONE_EXIT_CODE_FAIL ;
@@ -295,33 +306,31 @@ process_generate (cli_state_t *cli_state_p, /**< cli state */
295
306
return JERRY_STANDALONE_EXIT_CODE_FAIL ;
296
307
}
297
308
298
- size_t snapshot_size ;
309
+ jerry_value_t snapshot_result ;
299
310
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 ));
318
318
319
- if (snapshot_size == 0 )
319
+ if (jerry_value_has_error_flag ( snapshot_result ) )
320
320
{
321
321
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 );
322
328
return JERRY_STANDALONE_EXIT_CODE_FAIL ;
323
329
}
324
330
331
+ size_t snapshot_size = (size_t ) jerry_get_number_value (snapshot_result );
332
+ jerry_release_value (snapshot_result );
333
+
325
334
FILE * snapshot_file_p = fopen (output_file_name_p , "w" );
326
335
if (snapshot_file_p == NULL )
327
336
{
0 commit comments