Repository: jerryscript-project/jerryscript
File: jerry-core/ecma/operations/ecma-exceptions.c (C)

Code before:
} /* ecma_new_aggregate_error */

#endif /* JERRY_ESNEXT */

/**
 * Return the error type for an Error object.
 *
 * @return one of the jerry_error_t value
 *         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_p) /**< possible error object */
{
  if (!ecma_object_class_is (error_object_p, ECMA_OBJECT_CLASS_ERROR))
  {
    return JERRY_ERROR_NONE;
  }

  return (jerry_error_t) ((ecma_extended_object_t *) error_object_p)->u.cls.u1.error_type;
} /* ecma_get_error_type */

/**
 * Raise a standard ecma-error with the given type and message.
 *
 * Note: The given buffer must be CESU8 encoded and the buffer must exist until the error message is not freed.
 *
 * @return ecma value
 *         Returned value must be freed with ecma_free_value
 */
ecma_value_t
ecma_raise_standard_error (jerry_error_t error_type, /**< error type */
                           const char *msg_p) /**< error message */
{
  ecma_object_t *error_obj_p;
  const lit_utf8_byte_t *str_p = (lit_utf8_byte_t *) msg_p;

  if (msg_p != NULL)
  {
    ecma_string_t *error_msg_p =
      ecma_new_ecma_external_string_from_cesu8 (str_p, lit_zt_utf8_string_size (str_p), NULL);
    error_obj_p = ecma_new_standard_error (error_type, error_msg_p);
    ecma_deref_ecma_string (error_msg_p);
  }
  else
  {
    error_obj_p = ecma_new_standard_error (error_type, NULL);
  }

  jcontext_raise_exception (ecma_make_object_value (error_obj_p));
  return ECMA_VALUE_ERROR;
} /* ecma_raise_standard_error */

Review comment (suggestion):
The `char *` message argument should be replaced with `ecma_error_msg_t`, and this function should call `ecma_get_error_msg`. This guarantees that the string is a magic string and it exists statically, and then there's no reason for the note. Also the string size can then be retrieved, without the need for calling `lit_zt_utf8_string_size`.

Code after:
} /* ecma_new_aggregate_error */

#endif /* JERRY_ESNEXT */

/**
 * Return the error type for an Error object.
 *
 * @return one of the jerry_error_t value
 *         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_p) /**< possible error object */
{
  if (!ecma_object_class_is (error_object_p, ECMA_OBJECT_CLASS_ERROR))
  {
    return JERRY_ERROR_NONE;
  }

  return (jerry_error_t) ((ecma_extended_object_t *) error_object_p)->u.cls.u1.error_type;
} /* ecma_get_error_type */

/**
 * Raise a standard ecma-error with the given type and message.
 *
 * @return ecma value
 *         Returned value must be freed with ecma_free_value
 */
ecma_value_t
ecma_raise_standard_error (jerry_error_t error_type, /**< error type */
                           ecma_error_msg_t msg) /**< error message */
{
  ecma_object_t *error_obj_p;
  const lit_utf8_byte_t *str_p = (lit_utf8_byte_t *) ecma_get_error_msg (msg);

  if (msg != ECMA_ERR_EMPTY)
  {
    ecma_string_t *error_msg_p = ecma_new_ecma_external_string_from_cesu8 (str_p, ecma_get_error_size (msg), NULL);
    error_obj_p = ecma_new_standard_error (error_type, error_msg_p);
    ecma_deref_ecma_string (error_msg_p);
  }
  else
  {
    error_obj_p = ecma_new_standard_error (error_type, NULL);
  }

  jcontext_raise_exception (ecma_make_object_value (error_obj_p));
  return ECMA_VALUE_ERROR;
} /* ecma_raise_standard_error */

#if JERRY_ERROR_MESSAGES