Skip to content

Commit 9b1a982

Browse files
committed
Implement Iterator interface and Array iterators
JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
1 parent ae91800 commit 9b1a982

16 files changed

+909
-4
lines changed

jerry-core/config.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
# define CONFIG_DISABLE_ES2015_BUILTIN
4141
# define CONFIG_DISABLE_ES2015_CLASS
4242
# define CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER
43+
# define CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN
4344
# define CONFIG_DISABLE_ES2015_MAP_BUILTIN
4445
# define CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER
4546
# define CONFIG_DISABLE_ES2015_PROMISE_BUILTIN

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939
#ifndef CONFIG_DISABLE_ES2015_MAP_BUILTIN
4040
#include "ecma-map-object.h"
4141
#endif /* !CONFIG_DISABLE_ES2015_MAP_BUILTIN */
42+
#ifndef CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN
43+
#include "ecma-iterator-object.h"
44+
#endif /* !CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN */
4245

4346
/* TODO: Extract GC to a separate component */
4447

@@ -369,6 +372,12 @@ ecma_gc_mark (ecma_object_t *object_p) /**< object to mark from */
369372
break;
370373
}
371374
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */
375+
#ifndef CONFIG_DISABLE_ES2015_BUILTIN
376+
case ECMA_PSEUDO_ARRAY_ITERATOR:
377+
{
378+
break;
379+
}
380+
#endif /* !CONFIG_DISABLE_ES2015_BUILTIN */
372381
default:
373382
{
374383
JERRY_ASSERT (ext_object_p->u.pseudo_array.type == ECMA_PSEUDO_ARRAY_ARGUMENTS);
@@ -759,6 +768,13 @@ ecma_gc_free_object (ecma_object_t *object_p) /**< object to free */
759768
return;
760769
}
761770
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */
771+
#ifndef CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN
772+
case ECMA_PSEUDO_ARRAY_ITERATOR:
773+
{
774+
ecma_dealloc_extended_object (object_p, sizeof (ecma_extended_object_t));
775+
return;
776+
}
777+
#endif /* !CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN */
762778
default:
763779
{
764780
JERRY_ASSERT (ext_object_p->u.pseudo_array.type == ECMA_PSEUDO_ARRAY_ARGUMENTS);

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -659,8 +659,9 @@ typedef enum
659659
ECMA_PSEUDO_ARRAY_ARGUMENTS = 0, /**< Arguments object (10.6) */
660660
ECMA_PSEUDO_ARRAY_TYPEDARRAY = 1, /**< TypedArray which does NOT need extra space to store length and offset */
661661
ECMA_PSEUDO_ARRAY_TYPEDARRAY_WITH_INFO = 2, /**< TypedArray which NEEDS extra space to store length and offset */
662+
ECMA_PSEUDO_ARRAY_ITERATOR = 3, /**< Array iterator object (ECMAScript v6, 22.1.5.1) */
662663

663-
ECMA_PSEUDO_ARRAY__MAX = ECMA_PSEUDO_ARRAY_TYPEDARRAY_WITH_INFO /**< maximum value */
664+
ECMA_PSEUDO_ARRAY__MAX = ECMA_PSEUDO_ARRAY_ITERATOR /**< maximum value */
664665
} ecma_pseudo_array_type_t;
665666

666667
/**
@@ -681,6 +682,16 @@ typedef enum
681682
ECMA_LEXICAL_ENVIRONMENT_TYPE__MAX = ECMA_LEXICAL_ENVIRONMENT_SUPER_OBJECT_BOUND /**< maximum value */
682683
} ecma_lexical_environment_type_t;
683684

685+
/**
686+
* Types of array iterators.
687+
*/
688+
typedef enum
689+
{
690+
ECMA_ARRAY_ITERATOR_KEYS, /**< List only key indices */
691+
ECMA_ARRAY_ITERATOR_VALUES, /**< List only key values */
692+
ECMA_ARRAY_ITERATOR_KEYS_VALUES, /**< List key indices and values */
693+
} ecma_array_iterator_type_t;
694+
684695
/**
685696
* Offset for JERRY_CONTEXT (status_flags) top 8 bits.
686697
*/
@@ -841,18 +852,21 @@ typedef struct
841852
*/
842853
struct
843854
{
844-
uint8_t type; /**< pseudo array type, e.g. Arguments, TypedArray*/
855+
uint8_t type; /**< pseudo array type, e.g. Arguments, TypedArray, ArrayIterator */
845856
uint8_t extra_info; /**< extra information about the object.
846-
* e.g. element_width_shift for typed arrays */
857+
* e.g. element_width_shift for typed arrays,
858+
* elementKind for ArrayIterator */
847859
union
848860
{
849861
uint16_t length; /**< for arguments: length of names */
850862
uint16_t class_id; /**< for typedarray: the specific class name */
863+
uint16_t iterator_index; /**< for ArrayIterator: [[ArrayIteratorNextIndex]] property */
851864
} u1;
852865
union
853866
{
854867
ecma_value_t lex_env_cp; /**< for arguments: lexical environment */
855868
ecma_value_t arraybuffer; /**< for typedarray: internal arraybuffer */
869+
ecma_value_t iterated_value_cp; /**< for ArrayIterator: [[ArrayIteratorNextIndex]] property */
856870
} u2;
857871
} pseudo_array;
858872

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "ecma-gc.h"
2525
#include "ecma-globals.h"
2626
#include "ecma-helpers.h"
27+
#include "ecma-iterator-object.h"
2728
#include "ecma-objects.h"
2829
#include "ecma-string-object.h"
2930
#include "ecma-try-catch-macro.h"
@@ -2504,6 +2505,81 @@ ecma_builtin_array_prototype_object_find (ecma_value_t this_arg, /**< this argum
25042505
} /* ecma_builtin_array_prototype_object_find */
25052506
#endif /* !CONFIG_DISABLE_ES2015_BUILTIN */
25062507

2508+
#ifndef CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN
2509+
/**
2510+
* Helper function for Array.prototype object's {'keys', 'values', 'entries'}
2511+
* routines common parts.
2512+
*
2513+
* @return ecma value
2514+
* Returned value must be freed with ecma_free_value.
2515+
*/
2516+
static ecma_value_t
2517+
ecma_builtin_array_iterators_helper (ecma_value_t this_arg, /**< this argument */
2518+
uint8_t type) /**< any combination of
2519+
* ecma_array_iterator_type_t bits */
2520+
{
2521+
/* 1. */
2522+
ecma_value_t obj_this = ecma_op_to_object (this_arg);
2523+
2524+
if (ECMA_IS_VALUE_ERROR (obj_this))
2525+
{
2526+
/* 2. */
2527+
return obj_this;
2528+
}
2529+
2530+
/* 3. */
2531+
ecma_value_t ret_value = ecma_op_create_iterator_object (obj_this, ECMA_PSEUDO_ARRAY_ITERATOR, type);
2532+
ecma_free_value (obj_this);
2533+
2534+
return ret_value;
2535+
} /* ecma_builtin_array_iterators_helper */
2536+
2537+
/**
2538+
* The Array.prototype object's 'entries' routine
2539+
*
2540+
* See also:
2541+
* ECMA-262 v6, 22.1.3.4
2542+
*
2543+
* @return ecma value
2544+
* Returned value must be freed with ecma_free_value.
2545+
*/
2546+
static ecma_value_t
2547+
ecma_builtin_array_prototype_entries (ecma_value_t this_arg) /**< this argument */
2548+
{
2549+
return ecma_builtin_array_iterators_helper (this_arg, ECMA_ARRAY_ITERATOR_KEYS_VALUES);
2550+
} /* ecma_builtin_array_prototype_entries */
2551+
2552+
/**
2553+
* The Array.prototype object's 'values' routine
2554+
*
2555+
* See also:
2556+
* ECMA-262 v6, 22.1.3.29.
2557+
*
2558+
* @return ecma value
2559+
* Returned value must be freed with ecma_free_value.
2560+
*/
2561+
static ecma_value_t
2562+
ecma_builtin_array_prototype_values (ecma_value_t this_arg) /**< this argument */
2563+
{
2564+
return ecma_builtin_array_iterators_helper (this_arg, ECMA_ARRAY_ITERATOR_VALUES);
2565+
} /* ecma_builtin_array_prototype_values */
2566+
2567+
/**
2568+
* The Array.prototype object's 'keys' routine
2569+
*
2570+
* See also:
2571+
* ECMA-262 v6, 22.1.3.13
2572+
*
2573+
* @return ecma value
2574+
* Returned value must be freed with ecma_free_value.
2575+
*/
2576+
static ecma_value_t
2577+
ecma_builtin_array_prototype_keys (ecma_value_t this_arg) /**< this argument */
2578+
{
2579+
return ecma_builtin_array_iterators_helper (this_arg, ECMA_ARRAY_ITERATOR_KEYS);
2580+
} /* ecma_builtin_array_prototype_keys */
2581+
#endif /* !CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN */
2582+
25072583
/**
25082584
* @}
25092585
* @}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ ROUTINE (LIT_MAGIC_STRING_FILTER, ecma_builtin_array_prototype_object_filter, 2,
6161
ROUTINE (LIT_MAGIC_STRING_REDUCE, ecma_builtin_array_prototype_object_reduce, NON_FIXED, 1)
6262
ROUTINE (LIT_MAGIC_STRING_REDUCE_RIGHT_UL, ecma_builtin_array_prototype_object_reduce_right, NON_FIXED, 1)
6363
#ifndef CONFIG_DISABLE_ES2015_BUILTIN
64+
ROUTINE (LIT_MAGIC_STRING_ENTRIES, ecma_builtin_array_prototype_entries, 0, 0)
65+
ROUTINE (LIT_MAGIC_STRING_VALUES, ecma_builtin_array_prototype_values, 0, 0)
66+
ROUTINE (LIT_MAGIC_STRING_KEYS, ecma_builtin_array_prototype_keys, 0, 0)
6467
ROUTINE (LIT_MAGIC_STRING_FIND, ecma_builtin_array_prototype_object_find, 2, 1)
6568
#endif /* !CONFIG_DISABLE_ES2015_BUILTIN */
6669

Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
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-alloc.h"
17+
#include "ecma-array-object.h"
18+
#include "ecma-typedarray-object.h"
19+
#include "ecma-array-iterator-object.h"
20+
#include "ecma-builtin-helpers.h"
21+
#include "ecma-builtins.h"
22+
#include "ecma-exceptions.h"
23+
#include "ecma-gc.h"
24+
#include "ecma-globals.h"
25+
#include "ecma-helpers.h"
26+
#include "ecma-iterator-object.h"
27+
#include "ecma-number-arithmetic.h"
28+
#include "ecma-objects.h"
29+
#include "ecma-objects-general.h"
30+
#include "ecma-function-object.h"
31+
32+
/** \addtogroup ecma ECMA
33+
* @{
34+
*
35+
* \addtogroup ecmaarrayiteratorobject ECMA Array iterator object related routines
36+
* @{
37+
*/
38+
39+
#ifndef CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN
40+
/**
41+
* The %ArrayIteratorPrototype% object's 'next' routine
42+
*
43+
* See also:
44+
* ECMA-262 v6, 22.1.5.2.1
45+
*
46+
* @return ecma value
47+
* Returned value must be freed with ecma_free_value.
48+
*/
49+
ecma_value_t
50+
ecma_op_array_iterator_next (const ecma_value_t function_obj, /**< the function itself */
51+
const ecma_value_t this_val, /**< this_arg of the function */
52+
const ecma_value_t args_p[], /**< argument list */
53+
const ecma_length_t args_count) /**< argument number */
54+
{
55+
JERRY_UNUSED_3 (function_obj, args_p, args_count);
56+
57+
/* 1 - 2. */
58+
if (!ecma_is_value_object (this_val))
59+
{
60+
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not an object."));
61+
}
62+
63+
ecma_object_t *obj_p = ecma_get_object_from_value (this_val);
64+
ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) obj_p;
65+
66+
/* 3. */
67+
if (ext_obj_p->u.pseudo_array.type != ECMA_PSEUDO_ARRAY_ITERATOR)
68+
{
69+
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not an iterator."));
70+
}
71+
72+
ecma_object_t *array_object_p = ECMA_GET_POINTER (ecma_object_t,
73+
ext_obj_p->u.pseudo_array.u2.iterated_value_cp);
74+
75+
76+
/* 4 - 5 */
77+
if (array_object_p == NULL)
78+
{
79+
return ecma_create_iter_result_object (ECMA_VALUE_UNDEFINED, ECMA_VALUE_TRUE);
80+
}
81+
82+
uint32_t length;
83+
84+
/* 8 - 9. */
85+
#ifndef CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN
86+
if (ecma_is_typedarray (ecma_make_object_value (array_object_p)))
87+
{
88+
length = ecma_typedarray_get_length (array_object_p);
89+
}
90+
else
91+
{
92+
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */
93+
ecma_value_t length_value = ecma_op_object_get_by_magic_id (array_object_p, LIT_MAGIC_STRING_LENGTH);
94+
95+
if (ECMA_IS_VALUE_ERROR (length_value))
96+
{
97+
return length_value;
98+
}
99+
100+
/* 3. */
101+
ecma_number_t length_number;
102+
length_value = ecma_get_number (length_value, &length_number);
103+
104+
if (!ecma_is_value_empty (length_value))
105+
{
106+
return length_value;
107+
}
108+
109+
length = ecma_number_to_uint32 (length_number);
110+
ecma_free_value (length_value);
111+
#ifndef CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN
112+
}
113+
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */
114+
115+
uint32_t index = ext_obj_p->u.pseudo_array.u1.iterator_index;
116+
117+
if (JERRY_UNLIKELY (index == ECMA_ITERATOR_INDEX_LIMIT))
118+
{
119+
/* After the ECMA_ITERATOR_INDEX_LIMIT limit is reached the [[ArrayIteratorNextIndex]]
120+
property is stored as an internal property */
121+
ecma_string_t *prop_name_p = ecma_get_magic_string (LIT_INTERNAL_MAGIC_STRING_ITERATOR_NEXT_INDEX);
122+
ecma_value_t index_value = ecma_op_object_get (array_object_p, prop_name_p);
123+
124+
if (!ecma_is_value_undefined (index_value))
125+
{
126+
index = (uint32_t) (ecma_get_number_from_value (index_value) + 1);
127+
}
128+
129+
ecma_value_t put_result = ecma_op_object_put (array_object_p,
130+
prop_name_p,
131+
ecma_make_uint32_value (index),
132+
true);
133+
134+
JERRY_ASSERT (ecma_is_value_true (put_result));
135+
136+
ecma_free_value (index_value);
137+
}
138+
else
139+
{
140+
/* 11. */
141+
ext_obj_p->u.pseudo_array.u1.iterator_index++;
142+
}
143+
144+
if (index >= length)
145+
{
146+
ECMA_SET_POINTER (ext_obj_p->u.pseudo_array.u2.iterated_value_cp, NULL);
147+
return ecma_create_iter_result_object (ECMA_VALUE_UNDEFINED, ECMA_VALUE_TRUE);
148+
}
149+
150+
/* 7. */
151+
uint8_t iterator_type = ext_obj_p->u.pseudo_array.extra_info;
152+
153+
if (iterator_type == ECMA_ARRAY_ITERATOR_KEYS)
154+
{
155+
/* 12. */
156+
return ecma_create_iter_result_object (ecma_make_uint32_value (index), ECMA_VALUE_FALSE);
157+
}
158+
159+
ecma_value_t result;
160+
161+
/* 13. */
162+
ecma_string_t *index_string_p = ecma_new_ecma_string_from_uint32 (index);
163+
164+
/* 14. */
165+
ecma_value_t get_value = ecma_op_object_get (array_object_p, index_string_p);
166+
167+
/* 15. */
168+
if (ECMA_IS_VALUE_ERROR (get_value))
169+
{
170+
ecma_deref_ecma_string (index_string_p);
171+
return get_value;
172+
}
173+
174+
/* 16. */
175+
if (iterator_type == ECMA_ARRAY_ITERATOR_VALUES)
176+
{
177+
result = ecma_create_iter_result_object (get_value, ECMA_VALUE_FALSE);
178+
}
179+
else
180+
{
181+
/* 17.a */
182+
JERRY_ASSERT (iterator_type == ECMA_ARRAY_ITERATOR_KEYS_VALUES);
183+
184+
/* 17.b */
185+
ecma_value_t entry_array_value;
186+
entry_array_value = ecma_create_array_from_iter_element (get_value,
187+
ecma_make_uint32_value (index));
188+
189+
result = ecma_create_iter_result_object (entry_array_value, ECMA_VALUE_FALSE);
190+
ecma_free_value (entry_array_value);
191+
}
192+
193+
ecma_free_value (get_value);
194+
ecma_deref_ecma_string (index_string_p);
195+
return result;
196+
} /* ecma_op_array_iterator_next */
197+
#endif /* !CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN */
198+
199+
/**
200+
* @}
201+
* @}
202+
*/

0 commit comments

Comments
 (0)