Rework the core of class parsing/runtime semantic (#3598)

Changes:
 - Use the pre-scanner to provide information for the parser about the existence of the class constructor
 - The allocation of the super declarative environment is no longer needed
 - The VM frame context holds the information about the this binding status
 - Reduce the number of class related VM/CBC instructions
 - Improve ecma_op_function_{construct, call} to properly set new.target

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
This commit is contained in:
Robert Fancsik
2020-03-16 14:37:47 +01:00
committed by GitHub
parent 56b9f098ab
commit bfd2639634
49 changed files with 1671 additions and 1490 deletions
+461
View File
@@ -27,6 +27,7 @@
#include "ecma-lex-env.h"
#include "ecma-objects.h"
#include "ecma-promise-object.h"
#include "ecma-proxy-object.h"
#include "ecma-try-catch-macro.h"
#include "jcontext.h"
#include "opcodes.h"
@@ -756,6 +757,466 @@ opfunc_return_promise (ecma_value_t value) /**< value */
return result;
} /* opfunc_return_promise */
/**
* Implicit class constructor handler when the classHeritage is not present.
*
* See also: ECMAScript v6, 14.5.14.10.b.i
*
* @return ECMA_VALUE_ERROR - if the function was invoked without 'new'
* ECMA_VALUE_UNDEFINED - otherwise
*/
static ecma_value_t
ecma_op_implicit_constructor_handler_cb (const ecma_value_t function_obj, /**< the function itself */
const ecma_value_t this_val, /**< this_arg of the function */
const ecma_value_t args_p[], /**< argument list */
const ecma_length_t args_count) /**< argument number */
{
JERRY_UNUSED_4 (function_obj, this_val, args_p, args_count);
if (JERRY_CONTEXT (current_new_target) == NULL)
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Class constructor cannot be invoked without 'new'."));
}
JERRY_ASSERT (JERRY_CONTEXT (current_new_target) != JERRY_CONTEXT_INVALID_NEW_TARGET);
return ECMA_VALUE_UNDEFINED;
} /* ecma_op_implicit_constructor_handler_cb */
/**
* Implicit class constructor handler when the classHeritage is present.
*
* See also: ECMAScript v6, 14.5.14.10.a.i
*
* @return ECMA_VALUE_ERROR - if the operation fails
* result of the super call - otherwise
*/
static ecma_value_t
ecma_op_implicit_constructor_handler_heritage_cb (const ecma_value_t function_obj, /**< the function itself */
const ecma_value_t this_val, /**< this_arg of the function */
const ecma_value_t args_p[], /**< argument list */
const ecma_length_t args_count) /**< argument number */
{
JERRY_UNUSED_4 (function_obj, this_val, args_p, args_count);
if (JERRY_CONTEXT (current_new_target) == NULL)
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Class constructor cannot be invoked without 'new'."));
}
JERRY_ASSERT (JERRY_CONTEXT (current_new_target) != JERRY_CONTEXT_INVALID_NEW_TARGET);
ecma_object_t *func_obj_p = ecma_get_object_from_value (function_obj);
ecma_value_t super_ctor = ecma_op_function_get_super_constructor (func_obj_p);
if (ECMA_IS_VALUE_ERROR (super_ctor))
{
return super_ctor;
}
ecma_object_t *super_ctor_p = ecma_get_object_from_value (super_ctor);
ecma_value_t result = ecma_op_function_construct (super_ctor_p,
JERRY_CONTEXT (current_new_target),
args_p,
args_count);
if (ecma_is_value_object (result))
{
ecma_value_t proto_value = ecma_op_object_get_by_magic_id (JERRY_CONTEXT (current_new_target),
LIT_MAGIC_STRING_PROTOTYPE);
if (ECMA_IS_VALUE_ERROR (proto_value))
{
ecma_free_value (result);
result = ECMA_VALUE_ERROR;
}
else if (ecma_is_value_object (proto_value))
{
ECMA_SET_POINTER (ecma_get_object_from_value (result)->u2.prototype_cp,
ecma_get_object_from_value (proto_value));
}
ecma_free_value (proto_value);
}
ecma_deref_object (super_ctor_p);
return result;
} /* ecma_op_implicit_constructor_handler_heritage_cb */
/**
* Create implicit class constructor
*
* See also: ECMAScript v6, 14.5.14
*
* @return - new external function ecma-object
*/
ecma_value_t
opfunc_create_implicit_class_constructor (uint8_t opcode) /**< current cbc opcode */
{
/* 8. */
ecma_object_t *func_obj_p = ecma_create_object (ecma_builtin_get (ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE),
sizeof (ecma_extended_object_t),
ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION);
ecma_extended_object_t *ext_func_obj_p = (ecma_extended_object_t *) func_obj_p;
/* 10.a.i */
if (opcode == CBC_EXT_PUSH_IMPLICIT_CONSTRUCTOR_HERITAGE)
{
ext_func_obj_p->u.external_handler_cb = ecma_op_implicit_constructor_handler_heritage_cb;
}
/* 10.b.i */
else
{
ext_func_obj_p->u.external_handler_cb = ecma_op_implicit_constructor_handler_cb;
}
return ecma_make_object_value (func_obj_p);
} /* opfunc_create_implicit_class_constructor */
/**
* Set the [[HomeObject]] attribute of the given functon object
*/
static inline void JERRY_ATTR_ALWAYS_INLINE
opfunc_set_home_object (ecma_object_t *func_p, /**< function object */
ecma_object_t *parent_env_p) /**< parent environment */
{
if (ecma_get_object_type (func_p) == ECMA_OBJECT_TYPE_FUNCTION)
{
JERRY_ASSERT (!ecma_get_object_is_builtin (func_p));
ECMA_SET_NON_NULL_POINTER_TAG (((ecma_extended_object_t *) func_p)->u.function.scope_cp, parent_env_p, 0);
}
} /* opfunc_set_home_object */
/**
* ClassDefinitionEvaluation environment initialization part
*
* See also: ECMAScript v6, 14.5.14
*
* @return - ECMA_VALUE_ERROR - if the operation fails
* ECMA_VALUE_EMPTY - otherwise
*/
void
opfunc_push_class_environment (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
ecma_value_t **vm_stack_top, /**< VM stack top */
ecma_value_t class_name) /**< class name */
{
JERRY_ASSERT (ecma_is_value_undefined (class_name) || ecma_is_value_string (class_name));
ecma_object_t *class_env_p = ecma_create_decl_lex_env (frame_ctx_p->lex_env_p);
/* 4.a */
if (!ecma_is_value_undefined (class_name))
{
ecma_op_create_immutable_binding (class_env_p,
ecma_get_string_from_value (class_name),
ECMA_VALUE_UNINITIALIZED);
}
frame_ctx_p->lex_env_p = class_env_p;
*(*vm_stack_top)++ = ECMA_VALUE_RELEASE_LEX_ENV;
} /* opfunc_push_class_environment */
/**
* ClassDefinitionEvaluation object initialization part
*
* See also: ECMAScript v6, 14.5.14
*
* @return - ECMA_VALUE_ERROR - if the operation fails
* ECMA_VALUE_EMPTY - otherwise
*/
ecma_value_t
opfunc_init_class (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
ecma_value_t *stack_top_p) /**< stack top */
{
/* 5.b, 6.e.ii */
ecma_object_t *ctor_parent_p = ecma_builtin_get (ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE);
ecma_object_t *proto_parent_p = NULL;
bool free_proto_parent = false;
ecma_value_t super_class = stack_top_p[-2];
ecma_object_t *ctor_p = ecma_get_object_from_value (stack_top_p[-1]);
bool heritage_present = !ecma_is_value_array_hole (super_class);
/* 5. ClassHeritage opt is not present */
if (!heritage_present)
{
/* 5.a */
proto_parent_p = ecma_builtin_get (ECMA_BUILTIN_ID_OBJECT_PROTOTYPE);
}
else if (!ecma_is_value_null (super_class))
{
/* 6.f, 6.g.i */
if (!ecma_is_constructor (super_class)
|| ecma_op_function_is_generator (ecma_get_object_from_value (super_class)))
{
return ecma_raise_type_error ("Class extends value is not a constructor or null");
}
ecma_object_t *parent_p = ecma_get_object_from_value (super_class);
/* 6.g.ii */
ecma_value_t proto_parent = ecma_op_object_get_by_magic_id (parent_p, LIT_MAGIC_STRING_PROTOTYPE);
/* 6.g.iii */
if (ECMA_IS_VALUE_ERROR (proto_parent))
{
return proto_parent;
}
/* 6.g.iv */
if (ecma_is_value_object (proto_parent))
{
proto_parent_p = ecma_get_object_from_value (proto_parent);
free_proto_parent = true;
}
else if (ecma_is_value_null (proto_parent))
{
proto_parent_p = NULL;
}
else
{
return ecma_raise_type_error ("Property 'prototype' is not an object or null");
}
/* 6.g.v */
ctor_parent_p = parent_p;
}
/* 7. */
ecma_object_t *proto_p = ecma_create_object (proto_parent_p, 0, ECMA_OBJECT_TYPE_GENERAL);
ecma_value_t proto = ecma_make_object_value (proto_p);
ECMA_SET_POINTER (ctor_p->u2.prototype_cp, ctor_parent_p);
if (free_proto_parent)
{
ecma_deref_object (proto_parent_p);
}
ecma_free_value (super_class);
/* 16. */
ecma_property_value_t *property_value_p;
property_value_p = ecma_create_named_data_property (ctor_p,
ecma_get_magic_string (LIT_MAGIC_STRING_PROTOTYPE),
ECMA_PROPERTY_FIXED,
NULL);
property_value_p->value = proto;
/* 18. */
property_value_p = ecma_create_named_data_property (proto_p,
ecma_get_magic_string (LIT_MAGIC_STRING_CONSTRUCTOR),
ECMA_PROPERTY_CONFIGURABLE_WRITABLE,
NULL);
property_value_p->value = ecma_make_object_value (ctor_p);
if (ecma_get_object_type (ctor_p) == ECMA_OBJECT_TYPE_FUNCTION)
{
ecma_object_t *proto_env_p = ecma_create_object_lex_env (frame_ctx_p->lex_env_p,
proto_p,
ECMA_LEXICAL_ENVIRONMENT_HOME_OBJECT_BOUND);
ECMA_SET_NON_NULL_POINTER_TAG (((ecma_extended_object_t *) ctor_p)->u.function.scope_cp, proto_env_p, 0);
/* 15. set Fs [[ConstructorKind]] internal slot to "derived". */
if (heritage_present)
{
ECMA_SET_THIRD_BIT_TO_POINTER_TAG (((ecma_extended_object_t *) ctor_p)->u.function.scope_cp);
}
ecma_deref_object (proto_env_p);
}
stack_top_p[-2] = stack_top_p[-1];
stack_top_p[-1] = proto;
return ECMA_VALUE_EMPTY;
} /* opfunc_init_class */
/**
* Set [[Enumerable]] and [[HomeObject]] attributes for all class method
*/
static void
opfunc_set_class_attributes (ecma_object_t *obj_p, /**< object */
ecma_object_t *parent_env_p) /**< parent environment */
{
jmem_cpointer_t prop_iter_cp = obj_p->u1.property_list_cp;
#if ENABLED (JERRY_PROPRETY_HASHMAP)
if (prop_iter_cp != JMEM_CP_NULL)
{
ecma_property_header_t *prop_iter_p = ECMA_GET_NON_NULL_POINTER (ecma_property_header_t, prop_iter_cp);
if (prop_iter_p->types[0] == ECMA_PROPERTY_TYPE_HASHMAP)
{
prop_iter_cp = prop_iter_p->next_property_cp;
}
}
#endif /* ENABLED (JERRY_PROPRETY_HASHMAP) */
while (prop_iter_cp != JMEM_CP_NULL)
{
ecma_property_header_t *prop_iter_p = ECMA_GET_NON_NULL_POINTER (ecma_property_header_t, prop_iter_cp);
JERRY_ASSERT (ECMA_PROPERTY_IS_PROPERTY_PAIR (prop_iter_p));
ecma_property_pair_t *property_pair_p = (ecma_property_pair_t *) prop_iter_p;
for (uint32_t index = 0; index < ECMA_PROPERTY_PAIR_ITEM_COUNT; index++)
{
uint8_t property = property_pair_p->header.types[index];
if (ECMA_PROPERTY_GET_TYPE (property) == ECMA_PROPERTY_TYPE_NAMEDDATA)
{
JERRY_ASSERT (ecma_is_value_object (property_pair_p->values[index].value));
if (ecma_is_property_enumerable (property))
{
property_pair_p->header.types[index] = (uint8_t) (property & ~ECMA_PROPERTY_FLAG_ENUMERABLE);
opfunc_set_home_object (ecma_get_object_from_value (property_pair_p->values[index].value), parent_env_p);
}
}
else if (ECMA_PROPERTY_GET_TYPE (property) == ECMA_PROPERTY_TYPE_NAMEDACCESSOR)
{
ecma_property_value_t *accessor_objs_p = property_pair_p->values + index;
ecma_getter_setter_pointers_t *get_set_pair_p = ecma_get_named_accessor_property (accessor_objs_p);
if (get_set_pair_p->getter_cp != JMEM_CP_NULL)
{
opfunc_set_home_object (ECMA_GET_NON_NULL_POINTER (ecma_object_t, get_set_pair_p->getter_cp), parent_env_p);
}
if (get_set_pair_p->setter_cp != JMEM_CP_NULL)
{
opfunc_set_home_object (ECMA_GET_NON_NULL_POINTER (ecma_object_t, get_set_pair_p->setter_cp), parent_env_p);
}
}
else
{
JERRY_ASSERT (ECMA_PROPERTY_GET_TYPE (property) == ECMA_PROPERTY_TYPE_SPECIAL);
JERRY_ASSERT (property == ECMA_PROPERTY_TYPE_HASHMAP
|| property == ECMA_PROPERTY_TYPE_DELETED);
}
}
prop_iter_cp = prop_iter_p->next_property_cp;
}
} /* opfunc_set_class_attributes */
/**
* Pop the current lexical environment referenced by the frame context
*/
void
opfunc_pop_lexical_environment (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
{
ecma_object_t *outer_env_p = ECMA_GET_NON_NULL_POINTER (ecma_object_t, frame_ctx_p->lex_env_p->u2.outer_reference_cp);
ecma_deref_object (frame_ctx_p->lex_env_p);
frame_ctx_p->lex_env_p = outer_env_p;
} /* opfunc_pop_lexical_environment */
/**
* ClassDefinitionEvaluation finalization part
*
* See also: ECMAScript v6, 14.5.14
*/
void
opfunc_finalize_class (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
ecma_value_t **vm_stack_top_p, /**< current vm stack top */
ecma_value_t class_name) /**< class name */
{
JERRY_ASSERT (ecma_is_value_undefined (class_name) || ecma_is_value_string (class_name));
ecma_value_t *stack_top_p = *vm_stack_top_p;
ecma_object_t *ctor_p = ecma_get_object_from_value (stack_top_p[-2]);
ecma_object_t *proto_p = ecma_get_object_from_value (stack_top_p[-1]);
ecma_object_t *class_env_p = frame_ctx_p->lex_env_p;
/* 23.a */
if (!ecma_is_value_undefined (class_name))
{
ecma_op_initialize_binding (class_env_p, ecma_get_string_from_value (class_name), stack_top_p[-2]);
}
ecma_object_t *ctor_env_p = ecma_create_object_lex_env (class_env_p,
ctor_p,
ECMA_LEXICAL_ENVIRONMENT_HOME_OBJECT_BOUND);
ecma_object_t *proto_env_p = ecma_create_object_lex_env (class_env_p,
proto_p,
ECMA_LEXICAL_ENVIRONMENT_HOME_OBJECT_BOUND);
opfunc_set_class_attributes (ctor_p, ctor_env_p);
opfunc_set_class_attributes (proto_p, proto_env_p);
ecma_deref_object (proto_env_p);
ecma_deref_object (ctor_env_p);
opfunc_pop_lexical_environment (frame_ctx_p);
ecma_deref_object (proto_p);
/* only the current class remains on the stack */
JERRY_ASSERT (stack_top_p[-3] == ECMA_VALUE_RELEASE_LEX_ENV);
stack_top_p[-3] = stack_top_p[-2];
*vm_stack_top_p -= 2;
} /* opfunc_finalize_class */
/**
* MakeSuperPropertyReference operation
*
* See also: ECMAScript v6, 12.3.5.3
*
* @return ECMA_VALUE_ERROR - if the operation fails
* ECMA_VALUE_EMPTY - otherwise
*/
ecma_value_t
opfunc_form_super_reference (ecma_value_t **vm_stack_top_p, /**< current vm stack top */
vm_frame_ctx_t *frame_ctx_p, /**< frame context */
ecma_value_t prop_name, /**< property name to resolve */
uint8_t opcode) /**< current cbc opcode */
{
ecma_value_t parent = ecma_op_resolve_super_base (frame_ctx_p->lex_env_p);
if (ECMA_IS_VALUE_ERROR (parent))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Cannot invoke nullable super method."));
}
if (ECMA_IS_VALUE_ERROR (ecma_op_check_object_coercible (parent)))
{
return ECMA_VALUE_ERROR;
}
ecma_object_t *parent_p = ecma_get_object_from_value (parent);
ecma_string_t *prop_name_p = ecma_op_to_prop_name (prop_name);
if (prop_name_p == NULL)
{
ecma_deref_object (parent_p);
return ECMA_VALUE_ERROR;
}
ecma_value_t result = ecma_op_object_get_with_receiver (parent_p, prop_name_p, frame_ctx_p->this_binding);
ecma_deref_ecma_string (prop_name_p);
ecma_deref_object (parent_p);
if (ECMA_IS_VALUE_ERROR (result))
{
return result;
}
ecma_value_t *stack_top_p = *vm_stack_top_p;
if (opcode == CBC_EXT_SUPER_PROP_LITERAL_CALL_REFERENCE || opcode == CBC_EXT_SUPER_PROP_CALL_REFERENCE)
{
*stack_top_p++ = ecma_copy_value (frame_ctx_p->this_binding);
*stack_top_p++ = ECMA_VALUE_UNDEFINED;
}
*stack_top_p++ = result;
*vm_stack_top_p = stack_top_p;
return ECMA_VALUE_EMPTY;
} /* opfunc_form_super_reference */
#endif /* ENABLED (JERRY_ES2015) */
/**
+19
View File
@@ -118,6 +118,25 @@ opfunc_resume_executable_object (vm_executable_object_t *executable_object_p, ec
ecma_value_t
opfunc_return_promise (ecma_value_t value);
ecma_value_t
opfunc_create_implicit_class_constructor (uint8_t opcode);
void
opfunc_push_class_environment (vm_frame_ctx_t *frame_ctx_p, ecma_value_t **vm_stack_top, ecma_value_t class_name);
ecma_value_t
opfunc_init_class (vm_frame_ctx_t *frame_context_p, ecma_value_t *stack_top_p);
void
opfunc_pop_lexical_environment (vm_frame_ctx_t *frame_ctx_p);
void
opfunc_finalize_class (vm_frame_ctx_t *frame_ctx_p, ecma_value_t **vm_stack_top_p, ecma_value_t class_name);
ecma_value_t
opfunc_form_super_reference (ecma_value_t **vm_stack_top_p, vm_frame_ctx_t *frame_ctx_p, ecma_value_t prop_name,
uint8_t opcode);
#endif /* ENABLED (JERRY_ES2015) */
/**
-4
View File
@@ -29,8 +29,6 @@
JERRY_STATIC_ASSERT (PARSER_WITH_CONTEXT_STACK_ALLOCATION == PARSER_BLOCK_CONTEXT_STACK_ALLOCATION,
parser_with_context_stack_allocation_must_be_equal_to_parser_block_context_stack_allocation);
JERRY_STATIC_ASSERT (PARSER_WITH_CONTEXT_STACK_ALLOCATION == PARSER_SUPER_CLASS_CONTEXT_STACK_ALLOCATION,
parser_with_context_stack_allocation_must_be_equal_to_parser_super_class_context_stack_allocation);
/**
* Abort (finalize) the current stack context, and remove it.
@@ -69,7 +67,6 @@ vm_stack_context_abort (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
}
#if ENABLED (JERRY_ES2015)
case VM_CONTEXT_BLOCK:
case VM_CONTEXT_SUPER_CLASS:
#endif /* ENABLED (JERRY_ES2015) */
case VM_CONTEXT_WITH:
{
@@ -315,7 +312,6 @@ vm_get_context_value_offsets (ecma_value_t *context_item_p) /**< any item of a c
}
#if ENABLED (JERRY_ES2015)
case VM_CONTEXT_BLOCK:
case VM_CONTEXT_SUPER_CLASS:
#endif /* ENABLED (JERRY_ES2015) */
case VM_CONTEXT_WITH:
{
-1
View File
@@ -74,7 +74,6 @@ typedef enum
VM_CONTEXT_FOR_IN, /**< for-in context */
#if ENABLED (JERRY_ES2015)
VM_CONTEXT_FOR_OF, /**< for-of context */
VM_CONTEXT_SUPER_CLASS, /**< super class context */
#endif /* ENABLED (JERRY_ES2015) */
} vm_stack_context_type_t;
+82 -310
View File
@@ -548,24 +548,40 @@ vm_super_call (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
ecma_value_t func_value = *(--frame_ctx_p->stack_top_p);
ecma_value_t completion_value;
ecma_op_set_super_called (frame_ctx_p->lex_env_p);
ecma_value_t this_value = ecma_op_get_class_this_binding (frame_ctx_p->lex_env_p);
if (!ecma_is_constructor (func_value))
ecma_property_t *prop_p = ecma_op_get_this_property (frame_ctx_p->lex_env_p);
if (ecma_op_this_binding_is_initialized (prop_p))
{
completion_value = ecma_raise_reference_error (ECMA_ERR_MSG ("Super constructor may only be called once"));
}
else if (!ecma_is_constructor (func_value))
{
completion_value = ecma_raise_type_error (ECMA_ERR_MSG ("Class extends value is not a constructor."));
}
else
{
completion_value = ecma_op_function_construct (ecma_get_object_from_value (func_value),
this_value,
ecma_object_t *func_obj_p = ecma_get_object_from_value (func_value);
completion_value = ecma_op_function_construct (func_obj_p,
JERRY_CONTEXT (current_new_target),
arguments_p,
arguments_list_len);
if (this_value != completion_value && ecma_is_value_object (completion_value))
if (ecma_is_value_object (completion_value))
{
ecma_op_set_class_prototype (completion_value, this_value);
ecma_op_set_class_this_binding (frame_ctx_p->lex_env_p, completion_value);
ecma_value_t proto_value = ecma_op_object_get_by_magic_id (JERRY_CONTEXT (current_new_target),
LIT_MAGIC_STRING_PROTOTYPE);
if (ECMA_IS_VALUE_ERROR (proto_value))
{
ecma_free_value (completion_value);
completion_value = ECMA_VALUE_ERROR;
}
else if (ecma_is_value_object (proto_value))
{
ECMA_SET_POINTER (ecma_get_object_from_value (completion_value)->u2.prototype_cp,
ecma_get_object_from_value (proto_value));
}
ecma_free_value (proto_value);
}
}
@@ -591,6 +607,9 @@ vm_super_call (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
}
else
{
ecma_op_bind_this_value (prop_p, completion_value);
frame_ctx_p->this_binding = completion_value;
frame_ctx_p->byte_code_p = byte_code_p;
uint32_t opcode_data = vm_decode_table[(CBC_END + 1) + opcode];
@@ -641,7 +660,7 @@ vm_spread_operation (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
ecma_object_t *constructor_obj_p = ecma_get_object_from_value (func_value);
completion_value = ecma_op_function_construct (constructor_obj_p,
ECMA_VALUE_UNDEFINED,
constructor_obj_p,
collection_p->buffer_p,
collection_p->item_count);
}
@@ -829,7 +848,7 @@ opfunc_construct (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
ecma_object_t *constructor_obj_p = ecma_get_object_from_value (constructor_value);
completion_value = ecma_op_function_construct (constructor_obj_p,
ECMA_VALUE_UNDEFINED,
constructor_obj_p,
stack_top_p,
arguments_list_len);
}
@@ -1648,6 +1667,13 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
continue;
}
#if ENABLED (JERRY_ES2015)
case VM_OC_LOCAL_EVAL:
{
ECMA_CLEAR_LOCAL_PARSE_OPTS ();
uint8_t parse_opts = *byte_code_p++;
ECMA_SET_LOCAL_PARSE_OPTS (parse_opts);
continue;
}
case VM_OC_SUPER_CALL:
{
uint8_t arguments_list_len = *byte_code_p++;
@@ -1676,327 +1702,65 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
frame_ctx_p->stack_top_p = stack_top_p;
return ECMA_VALUE_UNDEFINED;
}
case VM_OC_CLASS_HERITAGE:
case VM_OC_PUSH_CLASS_ENVIRONMENT:
{
ecma_value_t super_value = *(--stack_top_p);
ecma_object_t *super_class_p;
branch_offset += (int32_t) (byte_code_start_p - frame_ctx_p->byte_code_start_p);
opfunc_push_class_environment (frame_ctx_p, &stack_top_p, left_value);
goto free_left_value;
}
case VM_OC_PUSH_IMPLICIT_CTOR:
{
*stack_top_p++ = opfunc_create_implicit_class_constructor (opcode);
continue;
}
case VM_OC_INIT_CLASS:
{
result = opfunc_init_class (frame_ctx_p, stack_top_p);
if (ecma_is_value_null (super_value))
if (ECMA_IS_VALUE_ERROR (result))
{
super_class_p = ecma_create_object (ecma_builtin_get (ECMA_BUILTIN_ID_OBJECT_PROTOTYPE),
0,
ECMA_OBJECT_TYPE_GENERAL);
goto error;
}
else
continue;
}
case VM_OC_FINALIZE_CLASS:
{
opfunc_finalize_class (frame_ctx_p, &stack_top_p, left_value);
goto free_left_value;
}
case VM_OC_PUSH_SUPER_CONSTRUCTOR:
{
result = ecma_op_function_get_super_constructor (JERRY_CONTEXT (current_function_obj_p));
if (ECMA_IS_VALUE_ERROR (result))
{
result = ecma_op_to_object (super_value);
ecma_free_value (super_value);
if (ECMA_IS_VALUE_ERROR (result) || !ecma_is_constructor (result))
{
if (ECMA_IS_VALUE_ERROR (result))
{
jcontext_release_exception ();
}
ecma_free_value (result);
result = ecma_raise_type_error ("Value provided by class extends is not an object or null.");
goto error;
}
else
{
super_class_p = ecma_get_object_from_value (result);
}
}
ecma_object_t *super_env_p = ecma_create_object_lex_env (frame_ctx_p->lex_env_p,
super_class_p,
ECMA_LEXICAL_ENVIRONMENT_SUPER_OBJECT_BOUND);
ecma_deref_object (super_class_p);
VM_PLUS_EQUAL_U16 (frame_ctx_p->context_depth, PARSER_SUPER_CLASS_CONTEXT_STACK_ALLOCATION);
stack_top_p += PARSER_SUPER_CLASS_CONTEXT_STACK_ALLOCATION;
stack_top_p[-1] = VM_CREATE_CONTEXT_WITH_ENV (VM_CONTEXT_SUPER_CLASS, branch_offset);
frame_ctx_p->lex_env_p = super_env_p;
continue;
}
case VM_OC_CLASS_INHERITANCE:
{
ecma_value_t child_value = stack_top_p[-2];
ecma_value_t child_prototype_value = stack_top_p[-1];
ecma_object_t *child_class_p = ecma_get_object_from_value (child_value);
ecma_object_t *child_prototype_class_p = ecma_get_object_from_value (child_prototype_value);
ecma_object_t *super_class_p = ecma_get_lex_env_binding_object (frame_ctx_p->lex_env_p);
if (super_class_p->u2.prototype_cp != JMEM_CP_NULL)
{
ecma_value_t super_prototype_value = ecma_op_object_get_by_magic_id (super_class_p,
LIT_MAGIC_STRING_PROTOTYPE);
if (ECMA_IS_VALUE_ERROR (super_prototype_value))
{
result = super_prototype_value;
goto error;
}
if (ecma_get_object_type (super_class_p) == ECMA_OBJECT_TYPE_BOUND_FUNCTION
&& !ecma_is_value_object (super_prototype_value))
{
ecma_free_value (super_prototype_value);
result = ecma_raise_type_error (ECMA_ERR_MSG ("Class extends value does not have valid "
"prototype property."));
goto error;
}
if (!(ECMA_IS_VALUE_ERROR (super_prototype_value) || !ecma_is_value_object (super_prototype_value)))
{
ecma_object_t *super_prototype_class_p = ecma_get_object_from_value (super_prototype_value);
ECMA_SET_NON_NULL_POINTER (child_prototype_class_p->u2.prototype_cp, super_prototype_class_p);
ECMA_SET_NON_NULL_POINTER (child_class_p->u2.prototype_cp, super_class_p);
}
ecma_free_value (super_prototype_value);
}
continue;
}
case VM_OC_PUSH_CLASS_CONSTRUCTOR_AND_PROTOTYPE:
{
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_FUNCTION_PROTOTYPE);
ecma_object_t *function_obj_p = ecma_create_object (prototype_obj_p,
sizeof (ecma_extended_object_t),
ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION);
ecma_extended_object_t *ext_func_obj_p = (ecma_extended_object_t *) function_obj_p;
ext_func_obj_p->u.external_handler_cb = ecma_op_function_implicit_constructor_handler_cb;
ecma_value_t function_obj_value = ecma_make_object_value (function_obj_p);
*stack_top_p++ = function_obj_value;
ecma_object_t *prototype_class_p = ecma_create_object (ecma_builtin_get (ECMA_BUILTIN_ID_OBJECT_PROTOTYPE),
0,
ECMA_OBJECT_TYPE_GENERAL);
*stack_top_p++ = ecma_make_object_value (prototype_class_p);
/* 14.5.14.18 Set constructor to prototype */
ecma_property_value_t *prop_value_p;
prop_value_p = ecma_create_named_data_property (prototype_class_p,
ecma_get_magic_string (LIT_MAGIC_STRING_CONSTRUCTOR),
ECMA_PROPERTY_CONFIGURABLE_WRITABLE,
NULL);
ecma_named_data_property_assign_value (prototype_class_p, prop_value_p, function_obj_value);
continue;
}
case VM_OC_SET_CLASS_CONSTRUCTOR:
{
ecma_object_t *new_constructor_obj_p = ecma_get_object_from_value (left_value);
ecma_object_t *current_constructor_obj_p = ecma_get_object_from_value (stack_top_p[-2]);
ecma_extended_object_t *new_ext_func_obj_p = (ecma_extended_object_t *) new_constructor_obj_p;
ecma_extended_object_t *current_ext_func_obj_p = (ecma_extended_object_t *) current_constructor_obj_p;
uint16_t type_flags_refs = current_constructor_obj_p->type_flags_refs;
const int new_type = ECMA_OBJECT_TYPE_FUNCTION - ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION;
current_constructor_obj_p->type_flags_refs = (uint16_t) (type_flags_refs + new_type);
ecma_compiled_code_t *bytecode_p;
bytecode_p = (ecma_compiled_code_t *) ecma_op_function_get_compiled_code (new_ext_func_obj_p);
bytecode_p->status_flags |= CBC_CODE_FLAGS_CONSTRUCTOR;
ecma_bytecode_ref ((ecma_compiled_code_t *) bytecode_p);
ECMA_SET_INTERNAL_VALUE_POINTER (current_ext_func_obj_p->u.function.bytecode_cp,
bytecode_p);
ecma_object_t *scope_p = ECMA_GET_NON_NULL_POINTER_FROM_POINTER_TAG (ecma_object_t,
new_ext_func_obj_p->u.function.scope_cp);
ECMA_SET_NON_NULL_POINTER_TAG (current_ext_func_obj_p->u.function.scope_cp, scope_p, 0);
ecma_deref_object (new_constructor_obj_p);
continue;
}
case VM_OC_PUSH_IMPL_CONSTRUCTOR:
{
ecma_object_t *current_constructor_obj_p = ecma_get_object_from_value (stack_top_p[-2]);
uint16_t type_flags_refs = current_constructor_obj_p->type_flags_refs;
const int new_type = ECMA_OBJECT_TYPE_BOUND_FUNCTION - ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION;
current_constructor_obj_p->type_flags_refs = (uint16_t) (type_flags_refs + new_type);
ecma_extended_object_t *ext_function_p = (ecma_extended_object_t *) current_constructor_obj_p;
ecma_object_t *super_obj_p = ecma_op_resolve_super_reference_value (frame_ctx_p->lex_env_p);
ECMA_SET_INTERNAL_VALUE_POINTER (ext_function_p->u.bound_function.target_function,
super_obj_p);
ext_function_p->u.bound_function.args_len_or_this = ECMA_VALUE_IMPLICIT_CONSTRUCTOR;
continue;
}
case VM_OC_CLASS_EXPR_CONTEXT_END:
{
JERRY_ASSERT (VM_GET_CONTEXT_TYPE (stack_top_p[-2]) == VM_CONTEXT_SUPER_CLASS);
stack_top_p = vm_stack_context_abort (frame_ctx_p, stack_top_p - 1);
stack_top_p++;
stack_top_p[-1] = *stack_top_p;
continue;
}
case VM_OC_CLASS_EVAL:
{
ECMA_CLEAR_SUPER_EVAL_PARSER_OPTS ();
ECMA_SET_SUPER_EVAL_PARSER_OPTS (*byte_code_p++);
continue;
}
case VM_OC_PUSH_CONSTRUCTOR_SUPER:
{
JERRY_ASSERT (byte_code_start_p[0] == CBC_EXT_OPCODE);
bool is_super_called = ecma_op_is_super_called (frame_ctx_p->lex_env_p);
if (byte_code_start_p[1] != CBC_EXT_PUSH_CONSTRUCTOR_SUPER_PROP)
{
/* Calling super(...) */
if (is_super_called)
{
result = ecma_raise_reference_error (ECMA_ERR_MSG ("Super constructor may only be called once."));
goto error;
}
}
else if (!is_super_called)
{
/* Reference to super.method or super["method"] */
result = ecma_raise_reference_error (ECMA_ERR_MSG ("Must call super constructor in derived class before "
"accessing 'super'."));
goto error;
}
/* FALLTHRU */
}
case VM_OC_PUSH_SUPER:
{
JERRY_ASSERT (byte_code_start_p[0] == CBC_EXT_OPCODE);
if (byte_code_start_p[1] == CBC_EXT_PUSH_SUPER
|| byte_code_start_p[1] == CBC_EXT_PUSH_CONSTRUCTOR_SUPER_PROP)
{
ecma_object_t *super_class_p = ecma_op_resolve_super_reference_value (frame_ctx_p->lex_env_p);
ecma_value_t super_prototype = ecma_op_object_get_by_magic_id (super_class_p,
LIT_MAGIC_STRING_PROTOTYPE);
if (ECMA_IS_VALUE_ERROR (super_prototype))
{
result = super_prototype;
goto error;
}
*stack_top_p++ = super_prototype;
}
else
{
ecma_object_t *super_class_p = ecma_op_resolve_super_reference_value (frame_ctx_p->lex_env_p);
ecma_ref_object (super_class_p);
*stack_top_p++ = ecma_make_object_value (super_class_p);
}
*stack_top_p++ = result;
continue;
}
case VM_OC_PUSH_CONSTRUCTOR_THIS:
case VM_OC_RESOLVE_LEXICAL_THIS:
{
if (!ecma_op_is_super_called (frame_ctx_p->lex_env_p))
result = ecma_op_get_this_binding (frame_ctx_p->lex_env_p);
if (ECMA_IS_VALUE_ERROR (result))
{
result = ecma_raise_reference_error (ECMA_ERR_MSG ("Must call super constructor in derived class before "
"accessing 'this' or returning from it."));
goto error;
}
*stack_top_p++ = ecma_copy_value (ecma_op_get_class_this_binding (frame_ctx_p->lex_env_p));
*stack_top_p++ = result;
continue;
}
case VM_OC_SUPER_PROP_REFERENCE:
case VM_OC_SUPER_REFERENCE:
{
/**
* In case of this VM_OC_SUPER_PROP_REFERENCE the previously pushed 'super' must be replaced
* with the current 'this' value to correctly access the properties.
*/
int index = -1; /* -1 in case of CBC_EXT_SUPER_PROP_ASSIGN */
result = opfunc_form_super_reference (&stack_top_p, frame_ctx_p, left_value, opcode);
if (JERRY_LIKELY (byte_code_start_p[1] == CBC_EXT_SUPER_PROP_CALL))
if (ECMA_IS_VALUE_ERROR (result))
{
cbc_opcode_t next_call_opcode = (cbc_opcode_t) byte_code_start_p[2];
/* The next opcode must be a call opcode */
JERRY_ASSERT ((next_call_opcode >= CBC_CALL && next_call_opcode <= CBC_CALL2_PROP_BLOCK)
|| (next_call_opcode == CBC_EXT_OPCODE
&& byte_code_start_p[3] >= CBC_EXT_SPREAD_CALL
&& byte_code_start_p[3] <= CBC_EXT_SPREAD_CALL_PROP_BLOCK));
int arguments_list_len;
if (next_call_opcode >= CBC_CALL0)
{
/* CBC_CALL{0,1,2}* have their arg count encoded, extract it from there */
arguments_list_len = (int) ((next_call_opcode - CBC_CALL0) / 6);
}
else
{
/**
* In this case the arguments are coded into the byte code stream as a byte argument
* following the call opcode.
*/
arguments_list_len = (int) byte_code_start_p[next_call_opcode == CBC_EXT_OPCODE ? 4 : 3];
}
/* The old 'super' value is at least '-3' element away from the current position on the stack. */
index = -3 - arguments_list_len;
}
else
{
/**
* The bytecode order for super assignment should be one of this:
* - CBC_EXT_PUSH_SUPER, CBC_EXT_SUPER_PROP_ASSIGN.
* - CBC_EXT_PUSH_STATIC_SUPER, CBC_EXT_SUPER_PROP_ASSIGN.
* - CBC_EXT_PUSH_CONSTRUCTOR_SUPER_PROP, CBC_EXT_SUPER_PROP_ASSIGN.
* That is one ext opcode back (-1).
*/
JERRY_ASSERT (byte_code_start_p[-1] == CBC_EXT_PUSH_SUPER
|| byte_code_start_p[-1] == CBC_EXT_PUSH_STATIC_SUPER
|| byte_code_start_p[-1] == CBC_EXT_PUSH_CONSTRUCTOR_SUPER_PROP);
goto error;
}
/* Replace the old 'super' value with the correct 'this' binding */
ecma_free_value (stack_top_p[index]);
stack_top_p[index] = ecma_copy_value (frame_ctx_p->this_binding);
continue;
}
case VM_OC_CONSTRUCTOR_RET:
{
result = left_value;
left_value = ECMA_VALUE_UNDEFINED;
if (!ecma_is_value_object (result))
{
if (ecma_is_value_undefined (result))
{
if (!ecma_op_is_super_called (frame_ctx_p->lex_env_p))
{
result = ecma_raise_reference_error (ECMA_ERR_MSG ("Must call super constructor in derived class "
"before returning from derived constructor"));
}
}
else
{
ecma_free_value (result);
result = ecma_raise_type_error (ECMA_ERR_MSG ("Derived constructors may only "
"return object or undefined."));
}
}
goto error;
goto free_left_value;
}
case VM_OC_PUSH_SPREAD_ELEMENT:
{
@@ -4016,7 +3780,15 @@ error:
while (stack_top_p > stack_bottom_p)
{
ecma_fast_free_value (*(--stack_top_p));
ecma_value_t stack_item = *(--stack_top_p);
#if ENABLED (JERRY_ES2015)
if (stack_item == ECMA_VALUE_RELEASE_LEX_ENV)
{
opfunc_pop_lexical_environment (frame_ctx_p);
continue;
}
#endif /* ENABLED (JERRY_ES2015) */
ecma_fast_free_value (stack_item);
}
#if ENABLED (JERRY_DEBUGGER)
+18 -24
View File
@@ -240,19 +240,16 @@ typedef enum
VM_OC_FOR_OF_GET_NEXT, /**< get next */
VM_OC_FOR_OF_HAS_NEXT, /**< has next */
VM_OC_CLASS_HERITAGE, /**< create a super class context */
VM_OC_CLASS_INHERITANCE, /**< inherit properties from the 'super' class */
VM_OC_PUSH_CLASS_CONSTRUCTOR_AND_PROTOTYPE, /**< push class constructor */
VM_OC_SET_CLASS_CONSTRUCTOR, /**< set class constructor to the given function literal */
VM_OC_PUSH_IMPL_CONSTRUCTOR, /**< create implicit class constructor */
VM_OC_CLASS_EXPR_CONTEXT_END, /**< class expression heritage context end */
VM_OC_CLASS_EVAL, /**< eval inside a class */
VM_OC_LOCAL_EVAL, /**< eval in local context */
VM_OC_SUPER_CALL, /**< call the 'super' constructor */
VM_OC_SUPER_PROP_REFERENCE, /**< resolve super property reference */
VM_OC_PUSH_SUPER, /**< push resolvable super reference */
VM_OC_PUSH_CONSTRUCTOR_SUPER, /**< push 'super' inside a class constructor */
VM_OC_PUSH_CONSTRUCTOR_THIS, /**< push 'this' inside a class constructor */
VM_OC_CONSTRUCTOR_RET, /**< explicit return from a class constructor */
VM_OC_PUSH_CLASS_ENVIRONMENT, /**< push class environment */
VM_OC_PUSH_IMPLICIT_CTOR, /**< create implicit class constructor */
VM_OC_INIT_CLASS, /**< initialize class */
VM_OC_FINALIZE_CLASS, /**< finalize class */
VM_OC_PUSH_SUPER_CONSTRUCTOR, /**< getSuperConstructor operation */
VM_OC_RESOLVE_LEXICAL_THIS, /**< resolve this_binding from from the lexical environment */
VM_OC_SUPER_REFERENCE, /**< push super reference */
VM_OC_PUSH_SPREAD_ELEMENT, /**< push spread element */
VM_OC_GET_ITERATOR, /**< GetIterator abstract operation */
VM_OC_ITERATOR_STEP, /**< IteratorStep abstract operation */
@@ -300,19 +297,16 @@ typedef enum
VM_OC_FOR_OF_GET_NEXT = VM_OC_NONE, /**< get next */
VM_OC_FOR_OF_HAS_NEXT = VM_OC_NONE, /**< has next */
VM_OC_CLASS_HERITAGE = VM_OC_NONE, /**< create a super class context */
VM_OC_CLASS_INHERITANCE = VM_OC_NONE, /**< inherit properties from the 'super' class */
VM_OC_PUSH_CLASS_CONSTRUCTOR_AND_PROTOTYPE = VM_OC_NONE, /**< push class constructor */
VM_OC_SET_CLASS_CONSTRUCTOR = VM_OC_NONE, /**< set class constructor to the given function literal */
VM_OC_PUSH_IMPL_CONSTRUCTOR = VM_OC_NONE, /**< create implicit class constructor */
VM_OC_CLASS_EXPR_CONTEXT_END = VM_OC_NONE, /**< class expression heritage context end */
VM_OC_CLASS_EVAL = VM_OC_NONE, /**< eval inside a class */
VM_OC_LOCAL_EVAL = VM_OC_NONE, /**< eval in local context */
VM_OC_SUPER_CALL = VM_OC_NONE, /**< call the 'super' constructor */
VM_OC_SUPER_PROP_REFERENCE = VM_OC_NONE, /**< resolve super property reference */
VM_OC_PUSH_SUPER = VM_OC_NONE, /**< push resolvable super reference */
VM_OC_PUSH_CONSTRUCTOR_SUPER = VM_OC_NONE, /**< push 'super' inside a class constructor */
VM_OC_PUSH_CONSTRUCTOR_THIS = VM_OC_NONE, /**< push 'this' inside a class constructor */
VM_OC_CONSTRUCTOR_RET = VM_OC_NONE, /**< explicit return from a class constructor */
VM_OC_PUSH_CLASS_ENVIRONMENT = VM_OC_NONE, /**< push class environment */
VM_OC_PUSH_IMPLICIT_CTOR = VM_OC_NONE, /**< create implicit class constructor */
VM_OC_INIT_CLASS = VM_OC_NONE, /**< initialize class */
VM_OC_FINALIZE_CLASS = VM_OC_NONE, /**< finalize class */
VM_OC_PUSH_SUPER_CONSTRUCTOR = VM_OC_NONE, /**< getSuperConstructor operation */
VM_OC_RESOLVE_LEXICAL_THIS = VM_OC_NONE, /**< resolve this_binding from from the lexical environment */
VM_OC_SUPER_REFERENCE = VM_OC_NONE, /**< push super reference */
VM_OC_PUSH_SPREAD_ELEMENT = VM_OC_NONE, /**< push spread element */
VM_OC_GET_ITERATOR = VM_OC_NONE, /**< GetIterator abstract operation */
VM_OC_ITERATOR_STEP = VM_OC_NONE, /**< IteratorStep abstract operation */