Implement Promise.any and AggregateError Object (#4623)

JerryScript-DCO-1.0-Signed-off-by: Bence Gabor Kis kisbg@inf.u-szeged.hu
This commit is contained in:
kisbg
2021-03-22 12:42:20 +01:00
committed by GitHub
parent edd2f20397
commit 22ebb00a4c
25 changed files with 744 additions and 174 deletions
@@ -0,0 +1,36 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecma-alloc.h"
#include "ecma-builtins.h"
#include "ecma-conversion.h"
#include "ecma-exceptions.h"
#include "ecma-gc.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-objects.h"
#include "ecma-string-object.h"
#include "jrt.h"
#if JERRY_BUILTIN_PROMISE
#define ECMA_BUILTINS_INTERNAL
#include "ecma-builtins-internal.h"
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-aggregateerror-prototype.inc.h"
#define BUILTIN_UNDERSCORED_ID aggregate_error_prototype
#include "ecma-builtin-internal-routines-template.inc.h"
#endif /* JERRY_BUILTIN_PROMISE */
@@ -0,0 +1,44 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* AggregateError.prototype built-in description
*/
#include "ecma-builtin-helpers-macro-defines.inc.h"
#if JERRY_BUILTIN_PROMISE
/* Object properties:
* (property name, object pointer getter) */
/* ECMA-262 v5, 15.11.7.8 */
OBJECT_VALUE (LIT_MAGIC_STRING_CONSTRUCTOR,
ECMA_BUILTIN_ID_AGGREGATE_ERROR,
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
/* ECMA-262 v5, 15.11.7.9 */
STRING_VALUE (LIT_MAGIC_STRING_NAME,
LIT_MAGIC_STRING_AGGREGATE_ERROR_UL,
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
/* ECMA-262 v5, 15.11.7.10 */
STRING_VALUE (LIT_MAGIC_STRING_MESSAGE,
LIT_MAGIC_STRING__EMPTY,
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
#endif /* JERRY_BUILTIN_PROMISE */
#include "ecma-builtin-helpers-macro-undefs.inc.h"
@@ -0,0 +1,112 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecma-alloc.h"
#include "ecma-builtins.h"
#include "ecma-conversion.h"
#include "ecma-exceptions.h"
#include "ecma-gc.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-builtin-helpers.h"
#include "ecma-objects.h"
#include "ecma-function-object.h"
#include "ecma-iterator-object.h"
#include "ecma-array-object.h"
#include "jrt.h"
#include "jcontext.h"
#if JERRY_BUILTIN_PROMISE
#define ECMA_BUILTINS_INTERNAL
#include "ecma-builtins-internal.h"
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-aggregateerror.inc.h"
#define BUILTIN_UNDERSCORED_ID aggregate_error
#include "ecma-builtin-internal-routines-template.inc.h"
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmabuiltins
* @{
*
* \addtogroup aggregateerror ECMA AggregateError object built-in
* @{
*/
/**
* Handle calling [[Call]] of built-in AggregateError object
*
* @return ecma value
*/
ecma_value_t
ecma_builtin_aggregate_error_dispatch_call (const ecma_value_t *arguments_list_p, /**< arguments list */
uint32_t arguments_list_len) /**< number of arguments */
{
JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);
ecma_value_t message_val = ECMA_VALUE_UNDEFINED;
ecma_value_t error_val = ECMA_VALUE_UNDEFINED;
if (arguments_list_len > 0)
{
error_val = arguments_list_p[0];
if (arguments_list_len > 1)
{
message_val = arguments_list_p[1];
}
}
return ecma_new_aggregate_error (error_val, message_val);
} /* ecma_builtin_aggregate_error_dispatch_call */
/**
* Handle calling [[Construct]] of built-in AggregateError object
*
* @return ecma value
*/
ecma_value_t
ecma_builtin_aggregate_error_dispatch_construct (const ecma_value_t *arguments_list_p, /**< arguments list */
uint32_t arguments_list_len) /**< number of arguments */
{
ecma_object_t *proto_p = ecma_op_get_prototype_from_constructor (JERRY_CONTEXT (current_new_target_p),
ECMA_BUILTIN_ID_AGGREGATE_ERROR_PROTOTYPE);
if (proto_p == NULL)
{
return ECMA_VALUE_ERROR;
}
ecma_value_t result = ecma_builtin_aggregate_error_dispatch_call (arguments_list_p, arguments_list_len);
if (!ECMA_IS_VALUE_ERROR (result))
{
ecma_object_t *object_p = ecma_get_object_from_value (result);
ECMA_SET_NON_NULL_POINTER (object_p->u2.prototype_cp, proto_p);
}
ecma_deref_object (proto_p);
return result;
} /* ecma_builtin_aggregate_error_dispatch_construct */
/**
* @}
* @}
* @}
*/
#endif /* JERRY_BUILTIN_PROMISE */
@@ -0,0 +1,45 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* AggregateError built-in description
*/
#include "ecma-builtin-helpers-macro-defines.inc.h"
#if JERRY_BUILTIN_PROMISE
/* Number properties:
* (property name, number value, writable, enumerable, configurable) */
NUMBER_VALUE (LIT_MAGIC_STRING_LENGTH,
2,
ECMA_PROPERTY_FLAG_DEFAULT_LENGTH)
/* Object properties:
* (property name, object pointer getter) */
/* ECMA-262 v5, 15.11.3.1 */
OBJECT_VALUE (LIT_MAGIC_STRING_PROTOTYPE,
ECMA_BUILTIN_ID_AGGREGATE_ERROR_PROTOTYPE,
ECMA_PROPERTY_FIXED)
STRING_VALUE (LIT_MAGIC_STRING_NAME,
LIT_MAGIC_STRING_AGGREGATE_ERROR_UL,
ECMA_PROPERTY_FLAG_CONFIGURABLE)
#endif /* JERRY_BUILTIN_PROMISE */
#include "ecma-builtin-helpers-macro-undefs.inc.h"
@@ -127,6 +127,12 @@ OBJECT_VALUE (LIT_MAGIC_STRING_TYPE_ERROR_UL,
ECMA_BUILTIN_ID_TYPE_ERROR,
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
#if JERRY_BUILTIN_PROMISE
OBJECT_VALUE (LIT_MAGIC_STRING_AGGREGATE_ERROR_UL,
ECMA_BUILTIN_ID_AGGREGATE_ERROR,
ECMA_PROPERTY_CONFIGURABLE_WRITABLE)
#endif /* JERRY_BUILTIN_PROMISE */
/* ECMA-262 v5, 15.1.4.15 */
OBJECT_VALUE (LIT_MAGIC_STRING_URI_ERROR_UL,
ECMA_BUILTIN_ID_URI_ERROR,
@@ -47,6 +47,7 @@ enum
ECMA_PROMISE_ROUTINE_RACE,
ECMA_PROMISE_ROUTINE_ALL,
ECMA_PROMISE_ROUTINE_ALLSETTLED,
ECMA_PROMISE_ROUTINE_ANY,
ECMA_PROMISE_ROUTINE_SPECIES_GET
};
@@ -187,7 +188,7 @@ exit:
} /* ecma_builtin_promise_perform_race */
/**
* Runtime Semantics: PerformPromise all or allSettled.
* Runtime Semantics: Perform Promise all, allSettled or any.
*
* See also:
* ES2020 25.6.4.1.1
@@ -196,14 +197,13 @@ exit:
* Returned value must be freed with ecma_free_value.
*/
static inline ecma_value_t
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]] */
ecma_builtin_promise_perform (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));
@@ -240,6 +240,12 @@ ecma_builtin_promise_perform_all_or_all_settled (ecma_value_t iterator, /**< ite
/* ii. - iii. */
if (ecma_promise_remaining_inc_or_dec (remaining, false) == 0)
{
if (builtin_routine_id == ECMA_PROMISE_ROUTINE_ANY)
{
ret_value = ecma_raise_aggregate_error (values_array, ECMA_VALUE_UNDEFINED);
goto done;
}
/* 2. */
ecma_value_t resolve_result = ecma_op_function_call (ecma_get_object_from_value (capability_p->resolve),
ECMA_VALUE_UNDEFINED,
@@ -291,38 +297,56 @@ ecma_builtin_promise_perform_all_or_all_settled (ecma_value_t iterator, /**< ite
goto exit;
}
/* k. */
ecma_object_t *executor_func_p = ecma_op_create_native_handler (ECMA_NATIVE_HANDLER_PROMISE_ALL_HELPER,
sizeof (ecma_promise_all_executor_t));
ecma_promise_all_executor_t *executor_p = (ecma_promise_all_executor_t *) executor_func_p;
/* m. + t. */
executor_p->index = ++idx;
/* n. */
executor_p->values = values_array;
/* o. */
executor_p->capability = ecma_make_object_value (capability_obj_p);
/* 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;
}
idx++;
ecma_value_t args[2];
args[0] = ecma_make_object_value (executor_func_p);
ecma_object_t *executor_func_p = NULL;
if (builtin_routine_id != ECMA_PROMISE_ROUTINE_ANY)
{
/* k. */
executor_func_p = ecma_op_create_native_handler (ECMA_NATIVE_HANDLER_PROMISE_ALL_HELPER,
sizeof (ecma_promise_all_executor_t));
ecma_promise_all_executor_t *executor_p = (ecma_promise_all_executor_t *) executor_func_p;
/* m. + t. */
executor_p->index = idx;
/* n. */
executor_p->values = values_array;
/* o. */
executor_p->capability = ecma_make_object_value (capability_obj_p);
/* 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;
}
args[0] = ecma_make_object_value (executor_func_p);
}
else
{
args[0] = capability_p->resolve;
}
/* q. */
ecma_promise_remaining_inc_or_dec (remaining, true);
ecma_value_t result;
if (builtin_routine_id == ECMA_PROMISE_ROUTINE_ALLSETTLED)
if (builtin_routine_id != ECMA_PROMISE_ROUTINE_ALL)
{
ecma_promise_all_exector_type_t type = ECMA_PROMISE_ALLSETTLED_REJECT;
if (builtin_routine_id == ECMA_PROMISE_ROUTINE_ANY)
{
type = ECMA_PROMISE_ANY_REJECT;
}
ecma_object_t *reject_func_p = ecma_op_create_native_handler (ECMA_NATIVE_HANDLER_PROMISE_ALL_HELPER,
sizeof (ecma_promise_all_executor_t));
@@ -331,18 +355,21 @@ ecma_builtin_promise_perform_all_or_all_settled (ecma_value_t iterator, /**< ite
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;
reject_p->header.u.class_prop.extra_info = (uint16_t) type;
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_free_value (next_promise);
if (builtin_routine_id != ECMA_PROMISE_ROUTINE_ANY)
{
ecma_deref_object (executor_func_p);
}
@@ -361,10 +388,10 @@ exit:
ecma_free_value (remaining);
ecma_deref_object (values_array_obj_p);
return ret_value;
} /* ecma_builtin_promise_perform_all_or_all_settled */
} /* ecma_builtin_promise_perform */
/**
* The common function for Promise.race, Promise.all and Promise.allSettled.
* The common function for Promise.race, Promise.all, Promise.any and Promise.allSettled.
*
* @return ecma value of the new promise.
* Returned value must be freed with ecma_free_value.
@@ -372,8 +399,7 @@ exit:
static ecma_value_t
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 */
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);
@@ -420,8 +446,13 @@ ecma_builtin_promise_helper (ecma_value_t this_arg, /**< 'this' argument */
}
else
{
ret = ecma_builtin_promise_perform_all_or_all_settled (iterator, next_method, capability_obj_p, this_arg, resolve,
builtin_routine_id, &is_done);
ret = ecma_builtin_promise_perform (iterator,
next_method,
capability_obj_p,
this_arg,
resolve,
builtin_routine_id,
&is_done);
}
if (ECMA_IS_VALUE_ERROR (ret))
@@ -508,6 +539,7 @@ 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:
case ECMA_PROMISE_ROUTINE_ANY:
{
return ecma_builtin_promise_helper (this_arg, argument, builtin_routine_id);
}
@@ -46,6 +46,7 @@ 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)
ROUTINE (LIT_MAGIC_STRING_ANY, ECMA_PROMISE_ROUTINE_ANY, 1, 1)
/* ES2015 25.4.4.6 */
ACCESSOR_READ_ONLY (LIT_GLOBAL_SYMBOL_SPECIES,
@@ -283,6 +283,22 @@ BUILTIN_ROUTINE (ECMA_BUILTIN_ID_TYPE_ERROR,
true,
type_error)
#if JERRY_BUILTIN_PROMISE
/* The AggregateError.prototype object (15.11.6.5) */
BUILTIN (ECMA_BUILTIN_ID_AGGREGATE_ERROR_PROTOTYPE,
ECMA_OBJECT_TYPE_GENERAL,
ECMA_BUILTIN_ID_ERROR_PROTOTYPE,
true,
aggregate_error_prototype)
/* The AggregateError object (15.11.6.5) */
BUILTIN_ROUTINE (ECMA_BUILTIN_ID_AGGREGATE_ERROR,
ECMA_OBJECT_TYPE_NATIVE_FUNCTION,
ECMA_BUILTIN_NATIVE_ERROR_PROTOTYPE_ID,
true,
aggregate_error)
#endif /* JERRY_BUILTIN_PROMISE */
/* The URIError.prototype object (15.11.6.6) */
BUILTIN (ECMA_BUILTIN_ID_URI_ERROR_PROTOTYPE,
ECMA_OBJECT_TYPE_GENERAL,