1818#include "jerryscript.h"
1919
2020#include "ecma-conversion.h"
21- #include "ecma-errors.h"
2221#include "ecma-exceptions.h"
2322#include "ecma-function-object.h"
2423#include "ecma-helpers.h"
3332
3433#if JERRY_SNAPSHOT_SAVE || JERRY_SNAPSHOT_EXEC
3534
35+ /**
36+ * Create an error object
37+ *
38+ * Note:
39+ * - returned value must be freed with jerry_release_value, when it is no longer needed
40+ * - the error flag is set for the returned value
41+ *
42+ * @return value of the constructed error object
43+ */
44+ static jerry_value_t
45+ jerry_create_error_from_id (jerry_error_t error_type , /**< type of error */
46+ lit_magic_string_id_t error_id ) /**< ecma_error id of value of 'message' property
47+ * of constructed error object */
48+ {
49+ return jerry_create_error (error_type , (jerry_char_t * ) lit_get_magic_string_utf8 (error_id ));
50+ } /* jerry_create_error_from_id */
51+
3652/**
3753 * Get snapshot configuration flags.
3854 *
@@ -153,8 +169,7 @@ snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p, /**< co
153169
154170 if (globals_p -> snapshot_buffer_write_offset > JERRY_SNAPSHOT_MAXIMUM_WRITE_OFFSET )
155171 {
156- globals_p -> snapshot_error =
157- jerry_create_error (JERRY_ERROR_RANGE , (const jerry_char_t * ) ecma_error_maximum_snapshot_size );
172+ globals_p -> snapshot_error = jerry_create_error_from_id (JERRY_ERROR_RANGE , ECMA_ERR_MSG (MAXIMUM_SNAPSHOT_SIZE ));
158173 return 0 ;
159174 }
160175
@@ -168,8 +183,7 @@ snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p, /**< co
168183#if JERRY_ESNEXT
169184 if (compiled_code_p -> status_flags & CBC_CODE_FLAGS_HAS_TAGGED_LITERALS )
170185 {
171- globals_p -> snapshot_error =
172- jerry_create_error (JERRY_ERROR_RANGE , (const jerry_char_t * ) ecma_error_tagged_template_literals );
186+ globals_p -> snapshot_error = jerry_create_error_from_id (JERRY_ERROR_RANGE , ECMA_ERR_MSG (TAGGED_TEMPLATE_LITERALS ));
173187 return 0 ;
174188 }
175189
@@ -339,8 +353,7 @@ static_snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p,
339353
340354 if (globals_p -> snapshot_buffer_write_offset >= JERRY_SNAPSHOT_MAXIMUM_WRITE_OFFSET )
341355 {
342- globals_p -> snapshot_error =
343- jerry_create_error (JERRY_ERROR_RANGE , (const jerry_char_t * ) ecma_error_maximum_snapshot_size );
356+ globals_p -> snapshot_error = jerry_create_error_from_id (JERRY_ERROR_RANGE , ECMA_ERR_MSG (MAXIMUM_SNAPSHOT_SIZE ));
344357 return 0 ;
345358 }
346359
@@ -355,7 +368,7 @@ static_snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p,
355368 {
356369 /* Regular expression literals are not supported. */
357370 globals_p -> snapshot_error =
358- jerry_create_error (JERRY_ERROR_RANGE , ( const jerry_char_t * ) ecma_error_regular_expression_not_supported );
371+ jerry_create_error_from_id (JERRY_ERROR_RANGE , ECMA_ERR_MSG ( REGULAR_EXPRESSION_NOT_SUPPORTED ) );
359372 return 0 ;
360373 }
361374
@@ -365,8 +378,7 @@ static_snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p,
365378 compiled_code_p ,
366379 ((size_t ) compiled_code_p -> size ) << JMEM_ALIGNMENT_LOG ))
367380 {
368- globals_p -> snapshot_error =
369- jerry_create_error (JERRY_ERROR_RANGE , (const jerry_char_t * ) ecma_error_snapshot_buffer_small );
381+ globals_p -> snapshot_error = jerry_create_error_from_id (JERRY_ERROR_RANGE , ECMA_ERR_MSG (SNAPSHOT_BUFFER_SMALL ));
370382 return 0 ;
371383 }
372384
@@ -753,7 +765,7 @@ jerry_generate_snapshot (jerry_value_t compiled_code, /**< parsed script or func
753765
754766 if ((generate_snapshot_opts & ~allowed_options ) != 0 )
755767 {
756- return jerry_create_error (JERRY_ERROR_RANGE , ( const jerry_char_t * ) ecma_error_snapshot_flag_not_supported );
768+ return jerry_create_error_from_id (JERRY_ERROR_RANGE , ECMA_ERR_MSG ( SNAPSHOT_FLAG_NOT_SUPPORTED ) );
757769 }
758770
759771 const ecma_compiled_code_t * bytecode_data_p = NULL ;
@@ -785,7 +797,7 @@ jerry_generate_snapshot (jerry_value_t compiled_code, /**< parsed script or func
785797
786798 if (JERRY_UNLIKELY (bytecode_data_p == NULL ))
787799 {
788- return jerry_create_error (JERRY_ERROR_RANGE , ( const jerry_char_t * ) ECMA_ERR_MSG ("Unsupported compiled code" ));
800+ return jerry_create_error_from_id (JERRY_ERROR_RANGE , ECMA_ERR_MSG (UNSUPPORTED_COMPILED_CODE ));
789801 }
790802
791803 snapshot_globals_t globals ;
@@ -835,7 +847,7 @@ jerry_generate_snapshot (jerry_value_t compiled_code, /**< parsed script or func
835847 & literals_num ))
836848 {
837849 JERRY_ASSERT (lit_map_p == NULL );
838- return jerry_create_error (JERRY_ERROR_COMMON , ( const jerry_char_t * ) ecma_error_cannot_allocate_memory_literals );
850+ return jerry_create_error_from_id (JERRY_ERROR_COMMON , ECMA_ERR_MSG ( CANNOT_ALLOCATE_MEMORY_LITERALS ) );
839851 }
840852
841853 jerry_snapshot_set_offsets (buffer_p + (aligned_header_size / sizeof (uint32_t )),
@@ -889,17 +901,15 @@ jerry_exec_snapshot (const uint32_t *snapshot_p, /**< snapshot */
889901
890902 if ((exec_snapshot_opts & ~(allowed_opts )) != 0 )
891903 {
892- ecma_raise_range_error (ECMA_ERR_MSG ("Unsupported snapshot exec flags are specified" ));
904+ ecma_raise_range_error (ECMA_ERR_MSG (UNSUPPORTED_SNAPSHOT_EXEC_FLAGS_ARE_SPECIFIED ));
893905 return ecma_create_error_reference_from_context ();
894906 }
895907
896- const char * const invalid_version_error_p = "Invalid snapshot version or unsupported features present" ;
897- const char * const invalid_format_error_p = "Invalid snapshot format" ;
898908 const uint8_t * snapshot_data_p = (uint8_t * ) snapshot_p ;
899909
900910 if (snapshot_size <= sizeof (jerry_snapshot_header_t ))
901911 {
902- ecma_raise_type_error (invalid_format_error_p );
912+ ecma_raise_type_error (ECMA_ERR_MSG ( INVALID_SNAPSHOT_FORMAT ) );
903913 return ecma_create_error_reference_from_context ();
904914 }
905915
@@ -908,19 +918,19 @@ jerry_exec_snapshot (const uint32_t *snapshot_p, /**< snapshot */
908918 if (header_p -> magic != JERRY_SNAPSHOT_MAGIC || header_p -> version != JERRY_SNAPSHOT_VERSION
909919 || !snapshot_check_global_flags (header_p -> global_flags ))
910920 {
911- ecma_raise_type_error (invalid_version_error_p );
921+ ecma_raise_type_error (ECMA_ERR_MSG ( INVALID_SNAPSHOT_VERSION_OR_FEATURES ) );
912922 return ecma_create_error_reference_from_context ();
913923 }
914924
915925 if (header_p -> lit_table_offset > snapshot_size )
916926 {
917- ecma_raise_type_error (invalid_version_error_p );
927+ ecma_raise_type_error (ECMA_ERR_MSG ( INVALID_SNAPSHOT_VERSION_OR_FEATURES ) );
918928 return ecma_create_error_reference_from_context ();
919929 }
920930
921931 if (func_index >= header_p -> number_of_funcs )
922932 {
923- ecma_raise_range_error (ECMA_ERR_MSG ("Function index is higher than maximum" ));
933+ ecma_raise_range_error (ECMA_ERR_MSG (FUNCTION_INDEX_IS_HIGHER_THAN_MAXIMUM ));
924934 return ecma_create_error_reference_from_context ();
925935 }
926936
@@ -933,13 +943,13 @@ jerry_exec_snapshot (const uint32_t *snapshot_p, /**< snapshot */
933943 {
934944 if (!(exec_snapshot_opts & JERRY_SNAPSHOT_EXEC_ALLOW_STATIC ))
935945 {
936- ecma_raise_common_error (ECMA_ERR_MSG ("Static snapshots are not enabled" ));
946+ ecma_raise_common_error (ECMA_ERR_MSG (STATIC_SNAPSHOTS_ARE_NOT_ENABLED ));
937947 return ecma_create_error_reference_from_context ();
938948 }
939949
940950 if (exec_snapshot_opts & JERRY_SNAPSHOT_EXEC_COPY_DATA )
941951 {
942- ecma_raise_common_error (ECMA_ERR_MSG ("Static snapshots cannot be copied into memory" ));
952+ ecma_raise_common_error (ECMA_ERR_MSG (STATIC_SNAPSHOTS_CANNOT_BE_COPIED_INTO_MEMORY ));
943953 return ecma_create_error_reference_from_context ();
944954 }
945955 }
@@ -995,7 +1005,7 @@ jerry_exec_snapshot (const uint32_t *snapshot_p, /**< snapshot */
9951005 {
9961006 JERRY_ASSERT (script_p -> refs_and_type >= CBC_SCRIPT_REF_ONE );
9971007 jmem_heap_free_block (script_p , script_size );
998- return ecma_raise_type_error (invalid_format_error_p );
1008+ return ecma_raise_type_error (ECMA_ERR_MSG ( INVALID_SNAPSHOT_FORMAT ) );
9991009 }
10001010
10011011 script_p -> refs_and_type -= CBC_SCRIPT_REF_ONE ;
0 commit comments