Skip to content

Commit fab35c3

Browse files
authored
Fix the indexing problem in typedarray_prototype_index_of & last_index_of (#4305)
JerryScript-DCO-1.0-Signed-off-by: Virag Orkenyi [email protected]
1 parent f7b7873 commit fab35c3

File tree

2 files changed

+23
-28
lines changed

2 files changed

+23
-28
lines changed

jerry-core/ecma/builtin-objects/typedarray/ecma-builtin-typedarray-prototype.c

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,28 +1300,20 @@ ecma_builtin_typedarray_prototype_index_of (ecma_typedarray_info_t *info_p, /**<
13001300
}
13011301
else
13021302
{
1303-
/* 6. 7. */
1304-
ecma_number_t num_var;
1305-
if (ECMA_IS_VALUE_ERROR (ecma_op_to_integer (args[1], &num_var)))
1303+
if (ECMA_IS_VALUE_ERROR (ecma_builtin_helper_uint32_index_normalize (args[1],
1304+
info_p->length,
1305+
&from_index)))
13061306
{
13071307
return ECMA_VALUE_ERROR;
13081308
}
1309-
/* 8. */
1310-
if (num_var >= info_p->length)
1311-
{
1312-
return ecma_make_integer_value (-1);
1313-
}
13141309

1315-
/* 9. 10. */
1316-
from_index = ((num_var >= 0) ? (uint32_t) num_var
1317-
: (uint32_t) (info_p->length + num_var));
13181310
}
13191311

13201312
ecma_typedarray_getter_fn_t getter_cb = ecma_get_typedarray_getter_fn (info_p->id);
13211313

13221314
/* 11. */
1323-
for (int32_t position = (int32_t) from_index * info_p->element_size;
1324-
(uint32_t) position < limit;
1315+
for (uint32_t position = from_index * info_p->element_size;
1316+
position < limit;
13251317
position += info_p->element_size)
13261318
{
13271319
ecma_value_t element = getter_cb (info_p->buffer_p + position);
@@ -1375,40 +1367,45 @@ ecma_builtin_typedarray_prototype_last_index_of (ecma_typedarray_info_t *info_p,
13751367
}
13761368
else
13771369
{
1378-
/* 6. 7. */
1379-
ecma_number_t num_var;
1380-
if (ECMA_IS_VALUE_ERROR (ecma_op_to_integer (args[1], &num_var)))
1370+
if (ECMA_IS_VALUE_ERROR (ecma_builtin_helper_uint32_index_normalize (args[1],
1371+
info_p->length,
1372+
&from_index)))
1373+
{
1374+
return ECMA_VALUE_ERROR;
1375+
}
1376+
1377+
ecma_number_t to_int;
1378+
1379+
if (ECMA_IS_VALUE_ERROR (ecma_op_to_integer (args[1], &to_int)))
13811380
{
13821381
return ECMA_VALUE_ERROR;
13831382
}
13841383

1385-
if (!ecma_number_is_nan (num_var)
1386-
&& -num_var > info_p->length)
1384+
if (info_p->length + to_int < 0)
13871385
{
13881386
return ecma_make_integer_value (-1);
13891387
}
13901388

1391-
/* 8. 9. */
1392-
from_index = ((num_var >= 0) ? (uint32_t) JERRY_MIN (num_var, info_p->length - 1)
1393-
: (uint32_t) (info_p->length + num_var));
1389+
from_index = JERRY_MIN (from_index, info_p->length - 1);
13941390
}
13951391

13961392
ecma_typedarray_getter_fn_t getter_cb = ecma_get_typedarray_getter_fn (info_p->id);
1393+
uint8_t *current_element_p = info_p->buffer_p + from_index * info_p->element_size;
13971394

13981395
/* 10. */
1399-
for (int32_t position = (int32_t) from_index * info_p->element_size;
1400-
position >= 0;
1401-
position += -info_p->element_size)
1396+
while (current_element_p >= info_p->buffer_p)
14021397
{
1403-
ecma_value_t element = getter_cb (info_p->buffer_p + position);
1398+
ecma_value_t element = getter_cb (info_p->buffer_p + from_index * info_p->element_size);
14041399

14051400
if (ecma_op_same_value_zero (args[0], element, true))
14061401
{
14071402
ecma_free_value (element);
1408-
return ecma_make_number_value ((ecma_number_t) position / info_p->element_size);
1403+
return ecma_make_number_value ((ecma_number_t) from_index * info_p->element_size / info_p->element_size);
14091404
}
14101405

14111406
ecma_free_value (element);
1407+
current_element_p -= info_p->element_size;
1408+
from_index--;
14121409
}
14131410

14141411
/* 11. */

tests/test262-esnext-excludelist.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,6 @@
220220
<test id="built-ins/TypedArray/prototype/filter/speciesctor-get-species-custom-ctor.js"><reason></reason></test>
221221
<test id="built-ins/TypedArray/prototype/filter/speciesctor-get-species-returns-throws.js"><reason></reason></test>
222222
<test id="built-ins/TypedArray/prototype/filter/speciesctor-get-species.js"><reason></reason></test>
223-
<test id="built-ins/TypedArray/prototype/indexOf/BigInt/search-found-returns-index.js"><reason></reason></test>
224-
<test id="built-ins/TypedArray/prototype/indexOf/search-found-returns-index.js"><reason></reason></test>
225223
<test id="built-ins/TypedArray/prototype/join/BigInt/get-length-uses-internal-arraylength.js"><reason></reason></test>
226224
<test id="built-ins/TypedArray/prototype/join/BigInt/return-abrupt-from-separator-symbol.js"><reason></reason></test>
227225
<test id="built-ins/TypedArray/prototype/join/BigInt/return-abrupt-from-separator.js"><reason></reason></test>

0 commit comments

Comments
 (0)