Update the isArray operation to handle proxy objects (#3666)

Added step 3. from ECMA-262 v6, 7.2.2

JerryScript-DCO-1.0-Signed-off-by: Adam Szilagyi aszilagy@inf.u-szeged.hu
This commit is contained in:
Szilagyi Adam 2020-04-15 12:00:15 +02:00 committed by GitHub
parent 4342c9ea6f
commit d82bbb1c77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 92 additions and 10 deletions

View File

@ -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 */
/**

View File

@ -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);

View File

@ -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)

View File

@ -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)

View File

@ -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))

View File

@ -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 */
/**

View File

@ -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);