From 6229e90c856eaa4d68ab99f614703e8f773f63b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20B=C3=A1tyai?= Date: Mon, 18 May 2015 11:27:48 +0200 Subject: [PATCH] Implemented 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 --- .../ecma-builtin-array-prototype.cpp | 134 ++++++++++++++++++ .../ecma-builtin-array-prototype.inc.h | 1 + tests/jerry/array_prototype_lastindexof.js | 62 ++++++++ 3 files changed, 197 insertions(+) create mode 100644 tests/jerry/array_prototype_lastindexof.js 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 0943bd2f3a..897233e144 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.cpp @@ -492,6 +492,140 @@ ecma_builtin_array_prototype_object_index_of (ecma_value_t this_arg, /**< this a return ret_value; } /* ecma_builtin_array_prototype_object_index_of */ +/** + * The Array.prototype object's 'lastIndexOf' routine + * + * See also: + * ECMA-262 v5, 15.4.4.15 + * + * @return completion value + * Returned value must be freed with ecma_free_completion_value. + */ +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 */ +{ + ecma_completion_value_t ret_value = ecma_make_empty_completion_value (); + + /* 1. */ + ECMA_TRY_CATCH (obj_this, + ecma_op_to_object (this_arg), + ret_value); + + ecma_object_t *obj_p = ecma_get_object_from_value (obj_this); + ecma_string_t *magic_string_length_p = ecma_get_magic_string (ECMA_MAGIC_STRING_LENGTH); + + /* 2. */ + ECMA_TRY_CATCH (len_value, + ecma_op_object_get (obj_p, magic_string_length_p), + ret_value); + + ECMA_OP_TO_NUMBER_TRY_CATCH (len_number, len_value, ret_value); + + /* 3. */ + uint32_t len = ecma_number_to_uint32 (len_number); + + ecma_number_t* num_p = ecma_alloc_number (); + *num_p = ecma_int32_to_number (-1); + + /* 4. */ + if (len == 0) + { + ret_value = ecma_make_normal_completion_value (ecma_make_number_value (num_p)); + } + else + { + uint32_t k = len - 1; + + /* 5. */ + if (!ecma_is_value_undefined (arg2)) + { + ECMA_OP_TO_NUMBER_TRY_CATCH (arg_from_idx, arg2, ret_value); + int32_t n = ecma_number_to_int32 (arg_from_idx); + + /* 6. */ + if (n >= 0) + { + /* min(n, len - 1)*/ + if ((uint32_t) n > len - 1) + { + k = len - 1; + } + else + { + k = (uint32_t) n; + } + } + /* 7. */ + else + { + n = -n; + + /* We prevent k from being negative, so that we can use an uint32 */ + if ((uint32_t) n <= len) + { + k = len - (uint32_t) n; + } + else + { + /* + * If k would be negative, we set it to UINT_MAX. See reasoning for this in the comment + * at the for loop below. + */ + k = (uint32_t) -1; + } + } + + ECMA_OP_TO_NUMBER_FINALIZE (arg_from_idx); + } + + /* 8. + * We should break from the loop when k < 0. We can still use an uint32_t for k, and check + * for an underflow instead. This is safe, because k will always start in [0, len - 1], + * and len is in [0, UINT_MAX], so k >= len means we've had an underflow, and should stop. + */ + for (;k < len && *num_p < 0 && ecma_is_completion_value_empty (ret_value); k--) + { + /* 8.a */ + ecma_string_t *idx_str_p = ecma_new_ecma_string_from_uint32 (k); + + /* 8.a */ + if (ecma_op_object_get_property (obj_p, idx_str_p) != NULL) + { + /* 8.b.i */ + 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)) + { + *num_p = ecma_uint32_to_number (k); + } + + ECMA_FINALIZE (get_value); + } + + ecma_deref_ecma_string (idx_str_p); + } + + if (ecma_is_completion_value_empty (ret_value)) + { + ret_value = ecma_make_normal_completion_value (ecma_make_number_value (num_p)); + } + else + { + ecma_dealloc_number (num_p); + } + } + + ECMA_OP_TO_NUMBER_FINALIZE (len_number); + ECMA_FINALIZE (len_value); + ecma_deref_ecma_string (magic_string_length_p); + ECMA_FINALIZE (obj_this); + + return ret_value; +} /* ecma_builtin_array_prototype_object_last_index_of */ + /** * The Array.prototype object's 'shift' routine * 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 2cb4a4371b..6ecccb3625 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 @@ -63,6 +63,7 @@ ROUTINE (ECMA_MAGIC_STRING_TO_STRING_UL, ecma_builtin_array_prototype_object_to_ ROUTINE (ECMA_MAGIC_STRING_POP, ecma_builtin_array_prototype_object_pop, 0, 0) ROUTINE (ECMA_MAGIC_STRING_PUSH, ecma_builtin_array_prototype_object_push, NON_FIXED, 1) ROUTINE (ECMA_MAGIC_STRING_INDEX_OF_UL, ecma_builtin_array_prototype_object_index_of, 2, 1) +ROUTINE (ECMA_MAGIC_STRING_LAST_INDEX_OF_UL, ecma_builtin_array_prototype_object_last_index_of, 2, 1) ROUTINE (ECMA_MAGIC_STRING_SHIFT, ecma_builtin_array_prototype_object_shift, 0, 0) ROUTINE (ECMA_MAGIC_STRING_UNSHIFT, ecma_builtin_array_prototype_object_unshift, NON_FIXED, 1) diff --git a/tests/jerry/array_prototype_lastindexof.js b/tests/jerry/array_prototype_lastindexof.js new file mode 100644 index 0000000000..32ca31460c --- /dev/null +++ b/tests/jerry/array_prototype_lastindexof.js @@ -0,0 +1,62 @@ +// Copyright 2015 Samsung Electronics Co., Ltd. +// Copyright 2015 University of Szeged. +// +// 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. + +var obj = {}; +var array = ["foo", 19, "bar", obj, "foo", 29, "baz"]; + +var index = array.lastIndexOf("foo"); +assert(index === 4); +assert(array[index] === "foo"); + +assert(array.lastIndexOf("foo", 3) === 0); +assert(array.lastIndexOf("foo", -8) === -1); + +var index = array.lastIndexOf("baz"); +assert(index === 6); +assert(array[index] === "baz"); + +assert(array.lastIndexOf("baz", -2) === -1); + +var index = array.lastIndexOf(obj); +assert(index === 3); +assert(array[index] === obj); + +var arr = []; +arr[4294967294] = "foo"; +assert(arr.lastIndexOf("foo", -1) === 4294967294) + +// Checking behavior when unable to get length +var obj = { lastIndexOf : Array.prototype.lastIndexOf} +Object.defineProperty(obj, 'length', { 'get' : function () { throw new ReferenceError ("foo"); } }); + +try { + obj.lastIndexOf("bar"); + assert(false); +} catch (e) { + assert(e.message === "foo"); + assert(e instanceof ReferenceError); +} + +// Checking behavior when unable to get element +var obj = { lastIndexOf : Array.prototype.lastIndexOf, length : 1} +Object.defineProperty(obj, '0', { 'get' : function () { throw new ReferenceError ("foo"); } }); + +try { + obj.lastIndexOf("bar"); + assert(false); +} catch (e) { + assert(e.message === "foo"); + assert(e instanceof ReferenceError); +}