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

Code before:
  }

  return ECMA_VALUE_TRUE;
} /* ecma_op_typedarray_define_index_prop */

/* Specify the creation of a new TypedArray
 * object using a constructor function.
 *
 * See also: ES11 22.2.4.6
 *
 * 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);
  if (ECMA_IS_VALUE_ERROR (ret_val))
  {
    return ret_val;
  }

  // 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);

Review comment (bug):
This part is completely wrong.

`ecma_op_function_construct` should be called with `constructor_p, constructor_p, arguments_list_p, arguments_list_len`

Code after:
      return set_element;
    }
  }

  return ECMA_VALUE_TRUE;
} /* ecma_op_typedarray_define_index_prop */

/**
 * Specify the creation of a new TypedArray
 * object using a constructor function.
 *
 * 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"));
    }