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

Code before:
      return ECMA_VALUE_ERROR;
    }

    if (arguments_number > 1 && !ecma_is_value_undefined (argument_list_p[1]))
    {
      /* 8-9. */
      if (ECMA_IS_VALUE_ERROR (ecma_builtin_helper_uint32_index_normalize (argument_list_p[1],
                                                                           len,
                                                                           &end)))
      {
        return ECMA_VALUE_ERROR;
      }
    }
  }

  /* 10. */
  uint32_t new_len = (end >= start) ? (end - start) : 0;

  /* 11. */
  ecma_value_t ctor;
#if JERRY_BUILTIN_SHAREDARRAYBUFFER
  if (ecma_is_shared_arraybuffer (this_arg))
  {
    ctor = ecma_op_species_constructor (object_p, ECMA_BUILTIN_ID_SHARED_ARRAYBUFFER);
  }
  else
  {
#endif /* JERRY_BUILTIN_SHAREDARRAYBUFFER */
    ctor = ecma_op_species_constructor (object_p, ECMA_BUILTIN_ID_ARRAYBUFFER);
#if JERRY_BUILTIN_SHAREDARRAYBUFFER
  }
#endif /* JERRY_BUILTIN_SHAREDARRAYBUFFER */

  if (ECMA_IS_VALUE_ERROR (ctor))
  {
    return ctor;
  }

  /* 12. */
  ecma_object_t *ctor_obj_p = ecma_get_object_from_value (ctor);
  ecma_value_t new_len_value = ecma_make_uint32_value (new_len);

  ecma_value_t new_arraybuffer = ecma_op_function_construct (ctor_obj_p, ctor_obj_p, &new_len_value, 1);

  ecma_deref_object (ctor_obj_p);
  ecma_free_value (new_len_value);

  if (ECMA_IS_VALUE_ERROR (new_arraybuffer))
  {
    return new_arraybuffer;
  }

Review comment (suggestion):
Afaik the call to the constructor could be simplified:
```c
 int buffer_builtin_id ; /* sidenote: not sure if "int" is the correct type here, needs a double check. */
#if JERRY_BUILTIN_SHAREDARRAYBUFFER
  if (ecma_is_shared_arraybuffer (this_arg))
  {
    buffer_builtin_id = ECMA_BUILTIN_ID_SHARED_ARRAYBUFFER;
  }
  else
#endif /* JERRY_BUILTIN_SHAREDARRAYBUFFER */
  {
    buffer_builtin_id = ECMA_BUILTIN_ID_ARRAYBUFFER;
  }

  ecma_value_t ctor = ecma_op_species_constructor (object_p, array_buffer_id);
```

Code after:
    return ctor;
  }

  /* 12. */
  ecma_object_t *ctor_obj_p = ecma_get_object_from_value (ctor);
  ecma_value_t new_len_value = ecma_make_uint32_value (new_len);

  ecma_value_t new_arraybuffer = ecma_op_function_construct (ctor_obj_p, ctor_obj_p, &new_len_value, 1);

  ecma_deref_object (ctor_obj_p);
  ecma_free_value (new_len_value);

  if (ECMA_IS_VALUE_ERROR (new_arraybuffer))
  {
    return new_arraybuffer;
  }

  ecma_object_t *new_arraybuffer_p = ecma_get_object_from_value (new_arraybuffer);
  ecma_value_t ret_value = ECMA_VALUE_EMPTY;

  /* 13. */
  if (!(ecma_object_class_is (new_arraybuffer_p, ECMA_OBJECT_CLASS_ARRAY_BUFFER)
        || ecma_object_is_shared_arraybuffer (new_arraybuffer_p)))
  {
    ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("Return value is not an ArrayBuffer object"));
    goto free_new_arraybuffer;
  }

  /* 14-15. */
  if (ecma_arraybuffer_is_detached (new_arraybuffer_p))
  {
    ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("Returned ArrayBuffer has been detached"));
    goto free_new_arraybuffer;
  }

  /* 16. */
  if (new_arraybuffer == this_arg)
  {
    ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("ArrayBuffer subclass returned this from species constructor"));
    goto free_new_arraybuffer;
  }

  /* 17. */
  if (ecma_arraybuffer_get_length (new_arraybuffer_p) < new_len)
  {
    ret_value = ecma_raise_type_error (ECMA_ERR_MSG ("Derived ArrayBuffer constructor created a too small buffer"));
    goto free_new_arraybuffer;
  }

  /* 19. */
  if (ecma_arraybuffer_is_detached (object_p))