Skip to content

Commit 1f8772d

Browse files
committed
Implemented Array.prototype.shift().
JerryScript-DCO-1.0-Signed-off-by: Dániel Bátyai dbatyai.u-szeged@partner.samsung.com
1 parent ecbc285 commit 1f8772d

File tree

3 files changed

+194
-0
lines changed

3 files changed

+194
-0
lines changed

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

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,119 @@ ecma_builtin_array_prototype_object_index_of (ecma_value_t this_arg, /**< this a
397397
return ret_value;
398398
} /* ecma_builtin_array_prototype_object_index_of */
399399

400+
/**
401+
* The Array.prototype object's 'shift' routine
402+
*
403+
* See also:
404+
* ECMA-262 v5, 15.4.4.9
405+
*
406+
* @return completion value
407+
* Returned value must be freed with ecma_free_completion_value.
408+
*/
409+
static ecma_completion_value_t
410+
ecma_builtin_array_prototype_object_shift (ecma_value_t this_arg) /**< this argument */
411+
{
412+
ecma_completion_value_t ret_value = ecma_make_empty_completion_value ();
413+
414+
/* 1. */
415+
ECMA_TRY_CATCH (obj_this,
416+
ecma_op_to_object (this_arg),
417+
ret_value);
418+
419+
ecma_object_t *obj_p = ecma_get_object_from_value (obj_this);
420+
ecma_string_t *magic_string_length_p = ecma_get_magic_string (ECMA_MAGIC_STRING_LENGTH);
421+
422+
/* 2. */
423+
ECMA_TRY_CATCH (len_value,
424+
ecma_op_object_get (obj_p, magic_string_length_p),
425+
ret_value);
426+
427+
ECMA_OP_TO_NUMBER_TRY_CATCH (len_number, len_value, ret_value);
428+
429+
/* 3. */
430+
uint32_t len = ecma_number_to_uint32 (len_number);
431+
432+
/* 4. */
433+
if (len == 0)
434+
{
435+
ECMA_TRY_CATCH (set_length_value,
436+
ecma_builtin_array_prototype_helper_set_length (obj_p, 0),
437+
ret_value);
438+
439+
ret_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_UNDEFINED);
440+
441+
ECMA_FINALIZE (set_length_value);
442+
}
443+
else
444+
{
445+
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (0);
446+
447+
/* 5. */
448+
ECMA_TRY_CATCH (first_value, ecma_op_object_get (obj_p, index_str_p), ret_value);
449+
450+
/* 6. and 7. */
451+
for (uint32_t k = 1; k < len && ecma_is_completion_value_empty (ret_value); k++)
452+
{
453+
/* 7.a */
454+
ecma_string_t *from_str_p = ecma_new_ecma_string_from_uint32 (k);
455+
/* 7.b */
456+
ecma_string_t *to_str_p = ecma_new_ecma_string_from_uint32 (k - 1);
457+
458+
/* 7.c */
459+
if (ecma_op_object_get_property (obj_p, from_str_p) != NULL)
460+
{
461+
/* 7.d.i */
462+
ECMA_TRY_CATCH (curr_value, ecma_op_object_get (obj_p, from_str_p), ret_value);
463+
464+
/* 7.d.ii*/
465+
ECMA_TRY_CATCH (put_value, ecma_op_object_put (obj_p, to_str_p, curr_value, true), ret_value);
466+
467+
ECMA_FINALIZE (put_value);
468+
ECMA_FINALIZE (curr_value);
469+
}
470+
else
471+
{
472+
/* 7.e.i */
473+
ECMA_TRY_CATCH (del_value, ecma_op_object_delete (obj_p, to_str_p, true), ret_value);
474+
ECMA_FINALIZE (del_value);
475+
}
476+
477+
ecma_deref_ecma_string (to_str_p);
478+
ecma_deref_ecma_string (from_str_p);
479+
}
480+
481+
if (ecma_is_completion_value_empty (ret_value))
482+
{
483+
len--;
484+
ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (len);
485+
486+
/* 8. */
487+
ECMA_TRY_CATCH (del_value, ecma_op_object_delete (obj_p, index_str_p, true), ret_value);
488+
489+
/* 9. */
490+
ECMA_TRY_CATCH (set_length_value,
491+
ecma_builtin_array_prototype_helper_set_length (obj_p, len),
492+
ret_value);
493+
/* 10. */
494+
ret_value = ecma_make_normal_completion_value (ecma_copy_value (first_value, true));
495+
496+
ECMA_FINALIZE (set_length_value);
497+
ECMA_FINALIZE (del_value);
498+
ecma_deref_ecma_string (index_str_p);
499+
}
500+
501+
ECMA_FINALIZE (first_value);
502+
ecma_deref_ecma_string (index_str_p);
503+
}
504+
505+
ECMA_OP_TO_NUMBER_FINALIZE (len_number);
506+
ECMA_FINALIZE (len_value);
507+
ecma_deref_ecma_string (magic_string_length_p);
508+
ECMA_FINALIZE (obj_this);
509+
510+
return ret_value;
511+
} /* ecma_builtin_array_prototype_object_shift */
512+
400513
/**
401514
* @}
402515
* @}

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
@@ -62,6 +62,7 @@ ROUTINE (ECMA_MAGIC_STRING_TO_STRING_UL, ecma_builtin_array_prototype_object_to_
6262
ROUTINE (ECMA_MAGIC_STRING_POP, ecma_builtin_array_prototype_object_pop, 0, 0)
6363
ROUTINE (ECMA_MAGIC_STRING_PUSH, ecma_builtin_array_prototype_object_push, NON_FIXED, 1)
6464
ROUTINE (ECMA_MAGIC_STRING_INDEX_OF_UL, ecma_builtin_array_prototype_object_index_of, 2, 1)
65+
ROUTINE (ECMA_MAGIC_STRING_SHIFT, ecma_builtin_array_prototype_object_shift, 0, 0)
6566

6667
#undef OBJECT_ID
6768
#undef SIMPLE_VALUE

tests/jerry/array_prototype_shift.js

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 array = ["foo", [], Infinity, 4]
17+
18+
assert(array.length === 4);
19+
20+
assert(array.shift() === "foo");
21+
assert(array.length === 3);
22+
23+
var a = array.shift();
24+
assert(a instanceof Array);
25+
assert(array.length === 2);
26+
27+
assert(array.shift() === Infinity);
28+
assert(array.length === 1);
29+
30+
assert(array.shift() === 4);
31+
assert(array.length === 0);
32+
33+
assert(array.shift() === undefined);
34+
assert(array.length === 0);
35+
36+
var referenceErrorThrower = function () {
37+
throw new ReferenceError ("foo");
38+
}
39+
40+
// Checking behavior when unable to get length
41+
var obj = { shift : Array.prototype.shift };
42+
Object.defineProperty(obj, 'length', { 'get' : referenceErrorThrower });
43+
44+
try {
45+
obj.shift();
46+
assert(false);
47+
} catch (e) {
48+
assert(e.message === "foo");
49+
assert(e instanceof ReferenceError);
50+
}
51+
52+
// Checking behavior when unable to set length
53+
var obj = { shift : Array.prototype.shift };
54+
Object.defineProperty(obj, 'length', { 'set' : referenceErrorThrower });
55+
56+
try {
57+
obj.shift();
58+
assert(false);
59+
} catch (e) {
60+
assert(e.message === "foo");
61+
assert(e instanceof ReferenceError);
62+
}
63+
64+
// Checking behavior when no length property defined
65+
var obj = { shift : Array.prototype.shift };
66+
assert (obj.length === undefined)
67+
assert (obj.shift() === undefined)
68+
assert (obj.length === 0)
69+
70+
// Checking behavior when unable to get element
71+
var obj = { shift : Array.prototype.shift, length : 1 };
72+
Object.defineProperty(obj, '0', { 'get' : referenceErrorThrower });
73+
74+
try {
75+
obj.shift();
76+
assert(false);
77+
} catch (e) {
78+
assert(e.message === "foo");
79+
assert(e instanceof ReferenceError);
80+
}

0 commit comments

Comments
 (0)