Skip to content

Commit d985517

Browse files
committed
Implement ES2015 class feature (part II.)
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 [email protected]
1 parent 7825e47 commit d985517

28 files changed

+1999
-86
lines changed

jerry-core/ecma/base/ecma-globals.h

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,11 @@ typedef enum
8787
{
8888
ECMA_PARSE_NO_OPTS = 0, /**< no options passed */
8989
ECMA_PARSE_STRICT_MODE = (1 << 0), /**< enable strict mode */
90-
ECMA_PARSE_DIRECT_EVAL = (1 << 1) /**< is eval called directly (ECMA-262 v5, 15.1.2.1.1) */
90+
ECMA_PARSE_DIRECT_EVAL = (1 << 1), /**< is eval called directly (ECMA-262 v5, 15.1.2.1.1) */
91+
ECMA_PARSE_HAS_SUPER = (1 << 2), /**< the current context has super reference */
92+
ECMA_PARSE_CLASS_CONSTRUCTOR = (1 << 3), /**< the current context is a class constructor */
93+
ECMA_PARSE_ARROW_FUNCTION = (1 << 4), /**< the current context is an arrow function */
94+
ECMA_PARSE_CLASS_STATIC_FUNCTION = (1 << 5), /**< the current context is a static class method */
9195
} ecma_parse_opts_t;
9296

9397
/**
@@ -633,12 +637,56 @@ typedef enum
633637
ECMA_LEXICAL_ENVIRONMENT_DECLARATIVE = 13, /**< declarative lexical environment */
634638
ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND = 14, /**< object-bound lexical environment
635639
* with provideThis flag */
640+
ECMA_LEXICAL_ENVIRONMENT_SUPER_OBJECT_BOUND = 15, /**< object-bound lexical environment
641+
* with provided super reference */
636642

637643
ECMA_LEXICAL_ENVIRONMENT_TYPE_START = ECMA_LEXICAL_ENVIRONMENT_DECLARATIVE, /**< first lexical
638644
* environment type */
639-
ECMA_LEXICAL_ENVIRONMENT_TYPE__MAX = ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND /**< maximum value */
645+
ECMA_LEXICAL_ENVIRONMENT_TYPE__MAX = ECMA_LEXICAL_ENVIRONMENT_SUPER_OBJECT_BOUND /**< maximum value */
640646
} ecma_lexical_environment_type_t;
641647

648+
/**
649+
* Option flags for super call.
650+
*/
651+
typedef enum
652+
{
653+
ECMA_HERITAGE_NO_OPTS = 0, /**< no speficied options are provided */
654+
ECMA_HERITAGE_SUPER_CALL = (1 << 0), /**< the next call operation is going to be a super constructor call */
655+
ECMA_HERITAGE_SUPER_CALLED = (1 << 1), /**< 'super ()' has been called in the current context */
656+
ECMA_HERITAGE_SUPER_PROP_CALL = (1 << 2), /**< call the resolved property with the current 'this' binding */
657+
ECMA_HERITAGE_BULTIN_RESULT = (1 << 3), /**< the SuperClass is an built-in object */
658+
ECMA_HERITAGE_EXPLICIT_RETURN = (1 << 4), /**< the constructor has explicit return statement */
659+
ECMA_HERITAGE_CLASS_BASE = (1 << 5), /**< the current frame is a base class */
660+
} ecma_heritage_opts_t;
661+
662+
/**
663+
* Offset for JERRY_CONTEXT (status_flags) top 8 bits.
664+
*/
665+
#define ECMA_SUPER_EVAL_OPTS_OFFSET (32 - 8)
666+
667+
/**
668+
* Set JERRY_CONTEXT (status_flags) top 8 bits to the specified 'opts'.
669+
*/
670+
#define ECMA_SET_SUPER_EVAL_PARSER_OPTS(base, opts) \
671+
do \
672+
{ \
673+
base = base | ((uint32_t) opts << ECMA_SUPER_EVAL_OPTS_OFFSET); \
674+
} while (0)
675+
676+
/**
677+
* Get JERRY_CONTEXT (status_flags) top 8 bits.
678+
*/
679+
#define ECMA_GET_SUPER_EVAL_PARSER_OPTS(base) (base >> ECMA_SUPER_EVAL_OPTS_OFFSET)
680+
681+
/**
682+
* Clear JERRY_CONTEXT (status_flags) top 8 bits.
683+
*/
684+
#define ECMA_CLEAR_SUPER_EVAL_PARSER_OPTS(base) \
685+
do \
686+
{ \
687+
base = base & ((1 << ECMA_SUPER_EVAL_OPTS_OFFSET) - 1); \
688+
} while (0)
689+
642690
/**
643691
* Ecma object type mask for getting the object type.
644692
*/

jerry-core/ecma/base/ecma-helpers.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ ecma_create_decl_lex_env (ecma_object_t *outer_lexical_environment_p) /**< outer
127127

128128
/**
129129
* Create a object lexical environment with specified outer lexical environment
130-
* (or NULL if the environment is not nested), binding object and provideThis flag.
130+
* (or NULL if the environment is not nested), binding object and provided type flag.
131131
*
132132
* See also: ECMA-262 v5, 10.2.1.2
133133
*
@@ -137,16 +137,15 @@ ecma_create_decl_lex_env (ecma_object_t *outer_lexical_environment_p) /**< outer
137137
*/
138138
ecma_object_t *
139139
ecma_create_object_lex_env (ecma_object_t *outer_lexical_environment_p, /**< outer lexical environment */
140-
ecma_object_t *binding_obj_p) /**< binding object */
140+
ecma_object_t *binding_obj_p, /**< binding object */
141+
ecma_lexical_environment_type_t type) /**< type of the new lexical environment */
141142
{
142143
JERRY_ASSERT (binding_obj_p != NULL
143144
&& !ecma_is_lexical_environment (binding_obj_p));
144145

145146
ecma_object_t *new_lexical_environment_p = ecma_alloc_object ();
146147

147-
uint16_t type = ECMA_OBJECT_FLAG_BUILT_IN_OR_LEXICAL_ENV | ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND;
148-
149-
new_lexical_environment_p->type_flags_refs = type;
148+
new_lexical_environment_p->type_flags_refs = (uint16_t) (ECMA_OBJECT_FLAG_BUILT_IN_OR_LEXICAL_ENV | type);
150149

151150
ecma_init_gc_info (new_lexical_environment_p);
152151

@@ -355,7 +354,7 @@ ecma_get_lex_env_binding_object (const ecma_object_t *object_p) /**< object-boun
355354
{
356355
JERRY_ASSERT (object_p != NULL);
357356
JERRY_ASSERT (ecma_is_lexical_environment (object_p));
358-
JERRY_ASSERT (ecma_get_lex_env_type (object_p) == ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND);
357+
JERRY_ASSERT (ecma_get_lex_env_type (object_p) != ECMA_LEXICAL_ENVIRONMENT_DECLARATIVE);
359358

360359
return ECMA_GET_NON_NULL_POINTER (ecma_object_t,
361360
object_p->property_list_or_bound_object_cp);

jerry-core/ecma/base/ecma-helpers.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,8 @@ ecma_collection_iterator_next (ecma_value_t *iterator_p);
293293
/* ecma-helpers.c */
294294
ecma_object_t *ecma_create_object (ecma_object_t *prototype_object_p, size_t ext_object_size, ecma_object_type_t type);
295295
ecma_object_t *ecma_create_decl_lex_env (ecma_object_t *outer_lexical_environment_p);
296-
ecma_object_t *ecma_create_object_lex_env (ecma_object_t *outer_lexical_environment_p, ecma_object_t *binding_obj_p);
296+
ecma_object_t *ecma_create_object_lex_env (ecma_object_t *outer_lexical_environment_p, ecma_object_t *binding_obj_p,
297+
ecma_lexical_environment_type_t type);
297298
bool JERRY_ATTR_PURE ecma_is_lexical_environment (const ecma_object_t *object_p);
298299
bool JERRY_ATTR_PURE ecma_get_object_extensible (const ecma_object_t *object_p);
299300
void ecma_set_object_extensible (ecma_object_t *object_p, bool is_extensible);

jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,18 @@ ecma_builtin_array_prototype_object_concat (ecma_value_t this_arg, /**< this arg
223223
ret_value);
224224

225225
/* 2. */
226+
#ifndef CONFIG_DISABLE_ES2015_CLASS
227+
ecma_object_t *obj_p = ecma_get_object_from_value (obj_this);
228+
ecma_value_t new_array = ecma_op_create_array_object_by_constructor (0, 0, false, obj_p);
229+
230+
if (ECMA_IS_VALUE_ERROR (new_array))
231+
{
232+
ecma_free_value (obj_this);
233+
return new_array;
234+
}
235+
#else /* CONFIG_DISABLE_ES2015_CLASS */
226236
ecma_value_t new_array = ecma_op_create_array_object (0, 0, false);
237+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
227238
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
228239
uint32_t new_length = 0;
229240

@@ -825,7 +836,19 @@ ecma_builtin_array_prototype_object_slice (ecma_value_t this_arg, /**< 'this' ar
825836

826837
JERRY_ASSERT (start <= len && end <= len);
827838

839+
#ifndef CONFIG_DISABLE_ES2015_CLASS
840+
ecma_value_t new_array = ecma_op_create_array_object_by_constructor (0, 0, false, obj_p);
841+
842+
if (ECMA_IS_VALUE_ERROR (new_array))
843+
{
844+
ecma_free_value (len_value);
845+
ecma_free_value (obj_this);
846+
return new_array;
847+
}
848+
#else /* CONFIG_DISABLE_ES2015_CLASS */
828849
ecma_value_t new_array = ecma_op_create_array_object (0, 0, false);
850+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
851+
829852
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
830853

831854
/* 9. */
@@ -1973,7 +1996,19 @@ ecma_builtin_array_prototype_object_map (ecma_value_t this_arg, /**< this argume
19731996
/* 5. arg2 is simply used as T */
19741997

19751998
/* 6. */
1999+
#ifndef CONFIG_DISABLE_ES2015_CLASS
2000+
ecma_value_t new_array = ecma_op_create_array_object_by_constructor (NULL, 0, false, obj_p);
2001+
2002+
if (ECMA_IS_VALUE_ERROR (new_array))
2003+
{
2004+
ecma_free_value (len_value);
2005+
ecma_free_value (obj_this);
2006+
return new_array;
2007+
}
2008+
#else /* CONFIG_DISABLE_ES2015_CLASS */
19762009
ecma_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
2010+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
2011+
19772012
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
19782013
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
19792014

@@ -2080,7 +2115,18 @@ ecma_builtin_array_prototype_object_filter (ecma_value_t this_arg, /**< this arg
20802115
ecma_object_t *func_object_p;
20812116

20822117
/* 6. */
2118+
#ifndef CONFIG_DISABLE_ES2015_CLASS
2119+
ecma_value_t new_array = ecma_op_create_array_object_by_constructor (NULL, 0, false, obj_p);
2120+
2121+
if (ECMA_IS_VALUE_ERROR (new_array))
2122+
{
2123+
ecma_free_value (len_value);
2124+
ecma_free_value (obj_this);
2125+
return new_array;
2126+
}
2127+
#else /* CONFIG_DISABLE_ES2015_CLASS */
20832128
ecma_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
2129+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
20842130
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
20852131
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
20862132

jerry-core/ecma/operations/ecma-array-object.c

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#include "ecma-number-arithmetic.h"
2525
#include "ecma-objects.h"
2626
#include "ecma-objects-general.h"
27+
#ifndef CONFIG_DISABLE_ES2015_CLASS
28+
#include "ecma-function-object.h"
29+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
2730

2831
/** \addtogroup ecma ECMA
2932
* @{
@@ -43,14 +46,14 @@
4346
*/
4447
ecma_value_t
4548
ecma_op_create_array_object (const ecma_value_t *arguments_list_p, /**< list of arguments that
46-
are passed to Array constructor */
49+
* are passed to Array constructor */
4750
ecma_length_t arguments_list_len, /**< length of the arguments' list */
4851
bool is_treat_single_arg_as_length) /**< if the value is true,
49-
arguments_list_len is 1
50-
and single argument is Number,
51-
then treat the single argument
52-
as new Array's length rather
53-
than as single item of the Array */
52+
* arguments_list_len is 1
53+
* and single argument is Number,
54+
* then treat the single argument
55+
* as new Array's length rather
56+
* than as single item of the Array */
5457
{
5558
JERRY_ASSERT (arguments_list_len == 0
5659
|| arguments_list_p != NULL);
@@ -131,6 +134,67 @@ ecma_op_create_array_object (const ecma_value_t *arguments_list_p, /**< list of
131134
return ecma_make_object_value (object_p);
132135
} /* ecma_op_create_array_object */
133136

137+
#ifndef CONFIG_DISABLE_ES2015_CLASS
138+
/**
139+
* Array object creation operation according to the 'constructor' property.
140+
*
141+
* See also: ECMA-262 v6, 9.4.2.3
142+
*
143+
* @return ecma value
144+
* Returned value must be freed with ecma_free_value
145+
*/
146+
ecma_value_t
147+
ecma_op_create_array_object_by_constructor (const ecma_value_t *arguments_list_p, /**< list of arguments that
148+
* are passed to Array constructor */
149+
ecma_length_t arguments_list_len, /**< length of the arguments' list */
150+
bool is_treat_single_arg_as_length, /**< if the value is true,
151+
* arguments_list_len is 1
152+
* and single argument is Number,
153+
* then treat the single argument
154+
* as new Array's length rather
155+
* than as single item of the
156+
* Array */
157+
ecma_object_t *object_p) /**< The object from whom the new array object
158+
* is being created */
159+
{
160+
ecma_value_t constructor_value = ecma_op_object_get_by_magic_id (object_p, LIT_MAGIC_STRING_CONSTRUCTOR);
161+
162+
if (ECMA_IS_VALUE_ERROR (constructor_value)
163+
|| !ecma_is_value_object (constructor_value)
164+
|| !ecma_is_constructor (constructor_value))
165+
{
166+
ecma_free_value (constructor_value);
167+
return ecma_raise_type_error (ECMA_ERR_MSG ("object.constructor is not a constructor."));
168+
}
169+
170+
ecma_object_t *constructor_object_p = ecma_get_object_from_value (constructor_value);
171+
172+
ecma_value_t result = ecma_op_create_array_object (arguments_list_p,
173+
arguments_list_len,
174+
is_treat_single_arg_as_length);
175+
176+
ecma_object_t *result_object_p = ecma_get_object_from_value (result);
177+
178+
ecma_value_t constructor_prototype = ecma_op_object_get_by_magic_id (constructor_object_p,
179+
LIT_MAGIC_STRING_PROTOTYPE);
180+
181+
if (ECMA_IS_VALUE_ERROR (constructor_prototype))
182+
{
183+
ecma_deref_object (result_object_p);
184+
ecma_deref_object (constructor_object_p);
185+
return constructor_prototype;
186+
}
187+
188+
ecma_object_t *constructor_prototpye_object_p = ecma_get_object_from_value (constructor_prototype);
189+
ECMA_SET_POINTER (result_object_p->prototype_or_outer_reference_cp, constructor_prototpye_object_p);
190+
191+
ecma_deref_object (constructor_prototpye_object_p);
192+
ecma_deref_object (constructor_object_p);
193+
194+
return result;
195+
} /* ecma_op_create_array_object_by_constructor */
196+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
197+
134198
/**
135199
* Update the length of an array to a new length
136200
*

jerry-core/ecma/operations/ecma-array-object.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ ecma_value_t
4343
ecma_op_create_array_object (const ecma_value_t *arguments_list_p, ecma_length_t arguments_list_len,
4444
bool is_treat_single_arg_as_length);
4545

46+
ecma_value_t
47+
ecma_op_create_array_object_by_constructor (const ecma_value_t *arguments_list_p, ecma_length_t arguments_list_len,
48+
bool is_treat_single_arg_as_length, ecma_object_t *object_p);
49+
4650
ecma_value_t
4751
ecma_op_array_object_set_length (ecma_object_t *object_p, ecma_value_t new_value, uint32_t flags);
4852

jerry-core/ecma/operations/ecma-eval.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
#include "js-parser.h"
2424
#include "vm.h"
2525

26-
#ifdef JERRY_ENABLE_LINE_INFO
26+
#if defined (JERRY_ENABLE_LINE_INFO) || !defined (CONFIG_DISABLE_ES2015_CLASS)
2727
#include "jcontext.h"
28-
#endif /* JERRY_ENABLE_LINE_INFO */
28+
#endif /* defined (JERRY_ENABLE_LINE_INFO) || !defined (CONFIG_DISABLE_ES2015_CLASS) */
2929

3030
/** \addtogroup ecma ECMA
3131
* @{
@@ -98,6 +98,12 @@ ecma_op_eval_chars_buffer (const lit_utf8_byte_t *code_p, /**< code characters b
9898
JERRY_CONTEXT (resource_name) = ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY);
9999
#endif /* JERRY_ENABLE_LINE_INFO */
100100

101+
#ifndef CONFIG_DISABLE_ES2015_CLASS
102+
parse_opts |= ECMA_GET_SUPER_EVAL_PARSER_OPTS (JERRY_CONTEXT (status_flags));
103+
104+
ECMA_CLEAR_SUPER_EVAL_PARSER_OPTS (JERRY_CONTEXT (status_flags));
105+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
106+
101107
ecma_value_t parse_status = parser_parse_script (NULL,
102108
0,
103109
code_p,

0 commit comments

Comments
 (0)