Skip to content

Commit fcebdfc

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 5060579 commit fcebdfc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+2521
-198
lines changed

jerry-core/api/jerry-snapshot.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ typedef struct
4141
/**
4242
* Jerry snapshot format version.
4343
*/
44-
#define JERRY_SNAPSHOT_VERSION (18u)
44+
#define JERRY_SNAPSHOT_VERSION (19u)
4545

4646
/**
4747
* Snapshot configuration flags.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ ecma_gc_mark_property (ecma_property_pair_t *property_pair_p, /**< property pair
195195
case ECMA_PROPERTY_TYPE_INTERNAL:
196196
{
197197
JERRY_ASSERT (ECMA_PROPERTY_GET_NAME_TYPE (property) == ECMA_DIRECT_STRING_MAGIC
198-
&& property_pair_p->names_cp[index] == LIT_INTERNAL_MAGIC_STRING_NATIVE_POINTER);
198+
&& property_pair_p->names_cp[index] >= LIT_FIRST_INTERNAL_MAGIC_STRING);
199199
break;
200200
}
201201
default:

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

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,19 @@ typedef enum
8282

8383
/**
8484
* Option flags for script parsing.
85+
* Note:
86+
* The enum members must be kept in sync with parser_general_flags_t
8587
*/
8688
typedef enum
8789
{
8890
ECMA_PARSE_NO_OPTS = 0, /**< no options passed */
89-
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) */
91+
ECMA_PARSE_STRICT_MODE = (1u << 0), /**< enable strict mode */
92+
ECMA_PARSE_DIRECT_EVAL = (1u << 1), /**< eval is called directly (ECMA-262 v5, 15.1.2.1.1) */
93+
/* These three status flags must be in this order. See PARSER_CLASS_PARSE_OPTS_OFFSET. */
94+
ECMA_PARSE_CLASS_CONSTRUCTOR = (1u << 2), /**< a class constructor is being parsed (this value must be kept in
95+
* in sync with PARSER_CLASS_CONSTRUCTOR) */
96+
ECMA_PARSE_HAS_SUPER = (1u << 3), /**< the current context has super reference */
97+
ECMA_PARSE_HAS_STATIC_SUPER = (1u << 4), /**< the current context is a static class method */
9198
} ecma_parse_opts_t;
9299

93100
/**
@@ -170,6 +177,7 @@ enum
170177
* ecma_op_object_find */
171178
ECMA_VALUE_REGISTER_REF = ECMA_MAKE_VALUE (8), /**< register reference,
172179
* a special "base" value for vm */
180+
ECMA_VALUE_IMPLICIT_CONSTRUCTOR = ECMA_MAKE_VALUE (9), /**< special value for bound class constructors */
173181
};
174182

175183
#if CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT32
@@ -397,6 +405,12 @@ typedef enum
397405
#define ECMA_CONVERT_DATA_PROPERTY_TO_INTERNAL_PROPERTY(property_p) \
398406
*(property_p) = (uint8_t) (*(property_p) + (ECMA_PROPERTY_TYPE_INTERNAL - ECMA_PROPERTY_TYPE_NAMEDDATA))
399407

408+
/**
409+
* Convert internal property to data property.
410+
*/
411+
#define ECMA_CONVERT_INTERNAL_PROPERTY_TO_DATA_PROPERTY(property_p) \
412+
*(property_p) = (uint8_t) (*(property_p) - (ECMA_PROPERTY_TYPE_INTERNAL - ECMA_PROPERTY_TYPE_NAMEDDATA))
413+
400414
/**
401415
* Special property identifiers.
402416
*/
@@ -633,12 +647,42 @@ typedef enum
633647
ECMA_LEXICAL_ENVIRONMENT_DECLARATIVE = 13, /**< declarative lexical environment */
634648
ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND = 14, /**< object-bound lexical environment
635649
* with provideThis flag */
650+
ECMA_LEXICAL_ENVIRONMENT_SUPER_OBJECT_BOUND = 15, /**< object-bound lexical environment
651+
* with provided super reference */
636652

637653
ECMA_LEXICAL_ENVIRONMENT_TYPE_START = ECMA_LEXICAL_ENVIRONMENT_DECLARATIVE, /**< first lexical
638-
* environment type */
639-
ECMA_LEXICAL_ENVIRONMENT_TYPE__MAX = ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND /**< maximum value */
654+
* environment type */
655+
ECMA_LEXICAL_ENVIRONMENT_TYPE__MAX = ECMA_LEXICAL_ENVIRONMENT_SUPER_OBJECT_BOUND /**< maximum value */
640656
} ecma_lexical_environment_type_t;
641657

658+
/**
659+
* Offset for JERRY_CONTEXT (status_flags) top 8 bits.
660+
*/
661+
#define ECMA_SUPER_EVAL_OPTS_OFFSET (32 - 8)
662+
663+
/**
664+
* Set JERRY_CONTEXT (status_flags) top 8 bits to the specified 'opts'.
665+
*/
666+
#define ECMA_SET_SUPER_EVAL_PARSER_OPTS(opts) \
667+
do \
668+
{ \
669+
JERRY_CONTEXT (status_flags) |= ((uint32_t) opts << ECMA_SUPER_EVAL_OPTS_OFFSET) | ECMA_STATUS_DIRECT_EVAL; \
670+
} while (0)
671+
672+
/**
673+
* Get JERRY_CONTEXT (status_flags) top 8 bits.
674+
*/
675+
#define ECMA_GET_SUPER_EVAL_PARSER_OPTS() (JERRY_CONTEXT (status_flags) >> ECMA_SUPER_EVAL_OPTS_OFFSET)
676+
677+
/**
678+
* Clear JERRY_CONTEXT (status_flags) top 8 bits.
679+
*/
680+
#define ECMA_CLEAR_SUPER_EVAL_PARSER_OPTS() \
681+
do \
682+
{ \
683+
JERRY_CONTEXT (status_flags) &= ((1 << ECMA_SUPER_EVAL_OPTS_OFFSET) - 1); \
684+
} while (0)
685+
642686
/**
643687
* Ecma object type mask for getting the object type.
644688
*/

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

Lines changed: 17 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,22 @@ 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
{
143+
#ifndef CONFIG_DISABLE_ES2015_CLASS
144+
JERRY_ASSERT (type == ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND
145+
|| type == ECMA_LEXICAL_ENVIRONMENT_SUPER_OBJECT_BOUND);
146+
#else /* CONFIG_DISABLE_ES2015_CLASS */
147+
JERRY_ASSERT (type == ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND);
148+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
149+
142150
JERRY_ASSERT (binding_obj_p != NULL
143151
&& !ecma_is_lexical_environment (binding_obj_p));
144152

145153
ecma_object_t *new_lexical_environment_p = ecma_alloc_object ();
146154

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;
155+
new_lexical_environment_p->type_flags_refs = (uint16_t) (ECMA_OBJECT_FLAG_BUILT_IN_OR_LEXICAL_ENV | type);
150156

151157
ecma_init_gc_info (new_lexical_environment_p);
152158

@@ -355,7 +361,12 @@ ecma_get_lex_env_binding_object (const ecma_object_t *object_p) /**< object-boun
355361
{
356362
JERRY_ASSERT (object_p != NULL);
357363
JERRY_ASSERT (ecma_is_lexical_environment (object_p));
364+
#ifndef CONFIG_DISABLE_ES2015
365+
JERRY_ASSERT (ecma_get_lex_env_type (object_p) == ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND
366+
|| ecma_get_lex_env_type (object_p) == ECMA_LEXICAL_ENVIRONMENT_SUPER_OBJECT_BOUND);
367+
#else /* CONFIG_DISABLE_ES2015 */
358368
JERRY_ASSERT (ecma_get_lex_env_type (object_p) == ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND);
369+
#endif /* !CONFIG_DISABLE_ES2015 */
359370

360371
return ECMA_GET_NON_NULL_POINTER (ecma_object_t,
361372
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
766777

767778
/* Must be a native pointer. */
768779
JERRY_ASSERT (ECMA_PROPERTY_GET_NAME_TYPE (*property_p) == ECMA_DIRECT_STRING_MAGIC
769-
&& (name_cp == LIT_INTERNAL_MAGIC_STRING_NATIVE_POINTER));
780+
&& name_cp >= LIT_FIRST_INTERNAL_MAGIC_STRING);
770781
break;
771782
}
772783
}

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: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,20 @@ ecma_builtin_array_prototype_object_concat (ecma_value_t this_arg, /**< this arg
223223
ret_value);
224224

225225
/* 2. */
226-
ecma_value_t new_array = ecma_op_create_array_object (0, 0, false);
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 (NULL, 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 */
236+
ecma_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
237+
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
238+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
239+
227240
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
228241
uint32_t new_length = 0;
229242

@@ -825,7 +838,20 @@ ecma_builtin_array_prototype_object_slice (ecma_value_t this_arg, /**< 'this' ar
825838

826839
JERRY_ASSERT (start <= len && end <= len);
827840

828-
ecma_value_t new_array = ecma_op_create_array_object (0, 0, false);
841+
#ifndef CONFIG_DISABLE_ES2015_CLASS
842+
ecma_value_t new_array = ecma_op_create_array_object_by_constructor (NULL, 0, false, obj_p);
843+
844+
if (ECMA_IS_VALUE_ERROR (new_array))
845+
{
846+
ecma_free_value (len_value);
847+
ecma_free_value (obj_this);
848+
return new_array;
849+
}
850+
#else /* CONFIG_DISABLE_ES2015_CLASS */
851+
ecma_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
852+
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
853+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
854+
829855
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
830856

831857
/* 9. */
@@ -1178,7 +1204,20 @@ ecma_builtin_array_prototype_object_splice (ecma_value_t this_arg, /**< this arg
11781204

11791205
const uint32_t len = ecma_number_to_uint32 (len_number);
11801206

1181-
ecma_value_t new_array = ecma_op_create_array_object (0, 0, false);
1207+
#ifndef CONFIG_DISABLE_ES2015_CLASS
1208+
ecma_value_t new_array = ecma_op_create_array_object_by_constructor (NULL, 0, false, obj_p);
1209+
1210+
if (ECMA_IS_VALUE_ERROR (new_array))
1211+
{
1212+
ecma_free_value (len_value);
1213+
ecma_free_value (obj_this);
1214+
return new_array;
1215+
}
1216+
#else /* CONFIG_DISABLE_ES2015_CLASS */
1217+
ecma_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
1218+
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
1219+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
1220+
11821221
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
11831222

11841223
uint32_t start = 0;
@@ -1973,8 +2012,20 @@ ecma_builtin_array_prototype_object_map (ecma_value_t this_arg, /**< this argume
19732012
/* 5. arg2 is simply used as T */
19742013

19752014
/* 6. */
2015+
#ifndef CONFIG_DISABLE_ES2015_CLASS
2016+
ecma_value_t new_array = ecma_op_create_array_object_by_constructor (NULL, 0, false, obj_p);
2017+
2018+
if (ECMA_IS_VALUE_ERROR (new_array))
2019+
{
2020+
ecma_free_value (len_value);
2021+
ecma_free_value (obj_this);
2022+
return new_array;
2023+
}
2024+
#else /* CONFIG_DISABLE_ES2015_CLASS */
19762025
ecma_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
19772026
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
2027+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
2028+
19782029
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
19792030

19802031
/* 7-8. */
@@ -2080,8 +2131,20 @@ ecma_builtin_array_prototype_object_filter (ecma_value_t this_arg, /**< this arg
20802131
ecma_object_t *func_object_p;
20812132

20822133
/* 6. */
2134+
#ifndef CONFIG_DISABLE_ES2015_CLASS
2135+
ecma_value_t new_array = ecma_op_create_array_object_by_constructor (NULL, 0, false, obj_p);
2136+
2137+
if (ECMA_IS_VALUE_ERROR (new_array))
2138+
{
2139+
ecma_free_value (len_value);
2140+
ecma_free_value (obj_this);
2141+
return new_array;
2142+
}
2143+
#else /* CONFIG_DISABLE_ES2015_CLASS */
20832144
ecma_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
20842145
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
2146+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
2147+
20852148
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
20862149

20872150
/* We already checked that arg1 is callable, so it will always be an object. */

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

Lines changed: 69 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "ecma-number-arithmetic.h"
2525
#include "ecma-objects.h"
2626
#include "ecma-objects-general.h"
27+
#include "ecma-function-object.h"
2728

2829
/** \addtogroup ecma ECMA
2930
* @{
@@ -43,14 +44,14 @@
4344
*/
4445
ecma_value_t
4546
ecma_op_create_array_object (const ecma_value_t *arguments_list_p, /**< list of arguments that
46-
are passed to Array constructor */
47+
* are passed to Array constructor */
4748
ecma_length_t arguments_list_len, /**< length of the arguments' list */
4849
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 */
50+
* arguments_list_len is 1
51+
* and single argument is Number,
52+
* then treat the single argument
53+
* as new Array's length rather
54+
* than as single item of the Array */
5455
{
5556
JERRY_ASSERT (arguments_list_len == 0
5657
|| arguments_list_p != NULL);
@@ -131,6 +132,68 @@ ecma_op_create_array_object (const ecma_value_t *arguments_list_p, /**< list of
131132
return ecma_make_object_value (object_p);
132133
} /* ecma_op_create_array_object */
133134

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

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ 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+
#ifndef CONFIG_DISABLE_ES2015_CLASS
47+
ecma_value_t
48+
ecma_op_create_array_object_by_constructor (const ecma_value_t *arguments_list_p, ecma_length_t arguments_list_len,
49+
bool is_treat_single_arg_as_length, ecma_object_t *object_p);
50+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
51+
4652
ecma_value_t
4753
ecma_op_array_object_set_length (ecma_object_t *object_p, ecma_value_t new_value, uint32_t flags);
4854

0 commit comments

Comments
 (0)