Skip to content

Commit 36b8fa5

Browse files
committed
Implemented Array.prototype.lastIndexOf().
JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai [email protected]
1 parent c16e065 commit 36b8fa5

File tree

3 files changed

+197
-0
lines changed

3 files changed

+197
-0
lines changed

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

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -529,6 +529,140 @@ ecma_builtin_array_prototype_object_index_of (ecma_value_t this_arg, /**< this a
529529
return ret_value;
530530
} /* ecma_builtin_array_prototype_object_index_of */
531531

532+
/**
533+
* The Array.prototype object's 'lastIndexOf' routine
534+
*
535+
* See also:
536+
* ECMA-262 v5, 15.4.4.15
537+
*
538+
* @return completion value
539+
* Returned value must be freed with ecma_free_completion_value.
540+
*/
541+
static ecma_completion_value_t
542+
ecma_builtin_array_prototype_object_last_index_of (ecma_value_t this_arg, /**< this argument */
543+
ecma_value_t arg1, /**< searchElement */
544+
ecma_value_t arg2) /**< fromIndex */
545+
{
546+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
547+
548+
/* 1. */
549+
ECMA_TRY_CATCH (obj_this,
550+
ecma_op_to_object (this_arg),
551+
ret_value);
552+
553+
ecma_object_t *obj_p = ecma_get_object_from_value (obj_this);
554+
ecma_string_t *magic_string_length_p = ecma_get_magic_string (ECMA_MAGIC_STRING_LENGTH);
555+
556+
/* 2. */
557+
ECMA_TRY_CATCH (len_value,
558+
ecma_op_object_get (obj_p, magic_string_length_p),
559+
ret_value);
560+
561+
ECMA_OP_TO_NUMBER_TRY_CATCH (len_number, len_value, ret_value);
562+
563+
/* 3. */
564+
uint32_t len = ecma_number_to_uint32 (len_number);
565+
566+
ecma_number_t* num_p = ecma_alloc_number ();
567+
*num_p = ecma_int32_to_number (-1);
568+
569+
/* 4. */
570+
if (len == 0)
571+
{
572+
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (num_p));
573+
}
574+
else
575+
{
576+
uint32_t k = len - 1;
577+
578+
/* 5. */
579+
if (!ecma_is_value_undefined (arg2))
580+
{
581+
ECMA_OP_TO_NUMBER_TRY_CATCH (arg_from_idx, arg2, ret_value);
582+
int32_t n = ecma_number_to_int32 (arg_from_idx);
583+
584+
/* 6. */
585+
if (n >= 0)
586+
{
587+
/* min(n, len - 1)*/
588+
if ((uint32_t) n > len - 1)
589+
{
590+
k = len - 1;
591+
}
592+
else
593+
{
594+
k = (uint32_t) n;
595+
}
596+
}
597+
/* 7. */
598+
else
599+
{
600+
n = -n;
601+
602+
/* We prevent k from being negative, so that we can use an uint32 */
603+
if ((uint32_t) n <= len)
604+
{
605+
k = len - (uint32_t) n;
606+
}
607+
else
608+
{
609+
/*
610+
* If k would be negative, we set it to UINT_MAX. See reasoning for this in the comment
611+
* at the for loop below.
612+
*/
613+
k = (uint32_t) -1;
614+
}
615+
}
616+
617+
ECMA_OP_TO_NUMBER_FINALIZE (arg_from_idx);
618+
}
619+
620+
/* 8.
621+
* We should break from the loop when k < 0. We can still use an uint32_t for k, and check
622+
* for an underflow instead. This is safe, because k will always start in [0, len - 1],
623+
* and len is in [0, UINT_MAX], so k >= len means we've had an underflow, and should stop.
624+
*/
625+
for (;k < len && *num_p < 0 && ecma_is_completion_value_empty (ret_value); k--)
626+
{
627+
/* 8.a */
628+
ecma_string_t *idx_str_p = ecma_new_ecma_string_from_uint32 (k);
629+
630+
/* 8.a */
631+
if (ecma_op_object_get_property (obj_p, idx_str_p) != NULL)
632+
{
633+
/* 8.b.i */
634+
ECMA_TRY_CATCH (get_value, ecma_op_object_get (obj_p, idx_str_p), ret_value);
635+
636+
/* 8.b.ii */
637+
if (ecma_op_strict_equality_compare (arg1, get_value))
638+
{
639+
*num_p = ecma_uint32_to_number (k);
640+
}
641+
642+
ECMA_FINALIZE (get_value);
643+
}
644+
645+
ecma_deref_ecma_string (idx_str_p);
646+
}
647+
648+
if (ecma_is_completion_value_empty (ret_value))
649+
{
650+
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (num_p));
651+
}
652+
else
653+
{
654+
ecma_dealloc_number (num_p);
655+
}
656+
}
657+
658+
ECMA_OP_TO_NUMBER_FINALIZE (len_number);
659+
ECMA_FINALIZE (len_value);
660+
ecma_deref_ecma_string (magic_string_length_p);
661+
ECMA_FINALIZE (obj_this);
662+
663+
return ret_value;
664+
} /* ecma_builtin_array_prototype_object_last_index_of */
665+
532666
/**
533667
* The Array.prototype object's 'shift' routine
534668
*

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ ROUTINE (ECMA_MAGIC_STRING_TO_STRING_UL, ecma_builtin_array_prototype_object_to_
6363
ROUTINE (ECMA_MAGIC_STRING_POP, ecma_builtin_array_prototype_object_pop, 0, 0)
6464
ROUTINE (ECMA_MAGIC_STRING_PUSH, ecma_builtin_array_prototype_object_push, NON_FIXED, 1)
6565
ROUTINE (ECMA_MAGIC_STRING_INDEX_OF_UL, ecma_builtin_array_prototype_object_index_of, 2, 1)
66+
ROUTINE (ECMA_MAGIC_STRING_LAST_INDEX_OF_UL, ecma_builtin_array_prototype_object_last_index_of, 2, 1)
6667
ROUTINE (ECMA_MAGIC_STRING_SHIFT, ecma_builtin_array_prototype_object_shift, 0, 0)
6768
ROUTINE (ECMA_MAGIC_STRING_UNSHIFT, ecma_builtin_array_prototype_object_unshift, NON_FIXED, 1)
6869
ROUTINE (ECMA_MAGIC_STRING_SLICE, ecma_builtin_array_prototype_object_slice, 2, 2)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2015 Samsung Electronics Co., Ltd.
2+
// Copyright 2015 University of Szeged.
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
var obj = {};
17+
var array = ["foo", 19, "bar", obj, "foo", 29, "baz"];
18+
19+
var index = array.lastIndexOf("foo");
20+
assert(index === 4);
21+
assert(array[index] === "foo");
22+
23+
assert(array.lastIndexOf("foo", 3) === 0);
24+
assert(array.lastIndexOf("foo", -8) === -1);
25+
26+
var index = array.lastIndexOf("baz");
27+
assert(index === 6);
28+
assert(array[index] === "baz");
29+
30+
assert(array.lastIndexOf("baz", -2) === -1);
31+
32+
var index = array.lastIndexOf(obj);
33+
assert(index === 3);
34+
assert(array[index] === obj);
35+
36+
var arr = [];
37+
arr[4294967294] = "foo";
38+
assert(arr.lastIndexOf("foo", -1) === 4294967294)
39+
40+
// Checking behavior when unable to get length
41+
var obj = { lastIndexOf : Array.prototype.lastIndexOf}
42+
Object.defineProperty(obj, 'length', { 'get' : function () { throw new ReferenceError ("foo"); } });
43+
44+
try {
45+
obj.lastIndexOf("bar");
46+
assert(false);
47+
} catch (e) {
48+
assert(e.message === "foo");
49+
assert(e instanceof ReferenceError);
50+
}
51+
52+
// Checking behavior when unable to get element
53+
var obj = { lastIndexOf : Array.prototype.lastIndexOf, length : 1}
54+
Object.defineProperty(obj, '0', { 'get' : function () { throw new ReferenceError ("foo"); } });
55+
56+
try {
57+
obj.lastIndexOf("bar");
58+
assert(false);
59+
} catch (e) {
60+
assert(e.message === "foo");
61+
assert(e instanceof ReferenceError);
62+
}

0 commit comments

Comments
 (0)