Repository: jerryscript-project/jerryscript
File: jerry-core/api/jerry.c (C)

Code before:
  }

  bool is_key_value_container = *is_key_value_p;

  for (uint32_t i = index; i < entry_count; i+= entry_size)
  {
    ecma_value_t *entry_p = start_p + i;

    if (ecma_is_value_empty (*entry_p))
    {
      continue;
    }

    if (is_value_iterator)
    {
      ext_obj_p->u.cls.u2.iterator_index = (uint16_t) (i + entry_size);

      if (iterator_kind != ECMA_ITERATOR_ENTRIES)
      {
        ext_obj_p->u.cls.u2.iterator_index = (uint16_t) (i + entry_size);
      }
    }

    if (is_key_value_container)
    {
      ecma_collection_push_back (collection_buffer, ecma_copy_value_if_not_object (*entry_p));
      ecma_collection_push_back (collection_buffer, ecma_copy_value_if_not_object (entry_p[1]));
    }
    else if (iterator_kind == ECMA_ITERATOR_VALUES)
    {
      ecma_collection_push_back (collection_buffer, ecma_copy_value_if_not_object (entry_p[1]));
    }
    else
    {
      ecma_collection_push_back (collection_buffer, ecma_copy_value_if_not_object (*entry_p));
    }

  }

  return ecma_op_new_array_object_from_collection (collection_buffer, false);
#endif /* JERRY_BUILTIN_CONTAINER */
  JERRY_UNUSED (value);
  JERRY_UNUSED (is_key_value_p);
  return jerry_throw (ecma_raise_type_error (ECMA_ERR_MSG ("Containers are disabled")));
} /* jerry_get_buffer_from_container */

/**
 * @}
 */


Review comment (suggestion):
At this point the `collection_buffer` is empty? If so do we need to create the `collection_buffer` that early? This seems to be a simple empty array return.

Code after:
jerry_value_t
jerry_get_array_from_container (jerry_value_t value, /**< the container or iterator object */
                                bool *is_key_value_p) /**< [out] is key-value structure */
{
  jerry_assert_api_available ();

#if JERRY_BUILTIN_CONTAINER
  const char *container_needed = ECMA_ERR_MSG ("Value is not a Container or Iterator");

  if (!ecma_is_value_object (value))
  {
    return jerry_throw (ecma_raise_type_error (container_needed));
  }

  ecma_object_t *obj_p = ecma_get_object_from_value (value);

  if (ecma_get_object_type (obj_p) != ECMA_OBJECT_TYPE_CLASS)
  {
    return jerry_throw (ecma_raise_type_error (container_needed));
  }

  ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) obj_p;

  uint32_t entry_count;
  uint8_t entry_size;

  uint32_t index = 0;
  uint8_t iterator_kind = ECMA_ITERATOR__COUNT;
  ecma_value_t *start_p;

  *is_key_value_p = false;

  if (ext_obj_p->u.cls.type == ECMA_OBJECT_CLASS_MAP_ITERATOR
      || ext_obj_p->u.cls.type == ECMA_OBJECT_CLASS_SET_ITERATOR)
  {
    ecma_value_t iterated_value = ext_obj_p->u.cls.u3.iterated_value;

    if (ecma_is_value_empty (iterated_value))
    {
      return ecma_op_new_array_object_from_collection (ecma_new_collection (), false);
    }

    ecma_extended_object_t *map_object_p = (ecma_extended_object_t *) (ecma_get_object_from_value (iterated_value));

    ecma_collection_t *container_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_collection_t, map_object_p->u.cls.u3.value);
    entry_count = ECMA_CONTAINER_ENTRY_COUNT (container_p);
    index = ext_obj_p->u.cls.u2.iterator_index;

    entry_size = ecma_op_container_entry_size (map_object_p->u.cls.u2.container_id);
    start_p = ECMA_CONTAINER_START (container_p);