diff --git a/jerry-core/ecma/base/ecma-helpers-value.c b/jerry-core/ecma/base/ecma-helpers-value.c index cc2354a4e..25babda2b 100644 --- a/jerry-core/ecma/base/ecma-helpers-value.c +++ b/jerry-core/ecma/base/ecma-helpers-value.c @@ -14,6 +14,7 @@ */ #include "ecma-alloc.h" +#include "ecma-exceptions.h" #include "ecma-gc.h" #include "ecma-globals.h" #include "ecma-helpers.h" @@ -412,14 +413,40 @@ ecma_check_value_type_is_spec_defined (ecma_value_t value) /**< ecma value */ /** * Checks if the given argument is an array or not. * - * @return true - if the given argument is an array object - * false - otherwise + * @return ECMA_VALUE_ERROR- if the operation fails + * ECMA_VALUE_{TRUE/FALSE} - depends on whether 'arg' is an array object */ -inline bool JERRY_ATTR_CONST JERRY_ATTR_ALWAYS_INLINE +ecma_value_t ecma_is_value_array (ecma_value_t arg) /**< argument */ { - return (ecma_is_value_object (arg) - && ecma_get_object_type (ecma_get_object_from_value (arg)) == ECMA_OBJECT_TYPE_ARRAY); + if (!ecma_is_value_object (arg)) + { + return ECMA_VALUE_FALSE; + } + + ecma_object_t *arg_obj_p = ecma_get_object_from_value (arg); + + if (ecma_get_object_type (arg_obj_p) == ECMA_OBJECT_TYPE_ARRAY) + { + return ECMA_VALUE_TRUE; + } + +#if ENABLED (JERRY_ES2015_BUILTIN_PROXY) + if (ECMA_OBJECT_IS_PROXY (arg_obj_p)) + { + ecma_proxy_object_t *proxy_obj_p = (ecma_proxy_object_t *) arg_obj_p; + + if (proxy_obj_p->handler == ECMA_VALUE_NULL) + { + return ecma_raise_type_error (ECMA_ERR_MSG ("Cannot perform 'IsArray' on the given proxy " + "because handler is null")); + } + + return ecma_is_value_array (proxy_obj_p->target); + } +#endif /* ENABLED (JERRY_ES2015_BUILTIN_PROXY) */ + + return ECMA_VALUE_FALSE; } /* ecma_is_value_array */ /** diff --git a/jerry-core/ecma/base/ecma-helpers.h b/jerry-core/ecma/base/ecma-helpers.h index cb937be1d..69c44de03 100644 --- a/jerry-core/ecma/base/ecma-helpers.h +++ b/jerry-core/ecma/base/ecma-helpers.h @@ -236,7 +236,7 @@ bool JERRY_ATTR_CONST ecma_is_value_direct_string (ecma_value_t value); bool JERRY_ATTR_CONST ecma_is_value_non_direct_string (ecma_value_t value); bool JERRY_ATTR_CONST ecma_is_value_object (ecma_value_t value); bool JERRY_ATTR_CONST ecma_is_value_error_reference (ecma_value_t value); -bool JERRY_ATTR_CONST ecma_is_value_array (ecma_value_t arg); +ecma_value_t ecma_is_value_array (ecma_value_t arg); void ecma_check_value_type_is_spec_defined (ecma_value_t value); diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-array.c b/jerry-core/ecma/builtin-objects/ecma-builtin-array.c index 86f8a411f..4e266cf3b 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-array.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-array.c @@ -61,7 +61,7 @@ ecma_builtin_array_object_is_array (ecma_value_t this_arg, /**< 'this' argument { JERRY_UNUSED (this_arg); - return ecma_make_boolean_value (ecma_is_value_array (arg)); + return ecma_is_value_array (arg); } /* ecma_builtin_array_object_is_array */ #if ENABLED (JERRY_ES2015) diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-helpers.c b/jerry-core/ecma/builtin-objects/ecma-builtin-helpers.c index 42aba88d8..26d2a15b8 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-helpers.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-helpers.c @@ -378,7 +378,7 @@ ecma_builtin_helper_array_concat_value (ecma_object_t *array_obj_p, /**< array * bool spread_object = is_spreadable == ECMA_VALUE_TRUE; #else /* !ENABLED (JERRY_ES2015) */ - bool spread_object = ecma_is_value_array (value); + bool spread_object = ecma_is_value_true (ecma_is_value_array (value)); #endif /* ENABLED (JERRY_ES2015) */ if (spread_object) diff --git a/jerry-core/ecma/operations/ecma-array-object.c b/jerry-core/ecma/operations/ecma-array-object.c index ba3168334..4d5dd6a84 100644 --- a/jerry-core/ecma/operations/ecma-array-object.c +++ b/jerry-core/ecma/operations/ecma-array-object.c @@ -683,7 +683,14 @@ ecma_op_array_species_create (ecma_object_t *original_array_p, /**< The object f ecma_value_t constructor = ECMA_VALUE_UNDEFINED; ecma_value_t original_array = ecma_make_object_value (original_array_p); - if (ecma_is_value_array (original_array)) + ecma_value_t is_array = ecma_is_value_array (original_array); + + if (ECMA_IS_VALUE_ERROR (is_array)) + { + return is_array; + } + + if (ecma_is_value_true (is_array)) { constructor = ecma_op_object_get_by_magic_id (original_array_p, LIT_MAGIC_STRING_CONSTRUCTOR); if (ECMA_IS_VALUE_ERROR (constructor)) diff --git a/jerry-core/ecma/operations/ecma-objects.c b/jerry-core/ecma/operations/ecma-objects.c index 8608ecbc6..c6faca64c 100644 --- a/jerry-core/ecma/operations/ecma-objects.c +++ b/jerry-core/ecma/operations/ecma-objects.c @@ -2807,7 +2807,7 @@ ecma_op_is_concat_spreadable (ecma_value_t arg) /**< argument */ return ecma_make_boolean_value (to_bool); } - return (ecma_make_boolean_value (ecma_is_value_array (arg))); + return ecma_is_value_array (arg); } /* ecma_op_is_concat_spreadable */ /** diff --git a/tests/jerry/es2015/array-isarray.js b/tests/jerry/es2015/array-isarray.js new file mode 100644 index 000000000..af4923331 --- /dev/null +++ b/tests/jerry/es2015/array-isarray.js @@ -0,0 +1,48 @@ +// 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. + +assert(Array.isArray([]) === true); +assert(Array.isArray([1]) === true); +assert(Array.isArray(new Array()) === true); +assert(Array.isArray(new Array('a', 'b', 'c', 'd')) === true); +assert(Array.isArray(new Array(3)) === true); +assert(Array.isArray(Array.prototype) === true); +assert(Array.isArray(new Proxy([], {})) === true); + +assert(Array.isArray() === false); +assert(Array.isArray({}) === false); +assert(Array.isArray(null) === false); +assert(Array.isArray(undefined) === false); +assert(Array.isArray(17) === false); +assert(Array.isArray('Array') === false); +assert(Array.isArray(true) === false); +assert(Array.isArray(false) === false); +assert(Array.isArray(new Uint8Array(32)) === false); +assert(Array.isArray({ __proto__: Array.prototype }) === false); + +var revocable = Proxy.revocable ({}, {}); +var proxy = revocable.proxy; +revocable.revoke(); + +try { + Array.isArray(proxy); + assert(false); +} catch (e) { + assert(e instanceof TypeError); +} + +var revocable = Proxy.revocable ([], {}); +var proxy = revocable.proxy; + +assert(Array.isArray(proxy) === true);