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,181 @@
/* 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-async-generator-object.h"
#include "ecma-builtins.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.h"
#if ENABLED (JERRY_ESNEXT)
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmaasyncgeneratorobject ECMA AsyncGenerator object related routines
* @{
*/
/**
* Enqueue a task into the command queue of an async generator
*
* @return ecma Promise value
* Returned value must be freed with ecma_free_value.
*/
ecma_value_t
ecma_async_generator_enqueue (vm_executable_object_t *async_generator_object_p, /**< async generator */
ecma_async_generator_operation_type_t operation, /**< operation */
ecma_value_t value) /**< value argument of operation */
{
ecma_async_generator_task_t *task_p = jmem_heap_alloc_block (sizeof (ecma_async_generator_task_t));
ECMA_SET_INTERNAL_VALUE_ANY_POINTER (task_p->next, NULL);
task_p->operation_value = ecma_copy_value_if_not_object (value);
task_p->operation_type = (uint8_t) operation;
ecma_object_t *old_new_target_p = JERRY_CONTEXT (current_new_target);
JERRY_CONTEXT (current_new_target) = ecma_builtin_get (ECMA_BUILTIN_ID_PROMISE);
ecma_value_t result = ecma_op_create_promise_object (ECMA_VALUE_EMPTY, ECMA_PROMISE_EXECUTOR_EMPTY);
JERRY_CONTEXT (current_new_target) = old_new_target_p;
task_p->promise = result;
ecma_value_t head = async_generator_object_p->extended_object.u.class_prop.u.head;
if (ECMA_IS_INTERNAL_VALUE_NULL (head))
{
ECMA_SET_INTERNAL_VALUE_POINTER (async_generator_object_p->extended_object.u.class_prop.u.head, task_p);
if (async_generator_object_p->extended_object.u.class_prop.extra_info & ECMA_ASYNC_GENERATOR_CALLED)
{
ecma_value_t executable_object = ecma_make_object_value ((ecma_object_t *) async_generator_object_p);
ecma_enqueue_promise_async_generator_job (executable_object);
return result;
}
async_generator_object_p->extended_object.u.class_prop.extra_info |= ECMA_ASYNC_GENERATOR_CALLED;
ecma_async_generator_run (async_generator_object_p);
return result;
}
/* Append the new task at the end. */
ecma_async_generator_task_t *prev_task_p;
prev_task_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_async_generator_task_t, head);
while (!ECMA_IS_INTERNAL_VALUE_NULL (prev_task_p->next))
{
prev_task_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_async_generator_task_t, prev_task_p->next);
}
ECMA_SET_INTERNAL_VALUE_POINTER (prev_task_p->next, task_p);
return result;
} /* ecma_async_generator_enqueue */
/**
* Execute the next task in the command queue of the async generator
*/
void
ecma_async_generator_run (vm_executable_object_t *async_generator_object_p) /**< async generator */
{
JERRY_ASSERT (async_generator_object_p->extended_object.u.class_prop.class_id
== LIT_MAGIC_STRING_ASYNC_GENERATOR_UL);
JERRY_ASSERT (!ECMA_IS_INTERNAL_VALUE_NULL (async_generator_object_p->extended_object.u.class_prop.u.head));
ecma_value_t head = async_generator_object_p->extended_object.u.class_prop.u.head;
ecma_async_generator_task_t *task_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_async_generator_task_t, head);
if (task_p->operation_type == ECMA_ASYNC_GENERATOR_DO_RETURN)
{
async_generator_object_p->frame_ctx.byte_code_p = opfunc_resume_executable_object_with_return;
}
else if (task_p->operation_type == ECMA_ASYNC_GENERATOR_DO_THROW)
{
async_generator_object_p->frame_ctx.byte_code_p = opfunc_resume_executable_object_with_throw;
}
ecma_value_t value = task_p->operation_value;
ecma_ref_if_object (value);
task_p->operation_value = ECMA_VALUE_UNDEFINED;
value = opfunc_resume_executable_object (async_generator_object_p, value);
if (async_generator_object_p->extended_object.u.class_prop.extra_info & ECMA_EXECUTABLE_OBJECT_COMPLETED)
{
JERRY_ASSERT (head == async_generator_object_p->extended_object.u.class_prop.u.head);
ecma_async_generator_finalize (async_generator_object_p, value);
}
} /* ecma_async_generator_run */
/**
* Finalize the promises of an executable generator
*/
void
ecma_async_generator_finalize (vm_executable_object_t *async_generator_object_p, /**< async generator */
ecma_value_t value) /**< final value (takes reference) */
{
ecma_value_t next = async_generator_object_p->extended_object.u.class_prop.u.head;
ecma_async_generator_task_t *task_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_async_generator_task_t, next);
ECMA_SET_INTERNAL_VALUE_ANY_POINTER (async_generator_object_p->extended_object.u.class_prop.u.head, NULL);
if (ECMA_IS_VALUE_ERROR (value))
{
value = jcontext_take_exception ();
ecma_reject_promise (task_p->promise, value);
}
else
{
ecma_value_t result = ecma_create_iter_result_object (value, ECMA_VALUE_TRUE);
ecma_fulfill_promise (task_p->promise, result);
ecma_free_value (result);
}
ecma_free_value (value);
next = task_p->next;
jmem_heap_free_block (task_p, sizeof (ecma_async_generator_task_t));
while (!ECMA_IS_INTERNAL_VALUE_NULL (next))
{
task_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_async_generator_task_t, next);
if (task_p->operation_type != ECMA_ASYNC_GENERATOR_DO_THROW)
{
value = ecma_create_iter_result_object (ECMA_VALUE_UNDEFINED, ECMA_VALUE_TRUE);
ecma_fulfill_promise (task_p->promise, value);
ecma_free_value (value);
}
else
{
ecma_reject_promise (task_p->promise, task_p->operation_value);
}
ecma_free_value_if_not_object (task_p->operation_value);
next = task_p->next;
jmem_heap_free_block (task_p, sizeof (ecma_async_generator_task_t));
}
} /* ecma_async_generator_finalize */
#endif /* ENABLED (JERRY_ESNEXT) */
/**
* @}
* @}
*/
@@ -0,0 +1,54 @@
/* 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.
*/
#ifndef ECMA_ASYNC_GENERATOR_OBJECT_H
#define ECMA_ASYNC_GENERATOR_OBJECT_H
#include "ecma-globals.h"
#include "vm-defines.h"
#if ENABLED (JERRY_ESNEXT)
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmaasyncgeneratorobject ECMA AsyncGenerator object related routines
* @{
*/
/**
* AsyncGenerator command types.
*/
typedef enum
{
ECMA_ASYNC_GENERATOR_DO_NEXT, /**< async generator next operation */
ECMA_ASYNC_GENERATOR_DO_THROW, /**< async generator throw operation */
ECMA_ASYNC_GENERATOR_DO_RETURN, /**< async generator return operation */
} ecma_async_generator_operation_type_t;
ecma_value_t ecma_async_generator_enqueue (vm_executable_object_t *async_generator_object_p,
ecma_async_generator_operation_type_t operation, ecma_value_t value);
void ecma_async_generator_run (vm_executable_object_t *async_generator_object_p);
void ecma_async_generator_finalize (vm_executable_object_t *async_generator_object_p, ecma_value_t value);
#endif /* ENABLED (JERRY_ESNEXT) */
/**
* @}
* @}
*/
#endif /* !ECMA_ASYNC_GENERATOR_OBJECT_H */
@@ -395,23 +395,28 @@ ecma_op_create_dynamic_function (const ecma_value_t *arguments_list_p, /**< argu
#if ENABLED (JERRY_ESNEXT)
ecma_object_t *new_target_p = JERRY_CONTEXT (current_new_target);
bool is_generator_func = parse_opts & ECMA_PARSE_GENERATOR_FUNCTION;
if (is_generator_func)
if (JERRY_UNLIKELY (parse_opts & (ECMA_PARSE_GENERATOR_FUNCTION | ECMA_PARSE_ASYNC_FUNCTION)))
{
fallback_proto = ECMA_BUILTIN_ID_GENERATOR;
}
if (new_target_p == NULL)
{
if (is_generator_func)
if (parse_opts & ECMA_PARSE_ASYNC_FUNCTION)
{
fallback_proto = ECMA_BUILTIN_ID_ASYNC_GENERATOR;
if (new_target_p == NULL)
{
new_target_p = ecma_builtin_get (ECMA_BUILTIN_ID_ASYNC_GENERATOR_FUNCTION);
}
}
else if (new_target_p == NULL)
{
new_target_p = ecma_builtin_get (ECMA_BUILTIN_ID_GENERATOR_FUNCTION);
}
else
{
new_target_p = ecma_builtin_get (ECMA_BUILTIN_ID_FUNCTION);
}
}
else if (new_target_p == NULL)
{
new_target_p = ecma_builtin_get (ECMA_BUILTIN_ID_FUNCTION);
}
ecma_object_t *proto = ecma_op_get_prototype_from_constructor (new_target_p, fallback_proto);
@@ -475,6 +480,20 @@ ecma_op_create_generator_function_object (ecma_object_t *scope_p, /**< function'
return ecma_op_create_function_object (scope_p, bytecode_data_p, ECMA_BUILTIN_ID_GENERATOR);
} /* ecma_op_create_generator_function_object */
/**
* AsyncGeneratorFunction object creation operation.
*
* See also: ECMA-262 v10, 25.3
*
* @return pointer to newly created Function object
*/
ecma_object_t *
ecma_op_create_async_generator_function_object (ecma_object_t *scope_p, /**< function's scope */
const ecma_compiled_code_t *bytecode_data_p) /**< byte-code array */
{
return ecma_op_create_function_object (scope_p, bytecode_data_p, ECMA_BUILTIN_ID_ASYNC_GENERATOR);
} /* ecma_op_create_async_generator_function_object */
/**
* Arrow function object creation operation.
*
@@ -1294,6 +1313,11 @@ ecma_op_function_construct (ecma_object_t *func_obj_p, /**< Function object */
message_p = ECMA_ERR_MSG ("Async functions cannot be invoked with 'new'.");
break;
}
case CBC_FUNCTION_ASYNC_GENERATOR:
{
message_p = ECMA_ERR_MSG ("Async generator functions cannot be invoked with 'new'.");
break;
}
case CBC_FUNCTION_ARROW:
{
message_p = ECMA_ERR_MSG ("Arrow functions cannot be invoked with 'new'.");
@@ -1397,6 +1421,14 @@ ecma_op_lazy_instantiate_prototype_object (ecma_object_t *object_p) /**< the fun
ECMA_OBJECT_TYPE_GENERAL);
init_constructor = false;
}
if (CBC_FUNCTION_GET_TYPE (byte_code_p->status_flags) == CBC_FUNCTION_ASYNC_GENERATOR)
{
proto_object_p = ecma_create_object (ecma_builtin_get (ECMA_BUILTIN_ID_ASYNC_GENERATOR_PROTOTYPE),
0,
ECMA_OBJECT_TYPE_GENERAL);
init_constructor = false;
}
}
#endif /* ENABLED (JERRY_ESNEXT) */
@@ -61,6 +61,9 @@ ecma_op_function_get_super_constructor (ecma_object_t *func_obj_p);
ecma_object_t *
ecma_op_create_generator_function_object (ecma_object_t *scope_p, const ecma_compiled_code_t *bytecode_data_p);
ecma_object_t *
ecma_op_create_async_generator_function_object (ecma_object_t *scope_p, const ecma_compiled_code_t *bytecode_data_p);
ecma_object_t *
ecma_op_create_arrow_function_object (ecma_object_t *scope_p, const ecma_compiled_code_t *bytecode_data_p,
ecma_value_t this_binding);
+71 -2
View File
@@ -13,6 +13,7 @@
* limitations under the License.
*/
#include "ecma-async-generator-object.h"
#include "ecma-function-object.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
@@ -57,6 +58,15 @@ typedef struct
ecma_value_t argument; /**< argument for the reaction */
} ecma_job_promise_async_reaction_t;
/**
* Description of the PromiseAsyncGeneratorJob
*/
typedef struct
{
ecma_job_queue_item_t header; /**< job queue item header */
ecma_value_t executable_object; /**< executable object */
} ecma_job_promise_async_generator_t;
/**
* Description of the PromiseResolveThenableJob
*/
@@ -129,6 +139,20 @@ ecma_free_promise_async_reaction_job (ecma_job_promise_async_reaction_t *job_p)
jmem_heap_free_block (job_p, sizeof (ecma_job_promise_async_reaction_t));
} /* ecma_free_promise_async_reaction_job */
/**
* Free the heap and the member of the PromiseAsyncGeneratorJob.
*/
static void
ecma_free_promise_async_generator_job (ecma_job_promise_async_generator_t *job_p) /**< points to the
* PromiseAsyncReactionJob */
{
JERRY_ASSERT (job_p != NULL);
ecma_free_value (job_p->executable_object);
jmem_heap_free_block (job_p, sizeof (ecma_job_promise_async_generator_t));
} /* ecma_free_promise_async_generator_job */
/**
* Free the heap and the member of the PromiseResolveThenableJob.
*/
@@ -240,13 +264,34 @@ ecma_process_promise_async_reaction_job (ecma_job_promise_async_reaction_t *job_
}
ecma_value_t result = opfunc_resume_executable_object (executable_object_p, job_p->argument);
/* Argument reference is taken by opfunc_resume_executable_object. */
/* Argument reference has been taken by opfunc_resume_executable_object. */
job_p->argument = ECMA_VALUE_UNDEFINED;
ecma_free_promise_async_reaction_job (job_p);
uint16_t expected_bits = (ECMA_EXECUTABLE_OBJECT_COMPLETED | ECMA_ASYNC_GENERATOR_CALLED);
if ((executable_object_p->extended_object.u.class_prop.extra_info & expected_bits) == expected_bits)
{
ecma_async_generator_finalize (executable_object_p, result);
result = ECMA_VALUE_UNDEFINED;
}
ecma_free_promise_async_reaction_job (job_p);
return result;
} /* ecma_process_promise_async_reaction_job */
/**
* The processor for PromiseAsyncGeneratorJob.
*/
static void
ecma_process_promise_async_generator_job (ecma_job_promise_async_generator_t *job_p) /**< the job to be operated */
{
ecma_object_t *object_p = ecma_get_object_from_value (job_p->executable_object);
ecma_async_generator_run ((vm_executable_object_t *) object_p);
ecma_free_value (job_p->executable_object);
jmem_heap_free_block (job_p, sizeof (ecma_job_promise_async_generator_t));
} /* ecma_process_promise_async_generator_job */
/**
* Process the PromiseResolveThenableJob.
*
@@ -359,6 +404,20 @@ ecma_enqueue_promise_async_reaction_job (ecma_value_t executable_object, /**< ex
ecma_enqueue_job (&job_p->header);
} /* ecma_enqueue_promise_async_reaction_job */
/**
* Enqueue a PromiseAsyncGeneratorJob into the job queue.
*/
void
ecma_enqueue_promise_async_generator_job (ecma_value_t executable_object) /**< executable object */
{
ecma_job_promise_async_generator_t *job_p;
job_p = (ecma_job_promise_async_generator_t *) jmem_heap_alloc_block (sizeof (ecma_job_promise_async_generator_t));
job_p->header.next_and_type = ECMA_JOB_PROMISE_ASYNC_GENERATOR;
job_p->executable_object = ecma_copy_value (executable_object);
ecma_enqueue_job (&job_p->header);
} /* ecma_enqueue_promise_async_generator_job */
/**
* Enqueue a PromiseResolveThenableJob into the job queue.
*/
@@ -413,6 +472,11 @@ ecma_process_all_enqueued_jobs (void)
ret = ecma_process_promise_async_reaction_job ((ecma_job_promise_async_reaction_t *) job_p);
break;
}
case ECMA_JOB_PROMISE_ASYNC_GENERATOR:
{
ecma_process_promise_async_generator_job ((ecma_job_promise_async_generator_t *) job_p);
break;
}
default:
{
JERRY_ASSERT (ecma_job_queue_get_type (job_p) == ECMA_JOB_PROMISE_THENABLE);
@@ -450,6 +514,11 @@ ecma_free_all_enqueued_jobs (void)
ecma_free_promise_async_reaction_job ((ecma_job_promise_async_reaction_t *) job_p);
break;
}
case ECMA_JOB_PROMISE_ASYNC_GENERATOR:
{
ecma_free_promise_async_generator_job ((ecma_job_promise_async_generator_t *) job_p);
break;
}
default:
{
JERRY_ASSERT (ecma_job_queue_get_type (job_p) == ECMA_JOB_PROMISE_THENABLE);
@@ -33,6 +33,7 @@ typedef enum
ECMA_JOB_PROMISE_REACTION, /**< promise reaction job */
ECMA_JOB_PROMISE_ASYNC_REACTION_FULFILLED, /**< fulfilled promise async reaction job */
ECMA_JOB_PROMISE_ASYNC_REACTION_REJECTED, /**< rejected promise async reaction job */
ECMA_JOB_PROMISE_ASYNC_GENERATOR, /**< continue async generator */
ECMA_JOB_PROMISE_THENABLE, /**< promise thenable job */
} ecma_job_queue_item_type_t;
@@ -49,6 +50,7 @@ void ecma_job_queue_init (void);
void ecma_enqueue_promise_reaction_job (ecma_value_t capability, ecma_value_t handler, ecma_value_t argument);
void ecma_enqueue_promise_async_reaction_job (ecma_value_t executable_object,
ecma_value_t argument, bool is_rejected);
void ecma_enqueue_promise_async_generator_job (ecma_value_t executable_object);
void ecma_enqueue_promise_resolve_thenable_job (ecma_value_t promise, ecma_value_t thenable, ecma_value_t then);
void ecma_free_all_enqueued_jobs (void);
@@ -2781,6 +2781,10 @@ ecma_object_get_class_name (ecma_object_t *obj_p) /**< object */
{
return LIT_MAGIC_STRING_GENERATOR_UL;
}
case ECMA_BUILTIN_ID_ASYNC_GENERATOR:
{
return LIT_MAGIC_STRING_ASYNC_GENERATOR_UL;
}
#endif /* ENABLED (JERRY_ESNEXT) */
#if ENABLED (JERRY_BUILTIN_JSON)
case ECMA_BUILTIN_ID_JSON: