Skip to content

Commit 61b4070

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 afe2a80 commit 61b4070

37 files changed

+2320
-199
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: 9 additions & 7 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,17 @@ 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+
JERRY_ASSERT (type == ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND
144+
|| type == ECMA_LEXICAL_ENVIRONMENT_SUPER_OBJECT_BOUND);
142145
JERRY_ASSERT (binding_obj_p != NULL
143146
&& !ecma_is_lexical_environment (binding_obj_p));
144147

145148
ecma_object_t *new_lexical_environment_p = ecma_alloc_object ();
146149

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

151152
ecma_init_gc_info (new_lexical_environment_p);
152153

@@ -355,7 +356,8 @@ ecma_get_lex_env_binding_object (const ecma_object_t *object_p) /**< object-boun
355356
{
356357
JERRY_ASSERT (object_p != NULL);
357358
JERRY_ASSERT (ecma_is_lexical_environment (object_p));
358-
JERRY_ASSERT (ecma_get_lex_env_type (object_p) == ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND);
359+
JERRY_ASSERT (ecma_get_lex_env_type (object_p) == ECMA_LEXICAL_ENVIRONMENT_THIS_OBJECT_BOUND
360+
|| ecma_get_lex_env_type (object_p) == ECMA_LEXICAL_ENVIRONMENT_SUPER_OBJECT_BOUND);
359361

360362
return ECMA_GET_NON_NULL_POINTER (ecma_object_t,
361363
object_p->property_list_or_bound_object_cp);
@@ -766,7 +768,7 @@ ecma_free_property (ecma_object_t *object_p, /**< object the property belongs to
766768

767769
/* Must be a native pointer. */
768770
JERRY_ASSERT (ECMA_PROPERTY_GET_NAME_TYPE (*property_p) == ECMA_DIRECT_STRING_MAGIC
769-
&& (name_cp == LIT_INTERNAL_MAGIC_STRING_NATIVE_POINTER));
771+
&& name_cp >= LIT_FIRST_INTERNAL_MAGIC_STRING);
770772
break;
771773
}
772774
}

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: 61 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,19 @@ 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+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
238+
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
227239
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
228240
uint32_t new_length = 0;
229241

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

826838
JERRY_ASSERT (start <= len && end <= len);
827839

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

831855
/* 9. */
@@ -1178,7 +1202,19 @@ ecma_builtin_array_prototype_object_splice (ecma_value_t this_arg, /**< this arg
11781202

11791203
const uint32_t len = ecma_number_to_uint32 (len_number);
11801204

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

11841220
uint32_t start = 0;
@@ -1973,7 +2009,18 @@ ecma_builtin_array_prototype_object_map (ecma_value_t this_arg, /**< this argume
19732009
/* 5. arg2 is simply used as T */
19742010

19752011
/* 6. */
2012+
#ifndef CONFIG_DISABLE_ES2015_CLASS
2013+
ecma_value_t new_array = ecma_op_create_array_object_by_constructor (NULL, 0, false, obj_p);
2014+
2015+
if (ECMA_IS_VALUE_ERROR (new_array))
2016+
{
2017+
ecma_free_value (len_value);
2018+
ecma_free_value (obj_this);
2019+
return new_array;
2020+
}
2021+
#else /* CONFIG_DISABLE_ES2015_CLASS */
19762022
ecma_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
2023+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
19772024
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
19782025
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
19792026

@@ -2080,7 +2127,18 @@ ecma_builtin_array_prototype_object_filter (ecma_value_t this_arg, /**< this arg
20802127
ecma_object_t *func_object_p;
20812128

20822129
/* 6. */
2130+
#ifndef CONFIG_DISABLE_ES2015_CLASS
2131+
ecma_value_t new_array = ecma_op_create_array_object_by_constructor (NULL, 0, false, obj_p);
2132+
2133+
if (ECMA_IS_VALUE_ERROR (new_array))
2134+
{
2135+
ecma_free_value (len_value);
2136+
ecma_free_value (obj_this);
2137+
return new_array;
2138+
}
2139+
#else /* CONFIG_DISABLE_ES2015_CLASS */
20832140
ecma_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
2141+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
20842142
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
20852143
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
20862144

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: 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: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,7 @@
2222
#include "ecma-lex-env.h"
2323
#include "js-parser.h"
2424
#include "vm.h"
25-
26-
#ifdef JERRY_ENABLE_LINE_INFO
2725
#include "jcontext.h"
28-
#endif /* JERRY_ENABLE_LINE_INFO */
2926

3027
/** \addtogroup ecma ECMA
3128
* @{
@@ -98,6 +95,12 @@ ecma_op_eval_chars_buffer (const lit_utf8_byte_t *code_p, /**< code characters b
9895
JERRY_CONTEXT (resource_name) = ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY);
9996
#endif /* JERRY_ENABLE_LINE_INFO */
10097

98+
#ifndef CONFIG_DISABLE_ES2015_CLASS
99+
parse_opts |= ECMA_GET_SUPER_EVAL_PARSER_OPTS ();
100+
101+
ECMA_CLEAR_SUPER_EVAL_PARSER_OPTS ();
102+
#endif /* !CONFIG_DISABLE_ES2015_CLASS */
103+
101104
ecma_value_t parse_status = parser_parse_script (NULL,
102105
0,
103106
code_p,

0 commit comments

Comments
 (0)