Fix Array.find and Array.findIndex omitting empty values. (#3563)

Empty values in array were omitted. Predicate should be called
for every element from 0 to length.

JerryScript-DCO-1.0-Signed-off-by: Rafal Walczyna r.walczyna@samsung.com
This commit is contained in:
rwalczyna
2020-02-18 15:19:53 +01:00
committed by GitHub
parent 181570ff41
commit 0ff664cab6
3 changed files with 42 additions and 31 deletions
@@ -2290,48 +2290,45 @@ ecma_builtin_array_prototype_object_find (ecma_value_t predicate, /**< callback
for (uint32_t index = 0; index < len; index++)
{
/* 8.a - 8.c */
ecma_value_t get_value = ecma_op_object_find_by_uint32_index (obj_p, index);
ecma_value_t get_value = ecma_op_object_get_by_uint32_index (obj_p, index);
if (ECMA_IS_VALUE_ERROR (get_value))
{
return get_value;
}
if (ecma_is_value_found (get_value))
/* 8.d - 8.e */
ecma_value_t current_index = ecma_make_uint32_value (index);
ecma_value_t call_args[] = { get_value, current_index, ecma_make_object_value (obj_p) };
ecma_value_t call_value = ecma_op_function_call (func_object_p, predicate_this_arg, call_args, 3);
if (ECMA_IS_VALUE_ERROR (call_value))
{
/* 8.d - 8.e */
ecma_value_t current_index = ecma_make_uint32_value (index);
ecma_free_value (get_value);
return call_value;
}
ecma_value_t call_args[] = { get_value, current_index, ecma_make_object_value (obj_p) };
bool call_value_to_bool = ecma_op_to_boolean (call_value);
ecma_value_t call_value = ecma_op_function_call (func_object_p, predicate_this_arg, call_args, 3);
ecma_free_value (call_value);
if (ECMA_IS_VALUE_ERROR (call_value))
if (call_value_to_bool)
{
/* 8.f */
if (is_find)
{
ecma_free_value (get_value);
return call_value;
}
bool call_value_to_bool = ecma_op_to_boolean (call_value);
ecma_free_value (call_value);
if (call_value_to_bool)
{
/* 8.f */
if (is_find)
{
ecma_free_value (current_index);
return get_value;
}
ecma_free_value (get_value);
return current_index;
ecma_free_value (current_index);
return get_value;
}
ecma_free_value (get_value);
ecma_free_value (current_index);
return current_index;
}
ecma_free_value (get_value);
ecma_free_value (current_index);
}
/* 9. */
@@ -149,7 +149,14 @@ function func (element) {
/* ES v6.0 22.1.3.9.8.c
Checking behavior when the first element deletes the second */
function f() { delete arr[1]; };
var arr = [0, 1, 2, 3];
Object.defineProperty(arr, '0', { 'get' : f });
Array.prototype.findIndex.call(arr, func);
function f() { delete arr[1]; };
var arr = [0, 1, 2, 3];
Object.defineProperty(arr, '0', { 'get' : f });
Array.prototype.findIndex.call(arr, func);
/* ES v6.0 22.1.3.9.8
Checking whether predicate is called also for empty elements */
var count = 0;
[,,,].findIndex(function() { count++; return false; });
assert (count == 3);
@@ -146,3 +146,10 @@ function f() { delete arr[1]; };
var arr = [0, 1, 2, 3];
Object.defineProperty(arr, '0', { 'get' : f });
Array.prototype.find.call(arr, func);
/* ES v6.0 22.1.3.8.8
Checking whether predicate is called also for empty elements */
var count = 0;
[,,,].find(function() { count++; return false; });
assert (count == 3);