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
+1 -1
View File
@@ -27,7 +27,7 @@ JERRY_STATIC_ASSERT ((sizeof (cbc_uint16_arguments_t) % sizeof (jmem_cpointer_t)
*/
JERRY_STATIC_ASSERT (CBC_END == 238,
number_of_cbc_opcodes_changed);
JERRY_STATIC_ASSERT (CBC_EXT_END == 119,
JERRY_STATIC_ASSERT (CBC_EXT_END == 121,
number_of_cbc_ext_opcodes_changed);
#if ENABLED (JERRY_PARSER)
+5
View File
@@ -734,8 +734,12 @@
VM_OC_YIELD) \
CBC_OPCODE (CBC_EXT_YIELD_ITERATOR, CBC_NO_FLAG, 0, \
VM_OC_YIELD) \
CBC_OPCODE (CBC_EXT_ASYNC_YIELD, CBC_NO_FLAG, 0, \
VM_OC_ASYNC_YIELD) \
CBC_OPCODE (CBC_EXT_AWAIT, CBC_NO_FLAG, 0, \
VM_OC_AWAIT) \
CBC_OPCODE (CBC_EXT_GENERATOR_AWAIT, CBC_NO_FLAG, 0, \
VM_OC_GENERATOR_AWAIT) \
CBC_OPCODE (CBC_EXT_ASYNC_EXIT, CBC_NO_FLAG, 0, \
VM_OC_ASYNC_EXIT) \
CBC_OPCODE (CBC_EXT_RETURN, CBC_NO_FLAG, -1, \
@@ -852,6 +856,7 @@ typedef enum
/* The following functions cannot be constructed (see CBC_FUNCTION_IS_CONSTRUCTABLE) */
CBC_FUNCTION_GENERATOR, /**< generator function */
CBC_FUNCTION_ASYNC, /**< async function */
CBC_FUNCTION_ASYNC_GENERATOR, /**< async generator function */
/* The following functions has no prototype (see CBC_FUNCTION_HAS_PROTOTYPE) */
CBC_FUNCTION_ARROW, /**< arrow function */
+11 -3
View File
@@ -1964,14 +1964,20 @@ parser_parse_unary_expression (parser_context_t *context_p, /**< context */
parser_check_assignment_expr (context_p);
lexer_next_token (context_p);
cbc_ext_opcode_t opcode = CBC_EXT_YIELD;
cbc_ext_opcode_t opcode = ((context_p->status_flags & PARSER_IS_ASYNC_FUNCTION) ? CBC_EXT_ASYNC_YIELD
: CBC_EXT_YIELD);
if (!lexer_check_yield_no_arg (context_p))
{
if (context_p->token.type == LEXER_MULTIPLY)
{
lexer_next_token (context_p);
opcode = CBC_EXT_YIELD_ITERATOR;
/* TODO: support yield * in async generator. Currently a meaningless error is thrown. */
if (context_p->status_flags & PARSER_IS_ASYNC_FUNCTION)
{
parser_raise_error (context_p, PARSER_ERR_INVALID_CHARACTER);
}
}
parser_parse_expression (context_p, PARSE_EXPR_NO_COMMA);
@@ -2389,7 +2395,9 @@ parser_process_unary_expression (parser_context_t *context_p, /**< context */
#if ENABLED (JERRY_ESNEXT)
else if (JERRY_UNLIKELY (token == LEXER_KEYW_AWAIT))
{
parser_emit_cbc_ext (context_p, CBC_EXT_AWAIT);
cbc_ext_opcode_t opcode = ((context_p->status_flags & PARSER_IS_GENERATOR_FUNCTION) ? CBC_EXT_GENERATOR_AWAIT
: CBC_EXT_AWAIT);
parser_emit_cbc_ext (context_p, opcode);
}
#endif /* ENABLED (JERRY_ESNEXT) */
else
@@ -201,6 +201,12 @@ typedef enum
#define PARSER_GET_EVAL_FLAG(type) \
((type) >> JERRY_LOG2 (ECMA_PARSE_ALLOW_SUPER))
/**
* Check non-generator async functions
*/
#define PARSER_IS_NORMAL_ASYNC_FUNCTION(status_flags) \
(((status_flags) & (PARSER_IS_GENERATOR_FUNCTION | PARSER_IS_ASYNC_FUNCTION)) == PARSER_IS_ASYNC_FUNCTION)
#else /* !ENABLED (JERRY_ESNEXT) */
/**
+30 -17
View File
@@ -692,6 +692,11 @@ parse_print_final_cbc (ecma_compiled_code_t *compiled_code_p, /**< compiled code
JERRY_DEBUG_MSG (",async");
break;
}
case CBC_FUNCTION_ASYNC_GENERATOR:
{
JERRY_DEBUG_MSG (",async_generator");
break;
}
case CBC_FUNCTION_ARROW:
{
JERRY_DEBUG_MSG (",arrow");
@@ -957,7 +962,7 @@ parser_post_processing (parser_context_t *context_p) /**< context */
JERRY_ASSERT (!(context_p->status_flags & PARSER_NO_END_LABEL));
}
if (context_p->status_flags & PARSER_IS_ASYNC_FUNCTION)
if (PARSER_IS_NORMAL_ASYNC_FUNCTION (context_p->status_flags))
{
PARSER_MINUS_EQUAL_U16 (context_p->stack_depth, PARSER_TRY_CONTEXT_STACK_ALLOCATION);
#ifndef JERRY_NDEBUG
@@ -1195,7 +1200,7 @@ parser_post_processing (parser_context_t *context_p) /**< context */
context_p->status_flags &= (uint32_t) ~PARSER_NO_END_LABEL;
#if ENABLED (JERRY_ESNEXT)
if (context_p->status_flags & PARSER_IS_ASYNC_FUNCTION)
if (PARSER_IS_NORMAL_ASYNC_FUNCTION (context_p->status_flags))
{
length++;
}
@@ -1350,31 +1355,42 @@ parser_post_processing (parser_context_t *context_p) /**< context */
}
#if ENABLED (JERRY_ESNEXT)
uint16_t function_type = CBC_FUNCTION_TO_TYPE_BITS (CBC_FUNCTION_NORMAL);
if (context_p->status_flags & (PARSER_IS_PROPERTY_GETTER | PARSER_IS_PROPERTY_SETTER))
{
compiled_code_p->status_flags |= CBC_FUNCTION_TO_TYPE_BITS (CBC_FUNCTION_ACCESSOR);
function_type = CBC_FUNCTION_TO_TYPE_BITS (CBC_FUNCTION_ACCESSOR);
}
else if (context_p->status_flags & PARSER_IS_ARROW_FUNCTION)
{
compiled_code_p->status_flags |= CBC_FUNCTION_TO_TYPE_BITS (CBC_FUNCTION_ARROW);
function_type = CBC_FUNCTION_TO_TYPE_BITS (CBC_FUNCTION_ARROW);
}
else if (context_p->status_flags & PARSER_IS_GENERATOR_FUNCTION)
{
compiled_code_p->status_flags |= CBC_FUNCTION_TO_TYPE_BITS (CBC_FUNCTION_GENERATOR);
if (context_p->status_flags & PARSER_IS_ASYNC_FUNCTION)
{
function_type = CBC_FUNCTION_TO_TYPE_BITS (CBC_FUNCTION_ASYNC_GENERATOR);
}
else
{
function_type = CBC_FUNCTION_TO_TYPE_BITS (CBC_FUNCTION_GENERATOR);
}
}
else if (context_p->status_flags & PARSER_IS_ASYNC_FUNCTION)
{
compiled_code_p->status_flags |= CBC_FUNCTION_TO_TYPE_BITS (CBC_FUNCTION_ASYNC);
function_type = CBC_FUNCTION_TO_TYPE_BITS (CBC_FUNCTION_ASYNC);
}
else if (context_p->status_flags & PARSER_CLASS_CONSTRUCTOR)
{
compiled_code_p->status_flags |= CBC_FUNCTION_TO_TYPE_BITS (CBC_FUNCTION_CONSTRUCTOR);
function_type = CBC_FUNCTION_TO_TYPE_BITS (CBC_FUNCTION_CONSTRUCTOR);
}
else
{
compiled_code_p->status_flags |= CBC_FUNCTION_TO_TYPE_BITS (CBC_FUNCTION_NORMAL);
function_type = CBC_FUNCTION_TO_TYPE_BITS (CBC_FUNCTION_NORMAL);
}
compiled_code_p->status_flags |= function_type;
if (context_p->status_flags & PARSER_FUNCTION_HAS_REST_PARAM)
{
compiled_code_p->status_flags |= CBC_CODE_FLAGS_REST_PARAMETER;
@@ -1582,7 +1598,7 @@ parser_post_processing (parser_context_t *context_p) /**< context */
*dst_p++ = CBC_RETURN_WITH_BLOCK;
#if ENABLED (JERRY_ESNEXT)
if (context_p->status_flags & PARSER_IS_ASYNC_FUNCTION)
if (PARSER_IS_NORMAL_ASYNC_FUNCTION (context_p->status_flags))
{
dst_p[-1] = CBC_EXT_OPCODE;
dst_p[0] = CBC_EXT_ASYNC_EXIT;
@@ -1745,14 +1761,7 @@ parser_parse_function_arguments (parser_context_t *context_p, /**< context */
bool has_duplicated_arg_names = false;
/* TODO: Currently async iterators are not supported, so generators ignore the async modifier. */
uint32_t mask = (PARSER_IS_GENERATOR_FUNCTION | PARSER_IS_ASYNC_FUNCTION);
if ((context_p->status_flags & mask) == mask)
{
context_p->status_flags &= (uint32_t) ~PARSER_IS_ASYNC_FUNCTION;
}
if (context_p->status_flags & PARSER_IS_ASYNC_FUNCTION)
if (PARSER_IS_NORMAL_ASYNC_FUNCTION (context_p->status_flags))
{
parser_branch_t branch;
parser_emit_cbc_ext_forward_branch (context_p, CBC_EXT_TRY_CREATE_CONTEXT, &branch);
@@ -2058,6 +2067,10 @@ parser_parse_source (const uint8_t *arg_list_p, /**< function argument list */
{
context.status_flags |= PARSER_IS_GENERATOR_FUNCTION;
}
if (parse_opts & ECMA_PARSE_ASYNC_FUNCTION)
{
context.status_flags |= PARSER_IS_ASYNC_FUNCTION;
}
#endif /* ENABLED (JERRY_ESNEXT) */
}
+4
View File
@@ -2335,6 +2335,10 @@ scanner_scan_all (parser_context_t *context_p, /**< context */
{
status_flags |= SCANNER_LITERAL_POOL_GENERATOR;
}
if (context_p->status_flags & PARSER_IS_ASYNC_FUNCTION)
{
status_flags |= SCANNER_LITERAL_POOL_ASYNC;
}
#endif /* ENABLED (JERRY_ESNEXT) */
scanner_push_literal_pool (context_p, &scanner_context, status_flags);