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

Code before:
    executor_p->header.u.class_prop.extra_info = ECMA_PROMISE_ALL_RESOLVE;

    if (builtin_routine_id == ECMA_PROMISE_ROUTINE_ALLSETTLED)
    {
      executor_p->header.u.class_prop.extra_info = ECMA_PROMISE_ALLSETTLED_RESOLVE;
    }

    ecma_value_t args[2];
    args[0] = ecma_make_object_value (executor_func_p);

    /* q. */
    ecma_promise_remaining_inc_or_dec (remaining, true);
    ecma_value_t result;

    if (builtin_routine_id == ECMA_PROMISE_ROUTINE_ALLSETTLED)
    {
      ecma_object_t *reject_func_p = ecma_op_create_native_handler (ECMA_NATIVE_HANDLER_PROMISE_ALL_HELPER,
                                                                    sizeof (ecma_promise_all_executor_t));

      ecma_promise_all_executor_t *reject_p = (ecma_promise_all_executor_t *) reject_func_p;
      reject_p->index = idx;
      reject_p->values = values_array;
      reject_p->capability = ecma_make_object_value (capability_obj_p);
      reject_p->remaining_elements = remaining;
      reject_p->header.u.class_prop.extra_info = ECMA_PROMISE_ALLSETTLED_REJECT;
      args[1] = ecma_make_object_value (reject_func_p);
      result = ecma_op_invoke_by_magic_id (next_promise, LIT_MAGIC_STRING_THEN, args, 2);
      ecma_deref_object (reject_func_p);
    }
    else
    {
      args[1] = capability_p->reject;
      result = ecma_op_invoke_by_magic_id (next_promise, LIT_MAGIC_STRING_THEN, args, 2);
    }

    ecma_free_value (next_promise);
    ecma_deref_object (executor_func_p);

    /* s. */
    if (ECMA_IS_VALUE_ERROR (result))
    {
      goto exit;
    }

    ecma_free_value (result);
  }

done:
  *done_p = true;
exit:
  ecma_free_value (remaining);

Review comment (suggestion):
Suggestion: to reduce memory consumption, we could pass the same function for resolve and reject, and when the function is called, we can check the internal type of the Promise to determine which one is called.

Code after:
    executor_p->remaining_elements = remaining;
    executor_p->header.u.class_prop.extra_info = ECMA_PROMISE_ALL_RESOLVE;

    if (builtin_routine_id == ECMA_PROMISE_ROUTINE_ALLSETTLED)
    {
      executor_p->header.u.class_prop.extra_info = ECMA_PROMISE_ALLSETTLED_RESOLVE;
    }

    ecma_value_t args[2];
    args[0] = ecma_make_object_value (executor_func_p);

    /* q. */
    ecma_promise_remaining_inc_or_dec (remaining, true);
    ecma_value_t result;
    if (builtin_routine_id == ECMA_PROMISE_ROUTINE_ALLSETTLED)
    {
      ecma_object_t *reject_func_p = ecma_op_create_native_handler (ECMA_NATIVE_HANDLER_PROMISE_ALL_HELPER,
                                                     sizeof (ecma_promise_all_executor_t));

      ecma_promise_all_executor_t *reject_p = (ecma_promise_all_executor_t *) reject_func_p;
      reject_p->index = idx;
      reject_p->values = values_array;
      reject_p->capability = ecma_make_object_value (capability_obj_p);
      reject_p->remaining_elements = remaining;
      reject_p->header.u.class_prop.extra_info = ECMA_PROMISE_ALLSETTLED_REJECT;
      args[1] = ecma_make_object_value (reject_func_p);
      result = ecma_op_invoke_by_magic_id (next_promise, LIT_MAGIC_STRING_THEN, args, 2);
      ecma_free_value (next_promise);
      ecma_deref_object (executor_func_p);
      ecma_deref_object (reject_func_p);
    }
    else
    {
      args[1] = capability_p->reject;
      result = ecma_op_invoke_by_magic_id (next_promise, LIT_MAGIC_STRING_THEN, args, 2);
      ecma_free_value (next_promise);
      ecma_deref_object (executor_func_p);
    }

    /* s. */
    if (ECMA_IS_VALUE_ERROR (result))
    {
      goto exit;
    }

    ecma_free_value (result);
  }

done:
  *done_p = true;
exit: