Skip to content

Commit b0ff80b

Browse files
committed
Fix undefined fromIndex in Array.prototype.lastIndexOf()
JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai.u-szeged@partner.samsung.com
1 parent 4ee30cb commit b0ff80b

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,10 +1862,11 @@ ecma_builtin_array_prototype_object_index_of (ecma_value_t this_arg, /**< this a
18621862
*/
18631863
static ecma_completion_value_t
18641864
ecma_builtin_array_prototype_object_last_index_of (ecma_value_t this_arg, /**< this argument */
1865-
ecma_value_t arg1, /**< searchElement */
1866-
ecma_value_t arg2) /**< fromIndex */
1865+
const ecma_value_t args[], /**< arguments list */
1866+
ecma_length_t args_number) /**< number of arguments */
18671867
{
18681868
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
1869+
ecma_value_t search_element = (args_number > 0) ? args[0] : ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED);
18691870

18701871
/* 1. */
18711872
ECMA_TRY_CATCH (obj_this,
@@ -1898,9 +1899,9 @@ ecma_builtin_array_prototype_object_last_index_of (ecma_value_t this_arg, /**< t
18981899
uint32_t from_idx = len - 1;
18991900

19001901
/* 5. */
1901-
if (!ecma_is_value_undefined (arg2))
1902+
if (args_number > 1)
19021903
{
1903-
ECMA_OP_TO_NUMBER_TRY_CATCH (arg_from_idx, arg2, ret_value);
1904+
ECMA_OP_TO_NUMBER_TRY_CATCH (arg_from_idx, args[1], ret_value);
19041905

19051906
if (!ecma_number_is_nan (arg_from_idx))
19061907
{
@@ -1972,7 +1973,7 @@ ecma_builtin_array_prototype_object_last_index_of (ecma_value_t this_arg, /**< t
19721973
ECMA_TRY_CATCH (get_value, ecma_op_object_get (obj_p, idx_str_p), ret_value);
19731974

19741975
/* 8.b.ii */
1975-
if (ecma_op_strict_equality_compare (arg1, get_value))
1976+
if (ecma_op_strict_equality_compare (search_element, get_value))
19761977
{
19771978
*num_p = ecma_uint32_to_number (from_idx);
19781979
}

jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.inc.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ ROUTINE (LIT_MAGIC_STRING_SORT, ecma_builtin_array_prototype_object_sort, 1, 1)
7272
ROUTINE (LIT_MAGIC_STRING_SPLICE, ecma_builtin_array_prototype_object_splice, NON_FIXED, 2)
7373
ROUTINE (LIT_MAGIC_STRING_UNSHIFT, ecma_builtin_array_prototype_object_unshift, NON_FIXED, 1)
7474
ROUTINE (LIT_MAGIC_STRING_INDEX_OF_UL, ecma_builtin_array_prototype_object_index_of, 2, 1)
75-
ROUTINE (LIT_MAGIC_STRING_LAST_INDEX_OF_UL, ecma_builtin_array_prototype_object_last_index_of, 2, 1)
75+
ROUTINE (LIT_MAGIC_STRING_LAST_INDEX_OF_UL, ecma_builtin_array_prototype_object_last_index_of, NON_FIXED, 1)
7676
ROUTINE (LIT_MAGIC_STRING_EVERY, ecma_builtin_array_prototype_object_every, 2, 1)
7777
ROUTINE (LIT_MAGIC_STRING_SOME, ecma_builtin_array_prototype_object_some, 2, 1)
7878
ROUTINE (LIT_MAGIC_STRING_FOR_EACH_UL, ecma_builtin_array_prototype_object_for_each, 2, 1)

tests/jerry/array-prototype-lastindexof.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ var arr = [];
4141
arr[4294967294] = "foo";
4242
assert(arr.lastIndexOf("foo", -1) === 4294967294)
4343

44+
var arr = [1,2];
45+
assert(arr.lastIndexOf(2, undefined) === -1);
46+
assert(arr.lastIndexOf(2) === 1);
47+
4448
// Checking behavior when unable to get length
4549
var obj = { lastIndexOf : Array.prototype.lastIndexOf}
4650
Object.defineProperty(obj, 'length', { 'get' : function () { throw new ReferenceError ("foo"); } });

0 commit comments

Comments
 (0)