Implement Iterator interface and Array iterators (#2640)

JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
This commit is contained in:
Robert Fancsik 2019-02-08 11:10:32 +01:00 committed by GitHub
parent 2dd854d28b
commit 4f5ffb4f3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 1045 additions and 6 deletions

View File

@ -41,6 +41,7 @@
# define CONFIG_DISABLE_ES2015_CLASS
# define CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER
# define CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER
# define CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN
# define CONFIG_DISABLE_ES2015_MAP_BUILTIN
# define CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER
# define CONFIG_DISABLE_ES2015_PROMISE_BUILTIN

View File

@ -369,6 +369,12 @@ ecma_gc_mark (ecma_object_t *object_p) /**< object to mark from */
break;
}
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */
#ifndef CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN
case ECMA_PSEUDO_ARRAY_ITERATOR:
{
break;
}
#endif /* !CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN */
default:
{
JERRY_ASSERT (ext_object_p->u.pseudo_array.type == ECMA_PSEUDO_ARRAY_ARGUMENTS);
@ -762,6 +768,13 @@ ecma_gc_free_object (ecma_object_t *object_p) /**< object to free */
return;
}
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */
#ifndef CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN
case ECMA_PSEUDO_ARRAY_ITERATOR:
{
ecma_dealloc_extended_object (object_p, sizeof (ecma_extended_object_t));
return;
}
#endif /* !CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN */
default:
{
JERRY_ASSERT (ext_object_p->u.pseudo_array.type == ECMA_PSEUDO_ARRAY_ARGUMENTS);

View File

@ -664,8 +664,9 @@ typedef enum
ECMA_PSEUDO_ARRAY_ARGUMENTS = 0, /**< Arguments object (10.6) */
ECMA_PSEUDO_ARRAY_TYPEDARRAY = 1, /**< TypedArray which does NOT need extra space to store length and offset */
ECMA_PSEUDO_ARRAY_TYPEDARRAY_WITH_INFO = 2, /**< TypedArray which NEEDS extra space to store length and offset */
ECMA_PSEUDO_ARRAY_ITERATOR = 3, /**< Array iterator object (ECMAScript v6, 22.1.5.1) */
ECMA_PSEUDO_ARRAY__MAX = ECMA_PSEUDO_ARRAY_TYPEDARRAY_WITH_INFO /**< maximum value */
ECMA_PSEUDO_ARRAY__MAX = ECMA_PSEUDO_ARRAY_ITERATOR /**< maximum value */
} ecma_pseudo_array_type_t;
/**
@ -686,6 +687,18 @@ typedef enum
ECMA_LEXICAL_ENVIRONMENT_TYPE__MAX = ECMA_LEXICAL_ENVIRONMENT_SUPER_OBJECT_BOUND /**< maximum value */
} ecma_lexical_environment_type_t;
#ifndef CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN
/**
* Types of array iterators.
*/
typedef enum
{
ECMA_ARRAY_ITERATOR_KEYS, /**< List only key indices */
ECMA_ARRAY_ITERATOR_VALUES, /**< List only key values */
ECMA_ARRAY_ITERATOR_KEYS_VALUES, /**< List key indices and values */
} ecma_array_iterator_type_t;
#endif /* !CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN */
/**
* Offset for JERRY_CONTEXT (status_flags) top 8 bits.
*/
@ -846,18 +859,21 @@ typedef struct
*/
struct
{
uint8_t type; /**< pseudo array type, e.g. Arguments, TypedArray*/
uint8_t type; /**< pseudo array type, e.g. Arguments, TypedArray, ArrayIterator */
uint8_t extra_info; /**< extra information about the object.
* e.g. element_width_shift for typed arrays */
* e.g. element_width_shift for typed arrays,
* [[IterationKind]] property for %Iterator% */
union
{
uint16_t length; /**< for arguments: length of names */
uint16_t class_id; /**< for typedarray: the specific class name */
uint16_t iterator_index; /**< for %Iterator%: [[%Iterator%NextIndex]] property */
} u1;
union
{
ecma_value_t lex_env_cp; /**< for arguments: lexical environment */
ecma_value_t arraybuffer; /**< for typedarray: internal arraybuffer */
ecma_value_t iterated_value_cp; /**< for %Iterator%: [[IteratedObject]] property */
} u2;
} pseudo_array;

View File

@ -0,0 +1,193 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecma-builtin-helpers.h"
#include "ecma-builtins.h"
#include "ecma-iterator-object.h"
#include "ecma-typedarray-object.h"
#ifndef CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN
#ifdef CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN
#error "Iterator builtin requires ES2015 symbol builtin"
#endif /* CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
#define ECMA_BUILTINS_INTERNAL
#include "ecma-builtins-internal.h"
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-array-iterator-prototype.inc.h"
#define BUILTIN_UNDERSCORED_ID array_iterator_prototype
#include "ecma-builtin-internal-routines-template.inc.h"
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmabuiltins
* @{
*
* \addtogroup %arrayiteratorprototype% ECMA %ArrayIteratorPrototype% object built-in
* @{
*/
/**
* The %ArrayIteratorPrototype% object's 'next' routine
*
* See also:
* ECMA-262 v6, 22.1.5.2.1
*
* Note:
* Returned value must be freed with ecma_free_value.
*
* @return iterator result object, if success
* error - otherwise
*/
static ecma_value_t
ecma_builtin_array_iterator_prototype_object_next (ecma_value_t this_val) /**< this argument */
{
/* 1 - 2. */
if (!ecma_is_value_object (this_val))
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not an object."));
}
ecma_object_t *obj_p = ecma_get_object_from_value (this_val);
ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) obj_p;
/* 3. */
if (ext_obj_p->u.pseudo_array.type != ECMA_PSEUDO_ARRAY_ITERATOR)
{
return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is not an iterator."));
}
ecma_object_t *array_object_p = ECMA_GET_POINTER (ecma_object_t,
ext_obj_p->u.pseudo_array.u2.iterated_value_cp);
/* 4 - 5 */
if (array_object_p == NULL)
{
return ecma_create_iter_result_object (ECMA_VALUE_UNDEFINED, ECMA_VALUE_TRUE);
}
uint32_t length;
/* 8 - 9. */
#ifndef CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN
if (ecma_is_typedarray (ecma_make_object_value (array_object_p)))
{
length = ecma_typedarray_get_length (array_object_p);
}
else
{
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */
JERRY_ASSERT (ecma_get_object_type (array_object_p) == ECMA_OBJECT_TYPE_ARRAY);
ecma_extended_object_t *ext_array_obj_p = (ecma_extended_object_t *) array_object_p;
length = ext_array_obj_p->u.array.length;
#ifndef CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN
}
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */
uint32_t index = ext_obj_p->u.pseudo_array.u1.iterator_index;
if (JERRY_UNLIKELY (index == ECMA_ITERATOR_INDEX_LIMIT))
{
/* After the ECMA_ITERATOR_INDEX_LIMIT limit is reached the [[%Iterator%NextIndex]]
property is stored as an internal property */
ecma_string_t *prop_name_p = ecma_get_magic_string (LIT_INTERNAL_MAGIC_STRING_ITERATOR_NEXT_INDEX);
ecma_value_t index_value = ecma_op_object_get (obj_p, prop_name_p);
if (!ecma_is_value_undefined (index_value))
{
index = (uint32_t) (ecma_get_number_from_value (index_value) + 1);
}
ecma_value_t put_result = ecma_op_object_put (obj_p,
prop_name_p,
ecma_make_uint32_value (index),
true);
JERRY_ASSERT (ecma_is_value_true (put_result));
ecma_free_value (index_value);
}
else
{
/* 11. */
ext_obj_p->u.pseudo_array.u1.iterator_index++;
}
if (index >= length)
{
ECMA_SET_POINTER (ext_obj_p->u.pseudo_array.u2.iterated_value_cp, NULL);
return ecma_create_iter_result_object (ECMA_VALUE_UNDEFINED, ECMA_VALUE_TRUE);
}
/* 7. */
uint8_t iterator_type = ext_obj_p->u.pseudo_array.extra_info;
if (iterator_type == ECMA_ARRAY_ITERATOR_KEYS)
{
/* 12. */
return ecma_create_iter_result_object (ecma_make_uint32_value (index), ECMA_VALUE_FALSE);
}
/* 13. */
ecma_string_t *index_string_p = ecma_new_ecma_string_from_uint32 (index);
/* 14. */
ecma_value_t get_value = ecma_op_object_get (array_object_p, index_string_p);
ecma_deref_ecma_string (index_string_p);
/* 15. */
if (ECMA_IS_VALUE_ERROR (get_value))
{
return get_value;
}
ecma_value_t result;
/* 16. */
if (iterator_type == ECMA_ARRAY_ITERATOR_VALUES)
{
result = ecma_create_iter_result_object (get_value, ECMA_VALUE_FALSE);
}
else
{
/* 17.a */
JERRY_ASSERT (iterator_type == ECMA_ARRAY_ITERATOR_KEYS_VALUES);
/* 17.b */
ecma_value_t entry_array_value;
entry_array_value = ecma_create_array_from_iter_element (get_value,
ecma_make_uint32_value (index));
result = ecma_create_iter_result_object (entry_array_value, ECMA_VALUE_FALSE);
ecma_free_value (entry_array_value);
}
ecma_free_value (get_value);
return result;
} /* ecma_builtin_array_iterator_prototype_object_next */
/**
* @}
* @}
* @}
*/
#endif /* !CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN */

View File

@ -0,0 +1,34 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* %ArrayIteratorPrototype% built-in description
*/
#include "ecma-builtin-helpers-macro-defines.inc.h"
#ifndef CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN
STRING_VALUE (LIT_GLOBAL_SYMBOL_TO_STRING_TAG,
LIT_MAGIC_STRING_ARRAY_ITERATOR_UL,
ECMA_PROPERTY_FLAG_CONFIGURABLE)
/* Routine properties:
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
ROUTINE (LIT_MAGIC_STRING_NEXT, ecma_builtin_array_iterator_prototype_object_next, 0, 0)
#endif /* !CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN */
#include "ecma-builtin-helpers-macro-undefs.inc.h"

View File

@ -24,6 +24,7 @@
#include "ecma-gc.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-iterator-object.h"
#include "ecma-objects.h"
#include "ecma-string-object.h"
#include "ecma-try-catch-macro.h"
@ -2504,6 +2505,116 @@ ecma_builtin_array_prototype_object_find (ecma_value_t this_arg, /**< this argum
} /* ecma_builtin_array_prototype_object_find */
#endif /* !CONFIG_DISABLE_ES2015_BUILTIN */
#ifndef CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN
/**
* Helper function for Array.prototype object's {'keys', 'values', 'entries'}
* routines common parts.
*
* Note:
* Returned value must be freed with ecma_free_value.
*
* @return iterator result object, if success
* error - otherwise
*/
static ecma_value_t
ecma_builtin_array_iterators_helper (ecma_value_t this_arg, /**< this argument */
uint8_t type) /**< any combination of
* ecma_array_iterator_type_t bits */
{
/* 1. */
ecma_value_t obj_this = ecma_op_to_object (this_arg);
if (ECMA_IS_VALUE_ERROR (obj_this))
{
/* 2. */
return obj_this;
}
ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_ARRAY_ITERATOR_PROTOTYPE);
/* 3. */
ecma_value_t ret_value = ecma_op_create_iterator_object (obj_this,
prototype_obj_p,
ECMA_PSEUDO_ARRAY_ITERATOR,
type);
ecma_free_value (obj_this);
return ret_value;
} /* ecma_builtin_array_iterators_helper */
/**
* The Array.prototype object's 'entries' routine
*
* See also:
* ECMA-262 v6, 22.1.3.4
*
* Note:
* Returned value must be freed with ecma_free_value.
*
* @return iterator result object, if success
* error - otherwise
*/
static ecma_value_t
ecma_builtin_array_prototype_entries (ecma_value_t this_arg) /**< this argument */
{
return ecma_builtin_array_iterators_helper (this_arg, ECMA_ARRAY_ITERATOR_KEYS_VALUES);
} /* ecma_builtin_array_prototype_entries */
/**
* The Array.prototype object's 'values' routine
*
* See also:
* ECMA-262 v6, 22.1.3.29.
*
* Note:
* Returned value must be freed with ecma_free_value.
*
* @return iterator result object, if success
* error - otherwise
*/
static ecma_value_t
ecma_builtin_array_prototype_values (ecma_value_t this_arg) /**< this argument */
{
return ecma_builtin_array_iterators_helper (this_arg, ECMA_ARRAY_ITERATOR_VALUES);
} /* ecma_builtin_array_prototype_values */
/**
* The Array.prototype object's '@@iterator' routine
*
* See also:
* ECMA-262 v6, 22.1.3.30.
*
* Note:
* Returned value must be freed with ecma_free_value.
*
* @return iterator result object, if success
* error - otherwise
*/
static ecma_value_t
ecma_builtin_array_prototype_symbol_iterator (ecma_value_t this_arg) /**< this argument */
{
return ecma_builtin_array_prototype_values (this_arg);
} /* ecma_builtin_array_prototype_symbol_iterator */
/**
* The Array.prototype object's 'keys' routine
*
* See also:
* ECMA-262 v6, 22.1.3.13
*
* Note:
* Returned value must be freed with ecma_free_value.
*
* @return iterator result object, if success
* error - otherwise
*/
static ecma_value_t
ecma_builtin_array_prototype_keys (ecma_value_t this_arg) /**< this argument */
{
return ecma_builtin_array_iterators_helper (this_arg, ECMA_ARRAY_ITERATOR_KEYS);
} /* ecma_builtin_array_prototype_keys */
#endif /* !CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN */
/**
* @}
* @}

View File

@ -63,6 +63,12 @@ ROUTINE (LIT_MAGIC_STRING_REDUCE_RIGHT_UL, ecma_builtin_array_prototype_object_r
#ifndef CONFIG_DISABLE_ES2015_BUILTIN
ROUTINE (LIT_MAGIC_STRING_FIND, ecma_builtin_array_prototype_object_find, 2, 1)
#endif /* !CONFIG_DISABLE_ES2015_BUILTIN */
#ifndef CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN
ROUTINE (LIT_MAGIC_STRING_ENTRIES, ecma_builtin_array_prototype_entries, 0, 0)
ROUTINE (LIT_MAGIC_STRING_VALUES, ecma_builtin_array_prototype_values, 0, 0)
ROUTINE (LIT_MAGIC_STRING_KEYS, ecma_builtin_array_prototype_keys, 0, 0)
ROUTINE (LIT_GLOBAL_SYMBOL_ITERATOR, ecma_builtin_array_prototype_symbol_iterator, 0, 0)
#endif /* !CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN */
#endif /* !CONFIG_DISABLE_ARRAY_BUILTIN */

View File

@ -0,0 +1,63 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecma-builtin-helpers.h"
#include "ecma-builtins.h"
#include "ecma-iterator-object.h"
#ifndef CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN
#define ECMA_BUILTINS_INTERNAL
#include "ecma-builtins-internal.h"
#define BUILTIN_INC_HEADER_NAME "ecma-builtin-iterator-prototype.inc.h"
#define BUILTIN_UNDERSCORED_ID iterator_prototype
#include "ecma-builtin-internal-routines-template.inc.h"
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmabuiltins
* @{
*
* \addtogroup %iteratorprototype% ECMA %IteratorPrototype% object built-in
* @{
*/
/**
* The %IteratorPrototype% object's '@@iterator' routine
*
* See also:
* ECMA-262 v6, 22.1.2.1
*
* Note:
* Returned value must be freed with ecma_free_value.
*
* @return the given this value
*/
static ecma_value_t
ecma_builtin_iterator_prototype_object_iterator (ecma_value_t this_val) /**< this argument */
{
/* 1. */
return ecma_copy_value (this_val);
} /* ecma_builtin_iterator_prototype_object_iterator */
/**
* @}
* @}
* @}
*/
#endif /* !CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN */

View File

@ -0,0 +1,30 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/*
* %IteratorPrototype% built-in description
*/
#include "ecma-builtin-helpers-macro-defines.inc.h"
#ifndef CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN
/* Routine properties:
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
ROUTINE (LIT_GLOBAL_SYMBOL_ITERATOR, ecma_builtin_iterator_prototype_object_iterator, 0, 0)
#endif /* !CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN */
#include "ecma-builtin-helpers-macro-undefs.inc.h"

View File

@ -458,6 +458,21 @@ BUILTIN_ROUTINE (ECMA_BUILTIN_ID_SYMBOL,
symbol)
#endif /* !CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN */
#ifndef CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN
/* The %IteratorPrototype% object (ECMA-262 v6, 25.1.2) */
BUILTIN (ECMA_BUILTIN_ID_ITERATOR_PROTOTYPE,
ECMA_OBJECT_TYPE_GENERAL,
ECMA_BUILTIN_ID_OBJECT_PROTOTYPE,
true,
iterator_prototype)
/* The %ArrayIteratorPrototype% object (ECMA-262 v6, 22.1.5.2) */
BUILTIN (ECMA_BUILTIN_ID_ARRAY_ITERATOR_PROTOTYPE,
ECMA_OBJECT_TYPE_GENERAL,
ECMA_BUILTIN_ID_ITERATOR_PROTOTYPE,
true,
array_iterator_prototype)
#endif /* !CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN */
/* The Global object (15.1) */
BUILTIN (ECMA_BUILTIN_ID_GLOBAL,

View File

@ -0,0 +1,168 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ecma-alloc.h"
#include "ecma-array-object.h"
#include "ecma-iterator-object.h"
#include "ecma-builtin-helpers.h"
#include "ecma-builtins.h"
#include "ecma-exceptions.h"
#include "ecma-gc.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-number-arithmetic.h"
#include "ecma-objects.h"
#include "ecma-objects-general.h"
#include "ecma-function-object.h"
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmaiteratorobject ECMA iterator object related routines
* @{
*/
#ifndef CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN
/**
* Implementation of 'CreateArrayFromList' specialized for iterators
*
* See also:
* ECMA-262 v6, 7.3.16.
*
* Note:
* Returned value must be freed with ecma_free_value.
*
* @return new array object
*/
ecma_value_t
ecma_create_array_from_iter_element (ecma_value_t value, /**< value */
ecma_value_t index_value) /**< iterator index */
{
/* 2. */
ecma_value_t new_array = ecma_op_create_array_object (NULL, 0, false);
JERRY_ASSERT (!ECMA_IS_VALUE_ERROR (new_array));
ecma_object_t *new_array_p = ecma_get_object_from_value (new_array);
/* 3 - 4. */
for (uint32_t index = 0; index < 2; index++)
{
ecma_string_t *index_string_p = ecma_new_ecma_string_from_uint32 (index);
/* 4.a */
ecma_value_t completion = ecma_builtin_helper_def_prop (new_array_p,
index_string_p,
(index == 0) ? index_value : value,
ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE,
false); /* Failure handling */
/* 4.b */
JERRY_ASSERT (ecma_is_value_true (completion));
ecma_deref_ecma_string (index_string_p);
}
/* 5. */
return new_array;
} /* ecma_create_array_from_iter_element */
/**
* CreateIterResultObject operation
*
* See also:
* ECMA-262 v6, 7.4.7.
*
* Note:
* Returned value must be freed with ecma_free_value.
*
* @return iterator result object
*/
ecma_value_t
ecma_create_iter_result_object (ecma_value_t value, /**< value */
ecma_value_t done) /**< ECMA_VALUE_{TRUE,FALSE} based
* on the iterator index */
{
/* 1. */
JERRY_ASSERT (ecma_is_value_boolean (done));
/* 2. */
ecma_object_t *object_p = ecma_create_object (ecma_builtin_get (ECMA_BUILTIN_ID_OBJECT_PROTOTYPE),
0,
ECMA_OBJECT_TYPE_GENERAL);
/* 3. */
ecma_property_value_t *prop_value_p;
prop_value_p = ecma_create_named_data_property (object_p,
ecma_get_magic_string (LIT_MAGIC_STRING_VALUE),
ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE,
NULL);
prop_value_p->value = ecma_copy_value_if_not_object (value);
/* 4. */
prop_value_p = ecma_create_named_data_property (object_p,
ecma_get_magic_string (LIT_MAGIC_STRING_DONE),
ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE,
NULL);
prop_value_p->value = done;
/* 5. */
return ecma_make_object_value (object_p);
} /* ecma_create_iter_result_object */
/**
* General iterator object creation operation.
*
* See also: ECMA-262 v6, 21.1.5.1, 22.1.5.1, 23.1.5.1
*
* Note:
* Returned value must be freed with ecma_free_value.
*
* @return iterator object
*/
ecma_value_t
ecma_op_create_iterator_object (ecma_value_t iterated_value, /**< value from create iterator */
ecma_object_t *prototype_obj_p, /**< prototype object */
uint8_t iterator_type, /**< itertator type, see ecma_pseudo_array_type_t */
uint8_t extra_info) /**< extra information */
{
/* 1. */
JERRY_ASSERT (ecma_is_value_object (iterated_value));
JERRY_ASSERT (iterator_type >= ECMA_PSEUDO_ARRAY_ITERATOR && iterator_type <= ECMA_PSEUDO_ARRAY__MAX);
ecma_object_t *iterated_obj_p = ecma_get_object_from_value (iterated_value);
/* 2. */
ecma_object_t *object_p = ecma_create_object (prototype_obj_p,
sizeof (ecma_extended_object_t),
ECMA_OBJECT_TYPE_PSEUDO_ARRAY);
ecma_extended_object_t *ext_obj_p = (ecma_extended_object_t *) object_p;
ext_obj_p->u.pseudo_array.type = iterator_type;
/* 3. */
ECMA_SET_NON_NULL_POINTER (ext_obj_p->u.pseudo_array.u2.iterated_value_cp, iterated_obj_p);
/* 4. */
ext_obj_p->u.pseudo_array.u1.iterator_index = 0;
/* 5. */
ext_obj_p->u.pseudo_array.extra_info = extra_info;
/* 6. */
return ecma_make_object_value (object_p);
} /* ecma_op_create_iterator_object */
#endif /* !CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN */
/**
* @}
* @}
*/

View File

@ -0,0 +1,52 @@
/* Copyright JS Foundation and other contributors, http://js.foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ECMA_ITERATOR_OBJECT_H
#define ECMA_ITERATOR_OBJECT_H
#ifndef CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN
#include "ecma-globals.h"
/** \addtogroup ecma ECMA
* @{
*
* \addtogroup ecmaiteratorobject ECMA iterator object related routines
* @{
*/
/**
* Maximum value of [[%Iterator%NextIndex]] until it can be stored
* in an ecma pseudo array object structure element.
*/
#define ECMA_ITERATOR_INDEX_LIMIT UINT16_MAX
ecma_value_t
ecma_op_create_iterator_object (ecma_value_t iterated_value, ecma_object_t *prototype_obj_p,
uint8_t iterator_type, uint8_t extra_info);
ecma_value_t
ecma_create_iter_result_object (ecma_value_t value, ecma_value_t done);
ecma_value_t
ecma_create_array_from_iter_element (ecma_value_t value, ecma_value_t index_value);
#endif /* !CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN */
/**
* @}
* @}
*/
#endif /* !ECMA_ITERATOR_OBJECT_H */

View File

@ -1829,6 +1829,10 @@ ecma_object_check_class_name_is_object (ecma_object_t *obj_p) /**< object */
#if CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64
|| ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_FLOAT64ARRAY_PROTOTYPE)
#endif /* CONFIG_ECMA_NUMBER_TYPE == CONFIG_ECMA_NUMBER_FLOAT64 */
#ifndef CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN
|| ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_ARRAY_ITERATOR_PROTOTYPE)
|| ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_ITERATOR_PROTOTYPE)
#endif /* !CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN */
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */
|| ecma_builtin_is (obj_p, ECMA_BUILTIN_ID_OBJECT_PROTOTYPE));
#else /* JERRY_NDEBUG */
@ -1871,6 +1875,12 @@ ecma_object_get_class_name (ecma_object_t *obj_p) /**< object */
return (lit_magic_string_id_t) ext_obj_p->u.pseudo_array.u1.class_id;
}
#endif /* !CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN */
#ifndef CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN
case ECMA_PSEUDO_ARRAY_ITERATOR:
{
return LIT_MAGIC_STRING_ARRAY_ITERATOR_UL;
}
#endif /* !CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN */
default:
{
JERRY_ASSERT (ext_obj_p->u.pseudo_array.type == ECMA_PSEUDO_ARRAY_ARGUMENTS);

View File

@ -46,6 +46,7 @@ typedef enum
LIT_INTERNAL_MAGIC_STRING_PROMISE_PROPERTY_INDEX, /**< [[Index]] property */
LIT_INTERNAL_MAGIC_STRING_PROMISE_PROPERTY_VALUE, /**< [[Values]] property */
LIT_INTERNAL_MAGIC_STRING_PROMISE_PROPERTY_REMAINING_ELEMENT, /**< [[RemainingElement]] property */
LIT_INTERNAL_MAGIC_STRING_ITERATOR_NEXT_INDEX, /**< [[%Iterator%NextIndex]] property */
/* List of well known symbols */
LIT_GLOBAL_SYMBOL_HAS_INSTANCE, /**< @@hasInstance well known symbol */
LIT_GLOBAL_SYMBOL_IS_CONCAT_SPREADABLE, /**< @@isConcatSpreadable well known symbol */

View File

@ -112,6 +112,9 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_CALL, "call")
#if !defined (CONFIG_DISABLE_MATH_BUILTIN)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_CEIL, "ceil")
#endif
#if !defined (CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_DONE, "done")
#endif
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_EVAL, "eval")
#if !defined (CONFIG_DISABLE_REGEXP_BUILTIN)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_EXEC, "exec")
@ -132,6 +135,9 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_JOIN, "join")
#endif
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_KEYS, "keys")
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_NAME, "name")
#if !defined (CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_NEXT, "next")
#endif
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_NULL, "null")
#if !defined (CONFIG_DISABLE_ARRAY_BUILTIN)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_PUSH, "push")
@ -300,6 +306,9 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SYMBOL, "symbol")
|| !defined (CONFIG_DISABLE_JSON_BUILTIN)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TO_JSON_UL, "toJSON")
#endif
#if !defined (CONFIG_DISABLE_ARRAY_BUILTIN) && !defined (CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_VALUES, "values")
#endif
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_BOOLEAN_UL, "Boolean")
#if !defined (CONFIG_DISABLE_ES2015_PROMISE_BUILTIN)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_PROMISE_UL, "Promise")
@ -315,6 +324,9 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_BOOLEAN, "boolean")
#if !defined (CONFIG_DISABLE_ANNEXB_BUILTIN) && !defined (CONFIG_DISABLE_REGEXP_BUILTIN)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_COMPILE, "compile")
#endif
#if !defined (CONFIG_DISABLE_ARRAY_BUILTIN) && !defined (CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_ENTRIES, "entries")
#endif
#if !defined (CONFIG_DISABLE_ARRAY_BUILTIN) \
|| !defined (CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_FOR_EACH_UL, "forEach")
@ -544,6 +556,9 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SET_UTC_SECONDS_UL, "setUTCSeconds")
#if !defined (CONFIG_DISABLE_NUMBER_BUILTIN)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TO_EXPONENTIAL_UL, "toExponential")
#endif
#if !defined (CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_ARRAY_ITERATOR_UL, "Array Iterator")
#endif
#if !defined (CONFIG_DISABLE_ERROR_BUILTINS)
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_REFERENCE_ERROR_UL, "ReferenceError")
#endif
@ -672,7 +687,9 @@ LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (13, LIT_MAGIC_STRING_GET_UTC_MINUTES_UL
#else
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (13, LIT_MAGIC_STRING_IS_PROTOTYPE_OF_UL)
#endif
#if !defined (CONFIG_DISABLE_ERROR_BUILTINS)
#if !defined (CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN)
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (14, LIT_MAGIC_STRING_ARRAY_ITERATOR_UL)
#elif !defined (CONFIG_DISABLE_ERROR_BUILTINS)
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (14, LIT_MAGIC_STRING_REFERENCE_ERROR_UL)
#else
LIT_MAGIC_STRING_FIRST_STRING_WITH_SIZE (14, LIT_MAGIC_STRING_DEFINE_PROPERTY_UL)

View File

@ -66,6 +66,7 @@ LIT_MAGIC_STRING_ATAN = "atan"
LIT_MAGIC_STRING_BIND = "bind"
LIT_MAGIC_STRING_CALL = "call"
LIT_MAGIC_STRING_CEIL = "ceil"
LIT_MAGIC_STRING_DONE = "done"
LIT_MAGIC_STRING_EVAL = "eval"
LIT_MAGIC_STRING_EXEC = "exec"
LIT_MAGIC_STRING_FILL = "fill"
@ -75,6 +76,7 @@ LIT_MAGIC_STRING_JOIN = "join"
LIT_MAGIC_STRING_KEYS = "keys"
LIT_MAGIC_STRING_NAME = "name"
LIT_MAGIC_STRING_NULL = "null"
LIT_MAGIC_STRING_NEXT = "next"
LIT_MAGIC_STRING_PUSH = "push"
LIT_MAGIC_STRING_RACE = "race"
LIT_MAGIC_STRING_SEAL = "seal"
@ -140,7 +142,9 @@ LIT_MAGIC_STRING_SPLICE = "splice"
LIT_MAGIC_STRING_STRING = "string"
LIT_MAGIC_STRING_SYMBOL = "symbol"
LIT_MAGIC_STRING_SUBSTR = "substr"
LIT_MAGIC_STRING_ENTRIES = "entries"
LIT_MAGIC_STRING_TO_JSON_UL = "toJSON"
LIT_MAGIC_STRING_VALUES = "values"
LIT_MAGIC_STRING_BOOLEAN_UL = "Boolean"
LIT_MAGIC_STRING_PROMISE_UL = "Promise"
LIT_MAGIC_STRING_SQRT1_2_U = "SQRT1_2"
@ -255,6 +259,7 @@ LIT_MAGIC_STRING_LOCALE_COMPARE_UL = "localeCompare"
LIT_MAGIC_STRING_SET_UTC_MINUTES_UL = "setUTCMinutes"
LIT_MAGIC_STRING_SET_UTC_SECONDS_UL = "setUTCSeconds"
LIT_MAGIC_STRING_TO_EXPONENTIAL_UL = "toExponential"
LIT_MAGIC_STRING_ARRAY_ITERATOR_UL = "Array Iterator"
LIT_MAGIC_STRING_REFERENCE_ERROR_UL = "ReferenceError"
LIT_MAGIC_STRING_DEFINE_PROPERTY_UL = "defineProperty"
LIT_MAGIC_STRING_GET_PROTOTYPE_OF_UL = "getPrototypeOf"

View File

@ -34,6 +34,7 @@ CONFIG_DISABLE_ES2015_BUILTIN
CONFIG_DISABLE_ES2015_CLASS
CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER
CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER
CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN
CONFIG_DISABLE_ES2015_MAP_BUILTIN
CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER
CONFIG_DISABLE_ES2015_PROMISE_BUILTIN
@ -96,6 +97,8 @@ In JerryScript all of the features are enabled by default, so an empty profile f
Disable the [default value](http://www.ecma-international.org/ecma-262/6.0/#sec-function-definitions) for formal parameters.
* `CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER`:
Disable the [rest parameter](http://www.ecma-international.org/ecma-262/6.0/#sec-function-definitions).
* `CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN`:
Disable the [Iterator](https://www.ecma-international.org/ecma-262/6.0/#sec-iterator-interface) built-in.
* `CONFIG_DISABLE_ES2015_MAP_BUILTIN`:
Disable the [Map](http://www.ecma-international.org/ecma-262/6.0/#sec-keyed-collection) built-ins.
* `CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER`:
@ -109,7 +112,7 @@ In JerryScript all of the features are enabled by default, so an empty profile f
* `CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN`:
Disable the [ArrayBuffer](http://www.ecma-international.org/ecma-262/6.0/#sec-arraybuffer-objects) and [TypedArray](http://www.ecma-international.org/ecma-262/6.0/#sec-typedarray-objects) built-ins.
* `CONFIG_DISABLE_ES2015`: Disable all of the implemented [ECMAScript2015 features](http://www.ecma-international.org/ecma-262/6.0/).
(equivalent to `CONFIG_DISABLE_ES2015_ARROW_FUNCTION`, `CONFIG_DISABLE_ES2015_BUILTIN`, `CONFIG_DISABLE_ES2015_CLASS`,
(equivalent to : `CONFIG_DISABLE_ES2015_ARROW_FUNCTION`, `CONFIG_DISABLE_ES2015_BUILTIN`, `CONFIG_DISABLE_ES2015_CLASS`,
`CONFIG_DISABLE_ES2015_FUNCTION_PARAMETER_INITIALIZER`, `CONFIG_DISABLE_ES2015_FUNCTION_REST_PARAMETER`,
`CONFIG_DISABLE_ES2015_MAP_BUILTIN`, `CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER`,
`CONFIG_DISABLE_ES2015_ITERATOR_BUILTIN`,`CONFIG_DISABLE_ES2015_MAP_BUILTIN`, `CONFIG_DISABLE_ES2015_OBJECT_INITIALIZER`,
`CONFIG_DISABLE_ES2015_SYMBOL_BUILTIN`, `CONFIG_DISABLE_ES2015_PROMISE_BUILTIN`, `CONFIG_DISABLE_ES2015_TEMPLATE_STRINGS`, and `CONFIG_DISABLE_ES2015_TYPEDARRAY_BUILTIN`).

View File

@ -0,0 +1,94 @@
// Copyright JS Foundation and other contributors, http://js.foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
try {
Array.prototype.entries.call (undefined);
assert (false);
} catch (e) {
assert (e instanceof TypeError)
}
var array = ['a', "foo", 1, 1.5, true, {} ,[], function f () { }];
var iterator = array.entries ();
var current_item = iterator.next ();
for (var i = 0; i < array.length; i++) {
assert (current_item.value[0] === i);
assert (current_item.value[1] === array[i]);
assert (current_item.done === false);
current_item = iterator.next ();
}
assert (current_item.value === undefined);
assert (current_item.done === true);
function foo_error () {
throw new ReferenceError ("foo");
}
array = [1, 2, 3, 4, 5, 6, 7];
['0', '3', '5'].forEach (function (e) {
Object.defineProperty (array, e, { 'get' : foo_error });
})
iterator = array.entries ();
var expected_values = [2, 3, 5, 7];
var expected_indices = [1, 2, 4, 6];
var expected_values_idx = 0;
for (var i = 0; i < array.length; i++) {
try {
current_item = iterator.next ();
assert (current_item.value[0] === expected_indices[expected_values_idx]);
assert (current_item.value[1] === expected_values[expected_values_idx]);
assert (current_item.done === false);
expected_values_idx++;
} catch (e) {
assert (e instanceof ReferenceError);
assert (e.message === "foo");
}
}
current_item = iterator.next ();
assert (current_item.value === undefined);
assert (current_item.done === true);
/* Test empty array */
array = [];
iterator = array.entries ();
current_item = iterator.next ();
assert (current_item.value === undefined);
assert (current_item.done === true);
/* Test delete elements after the iterator has been constructed */
array = [0, 1, 2, 3, 4, 5];
iterator = array.entries ();
for (var i = 0; i < array.length; i++) {
current_item = iterator.next ();
assert (current_item.value[0] === i);
assert (current_item.value[1] === array[i]);
assert (current_item.done === false);
array.pop();
}
assert ([].entries ().toString () === "[object Array Iterator]");

View File

@ -0,0 +1,86 @@
// Copyright JS Foundation and other contributors, http://js.foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
try {
Array.prototype.keys.call (undefined);
assert (false);
} catch (e) {
assert (e instanceof TypeError)
}
var array = ['a', "foo", 1, 1.5, true, {} ,[], function f () { }];
var iterator = array.keys ();
var current_item = iterator.next ();
for (var i = 0; i < array.length; i++) {
assert (current_item.value === i);
assert (current_item.done === false);
current_item = iterator.next ();
}
assert (current_item.value === undefined);
assert (current_item.done === true);
function foo_error () {
throw new ReferenceError ("foo");
}
array = [1, 2, 3, 4, 5, 6, 7];
['0', '3', '5'].forEach (function (e) {
Object.defineProperty (array, e, { 'get' : foo_error });
})
iterator = array.keys ();
for (var i = 0; i < array.length; i++) {
try {
current_item = iterator.next ();
assert (current_item.value === i);
assert (current_item.done === false);
} catch (e) {
assert (e instanceof ReferenceError);
assert (e.message === "foo");
}
}
current_item = iterator.next ();
assert (current_item.value === undefined);
assert (current_item.done === true);
/* Test empty array */
array = [];
iterator = array.keys ();
current_item = iterator.next ();
assert (current_item.value === undefined);
assert (current_item.done === true);
/* Test delete elements after the iterator has been constructed */
array = [0, 1, 2, 3, 4, 5];
iterator = array.keys ();
for (var i = 0; i < array.length; i++) {
current_item = iterator.next ();
assert (current_item.value === i);
assert (current_item.done === false);
array.pop();
}
assert ([].keys ().toString () === "[object Array Iterator]");

View File

@ -0,0 +1,100 @@
// Copyright JS Foundation and other contributors, http://js.foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
try {
Array.prototype.values.call (undefined);
assert (false);
} catch (e) {
assert (e instanceof TypeError)
}
var array = ['a', "foo", 1, 1.5, true, {} ,[], function f () { }];
var iterator = array.values ();
/* The initial value of the @@iterator property is the same function object
as the initial value of the Array.prototype.values property. */
var symbol_iterator = array[Symbol.iterator] ();
var current_item = iterator.next ();
var symbol_current_item = symbol_iterator.next ();
for (var i = 0; i < array.length; i++) {
assert (current_item.value === array[i]);
assert (current_item.done === false);
assert (current_item.value === symbol_current_item.value);
assert (current_item.done === symbol_current_item.done);
current_item = iterator.next ();
symbol_current_item = symbol_iterator.next ();
}
assert (current_item.value === undefined);
assert (current_item.done === true);
assert (current_item.value === symbol_current_item.value);
assert (current_item.done === symbol_current_item.done);
function foo_error () {
throw new ReferenceError ("foo");
}
array = [1, 2, 3, 4, 5, 6, 7];
['0', '3', '5'].forEach (function (e) {
Object.defineProperty (array, e, { 'get' : foo_error });
})
iterator = array.values ();
var expected_values = [2, 3, 5, 7];
var expected_values_idx = 0;
for (var i = 0; i < array.length; i++) {
try {
current_item = iterator.next ();
assert (current_item.value === expected_values[expected_values_idx++]);
assert (current_item.done === false);
} catch (e) {
assert (e instanceof ReferenceError);
assert (e.message === "foo");
}
}
current_item = iterator.next ();
assert (current_item.value === undefined);
assert (current_item.done === true);
/* Test empty array */
array = [];
iterator = array.values ();
current_item = iterator.next ();
assert (current_item.value === undefined);
assert (current_item.done === true);
/* Test delete elements after the iterator has been constructed */
array = [0, 1, 2, 3, 4, 5];
iterator = array.values ();
for (var i = 0; i < array.length; i++) {
current_item = iterator.next ();
assert (current_item.value === array[i]);
assert (current_item.done === false);
array.pop();
}
assert ([].values ().toString () === "[object Array Iterator]");

View File

@ -0,0 +1,21 @@
// Copyright JS Foundation and other contributors, http://js.foundation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
var array = [];
var array_iterator = array[Symbol.iterator]();
var array_iterator_prototype = Object.getPrototypeOf (array_iterator);
var iterator_prototype = Object.getPrototypeOf (array_iterator_prototype);
var iterator_prototype_iterator = iterator_prototype[Symbol.iterator]();
assert (iterator_prototype === iterator_prototype_iterator);