mirror of
https://github.com/jerryscript-project/jerryscript.git
synced 2025-12-15 16:29:21 +00:00
Implement Array.from routine (#3402)
The patch also revealed a minor issue about Map[Symbol.iterator] which have been fixed as well. Co-authored-by: Daniella Barsony bella@inf.u-szeged.hu JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu
This commit is contained in:
parent
d31871d7c9
commit
bfc495f0cb
@ -15,14 +15,16 @@
|
||||
|
||||
#include "ecma-alloc.h"
|
||||
#include "ecma-builtins.h"
|
||||
#include "ecma-builtin-helpers.h"
|
||||
#include "ecma-conversion.h"
|
||||
#include "ecma-exceptions.h"
|
||||
#include "ecma-function-object.h"
|
||||
#include "ecma-gc.h"
|
||||
#include "ecma-globals.h"
|
||||
#include "ecma-helpers.h"
|
||||
#include "ecma-iterator-object.h"
|
||||
#include "ecma-objects.h"
|
||||
#include "ecma-array-object.h"
|
||||
#include "ecma-try-catch-macro.h"
|
||||
#include "jrt.h"
|
||||
|
||||
#if ENABLED (JERRY_BUILTIN_ARRAY)
|
||||
@ -62,6 +64,334 @@ ecma_builtin_array_object_is_array (ecma_value_t this_arg, /**< 'this' argument
|
||||
return ecma_make_boolean_value (ecma_is_value_array (arg));
|
||||
} /* ecma_builtin_array_object_is_array */
|
||||
|
||||
#if ENABLED (JERRY_ES2015)
|
||||
/**
|
||||
* The Array object's 'from' routine
|
||||
*
|
||||
* See also:
|
||||
* ECMA-262 v6, 22.1.2.1
|
||||
*
|
||||
* @return ecma value
|
||||
* Returned value must be freed with ecma_free_value.
|
||||
*/
|
||||
static ecma_value_t
|
||||
ecma_builtin_array_object_from (ecma_value_t this_arg, /**< 'this' argument */
|
||||
const ecma_value_t *arguments_list_p, /**< arguments list */
|
||||
ecma_length_t arguments_list_len) /**< number of arguments */
|
||||
{
|
||||
/* 1. */
|
||||
ecma_value_t constructor = this_arg;
|
||||
ecma_value_t call_this_arg = ECMA_VALUE_UNDEFINED;
|
||||
ecma_value_t items = arguments_list_p[0];
|
||||
ecma_value_t mapfn = (arguments_list_len > 1) ? arguments_list_p[1] : ECMA_VALUE_UNDEFINED;
|
||||
|
||||
/* 2. */
|
||||
ecma_object_t *mapfn_obj_p = NULL;
|
||||
|
||||
/* 3. */
|
||||
if (!ecma_is_value_undefined (mapfn))
|
||||
{
|
||||
/* 3.a */
|
||||
if (!ecma_op_is_callable (mapfn))
|
||||
{
|
||||
return ecma_raise_type_error (ECMA_ERR_MSG ("Callback function is not callable."));
|
||||
}
|
||||
|
||||
/* 3.b */
|
||||
if (arguments_list_len > 2)
|
||||
{
|
||||
call_this_arg = arguments_list_p[2];
|
||||
}
|
||||
|
||||
/* 3.c */
|
||||
mapfn_obj_p = ecma_get_object_from_value (mapfn);
|
||||
}
|
||||
|
||||
/* 4. */
|
||||
ecma_value_t using_iterator = ecma_op_get_method_by_symbol_id (items, LIT_MAGIC_STRING_ITERATOR);
|
||||
|
||||
/* 5. */
|
||||
if (ECMA_IS_VALUE_ERROR (using_iterator))
|
||||
{
|
||||
return using_iterator;
|
||||
}
|
||||
|
||||
ecma_value_t ret_value = ECMA_VALUE_ERROR;
|
||||
|
||||
/* 6. */
|
||||
if (!ecma_is_value_undefined (using_iterator))
|
||||
{
|
||||
ecma_value_t array;
|
||||
|
||||
/* 6.a */
|
||||
if (ecma_is_constructor (constructor))
|
||||
{
|
||||
ecma_object_t *constructor_obj_p = ecma_get_object_from_value (constructor);
|
||||
|
||||
array = ecma_op_function_construct (constructor_obj_p, ECMA_VALUE_UNDEFINED, NULL, 0);
|
||||
|
||||
if (ecma_is_value_undefined (array) || ecma_is_value_null (array))
|
||||
{
|
||||
ecma_free_value (using_iterator);
|
||||
return ecma_raise_type_error (ECMA_ERR_MSG ("Cannot convert undefined or null to object"));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 6.b */
|
||||
array = ecma_op_create_array_object (NULL, 0, false);
|
||||
}
|
||||
|
||||
/* 6.c */
|
||||
if (ECMA_IS_VALUE_ERROR (array))
|
||||
{
|
||||
ecma_free_value (using_iterator);
|
||||
return array;
|
||||
}
|
||||
|
||||
ecma_object_t *array_obj_p = ecma_get_object_from_value (array);
|
||||
|
||||
/* 6.d */
|
||||
ecma_value_t iterator = ecma_op_get_iterator (items, using_iterator);
|
||||
ecma_free_value (using_iterator);
|
||||
|
||||
/* 6.e */
|
||||
if (ECMA_IS_VALUE_ERROR (iterator))
|
||||
{
|
||||
ecma_free_value (array);
|
||||
return iterator;
|
||||
}
|
||||
|
||||
/* 6.f */
|
||||
uint32_t k = 0;
|
||||
|
||||
/* 6.g */
|
||||
while (true)
|
||||
{
|
||||
/* 6.g.ii */
|
||||
ecma_value_t next = ecma_op_iterator_step (iterator);
|
||||
|
||||
/* 6.g.iii */
|
||||
if (ECMA_IS_VALUE_ERROR (next))
|
||||
{
|
||||
goto iterator_cleanup;
|
||||
}
|
||||
|
||||
/* 6.g.iii */
|
||||
if (ecma_is_value_false (next))
|
||||
{
|
||||
/* 6.g.iv.1 */
|
||||
ecma_value_t len_value = ecma_make_uint32_value (k);
|
||||
ecma_value_t set_status = ecma_op_object_put (array_obj_p,
|
||||
ecma_get_magic_string (LIT_MAGIC_STRING_LENGTH),
|
||||
len_value,
|
||||
true);
|
||||
ecma_free_value (len_value);
|
||||
|
||||
/* 6.g.iv.2 */
|
||||
if (ECMA_IS_VALUE_ERROR (set_status))
|
||||
{
|
||||
goto iterator_cleanup;
|
||||
}
|
||||
|
||||
ecma_free_value (iterator);
|
||||
/* 6.g.iv.3 */
|
||||
return array;
|
||||
}
|
||||
|
||||
/* 6.g.v */
|
||||
ecma_value_t next_value = ecma_op_iterator_value (next);
|
||||
|
||||
ecma_free_value (next);
|
||||
|
||||
/* 6.g.vi */
|
||||
if (ECMA_IS_VALUE_ERROR (next_value))
|
||||
{
|
||||
goto iterator_cleanup;
|
||||
}
|
||||
|
||||
ecma_value_t mapped_value;
|
||||
/* 6.g.vii */
|
||||
if (mapfn_obj_p != NULL)
|
||||
{
|
||||
/* 6.g.vii.1 */
|
||||
ecma_value_t args_p[2] = { next_value, ecma_make_uint32_value (k) };
|
||||
/* 6.g.vii.3 */
|
||||
mapped_value = ecma_op_function_call (mapfn_obj_p, call_this_arg, args_p, 2);
|
||||
ecma_free_value (args_p[1]);
|
||||
ecma_free_value (next_value);
|
||||
|
||||
/* 6.g.vii.2 */
|
||||
if (ECMA_IS_VALUE_ERROR (mapped_value))
|
||||
{
|
||||
ecma_op_iterator_close (iterator);
|
||||
goto iterator_cleanup;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 6.g.viii */
|
||||
mapped_value = next_value;
|
||||
}
|
||||
|
||||
/* 6.g.ix */
|
||||
const uint32_t flags = ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE | ECMA_IS_THROW;
|
||||
ecma_value_t set_status = ecma_builtin_helper_def_prop_by_index (array_obj_p, k, mapped_value, flags);
|
||||
|
||||
ecma_free_value (mapped_value);
|
||||
|
||||
/* 6.g.x */
|
||||
if (ECMA_IS_VALUE_ERROR (set_status))
|
||||
{
|
||||
ecma_op_iterator_close (iterator);
|
||||
goto iterator_cleanup;
|
||||
}
|
||||
|
||||
/* 6.g.xi */
|
||||
k++;
|
||||
}
|
||||
|
||||
iterator_cleanup:
|
||||
ecma_free_value (iterator);
|
||||
ecma_free_value (array);
|
||||
|
||||
return ret_value;
|
||||
}
|
||||
|
||||
/* 8. */
|
||||
ecma_value_t array_like = ecma_op_to_object (items);
|
||||
|
||||
/* 9. */
|
||||
if (ECMA_IS_VALUE_ERROR (array_like))
|
||||
{
|
||||
return array_like;
|
||||
}
|
||||
|
||||
ecma_object_t *array_like_obj_p = ecma_get_object_from_value (array_like);
|
||||
|
||||
/* 10. */
|
||||
uint32_t len;
|
||||
ecma_value_t len_value = ecma_op_object_get_length (array_like_obj_p, &len);
|
||||
|
||||
/* 11. */
|
||||
if (ECMA_IS_VALUE_ERROR (len_value))
|
||||
{
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
len_value = ecma_make_uint32_value (len);
|
||||
|
||||
/* 12. */
|
||||
ecma_value_t array;
|
||||
|
||||
/* 12.a */
|
||||
if (ecma_is_constructor (constructor))
|
||||
{
|
||||
ecma_object_t *constructor_obj_p = ecma_get_object_from_value (constructor);
|
||||
|
||||
array = ecma_op_function_construct (constructor_obj_p, ECMA_VALUE_UNDEFINED, &len_value, 1);
|
||||
|
||||
if (ecma_is_value_undefined (array) || ecma_is_value_null (array))
|
||||
{
|
||||
ecma_free_value (len_value);
|
||||
ecma_raise_type_error (ECMA_ERR_MSG ("Cannot convert undefined or null to object"));
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 13.a */
|
||||
array = ecma_op_create_array_object (&len_value, 1, true);
|
||||
}
|
||||
|
||||
ecma_free_value (len_value);
|
||||
|
||||
/* 14. */
|
||||
if (ECMA_IS_VALUE_ERROR (array))
|
||||
{
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
ecma_object_t *array_obj_p = ecma_get_object_from_value (array);
|
||||
|
||||
/* 15. */
|
||||
uint32_t k = 0;
|
||||
|
||||
/* 16. */
|
||||
while (k < len)
|
||||
{
|
||||
/* 16.b */
|
||||
ecma_string_t *pk = ecma_new_ecma_string_from_uint32 (k);
|
||||
ecma_value_t k_value = ecma_op_object_get (array_like_obj_p, pk);
|
||||
ecma_deref_ecma_string (pk);
|
||||
|
||||
/* 16.c */
|
||||
if (ECMA_IS_VALUE_ERROR (k_value))
|
||||
{
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
ecma_value_t mapped_value;
|
||||
/* 16.d */
|
||||
if (mapfn_obj_p != NULL)
|
||||
{
|
||||
/* 16.d.i */
|
||||
ecma_value_t args_p[2] = { k_value, ecma_make_uint32_value (k) };
|
||||
mapped_value = ecma_op_function_call (mapfn_obj_p, call_this_arg, args_p, 2);
|
||||
ecma_free_value (args_p[1]);
|
||||
ecma_free_value (k_value);
|
||||
|
||||
/* 16.d.ii */
|
||||
if (ECMA_IS_VALUE_ERROR (mapped_value))
|
||||
{
|
||||
goto cleanup;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
/* 16.e */
|
||||
mapped_value = k_value;
|
||||
}
|
||||
|
||||
/* 16.f */
|
||||
const uint32_t flags = ECMA_PROPERTY_CONFIGURABLE_ENUMERABLE_WRITABLE | ECMA_IS_THROW;
|
||||
ecma_value_t set_status = ecma_builtin_helper_def_prop_by_index (array_obj_p, k, mapped_value, flags);
|
||||
|
||||
ecma_free_value (mapped_value);
|
||||
|
||||
/* 16.g */
|
||||
if (ECMA_IS_VALUE_ERROR (set_status))
|
||||
{
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* 16.h */
|
||||
k++;
|
||||
}
|
||||
|
||||
/* 17. */
|
||||
len_value = ecma_make_uint32_value (k);
|
||||
ecma_value_t set_status = ecma_op_object_put (array_obj_p,
|
||||
ecma_get_magic_string (LIT_MAGIC_STRING_LENGTH),
|
||||
len_value,
|
||||
true);
|
||||
ecma_free_value (len_value);
|
||||
|
||||
/* 18. */
|
||||
if (ECMA_IS_VALUE_ERROR (set_status))
|
||||
{
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* 19. */
|
||||
ret_value = ecma_make_object_value (array_obj_p);
|
||||
|
||||
cleanup:
|
||||
ecma_deref_object (array_like_obj_p);
|
||||
return ret_value;
|
||||
} /* ecma_builtin_array_object_from */
|
||||
#endif /* ENABLED (JERRY_ES2015) */
|
||||
|
||||
/**
|
||||
* Handle calling [[Call]] of built-in Array object
|
||||
*
|
||||
|
||||
@ -40,6 +40,9 @@ NUMBER_VALUE (LIT_MAGIC_STRING_LENGTH,
|
||||
/* Routine properties:
|
||||
* (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */
|
||||
ROUTINE (LIT_MAGIC_STRING_IS_ARRAY_UL, ecma_builtin_array_object_is_array, 1, 1)
|
||||
#if ENABLED (JERRY_ES2015)
|
||||
ROUTINE (LIT_MAGIC_STRING_FROM, ecma_builtin_array_object_from, NON_FIXED, 1)
|
||||
#endif /* ENABLED (JERRY_ES2015) */
|
||||
|
||||
#endif /* !(ENABLED (JERRY_BUILTIN_ARRAY)) */
|
||||
|
||||
|
||||
@ -48,7 +48,7 @@ ROUTINE (LIT_MAGIC_STRING_SET, ecma_builtin_map_prototype_object_set, 2, 2)
|
||||
ROUTINE (LIT_MAGIC_STRING_ENTRIES, ecma_builtin_map_prototype_object_entries, 0, 0)
|
||||
ROUTINE (LIT_MAGIC_STRING_VALUES, ecma_builtin_map_prototype_object_values, 0, 0)
|
||||
ROUTINE (LIT_MAGIC_STRING_KEYS, ecma_builtin_map_prototype_object_keys, 0, 0)
|
||||
ROUTINE (LIT_GLOBAL_SYMBOL_ITERATOR, ecma_builtin_map_prototype_object_values, 0, 0)
|
||||
ROUTINE (LIT_GLOBAL_SYMBOL_ITERATOR, ecma_builtin_map_prototype_object_entries, 0, 0)
|
||||
#endif /* ENABLED (JERRY_ES2015) */
|
||||
|
||||
/* ECMA-262 v6, 23.1.3.10 */
|
||||
|
||||
@ -139,8 +139,6 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_EXEC, "exec")
|
||||
|| ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_FILL, "fill")
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_FIND, "find")
|
||||
#endif
|
||||
#if ENABLED (JERRY_ES2015_BUILTIN_TYPEDARRAY)
|
||||
LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_FROM, "from")
|
||||
#endif
|
||||
#if ENABLED (JERRY_BUILTIN_ARRAY) \
|
||||
|
||||
344
tests/jerry/es2015/array-from.js
Normal file
344
tests/jerry/es2015/array-from.js
Normal file
@ -0,0 +1,344 @@
|
||||
// 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.
|
||||
|
||||
// Copyright 2014 the V8 project authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
// Some methods are taken from v8/test/mjsunit/mjsunit.js
|
||||
|
||||
function classOf(object) {
|
||||
// Argument must not be null or undefined.
|
||||
var string = Object.prototype.toString.call(object);
|
||||
// String has format [object <ClassName>].
|
||||
return string.substring(8, string.length - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two objects for key/value equality.
|
||||
* Returns true if they are equal, false otherwise.
|
||||
*/
|
||||
function deepObjectEquals(a, b) {
|
||||
var aProps = Object.keys(a);
|
||||
aProps.sort();
|
||||
var bProps = Object.keys(b);
|
||||
bProps.sort();
|
||||
if (!deepEquals(aProps, bProps)) {
|
||||
return false;
|
||||
}
|
||||
for (var i = 0; i < aProps.length; i++) {
|
||||
if (!deepEquals(a[aProps[i]], b[aProps[i]])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
var assertInstanceof = function assertInstanceof(obj, type) {
|
||||
if (!(obj instanceof type)) {
|
||||
var actualTypeName = null;
|
||||
var actualConstructor = Object.getPrototypeOf(obj).constructor;
|
||||
if (typeof actualConstructor == "function") {
|
||||
actualTypeName = actualConstructor.name || String(actualConstructor);
|
||||
} print("Object <" + obj + "> is not an instance of <" + (type.name || type) + ">" + (actualTypeName ? " but of < " + actualTypeName + ">" : ""));
|
||||
}
|
||||
};
|
||||
|
||||
function deepEquals(a, b) {
|
||||
if (a === b) {
|
||||
if (a === 0) return (1 / a) === (1 / b);
|
||||
return true;
|
||||
}
|
||||
if (typeof a != typeof b) return false;
|
||||
if (typeof a == "number") return isNaN(a) && isNaN(b);
|
||||
if (typeof a !== "object" && typeof a !== "function") return false;
|
||||
var objectClass = classOf(a);
|
||||
if (objectClass !== classOf(b)) return false;
|
||||
if (objectClass === "RegExp") {
|
||||
return (a.toString() === b.toString());
|
||||
}
|
||||
if (objectClass === "Function") return false;
|
||||
if (objectClass === "Array") {
|
||||
var elementCount = 0;
|
||||
if (a.length != b.length) {
|
||||
return false;
|
||||
}
|
||||
for (var i = 0; i < a.length; i++) {
|
||||
if (!deepEquals(a[i], b[i]))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
if (objectClass == "String" || objectClass == "Number" || objectClass == "Boolean" || objectClass == "Date") {
|
||||
if (a.valueOf() !== b.valueOf()) return false;
|
||||
}
|
||||
return deepObjectEquals(a, b);
|
||||
}
|
||||
|
||||
var assertEquals = function assertEquals(expected, found, name_opt) {
|
||||
if (!deepEquals(found, expected)) {
|
||||
assert(false);
|
||||
}
|
||||
};
|
||||
|
||||
function assertArrayLikeEquals(value, expected, type) {
|
||||
assertInstanceof(value, type);
|
||||
assert(expected.length === value.length);
|
||||
for (var i=0; i<value.length; ++i) {
|
||||
assertEquals(expected[i], value[i]);
|
||||
}
|
||||
}
|
||||
|
||||
assert(1 === Array.from.length);
|
||||
|
||||
// Assert that constructor is called with "length" for array-like objects
|
||||
|
||||
var myCollectionCalled = false;
|
||||
function MyCollection(length) {
|
||||
myCollectionCalled = true;
|
||||
assert(1 === arguments.length);
|
||||
|
||||
assert(5 === length);
|
||||
}
|
||||
|
||||
Array.from.call(MyCollection, {length: 5});
|
||||
assert(myCollectionCalled === true);
|
||||
// Assert that calling mapfn with / without thisArg in sloppy and strict modes
|
||||
// works as expected.
|
||||
|
||||
var global = this;
|
||||
function non_strict(){ assert(global === this); }
|
||||
function strict(){ "use strict"; assert(void 0 === this); }
|
||||
function strict_null(){ "use strict"; assert(null === this); }
|
||||
Array.from([1], non_strict);
|
||||
Array.from([1], non_strict, void 0);
|
||||
Array.from([1], strict);
|
||||
Array.from([1], strict, void 0);
|
||||
|
||||
function testArrayFrom(thisArg, constructor) {
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, [], undefined), [], constructor);
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, NaN), [], constructor);
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, Infinity), [], constructor);
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, 10000000), [], constructor);
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, 'test'), ['t', 'e', 's', 't'], constructor);
|
||||
|
||||
assertArrayLikeEquals(Array.from.call(thisArg,
|
||||
{ length: 1, '0': { 'foo': 'bar' } }), [{'foo': 'bar'}], constructor);
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, { length: -1, '0': { 'foo': 'bar' } }), [], constructor);
|
||||
assertArrayLikeEquals(Array.from.call(thisArg,
|
||||
[ 'foo', 'bar', 'baz' ]), ['foo', 'bar', 'baz'], constructor);
|
||||
var kSet = new Set(['foo', 'bar', 'baz']);
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, kSet), ['foo', 'bar', 'baz'], constructor);
|
||||
var kMap = new Map(['foo', 'bar', 'baz'].entries());
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, kMap), [[0, 'foo'], [1, 'bar'], [2, 'baz']], constructor);
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) {
|
||||
return this.filter(x);
|
||||
}, {
|
||||
filter: function(x) { return x.toUpperCase(); }
|
||||
}), ['T', 'E', 'S', 'T'], constructor);
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, 'test', function(x) {
|
||||
return x.toUpperCase();
|
||||
}), ['T', 'E', 'S', 'T'], constructor);
|
||||
|
||||
try {
|
||||
Array.from.call(thisArg, null);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
try {
|
||||
Array.from.call(thisArg, undefined);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
try {
|
||||
Array.from.call(thisArg, [], null);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
try {
|
||||
Array.from.call(thisArg, [], "noncallable");
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
var nullIterator = {};
|
||||
nullIterator[Symbol.iterator] = null;
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, nullIterator), [],
|
||||
constructor);
|
||||
|
||||
var nonObjIterator = {};
|
||||
nonObjIterator[Symbol.iterator] = function() { return "nonObject"; };
|
||||
|
||||
try {
|
||||
Array.from.call(thisArg, nonObjIterator);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
try {
|
||||
Array.from.call(thisArg, [], null);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
// Ensure iterator is only accessed once, and only invoked once
|
||||
var called = false;
|
||||
var arr = [1, 2, 3];
|
||||
var obj = {};
|
||||
|
||||
// Test order --- only get iterator method once
|
||||
function testIterator() {
|
||||
assert(called !== "@@iterator should be called only once");
|
||||
called = true;
|
||||
assert(obj === this);
|
||||
return arr[Symbol.iterator]();
|
||||
}
|
||||
var getCalled = false;
|
||||
Object.defineProperty(obj, Symbol.iterator, {
|
||||
get: function() {
|
||||
assert(getCalled !== "@@iterator should be accessed only once");
|
||||
getCalled = true;
|
||||
return testIterator;
|
||||
},
|
||||
set: function() {
|
||||
// "@@iterator should not be set"
|
||||
assert(false);
|
||||
}
|
||||
});
|
||||
assertArrayLikeEquals(Array.from.call(thisArg, obj), [1, 2, 3], constructor);
|
||||
}
|
||||
|
||||
function Other() {}
|
||||
|
||||
var boundFn = (function() {}).bind(Array, 27);
|
||||
|
||||
testArrayFrom(Array, Array);
|
||||
testArrayFrom(null, Array);
|
||||
testArrayFrom({}, Array);
|
||||
testArrayFrom(Object, Object);
|
||||
testArrayFrom(Other, Other);
|
||||
testArrayFrom(Math.cos, Array);
|
||||
testArrayFrom(boundFn, boundFn);
|
||||
|
||||
// Assert that [[DefineOwnProperty]] is used in ArrayFrom, meaning a
|
||||
// setter isn't called, and a failed [[DefineOwnProperty]] will throw.
|
||||
var setterCalled = 0;
|
||||
function exotic() {
|
||||
Object.defineProperty(this, '0', {
|
||||
get: function() { return 2; },
|
||||
set: function() { setterCalled++; }
|
||||
});
|
||||
}
|
||||
|
||||
// Non-configurable properties can't be overwritten with DefineOwnProperty
|
||||
// The setter wasn't called
|
||||
try {
|
||||
Array.from.call(exotic, [1]);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
assert( 0 === setterCalled);
|
||||
|
||||
// Non-callable iterators should cause a TypeError before calling the target
|
||||
// constructor.
|
||||
items = {};
|
||||
items[Symbol.iterator] = 7;
|
||||
function TestError() {}
|
||||
function ArrayLike() { throw new TestError() }
|
||||
|
||||
try {
|
||||
Array.from.call(ArrayLike, items);
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert (e instanceof TypeError);
|
||||
}
|
||||
|
||||
// Check that array properties defined are writable, enumerable, configurable
|
||||
function ordinary() { }
|
||||
var x = Array.from.call(ordinary, [2]);
|
||||
var xlength = Object.getOwnPropertyDescriptor(x, 'length');
|
||||
assert(1 === xlength.value);
|
||||
assert(true === xlength.writable);
|
||||
assert(true === xlength.enumerable);
|
||||
assert(true === xlength.configurable);
|
||||
var x0 = Object.getOwnPropertyDescriptor(x, 0);
|
||||
assert(2 === x0.value);
|
||||
assert(true === xlength.writable);
|
||||
assert(true === xlength.enumerable);
|
||||
assert(true === xlength.configurable);
|
||||
|
||||
/* Test iterator close */
|
||||
function __createIterableObject (arr, methods) {
|
||||
methods = methods || {};
|
||||
if (typeof Symbol !== 'function' || !Symbol.iterator) {
|
||||
return {};
|
||||
}
|
||||
arr.length++;
|
||||
var iterator = {
|
||||
next: function() {
|
||||
return { value: arr.shift(), done: arr.length <= 0 };
|
||||
},
|
||||
'return': methods['return'],
|
||||
'throw': methods['throw']
|
||||
};
|
||||
var iterable = {};
|
||||
iterable[Symbol.iterator] = function () { return iterator; };
|
||||
return iterable;
|
||||
};
|
||||
|
||||
function close1() {
|
||||
var closed = false;
|
||||
var iter = __createIterableObject([1, 2, 3], {
|
||||
'return': function() { closed = true; return {}; }
|
||||
});
|
||||
|
||||
try {
|
||||
Array.from(iter, x => { throw 5 });
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e === 5);
|
||||
}
|
||||
|
||||
return closed;
|
||||
}
|
||||
assert(close1());
|
||||
|
||||
function close2() {
|
||||
var closed = false;
|
||||
var iter = __createIterableObject([1, 2, 3], {
|
||||
'return': function() { closed = true; throw 6 }
|
||||
});
|
||||
|
||||
try {
|
||||
Array.from(iter, x => { throw 5 });
|
||||
assert(false);
|
||||
} catch (e) {
|
||||
assert(e === 5);
|
||||
}
|
||||
|
||||
return closed;
|
||||
}
|
||||
assert(close2());
|
||||
|
||||
@ -62,62 +62,62 @@ methods.forEach(function (method) {
|
||||
}
|
||||
});
|
||||
|
||||
var valueIterators = [m.values(), m[Symbol.iterator]()];
|
||||
var entryIterators = [m.entries(), m[Symbol.iterator]()];
|
||||
var keyIterator = m.keys();
|
||||
var entryIterator = m.entries();
|
||||
var valueIterator = m.values();
|
||||
var elementCount = m.size;
|
||||
|
||||
for (var i = 0; i < elementCount; i++) {
|
||||
valueIterators.forEach(function(element) {
|
||||
entryIterators.forEach(function(element) {
|
||||
var next = element.next();
|
||||
assert(next.done === false);
|
||||
assert(next.value === i);
|
||||
assert(next.value[0] === '' + i);
|
||||
assert(next.value[1] === i);
|
||||
});
|
||||
|
||||
var next = keyIterator.next();
|
||||
assert(next.done === false);
|
||||
assert(next.value === '' + i);
|
||||
|
||||
var next = entryIterator.next();
|
||||
var next = valueIterator.next();
|
||||
assert(next.done === false);
|
||||
assert(next.value[0] === '' + i);
|
||||
assert(next.value[1] === i);
|
||||
assert(next.value === i);
|
||||
}
|
||||
|
||||
valueIterators.forEach(function(element) {
|
||||
var next = element.next();
|
||||
assert(next.done === true);
|
||||
assert(next.value === undefined);
|
||||
});
|
||||
entryIterators.forEach(function(element) {
|
||||
var next = element.next();
|
||||
assert(next.done === true);
|
||||
assert(next.value === undefined);
|
||||
});
|
||||
|
||||
var next = keyIterator.next();
|
||||
assert(next.done === true);
|
||||
assert(next.value === undefined);
|
||||
|
||||
next = entryIterator.next();
|
||||
next = valueIterator.next();
|
||||
assert(next.done === true);
|
||||
assert(next.value === undefined);
|
||||
|
||||
var valueIterators = [m.values(), m[Symbol.iterator]()];
|
||||
var entryIterators = [m.entries(), m[Symbol.iterator]()];
|
||||
var keyIterator = m.keys();
|
||||
var entryIterator = m.entries();
|
||||
var valueIterator = m.values();
|
||||
var elementCount = m.size;
|
||||
|
||||
for (var i = 0; i < elementCount; i++) {
|
||||
valueIterators.forEach(function(element) {
|
||||
entryIterators.forEach(function(element) {
|
||||
var next = element.next();
|
||||
assert(next.done === false);
|
||||
assert(next.value === i);
|
||||
assert(next.value[0] === '' + i);
|
||||
assert(next.value[1] === i);
|
||||
});
|
||||
|
||||
var next = keyIterator.next();
|
||||
assert(next.done === false);
|
||||
assert(next.value === '' + i);
|
||||
|
||||
var next = entryIterator.next();
|
||||
var next = valueIterator.next();
|
||||
assert(next.done === false);
|
||||
assert(next.value[0] === '' + i);
|
||||
assert(next.value[1] === i);
|
||||
assert(next.value === i);
|
||||
m.delete('' + i);
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user