aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZoltan Herczeg <zherczeg.u-szeged@partner.samsung.com>2021-05-21 12:36:07 +0200
committerGitHub <noreply@github.com>2021-05-21 12:36:07 +0200
commit431a28b9022a1d39790212d27d0af4392a37a54e (patch)
treee843d44a4949f5725343a58b16f8ad62dc55a9a8
parent3c512d937bef15f30e185c6db41982512f68977a (diff)
Fix native error check (#4674)
JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
-rw-r--r--jerry-core/ecma/base/ecma-globals.h1
-rw-r--r--jerry-core/ecma/operations/ecma-exceptions.c68
-rw-r--r--jerry-core/ecma/operations/ecma-exceptions.h2
-rw-r--r--tests/unit-core/test-api-errortype.c9
4 files changed, 24 insertions, 56 deletions
diff --git a/jerry-core/ecma/base/ecma-globals.h b/jerry-core/ecma/base/ecma-globals.h
index 83a3c771..5bbcf11b 100644
--- a/jerry-core/ecma/base/ecma-globals.h
+++ b/jerry-core/ecma/base/ecma-globals.h
@@ -1032,6 +1032,7 @@ typedef struct
union
{
uint8_t arguments_flags; /**< arguments object flags */
+ uint8_t error_type; /**< jerry_error_t type of native error objects */
#if JERRY_BUILTIN_DATE
uint8_t date_flags; /**< flags for date objects */
#endif /* JERRY_BUILTIN_DATE */
diff --git a/jerry-core/ecma/operations/ecma-exceptions.c b/jerry-core/ecma/operations/ecma-exceptions.c
index 1732f0e6..2649f7bd 100644
--- a/jerry-core/ecma/operations/ecma-exceptions.c
+++ b/jerry-core/ecma/operations/ecma-exceptions.c
@@ -39,38 +39,6 @@
*/
/**
- * Map error type to error prototype.
- */
-typedef struct
-{
- jerry_error_t error_type; /**< Native error type */
- ecma_builtin_id_t error_prototype_id; /**< ID of the error prototype */
-} ecma_error_mapping_t;
-
-/**
- * List of error type mappings
- */
-const ecma_error_mapping_t ecma_error_mappings[] =
-{
-#define ERROR_ELEMENT(TYPE, ID) { TYPE, ID }
- ERROR_ELEMENT (JERRY_ERROR_COMMON, ECMA_BUILTIN_ID_ERROR_PROTOTYPE),
-
-#if JERRY_BUILTIN_ERRORS
- ERROR_ELEMENT (JERRY_ERROR_EVAL, ECMA_BUILTIN_ID_EVAL_ERROR_PROTOTYPE),
- ERROR_ELEMENT (JERRY_ERROR_RANGE, ECMA_BUILTIN_ID_RANGE_ERROR_PROTOTYPE),
- ERROR_ELEMENT (JERRY_ERROR_REFERENCE, ECMA_BUILTIN_ID_REFERENCE_ERROR_PROTOTYPE),
- ERROR_ELEMENT (JERRY_ERROR_TYPE, ECMA_BUILTIN_ID_TYPE_ERROR_PROTOTYPE),
- ERROR_ELEMENT (JERRY_ERROR_URI, ECMA_BUILTIN_ID_URI_ERROR_PROTOTYPE),
- ERROR_ELEMENT (JERRY_ERROR_SYNTAX, ECMA_BUILTIN_ID_SYNTAX_ERROR_PROTOTYPE),
-#if JERRY_BUILTIN_PROMISE
- ERROR_ELEMENT (JERRY_ERROR_AGGREGATE, ECMA_BUILTIN_ID_AGGREGATE_ERROR_PROTOTYPE),
-#endif /* JERRY_BUILTIN_PROMISE */
-#endif /* JERRY_BUILTIN_ERRORS */
-
-#undef ERROR_ELEMENT
-};
-
-/**
* Standard ecma-error object constructor.
*
* Note:
@@ -150,16 +118,18 @@ ecma_new_standard_error (jerry_error_t error_type, /**< native error type */
ecma_object_t *prototype_obj_p = ecma_builtin_get (prototype_id);
- ecma_object_t *new_error_obj_p = ecma_create_object (prototype_obj_p,
- sizeof (ecma_extended_object_t),
- ECMA_OBJECT_TYPE_CLASS);
+ ecma_object_t *error_object_p = ecma_create_object (prototype_obj_p,
+ sizeof (ecma_extended_object_t),
+ ECMA_OBJECT_TYPE_CLASS);
- ((ecma_extended_object_t *) new_error_obj_p)->u.cls.type = ECMA_OBJECT_CLASS_ERROR;
+ ecma_extended_object_t *extended_object_p = (ecma_extended_object_t *) error_object_p;
+ extended_object_p->u.cls.type = ECMA_OBJECT_CLASS_ERROR;
+ extended_object_p->u.cls.u1.error_type = (uint8_t) error_type;
if (message_string_p != NULL)
{
ecma_property_value_t *prop_value_p;
- prop_value_p = ecma_create_named_data_property (new_error_obj_p,
+ prop_value_p = ecma_create_named_data_property (error_object_p,
ecma_get_magic_string (LIT_MAGIC_STRING_MESSAGE),
ECMA_PROPERTY_CONFIGURABLE_WRITABLE,
NULL);
@@ -173,7 +143,7 @@ ecma_new_standard_error (jerry_error_t error_type, /**< native error type */
&& !(JERRY_CONTEXT (status_flags) & ECMA_STATUS_ERROR_UPDATE))
{
JERRY_CONTEXT (status_flags) |= ECMA_STATUS_ERROR_UPDATE;
- JERRY_CONTEXT (error_object_created_callback_p) (ecma_make_object_value (new_error_obj_p),
+ JERRY_CONTEXT (error_object_created_callback_p) (ecma_make_object_value (error_object_p),
JERRY_CONTEXT (error_object_created_callback_user_p));
JERRY_CONTEXT (status_flags) &= (uint32_t) ~ECMA_STATUS_ERROR_UPDATE;
}
@@ -183,7 +153,7 @@ ecma_new_standard_error (jerry_error_t error_type, /**< native error type */
/* Default decorator when line info is enabled. */
ecma_string_t *stack_str_p = ecma_get_magic_string (LIT_MAGIC_STRING_STACK);
- ecma_property_value_t *prop_value_p = ecma_create_named_data_property (new_error_obj_p,
+ ecma_property_value_t *prop_value_p = ecma_create_named_data_property (error_object_p,
stack_str_p,
ECMA_PROPERTY_CONFIGURABLE_WRITABLE,
NULL);
@@ -196,7 +166,7 @@ ecma_new_standard_error (jerry_error_t error_type, /**< native error type */
#endif /* JERRY_LINE_INFO */
}
- return new_error_obj_p;
+ return error_object_p;
} /* ecma_new_standard_error */
#if JERRY_BUILTIN_PROMISE
@@ -312,26 +282,14 @@ ecma_new_aggregate_error (ecma_value_t error_list_val, /**< errors list */
* if it is not an Error object then JERRY_ERROR_NONE will be returned
*/
jerry_error_t
-ecma_get_error_type (ecma_object_t *error_object) /**< possible error object */
+ecma_get_error_type (ecma_object_t *error_object_p) /**< possible error object */
{
- if (error_object->u2.prototype_cp == JMEM_CP_NULL || ECMA_OBJECT_IS_PROXY (error_object))
+ if (!ecma_object_class_is (error_object_p, ECMA_OBJECT_CLASS_ERROR))
{
return JERRY_ERROR_NONE;
}
- ecma_object_t *prototype_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, error_object->u2.prototype_cp);
-
- uint8_t builtin_id = ecma_get_object_builtin_id (prototype_p);
-
- for (uint8_t idx = 0; idx < sizeof (ecma_error_mappings) / sizeof (ecma_error_mappings[0]); idx++)
- {
- if (ecma_error_mappings[idx].error_prototype_id == builtin_id)
- {
- return ecma_error_mappings[idx].error_type;
- }
- }
-
- return JERRY_ERROR_NONE;
+ return (jerry_error_t) ((ecma_extended_object_t *) error_object_p)->u.cls.u1.error_type;
} /* ecma_get_error_type */
/**
diff --git a/jerry-core/ecma/operations/ecma-exceptions.h b/jerry-core/ecma/operations/ecma-exceptions.h
index 120c1ada..c6c00226 100644
--- a/jerry-core/ecma/operations/ecma-exceptions.h
+++ b/jerry-core/ecma/operations/ecma-exceptions.h
@@ -32,7 +32,7 @@
#define ECMA_ERR_MSG(msg) NULL
#endif /* JERRY_ERROR_MESSAGES */
-jerry_error_t ecma_get_error_type (ecma_object_t *error_object);
+jerry_error_t ecma_get_error_type (ecma_object_t *error_object_p);
ecma_object_t *ecma_new_standard_error (jerry_error_t error_type, ecma_string_t *message_string_p);
#if JERRY_ERROR_MESSAGES
ecma_value_t ecma_raise_standard_error_with_format (jerry_error_t error_type, const char *msg_p, ...);
diff --git a/tests/unit-core/test-api-errortype.c b/tests/unit-core/test-api-errortype.c
index 7b9cfac9..2e0d9700 100644
--- a/tests/unit-core/test-api-errortype.c
+++ b/tests/unit-core/test-api-errortype.c
@@ -72,5 +72,14 @@ main (void)
jerry_release_value (result);
+ char test_invalid_error[] = "Object.create(Error.prototype)";
+ result = jerry_eval ((const jerry_char_t *) test_invalid_error,
+ sizeof (test_invalid_error) - 1,
+ JERRY_PARSE_NO_OPTS);
+ TEST_ASSERT (!jerry_value_is_error (result) && jerry_value_is_object (result));
+ TEST_ASSERT (jerry_get_error_type (result) == JERRY_ERROR_NONE);
+
+ jerry_release_value (result);
+
jerry_cleanup ();
} /* main */