From 5bb56643aae84811b1de00e0eefe43a10032a1bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20B=C3=A1tyai?= Date: Thu, 13 Aug 2015 14:36:20 +0200 Subject: [PATCH] Fix undefined fromIndex in Array.prototype.lastIndexOf() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai.u-szeged@partner.samsung.com --- .../builtin-objects/ecma-builtin-array-prototype.cpp | 11 ++++++----- .../ecma-builtin-array-prototype.inc.h | 2 +- tests/jerry/array-prototype-lastindexof.js | 4 ++++ 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.cpp index 381eb0d40c..d32c75d618 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.cpp @@ -1862,10 +1862,11 @@ ecma_builtin_array_prototype_object_index_of (ecma_value_t this_arg, /**< this a */ static ecma_completion_value_t ecma_builtin_array_prototype_object_last_index_of (ecma_value_t this_arg, /**< this argument */ - ecma_value_t arg1, /**< searchElement */ - ecma_value_t arg2) /**< fromIndex */ + const ecma_value_t args[], /**< arguments list */ + ecma_length_t args_number) /**< number of arguments */ { ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); + ecma_value_t search_element = (args_number > 0) ? args[0] : ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED); /* 1. */ ECMA_TRY_CATCH (obj_this, @@ -1898,9 +1899,9 @@ ecma_builtin_array_prototype_object_last_index_of (ecma_value_t this_arg, /**< t uint32_t from_idx = len - 1; /* 5. */ - if (!ecma_is_value_undefined (arg2)) + if (args_number > 1) { - ECMA_OP_TO_NUMBER_TRY_CATCH (arg_from_idx, arg2, ret_value); + ECMA_OP_TO_NUMBER_TRY_CATCH (arg_from_idx, args[1], ret_value); if (!ecma_number_is_nan (arg_from_idx)) { @@ -1972,7 +1973,7 @@ ecma_builtin_array_prototype_object_last_index_of (ecma_value_t this_arg, /**< t ECMA_TRY_CATCH (get_value, ecma_op_object_get (obj_p, idx_str_p), ret_value); /* 8.b.ii */ - if (ecma_op_strict_equality_compare (arg1, get_value)) + if (ecma_op_strict_equality_compare (search_element, get_value)) { *num_p = ecma_uint32_to_number (from_idx); } diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.inc.h b/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.inc.h index b0305ba410..350c324061 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.inc.h +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.inc.h @@ -72,7 +72,7 @@ ROUTINE (LIT_MAGIC_STRING_SORT, ecma_builtin_array_prototype_object_sort, 1, 1) ROUTINE (LIT_MAGIC_STRING_SPLICE, ecma_builtin_array_prototype_object_splice, NON_FIXED, 2) ROUTINE (LIT_MAGIC_STRING_UNSHIFT, ecma_builtin_array_prototype_object_unshift, NON_FIXED, 1) ROUTINE (LIT_MAGIC_STRING_INDEX_OF_UL, ecma_builtin_array_prototype_object_index_of, 2, 1) -ROUTINE (LIT_MAGIC_STRING_LAST_INDEX_OF_UL, ecma_builtin_array_prototype_object_last_index_of, 2, 1) +ROUTINE (LIT_MAGIC_STRING_LAST_INDEX_OF_UL, ecma_builtin_array_prototype_object_last_index_of, NON_FIXED, 1) ROUTINE (LIT_MAGIC_STRING_EVERY, ecma_builtin_array_prototype_object_every, 2, 1) ROUTINE (LIT_MAGIC_STRING_SOME, ecma_builtin_array_prototype_object_some, 2, 1) ROUTINE (LIT_MAGIC_STRING_FOR_EACH_UL, ecma_builtin_array_prototype_object_for_each, 2, 1) diff --git a/tests/jerry/array-prototype-lastindexof.js b/tests/jerry/array-prototype-lastindexof.js index ba71eab1b9..967d883d51 100644 --- a/tests/jerry/array-prototype-lastindexof.js +++ b/tests/jerry/array-prototype-lastindexof.js @@ -41,6 +41,10 @@ var arr = []; arr[4294967294] = "foo"; assert(arr.lastIndexOf("foo", -1) === 4294967294) +var arr = [1,2]; +assert(arr.lastIndexOf(2, undefined) === -1); +assert(arr.lastIndexOf(2) === 1); + // Checking behavior when unable to get length var obj = { lastIndexOf : Array.prototype.lastIndexOf} Object.defineProperty(obj, 'length', { 'get' : function () { throw new ReferenceError ("foo"); } });