Skip to content

Commit 1085deb

Browse files
committed
Outsource magic error messages
Modify tools/gen-magic-strings.py to generate error messages. JerryScript-DCO-1.0-Signed-off-by: Csaba Repasi repasics@inf.u-szeged.hu
1 parent 4592143 commit 1085deb

File tree

112 files changed

+1937
-927
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

112 files changed

+1937
-927
lines changed

.github/workflows/gh-actions.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
if: ${{ always() }}
2626
- run: $RUNNER --check-license
2727
if: ${{ always() }}
28-
- run: $RUNNER --check-magic-strings
28+
- run: $RUNNER --check-strings
2929
if: ${{ always() }}
3030
- run: $RUNNER --check-pylint
3131
if: ${{ always() }}

jerry-core/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ if(ENABLE_AMALGAM)
341341
api/jerry-snapshot.h
342342
debugger/debugger.h
343343
ecma/base/ecma-alloc.h
344+
ecma/base/ecma-err-msg.inc.h
344345
ecma/base/ecma-errors.h
345346
ecma/base/ecma-gc.h
346347
ecma/base/ecma-globals.h

jerry-core/api/jerry-snapshot.c

+32-21
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,23 @@
3333

3434
#if JERRY_SNAPSHOT_SAVE || JERRY_SNAPSHOT_EXEC
3535

36+
/**
37+
* Create an error object
38+
*
39+
* Note:
40+
* - returned value must be freed with jerry_release_value, when it is no longer needed
41+
* - the error flag is set for the returned value
42+
*
43+
* @return value of the constructed error object
44+
*/
45+
static jerry_value_t
46+
jerry_create_error_from_id (jerry_error_t error_type, /**< type of error */
47+
ecma_error_id_t error_id) /**< ecma_error id of value of 'message' property
48+
* of constructed error object */
49+
{
50+
return jerry_create_error (error_type, (jerry_char_t *) get_ecma_error_utf8 (error_id));
51+
} /* jerry_create_error_from_id */
52+
3653
/**
3754
* Get snapshot configuration flags.
3855
*
@@ -153,8 +170,7 @@ snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p, /**< co
153170

154171
if (globals_p->snapshot_buffer_write_offset > JERRY_SNAPSHOT_MAXIMUM_WRITE_OFFSET)
155172
{
156-
globals_p->snapshot_error =
157-
jerry_create_error (JERRY_ERROR_RANGE, (const jerry_char_t *) ecma_error_maximum_snapshot_size);
173+
globals_p->snapshot_error = jerry_create_error_from_id (JERRY_ERROR_RANGE, ECMA_ERR_MAXIMUM_SNAPSHOT_SIZE);
158174
return 0;
159175
}
160176

@@ -168,8 +184,7 @@ snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p, /**< co
168184
#if JERRY_ESNEXT
169185
if (compiled_code_p->status_flags & CBC_CODE_FLAGS_HAS_TAGGED_LITERALS)
170186
{
171-
globals_p->snapshot_error =
172-
jerry_create_error (JERRY_ERROR_RANGE, (const jerry_char_t *) ecma_error_tagged_template_literals);
187+
globals_p->snapshot_error = jerry_create_error_from_id (JERRY_ERROR_RANGE, ECMA_ERR_TAGGED_TEMPLATE_LITERALS);
173188
return 0;
174189
}
175190

@@ -339,8 +354,7 @@ static_snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p,
339354

340355
if (globals_p->snapshot_buffer_write_offset >= JERRY_SNAPSHOT_MAXIMUM_WRITE_OFFSET)
341356
{
342-
globals_p->snapshot_error =
343-
jerry_create_error (JERRY_ERROR_RANGE, (const jerry_char_t *) ecma_error_maximum_snapshot_size);
357+
globals_p->snapshot_error = jerry_create_error_from_id (JERRY_ERROR_RANGE, ECMA_ERR_MAXIMUM_SNAPSHOT_SIZE);
344358
return 0;
345359
}
346360

@@ -355,7 +369,7 @@ static_snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p,
355369
{
356370
/* Regular expression literals are not supported. */
357371
globals_p->snapshot_error =
358-
jerry_create_error (JERRY_ERROR_RANGE, (const jerry_char_t *) ecma_error_regular_expression_not_supported);
372+
jerry_create_error_from_id (JERRY_ERROR_RANGE, ECMA_ERR_REGULAR_EXPRESSION_NOT_SUPPORTED);
359373
return 0;
360374
}
361375

@@ -365,8 +379,7 @@ static_snapshot_add_compiled_code (const ecma_compiled_code_t *compiled_code_p,
365379
compiled_code_p,
366380
((size_t) compiled_code_p->size) << JMEM_ALIGNMENT_LOG))
367381
{
368-
globals_p->snapshot_error =
369-
jerry_create_error (JERRY_ERROR_RANGE, (const jerry_char_t *) ecma_error_snapshot_buffer_small);
382+
globals_p->snapshot_error = jerry_create_error_from_id (JERRY_ERROR_RANGE, ECMA_ERR_SNAPSHOT_BUFFER_SMALL);
370383
return 0;
371384
}
372385

@@ -753,7 +766,7 @@ jerry_generate_snapshot (jerry_value_t compiled_code, /**< parsed script or func
753766

754767
if ((generate_snapshot_opts & ~allowed_options) != 0)
755768
{
756-
return jerry_create_error (JERRY_ERROR_RANGE, (const jerry_char_t *) ecma_error_snapshot_flag_not_supported);
769+
return jerry_create_error_from_id (JERRY_ERROR_RANGE, ECMA_ERR_SNAPSHOT_FLAG_NOT_SUPPORTED);
757770
}
758771

759772
const ecma_compiled_code_t *bytecode_data_p = NULL;
@@ -835,7 +848,7 @@ jerry_generate_snapshot (jerry_value_t compiled_code, /**< parsed script or func
835848
&literals_num))
836849
{
837850
JERRY_ASSERT (lit_map_p == NULL);
838-
return jerry_create_error (JERRY_ERROR_COMMON, (const jerry_char_t *) ecma_error_cannot_allocate_memory_literals);
851+
return jerry_create_error_from_id (JERRY_ERROR_COMMON, ECMA_ERR_CANNOT_ALLOCATE_MEMORY_LITERALS);
839852
}
840853

841854
jerry_snapshot_set_offsets (buffer_p + (aligned_header_size / sizeof (uint32_t)),
@@ -889,17 +902,15 @@ jerry_exec_snapshot (const uint32_t *snapshot_p, /**< snapshot */
889902

890903
if ((exec_snapshot_opts & ~(allowed_opts)) != 0)
891904
{
892-
ecma_raise_range_error (ECMA_ERR_MSG ("Unsupported snapshot exec flags are specified"));
905+
ecma_raise_range_error (ECMA_ERR_UNSUPPORTED_SNAPSHOT_EXEC_FLAGS_ARE_SPECIFIED);
893906
return ecma_create_error_reference_from_context ();
894907
}
895908

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";
898909
const uint8_t *snapshot_data_p = (uint8_t *) snapshot_p;
899910

900911
if (snapshot_size <= sizeof (jerry_snapshot_header_t))
901912
{
902-
ecma_raise_type_error (invalid_format_error_p);
913+
ecma_raise_type_error (ECMA_ERR_INVALID_SNAPSHOT_FORMAT);
903914
return ecma_create_error_reference_from_context ();
904915
}
905916

@@ -908,19 +919,19 @@ jerry_exec_snapshot (const uint32_t *snapshot_p, /**< snapshot */
908919
if (header_p->magic != JERRY_SNAPSHOT_MAGIC || header_p->version != JERRY_SNAPSHOT_VERSION
909920
|| !snapshot_check_global_flags (header_p->global_flags))
910921
{
911-
ecma_raise_type_error (invalid_version_error_p);
922+
ecma_raise_type_error (ECMA_ERR_INVALID_SNAPSHOT_VERSION_OR_FEATURES);
912923
return ecma_create_error_reference_from_context ();
913924
}
914925

915926
if (header_p->lit_table_offset > snapshot_size)
916927
{
917-
ecma_raise_type_error (invalid_version_error_p);
928+
ecma_raise_type_error (ECMA_ERR_INVALID_SNAPSHOT_VERSION_OR_FEATURES);
918929
return ecma_create_error_reference_from_context ();
919930
}
920931

921932
if (func_index >= header_p->number_of_funcs)
922933
{
923-
ecma_raise_range_error (ECMA_ERR_MSG ("Function index is higher than maximum"));
934+
ecma_raise_range_error (ECMA_ERR_FUNCTION_INDEX_IS_HIGHER_THAN_MAXIMUM);
924935
return ecma_create_error_reference_from_context ();
925936
}
926937

@@ -933,13 +944,13 @@ jerry_exec_snapshot (const uint32_t *snapshot_p, /**< snapshot */
933944
{
934945
if (!(exec_snapshot_opts & JERRY_SNAPSHOT_EXEC_ALLOW_STATIC))
935946
{
936-
ecma_raise_common_error (ECMA_ERR_MSG ("Static snapshots are not enabled"));
947+
ecma_raise_common_error (ECMA_ERR_STATIC_SNAPSHOTS_ARE_NOT_ENABLED);
937948
return ecma_create_error_reference_from_context ();
938949
}
939950

940951
if (exec_snapshot_opts & JERRY_SNAPSHOT_EXEC_COPY_DATA)
941952
{
942-
ecma_raise_common_error (ECMA_ERR_MSG ("Static snapshots cannot be copied into memory"));
953+
ecma_raise_common_error (ECMA_ERR_STATIC_SNAPSHOTS_CANNOT_BE_COPIED_INTO_MEMORY);
943954
return ecma_create_error_reference_from_context ();
944955
}
945956
}
@@ -995,7 +1006,7 @@ jerry_exec_snapshot (const uint32_t *snapshot_p, /**< snapshot */
9951006
{
9961007
JERRY_ASSERT (script_p->refs_and_type >= CBC_SCRIPT_REF_ONE);
9971008
jmem_heap_free_block (script_p, script_size);
998-
return ecma_raise_type_error (invalid_format_error_p);
1009+
return ecma_raise_type_error (ECMA_ERR_INVALID_SNAPSHOT_FORMAT);
9991010
}
10001011

10011012
script_p->refs_and_type -= CBC_SCRIPT_REF_ONE;

0 commit comments

Comments
 (0)