Repository: jerryscript-project/jerryscript
File: jerry-core/ecma/builtin-objects/ecma-builtin-regexp-prototype.c (C)

Code before:
{
  JERRY_UNUSED (arguments_number);

#if ENABLED (JERRY_BUILTIN_ANNEXB)
  if (builtin_routine_id == ECMA_REGEXP_PROTOTYPE_ROUTINE_COMPILE)
  {
    return ecma_builtin_regexp_prototype_compile (this_arg, arguments_list_p[0], arguments_list_p[1]);
  }
#endif /* ENABLED (JERRY_BUILTIN_ANNEXB) */

  /* 1. */
  if (!ecma_is_value_object (this_arg))
  {
    return ecma_raise_type_error (ECMA_ERR_MSG ("'this' is not an object"));
  }

  /* 2. */
  ecma_object_t *obj_p = ecma_get_object_from_value (this_arg);

#if !ENABLED (JERRY_ESNEXT)
  bool require_regexp = builtin_routine_id <= ECMA_REGEXP_PROTOTYPE_ROUTINE_TO_STRING;
#else /* ENABLED (JERRY_ESNEXT) */
  bool require_regexp = builtin_routine_id < ECMA_REGEXP_PROTOTYPE_ROUTINE_TEST;
#endif /* ENABLED (JERRY_ESNEXT) */

  if (require_regexp && !ecma_object_class_is (obj_p, LIT_MAGIC_STRING_REGEXP_UL))
  {
    return ecma_raise_type_error (ECMA_ERR_MSG ("'this' is not a RegExp object"));
  }

  switch (builtin_routine_id)
  {
    case ECMA_REGEXP_PROTOTYPE_ROUTINE_TEST:
    {
      return ecma_builtin_regexp_prototype_test (this_arg, arguments_list_p[0]);
    }
    case ECMA_REGEXP_PROTOTYPE_ROUTINE_EXEC:
    {
      return ecma_builtin_regexp_prototype_exec (this_arg, arguments_list_p[0]);
    }
    case ECMA_REGEXP_PROTOTYPE_ROUTINE_TO_STRING:
    {
      return ecma_builtin_regexp_prototype_to_string (obj_p);
    }
#if ENABLED (JERRY_ESNEXT)
    case ECMA_REGEXP_PROTOTYPE_ROUTINE_SYMBOL_SEARCH:
    {
      return ecma_regexp_search_helper (this_arg, arguments_list_p[0]);
    }
    case ECMA_REGEXP_PROTOTYPE_ROUTINE_SYMBOL_MATCH:
    {

Review comment (suggestion):
I would prefer to do the RegExp check first, so that error messages remain consistent. Otherwise when we have a non-object this value where a RegExp is required, the error message would only state that it should be an object.

Code after:
                                                const ecma_value_t arguments_list_p[], /**< list of arguments
                                                                                        *   passed to routine */
                                                uint32_t arguments_number) /**< length of arguments' list */
{
  JERRY_UNUSED (arguments_number);

#if !ENABLED (JERRY_ESNEXT)
  bool require_regexp = builtin_routine_id <= ECMA_REGEXP_PROTOTYPE_ROUTINE_TO_STRING;
#else /* ENABLED (JERRY_ESNEXT) */
  bool require_regexp = builtin_routine_id < ECMA_REGEXP_PROTOTYPE_ROUTINE_TEST;
#endif /* ENABLED (JERRY_ESNEXT) */

  ecma_object_t *obj_p = NULL;

  if (ecma_is_value_object (this_arg))
  {
    /* 2. */
    obj_p = ecma_get_object_from_value (this_arg);

    if (require_regexp && !ecma_object_class_is (obj_p, LIT_MAGIC_STRING_REGEXP_UL))
    {
      return ecma_raise_type_error (ECMA_ERR_MSG ("'this' is not a RegExp object"));
    }
  }
  /* 1. */
  else
  {
    return ecma_raise_type_error (ECMA_ERR_MSG ("'this' is not an object"));
  }

  JERRY_ASSERT (obj_p != NULL);

  switch (builtin_routine_id)
  {
#if ENABLED (JERRY_BUILTIN_ANNEXB)
    case ECMA_REGEXP_PROTOTYPE_ROUTINE_COMPILE:
    {
      return ecma_builtin_regexp_prototype_compile (this_arg, arguments_list_p[0], arguments_list_p[1]);
    }
#endif /* ENABLED (JERRY_BUILTIN_ANNEXB) */
    case ECMA_REGEXP_PROTOTYPE_ROUTINE_TEST:
    {
      return ecma_builtin_regexp_prototype_test (this_arg, arguments_list_p[0]);
    }
    case ECMA_REGEXP_PROTOTYPE_ROUTINE_EXEC:
    {
      return ecma_builtin_regexp_prototype_exec (this_arg, arguments_list_p[0]);
    }
    case ECMA_REGEXP_PROTOTYPE_ROUTINE_TO_STRING:
    {
      return ecma_builtin_regexp_prototype_to_string (obj_p);