Implement the core of the generator functions. (#3368)

Some things are missing:
 - yield* support
 - generator definition in object literal
 - the hidden GeneratorFunction

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2019-11-28 11:54:27 +01:00
committed by Dániel Bátyai
parent 14e95a4775
commit 110f75c99d
35 changed files with 1535 additions and 85 deletions
+173
View File
@@ -30,6 +30,7 @@
#include "jcontext.h"
#include "opcodes.h"
#include "vm-defines.h"
#include "vm-stack.h"
/** \addtogroup vm Virtual machine
* @{
@@ -280,6 +281,7 @@ opfunc_for_in (ecma_value_t left_value, /**< left value */
} /* opfunc_for_in */
#if ENABLED (JERRY_ES2015)
/**
* 'VM_OC_APPEND_ARRAY' opcode handler specialized for spread objects
*
@@ -454,6 +456,7 @@ opfunc_spread_arguments (ecma_value_t *stack_top_p, /**< pointer to the current
return buff_p;
} /* opfunc_spread_arguments */
#endif /* ENABLED (JERRY_ES2015) */
/**
@@ -539,6 +542,176 @@ opfunc_append_array (ecma_value_t *stack_top_p, /**< current stack top */
return ECMA_VALUE_EMPTY;
} /* opfunc_append_array */
#if ENABLED (JERRY_ES2015)
/**
* Create an executable object using the current frame context
*
* @return executable object
*/
ecma_value_t
opfunc_create_executable_object (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
{
const ecma_compiled_code_t *bytecode_header_p = frame_ctx_p->bytecode_header_p;
size_t size;
ecma_bytecode_ref ((ecma_compiled_code_t *) bytecode_header_p);
if (bytecode_header_p->status_flags & CBC_CODE_FLAGS_UINT16_ARGUMENTS)
{
cbc_uint16_arguments_t *args_p = (cbc_uint16_arguments_t *) bytecode_header_p;
size = ((size_t) args_p->register_end + (size_t) args_p->stack_limit) * sizeof (ecma_value_t);
}
else
{
cbc_uint8_arguments_t *args_p = (cbc_uint8_arguments_t *) bytecode_header_p;
size = ((size_t) args_p->register_end + (size_t) args_p->stack_limit) * sizeof (ecma_value_t);
}
size_t total_size = JERRY_ALIGNUP (sizeof (vm_executable_object_t) + size, sizeof (uintptr_t));
ecma_object_t *object_p = ecma_create_object (ecma_builtin_get (ECMA_BUILTIN_ID_GENERATOR_PROTOTYPE),
total_size,
ECMA_OBJECT_TYPE_CLASS);
vm_executable_object_t *executable_object_p = (vm_executable_object_t *) object_p;
executable_object_p->extended_object.u.class_prop.class_id = LIT_MAGIC_STRING_GENERATOR_UL;
executable_object_p->extended_object.u.class_prop.extra_info = 0;
JERRY_ASSERT (!frame_ctx_p->is_eval_code);
JERRY_ASSERT (frame_ctx_p->context_depth == 0);
vm_frame_ctx_t *new_frame_ctx_p = &(executable_object_p->frame_ctx);
*new_frame_ctx_p = *frame_ctx_p;
/* The old register values are discarded. */
ecma_value_t *new_registers_p = VM_GET_REGISTERS (new_frame_ctx_p);
memcpy (new_registers_p, VM_GET_REGISTERS (frame_ctx_p), size);
size_t stack_top = (size_t) (frame_ctx_p->stack_top_p - VM_GET_REGISTERS (frame_ctx_p));
ecma_value_t *new_stack_top_p = new_registers_p + stack_top;
new_frame_ctx_p->stack_top_p = new_stack_top_p;
/* Initial state is "not running", so all object references are released. */
while (new_registers_p < new_stack_top_p)
{
ecma_deref_if_object (*new_registers_p++);
}
new_frame_ctx_p->this_binding = ecma_copy_value_if_not_object (new_frame_ctx_p->this_binding);
JERRY_CONTEXT (vm_top_context_p) = new_frame_ctx_p->prev_context_p;
return ecma_make_object_value (object_p);
} /* opfunc_create_executable_object */
/**
* Resume the execution of an inactive executable object
*
* @return value provided by the execution
*/
ecma_value_t
opfunc_resume_executable_object (vm_executable_object_t *executable_object_p, /**< executable object */
ecma_value_t value) /**< value pushed onto the stack */
{
const ecma_compiled_code_t *bytecode_header_p = executable_object_p->frame_ctx.bytecode_header_p;
ecma_value_t *register_p = VM_GET_REGISTERS (&executable_object_p->frame_ctx);
ecma_value_t *register_end_p;
if (bytecode_header_p->status_flags & CBC_CODE_FLAGS_UINT16_ARGUMENTS)
{
cbc_uint16_arguments_t *args_p = (cbc_uint16_arguments_t *) bytecode_header_p;
register_end_p = register_p + args_p->register_end;
}
else
{
cbc_uint8_arguments_t *args_p = (cbc_uint8_arguments_t *) bytecode_header_p;
register_end_p = register_p + args_p->register_end;
}
while (register_p < register_end_p)
{
ecma_ref_if_object (*register_p++);
}
if (executable_object_p->frame_ctx.context_depth > 0)
{
vm_ref_lex_env_chain (executable_object_p->frame_ctx.lex_env_p,
executable_object_p->frame_ctx.context_depth,
register_p,
true);
register_p += executable_object_p->frame_ctx.context_depth;
}
ecma_value_t *stack_top_p = executable_object_p->frame_ctx.stack_top_p;
while (register_p < stack_top_p)
{
ecma_ref_if_object (*register_p++);
}
uint8_t *byte_code_p = executable_object_p->frame_ctx.byte_code_p;
JERRY_ASSERT (byte_code_p[0] == CBC_EXT_OPCODE && byte_code_p[1] == CBC_EXT_CONTINUE_EXEC);
*register_p++ = ecma_copy_value (value);
executable_object_p->frame_ctx.stack_top_p = register_p;
JERRY_ASSERT (ECMA_EXECUTABLE_OBJECT_IS_SUSPENDED (executable_object_p->extended_object.u.class_prop.extra_info));
executable_object_p->extended_object.u.class_prop.extra_info |= ECMA_EXECUTABLE_OBJECT_RUNNING;
executable_object_p->frame_ctx.prev_context_p = JERRY_CONTEXT (vm_top_context_p);
JERRY_CONTEXT (vm_top_context_p) = &executable_object_p->frame_ctx;
ecma_value_t result = vm_execute (&executable_object_p->frame_ctx);
executable_object_p->extended_object.u.class_prop.extra_info &= (uint16_t) ~ECMA_EXECUTABLE_OBJECT_RUNNING;
if (executable_object_p->frame_ctx.call_operation != VM_EXEC_RETURN)
{
JERRY_ASSERT (executable_object_p->frame_ctx.call_operation == VM_NO_EXEC_OP);
/* All resources are released. */
executable_object_p->extended_object.u.class_prop.extra_info |= ECMA_EXECUTABLE_OBJECT_COMPLETED;
return result;
}
JERRY_CONTEXT (vm_top_context_p) = executable_object_p->frame_ctx.prev_context_p;
register_p = VM_GET_REGISTERS (&executable_object_p->frame_ctx);
while (register_p < register_end_p)
{
ecma_deref_if_object (*register_p++);
}
if (executable_object_p->frame_ctx.context_depth > 0)
{
vm_ref_lex_env_chain (executable_object_p->frame_ctx.lex_env_p,
executable_object_p->frame_ctx.context_depth,
register_p,
false);
register_p += executable_object_p->frame_ctx.context_depth;
}
stack_top_p = executable_object_p->frame_ctx.stack_top_p;
while (register_p < stack_top_p)
{
ecma_deref_if_object (*register_p++);
}
return result;
} /* opfunc_resume_executable_object */
#endif /* ENABLED (JERRY_ES2015) */
/**
* @}
* @}
+10 -2
View File
@@ -101,12 +101,20 @@ vm_op_delete_var (ecma_value_t name_literal, ecma_object_t *lex_env_p);
ecma_collection_t *
opfunc_for_in (ecma_value_t left_value, ecma_value_t *result_obj_p);
#if ENABLED (JERRY_ES2015)
ecma_collection_t *
opfunc_spread_arguments (ecma_value_t *stack_top_p, uint8_t argument_list_len);
#endif /* ENABLED (JERRY_ES2015) */
ecma_value_t
opfunc_append_array (ecma_value_t *stack_top_p, uint16_t values_length);
#if ENABLED (JERRY_ES2015)
ecma_collection_t *
opfunc_spread_arguments (ecma_value_t *stack_top_p, uint8_t argument_list_len);
ecma_value_t
opfunc_create_executable_object (vm_frame_ctx_t *frame_ctx_p);
ecma_value_t
opfunc_resume_executable_object (vm_executable_object_t *executable_object_p, ecma_value_t value);
#endif /* ENABLED (JERRY_ES2015) */
/**
+11 -2
View File
@@ -64,12 +64,21 @@ typedef struct vm_frame_ctx_t
/**
* Get register list corresponding to the frame context.
*/
#define VM_GET_REGISTERS(frame_ctx_p) ((ecma_value_t *) (frame_ctx_p + 1))
#define VM_GET_REGISTERS(frame_ctx_p) ((ecma_value_t *) ((frame_ctx_p) + 1))
/**
* Read or write a specific register.
*/
#define VM_GET_REGISTER(frame_ctx_p, i) (((ecma_value_t *) (frame_ctx_p + 1))[i])
#define VM_GET_REGISTER(frame_ctx_p, i) (((ecma_value_t *) ((frame_ctx_p) + 1))[i])
/**
* Generator frame context.
*/
typedef struct
{
ecma_extended_object_t extended_object; /**< extended object part */
vm_frame_ctx_t frame_ctx; /**< frame context part */
} vm_executable_object_t;
/**
* @}
+107 -18
View File
@@ -56,35 +56,21 @@ vm_stack_context_abort (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
case VM_CONTEXT_FINALLY_RETURN:
{
ecma_free_value (vm_stack_top_p[-2]);
VM_MINUS_EQUAL_U16 (frame_ctx_p->context_depth, PARSER_TRY_CONTEXT_STACK_ALLOCATION);
vm_stack_top_p -= PARSER_TRY_CONTEXT_STACK_ALLOCATION;
break;
/* FALLTHRU */
}
case VM_CONTEXT_FINALLY_JUMP:
case VM_CONTEXT_TRY:
case VM_CONTEXT_CATCH:
{
VM_MINUS_EQUAL_U16 (frame_ctx_p->context_depth, PARSER_TRY_CONTEXT_STACK_ALLOCATION);
vm_stack_top_p -= PARSER_TRY_CONTEXT_STACK_ALLOCATION;
break;
}
case VM_CONTEXT_CATCH:
{
JERRY_ASSERT (PARSER_TRY_CONTEXT_STACK_ALLOCATION > PARSER_WITH_CONTEXT_STACK_ALLOCATION);
const uint16_t size_diff = PARSER_TRY_CONTEXT_STACK_ALLOCATION - PARSER_WITH_CONTEXT_STACK_ALLOCATION;
VM_MINUS_EQUAL_U16 (frame_ctx_p->context_depth, size_diff);
vm_stack_top_p -= size_diff;
/* FALLTHRU */
}
#if ENABLED (JERRY_ES2015)
case VM_CONTEXT_BLOCK:
#endif /* ENABLED (JERRY_ES2015) */
case VM_CONTEXT_WITH:
#if ENABLED (JERRY_ES2015)
case VM_CONTEXT_SUPER_CLASS:
#endif /* ENABLED (JERRY_ES2015) */
case VM_CONTEXT_WITH:
{
VM_MINUS_EQUAL_U16 (frame_ctx_p->context_depth, PARSER_WITH_CONTEXT_STACK_ALLOCATION);
vm_stack_top_p -= PARSER_WITH_CONTEXT_STACK_ALLOCATION;
@@ -116,7 +102,7 @@ vm_stack_context_abort (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
ecma_collection_destroy (collection_p);
ecma_deref_object (ecma_get_object_from_value (vm_stack_top_p[-4]));
ecma_free_value (vm_stack_top_p[-4]);
VM_MINUS_EQUAL_U16 (frame_ctx_p->context_depth, PARSER_FOR_IN_CONTEXT_STACK_ALLOCATION);
vm_stack_top_p -= PARSER_FOR_IN_CONTEXT_STACK_ALLOCATION;
@@ -296,6 +282,109 @@ vm_stack_find_finally (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
return false;
} /* vm_stack_find_finally */
#if ENABLED (JERRY_ES2015)
/**
* Get the offsets of ecma values from the specified item of a context.
*
* @return array of offsets, last item represents the size of the context item
*/
uint32_t
vm_get_context_value_offsets (ecma_value_t *context_item_p) /**< any item of a context */
{
switch (VM_GET_CONTEXT_TYPE (context_item_p[-1]))
{
case VM_CONTEXT_FINALLY_THROW:
case VM_CONTEXT_FINALLY_RETURN:
{
return (2 << (VM_CONTEXT_OFFSET_SHIFT)) | PARSER_TRY_CONTEXT_STACK_ALLOCATION;
}
case VM_CONTEXT_FINALLY_JUMP:
case VM_CONTEXT_TRY:
case VM_CONTEXT_CATCH:
{
return PARSER_TRY_CONTEXT_STACK_ALLOCATION;
}
#if ENABLED (JERRY_ES2015)
case VM_CONTEXT_BLOCK:
case VM_CONTEXT_SUPER_CLASS:
#endif /* ENABLED (JERRY_ES2015) */
case VM_CONTEXT_WITH:
{
return PARSER_WITH_CONTEXT_STACK_ALLOCATION;
}
#if ENABLED (JERRY_ES2015)
case VM_CONTEXT_FOR_OF:
{
return ((3 << (VM_CONTEXT_OFFSET_SHIFT * 2))
| (2 << (VM_CONTEXT_OFFSET_SHIFT))
| PARSER_FOR_OF_CONTEXT_STACK_ALLOCATION);
}
#endif /* ENABLED (JERRY_ES2015) */
default:
{
return (4 << (VM_CONTEXT_OFFSET_SHIFT)) | PARSER_FOR_IN_CONTEXT_STACK_ALLOCATION;
}
}
} /* vm_get_context_value_offsets */
/**
* Ref / deref lexical environments in the chain using the current context.
*/
void
vm_ref_lex_env_chain (ecma_object_t *lex_env_p, /**< top of lexical environment */
uint16_t context_depth, /**< depth of function context */
ecma_value_t *context_end_p, /**< end of function context */
bool do_ref) /**< ref or deref lexical environments */
{
ecma_value_t *context_top_p = context_end_p + context_depth;
JERRY_ASSERT (context_top_p > context_end_p);
do
{
if (context_top_p[-1] & VM_CONTEXT_HAS_LEX_ENV)
{
JERRY_ASSERT (lex_env_p->u2.outer_reference_cp != JMEM_CP_NULL);
ecma_object_t *next_lex_env_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, lex_env_p->u2.outer_reference_cp);
if (do_ref)
{
ecma_ref_object (lex_env_p);
}
else
{
ecma_deref_object (lex_env_p);
}
lex_env_p = next_lex_env_p;
}
uint32_t offsets = vm_get_context_value_offsets (context_top_p);
while (VM_CONTEXT_HAS_NEXT_OFFSET (offsets))
{
int32_t offset = VM_CONTEXT_GET_NEXT_OFFSET (offsets);
if (do_ref)
{
ecma_ref_if_object (context_top_p[offset]);
}
else
{
ecma_deref_if_object (context_top_p[offset]);
}
offsets >>= VM_CONTEXT_OFFSET_SHIFT;
}
JERRY_ASSERT (context_top_p >= context_end_p + offsets);
context_top_p -= offsets;
}
while (context_top_p > context_end_p);
} /* vm_ref_lex_env_chain */
#endif /* ENABLED (JERRY_ES2015) */
/**
* @}
* @}
+18
View File
@@ -79,9 +79,27 @@ typedef enum
#define VM_CONTEXT_IS_FINALLY(context_type) \
((context_type) <= VM_CONTEXT_FINALLY_RETURN)
/**
* Shift needs to be applied to get the next item of the offset array.
*/
#define VM_CONTEXT_OFFSET_SHIFT 4
/**
* Checks whether an offset is available.
*/
#define VM_CONTEXT_HAS_NEXT_OFFSET(offsets) ((offsets) >= (1 << VM_CONTEXT_OFFSET_SHIFT))
/**
* Gets the next offset from the offset array.
*/
#define VM_CONTEXT_GET_NEXT_OFFSET(offsets) (-((int32_t) ((offsets) & ((1 << VM_CONTEXT_OFFSET_SHIFT) - 1))))
ecma_value_t *vm_stack_context_abort (vm_frame_ctx_t *frame_ctx_p, ecma_value_t *vm_stack_top_p);
bool vm_stack_find_finally (vm_frame_ctx_t *frame_ctx_p, ecma_value_t **vm_stack_top_ref_p,
vm_stack_context_type_t finally_type, uint32_t search_limit);
uint32_t vm_get_context_value_offsets (ecma_value_t *context_item_p);
void vm_ref_lex_env_chain (ecma_object_t *lex_env_p, uint16_t context_depth,
ecma_value_t *context_end_p, bool do_ref);
/**
* @}
+47 -3
View File
@@ -2062,7 +2062,47 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
frame_ctx_p->stack_top_p = stack_top_p;
return ECMA_VALUE_UNDEFINED;
}
#endif /* ENABLED (JERRY_ES2015) */
case VM_OC_CREATE_GENERATOR:
{
frame_ctx_p->call_operation = VM_EXEC_RETURN;
frame_ctx_p->byte_code_p = byte_code_p;
frame_ctx_p->stack_top_p = stack_top_p;
return opfunc_create_executable_object (frame_ctx_p);
}
case VM_OC_YIELD:
{
frame_ctx_p->call_operation = VM_EXEC_RETURN;
frame_ctx_p->byte_code_p = byte_code_p;
frame_ctx_p->stack_top_p = stack_top_p;
return left_value;
}
case VM_OC_CONTINUE_EXEC:
{
if (JERRY_UNLIKELY (frame_ctx_p->call_operation == ECMA_GENERATOR_RETURN))
{
ecma_value_t *stack_bottom_p = VM_GET_REGISTERS (frame_ctx_p) + register_end + frame_ctx_p->context_depth;
result = *(--stack_top_p);
while (stack_top_p > stack_bottom_p)
{
ecma_fast_free_value (*(--stack_top_p));
}
goto error;
}
if (JERRY_UNLIKELY (frame_ctx_p->call_operation == ECMA_GENERATOR_THROW))
{
JERRY_CONTEXT (error_value) = *(--stack_top_p);
JERRY_CONTEXT (status_flags) |= ECMA_STATUS_EXCEPTION;
result = ECMA_VALUE_ERROR;
goto error;
}
continue;
}
#endif /* ENABLED (JERRY_ES2015) */
case VM_OC_PUSH_ELISON:
{
*stack_top_p++ = ECMA_VALUE_ARRAY_HOLE;
@@ -3804,8 +3844,8 @@ free_both_values:
free_left_value:
ecma_fast_free_value (left_value);
}
error:
error:
ecma_fast_free_value (left_value);
ecma_fast_free_value (right_value);
@@ -4031,7 +4071,7 @@ vm_init_exec (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
*
* @return ecma value
*/
static ecma_value_t JERRY_ATTR_NOINLINE
ecma_value_t JERRY_ATTR_NOINLINE
vm_execute (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
{
while (true)
@@ -4056,6 +4096,10 @@ vm_execute (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
vm_spread_operation (frame_ctx_p);
break;
}
case VM_EXEC_RETURN:
{
return completion_value;
}
#endif /* ENABLED (JERRY_ES2015) */
case VM_EXEC_CONSTRUCT:
{
+8
View File
@@ -255,6 +255,9 @@ typedef enum
VM_OC_REST_INITIALIZER, /**< create rest object inside an array pattern */
VM_OC_INITIALIZER_PUSH_PROP, /**< push property for object initializer */
VM_OC_SPREAD_ARGUMENTS, /**< perform function call/construct with spreaded arguments */
VM_OC_CREATE_GENERATOR, /**< create a generator object */
VM_OC_YIELD, /**< yield operation */
VM_OC_CONTINUE_EXEC, /**< first byte code after a function is resumed */
#endif /* ENABLED (JERRY_ES2015) */
VM_OC_NONE, /**< a special opcode for unsupported byte codes */
} vm_oc_types;
@@ -305,6 +308,9 @@ typedef enum
VM_OC_REST_INITIALIZER = VM_OC_NONE, /**< create rest object inside an array pattern */
VM_OC_INITIALIZER_PUSH_PROP = VM_OC_NONE, /**< push property for object initializer */
VM_OC_SPREAD_ARGUMENTS = VM_OC_NONE, /**< perform function call/construct with spreaded arguments */
VM_OC_CREATE_GENERATOR = VM_OC_NONE, /**< create a generator object */
VM_OC_YIELD = VM_OC_NONE, /**< yield operation */
VM_OC_CONTINUE_EXEC = VM_OC_NONE, /**< first byte code after a function is resumed */
#endif /* !ENABLED (JERRY_ES2015) */
VM_OC_UNUSED = VM_OC_NONE /**< placeholder if the list is empty */
@@ -388,6 +394,7 @@ typedef enum
VM_EXEC_CALL, /**< invoke a function */
VM_EXEC_SUPER_CALL, /**< invoke a function through 'super' keyword */
VM_EXEC_SPREAD_OP, /**< call/construct operation with spreaded argument list */
VM_EXEC_RETURN, /**< return with the completion value without freeing registers */
VM_EXEC_CONSTRUCT, /**< construct a new object */
} vm_call_operation;
@@ -400,6 +407,7 @@ ecma_value_t vm_run_module (const ecma_compiled_code_t *bytecode_p, ecma_object_
ecma_value_t vm_run (const ecma_compiled_code_t *bytecode_header_p, ecma_value_t this_binding_value,
ecma_object_t *lex_env_p, const ecma_value_t *arg_list_p, ecma_length_t arg_list_len);
ecma_value_t vm_execute (vm_frame_ctx_t *frame_ctx_p);
bool vm_is_strict_mode (void);
bool vm_is_direct_eval_form_call (void);