Introduce C API to query the type of an Error object (#2177)
New api function: * jerry_get_error_type Additionally update a few places where this new function can be used. JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com
This commit is contained in:
+24
-1
@@ -42,7 +42,8 @@
|
||||
JERRY_STATIC_ASSERT (sizeof (jerry_value_t) == sizeof (ecma_value_t),
|
||||
size_of_jerry_value_t_must_be_equal_to_size_of_ecma_value_t);
|
||||
|
||||
JERRY_STATIC_ASSERT ((int) ECMA_ERROR_COMMON == (int) JERRY_ERROR_COMMON
|
||||
JERRY_STATIC_ASSERT ((int) ECMA_ERROR_NONE == (int) JERRY_ERROR_NONE
|
||||
&& (int) ECMA_ERROR_COMMON == (int) JERRY_ERROR_COMMON
|
||||
&& (int) ECMA_ERROR_EVAL == (int) JERRY_ERROR_EVAL
|
||||
&& (int) ECMA_ERROR_RANGE == (int) JERRY_ERROR_RANGE
|
||||
&& (int) ECMA_ERROR_REFERENCE == (int) JERRY_ERROR_REFERENCE
|
||||
@@ -918,6 +919,28 @@ jerry_value_t jerry_get_value_without_error_flag (jerry_value_t value) /**< api
|
||||
return jerry_acquire_value (jerry_get_arg_value (value));
|
||||
} /* jerry_get_value_without_error_flag */
|
||||
|
||||
/**
|
||||
* Return the type of the Error object if possible.
|
||||
*
|
||||
* @return one of the jerry_error_t value as the type of the Error object
|
||||
* JERRY_ERROR_NONE - if the input value is not an Error object
|
||||
*/
|
||||
jerry_error_t
|
||||
jerry_get_error_type (const jerry_value_t value) /**< api value */
|
||||
{
|
||||
jerry_value_t object = jerry_get_arg_value (value);
|
||||
|
||||
if (!ecma_is_value_object (object))
|
||||
{
|
||||
return JERRY_ERROR_NONE;
|
||||
}
|
||||
|
||||
ecma_object_t *object_p = ecma_get_object_from_value (object);
|
||||
ecma_standard_error_t error_type = ecma_get_error_type (object_p);
|
||||
|
||||
return error_type;
|
||||
} /* jerry_get_error_type */
|
||||
|
||||
/**
|
||||
* Get boolean from the specified value.
|
||||
*
|
||||
|
||||
@@ -30,10 +30,36 @@
|
||||
* \addtogroup exceptions Exceptions
|
||||
* @{
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
ecma_standard_error_t error_type;
|
||||
ecma_builtin_id_t error_prototype_id;
|
||||
} ecma_error_mapping_t;
|
||||
|
||||
const ecma_error_mapping_t ecma_error_mappings[] =
|
||||
{
|
||||
#define ERROR_ELEMENT(TYPE, ID) { TYPE, ID }
|
||||
ERROR_ELEMENT (ECMA_ERROR_COMMON, ECMA_BUILTIN_ID_ERROR_PROTOTYPE),
|
||||
|
||||
#ifndef CONFIG_DISABLE_ERROR_BUILTINS
|
||||
ERROR_ELEMENT (ECMA_ERROR_EVAL, ECMA_BUILTIN_ID_EVAL_ERROR_PROTOTYPE),
|
||||
ERROR_ELEMENT (ECMA_ERROR_RANGE, ECMA_BUILTIN_ID_RANGE_ERROR_PROTOTYPE),
|
||||
ERROR_ELEMENT (ECMA_ERROR_REFERENCE, ECMA_BUILTIN_ID_REFERENCE_ERROR_PROTOTYPE),
|
||||
ERROR_ELEMENT (ECMA_ERROR_TYPE, ECMA_BUILTIN_ID_TYPE_ERROR_PROTOTYPE),
|
||||
ERROR_ELEMENT (ECMA_ERROR_URI, ECMA_BUILTIN_ID_URI_ERROR_PROTOTYPE),
|
||||
ERROR_ELEMENT (ECMA_ERROR_SYNTAX, ECMA_BUILTIN_ID_SYNTAX_ERROR_PROTOTYPE),
|
||||
#endif /* !CONFIG_DISABLE_ERROR_BUILTINS */
|
||||
|
||||
#undef ERROR_ELEMENT
|
||||
};
|
||||
|
||||
/**
|
||||
* Standard ecma-error object constructor.
|
||||
*
|
||||
* Note:
|
||||
* calling with ECMA_ERROR_NONE does not make sense thus it will
|
||||
* cause a fault in the system.
|
||||
*
|
||||
* @return pointer to ecma-object representing specified error
|
||||
* with reference counter set to one.
|
||||
*/
|
||||
@@ -86,6 +112,12 @@ ecma_new_standard_error (ecma_standard_error_t error_type) /**< native error typ
|
||||
prototype_id = ECMA_BUILTIN_ID_SYNTAX_ERROR_PROTOTYPE;
|
||||
break;
|
||||
}
|
||||
|
||||
case ECMA_ERROR_NONE:
|
||||
{
|
||||
JERRY_UNREACHABLE ();
|
||||
break;
|
||||
}
|
||||
}
|
||||
#else
|
||||
JERRY_UNUSED (error_type);
|
||||
@@ -105,6 +137,32 @@ ecma_new_standard_error (ecma_standard_error_t error_type) /**< native error typ
|
||||
return new_error_obj_p;
|
||||
} /* ecma_new_standard_error */
|
||||
|
||||
/**
|
||||
* Return the error type for an Error object.
|
||||
*
|
||||
* @return one of the ecma_standard_error_t value
|
||||
* if it is not an Error object then ECMA_ERROR_NONE will be returned
|
||||
*/
|
||||
ecma_standard_error_t
|
||||
ecma_get_error_type (ecma_object_t *error_object) /**< possible error object */
|
||||
{
|
||||
ecma_object_t *prototype_p = ecma_get_object_prototype (error_object);
|
||||
if (prototype_p != NULL)
|
||||
{
|
||||
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 ECMA_ERROR_NONE;
|
||||
} /* ecma_get_error_type */
|
||||
|
||||
/**
|
||||
* Standard ecma-error object constructor.
|
||||
*
|
||||
|
||||
@@ -39,6 +39,8 @@
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
ECMA_ERROR_NONE, /**< Not an Error */
|
||||
|
||||
ECMA_ERROR_COMMON, /**< Error */
|
||||
ECMA_ERROR_EVAL, /**< EvalError */
|
||||
ECMA_ERROR_RANGE, /**< RangeError */
|
||||
@@ -48,6 +50,7 @@ typedef enum
|
||||
ECMA_ERROR_URI /**< URIError */
|
||||
} ecma_standard_error_t;
|
||||
|
||||
ecma_standard_error_t ecma_get_error_type (ecma_object_t *error_object);
|
||||
ecma_object_t *ecma_new_standard_error (ecma_standard_error_t error_type);
|
||||
ecma_object_t *ecma_new_standard_error_with_message (ecma_standard_error_t error_type, ecma_string_t *message_string_p);
|
||||
ecma_value_t ecma_raise_standard_error (ecma_standard_error_t error_type, const lit_utf8_byte_t *msg_p);
|
||||
|
||||
@@ -64,6 +64,8 @@ typedef enum
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
JERRY_ERROR_NONE = 0, /**< No Error */
|
||||
|
||||
JERRY_ERROR_COMMON, /**< Error */
|
||||
JERRY_ERROR_EVAL, /**< EvalError */
|
||||
JERRY_ERROR_RANGE, /**< RangeError */
|
||||
@@ -299,6 +301,11 @@ void jerry_value_set_error_flag (jerry_value_t *value_p);
|
||||
void jerry_value_set_abort_flag (jerry_value_t *value_p);
|
||||
jerry_value_t jerry_get_value_without_error_flag (jerry_value_t value);
|
||||
|
||||
/**
|
||||
* Error object function(s).
|
||||
*/
|
||||
jerry_error_t jerry_get_error_type (const jerry_value_t value);
|
||||
|
||||
/**
|
||||
* Getter functions of 'jerry_value_t'.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user