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
21 changed files with 1045 additions and 6 deletions
@@ -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 */
@@ -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"
@@ -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 */
/**
* @}
* @}
@@ -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 */
@@ -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 */
@@ -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"
@@ -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,