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 */
126163typedef 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 {
0 commit comments