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

Code before:
 * Definition of Then Finally Function
 *
 * See also:
 *         ES2020 25.6.5.3.1
 *
 * @return ecma value
 */
ecma_value_t
ecma_promise_then_finally_cb (const ecma_value_t function_obj, /**< the function itself */
                              const ecma_value_t this_val, /**< this_arg of the function */
                              const ecma_value_t args_p[], /**< argument list */
                              const ecma_length_t args_count) /**< argument number */
{
  JERRY_UNUSED_3 (this_val, args_p, args_count);

  /* 7. */
  ecma_value_t promise = ecma_promise_than_catch_finally_helper (function_obj);

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

  /* 8. */
  ecma_object_t *thunk_obj_p;
  thunk_obj_p = ecma_create_object (ecma_builtin_get (ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE),
                                    sizeof (ecma_promise_value_thunk_t),
                                    ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION);

  ecma_promise_value_thunk_t *thunk_func_obj = (ecma_promise_value_thunk_t *) thunk_obj_p;
  thunk_func_obj->header.u.external_handler_cb = ecma_value_thunk_helper_cb;

  JERRY_ASSERT (args_count > 0);
  thunk_func_obj->value = ecma_copy_value_if_not_object (args_p[0]);

  /* 9. */
  ecma_value_t value_thunk = ecma_make_object_value (thunk_obj_p);
  ecma_value_t ret_value = ecma_op_invoke_by_magic_id (promise, LIT_MAGIC_STRING_THEN, &value_thunk, 1);

  ecma_free_value (promise);
  ecma_deref_object (thunk_obj_p);

  return ret_value;
} /* ecma_promise_then_finally_cb */

/**
 * Definition of Catch Finally Function
 *
 * See also:
 *         ES2020 25.6.5.3.2
 *

Review comment (suggestion):
~~AFAIK to create the function object we could use the `ecma_op_create_external_function_object` method to avoid "manually" creating the function object.~~ Nevermind, the `sizeof` arg is different in that method.

Code after:
  value_thunk_func_obj->header.u.external_handler_cb = ext_func_obj;

  value_thunk_func_obj->value = ecma_copy_value_if_not_object (arg);

  /* 9. */
  ecma_value_t value_thunk = ecma_make_object_value (value_thunk_func_p);
  ecma_value_t ret_value = ecma_op_invoke_by_magic_id (promise, LIT_MAGIC_STRING_THEN, &value_thunk, 1);

  ecma_free_value (promise);
  ecma_deref_object (value_thunk_func_p);

  return ret_value;
} /* ecma_promise_than_catch_finally_helper */

/**
 * Definition of Then Finally Function
 *
 * See also:
 *         ES2020 25.6.5.3.1
 *
 * @return ecma value
 */
ecma_value_t
ecma_promise_then_finally_cb (const ecma_value_t function_obj, /**< the function itself */
                              const ecma_value_t this_val, /**< this_arg of the function */
                              const ecma_value_t args_p[], /**< argument list */
                              const uint32_t args_count) /**< argument number */
{
  JERRY_UNUSED_2 (this_val, args_count);
  JERRY_ASSERT (args_count > 0);

  return ecma_promise_than_catch_finally_helper (function_obj, ecma_value_thunk_helper_cb, args_p[0]);
} /* ecma_promise_then_finally_cb */

/**
 * Definition of Catch Finally Function
 *
 * See also:
 *         ES2020 25.6.5.3.2
 *
 * @return ecma value
 */
ecma_value_t
ecma_promise_catch_finally_cb (const ecma_value_t function_obj, /**< the function itself */
                               const ecma_value_t this_val, /**< this_arg of the function */
                               const ecma_value_t args_p[], /**< argument list */
                               const uint32_t args_count) /**< argument number */
{
  JERRY_UNUSED_2 (this_val, args_count);
  JERRY_ASSERT (args_count > 0);