Skip to content

Commit 647241a

Browse files
committed
Implement Array iterators
JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
1 parent 4f0b075 commit 647241a

12 files changed

+917
-3
lines changed

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

Lines changed: 17 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_BUILTIN
43+
#include "ecma-array-iterator-object.h"
44+
#endif /* !CONFIG_DISABLE_ES2015_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,14 @@ 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_BUILTIN
772+
case ECMA_PSEUDO_ARRAY_ITERATOR:
773+
{
774+
ecma_free_array_iterator_object (ext_object_p);
775+
ecma_dealloc_extended_object (object_p, sizeof (ecma_extended_object_t));
776+
return;
777+
}
778+
#endif /* !CONFIG_DISABLE_ES2015_BUILTIN */
762779
default:
763780
{
764781
JERRY_ASSERT (ext_object_p->u.pseudo_array.type == ECMA_PSEUDO_ARRAY_ARGUMENTS);

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

Lines changed: 21 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,20 @@ 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 = (1 << 5), /**< List only key indices */
691+
ECMA_ARRAY_ITERATOR_VALUES = (1 << 6), /**< List only key values */
692+
} ecma_array_iterator_type_t;
693+
694+
/**
695+
* List key indices and values simultaneously.
696+
*/
697+
#define ECMA_ARRAY_ITERATOR_KEYS_VALUES (ECMA_ARRAY_ITERATOR_KEYS | ECMA_ARRAY_ITERATOR_VALUES)
698+
684699
/**
685700
* Offset for JERRY_CONTEXT (status_flags) top 8 bits.
686701
*/
@@ -841,18 +856,21 @@ typedef struct
841856
*/
842857
struct
843858
{
844-
uint8_t type; /**< pseudo array type, e.g. Arguments, TypedArray*/
859+
uint8_t type; /**< pseudo array type, e.g. Arguments, TypedArray, ArrayIterator */
845860
uint8_t extra_info; /**< extra information about the object.
846-
* e.g. element_width_shift for typed arrays */
861+
* e.g. element_width_shift for typed arrays,
862+
* elementKind for ArrayIterator */
847863
union
848864
{
849865
uint16_t length; /**< for arguments: length of names */
850866
uint16_t class_id; /**< for typedarray: the specific class name */
867+
uint16_t iterator_index; /**< for ArrayIterator: [[ArrayIteratorNextIndex]] property */
851868
} u1;
852869
union
853870
{
854871
ecma_value_t lex_env_cp; /**< for arguments: lexical environment */
855872
ecma_value_t arraybuffer; /**< for typedarray: internal arraybuffer */
873+
ecma_value_t prop_chunk_cp; /**< for ArrayIterator: current collection chunk */
856874
} u2;
857875
} pseudo_array;
858876

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "ecma-string-object.h"
2929
#include "ecma-try-catch-macro.h"
3030
#include "jrt.h"
31+
#include "ecma-array-iterator-object.h"
3132

3233
#ifndef CONFIG_DISABLE_ARRAY_BUILTIN
3334

@@ -2502,6 +2503,79 @@ ecma_builtin_array_prototype_object_find (ecma_value_t this_arg, /**< this argum
25022503

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

25072581
/**

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

0 commit comments

Comments
 (0)