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

Code before:

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

  ecma_object_t *target_obj_p = ecma_get_object_from_value (new_typedarray);
  ecma_typedarray_info_t target_info = ecma_typedarray_get_info (target_obj_p);

  ecma_typedarray_getter_fn_t src_typedarray_getter_cb = ecma_get_typedarray_getter_fn (src_info_p->id);
  ecma_typedarray_setter_fn_t target_typedarray_setter_cb = ecma_get_typedarray_setter_fn (target_info.id);

  uint32_t src_byte_pos = 0;

  for (uint32_t index = 0; index < src_info_p->length; index++)
  {
    ecma_value_t current_index = ecma_make_uint32_value (index);
    ecma_value_t element = src_typedarray_getter_cb (src_info_p->buffer_p + src_byte_pos);

    ecma_value_t call_args[] = { element, current_index, this_arg };
    ecma_value_t mapped_value = ecma_op_function_call (func_object_p, cb_this_arg, call_args, 3);

    ecma_free_value (current_index);
    ecma_free_value (element);

    if (ecma_arraybuffer_is_detached (src_info_p->array_buffer_p))
    {
      ecma_free_value (mapped_value);
      ecma_free_value (new_typedarray);
      return ecma_raise_type_error (ECMA_ERR_MSG ("ArrayBuffer has been detached"));
    }

    if (ECMA_IS_VALUE_ERROR (mapped_value))
    {
      ecma_free_value (new_typedarray);
      return mapped_value;
    }

    uint32_t target_byte_pos = index << target_info.shift;
    ecma_value_t set_element = target_typedarray_setter_cb (target_info.buffer_p + target_byte_pos, mapped_value);
    ecma_free_value (mapped_value);

    if (ECMA_IS_VALUE_ERROR (set_element))
    {
      ecma_free_value (new_typedarray);
      return set_element;
    }

    src_byte_pos += src_info_p->element_size;
  }


Review comment (suggestion):
There are several checks like this, which sould be after `if (ECMA_IS_VALUE_ERROR (mapped_value))`. Please check it in the whole PR.

Code after:

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

  ecma_object_t *target_obj_p = ecma_get_object_from_value (new_typedarray);
  ecma_typedarray_info_t target_info = ecma_typedarray_get_info (target_obj_p);

  ecma_typedarray_getter_fn_t src_typedarray_getter_cb = ecma_get_typedarray_getter_fn (src_info_p->id);
  ecma_typedarray_setter_fn_t target_typedarray_setter_cb = ecma_get_typedarray_setter_fn (target_info.id);

  uint32_t src_byte_pos = 0;

  for (uint32_t index = 0; index < src_info_p->length; index++)
  {
    ecma_value_t current_index = ecma_make_uint32_value (index);
    ecma_value_t element = src_typedarray_getter_cb (src_info_p->buffer_p + src_byte_pos);

    ecma_value_t call_args[] = { element, current_index, this_arg };
    ecma_value_t mapped_value = ecma_op_function_call (func_object_p, cb_this_arg, call_args, 3);

    ecma_free_value (current_index);
    ecma_free_value (element);

    if (ECMA_IS_VALUE_ERROR (mapped_value))
    {
      ecma_free_value (new_typedarray);
      return mapped_value;
    }

    if (ecma_arraybuffer_is_detached (src_info_p->array_buffer_p))
    {
      ecma_free_value (mapped_value);
      ecma_free_value (new_typedarray);
      return ecma_raise_type_error (ECMA_ERR_MSG (ecma_error_arraybuffer_is_detached));
    }

    uint32_t target_byte_pos = index << target_info.shift;
    ecma_value_t set_element = target_typedarray_setter_cb (target_info.buffer_p + target_byte_pos, mapped_value);
    ecma_free_value (mapped_value);

    if (ECMA_IS_VALUE_ERROR (set_element))
    {
      ecma_free_value (new_typedarray);
      return set_element;
    }

    src_byte_pos += src_info_p->element_size;
  }