Implement Promise.allsettled (#4616)
Also i updated the promise race and all method to the latest standart The two jerry/es.next test-cases has been update to support the latest standart JerryScript-DCO-1.0-Signed-off-by: bence gabor kis kisbg@inf.u-szeged.hu
This commit is contained in:
@@ -2120,6 +2120,16 @@ typedef struct
|
||||
* real index + 1 in the [[Values]] list - otherwise */
|
||||
} ecma_promise_all_executor_t;
|
||||
|
||||
/**
|
||||
* Promise prototype methods helper.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
ECMA_PROMISE_ALL_RESOLVE, /**< promise.all resolve */
|
||||
ECMA_PROMISE_ALLSETTLED_RESOLVE, /**< promise.allSettled resolve */
|
||||
ECMA_PROMISE_ALLSETTLED_REJECT, /**< promise.allSettled reject */
|
||||
} ecma_promise_helper;
|
||||
|
||||
#endif /* JERRY_ESNEXT */
|
||||
|
||||
#if JERRY_BUILTIN_DATAVIEW
|
||||
|
||||
@@ -17,7 +17,7 @@ ECMA_NATIVE_HANDLER (ECMA_NATIVE_HANDLER_PROMISE_RESOLVE, ecma_promise_resolve_h
|
||||
ECMA_NATIVE_HANDLER (ECMA_NATIVE_HANDLER_PROMISE_REJECT, ecma_promise_reject_handler, 1)
|
||||
ECMA_NATIVE_HANDLER (ECMA_NATIVE_HANDLER_PROMISE_THEN_FINALLY, ecma_promise_then_finally_cb, 1)
|
||||
ECMA_NATIVE_HANDLER (ECMA_NATIVE_HANDLER_PROMISE_CATCH_FINALLY, ecma_promise_catch_finally_cb, 1)
|
||||
ECMA_NATIVE_HANDLER (ECMA_NATIVE_HANDLER_PROMISE_ALL_HELPER, ecma_promise_all_handler_cb, 1)
|
||||
ECMA_NATIVE_HANDLER (ECMA_NATIVE_HANDLER_PROMISE_ALL_HELPER, ecma_promise_all_or_all_settled_handler_cb, 1)
|
||||
ECMA_NATIVE_HANDLER (ECMA_NATIVE_HANDLER_PROMISE_CAPABILITY_EXECUTOR, ecma_op_get_capabilities_executor_cb, 2)
|
||||
#if JERRY_BUILTIN_PROXY
|
||||
ECMA_NATIVE_HANDLER (ECMA_NATIVE_HANDLER_PROXY_REVOKE, ecma_proxy_revoke_cb, 0)
|
||||
|
||||
@@ -46,6 +46,7 @@ enum
|
||||
ECMA_PROMISE_ROUTINE_RESOLVE,
|
||||
ECMA_PROMISE_ROUTINE_RACE,
|
||||
ECMA_PROMISE_ROUTINE_ALL,
|
||||
ECMA_PROMISE_ROUTINE_ALLSETTLED,
|
||||
ECMA_PROMISE_ROUTINE_SPECIES_GET
|
||||
};
|
||||
|
||||
@@ -116,6 +117,7 @@ ecma_builtin_promise_perform_race (ecma_value_t iterator, /**< the iterator for
|
||||
ecma_value_t next_method, /**< next method */
|
||||
ecma_object_t *capability_obj_p, /**< PromiseCapability record */
|
||||
ecma_value_t ctor, /**< Constructor value */
|
||||
ecma_value_t resolve, /** the resolve of Promise.all */
|
||||
bool *done_p) /**< [out] iteratorRecord[[done]] */
|
||||
{
|
||||
JERRY_ASSERT (ecma_is_value_object (iterator));
|
||||
@@ -124,21 +126,6 @@ ecma_builtin_promise_perform_race (ecma_value_t iterator, /**< the iterator for
|
||||
|
||||
ecma_promise_capabality_t *capability_p = (ecma_promise_capabality_t *) capability_obj_p;
|
||||
|
||||
/* 3. */
|
||||
ecma_value_t resolve = ecma_op_object_get_by_magic_id (ecma_get_object_from_value (ctor), LIT_MAGIC_STRING_RESOLVE);
|
||||
|
||||
if (ECMA_IS_VALUE_ERROR (resolve))
|
||||
{
|
||||
return resolve;
|
||||
}
|
||||
|
||||
/* 4. */
|
||||
if (!ecma_op_is_callable (resolve))
|
||||
{
|
||||
ecma_free_value (resolve);
|
||||
return ecma_raise_type_error (ECMA_ERR_MSG ("Resolve method must be callable"));
|
||||
}
|
||||
|
||||
ecma_object_t *resolve_func_p = ecma_get_object_from_value (resolve);
|
||||
ecma_value_t ret_value = ECMA_VALUE_ERROR;
|
||||
|
||||
@@ -196,12 +183,11 @@ ecma_builtin_promise_perform_race (ecma_value_t iterator, /**< the iterator for
|
||||
done:
|
||||
*done_p = true;
|
||||
exit:
|
||||
ecma_deref_object (resolve_func_p);
|
||||
return ret_value;
|
||||
} /* ecma_builtin_promise_perform_race */
|
||||
|
||||
/**
|
||||
* Runtime Semantics: PerformPromiseAll.
|
||||
* Runtime Semantics: PerformPromise all or allSettled.
|
||||
*
|
||||
* See also:
|
||||
* ES2020 25.6.4.1.1
|
||||
@@ -210,11 +196,14 @@ exit:
|
||||
* Returned value must be freed with ecma_free_value.
|
||||
*/
|
||||
static inline ecma_value_t
|
||||
ecma_builtin_promise_perform_all (ecma_value_t iterator, /**< iteratorRecord */
|
||||
ecma_value_t next_method, /**< next method */
|
||||
ecma_object_t *capability_obj_p, /**< PromiseCapability record */
|
||||
ecma_value_t ctor, /**< the caller of Promise.race */
|
||||
bool *done_p) /**< [out] iteratorRecord[[done]] */
|
||||
ecma_builtin_promise_perform_all_or_all_settled (ecma_value_t iterator, /**< iteratorRecord */
|
||||
ecma_value_t next_method, /**< next method */
|
||||
ecma_object_t *capability_obj_p, /**< PromiseCapability record */
|
||||
ecma_value_t ctor, /**< the caller of Promise.all */
|
||||
ecma_value_t resolve, /** the resolve of Promise.all */
|
||||
uint8_t builtin_routine_id, /**< built-in wide routine
|
||||
* identifier */
|
||||
bool *done_p) /**< [out] iteratorRecord[[done]] */
|
||||
{
|
||||
/* 1. - 2. */
|
||||
JERRY_ASSERT (ecma_object_class_is (capability_obj_p, LIT_INTERNAL_MAGIC_PROMISE_CAPABILITY));
|
||||
@@ -222,20 +211,6 @@ ecma_builtin_promise_perform_all (ecma_value_t iterator, /**< iteratorRecord */
|
||||
|
||||
ecma_promise_capabality_t *capability_p = (ecma_promise_capabality_t *) capability_obj_p;
|
||||
|
||||
ecma_value_t resolve = ecma_op_object_get_by_magic_id (ecma_get_object_from_value (ctor),
|
||||
LIT_MAGIC_STRING_RESOLVE);
|
||||
|
||||
if (ECMA_IS_VALUE_ERROR (resolve))
|
||||
{
|
||||
return resolve;
|
||||
}
|
||||
|
||||
if (!ecma_op_is_callable (resolve))
|
||||
{
|
||||
ecma_free_value (resolve);
|
||||
return ecma_raise_type_error (ECMA_ERR_MSG ("Resolve method must be callable"));
|
||||
}
|
||||
|
||||
ecma_object_t *resolve_func_p = ecma_get_object_from_value (resolve);
|
||||
|
||||
/* 3. */
|
||||
@@ -333,17 +308,43 @@ ecma_builtin_promise_perform_all (ecma_value_t iterator, /**< iteratorRecord */
|
||||
|
||||
/* p. */
|
||||
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));
|
||||
|
||||
/* r. */
|
||||
ecma_value_t args[2];
|
||||
args[0] = ecma_make_object_value (executor_func_p);
|
||||
args[1] = capability_p->reject;
|
||||
ecma_value_t 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_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))
|
||||
@@ -359,21 +360,20 @@ done:
|
||||
exit:
|
||||
ecma_free_value (remaining);
|
||||
ecma_deref_object (values_array_obj_p);
|
||||
ecma_deref_object (resolve_func_p);
|
||||
|
||||
return ret_value;
|
||||
} /* ecma_builtin_promise_perform_all */
|
||||
} /* ecma_builtin_promise_perform_all_or_all_settled */
|
||||
|
||||
/**
|
||||
* The common function for both Promise.race and Promise.all.
|
||||
* The common function for Promise.race, Promise.all and Promise.allSettled.
|
||||
*
|
||||
* @return ecma value of the new promise.
|
||||
* Returned value must be freed with ecma_free_value.
|
||||
*/
|
||||
static ecma_value_t
|
||||
ecma_builtin_promise_race_or_all (ecma_value_t this_arg, /**< 'this' argument */
|
||||
ecma_value_t iterable, /**< the items to be resolved */
|
||||
bool is_race) /**< indicates whether it is race function */
|
||||
ecma_builtin_promise_helper (ecma_value_t this_arg, /**< 'this' argument */
|
||||
ecma_value_t iterable, /**< the items to be resolved */
|
||||
uint8_t builtin_routine_id) /**< built-in wide routine
|
||||
* identifier */
|
||||
{
|
||||
ecma_object_t *capability_obj_p = ecma_promise_new_capability (this_arg, ECMA_VALUE_UNDEFINED);
|
||||
|
||||
@@ -382,6 +382,25 @@ ecma_builtin_promise_race_or_all (ecma_value_t this_arg, /**< 'this' argument */
|
||||
return ECMA_VALUE_ERROR;
|
||||
}
|
||||
|
||||
ecma_value_t resolve = ecma_op_object_get_by_magic_id (ecma_get_object_from_value (this_arg),
|
||||
LIT_MAGIC_STRING_RESOLVE);
|
||||
|
||||
if (ECMA_IS_VALUE_ERROR (resolve))
|
||||
{
|
||||
resolve = ecma_builtin_promise_reject_abrupt (resolve, capability_obj_p);
|
||||
ecma_deref_object (capability_obj_p);
|
||||
return resolve;
|
||||
}
|
||||
|
||||
if (!ecma_op_is_callable (resolve))
|
||||
{
|
||||
ecma_free_value (resolve);
|
||||
ecma_raise_type_error (ECMA_ERR_MSG ("Resolve method must be callable"));
|
||||
resolve = ecma_builtin_promise_reject_abrupt (ECMA_VALUE_ERROR, capability_obj_p);
|
||||
ecma_deref_object (capability_obj_p);
|
||||
return resolve;
|
||||
}
|
||||
|
||||
ecma_value_t next_method;
|
||||
ecma_value_t iterator = ecma_op_get_iterator (iterable, ECMA_VALUE_SYNC_ITERATOR, &next_method);
|
||||
iterator = ecma_builtin_promise_reject_abrupt (iterator, capability_obj_p);
|
||||
@@ -395,13 +414,14 @@ ecma_builtin_promise_race_or_all (ecma_value_t this_arg, /**< 'this' argument */
|
||||
ecma_value_t ret = ECMA_VALUE_EMPTY;
|
||||
bool is_done = false;
|
||||
|
||||
if (is_race)
|
||||
if (builtin_routine_id == ECMA_PROMISE_ROUTINE_RACE)
|
||||
{
|
||||
ret = ecma_builtin_promise_perform_race (iterator, next_method, capability_obj_p, this_arg, &is_done);
|
||||
ret = ecma_builtin_promise_perform_race (iterator, next_method, capability_obj_p, this_arg, resolve, &is_done);
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = ecma_builtin_promise_perform_all (iterator, next_method, capability_obj_p, this_arg, &is_done);
|
||||
ret = ecma_builtin_promise_perform_all_or_all_settled (iterator, next_method, capability_obj_p, this_arg, resolve,
|
||||
builtin_routine_id, &is_done);
|
||||
}
|
||||
|
||||
if (ECMA_IS_VALUE_ERROR (ret))
|
||||
@@ -416,10 +436,11 @@ ecma_builtin_promise_race_or_all (ecma_value_t this_arg, /**< 'this' argument */
|
||||
|
||||
ecma_free_value (iterator);
|
||||
ecma_free_value (next_method);
|
||||
ecma_free_value (resolve);
|
||||
ecma_deref_object (capability_obj_p);
|
||||
|
||||
return ret;
|
||||
} /* ecma_builtin_promise_race_or_all */
|
||||
} /* ecma_builtin_promise_helper */
|
||||
|
||||
/**
|
||||
* Handle calling [[Call]] of built-in Promise object.
|
||||
@@ -486,9 +507,9 @@ ecma_builtin_promise_dispatch_routine (uint8_t builtin_routine_id, /**< built-in
|
||||
}
|
||||
case ECMA_PROMISE_ROUTINE_RACE:
|
||||
case ECMA_PROMISE_ROUTINE_ALL:
|
||||
case ECMA_PROMISE_ROUTINE_ALLSETTLED:
|
||||
{
|
||||
bool is_race = (builtin_routine_id == ECMA_PROMISE_ROUTINE_RACE);
|
||||
return ecma_builtin_promise_race_or_all (this_arg, argument, is_race);
|
||||
return ecma_builtin_promise_helper (this_arg, argument, builtin_routine_id);
|
||||
}
|
||||
case ECMA_PROMISE_ROUTINE_SPECIES_GET:
|
||||
{
|
||||
|
||||
@@ -45,6 +45,7 @@ ROUTINE (LIT_MAGIC_STRING_REJECT, ECMA_PROMISE_ROUTINE_REJECT, 1, 1)
|
||||
ROUTINE (LIT_MAGIC_STRING_RESOLVE, ECMA_PROMISE_ROUTINE_RESOLVE, 1, 1)
|
||||
ROUTINE (LIT_MAGIC_STRING_RACE, ECMA_PROMISE_ROUTINE_RACE, 1, 1)
|
||||
ROUTINE (LIT_MAGIC_STRING_ALL, ECMA_PROMISE_ROUTINE_ALL, 1, 1)
|
||||
ROUTINE (LIT_MAGIC_STRING_ALLSETTLED, ECMA_PROMISE_ROUTINE_ALLSETTLED, 1, 1)
|
||||
|
||||
/* ES2015 25.4.4.6 */
|
||||
ACCESSOR_READ_ONLY (LIT_GLOBAL_SYMBOL_SPECIES,
|
||||
|
||||
@@ -609,7 +609,7 @@ ecma_promise_remaining_inc_or_dec (ecma_value_t remaining, /**< the remaining co
|
||||
} /* ecma_promise_remaining_inc_or_dec */
|
||||
|
||||
/**
|
||||
* Native handler for Promise.all Resolve Element Function.
|
||||
* Native handler for Promise.all and Promise.allSettled Resolve Element Function.
|
||||
*
|
||||
* See also:
|
||||
* ES2015 25.4.4.1.2
|
||||
@@ -617,12 +617,13 @@ ecma_promise_remaining_inc_or_dec (ecma_value_t remaining, /**< the remaining co
|
||||
* @return ecma value of undefined.
|
||||
*/
|
||||
ecma_value_t
|
||||
ecma_promise_all_handler_cb (ecma_object_t *function_obj_p, /**< function object */
|
||||
ecma_promise_all_or_all_settled_handler_cb (ecma_object_t *function_obj_p, /**< function object */
|
||||
const ecma_value_t args_p[], /**< argument list */
|
||||
const uint32_t args_count) /**< argument number */
|
||||
{
|
||||
JERRY_UNUSED (args_count);
|
||||
ecma_promise_all_executor_t *executor_p = (ecma_promise_all_executor_t *) function_obj_p;
|
||||
uint16_t promise_type = executor_p->header.u.class_prop.extra_info;
|
||||
|
||||
/* 1 - 2. */
|
||||
if (executor_p->index == 0)
|
||||
@@ -630,11 +631,53 @@ ecma_promise_all_handler_cb (ecma_object_t *function_obj_p, /**< function object
|
||||
return ECMA_VALUE_UNDEFINED;
|
||||
}
|
||||
|
||||
/* 8. */
|
||||
ecma_op_object_put_by_index (ecma_get_object_from_value (executor_p->values),
|
||||
(uint32_t) (executor_p->index - 1),
|
||||
args_p[0],
|
||||
false);
|
||||
if (promise_type == ECMA_PROMISE_ALL_RESOLVE)
|
||||
{
|
||||
/* 8. */
|
||||
ecma_op_object_put_by_index (ecma_get_object_from_value (executor_p->values),
|
||||
(uint32_t) (executor_p->index - 1),
|
||||
args_p[0],
|
||||
false);
|
||||
}
|
||||
else
|
||||
{
|
||||
lit_magic_string_id_t status_property_val = LIT_MAGIC_STRING_REJECTED;
|
||||
lit_magic_string_id_t data_propery_name = LIT_MAGIC_STRING_REASON;
|
||||
|
||||
if (promise_type == ECMA_PROMISE_ALLSETTLED_RESOLVE)
|
||||
{
|
||||
status_property_val = LIT_MAGIC_STRING_FULFILLED;
|
||||
data_propery_name = LIT_MAGIC_STRING_VALUE;
|
||||
}
|
||||
|
||||
ecma_object_t *obj_p = ecma_create_object (NULL, 0, ECMA_OBJECT_TYPE_GENERAL);
|
||||
ecma_property_value_t *prop_value_p;
|
||||
prop_value_p = ecma_create_named_data_property (obj_p,
|
||||
ecma_get_magic_string (LIT_MAGIC_STRING_STATUS),
|
||||
ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE,
|
||||
NULL);
|
||||
|
||||
prop_value_p->value = ecma_make_magic_string_value (status_property_val);
|
||||
|
||||
prop_value_p = ecma_create_named_data_property (obj_p,
|
||||
ecma_get_magic_string (data_propery_name),
|
||||
ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE,
|
||||
NULL);
|
||||
prop_value_p->value = ECMA_VALUE_UNDEFINED;
|
||||
|
||||
if (args_count != 0)
|
||||
{
|
||||
prop_value_p->value = ecma_copy_value_if_not_object (args_p[0]);
|
||||
}
|
||||
|
||||
ecma_value_t obj_val = ecma_make_object_value (obj_p);
|
||||
/* 12. */
|
||||
ecma_op_object_put_by_index (ecma_get_object_from_value (executor_p->values),
|
||||
(uint32_t) (executor_p->index - 1),
|
||||
obj_val,
|
||||
false);
|
||||
ecma_deref_object (obj_p);
|
||||
}
|
||||
/* 3. */
|
||||
executor_p->index = 0;
|
||||
|
||||
@@ -651,7 +694,7 @@ ecma_promise_all_handler_cb (ecma_object_t *function_obj_p, /**< function object
|
||||
}
|
||||
|
||||
return ret;
|
||||
} /* ecma_promise_all_handler_cb */
|
||||
} /* ecma_promise_all_or_all_settled_handler_cb */
|
||||
|
||||
/**
|
||||
* GetCapabilitiesExecutor Functions
|
||||
|
||||
@@ -120,8 +120,8 @@ ecma_value_t ecma_promise_reject_handler (ecma_object_t *function_obj_p,
|
||||
const ecma_value_t argv[], const uint32_t args_count);
|
||||
ecma_value_t ecma_promise_resolve_handler (ecma_object_t *function_obj_p,
|
||||
const ecma_value_t argv[], const uint32_t args_count);
|
||||
ecma_value_t ecma_promise_all_handler_cb (ecma_object_t *function_obj_p,
|
||||
const ecma_value_t args_p[], const uint32_t args_count);
|
||||
ecma_value_t ecma_promise_all_or_all_settled_handler_cb (ecma_object_t *function_obj_p,
|
||||
const ecma_value_t args_p[], const uint32_t args_count);
|
||||
ecma_value_t ecma_op_get_capabilities_executor_cb (ecma_object_t *function_obj_p,
|
||||
const ecma_value_t args_p[], const uint32_t args_count);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user