/**
 * Create internal property with specified identifier and store external pointer in the property.
 *
 * Note:
 *      property identifier should be one of the following:
 *        - ECMA_INTERNAL_PROPERTY_NATIVE_CODE;
 *        - ECMA_INTERNAL_PROPERTY_NATIVE_HANDLE;
 *        - ECMA_INTERNAL_PROPERTY_FREE_CALLBACK.
 *
 * @return true - if property was just created with specified value,
 *         false - otherwise, if property existed before the call, it's value was updated.
 */
bool
ecma_create_external_pointer_property (ecma_object_t *obj_p, /**< object to create property in */
                                       ecma_internal_property_id_t id, /**< identifier of internal
                                                                        *   property to create */
                                       ecma_external_pointer_t ptr_value) /**< value to store in the property */
{
  JERRY_ASSERT (id == ECMA_INTERNAL_PROPERTY_NATIVE_CODE
                || id == ECMA_INTERNAL_PROPERTY_NATIVE_HANDLE
                || id == ECMA_INTERNAL_PROPERTY_FREE_CALLBACK);

  bool is_new;
  ecma_property_t *prop_p = ecma_find_internal_property (obj_p, id);

  if (prop_p == NULL)
  {
    prop_p = ecma_create_internal_property (obj_p, id);

    is_new = true;
  }
  else
  {
    is_new = false;
  }

  JERRY_STATIC_ASSERT (sizeof (uint32_t) <= sizeof (ECMA_PROPERTY_VALUE_PTR (prop_p)->value),
                       size_of_internal_property_value_must_be_greater_than_or_equal_to_4_bytes);

  if (sizeof (ecma_external_pointer_t) == sizeof (uint32_t))
  {
    ECMA_PROPERTY_VALUE_PTR (prop_p)->value = (uint32_t) ptr_value;
  }
  else
  {
    ecma_external_pointer_t *handler_p;

    if (is_new)
    {
      handler_p = ecma_alloc_external_pointer ();

      ECMA_SET_NON_NULL_POINTER (ECMA_PROPERTY_VALUE_PTR (prop_p)->value, handler_p);
    }
    else
    {
      handler_p = ECMA_GET_NON_NULL_POINTER (ecma_external_pointer_t,
                                             ECMA_PROPERTY_VALUE_PTR (prop_p)->value);
    }

    *handler_p = ptr_value;
  }

  return is_new;
} /* ecma_create_external_pointer_property */
/**
 * Create internal property with specified identifier and store external pointer in the property.
 *
 * Note:
 *      property identifier should be one of the following:
 *        - ECMA_INTERNAL_PROPERTY_NATIVE_HANDLE;
 *        - ECMA_INTERNAL_PROPERTY_FREE_CALLBACK.
 *
 * @return true - if property was just created with specified value,
 *         false - otherwise, if property existed before the call, it's value was updated.
 */
bool
ecma_create_external_pointer_property (ecma_object_t *obj_p, /**< object to create property in */
                                       ecma_internal_property_id_t id, /**< identifier of internal
                                                                        *   property to create */
                                       ecma_external_pointer_t ptr_value) /**< value to store in the property */
{
  JERRY_ASSERT (id == ECMA_INTERNAL_PROPERTY_NATIVE_HANDLE
                || id == ECMA_INTERNAL_PROPERTY_FREE_CALLBACK);

  ecma_value_t *prop_p = ecma_find_internal_property (obj_p, id);
  bool is_new = (prop_p == NULL);

  if (is_new)
  {
    prop_p = ecma_create_internal_property (obj_p, id);
  }

  JERRY_STATIC_ASSERT (sizeof (uint32_t) <= sizeof (ECMA_PROPERTY_VALUE_PTR (prop_p)->value),
                       size_of_internal_property_value_must_be_greater_than_or_equal_to_4_bytes);

#ifdef ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY

  *prop_p = (ecma_value_t) ptr_value;

#else /* !ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY */

  ecma_external_pointer_t *handler_p;

  if (is_new)
  {
    handler_p = ecma_alloc_external_pointer ();

    ECMA_SET_NON_NULL_POINTER (*prop_p, handler_p);
  }
  else
  {
    handler_p = ECMA_GET_NON_NULL_POINTER (ecma_external_pointer_t, *prop_p);
  }

  *handler_p = ptr_value;

#endif /* ECMA_VALUE_CAN_STORE_UINTPTR_VALUE_DIRECTLY */

  return is_new;
} /* ecma_create_external_pointer_property */
/**
 * Create internal property with specified identifier and store external pointer in the property.
 *
 * Note:
 *      property identifier should be one of the following:
 *        - ECMA_INTERNAL_PROPERTY_NATIVE_CODE;
 *        - ECMA_INTERNAL_PROPERTY_NATIVE_HANDLE;
 *        - ECMA_INTERNAL_PROPERTY_FREE_CALLBACK.
 *
 * @return true - if property was just created with specified value,
 *         false - otherwise, if property existed before the call, it's value was updated.
 */
bool
ecma_create_external_pointer_property (ecma_object_t *obj_p, /**< object to create property in */
                                       ecma_internal_property_id_t id, /**< identifier of internal
                                                                        *   property to create */
                                       ecma_external_pointer_t ptr_value) /**< value to store in the property */
{
  JERRY_ASSERT (id == ECMA_INTERNAL_PROPERTY_NATIVE_CODE
                || id == ECMA_INTERNAL_PROPERTY_NATIVE_HANDLE
                || id == ECMA_INTERNAL_PROPERTY_FREE_CALLBACK);

  bool ret_val;
  ecma_property_t *prop_p = ecma_find_internal_property (obj_p, id);

  if (prop_p == NULL)
  {
    prop_p = ecma_create_internal_property (obj_p, id);

    ret_val = true;
  }
  else
  {
    ret_val = false;
  }

  JERRY_STATIC_ASSERT (sizeof (uint32_t) <= sizeof (prop_p->u.internal_property.value));

  if (sizeof (ecma_external_pointer_t) == sizeof (uint32_t))
  {
    prop_p->u.internal_property.value = (uint32_t) ptr_value;
  }
  else
  {
    ecma_external_pointer_t *handler_p = ecma_alloc_external_pointer ();
    *handler_p = ptr_value;

    ECMA_SET_NON_NULL_POINTER (prop_p->u.internal_property.value, handler_p);
  }

  return ret_val;
} /* ecma_create_external_pointer_property */