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 28f873aad0..5de96834f3 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.cpp @@ -266,6 +266,119 @@ ecma_builtin_array_prototype_object_push (ecma_value_t this_arg, /**< this argum return ret_value; } /* ecma_builtin_array_prototype_object_push */ +/** + * The Array.prototype object's 'shift' routine + * + * See also: + * ECMA-262 v5, 15.4.4.9 + * + * @return completion value + * Returned value must be freed with ecma_free_completion_value. + */ +static ecma_completion_value_t +ecma_builtin_array_prototype_object_shift (ecma_value_t this_arg) /**< this argument */ +{ + 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); + + /* 4. */ + if (len == 0) + { + ECMA_TRY_CATCH (set_length_value, + ecma_builtin_array_prototype_helper_set_length (obj_p, 0), + ret_value); + + ret_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_UNDEFINED); + + ECMA_FINALIZE (set_length_value); + } + else + { + ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (0); + + /* 5. */ + ECMA_TRY_CATCH (first_value, ecma_op_object_get (obj_p, index_str_p), ret_value); + + /* 6. and 7. */ + for (uint32_t k = 1; k < len && ecma_is_completion_value_empty (ret_value); k++) + { + /* 7.a */ + ecma_string_t *from_str_p = ecma_new_ecma_string_from_uint32 (k); + /* 7.b */ + ecma_string_t *to_str_p = ecma_new_ecma_string_from_uint32 (k - 1); + + /* 7.c */ + if (ecma_op_object_get_property (obj_p, from_str_p) != NULL) + { + /* 7.d.i */ + ECMA_TRY_CATCH (curr_value, ecma_op_object_get (obj_p, from_str_p), ret_value); + + /* 7.d.ii*/ + ECMA_TRY_CATCH (put_value, ecma_op_object_put (obj_p, to_str_p, curr_value, true), ret_value); + + ECMA_FINALIZE (put_value); + ECMA_FINALIZE (curr_value); + } + else + { + /* 7.e.i */ + ECMA_TRY_CATCH (del_value, ecma_op_object_delete (obj_p, to_str_p, true), ret_value); + ECMA_FINALIZE (del_value); + } + + ecma_deref_ecma_string (to_str_p); + ecma_deref_ecma_string (from_str_p); + } + + if (ecma_is_completion_value_empty (ret_value)) + { + len--; + ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (len); + + /* 8. */ + ECMA_TRY_CATCH (del_value, ecma_op_object_delete (obj_p, index_str_p, true), ret_value); + + /* 9. */ + ECMA_TRY_CATCH (set_length_value, + ecma_builtin_array_prototype_helper_set_length (obj_p, len), + ret_value); + /* 10. */ + ret_value = ecma_make_normal_completion_value (ecma_copy_value (first_value, true)); + + ECMA_FINALIZE (set_length_value); + ECMA_FINALIZE (del_value); + ecma_deref_ecma_string (index_str_p); + } + + ECMA_FINALIZE (first_value); + ecma_deref_ecma_string (index_str_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_shift */ + /** * @} * @} 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 dd7609f7ad..46b1e3d35b 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 @@ -61,6 +61,7 @@ NUMBER_VALUE (ECMA_MAGIC_STRING_LENGTH, ROUTINE (ECMA_MAGIC_STRING_TO_STRING_UL, ecma_builtin_array_prototype_object_to_string, 0, 0) 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_SHIFT, ecma_builtin_array_prototype_object_shift, 0, 0) #undef OBJECT_ID #undef SIMPLE_VALUE diff --git a/tests/jerry/array_prototype_shift.js b/tests/jerry/array_prototype_shift.js new file mode 100644 index 0000000000..a347c5d5be --- /dev/null +++ b/tests/jerry/array_prototype_shift.js @@ -0,0 +1,80 @@ +// 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 array = ["foo", [], Infinity, 4] + +assert(array.length === 4); + +assert(array.shift() === "foo"); +assert(array.length === 3); + +var a = array.shift(); +assert(a instanceof Array); +assert(array.length === 2); + +assert(array.shift() === Infinity); +assert(array.length === 1); + +assert(array.shift() === 4); +assert(array.length === 0); + +assert(array.shift() === undefined); +assert(array.length === 0); + +var referenceErrorThrower = function () { + throw new ReferenceError ("foo"); +} + +// Checking behavior when unable to get length +var obj = { shift : Array.prototype.shift }; +Object.defineProperty(obj, 'length', { 'get' : referenceErrorThrower }); + +try { + obj.shift(); + assert(false); +} catch (e) { + assert(e.message === "foo"); + assert(e instanceof ReferenceError); +} + +// Checking behavior when unable to set length +var obj = { shift : Array.prototype.shift }; +Object.defineProperty(obj, 'length', { 'set' : referenceErrorThrower }); + +try { + obj.shift(); + assert(false); +} catch (e) { + assert(e.message === "foo"); + assert(e instanceof ReferenceError); +} + +// Checking behavior when no length property defined +var obj = { shift : Array.prototype.shift }; +assert (obj.length === undefined) +assert (obj.shift() === undefined) +assert (obj.length === 0) + +// Checking behavior when unable to get element +var obj = { shift : Array.prototype.shift, length : 1 }; +Object.defineProperty(obj, '0', { 'get' : referenceErrorThrower }); + +try { + obj.shift(); + assert(false); +} catch (e) { + assert(e.message === "foo"); + assert(e instanceof ReferenceError); +} \ No newline at end of file