Implement AsyncIteratorClose for for-await-of statement. (#3955)

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2020-07-01 12:40:39 +02:00
committed by GitHub
parent dc3165533e
commit 6389816f67
8 changed files with 948 additions and 123 deletions
+137 -40
View File
@@ -14,11 +14,16 @@
*/
#include "ecma-alloc.h"
#include "ecma-exceptions.h"
#include "ecma-function-object.h"
#include "ecma-gc.h"
#include "ecma-helpers.h"
#include "ecma-iterator-object.h"
#include "ecma-objects.h"
#include "ecma-promise-object.h"
#include "jcontext.h"
#include "vm-defines.h"
#include "vm-stack.h"
#include "ecma-iterator-object.h"
/** \addtogroup vm Virtual machine
* @{
@@ -84,16 +89,10 @@ vm_stack_context_abort (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
case VM_CONTEXT_FOR_OF:
case VM_CONTEXT_FOR_AWAIT_OF:
{
ecma_value_t iterator = vm_stack_top_p[-3];
ecma_free_value (vm_stack_top_p[-2]);
ecma_free_value (vm_stack_top_p[-3]);
ecma_free_value (vm_stack_top_p[-4]);
if (context_info & VM_CONTEXT_CLOSE_ITERATOR)
{
ecma_op_iterator_close (iterator);
}
ecma_free_value (iterator);
VM_MINUS_EQUAL_U16 (frame_ctx_p->context_depth, PARSER_FOR_OF_CONTEXT_STACK_ALLOCATION);
vm_stack_top_p -= PARSER_FOR_OF_CONTEXT_STACK_ALLOCATION;
break;
@@ -158,20 +157,29 @@ vm_decode_branch_offset (const uint8_t *branch_offset_p, /**< start offset of by
return branch_offset;
} /* vm_decode_branch_offset */
#if ENABLED (JERRY_ESNEXT)
/**
* Byte code which resumes an executable object with throw
*/
static const uint8_t vm_stack_resume_executable_object_with_context_end[1] =
{
CBC_CONTEXT_END
};
#endif /* ENABLED (JERRY_ESNEXT) */
/**
* Find a finally up to the end position.
*
* @return true - if 'finally' found,
* false - otherwise
* @return value specified in vm_stack_found_type
*/
bool
vm_stack_found_type
vm_stack_find_finally (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
ecma_value_t **vm_stack_top_ref_p, /**< current stack top */
ecma_value_t *stack_top_p, /**< current stack top */
vm_stack_context_type_t finally_type, /**< searching this finally */
uint32_t search_limit) /**< search up-to this byte code */
{
ecma_value_t *vm_stack_top_p = *vm_stack_top_ref_p;
JERRY_ASSERT (finally_type <= VM_CONTEXT_FINALLY_RETURN);
if (finally_type != VM_CONTEXT_FINALLY_JUMP)
@@ -182,15 +190,15 @@ vm_stack_find_finally (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
while (frame_ctx_p->context_depth > 0)
{
vm_stack_context_type_t context_type;
uint32_t context_end = VM_GET_CONTEXT_END (vm_stack_top_p[-1]);
uint32_t context_end = VM_GET_CONTEXT_END (stack_top_p[-1]);
if (search_limit < context_end)
{
*vm_stack_top_ref_p = vm_stack_top_p;
return false;
frame_ctx_p->stack_top_p = stack_top_p;
return VM_CONTEXT_FOUND_EXPECTED;
}
context_type = VM_GET_CONTEXT_TYPE (vm_stack_top_p[-1]);
context_type = VM_GET_CONTEXT_TYPE (stack_top_p[-1]);
if (context_type == VM_CONTEXT_TRY || context_type == VM_CONTEXT_CATCH)
{
const uint8_t *byte_code_p;
@@ -199,12 +207,12 @@ vm_stack_find_finally (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
if (search_limit == context_end)
{
*vm_stack_top_ref_p = vm_stack_top_p;
return false;
frame_ctx_p->stack_top_p = stack_top_p;
return VM_CONTEXT_FOUND_EXPECTED;
}
#if ENABLED (JERRY_ESNEXT)
if (vm_stack_top_p[-1] & VM_CONTEXT_HAS_LEX_ENV)
if (stack_top_p[-1] & VM_CONTEXT_HAS_LEX_ENV)
{
ecma_object_t *lex_env_p = frame_ctx_p->lex_env_p;
JERRY_ASSERT (lex_env_p->u2.outer_reference_cp != JMEM_CP_NULL);
@@ -230,13 +238,12 @@ vm_stack_find_finally (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
{
branch_offset += (uint32_t) (byte_code_p - frame_ctx_p->byte_code_start_p);
vm_stack_top_p[-1] = VM_CREATE_CONTEXT (VM_CONTEXT_CATCH, branch_offset);
stack_top_p[-1] = VM_CREATE_CONTEXT (VM_CONTEXT_CATCH, branch_offset);
byte_code_p += 2 + branch_offset_length;
frame_ctx_p->byte_code_p = byte_code_p;
*vm_stack_top_ref_p = vm_stack_top_p;
return true;
frame_ctx_p->stack_top_p = stack_top_p;
return VM_CONTEXT_FOUND_FINALLY;
}
byte_code_p += branch_offset;
@@ -244,7 +251,7 @@ vm_stack_find_finally (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
if (*byte_code_p == CBC_CONTEXT_END)
{
VM_MINUS_EQUAL_U16 (frame_ctx_p->context_depth, PARSER_TRY_CONTEXT_STACK_ALLOCATION);
vm_stack_top_p -= PARSER_TRY_CONTEXT_STACK_ALLOCATION;
stack_top_p -= PARSER_TRY_CONTEXT_STACK_ALLOCATION;
continue;
}
}
@@ -254,7 +261,7 @@ vm_stack_find_finally (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
JERRY_ASSERT (context_type == VM_CONTEXT_CATCH);
#if !ENABLED (JERRY_ESNEXT)
if (vm_stack_top_p[-1] & VM_CONTEXT_HAS_LEX_ENV)
if (stack_top_p[-1] & VM_CONTEXT_HAS_LEX_ENV)
{
ecma_object_t *lex_env_p = frame_ctx_p->lex_env_p;
JERRY_ASSERT (lex_env_p->u2.outer_reference_cp != JMEM_CP_NULL);
@@ -266,7 +273,7 @@ vm_stack_find_finally (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
if (byte_code_p[0] == CBC_CONTEXT_END)
{
VM_MINUS_EQUAL_U16 (frame_ctx_p->context_depth, PARSER_TRY_CONTEXT_STACK_ALLOCATION);
vm_stack_top_p -= PARSER_TRY_CONTEXT_STACK_ALLOCATION;
stack_top_p -= PARSER_TRY_CONTEXT_STACK_ALLOCATION;
continue;
}
}
@@ -274,18 +281,17 @@ vm_stack_find_finally (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
JERRY_ASSERT (byte_code_p[0] == CBC_EXT_OPCODE);
VM_PLUS_EQUAL_U16 (frame_ctx_p->context_depth, PARSER_FINALLY_CONTEXT_EXTRA_STACK_ALLOCATION);
vm_stack_top_p += PARSER_FINALLY_CONTEXT_EXTRA_STACK_ALLOCATION;
stack_top_p += PARSER_FINALLY_CONTEXT_EXTRA_STACK_ALLOCATION;
#if ENABLED (JERRY_ESNEXT)
if (JERRY_UNLIKELY (byte_code_p[1] == CBC_EXT_ASYNC_EXIT))
{
branch_offset = (uint32_t) (byte_code_p - frame_ctx_p->byte_code_start_p);
vm_stack_top_p[-1] = VM_CREATE_CONTEXT ((uint32_t) finally_type, branch_offset);
stack_top_p[-1] = VM_CREATE_CONTEXT ((uint32_t) finally_type, branch_offset);
frame_ctx_p->byte_code_p = byte_code_p;
*vm_stack_top_ref_p = vm_stack_top_p;
return true;
frame_ctx_p->stack_top_p = stack_top_p;
return VM_CONTEXT_FOUND_FINALLY;
}
#endif /* ENABLED (JERRY_ESNEXT) */
@@ -298,20 +304,111 @@ vm_stack_find_finally (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
branch_offset += (uint32_t) (byte_code_p - frame_ctx_p->byte_code_start_p);
vm_stack_top_p[-1] = VM_CREATE_CONTEXT ((uint32_t) finally_type, branch_offset);
stack_top_p[-1] = VM_CREATE_CONTEXT ((uint32_t) finally_type, branch_offset);
byte_code_p += 2 + branch_offset_length;
frame_ctx_p->byte_code_p = byte_code_p;
*vm_stack_top_ref_p = vm_stack_top_p;
return true;
frame_ctx_p->stack_top_p = stack_top_p;
return VM_CONTEXT_FOUND_FINALLY;
}
#if ENABLED (JERRY_ESNEXT)
else if (stack_top_p[-1] & VM_CONTEXT_CLOSE_ITERATOR)
{
JERRY_ASSERT (context_type == VM_CONTEXT_FOR_OF || context_type == VM_CONTEXT_FOR_AWAIT_OF);
JERRY_ASSERT (finally_type == VM_CONTEXT_FINALLY_THROW || !jcontext_has_pending_exception ());
vm_stack_top_p = vm_stack_context_abort (frame_ctx_p, vm_stack_top_p);
ecma_value_t exception = ECMA_VALUE_UNDEFINED;
if (finally_type == VM_CONTEXT_FINALLY_THROW)
{
exception = jcontext_take_exception ();
}
ecma_value_t iterator = stack_top_p[-3];
ecma_value_t result = ecma_op_get_method_by_magic_id (iterator, LIT_MAGIC_STRING_RETURN);
if (!ECMA_IS_VALUE_ERROR (result) && !ecma_is_value_undefined (result))
{
if (!ecma_is_value_object (result) || !ecma_op_is_callable (result))
{
ecma_free_value (result);
result = ecma_raise_type_error (ECMA_ERR_MSG ("Iterator return() is not callable"));
}
else
{
ecma_object_t *return_obj_p = ecma_get_object_from_value (result);
result = ecma_op_function_call (return_obj_p, iterator, NULL, 0);
ecma_deref_object (return_obj_p);
if (context_type == VM_CONTEXT_FOR_AWAIT_OF && !ECMA_IS_VALUE_ERROR (result))
{
ecma_extended_object_t *async_generator_object_p = VM_GET_EXECUTABLE_OBJECT (frame_ctx_p);
result = ecma_promise_async_await (async_generator_object_p, result);
if (!ECMA_IS_VALUE_ERROR (result))
{
uint16_t extra_flags = (ECMA_EXECUTABLE_OBJECT_DO_AWAIT_OR_YIELD
| (ECMA_AWAIT_FOR_CLOSE << ECMA_AWAIT_STATE_SHIFT));
async_generator_object_p->u.class_prop.extra_info |= extra_flags;
stack_top_p = vm_stack_context_abort (frame_ctx_p, stack_top_p);
VM_PLUS_EQUAL_U16 (frame_ctx_p->context_depth, PARSER_FINALLY_CONTEXT_STACK_ALLOCATION);
stack_top_p += PARSER_FINALLY_CONTEXT_STACK_ALLOCATION;
stack_top_p[-1] = VM_CREATE_CONTEXT ((uint32_t) finally_type, context_end);
if (finally_type == VM_CONTEXT_FINALLY_THROW)
{
stack_top_p[-2] = exception;
}
frame_ctx_p->call_operation = VM_EXEC_RETURN;
frame_ctx_p->byte_code_p = vm_stack_resume_executable_object_with_context_end;
frame_ctx_p->stack_top_p = stack_top_p;
return VM_CONTEXT_FOUND_AWAIT;
}
}
if (!ECMA_IS_VALUE_ERROR (result))
{
bool is_object = ecma_is_value_object (result);
ecma_free_value (result);
result = ECMA_VALUE_UNDEFINED;
if (!is_object)
{
result = ecma_raise_type_error (ECMA_ERR_MSG ("Iterator return() result is not object"));
}
}
}
}
JERRY_ASSERT (ECMA_IS_VALUE_ERROR (result) || result == ECMA_VALUE_UNDEFINED);
if (ECMA_IS_VALUE_ERROR (result))
{
if (finally_type != VM_CONTEXT_FINALLY_THROW)
{
frame_ctx_p->stack_top_p = vm_stack_context_abort (frame_ctx_p, stack_top_p);
return VM_CONTEXT_FOUND_ERROR;
}
ecma_free_value (jcontext_take_exception ());
jcontext_raise_exception (exception);
}
else if (finally_type == VM_CONTEXT_FINALLY_THROW)
{
jcontext_raise_exception (exception);
}
}
#endif /* ENABLED (JERRY_ESNEXT) */
stack_top_p = vm_stack_context_abort (frame_ctx_p, stack_top_p);
}
*vm_stack_top_ref_p = vm_stack_top_p;
return false;
frame_ctx_p->stack_top_p = stack_top_p;
return VM_CONTEXT_FOUND_EXPECTED;
} /* vm_stack_find_finally */
#if ENABLED (JERRY_ESNEXT)