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

Code before:

/**
 * Checks whether a resolver is called before.
 *
 * @return true if it was called before, false otherwise
 */
static inline bool JERRY_ATTR_ALWAYS_INLINE
ecma_is_resolver_already_called (ecma_object_t *promise_obj_p) /**< promise */
{
  return (ecma_promise_get_flags (promise_obj_p) & ECMA_PROMISE_ALREADY_RESOLVED) != 0;
} /* ecma_is_resolver_already_called */

/**
 * HostPromiseRejectionTracker
 *
 * See also: ES11 25.6.1.9
 */
static void
ecma_promise_track_rejection (ecma_object_t *obj_p, /**< promise */
                              ecma_value_t reason) /**< reason for reject */
{
  JERRY_ASSERT (ecma_is_promise (obj_p));

  if (!(ecma_promise_get_flags (obj_p) & ECMA_PROMISE_HANDLED))
  {
    jerry_port_report_unhandled_promise_rejection (ecma_make_object_value (obj_p), reason);
  }
} /* ecma_promise_track_rejection */

/**
 * Reject a Promise with a reason.
 *
 * See also: ES2015 25.4.1.7
 */
void
ecma_reject_promise (ecma_value_t promise, /**< promise */
                     ecma_value_t reason) /**< reason for reject */
{
  ecma_object_t *obj_p = ecma_get_object_from_value (promise);

  JERRY_ASSERT (ecma_promise_get_flags (obj_p) & ECMA_PROMISE_IS_PENDING);

  ecma_promise_set_state (obj_p, false);
  ecma_promise_set_result (obj_p, ecma_copy_value_if_not_object (reason));
  ecma_promise_object_t *promise_p = (ecma_promise_object_t *) obj_p;

  /* GC can be triggered by ecma_new_collection so freeing the collection
     first and creating a new one might cause a heap after use event. */
  ecma_collection_t *reactions = promise_p->reactions;

  /* Fulfill reactions will never be triggered. */

Review comment (refactor):
The name seems a bit counterintuitive to me. If the abstract operation of the spec is called HostPromiseRejectionTracker and the ecma core function is called `ecma_promise_track_rejection`, then why is the port function called `jerry_port_report_unhandled_promise_rejection`? Why report? Why unhandled?

Wouldn't be `jerry_port_track_promise_rejection` more natural?

Code after:

/**
 * Checks whether a resolver is called before.
 *
 * @return true if it was called before, false otherwise
 */
static inline bool JERRY_ATTR_ALWAYS_INLINE
ecma_is_resolver_already_called (ecma_object_t *promise_obj_p) /**< promise */
{
  return (ecma_promise_get_flags (promise_obj_p) & ECMA_PROMISE_ALREADY_RESOLVED) != 0;
} /* ecma_is_resolver_already_called */

/**
 * HostPromiseRejectionTracker
 *
 * See also: ES11 25.6.1.9
 */
static void
ecma_track_promise_rejection (ecma_object_t *obj_p, /**< rejected promise */
                              jerry_promise_rejection_operation_t operation) /**< operation */
{
  JERRY_ASSERT (ecma_is_promise (obj_p));

  if (!(ecma_promise_get_flags (obj_p) & ECMA_PROMISE_HANDLED))
  {
    jerry_port_track_promise_rejection (ecma_make_object_value (obj_p), operation);
  }
} /* ecma_track_promise_rejection */

/**
 * Reject a Promise with a reason.
 *
 * See also: ES2015 25.4.1.7
 */
void
ecma_reject_promise (ecma_value_t promise, /**< promise */
                     ecma_value_t reason) /**< reason for reject */
{
  ecma_object_t *obj_p = ecma_get_object_from_value (promise);

  JERRY_ASSERT (ecma_promise_get_flags (obj_p) & ECMA_PROMISE_IS_PENDING);

  ecma_promise_set_state (obj_p, false);
  ecma_promise_set_result (obj_p, ecma_copy_value_if_not_object (reason));
  ecma_promise_object_t *promise_p = (ecma_promise_object_t *) obj_p;

  /* GC can be triggered by ecma_new_collection so freeing the collection
     first and creating a new one might cause a heap after use event. */
  ecma_collection_t *reactions = promise_p->reactions;

  /* Fulfill reactions will never be triggered. */