Skip to content

Commit 8b41bf3

Browse files
authored
Introduce builtin intrinsic object to share property values between builtin objects (#3490)
Fixed: - Global symbol access - Array.prototype.values and Array.prototype[Symbol.iterator] must be the same function object To test the new functionality arguments object Symbol.iterator property is added. JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
1 parent 332e216 commit 8b41bf3

26 files changed

Lines changed: 349 additions & 110 deletions

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -666,7 +666,7 @@ typedef enum
666666
ECMA_ITERATOR_KEYS, /**< List only key indices */
667667
ECMA_ITERATOR_VALUES, /**< List only key values */
668668
ECMA_ITERATOR_KEYS_VALUES, /**< List key indices and values */
669-
} ecma_iterator_type_t;
669+
} ecma_array_iterator_type_t;
670670

671671
#endif /* ENABLED (JERRY_ES2015) */
672672

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

Lines changed: 4 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include "ecma-gc.h"
2525
#include "ecma-globals.h"
2626
#include "ecma-helpers.h"
27-
#include "ecma-iterator-object.h"
2827
#include "ecma-objects.h"
2928
#include "ecma-string-object.h"
3029
#include "lit-char-helpers.h"
@@ -73,7 +72,6 @@ enum
7372
ECMA_ARRAY_PROTOTYPE_FIND,
7473
ECMA_ARRAY_PROTOTYPE_FIND_INDEX,
7574
ECMA_ARRAY_PROTOTYPE_ENTRIES,
76-
ECMA_ARRAY_PROTOTYPE_VALUES,
7775
ECMA_ARRAY_PROTOTYPE_KEYS,
7876
ECMA_ARRAY_PROTOTYPE_SYMBOL_ITERATOR,
7977
ECMA_ARRAY_PROTOTYPE_FILL,
@@ -2493,36 +2491,6 @@ ecma_builtin_array_prototype_object_copy_within (const ecma_value_t args[], /**<
24932491

24942492
return ecma_copy_value (ecma_make_object_value (obj_p));
24952493
} /* ecma_builtin_array_prototype_object_copy_within */
2496-
2497-
/**
2498-
* Helper function for Array.prototype object's {'keys', 'values', 'entries', '@@iterator'}
2499-
* routines common parts.
2500-
*
2501-
* See also:
2502-
* ECMA-262 v6, 22.1.3.4
2503-
* ECMA-262 v6, 22.1.3.13
2504-
* ECMA-262 v6, 22.1.3.29
2505-
* ECMA-262 v6, 22.1.3.30
2506-
*
2507-
* Note:
2508-
* Returned value must be freed with ecma_free_value.
2509-
*
2510-
* @return iterator result object, if success
2511-
* error - otherwise
2512-
*/
2513-
static ecma_value_t
2514-
ecma_builtin_array_iterators_helper (ecma_object_t *obj_p, /**< array object */
2515-
uint8_t type) /**< any combination of
2516-
* ecma_iterator_type_t bits */
2517-
{
2518-
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_ARRAY_ITERATOR_PROTOTYPE);
2519-
2520-
return ecma_op_create_iterator_object (ecma_make_object_value (obj_p),
2521-
prototype_obj_p,
2522-
ECMA_PSEUDO_ARRAY_ITERATOR,
2523-
type);
2524-
} /* ecma_builtin_array_iterators_helper */
2525-
25262494
#endif /* ENABLED (JERRY_ES2015) */
25272495

25282496
/**
@@ -2570,24 +2538,18 @@ ecma_builtin_array_prototype_dispatch_routine (uint16_t builtin_routine_id, /**<
25702538

25712539
#if ENABLED (JERRY_ES2015)
25722540
if (JERRY_UNLIKELY (builtin_routine_id >= ECMA_ARRAY_PROTOTYPE_ENTRIES
2573-
&& builtin_routine_id <= ECMA_ARRAY_PROTOTYPE_SYMBOL_ITERATOR))
2541+
&& builtin_routine_id <= ECMA_ARRAY_PROTOTYPE_KEYS))
25742542
{
25752543
ecma_value_t ret_value;
25762544

25772545
if (builtin_routine_id == ECMA_ARRAY_PROTOTYPE_ENTRIES)
25782546
{
2579-
ret_value = ecma_builtin_array_iterators_helper (obj_p, ECMA_ITERATOR_KEYS_VALUES);
2580-
}
2581-
else if (builtin_routine_id == ECMA_ARRAY_PROTOTYPE_KEYS)
2582-
{
2583-
ret_value = ecma_builtin_array_iterators_helper (obj_p, ECMA_ITERATOR_KEYS);
2547+
ret_value = ecma_op_create_array_iterator (obj_p, ECMA_ITERATOR_KEYS_VALUES);
25842548
}
25852549
else
25862550
{
2587-
JERRY_ASSERT (builtin_routine_id == ECMA_ARRAY_PROTOTYPE_VALUES
2588-
|| builtin_routine_id == ECMA_ARRAY_PROTOTYPE_SYMBOL_ITERATOR);
2589-
2590-
ret_value = ecma_builtin_array_iterators_helper (obj_p, ECMA_ITERATOR_VALUES);
2551+
JERRY_ASSERT (builtin_routine_id == ECMA_ARRAY_PROTOTYPE_KEYS);
2552+
ret_value = ecma_op_create_array_iterator (obj_p, ECMA_ITERATOR_KEYS);
25912553
}
25922554

25932555
ecma_deref_object (obj_p);

jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.inc.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ ROUTINE (LIT_MAGIC_STRING_FIND_INDEX, ECMA_ARRAY_PROTOTYPE_FIND_INDEX, 2, 1)
7575
ROUTINE (LIT_MAGIC_STRING_FILL, ECMA_ARRAY_PROTOTYPE_FILL, 3, 1)
7676
ROUTINE (LIT_MAGIC_STRING_COPY_WITHIN, ECMA_ARRAY_PROTOTYPE_COPY_WITHIN, NON_FIXED, 2)
7777
ROUTINE (LIT_MAGIC_STRING_ENTRIES, ECMA_ARRAY_PROTOTYPE_ENTRIES, 0, 0)
78-
ROUTINE (LIT_MAGIC_STRING_VALUES, ECMA_ARRAY_PROTOTYPE_VALUES, 0, 0)
7978
ROUTINE (LIT_MAGIC_STRING_KEYS, ECMA_ARRAY_PROTOTYPE_KEYS, 0, 0)
80-
ROUTINE (LIT_GLOBAL_SYMBOL_ITERATOR, ECMA_ARRAY_PROTOTYPE_SYMBOL_ITERATOR, 0, 0)
79+
INTRINSIC_PROPERTY (LIT_MAGIC_STRING_VALUES, LIT_INTERNAL_MAGIC_STRING_ARRAY_PROTOTYPE_VALUES)
80+
INTRINSIC_PROPERTY (LIT_GLOBAL_SYMBOL_ITERATOR, LIT_INTERNAL_MAGIC_STRING_ARRAY_PROTOTYPE_VALUES)
8181
#endif /* ENABLED (JERRY_ES2015) */
8282

8383
#endif /* ENABLED (JERRY_BUILTIN_ARRAY) */

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ ecma_builtin_array_object_from (ecma_value_t this_arg, /**< 'this' argument */
108108
}
109109

110110
/* 4. */
111-
ecma_value_t using_iterator = ecma_op_get_method_by_symbol_id (items, LIT_MAGIC_STRING_ITERATOR);
111+
ecma_value_t using_iterator = ecma_op_get_method_by_symbol_id (items, LIT_GLOBAL_SYMBOL_ITERATOR);
112112

113113
/* 5. */
114114
if (ECMA_IS_VALUE_ERROR (using_iterator))

jerry-core/ecma/builtin-objects/ecma-builtin-helpers-macro-defines.inc.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,12 @@
2727

2828
#if ENABLED (JERRY_ES2015)
2929
#ifndef SYMBOL_VALUE
30-
#define SYMBOL_VALUE(name, desc_string_id)
30+
#define SYMBOL_VALUE(symbol, desc_magic_string_id)
3131
#endif /* !SYMBOL_VALUE */
32+
33+
#ifndef INTRINSIC_PROPERTY
34+
#define INTRINSIC_PROPERTY(name, magic_string_id)
35+
#endif /* !INTRINSIC_PROPERTY */
3236
#endif /* ENABLED (JERRY_ES2015) */
3337

3438
#ifndef OBJECT_VALUE

jerry-core/ecma/builtin-objects/ecma-builtin-helpers-macro-undefs.inc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#undef STRING_VALUE
1919
#if ENABLED (JERRY_ES2015)
2020
#undef SYMBOL_VALUE
21+
#undef INTRINSIC_PROPERTY
2122
#endif /* ENABLED (JERRY_ES2015) */
2223
#undef OBJECT_VALUE
2324
#undef ROUTINE

jerry-core/ecma/builtin-objects/ecma-builtin-helpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ ecma_builtin_helper_object_to_string (const ecma_value_t this_arg) /**< this arg
142142
type_string = ecma_object_get_class_name (obj_p);
143143

144144
#if ENABLED (JERRY_ES2015)
145-
ecma_value_t tag_value = ecma_op_object_get_by_symbol_id (obj_p, LIT_MAGIC_STRING_TO_STRING_TAG);
145+
ecma_value_t tag_value = ecma_op_object_get_by_symbol_id (obj_p, LIT_GLOBAL_SYMBOL_TO_STRING_TAG);
146146

147147
if (ECMA_IS_VALUE_ERROR (tag_value))
148148
{

jerry-core/ecma/builtin-objects/ecma-builtin-internal-routines-template.inc.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,19 @@ const ecma_builtin_property_descriptor_t PROPERTY_DESCRIPTOR_LIST_NAME[] =
175175
magic_string_id \
176176
},
177177
#if ENABLED (JERRY_ES2015)
178-
#define SYMBOL_VALUE(name, desc_string_id) \
178+
#define SYMBOL_VALUE(symbol, desc_magic_string_id) \
179179
{ \
180-
name, \
180+
symbol, \
181181
ECMA_BUILTIN_PROPERTY_SYMBOL, \
182182
ECMA_PROPERTY_FIXED, \
183-
desc_string_id \
183+
desc_magic_string_id \
184+
},
185+
#define INTRINSIC_PROPERTY(name, magic_string_id) \
186+
{ \
187+
name, \
188+
ECMA_BUILTIN_PROPERTY_INTRINSIC_PROPERTY, \
189+
ECMA_PROPERTY_CONFIGURABLE_WRITABLE, \
190+
magic_string_id \
184191
},
185192
#endif /* ENABLED (JERRY_ES2015) */
186193
#define ACCESSOR_READ_WRITE(name, c_getter_name, c_setter_name, prop_attributes) \
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#include "ecma-builtins.h"
17+
#include "ecma-array-object.h"
18+
#include "ecma-gc.h"
19+
20+
#if ENABLED (JERRY_ES2015)
21+
22+
#define ECMA_BUILTINS_INTERNAL
23+
#include "ecma-builtins-internal.h"
24+
25+
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-intrinsic.inc.h"
26+
#define BUILTIN_UNDERSCORED_ID intrinsic
27+
#include "ecma-builtin-internal-routines-template.inc.h"
28+
29+
/** \addtogroup ecma ECMA
30+
* @{
31+
*
32+
* \addtogroup ecmabuiltins
33+
* @{
34+
*
35+
* \addtogroup intrinsic ECMA Intrinsic object built-in
36+
* @{
37+
*/
38+
39+
/**
40+
* The %ArrayProto_values% intrinsic routine
41+
*
42+
* See also:
43+
* ECMA-262 v5, 15.4.4.4
44+
*
45+
* @return ecma value
46+
* Returned value must be freed with ecma_free_value.
47+
*/
48+
static ecma_value_t
49+
ecma_builtin_intrinsic_array_prototype_values (ecma_value_t this_value) /**< this argument */
50+
{
51+
ecma_value_t this_obj = ecma_op_to_object (this_value);
52+
53+
if (ECMA_IS_VALUE_ERROR (this_obj))
54+
{
55+
return this_obj;
56+
}
57+
58+
ecma_object_t *this_obj_p = ecma_get_object_from_value (this_obj);
59+
60+
ecma_value_t ret_value = ecma_op_create_array_iterator (this_obj_p, ECMA_ITERATOR_VALUES);
61+
62+
ecma_deref_object (this_obj_p);
63+
64+
return ret_value;
65+
} /* ecma_builtin_intrinsic_array_prototype_values */
66+
67+
/**
68+
* @}
69+
* @}
70+
* @}
71+
*/
72+
73+
#endif /* ENABLED (JERRY_ES2015) */
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
/*
17+
* Intrinsic built-in description
18+
*/
19+
20+
#include "ecma-builtin-helpers-macro-defines.inc.h"
21+
22+
#if ENABLED (JERRY_ES2015)
23+
24+
/* ECMA-262 v6, 19.4.2.2 */
25+
SYMBOL_VALUE (LIT_GLOBAL_SYMBOL_HAS_INSTANCE,
26+
LIT_MAGIC_STRING_HAS_INSTANCE)
27+
28+
/* ECMA-262 v6, 19.4.2.3 */
29+
SYMBOL_VALUE (LIT_GLOBAL_SYMBOL_IS_CONCAT_SPREADABLE,
30+
LIT_MAGIC_STRING_IS_CONCAT_SPREADABLE)
31+
32+
/* ECMA-262 v6, 19.4.2.4 */
33+
SYMBOL_VALUE (LIT_GLOBAL_SYMBOL_ITERATOR,
34+
LIT_MAGIC_STRING_ITERATOR)
35+
36+
/* ECMA-262 v6, 19.4.2.6 */
37+
SYMBOL_VALUE (LIT_GLOBAL_SYMBOL_MATCH,
38+
LIT_MAGIC_STRING_MATCH)
39+
40+
/* ECMA-262 v6, 19.4.2.8 */
41+
SYMBOL_VALUE (LIT_GLOBAL_SYMBOL_REPLACE,
42+
LIT_MAGIC_STRING_REPLACE)
43+
44+
/* ECMA-262 v6, 19.4.2.9 */
45+
SYMBOL_VALUE (LIT_GLOBAL_SYMBOL_SEARCH,
46+
LIT_MAGIC_STRING_SEARCH)
47+
48+
/* ECMA-262 v6, 19.4.2.10 */
49+
SYMBOL_VALUE (LIT_GLOBAL_SYMBOL_SPECIES,
50+
LIT_MAGIC_STRING_SPECIES)
51+
52+
/* ECMA-262 v6, 19.4.2.11 */
53+
SYMBOL_VALUE (LIT_GLOBAL_SYMBOL_SPLIT,
54+
LIT_MAGIC_STRING_SPLIT)
55+
56+
/* ECMA-262 v6, 19.4.2.12 */
57+
SYMBOL_VALUE (LIT_GLOBAL_SYMBOL_TO_PRIMITIVE,
58+
LIT_MAGIC_STRING_TO_PRIMITIVE)
59+
60+
/* ECMA-262 v6, 19.4.2.13 */
61+
SYMBOL_VALUE (LIT_GLOBAL_SYMBOL_TO_STRING_TAG,
62+
LIT_MAGIC_STRING_TO_STRING_TAG)
63+
64+
/* ECMA-262 v6, 19.4.2.14 */
65+
SYMBOL_VALUE (LIT_GLOBAL_SYMBOL_UNSCOPABLES,
66+
LIT_MAGIC_STRING_UNSCOPABLES)
67+
68+
ROUTINE (LIT_INTERNAL_MAGIC_STRING_ARRAY_PROTOTYPE_VALUES, ecma_builtin_intrinsic_array_prototype_values, 0, 0)
69+
70+
#endif /* ENABLED (JERRY_ES2015) */
71+
#include "ecma-builtin-helpers-macro-undefs.inc.h"

0 commit comments

Comments
 (0)