Implement async generators (#3916)

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2020-06-22 13:54:18 +02:00
committed by GitHub
parent 0f0041d720
commit b2a6109430
32 changed files with 1247 additions and 50 deletions
@@ -0,0 +1,74 @@
/* 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-globals.h"
#if ENABLED (JERRY_ESNEXT)
#define ECMA_BUILTINS_INTERNAL
#include "ecma-builtins-internal.h"
#include "ecma-function-object.h"
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-async-generator-function.inc.h"
#define BUILTIN_UNDERSCORED_ID async_generator_function
#include "ecma-builtin-internal-routines-template.inc.h"
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmabuiltins
* @{
*
* \addtogroup asyncgenerator ECMA AsyncGeneratorFunction object built-in
* @{
*/
/**
* Handle calling [[Call]] of built-in AsyncGeneratorFunction object
*
* @return constructed async generator function object - if success
* raised error otherwise
*/
ecma_value_t
ecma_builtin_async_generator_function_dispatch_call (const ecma_value_t *arguments_list_p, /**< arguments list */
ecma_length_t arguments_list_len) /**< number of arguments */
{
JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);
return ecma_op_create_dynamic_function (arguments_list_p,
arguments_list_len,
ECMA_PARSE_GENERATOR_FUNCTION | ECMA_PARSE_ASYNC_FUNCTION);
} /* ecma_builtin_async_generator_function_dispatch_call */
/**
* Handle calling [[Construct]] of built-in AsyncGeneratorFunction object
*
* @return constructed async generator function object - if success
* raised error otherwise
*/
ecma_value_t
ecma_builtin_async_generator_function_dispatch_construct (const ecma_value_t *arguments_list_p, /**< arguments list */
ecma_length_t arguments_list_len) /**< number of arguments */
{
return ecma_builtin_async_generator_function_dispatch_call (arguments_list_p, arguments_list_len);
} /* ecma_builtin_async_generator_function_dispatch_construct */
/**
* @}
* @}
* @}
*/
#endif /* ENABLED (JERRY_ESNEXT) */
@@ -0,0 +1,41 @@
/* 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.
*/
/*
* %AsyncGeneratorFunction% built-in description
*/
#include "ecma-builtin-helpers-macro-defines.inc.h"
#if ENABLED (JERRY_ESNEXT)
/* ECMA-262 v10, 25.3.2 */
STRING_VALUE (LIT_MAGIC_STRING_NAME,
LIT_MAGIC_STRING_ASYNC_GENERATOR_FUNCTION_UL,
ECMA_PROPERTY_FLAG_CONFIGURABLE)
/* ECMA-262 v10, 25.3.2.1 */
NUMBER_VALUE (LIT_MAGIC_STRING_LENGTH,
1,
ECMA_PROPERTY_FLAG_CONFIGURABLE)
/* ECMA-262 v6, 25.3.2.2 */
OBJECT_VALUE (LIT_MAGIC_STRING_PROTOTYPE,
ECMA_BUILTIN_ID_ASYNC_GENERATOR,
ECMA_PROPERTY_FIXED)
#endif /* ENABLED (JERRY_ESNEXT) */
#include "ecma-builtin-helpers-macro-undefs.inc.h"
@@ -0,0 +1,147 @@
/* 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-async-generator-object.h"
#include "ecma-builtins.h"
#include "ecma-exceptions.h"
#include "ecma-gc.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-iterator-object.h"
#include "ecma-promise-object.h"
#include "jcontext.h"
#include "opcodes.h"
#include "vm-defines.h"
#if ENABLED (JERRY_ESNEXT)
#define ECMA_BUILTINS_INTERNAL
#include "ecma-builtins-internal.h"
/**
* This object has a custom dispatch function.
*/
#define BUILTIN_CUSTOM_DISPATCH
/**
* List of built-in routine identifiers.
*/
enum
{
ECMA_ASYNC_GENERATOR_PROTOTYPE_ROUTINE_START = ECMA_BUILTIN_ID__COUNT - 1,
ECMA_ASYNC_GENERATOR_PROTOTYPE_ROUTINE_NEXT,
ECMA_ASYNC_GENERATOR_PROTOTYPE_ROUTINE_THROW,
ECMA_ASYNC_GENERATOR_PROTOTYPE_ROUTINE_RETURN
};
/**
* Convert routine type to operation type..
*/
#define ECMA_ASYNC_GENERATOR_ROUTINE_TO_OPERATION(type) \
((ecma_async_generator_operation_type_t) ((type) - ECMA_ASYNC_GENERATOR_PROTOTYPE_ROUTINE_NEXT))
JERRY_STATIC_ASSERT (ECMA_ASYNC_GENERATOR_ROUTINE_TO_OPERATION (ECMA_ASYNC_GENERATOR_PROTOTYPE_ROUTINE_NEXT)
== ECMA_ASYNC_GENERATOR_DO_NEXT,
convert_ecma_async_generator_routine_next_to_ecma_async_generator_do_next_failed);
JERRY_STATIC_ASSERT (ECMA_ASYNC_GENERATOR_ROUTINE_TO_OPERATION (ECMA_ASYNC_GENERATOR_PROTOTYPE_ROUTINE_THROW)
== ECMA_ASYNC_GENERATOR_DO_THROW,
convert_ecma_async_generator_routine_throw_to_ecma_async_generator_do_throw_failed);
JERRY_STATIC_ASSERT (ECMA_ASYNC_GENERATOR_ROUTINE_TO_OPERATION (ECMA_ASYNC_GENERATOR_PROTOTYPE_ROUTINE_RETURN)
== ECMA_ASYNC_GENERATOR_DO_RETURN,
convert_ecma_async_generator_routine_return_to_ecma_async_generator_do_return_failed);
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-async-generator-prototype.inc.h"
#define BUILTIN_UNDERSCORED_ID async_generator_prototype
#include "ecma-builtin-internal-routines-template.inc.h"
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmabuiltins
* @{
*
* \addtogroup asyncgenerator ECMA AsyncGenerator.prototype object built-in
* @{
*/
/**
* Dispatcher of the Generator built-in's routines
*
* @return ecma value
* Returned value must be freed with ecma_free_value.
*/
ecma_value_t
ecma_builtin_async_generator_prototype_dispatch_routine (uint16_t builtin_routine_id, /**< built-in wide routine
* identifier */
ecma_value_t this_arg, /**< 'this' argument value */
const ecma_value_t arguments_list_p[], /**< list of arguments
* passed to
* routine */
ecma_length_t arguments_number) /**< length of arguments'
* list */
{
JERRY_UNUSED (arguments_number);
vm_executable_object_t *executable_object_p = NULL;
if (ecma_is_value_object (this_arg))
{
ecma_object_t *object_p = ecma_get_object_from_value (this_arg);
if (ecma_get_object_type (object_p) == ECMA_OBJECT_TYPE_CLASS)
{
ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
if (ext_object_p->u.class_prop.class_id == LIT_MAGIC_STRING_ASYNC_GENERATOR_UL)
{
executable_object_p = (vm_executable_object_t *) ext_object_p;
}
}
}
if (executable_object_p == NULL)
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not an async generator object."));
}
if (executable_object_p->extended_object.u.class_prop.extra_info & ECMA_EXECUTABLE_OBJECT_COMPLETED)
{
ecma_value_t promise = ecma_make_object_value (ecma_builtin_get (ECMA_BUILTIN_ID_PROMISE));
if (JERRY_UNLIKELY (builtin_routine_id == ECMA_ASYNC_GENERATOR_PROTOTYPE_ROUTINE_THROW))
{
return ecma_promise_reject_or_resolve (promise, arguments_list_p[0], false);
}
ecma_value_t iter_result = ecma_create_iter_result_object (ECMA_VALUE_UNDEFINED, ECMA_VALUE_TRUE);
ecma_value_t result = ecma_promise_reject_or_resolve (promise, iter_result, true);
ecma_free_value (iter_result);
return result;
}
return ecma_async_generator_enqueue (executable_object_p,
ECMA_ASYNC_GENERATOR_ROUTINE_TO_OPERATION (builtin_routine_id),
arguments_list_p[0]);
} /* ecma_builtin_async_generator_prototype_dispatch_routine */
/**
* @}
* @}
* @}
*/
#endif /* ENABLED (JERRY_ESNEXT) */
@@ -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.
*/
/*
* AsyncGenerator.prototype built-in description
*/
#include "ecma-builtin-helpers-macro-defines.inc.h"
#if ENABLED (JERRY_ESNEXT)
/* Object properties:
* (property name, object pointer getter) */
/* ECMA-262 v6, 25.3.1.5 */
STRING_VALUE (LIT_GLOBAL_SYMBOL_TO_STRING_TAG,
LIT_MAGIC_STRING_ASYNC_GENERATOR_UL,
ECMA_PROPERTY_FLAG_CONFIGURABLE)
/* ECMA-262 v6, 25.2.3.1 */
OBJECT_VALUE (LIT_MAGIC_STRING_CONSTRUCTOR,
ECMA_BUILTIN_ID_ASYNC_GENERATOR,
ECMA_PROPERTY_FLAG_CONFIGURABLE)
/* Routine properties:
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
ROUTINE (LIT_MAGIC_STRING_NEXT, ECMA_ASYNC_GENERATOR_PROTOTYPE_ROUTINE_NEXT, 1, 1)
ROUTINE (LIT_MAGIC_STRING_RETURN, ECMA_ASYNC_GENERATOR_PROTOTYPE_ROUTINE_RETURN, 1, 1)
ROUTINE (LIT_MAGIC_STRING_THROW, ECMA_ASYNC_GENERATOR_PROTOTYPE_ROUTINE_THROW, 1, 1)
#endif /* ENABLED (JERRY_ESNEXT) */
#include "ecma-builtin-helpers-macro-undefs.inc.h"
@@ -0,0 +1,43 @@
/* 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-globals.h"
#if ENABLED (JERRY_ESNEXT)
#define ECMA_BUILTINS_INTERNAL
#include "ecma-builtins-internal.h"
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-async-generator.inc.h"
#define BUILTIN_UNDERSCORED_ID async_generator
#include "ecma-builtin-internal-routines-template.inc.h"
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmabuiltins
* @{
*
* \addtogroup asyncgenerator ECMA AsyncGenerator object built-in
* @{
*/
/**
* @}
* @}
* @}
*/
#endif /* ENABLED (JERRY_ESNEXT) */
@@ -0,0 +1,41 @@
/* 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.
*/
/*
* %AsyncGenerator% built-in description (AsyncGeneratorFunction.prototype)
*/
#include "ecma-builtin-helpers-macro-defines.inc.h"
#if ENABLED (JERRY_ESNEXT)
/* ECMA-262 v6, 25.3.2.3.1 */
OBJECT_VALUE (LIT_MAGIC_STRING_CONSTRUCTOR,
ECMA_BUILTIN_ID_ASYNC_GENERATOR_FUNCTION,
ECMA_PROPERTY_FLAG_CONFIGURABLE)
/* ECMA-262 v6, 25.3.2.3.2 */
OBJECT_VALUE (LIT_MAGIC_STRING_PROTOTYPE,
ECMA_BUILTIN_ID_ASYNC_GENERATOR_PROTOTYPE,
ECMA_PROPERTY_FLAG_CONFIGURABLE)
/* ECMA-262 v6, 25.3.2.3.3 */
STRING_VALUE (LIT_GLOBAL_SYMBOL_TO_STRING_TAG,
LIT_MAGIC_STRING_ASYNC_GENERATOR_FUNCTION_UL,
ECMA_PROPERTY_FLAG_CONFIGURABLE)
#endif /* ENABLED (JERRY_ESNEXT) */
#include "ecma-builtin-helpers-macro-undefs.inc.h"
@@ -31,7 +31,7 @@
/**
* This object has a custom dispatch function.
*/
#define BUILTIN_CUSTOM_DISPATCH
#define BUILTIN_CUSTOM_DISPATCH
/**
* List of built-in routine identifiers.
@@ -58,14 +58,6 @@ enum
* @{
*/
/**
* Byte code sequence which returns from the generator.
*/
static const uint8_t ecma_builtin_generator_prototype_return[2] =
{
CBC_EXT_OPCODE, CBC_EXT_RETURN
};
/**
* Helper function for next / return / throw
*
@@ -120,7 +112,7 @@ ecma_builtin_generator_prototype_object_do (vm_executable_object_t *executable_o
if (resume_mode == ECMA_ITERATOR_RETURN)
{
executable_object_p->frame_ctx.byte_code_p = ecma_builtin_generator_prototype_return;
executable_object_p->frame_ctx.byte_code_p = opfunc_resume_executable_object_with_return;
}
else if (resume_mode == ECMA_ITERATOR_THROW)
{
@@ -651,6 +651,27 @@ BUILTIN (ECMA_BUILTIN_ID_GENERATOR_PROTOTYPE,
true,
generator_prototype)
/* The %(AsyncGeneratorFunction)% object */
BUILTIN_ROUTINE (ECMA_BUILTIN_ID_ASYNC_GENERATOR_FUNCTION,
ECMA_OBJECT_TYPE_FUNCTION,
ECMA_BUILTIN_ID_FUNCTION,
true,
async_generator_function)
/* The %(AsyncGenerator)% object */
BUILTIN (ECMA_BUILTIN_ID_ASYNC_GENERATOR,
ECMA_OBJECT_TYPE_GENERAL,
ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE,
true,
async_generator)
/* The %(AsyncGenerator).prototype% object */
BUILTIN (ECMA_BUILTIN_ID_ASYNC_GENERATOR_PROTOTYPE,
ECMA_OBJECT_TYPE_GENERAL,
ECMA_BUILTIN_ID_ITERATOR_PROTOTYPE,
true,
async_generator_prototype)
#if ENABLED (JERRY_BUILTIN_SET)
/* The %SetIteratorPrototype% object (ECMA-262 v6, 23.2.5.2) */
BUILTIN (ECMA_BUILTIN_ID_SET_ITERATOR_PROTOTYPE,