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
+1 -1
View File
@@ -195,7 +195,7 @@ ecma_gc_mark_property (ecma_property_pair_t *property_pair_p, /**< property pair
case ECMA_PROPERTY_TYPE_INTERNAL:
{
JERRY_ASSERT (ECMA_PROPERTY_GET_NAME_TYPE (property) == ECMA_DIRECT_STRING_MAGIC
&& property_pair_p->names_cp[index] == LIT_INTERNAL_MAGIC_STRING_NATIVE_POINTER);
&& property_pair_p->names_cp[index] >= LIT_FIRST_INTERNAL_MAGIC_STRING);
break;
}
default:
+48 -4
View File
@@ -82,12 +82,19 @@ typedef enum
/**
* Option flags for script parsing.
* Note:
* The enum members must be kept in sync with parser_general_flags_t
*/
typedef enum
{
ECMA_PARSE_NO_OPTS = 0, /**< no options passed */
ECMA_PARSE_STRICT_MODE = (1 << 0), /**< enable strict mode */
ECMA_PARSE_DIRECT_EVAL = (1 << 1) /**< is eval called directly (ECMA-262 v5, 15.1.2.1.1) */
ECMA_PARSE_STRICT_MODE = (1u << 0), /**< enable strict mode */
ECMA_PARSE_DIRECT_EVAL = (1u << 1), /**< eval is called directly (ECMA-262 v5, 15.1.2.1.1) */
/* These three status flags must be in this order. See PARSER_CLASS_PARSE_OPTS_OFFSET. */
ECMA_PARSE_CLASS_CONSTRUCTOR = (1u << 2), /**< a class constructor is being parsed (this value must be kept in
* in sync with PARSER_CLASS_CONSTRUCTOR) */
ECMA_PARSE_HAS_SUPER = (1u << 3), /**< the current context has super reference */
ECMA_PARSE_HAS_STATIC_SUPER = (1u << 4), /**< the current context is a static class method */
} ecma_parse_opts_t;
/**
@@ -170,6 +177,7 @@ enum
* ecma_op_object_find */
ECMA_VALUE_REGISTER_REF = ECMA_MAKE_VALUE (8), /**< register reference,
* a special "base" value for vm */
ECMA_VALUE_IMPLICIT_CONSTRUCTOR = ECMA_MAKE_VALUE (9), /**< special value for bound class constructors */
};
#if CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT32
@@ -397,6 +405,12 @@ typedef enum
#define ECMA_CONVERT_DATA_PROPERTY_TO_INTERNAL_PROPERTY(property_p) \
*(property_p) = (uint8_t) (*(property_p) + (ECMA_PROPERTY_TYPE_INTERNAL - ECMA_PROPERTY_TYPE_NAMEDDATA))
/**
* Convert internal property to data property.
*/
#define ECMA_CONVERT_INTERNAL_PROPERTY_TO_DATA_PROPERTY(property_p) \
*(property_p) = (uint8_t) (*(property_p) - (ECMA_PROPERTY_TYPE_INTERNAL - ECMA_PROPERTY_TYPE_NAMEDDATA))
/**
* Special property identifiers.
*/
@@ -633,12 +647,42 @@ typedef enum
ECMA_LEXICAL_ENVIRONMENT_DECLARATIVE = 13, /**< declarative lexical environment */
ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND = 14, /**< object-bound lexical environment
* with provideThis flag */
ECMA_LEXICAL_ENVIRONMENT_SUPER_OBJECT_BOUND = 15, /**< object-bound lexical environment
* with provided super reference */
ECMA_LEXICAL_ENVIRONMENT_TYPE_START = ECMA_LEXICAL_ENVIRONMENT_DECLARATIVE, /**< first lexical
* environment type */
ECMA_LEXICAL_ENVIRONMENT_TYPE__MAX = ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND /**< maximum value */
* environment type */
ECMA_LEXICAL_ENVIRONMENT_TYPE__MAX = ECMA_LEXICAL_ENVIRONMENT_SUPER_OBJECT_BOUND /**< maximum value */
} ecma_lexical_environment_type_t;
/**
* Offset for JERRY_CONTEXT (status_flags) top 8 bits.
*/
#define ECMA_SUPER_EVAL_OPTS_OFFSET (32 - 8)
/**
* Set JERRY_CONTEXT (status_flags) top 8 bits to the specified 'opts'.
*/
#define ECMA_SET_SUPER_EVAL_PARSER_OPTS(opts) \
do \
{ \
JERRY_CONTEXT (status_flags) |= ((uint32_t) opts << ECMA_SUPER_EVAL_OPTS_OFFSET) | ECMA_STATUS_DIRECT_EVAL; \
} while (0)
/**
* Get JERRY_CONTEXT (status_flags) top 8 bits.
*/
#define ECMA_GET_SUPER_EVAL_PARSER_OPTS() (JERRY_CONTEXT (status_flags) >> ECMA_SUPER_EVAL_OPTS_OFFSET)
/**
* Clear JERRY_CONTEXT (status_flags) top 8 bits.
*/
#define ECMA_CLEAR_SUPER_EVAL_PARSER_OPTS() \
do \
{ \
JERRY_CONTEXT (status_flags) &= ((1 << ECMA_SUPER_EVAL_OPTS_OFFSET) - 1); \
} while (0)
/**
* Ecma object type mask for getting the object type.
*/
+17 -6
View File
@@ -127,7 +127,7 @@ ecma_create_decl_lex_env (ecma_object_t *outer_lexical_environment_p) /**< outer
/**
* Create a object lexical environment with specified outer lexical environment
* (or NULL if the environment is not nested), binding object and provideThis flag.
* (or NULL if the environment is not nested), binding object and provided type flag.
*
* See also: ECMA-262 v5, 10.2.1.2
*
@@ -137,16 +137,22 @@ ecma_create_decl_lex_env (ecma_object_t *outer_lexical_environment_p) /**< outer
*/
ecma_object_t *
ecma_create_object_lex_env (ecma_object_t *outer_lexical_environment_p, /**< outer lexical environment */
ecma_object_t *binding_obj_p) /**< binding object */
ecma_object_t *binding_obj_p, /**< binding object */
ecma_lexical_environment_type_t type) /**< type of the new lexical environment */
{
#ifndef CONFIG_DISABLE_ES2015_CLASS
JERRY_ASSERT (type == ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND
|| type == ECMA_LEXICAL_ENVIRONMENT_SUPER_OBJECT_BOUND);
#else /* CONFIG_DISABLE_ES2015_CLASS */
JERRY_ASSERT (type == ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND);
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
JERRY_ASSERT (binding_obj_p != NULL
&& !ecma_is_lexical_environment (binding_obj_p));
ecma_object_t *new_lexical_environment_p = ecma_alloc_object ();
uint16_t type = ECMA_OBJECT_FLAG_BUILT_IN_OR_LEXICAL_ENV | ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND;
new_lexical_environment_p->type_flags_refs = type;
new_lexical_environment_p->type_flags_refs = (uint16_t) (ECMA_OBJECT_FLAG_BUILT_IN_OR_LEXICAL_ENV | type);
ecma_init_gc_info (new_lexical_environment_p);
@@ -355,7 +361,12 @@ ecma_get_lex_env_binding_object (const ecma_object_t *object_p) /**< object-boun
{
JERRY_ASSERT (object_p != NULL);
JERRY_ASSERT (ecma_is_lexical_environment (object_p));
#ifndef CONFIG_DISABLE_ES2015
JERRY_ASSERT (ecma_get_lex_env_type (object_p) == ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND
|| ecma_get_lex_env_type (object_p) == ECMA_LEXICAL_ENVIRONMENT_SUPER_OBJECT_BOUND);
#else /* CONFIG_DISABLE_ES2015 */
JERRY_ASSERT (ecma_get_lex_env_type (object_p) == ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND);
#endif /* !CONFIG_DISABLE_ES2015 */
return ECMA_GET_NON_NULL_POINTER (ecma_object_t,
object_p->property_list_or_bound_object_cp);
@@ -766,7 +777,7 @@ ecma_free_property (ecma_object_t *object_p, /**< object the property belongs to
/* Must be a native pointer. */
JERRY_ASSERT (ECMA_PROPERTY_GET_NAME_TYPE (*property_p) == ECMA_DIRECT_STRING_MAGIC
&& (name_cp == LIT_INTERNAL_MAGIC_STRING_NATIVE_POINTER));
&& name_cp >= LIT_FIRST_INTERNAL_MAGIC_STRING);
break;
}
}
+2 -1
View File
@@ -293,7 +293,8 @@ ecma_collection_iterator_next (ecma_value_t *iterator_p);
/* ecma-helpers.c */
ecma_object_t *ecma_create_object (ecma_object_t *prototype_object_p, size_t ext_object_size, ecma_object_type_t type);
ecma_object_t *ecma_create_decl_lex_env (ecma_object_t *outer_lexical_environment_p);
ecma_object_t *ecma_create_object_lex_env (ecma_object_t *outer_lexical_environment_p, ecma_object_t *binding_obj_p);
ecma_object_t *ecma_create_object_lex_env (ecma_object_t *outer_lexical_environment_p, ecma_object_t *binding_obj_p,
ecma_lexical_environment_type_t type);
bool JERRY_ATTR_PURE ecma_is_lexical_environment (const ecma_object_t *object_p);
bool JERRY_ATTR_PURE ecma_get_object_extensible (const ecma_object_t *object_p);
void ecma_set_object_extensible (ecma_object_t *object_p, bool is_extensible);
@@ -223,7 +223,20 @@ ecma_builtin_array_prototype_object_concat (ecma_value_t this_arg, /**< this arg
ret_value);
/* 2. */
ecma_value_t new_array = ecma_op_create_array_object (0, 0, false);
#ifndef CONFIG_DISABLE_ES2015_CLASS
ecma_object_t *obj_p = ecma_get_object_from_value (obj_this);
ecma_value_t new_array = ecma_op_create_array_object_by_constructor (NULL, 0, false, obj_p);
if (ECMA_IS_VALUE_ERROR (new_array))
{
ecma_free_value (obj_this);
return new_array;
}
#else /* CONFIG_DISABLE_ES2015_CLASS */
ecma_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
uint32_t new_length = 0;
@@ -825,7 +838,20 @@ ecma_builtin_array_prototype_object_slice (ecma_value_t this_arg, /**< 'this' ar
JERRY_ASSERT (start <= len && end <= len);
ecma_value_t new_array = ecma_op_create_array_object (0, 0, false);
#ifndef CONFIG_DISABLE_ES2015_CLASS
ecma_value_t new_array = ecma_op_create_array_object_by_constructor (NULL, 0, false, obj_p);
if (ECMA_IS_VALUE_ERROR (new_array))
{
ecma_free_value (len_value);
ecma_free_value (obj_this);
return new_array;
}
#else /* CONFIG_DISABLE_ES2015_CLASS */
ecma_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
/* 9. */
@@ -1178,7 +1204,20 @@ ecma_builtin_array_prototype_object_splice (ecma_value_t this_arg, /**< this arg
const uint32_t len = ecma_number_to_uint32 (len_number);
ecma_value_t new_array = ecma_op_create_array_object (0, 0, false);
#ifndef CONFIG_DISABLE_ES2015_CLASS
ecma_value_t new_array = ecma_op_create_array_object_by_constructor (NULL, 0, false, obj_p);
if (ECMA_IS_VALUE_ERROR (new_array))
{
ecma_free_value (len_value);
ecma_free_value (obj_this);
return new_array;
}
#else /* CONFIG_DISABLE_ES2015_CLASS */
ecma_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
uint32_t start = 0;
@@ -1973,8 +2012,20 @@ ecma_builtin_array_prototype_object_map (ecma_value_t this_arg, /**< this argume
/* 5. arg2 is simply used as T */
/* 6. */
#ifndef CONFIG_DISABLE_ES2015_CLASS
ecma_value_t new_array = ecma_op_create_array_object_by_constructor (NULL, 0, false, obj_p);
if (ECMA_IS_VALUE_ERROR (new_array))
{
ecma_free_value (len_value);
ecma_free_value (obj_this);
return new_array;
}
#else /* CONFIG_DISABLE_ES2015_CLASS */
ecma_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
/* 7-8. */
@@ -2080,8 +2131,20 @@ ecma_builtin_array_prototype_object_filter (ecma_value_t this_arg, /**< this arg
ecma_object_t *func_object_p;
/* 6. */
#ifndef CONFIG_DISABLE_ES2015_CLASS
ecma_value_t new_array = ecma_op_create_array_object_by_constructor (NULL, 0, false, obj_p);
if (ECMA_IS_VALUE_ERROR (new_array))
{
ecma_free_value (len_value);
ecma_free_value (obj_this);
return new_array;
}
#else /* CONFIG_DISABLE_ES2015_CLASS */
ecma_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
/* We already checked that arg1 is callable, so it will always be an object. */
+69 -6
View File
@@ -24,6 +24,7 @@
#include "ecma-number-arithmetic.h"
#include "ecma-objects.h"
#include "ecma-objects-general.h"
#include "ecma-function-object.h"
/** \addtogroup ecma ECMA
* @{
@@ -43,14 +44,14 @@
*/
ecma_value_t
ecma_op_create_array_object (const ecma_value_t *arguments_list_p, /**< list of arguments that
are passed to Array constructor */
* are passed to Array constructor */
ecma_length_t arguments_list_len, /**< length of the arguments' list */
bool is_treat_single_arg_as_length) /**< if the value is true,
arguments_list_len is 1
and single argument is Number,
then treat the single argument
as new Array's length rather
than as single item of the Array */
* arguments_list_len is 1
* and single argument is Number,
* then treat the single argument
* as new Array's length rather
* than as single item of the Array */
{
JERRY_ASSERT (arguments_list_len == 0
|| arguments_list_p != NULL);
@@ -131,6 +132,68 @@ ecma_op_create_array_object (const ecma_value_t *arguments_list_p, /**< list of
return ecma_make_object_value (object_p);
} /* ecma_op_create_array_object */
#ifndef CONFIG_DISABLE_ES2015_CLASS
/**
* Array object creation with custom prototype.
*
* See also: ECMA-262 v6, 9.4.2.3
*
* @return ecma value
* Returned value must be freed with ecma_free_value
*/
ecma_value_t
ecma_op_create_array_object_by_constructor (const ecma_value_t *arguments_list_p, /**< list of arguments that
* are passed to
* Array constructor */
ecma_length_t arguments_list_len, /**< length of the arguments' list */
bool is_treat_single_arg_as_length, /**< if the value is true,
* arguments_list_len is 1
* and single argument is Number,
* then treat the single argument
* as new Array's length rather
* than as single item of the
* Array */
ecma_object_t *object_p) /**< The object from whom the new array object
* is being created */
{
ecma_value_t constructor_value = ecma_op_object_get_by_magic_id (object_p, LIT_MAGIC_STRING_CONSTRUCTOR);
if (ECMA_IS_VALUE_ERROR (constructor_value)
|| !ecma_is_value_object (constructor_value)
|| !ecma_is_constructor (constructor_value))
{
ecma_free_value (constructor_value);
return ecma_raise_type_error (ECMA_ERR_MSG ("object.constructor is not a constructor."));
}
ecma_object_t *constructor_object_p = ecma_get_object_from_value (constructor_value);
ecma_value_t constructor_prototype = ecma_op_object_get_by_magic_id (constructor_object_p,
LIT_MAGIC_STRING_PROTOTYPE);
ecma_deref_object (constructor_object_p);
if (ECMA_IS_VALUE_ERROR (constructor_prototype))
{
return constructor_prototype;
}
ecma_value_t result = ecma_op_create_array_object (arguments_list_p,
arguments_list_len,
is_treat_single_arg_as_length);
if (ecma_is_value_object (constructor_prototype))
{
ecma_object_t *result_object_p = ecma_get_object_from_value (result);
ecma_object_t *constructor_prototpye_object_p = ecma_get_object_from_value (constructor_prototype);
ECMA_SET_POINTER (result_object_p->prototype_or_outer_reference_cp, constructor_prototpye_object_p);
}
ecma_free_value (constructor_prototype);
return result;
} /* ecma_op_create_array_object_by_constructor */
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
/**
* Update the length of an array to a new length
*
@@ -43,6 +43,12 @@ ecma_value_t
ecma_op_create_array_object (const ecma_value_t *arguments_list_p, ecma_length_t arguments_list_len,
bool is_treat_single_arg_as_length);
#ifndef CONFIG_DISABLE_ES2015_CLASS
ecma_value_t
ecma_op_create_array_object_by_constructor (const ecma_value_t *arguments_list_p, ecma_length_t arguments_list_len,
bool is_treat_single_arg_as_length, ecma_object_t *object_p);
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
ecma_value_t
ecma_op_array_object_set_length (ecma_object_t *object_p, ecma_value_t new_value, uint32_t flags);
+6 -3
View File
@@ -22,10 +22,7 @@
#include "ecma-lex-env.h"
#include "js-parser.h"
#include "vm.h"
#ifdef JERRY_ENABLE_LINE_INFO
#include "jcontext.h"
#endif /* JERRY_ENABLE_LINE_INFO */
/** \addtogroup ecma ECMA
* @{
@@ -98,6 +95,12 @@ ecma_op_eval_chars_buffer (const lit_utf8_byte_t *code_p, /**< code characters b
JERRY_CONTEXT (resource_name) = ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY);
#endif /* JERRY_ENABLE_LINE_INFO */
#ifndef CONFIG_DISABLE_ES2015_CLASS
parse_opts |= ECMA_GET_SUPER_EVAL_PARSER_OPTS ();
ECMA_CLEAR_SUPER_EVAL_PARSER_OPTS ();
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
ecma_value_t parse_status = parser_parse_script (NULL,
0,
code_p,
+236 -13
View File
@@ -453,12 +453,165 @@ ecma_op_function_has_construct_flag (const ecma_value_t *arguments_list_p) /**<
{
#ifndef CONFIG_DISABLE_ES2015_CLASS
return (((uintptr_t) arguments_list_p) & ECMA_CLASS_CONSTRUCT_FLAG);
#else
#else /* CONFIG_DISABLE_ES2015_CLASS */
JERRY_UNUSED (arguments_list_p);
return false;
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
} /* ecma_op_function_has_construct_flag */
#ifndef CONFIG_DISABLE_ES2015
/**
* Returns the closest declarative lexical enviroment to the super object bound lexical enviroment.
*
* @return the found lexical enviroment
*/
static ecma_object_t *
ecma_op_find_super_declerative_lex_env (ecma_object_t *lex_env_p) /**< starting lexical enviroment */
{
JERRY_ASSERT (lex_env_p);
JERRY_ASSERT (ecma_op_resolve_super_reference_value (lex_env_p));
JERRY_ASSERT (ecma_get_lex_env_type (lex_env_p) != ECMA_LEXICAL_ENVIRONMENT_SUPER_OBJECT_BOUND);
while (true)
{
ecma_object_t *lex_env_outer_p = ecma_get_lex_env_outer_reference (lex_env_p);
JERRY_ASSERT (lex_env_outer_p);
if (ecma_get_lex_env_type (lex_env_outer_p) == ECMA_LEXICAL_ENVIRONMENT_SUPER_OBJECT_BOUND)
{
JERRY_ASSERT (ecma_get_lex_env_type (lex_env_p) == ECMA_LEXICAL_ENVIRONMENT_DECLARATIVE);
return lex_env_p;
}
lex_env_p = lex_env_outer_p;
}
} /* ecma_op_find_super_declerative_lex_env */
/**
* Returns with the current class this_binding property
*
* @return NULL - if the property was not found
* the found property - otherwise
*/
static ecma_property_t *
ecma_op_get_class_this_binding_property (ecma_object_t *lex_env_p) /**< starting lexical enviroment */
{
JERRY_ASSERT (lex_env_p);
JERRY_ASSERT (ecma_is_lexical_environment (lex_env_p));
lex_env_p = ecma_op_find_super_declerative_lex_env (lex_env_p);
ecma_string_t *name_p = ecma_get_magic_string (LIT_INTERNAL_MAGIC_STRING_CLASS_THIS_BINDING);
return ecma_find_named_property (lex_env_p, name_p);
} /* ecma_op_get_class_this_binding_property */
/**
* Checks whether the 'super(...)' has been called.
*
* @return true - if the 'super (...)' has been called
* false - otherwise
*/
inline bool JERRY_ATTR_PURE
ecma_op_is_super_called (ecma_object_t *lex_env_p) /**< starting lexical enviroment */
{
ecma_property_t *property_p = ecma_op_get_class_this_binding_property (lex_env_p);
JERRY_ASSERT (property_p);
return (ECMA_PROPERTY_GET_TYPE (*property_p) == ECMA_PROPERTY_TYPE_NAMEDDATA);
} /* ecma_op_is_super_called */
/**
* Sets the value of 'super(...)' has been called.
*/
inline void
ecma_op_set_super_called (ecma_object_t *lex_env_p) /**< starting lexical enviroment */
{
ecma_property_t *property_p = ecma_op_get_class_this_binding_property (lex_env_p);
JERRY_ASSERT (property_p);
JERRY_ASSERT (ECMA_PROPERTY_GET_TYPE (*property_p) == ECMA_PROPERTY_TYPE_INTERNAL);
ECMA_CONVERT_INTERNAL_PROPERTY_TO_DATA_PROPERTY (property_p);
} /* ecma_op_set_super_called */
/**
* Sets the class context this_binding value.
*/
void
ecma_op_set_class_this_binding (ecma_object_t *lex_env_p, /**< starting lexical enviroment */
ecma_value_t this_binding) /**< 'this' argument's value */
{
JERRY_ASSERT (ecma_is_value_object (this_binding));
ecma_property_t *property_p = ecma_op_get_class_this_binding_property (lex_env_p);
ecma_property_value_t *value_p;
if (property_p)
{
JERRY_ASSERT (ECMA_PROPERTY_GET_TYPE (*property_p) == ECMA_PROPERTY_TYPE_NAMEDDATA);
value_p = ECMA_PROPERTY_VALUE_PTR (property_p);
}
else
{
ecma_string_t *name_p = ecma_get_magic_string (LIT_INTERNAL_MAGIC_STRING_CLASS_THIS_BINDING);
value_p = ecma_create_named_data_property (lex_env_p, name_p, ECMA_PROPERTY_FLAG_WRITABLE, &property_p);
ECMA_CONVERT_DATA_PROPERTY_TO_INTERNAL_PROPERTY (property_p);
}
value_p->value = this_binding;
} /* ecma_op_set_class_this_binding */
/**
* Gets the class context this binding value.
*
* @return the class context this binding value
*/
ecma_value_t
ecma_op_get_class_this_binding (ecma_object_t *lex_env_p) /**< starting lexical enviroment */
{
ecma_property_t *property_p = ecma_op_get_class_this_binding_property (lex_env_p);
JERRY_ASSERT (property_p);
ecma_property_value_t *value_p = ECMA_PROPERTY_VALUE_PTR (property_p);
JERRY_ASSERT (ecma_is_value_object (value_p->value));
return value_p->value;
} /* ecma_op_get_class_this_binding */
/**
* Dummy external function for implicit constructor call.
*
* @return ECMA_VALUE_ERROR - TypeError
*/
ecma_value_t
ecma_op_function_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);
return ecma_raise_type_error (ECMA_ERR_MSG ("Class constructor cannot be invoked without 'new'."));
} /* ecma_op_function_implicit_constructor_handler_cb */
/**
* Sets the completion value [[Prototype]] based on the this_arg value
*/
void
ecma_op_set_class_prototype (ecma_value_t completion_value, /**< completion_value */
ecma_value_t this_arg) /**< this argument*/
{
JERRY_ASSERT (ecma_is_value_object (completion_value));
JERRY_ASSERT (ecma_is_value_object (this_arg));
ecma_object_t *completion_obj_p = ecma_get_object_from_value (completion_value);
ecma_object_t *prototype_obj_p = ecma_get_object_prototype (ecma_get_object_from_value (this_arg));
JERRY_ASSERT (prototype_obj_p);
ECMA_SET_POINTER (completion_obj_p->prototype_or_outer_reference_cp, prototype_obj_p);
} /* ecma_op_set_class_prototype */
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
/**
* [[Call]] implementation for Function objects,
* created through 13.2 (ECMA_OBJECT_TYPE_FUNCTION)
@@ -484,6 +637,8 @@ ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */
ecma_object_type_t func_type = ecma_get_object_type (func_obj_p);
JERRY_ASSERT (func_type == ECMA_OBJECT_TYPE_FUNCTION
|| func_type == ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION
|| func_type == ECMA_OBJECT_TYPE_BOUND_FUNCTION
|| !ecma_op_function_has_construct_flag (arguments_list_p));
if (func_type == ECMA_OBJECT_TYPE_FUNCTION)
@@ -507,21 +662,20 @@ ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */
/* 8. */
ecma_value_t this_binding = this_arg_value;
bool free_this_binding = false;
bool is_strict;
bool is_no_lex_env;
const ecma_compiled_code_t *bytecode_data_p = ecma_op_function_get_compiled_code (ext_func_p);
#ifndef CONFIG_DISABLE_ES2015_CLASS
if (bytecode_data_p->status_flags & CBC_CODE_FLAGS_CONSTRUCTOR &&
!ecma_op_function_has_construct_flag (arguments_list_p))
bool is_class_constructor = bytecode_data_p->status_flags & CBC_CODE_FLAGS_CONSTRUCTOR;
if (is_class_constructor && !ecma_op_function_has_construct_flag (arguments_list_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Class constructor cannot be invoked without 'new'."));
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
is_strict = (bytecode_data_p->status_flags & CBC_CODE_FLAGS_STRICT_MODE) ? true : false;
is_no_lex_env = (bytecode_data_p->status_flags & CBC_CODE_FLAGS_LEXICAL_ENV_NOT_NEEDED) ? true : false;
bool is_strict = (bytecode_data_p->status_flags & CBC_CODE_FLAGS_STRICT_MODE) ? true : false;
bool is_no_lex_env = (bytecode_data_p->status_flags & CBC_CODE_FLAGS_LEXICAL_ENV_NOT_NEEDED) ? true : false;
/* 1. */
if (!is_strict)
@@ -561,6 +715,12 @@ ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */
arguments_list_len,
bytecode_data_p);
}
#ifndef CONFIG_DISABLE_ES2015_CLASS
if (JERRY_UNLIKELY (is_class_constructor))
{
ecma_op_set_class_this_binding (local_env_p, this_binding);
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
}
ecma_value_t ret_value = vm_run (bytecode_data_p,
@@ -658,7 +818,26 @@ ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */
if (!ecma_is_value_integer_number (args_len_or_this))
{
this_arg_value = args_len_or_this;
#ifndef CONFIG_DISABLE_ES2015_CLASS
if (JERRY_UNLIKELY (args_len_or_this == ECMA_VALUE_IMPLICIT_CONSTRUCTOR))
{
if (!ecma_op_function_has_construct_flag (arguments_list_p))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Class constructor cannot be invoked without 'new'."));
}
if (ecma_get_object_is_builtin (target_func_obj_p))
{
arguments_list_p = ecma_op_function_clear_construct_flag (arguments_list_p);
}
}
else
{
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
this_arg_value = args_len_or_this;
#ifndef CONFIG_DISABLE_ES2015_CLASS
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
args_length = 1;
}
else
@@ -675,6 +854,11 @@ ecma_op_function_call (ecma_object_t *func_obj_p, /**< Function object */
continue;
}
#ifndef CONFIG_DISABLE_ES2015_CLASS
arguments_list_p = ecma_op_function_clear_construct_flag (arguments_list_p);
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
JERRY_ASSERT (!ecma_op_function_has_construct_flag (arguments_list_p));
args_length--;
ecma_length_t merged_args_list_len = args_length + arguments_list_len;
@@ -721,12 +905,13 @@ ecma_op_function_construct (ecma_object_t *func_obj_p, /**< Function object */
JERRY_ASSERT (ecma_is_value_object (this_arg_value)
|| this_arg_value == ECMA_VALUE_UNDEFINED);
ecma_object_t *target_func_obj_p = NULL;
while (JERRY_UNLIKELY (ecma_get_object_type (func_obj_p) == ECMA_OBJECT_TYPE_BOUND_FUNCTION))
{
/* 1-3. */
ecma_extended_object_t *ext_function_p = (ecma_extended_object_t *) func_obj_p;
ecma_object_t *target_func_obj_p;
target_func_obj_p = ECMA_GET_INTERNAL_VALUE_POINTER (ecma_object_t,
ext_function_p->u.bound_function.target_function);
@@ -742,9 +927,15 @@ ecma_op_function_construct (ecma_object_t *func_obj_p, /**< Function object */
JERRY_ASSERT (args_length > 0);
/* 5. */
if (args_length == 1)
{
/* 5. */
#ifndef CONFIG_DISABLE_ES2015_CLASS
if (args_len_or_this == ECMA_VALUE_IMPLICIT_CONSTRUCTOR && ecma_is_value_undefined (this_arg_value))
{
break;
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
func_obj_p = target_func_obj_p;
continue;
}
@@ -787,9 +978,18 @@ ecma_op_function_construct (ecma_object_t *func_obj_p, /**< Function object */
return ecma_raise_type_error (ECMA_ERR_MSG ("Built-in routines have no constructor."));
}
return ecma_builtin_dispatch_construct (func_obj_p,
arguments_list_p,
arguments_list_len);
ecma_value_t ret_value = ecma_builtin_dispatch_construct (func_obj_p,
arguments_list_p,
arguments_list_len);
#ifndef CONFIG_DISABLE_ES2015_CLASS
if (!ecma_is_value_undefined (this_arg_value) && !ECMA_IS_VALUE_ERROR (ret_value))
{
ecma_op_set_class_prototype (ret_value, this_arg_value);
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
return ret_value;
}
ecma_object_t *new_this_obj_p = NULL;
@@ -843,10 +1043,33 @@ ecma_op_function_construct (ecma_object_t *func_obj_p, /**< Function object */
arguments_list_len);
break;
}
#ifndef CONFIG_DISABLE_ES2015_CLASS
case ECMA_OBJECT_TYPE_BOUND_FUNCTION:
{
JERRY_ASSERT (!ecma_op_function_has_construct_flag (arguments_list_p));
JERRY_ASSERT (target_func_obj_p != NULL);
ret_value = ecma_op_function_construct (target_func_obj_p,
this_arg_value,
arguments_list_p,
arguments_list_len);
break;
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
default:
{
JERRY_ASSERT (type == ECMA_OBJECT_TYPE_EXTERNAL_FUNCTION);
#ifndef CONFIG_DISABLE_ES2015_CLASS
ecma_extended_object_t *ext_func_obj_p = (ecma_extended_object_t *) func_obj_p;
if (ext_func_obj_p->u.external_handler_cb == ecma_op_function_implicit_constructor_handler_cb)
{
ret_value = ECMA_VALUE_UNDEFINED;
break;
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
ret_value = ecma_op_function_call (func_obj_p,
this_arg_value,
arguments_list_p,
@@ -46,6 +46,29 @@ ecma_op_create_external_function_object (ecma_external_handler_t handler_cb);
const ecma_compiled_code_t *
ecma_op_function_get_compiled_code (ecma_extended_object_t *function_p);
#ifndef CONFIG_DISABLE_ES2015_CLASS
void
ecma_op_set_super_called (ecma_object_t *lex_env_p);
bool
ecma_op_is_super_called (ecma_object_t *lex_env_p);
void
ecma_op_set_class_this_binding (ecma_object_t *lex_env_p, ecma_value_t this_binding);
ecma_value_t
ecma_op_get_class_this_binding (ecma_object_t *lex_env_p);
ecma_value_t
ecma_op_function_implicit_constructor_handler_cb (const ecma_value_t function_obj,
const ecma_value_t this_val,
const ecma_value_t args_p[],
const ecma_length_t args_count);
void
ecma_op_set_class_prototype (ecma_value_t completion_value, ecma_value_t this_arg);
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
#ifndef CONFIG_DISABLE_ES2015_ARROW_FUNCTION
const ecma_compiled_code_t *
ecma_op_arrow_function_get_compiled_code (ecma_arrow_function_t *arrow_function_p);
+12 -3
View File
@@ -41,7 +41,9 @@ ecma_init_global_lex_env (void)
{
ecma_object_t *glob_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_GLOBAL);
JERRY_CONTEXT (ecma_global_lex_env_p) = ecma_create_object_lex_env (NULL, glob_obj_p);
JERRY_CONTEXT (ecma_global_lex_env_p) = ecma_create_object_lex_env (NULL,
glob_obj_p,
ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND);
ecma_deref_object (glob_obj_p);
} /* ecma_init_global_lex_env */
@@ -86,7 +88,9 @@ ecma_op_has_binding (ecma_object_t *lex_env_p, /**< lexical environment */
JERRY_ASSERT (lex_env_p != NULL
&& ecma_is_lexical_environment (lex_env_p));
if (ecma_get_lex_env_type (lex_env_p) == ECMA_LEXICAL_ENVIRONMENT_DECLARATIVE)
ecma_lexical_environment_type_t lex_env_type = ecma_get_lex_env_type (lex_env_p);
if (lex_env_type == ECMA_LEXICAL_ENVIRONMENT_DECLARATIVE)
{
ecma_property_t *property_p = ecma_find_named_property (lex_env_p, name_p);
@@ -94,7 +98,12 @@ ecma_op_has_binding (ecma_object_t *lex_env_p, /**< lexical environment */
}
else
{
JERRY_ASSERT (ecma_get_lex_env_type (lex_env_p) == ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND);
#ifndef CONFIG_DISABLE_ES2015_CLASS
JERRY_ASSERT (lex_env_type == ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND
|| lex_env_type == ECMA_LEXICAL_ENVIRONMENT_SUPER_OBJECT_BOUND);
#else /* CONFIG_DISABLE_ES2015_CLASS */
JERRY_ASSERT (lex_env_type == ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND);
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
ecma_object_t *binding_obj_p = ecma_get_lex_env_binding_object (lex_env_p);
+35 -4
View File
@@ -59,6 +59,29 @@ ecma_op_resolve_reference_base (ecma_object_t *lex_env_p, /**< starting lexical
return NULL;
} /* ecma_op_resolve_reference_base */
#ifndef CONFIG_DISABLE_ES2015_CLASS
/**
* Resolve super reference.
*
* @return value of the reference
*/
ecma_object_t *
ecma_op_resolve_super_reference_value (ecma_object_t *lex_env_p) /**< starting lexical environment */
{
while (true)
{
JERRY_ASSERT (lex_env_p != NULL);
if (ecma_get_lex_env_type (lex_env_p) == ECMA_LEXICAL_ENVIRONMENT_SUPER_OBJECT_BOUND)
{
return ecma_get_lex_env_binding_object (lex_env_p);
}
lex_env_p = ecma_get_lex_env_outer_reference (lex_env_p);
}
} /* ecma_op_resolve_super_reference_value */
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
/**
* Resolve value corresponding to reference.
*
@@ -72,7 +95,9 @@ ecma_op_resolve_reference_value (ecma_object_t *lex_env_p, /**< starting lexical
while (lex_env_p != NULL)
{
if (ecma_get_lex_env_type (lex_env_p) == ECMA_LEXICAL_ENVIRONMENT_DECLARATIVE)
ecma_lexical_environment_type_t lex_env_type = ecma_get_lex_env_type (lex_env_p);
if (lex_env_type == ECMA_LEXICAL_ENVIRONMENT_DECLARATIVE)
{
ecma_property_t *property_p = ecma_find_named_property (lex_env_p, name_p);
@@ -81,10 +106,8 @@ ecma_op_resolve_reference_value (ecma_object_t *lex_env_p, /**< starting lexical
return ecma_fast_copy_value (ECMA_PROPERTY_VALUE_PTR (property_p)->value);
}
}
else
else if (lex_env_type == ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND)
{
JERRY_ASSERT (ecma_get_lex_env_type (lex_env_p) == ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND);
ecma_object_t *binding_obj_p = ecma_get_lex_env_binding_object (lex_env_p);
#ifndef CONFIG_ECMA_LCACHE_DISABLE
@@ -120,6 +143,14 @@ ecma_op_resolve_reference_value (ecma_object_t *lex_env_p, /**< starting lexical
return prop_value;
}
}
else
{
#ifndef CONFIG_DISABLE_ES2015_CLASS
JERRY_ASSERT (lex_env_type == ECMA_LEXICAL_ENVIRONMENT_SUPER_OBJECT_BOUND);
#else /* CONFIG_DISABLE_ES2015_CLASS */
JERRY_UNREACHABLE ();
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
}
lex_env_p = ecma_get_lex_env_outer_reference (lex_env_p);
}
@@ -28,6 +28,9 @@
ecma_object_t *ecma_op_resolve_reference_base (ecma_object_t *lex_env_p, ecma_string_t *name_p);
ecma_value_t ecma_op_resolve_reference_value (ecma_object_t *lex_env_p, ecma_string_t *name_p);
#ifndef CONFIG_DISABLE_ES2015_CLASS
ecma_object_t *ecma_op_resolve_super_reference_value (ecma_object_t *lex_env_p);
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
/**
* @}
@@ -257,7 +257,7 @@ ecma_typedarray_create_object_with_length (ecma_length_t array_length, /**< leng
ecma_free_value (new_arraybuffer_p);
return ecma_make_object_value (object_p);
} /* !ecma_typedarray_create_object_with_length */
} /* ecma_typedarray_create_object_with_length */
/**
* Create a TypedArray object by given buffer, offset, and array_length
@@ -984,6 +984,31 @@ ecma_op_create_typedarray_with_type_and_length (ecma_object_t *obj_p, /**< Typed
}
}
#ifndef CONFIG_DISABLE_ES2015_CLASS
ecma_value_t constructor_value = ecma_op_object_get_by_magic_id (obj_p, LIT_MAGIC_STRING_CONSTRUCTOR);
if (ECMA_IS_VALUE_ERROR (constructor_value)
|| !ecma_is_value_object (constructor_value)
|| !ecma_is_constructor (constructor_value))
{
ecma_deref_object (proto_p);
ecma_free_value (constructor_value);
return ecma_raise_type_error (ECMA_ERR_MSG ("object.constructor is not a constructor."));
}
ecma_object_t *constructor_object_p = ecma_get_object_from_value (constructor_value);
ecma_value_t constructor_prototype = ecma_op_object_get_by_magic_id (constructor_object_p,
LIT_MAGIC_STRING_PROTOTYPE);
ecma_deref_object (constructor_object_p);
if (ECMA_IS_VALUE_ERROR (constructor_prototype))
{
ecma_deref_object (proto_p);
return constructor_prototype;
}
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
ecma_value_t new_obj = ecma_typedarray_create_object_with_length (array_length,
proto_p,
element_size_shift,
@@ -991,6 +1016,14 @@ ecma_op_create_typedarray_with_type_and_length (ecma_object_t *obj_p, /**< Typed
ecma_deref_object (proto_p);
#ifndef CONFIG_DISABLE_ES2015_CLASS
ecma_object_t *constructor_prototype_object_p = ecma_get_object_from_value (constructor_prototype);
ecma_object_t *new_obj_p = ecma_get_object_from_value (new_obj);
ECMA_SET_POINTER (new_obj_p->prototype_or_outer_reference_cp, constructor_prototype_object_p);
ecma_deref_object (constructor_prototype_object_p);
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
return new_obj;
} /* ecma_op_create_typedarray_with_type_and_length */