Skip to content

Commit 431a28b

Browse files
authored
Fix native error check (#4674)
JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg [email protected]
1 parent 3c512d9 commit 431a28b

File tree

4 files changed

+24
-56
lines changed

4 files changed

+24
-56
lines changed

jerry-core/ecma/base/ecma-globals.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,7 @@ typedef struct
10321032
union
10331033
{
10341034
uint8_t arguments_flags; /**< arguments object flags */
1035+
uint8_t error_type; /**< jerry_error_t type of native error objects */
10351036
#if JERRY_BUILTIN_DATE
10361037
uint8_t date_flags; /**< flags for date objects */
10371038
#endif /* JERRY_BUILTIN_DATE */

jerry-core/ecma/operations/ecma-exceptions.c

Lines changed: 13 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -38,38 +38,6 @@
3838
* @{
3939
*/
4040

41-
/**
42-
* Map error type to error prototype.
43-
*/
44-
typedef struct
45-
{
46-
jerry_error_t error_type; /**< Native error type */
47-
ecma_builtin_id_t error_prototype_id; /**< ID of the error prototype */
48-
} ecma_error_mapping_t;
49-
50-
/**
51-
* List of error type mappings
52-
*/
53-
const ecma_error_mapping_t ecma_error_mappings[] =
54-
{
55-
#define ERROR_ELEMENT(TYPE, ID) { TYPE, ID }
56-
ERROR_ELEMENT (JERRY_ERROR_COMMON, ECMA_BUILTIN_ID_ERROR_PROTOTYPE),
57-
58-
#if JERRY_BUILTIN_ERRORS
59-
ERROR_ELEMENT (JERRY_ERROR_EVAL, ECMA_BUILTIN_ID_EVAL_ERROR_PROTOTYPE),
60-
ERROR_ELEMENT (JERRY_ERROR_RANGE, ECMA_BUILTIN_ID_RANGE_ERROR_PROTOTYPE),
61-
ERROR_ELEMENT (JERRY_ERROR_REFERENCE, ECMA_BUILTIN_ID_REFERENCE_ERROR_PROTOTYPE),
62-
ERROR_ELEMENT (JERRY_ERROR_TYPE, ECMA_BUILTIN_ID_TYPE_ERROR_PROTOTYPE),
63-
ERROR_ELEMENT (JERRY_ERROR_URI, ECMA_BUILTIN_ID_URI_ERROR_PROTOTYPE),
64-
ERROR_ELEMENT (JERRY_ERROR_SYNTAX, ECMA_BUILTIN_ID_SYNTAX_ERROR_PROTOTYPE),
65-
#if JERRY_BUILTIN_PROMISE
66-
ERROR_ELEMENT (JERRY_ERROR_AGGREGATE, ECMA_BUILTIN_ID_AGGREGATE_ERROR_PROTOTYPE),
67-
#endif /* JERRY_BUILTIN_PROMISE */
68-
#endif /* JERRY_BUILTIN_ERRORS */
69-
70-
#undef ERROR_ELEMENT
71-
};
72-
7341
/**
7442
* Standard ecma-error object constructor.
7543
*
@@ -150,16 +118,18 @@ ecma_new_standard_error (jerry_error_t error_type, /**< native error type */
150118

151119
ecma_object_t *prototype_obj_p = ecma_builtin_get (prototype_id);
152120

153-
ecma_object_t *new_error_obj_p = ecma_create_object (prototype_obj_p,
154-
sizeof (ecma_extended_object_t),
155-
ECMA_OBJECT_TYPE_CLASS);
121+
ecma_object_t *error_object_p = ecma_create_object (prototype_obj_p,
122+
sizeof (ecma_extended_object_t),
123+
ECMA_OBJECT_TYPE_CLASS);
156124

157-
((ecma_extended_object_t *) new_error_obj_p)->u.cls.type = ECMA_OBJECT_CLASS_ERROR;
125+
ecma_extended_object_t *extended_object_p = (ecma_extended_object_t *) error_object_p;
126+
extended_object_p->u.cls.type = ECMA_OBJECT_CLASS_ERROR;
127+
extended_object_p->u.cls.u1.error_type = (uint8_t) error_type;
158128

159129
if (message_string_p != NULL)
160130
{
161131
ecma_property_value_t *prop_value_p;
162-
prop_value_p = ecma_create_named_data_property (new_error_obj_p,
132+
prop_value_p = ecma_create_named_data_property (error_object_p,
163133
ecma_get_magic_string (LIT_MAGIC_STRING_MESSAGE),
164134
ECMA_PROPERTY_CONFIGURABLE_WRITABLE,
165135
NULL);
@@ -173,7 +143,7 @@ ecma_new_standard_error (jerry_error_t error_type, /**< native error type */
173143
&& !(JERRY_CONTEXT (status_flags) & ECMA_STATUS_ERROR_UPDATE))
174144
{
175145
JERRY_CONTEXT (status_flags) |= ECMA_STATUS_ERROR_UPDATE;
176-
JERRY_CONTEXT (error_object_created_callback_p) (ecma_make_object_value (new_error_obj_p),
146+
JERRY_CONTEXT (error_object_created_callback_p) (ecma_make_object_value (error_object_p),
177147
JERRY_CONTEXT (error_object_created_callback_user_p));
178148
JERRY_CONTEXT (status_flags) &= (uint32_t) ~ECMA_STATUS_ERROR_UPDATE;
179149
}
@@ -183,7 +153,7 @@ ecma_new_standard_error (jerry_error_t error_type, /**< native error type */
183153
/* Default decorator when line info is enabled. */
184154
ecma_string_t *stack_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_STACK);
185155

186-
ecma_property_value_t *prop_value_p = ecma_create_named_data_property (new_error_obj_p,
156+
ecma_property_value_t *prop_value_p = ecma_create_named_data_property (error_object_p,
187157
stack_str_p,
188158
ECMA_PROPERTY_CONFIGURABLE_WRITABLE,
189159
NULL);
@@ -196,7 +166,7 @@ ecma_new_standard_error (jerry_error_t error_type, /**< native error type */
196166
#endif /* JERRY_LINE_INFO */
197167
}
198168

199-
return new_error_obj_p;
169+
return error_object_p;
200170
} /* ecma_new_standard_error */
201171

202172
#if JERRY_BUILTIN_PROMISE
@@ -312,26 +282,14 @@ ecma_new_aggregate_error (ecma_value_t error_list_val, /**< errors list */
312282
* if it is not an Error object then JERRY_ERROR_NONE will be returned
313283
*/
314284
jerry_error_t
315-
ecma_get_error_type (ecma_object_t *error_object) /**< possible error object */
285+
ecma_get_error_type (ecma_object_t *error_object_p) /**< possible error object */
316286
{
317-
if (error_object->u2.prototype_cp == JMEM_CP_NULL || ECMA_OBJECT_IS_PROXY (error_object))
287+
if (!ecma_object_class_is (error_object_p, ECMA_OBJECT_CLASS_ERROR))
318288
{
319289
return JERRY_ERROR_NONE;
320290
}
321291

322-
ecma_object_t *prototype_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, error_object->u2.prototype_cp);
323-
324-
uint8_t builtin_id = ecma_get_object_builtin_id (prototype_p);
325-
326-
for (uint8_t idx = 0; idx < sizeof (ecma_error_mappings) / sizeof (ecma_error_mappings[0]); idx++)
327-
{
328-
if (ecma_error_mappings[idx].error_prototype_id == builtin_id)
329-
{
330-
return ecma_error_mappings[idx].error_type;
331-
}
332-
}
333-
334-
return JERRY_ERROR_NONE;
292+
return (jerry_error_t) ((ecma_extended_object_t *) error_object_p)->u.cls.u1.error_type;
335293
} /* ecma_get_error_type */
336294

337295
/**

jerry-core/ecma/operations/ecma-exceptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
#define ECMA_ERR_MSG(msg) NULL
3333
#endif /* JERRY_ERROR_MESSAGES */
3434

35-
jerry_error_t ecma_get_error_type (ecma_object_t *error_object);
35+
jerry_error_t ecma_get_error_type (ecma_object_t *error_object_p);
3636
ecma_object_t *ecma_new_standard_error (jerry_error_t error_type, ecma_string_t *message_string_p);
3737
#if JERRY_ERROR_MESSAGES
3838
ecma_value_t ecma_raise_standard_error_with_format (jerry_error_t error_type, const char *msg_p, ...);

tests/unit-core/test-api-errortype.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,14 @@ main (void)
7272

7373
jerry_release_value (result);
7474

75+
char test_invalid_error[] = "Object.create(Error.prototype)";
76+
result = jerry_eval ((const jerry_char_t *) test_invalid_error,
77+
sizeof (test_invalid_error) - 1,
78+
JERRY_PARSE_NO_OPTS);
79+
TEST_ASSERT (!jerry_value_is_error (result) && jerry_value_is_object (result));
80+
TEST_ASSERT (jerry_get_error_type (result) == JERRY_ERROR_NONE);
81+
82+
jerry_release_value (result);
83+
7584
jerry_cleanup ();
7685
} /* main */

0 commit comments

Comments
 (0)