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 0d2b436101..add4f8a52d 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.cpp @@ -44,6 +44,33 @@ * @{ */ +/** + * Helper function to set an object's length property + * + * @return completion value (return value of the [[Put]] method) + * Returned value must be freed with ecma_free_completion_value. + */ +static ecma_completion_value_t +ecma_builtin_array_prototype_helper_set_length (ecma_object_t *object, /**< object*/ + uint32_t length) /**< new length */ +{ + ecma_completion_value_t ret_value; + ecma_string_t *magic_string_length_p = ecma_get_magic_string (ECMA_MAGIC_STRING_LENGTH); + + ecma_number_t *len_p = ecma_alloc_number (); + *len_p = ecma_uint32_to_number (length); + + ret_value = ecma_op_object_put (object, + magic_string_length_p, + ecma_make_number_value (len_p), + true), + + ecma_dealloc_number (len_p); + ecma_deref_ecma_string (magic_string_length_p); + + return ret_value; +} /* ecma_builtin_array_prototype_helper_set_length */ + /** * The Array.prototype object's 'toString' routine * @@ -59,6 +86,85 @@ ecma_builtin_array_prototype_object_to_string (ecma_value_t this_arg) /**< this ECMA_BUILTIN_CP_UNIMPLEMENTED (this_arg); } /* ecma_builtin_array_prototype_object_to_string */ +/** + * The Array.prototype object's 'pop' routine + * + * See also: + * ECMA-262 v5, 15.4.4.6 + * + * @return completion value + * Returned value must be freed with ecma_free_completion_value. + */ +static ecma_completion_value_t +ecma_builtin_array_prototype_object_pop (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) + { + /* 4.a */ + ECMA_TRY_CATCH (set_length_value, + ecma_builtin_array_prototype_helper_set_length (obj_p, 0), + ret_value); + + /* 4.b */ + ret_value = ecma_make_simple_completion_value (ECMA_SIMPLE_VALUE_UNDEFINED); + + ECMA_FINALIZE (set_length_value) + } + else + { + len--; + /* 5.a */ + ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (len); + + /* 5.b */ + ECMA_TRY_CATCH (get_value, ecma_op_object_get (obj_p, index_str_p), ret_value); + + /* 5.c */ + ECMA_TRY_CATCH (del_value, ecma_op_object_delete (obj_p, index_str_p, true), ret_value); + + /* 5.d */ + ECMA_TRY_CATCH (set_length_value, + ecma_builtin_array_prototype_helper_set_length (obj_p, len), + ret_value); + + ret_value = ecma_make_normal_completion_value (ecma_copy_value (get_value, true)); + + ECMA_FINALIZE (set_length_value); + ECMA_FINALIZE (del_value); + ECMA_FINALIZE (get_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_pop */ + /** * The Array.prototype object's 'push' 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 4e09fd7b71..dd7609f7ad 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 @@ -59,6 +59,7 @@ NUMBER_VALUE (ECMA_MAGIC_STRING_LENGTH, /* Routine properties: * (property name, C routine name, arguments number or NON_FIXED, value of the routine's length property) */ 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) #undef OBJECT_ID diff --git a/tests/jerry/array_prototype_pop.js b/tests/jerry/array_prototype_pop.js new file mode 100644 index 0000000000..b1435d5df9 --- /dev/null +++ b/tests/jerry/array_prototype_pop.js @@ -0,0 +1,75 @@ +// 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.pop() === 4) +assert(array.length === 3); + +assert(array.pop() === Infinity); +assert(array.length === 2); + +var a = array.pop() +assert(a instanceof Array); +assert(array.length === 1); + +assert(array.pop() === "foo"); +assert(array.length === 0); + +assert(array.pop() === undefined); +assert(array.length === 0); + +// Checking behavior when unable to get length +var obj = { pop : Array.prototype.pop }; +Object.defineProperty(obj, 'length', { 'get' : function () {throw new ReferenceError ("foo"); } }); + +try { + obj.pop(); + assert(false); +} catch (e) { + assert(e.message === "foo"); + assert(e instanceof ReferenceError); +} + +// Checking behavior when unable to set length +var obj = { pop : Array.prototype.pop }; +Object.defineProperty(obj, 'length', { 'set' : function () {throw new ReferenceError ("foo"); } }); + +try { + obj.pop(); + assert(false); +} catch (e) { + assert(e.message === "foo"); + assert(e instanceof ReferenceError); +} + +// Checking behavior when no length property defined +var obj = { pop : Array.prototype.pop }; +assert(obj.length === undefined) +assert(obj.pop() === undefined) +assert(obj.length === 0) + +// Checking behavior when unable to get element +var obj = { pop : Array.prototype.pop, length : 1 }; +Object.defineProperty(obj, '0', { 'get' : function () {throw new ReferenceError ("foo"); } }); + +try { + obj.pop(); + assert(false); +} catch (e) { + assert(e.message === "foo"); + assert(e instanceof ReferenceError); +}