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

Code before:
{
  ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) regexp_obj_p;

#if !ENABLED (JERRY_ESNEXT)
  if (ext_obj_p->u.class_prop.class_id == LIT_MAGIC_STRING_UNDEFINED)
  {
    /* This instance has not been initialized before. */
    ecma_regexp_create_props (regexp_obj_p, pattern_str_p, flags);
  }
  else
  {
    ecma_regexp_update_props (regexp_obj_p, pattern_str_p, flags);
  }
#endif /* !ENABLED (JERRY_ESNEXT) */

#if ENABLED (JERRY_ESNEXT)
  JERRY_UNUSED (pattern_str_p);
  JERRY_UNUSED (flags);
#endif /* ENABLED (JERRY_ESNEXT) */

  ecma_value_t old_bc = ext_obj_p->u.class_prop.u.value;

  ext_obj_p->u.class_prop.class_id = LIT_MAGIC_STRING_REGEXP_UL;
  ECMA_SET_INTERNAL_VALUE_POINTER (ext_obj_p->u.class_prop.u.value, bc_p);

  ecma_value_t set_last_index = ecma_builtin_helper_def_prop (regexp_obj_p,
                                                              ecma_get_magic_string (LIT_MAGIC_STRING_LASTINDEX_UL),
                                                              ecma_make_uint32_value (0),
                                                              ECMA_PROPERTY_FLAG_WRITABLE | ECMA_PROP_IS_THROW);

  if (ECMA_IS_VALUE_ERROR (set_last_index) && old_bc != JMEM_CP_NULL)
  {
    re_compiled_code_t *old_bc_p = ECMA_GET_INTERNAL_VALUE_POINTER (re_compiled_code_t,
                                                                    old_bc);
    ecma_bytecode_deref ((ecma_compiled_code_t *) old_bc_p);
  }

  return set_last_index;
} /* ecma_op_regexp_initialize */

/**
 * Method for creating a RegExp object from pattern.
 *
 * Note:
 *      Allocation have to happen before invoking this function using ecma_op_regexp_alloc.
 *
 * @return ecma_value_t
 */
ecma_value_t
ecma_op_create_regexp_from_pattern (ecma_object_t *regexp_obj_p, /**< RegExp object */
                                    ecma_value_t pattern_value, /**< pattern */

Review comment (bug):
I'd rather not move this into regexp_initialize, the only place where this can throw is in the compile method. Cant we just move the def_prop call in the compile method down into the final `if (!ECMA_IS_VALUE_ERROR (ret_value))` block?

Code after:
 * Helper method for initializing an aready existing RegExp object.
 */
static void
ecma_op_regexp_initialize (ecma_object_t *regexp_obj_p, /**< RegExp object */
                           const re_compiled_code_t *bc_p, /**< bytecode */
                           ecma_string_t *pattern_str_p, /**< pattern */
                           uint16_t flags) /**< flags */
{
  ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) regexp_obj_p;

#if !ENABLED (JERRY_ESNEXT)
  if (ext_obj_p->u.class_prop.class_id == LIT_MAGIC_STRING_UNDEFINED)
  {
    /* This instance has not been initialized before. */
    ecma_regexp_create_props (regexp_obj_p, pattern_str_p, flags);
  }
  else
  {
    ecma_regexp_update_props (regexp_obj_p, pattern_str_p, flags);
  }
#endif /* !ENABLED (JERRY_ESNEXT) */

#if ENABLED (JERRY_ESNEXT)
  JERRY_UNUSED (pattern_str_p);
  JERRY_UNUSED (flags);
#endif /* ENABLED (JERRY_ESNEXT) */

  ext_obj_p->u.class_prop.class_id = LIT_MAGIC_STRING_REGEXP_UL;
  ECMA_SET_INTERNAL_VALUE_POINTER (ext_obj_p->u.class_prop.u.value, bc_p);
} /* ecma_op_regexp_initialize */

/**
 * Method for creating a RegExp object from pattern.
 *
 * Note:
 *      Allocation have to happen before invoking this function using ecma_op_regexp_alloc.
 *
 * @return ecma_value_t
 */
ecma_value_t
ecma_op_create_regexp_from_pattern (ecma_object_t *regexp_obj_p, /**< RegExp object */
                                    ecma_value_t pattern_value, /**< pattern */
                                    ecma_value_t flags_value) /**< flags */
{
  ecma_string_t *pattern_str_p = ecma_regexp_read_pattern_str_helper (pattern_value);
  uint16_t flags = 0;

  if (JERRY_UNLIKELY (pattern_str_p == NULL))
  {
    return ECMA_VALUE_ERROR;
  }