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

Code before:
 * Used by:
 *        - ecma_typedarray_species_create
 *
 * @return ecma_value_t
 */
ecma_value_t ecma_typedarray_create (ecma_object_t *constructor_p, /**< constructor function */
                                     ecma_value_t *arguments_list_p, /**< argument list */
                                     uint32_t arguments_list_len) /**< length of argument list */
{
  ecma_number_t num;
  if (ECMA_IS_VALUE_ERROR (ecma_op_to_number (arguments_list_p[0], &num)))
  {
    return ECMA_VALUE_ERROR;
  }
  uint32_t length = ecma_number_to_uint32 (num);

  // 1. Let newTypedArray be ? Construct(constructor, argumentList).
  ecma_value_t ret_val = ecma_op_function_construct (constructor_p,
                                                     constructor_p,
                                                     arguments_list_p,
                                                     length);
  // 2. Perform ? ValidateTypedArray(newTypedArray).
  if (!ecma_is_typedarray (ret_val))
  {
    ecma_free_value (ret_val);
    return ecma_raise_type_error (ECMA_ERR_MSG ("Constructed object is not TypedArray."));
  }

  // 3. If argumentList is a List of a single Number, then
  ecma_object_t *typedarray_p = ecma_get_object_from_value (ret_val);
  ecma_typedarray_info_t info = ecma_typedarray_get_info (typedarray_p);

  if ((arguments_list_len == 1) && (info.length < num))
  {
    ecma_free_value (ret_val);
    return ecma_raise_type_error (ECMA_ERR_MSG ("Constructed typedarray is smaller than filter call result"));
  }
  return ret_val;
} /* ecma_typedarray_create */

/* Specify the creation of a new TypedArray object
 * using a constructor function that is derived from this_arg.
 *
 * See also: ES11 22.2.4.7
 *
 * @return ecma_value_t
 */
ecma_value_t
ecma_typedarray_species_create (ecma_value_t this_arg, /**< this argument */
                                ecma_value_t *arguments_list_p) /**< the arg list passed to typedarray construct */
{

Review comment (suggestion):
`ret_val` can be `ECMA_VALUE_ERROR` at this point so you should create an early return after `Construct` call.

Code after:
 *
 * See also: ES11 22.2.4.6
 *
 * Used by:
 *        - ecma_typedarray_species_create
 *
 * @return ecma_value_t function object from created from constructor_p argument
 */

ecma_value_t
ecma_typedarray_create (ecma_object_t *constructor_p, /**< constructor function */
                        ecma_value_t *arguments_list_p, /**< argument list */
                        uint32_t arguments_list_len) /**< length of argument list */
{
  ecma_value_t ret_val = ecma_op_function_construct (constructor_p,
                                                     constructor_p,
                                                     arguments_list_p,
                                                     arguments_list_len);
  if (ECMA_IS_VALUE_ERROR (ret_val))
  {
    return ret_val;
  }

  if (!ecma_is_typedarray (ret_val))
  {
    ecma_free_value (ret_val);
    return ecma_raise_type_error (ECMA_ERR_MSG ("Constructed object is not TypedArray."));
  }

  ecma_object_t *typedarray_p = ecma_get_object_from_value (ret_val);

  if ((arguments_list_len == 1) && (ecma_is_value_number (arguments_list_p[0])))
  {
    ecma_number_t num = ecma_get_number_from_value (arguments_list_p[0]);
    ecma_typedarray_info_t info = ecma_typedarray_get_info (typedarray_p);

    if (info.length < num)
    {
      ecma_free_value (ret_val);
      return ecma_raise_type_error (ECMA_ERR_MSG ("Constructed typedarray is smaller than filter call result"));
    }
  }
  return ret_val;
} /* ecma_typedarray_create */

/* Specify the creation of a new TypedArray object
 * using a constructor function that is derived from this_arg.
 *
 * See also: ES11 22.2.4.7
 *
 * @return ecma value of the new typedarray object, constructed by default or species constructor