Skip to content

Commit da66b1c

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 da66b1c

16 files changed

+1054
-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: 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_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,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_ITERATOR_BUILTIN
772+
case ECMA_PSEUDO_ARRAY_ITERATOR:
773+
{
774+
ecma_op_free_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_ITERATOR_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: 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

0 commit comments

Comments
 (0)