Implement ES2015 class feature (part II.) (#2439)

This patch is the second milestone of the implementation of this new language element.

Supported:
 - Single class inheritance
 - Functionality of 'super' keyword
 - Implicit constructor in class heritage
 - Specific behaviour while extending with the built-in 'Array' or '%TypedArray%' object
 - Abstract subclasses (Mix-ins)

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
This commit is contained in:
Robert Fancsik
2018-10-25 16:00:48 +02:00
committed by Akos Kiss
parent e0e6363f85
commit d1860d0e34
54 changed files with 2543 additions and 199 deletions
+6
View File
@@ -26,6 +26,9 @@
* @{
*/
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.
*
@@ -64,6 +67,9 @@ vm_stack_context_abort (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
/* FALLTHRU */
}
case VM_CONTEXT_WITH:
#ifndef CONFIG_DISABLE_ES2015_CLASS
case VM_CONTEXT_SUPER_CLASS:
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
{
ecma_object_t *lex_env_p = frame_ctx_p->lex_env_p;
frame_ctx_p->lex_env_p = ecma_get_lex_env_outer_reference (lex_env_p);
+3
View File
@@ -57,6 +57,9 @@ typedef enum
VM_CONTEXT_TRY, /**< try context */
VM_CONTEXT_CATCH, /**< catch context */
VM_CONTEXT_WITH, /**< with context */
#ifndef CONFIG_DISABLE_ES2015_CLASS
VM_CONTEXT_SUPER_CLASS, /**< super class context */
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
VM_CONTEXT_FOR_IN, /**< for-in context */
} vm_stack_context_type_t;
+369 -1
View File
@@ -382,6 +382,59 @@ vm_get_implicit_this_value (ecma_value_t *this_value_p) /**< [in,out] this value
return false;
} /* vm_get_implicit_this_value */
#ifndef CONFIG_DISABLE_ES2015_CLASS
/**
* 'super(...)' function call handler.
*/
static void
vm_super_call (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
{
JERRY_ASSERT (frame_ctx_p->call_operation == VM_EXEC_SUPER_CALL);
JERRY_ASSERT (frame_ctx_p->byte_code_p[0] == CBC_EXT_OPCODE);
uint32_t arguments_list_len = frame_ctx_p->byte_code_p[2];
ecma_value_t *stack_top_p = frame_ctx_p->stack_top_p - arguments_list_len;
ecma_value_t func_value = stack_top_p[-1];
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))
{
completion_value = ecma_raise_type_error ("Class extends value is not a constructor.");
}
else
{
completion_value = ecma_op_function_construct (ecma_get_object_from_value (func_value),
this_value,
stack_top_p,
arguments_list_len);
if (this_value != completion_value && 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);
}
}
/* Free registers. */
for (uint32_t i = 0; i < arguments_list_len; i++)
{
ecma_fast_free_value (stack_top_p[i]);
}
ecma_free_value (stack_top_p[-1]);
stack_top_p[-1] = completion_value;
frame_ctx_p->stack_top_p = stack_top_p;
/* 'super (...)' call continues as a normal 'Function call' */
frame_ctx_p->call_operation = VM_EXEC_CALL;
} /* vm_super_call */
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
/**
* 'Function call' opcode handler.
*
@@ -1186,6 +1239,313 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
*stack_top_p++ = result;
continue;
}
#ifndef CONFIG_DISABLE_ES2015_CLASS
case VM_OC_SUPER_CALL:
{
if (frame_ctx_p->call_operation == VM_NO_EXEC_OP)
{
frame_ctx_p->call_operation = VM_EXEC_SUPER_CALL;
frame_ctx_p->byte_code_p = byte_code_start_p;
frame_ctx_p->stack_top_p = stack_top_p;
frame_ctx_p->call_block_result = block_result;
return ECMA_VALUE_UNDEFINED;
}
byte_code_p++;
frame_ctx_p->call_operation = VM_NO_EXEC_OP;
result = *(--stack_top_p);
block_result = frame_ctx_p->call_block_result;
if (ECMA_IS_VALUE_ERROR (result))
{
goto error;
}
if (!(opcode_data & (VM_OC_PUT_STACK | VM_OC_PUT_BLOCK)))
{
ecma_fast_free_value (result);
}
else if (opcode_data & VM_OC_PUT_STACK)
{
*stack_top_p++ = result;
}
else
{
ecma_fast_free_value (block_result);
block_result = result;
}
continue;
}
case VM_OC_CLASS_HERITAGE:
{
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);
JERRY_ASSERT (frame_ctx_p->registers_p + register_end + frame_ctx_p->context_depth == stack_top_p);
if (ecma_is_value_null (super_value))
{
super_class_p = ecma_create_object (NULL, 0, ECMA_OBJECT_TYPE_GENERAL);
}
else
{
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))
{
ecma_free_value (JERRY_CONTEXT (error_value));
}
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 (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_property_value_t *prop_value_p;
prop_value_p = ecma_create_named_data_property (child_prototype_class_p,
ecma_get_magic_string (LIT_MAGIC_STRING_CONSTRUCTOR),
ECMA_PROPERTY_CONFIGURABLE_WRITABLE,
NULL);
ecma_named_data_property_assign_value (child_prototype_class_p, prop_value_p, child_value);
ecma_object_t *super_class_p = ecma_get_lex_env_binding_object (frame_ctx_p->lex_env_p);
if (ecma_get_object_prototype (super_class_p))
{
ecma_value_t super_prototype_value = ecma_op_object_get_by_magic_id (super_class_p,
LIT_MAGIC_STRING_PROTOTYPE);
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_POINTER (child_prototype_class_p->prototype_or_outer_reference_cp, super_prototype_class_p);
ECMA_SET_POINTER (child_class_p->prototype_or_outer_reference_cp, super_class_p);
}
ecma_free_value (super_prototype_value);
}
continue;
}
case VM_OC_PUSH_CLASS_CONSTRUCTOR:
{
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_deref_object (prototype_obj_p);
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;
*stack_top_p++ = ecma_make_object_value (function_obj_p);
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_SET_INTERNAL_VALUE_POINTER (current_ext_func_obj_p->u.function.scope_cp,
ECMA_GET_INTERNAL_VALUE_POINTER (const ecma_object_t,
new_ext_func_obj_p->u.function.scope_cp));
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 (frame_ctx_p->registers_p + register_end + frame_ctx_p->context_depth == stack_top_p - 1);
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);
JERRY_ASSERT (frame_ctx_p->registers_p + register_end + frame_ctx_p->context_depth == stack_top_p);
stack_top_p++;
stack_top_p[-1] = *stack_top_p;
continue;
}
case VM_OC_CLASS_EVAL:
{
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);
*stack_top_p++ = ecma_fast_copy_value (ecma_make_object_value (super_class_p));
}
continue;
}
case VM_OC_PUSH_CONSTRUCTOR_THIS:
{
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 "
"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));
continue;
}
case VM_OC_SUPER_PROP_REFERENCE:
{
const int index = (byte_code_start_p[1] == CBC_EXT_SUPER_PROP_ASSIGN) ? -1 : -3;
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;
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
case VM_OC_PUSH_ELISON:
{
*stack_top_p++ = ECMA_VALUE_ARRAY_HOLE;
@@ -2365,7 +2725,9 @@ vm_loop (vm_frame_ctx_t *frame_ctx_p) /**< frame context */
object_p = ecma_get_object_from_value (result);
with_env_p = ecma_create_object_lex_env (frame_ctx_p->lex_env_p, object_p);
with_env_p = ecma_create_object_lex_env (frame_ctx_p->lex_env_p,
object_p,
ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND);
ecma_deref_object (object_p);
VM_PLUS_EQUAL_U16 (frame_ctx_p->context_depth, PARSER_WITH_CONTEXT_STACK_ALLOCATION);
@@ -3060,6 +3422,12 @@ vm_execute (vm_frame_ctx_t *frame_ctx_p, /**< frame context */
{
opfunc_call (frame_ctx_p);
}
#ifndef CONFIG_DISABLE_ES2015_CLASS
else if (frame_ctx_p->call_operation == VM_EXEC_SUPER_CALL)
{
vm_super_call (frame_ctx_p);
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
else
{
JERRY_ASSERT (frame_ctx_p->call_operation == VM_EXEC_CONSTRUCT);
+32 -1
View File
@@ -209,6 +209,21 @@ typedef enum
VM_OC_FINALLY, /**< finally */
VM_OC_CONTEXT_END, /**< context end */
VM_OC_JUMP_AND_EXIT_CONTEXT, /**< jump and exit context */
#ifndef CONFIG_DISABLE_ES2015_CLASS
VM_OC_CLASS_HERITAGE, /**< create a super class context */
VM_OC_CLASS_INHERITANCE, /**< inherit properties from the 'super' class */
VM_OC_PUSH_CLASS_CONSTRUCTOR, /**< 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_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 */
#endif /* !CONFIG_DISABLE_ES2015 */
#ifdef JERRY_DEBUGGER
VM_OC_BREAKPOINT_ENABLED, /**< enabled breakpoint for debugger */
VM_OC_BREAKPOINT_DISABLED, /**< disabled breakpoint for debugger */
@@ -217,7 +232,7 @@ typedef enum
VM_OC_RESOURCE_NAME, /**< resource name of the current function */
VM_OC_LINE, /**< line number of the next statement */
#endif /* JERRY_ENABLE_LINE_INFO */
VM_OC_NONE, /**< a special opcode for */
VM_OC_NONE, /**< a special opcode for unsupported byte codes */
} vm_oc_types;
/**
@@ -236,6 +251,21 @@ typedef enum
VM_OC_RESOURCE_NAME = VM_OC_NONE, /**< resource name of the current function is unused */
VM_OC_LINE = VM_OC_NONE, /**< line number of the next statement is unused */
#endif /* !JERRY_ENABLE_LINE_INFO */
#ifdef CONFIG_DISABLE_ES2015_CLASS
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 = 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_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 */
#endif /* CONFIG_DISABLE_ES2015 */
VM_OC_UNUSED = VM_OC_NONE /**< placeholder if the list is empty */
} vm_oc_unused_types;
@@ -315,6 +345,7 @@ typedef enum
{
VM_NO_EXEC_OP, /**< do nothing */
VM_EXEC_CALL, /**< invoke a function */
VM_EXEC_SUPER_CALL, /**< invoke a function through 'super' keyword */
VM_EXEC_CONSTRUCT, /**< construct a new object */
} vm_call_operation;