From a80282a082be2f41e7c01be8d5ce2f4fc3c9c536 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20B=C3=A1tyai?= Date: Thu, 21 May 2015 13:11:24 +0200 Subject: [PATCH] Implemented Array.prototype.filter() 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 | 124 ++++++++++++++++++ .../ecma-builtin-array-prototype.inc.h | 1 + tests/jerry/array_prototype_filter.js | 72 ++++++++++ 3 files changed, 197 insertions(+) create mode 100644 tests/jerry/array_prototype_filter.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 f82d2ce527..5114e02927 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.cpp @@ -1521,6 +1521,130 @@ ecma_builtin_array_prototype_object_some (ecma_value_t this_arg, /**< this argum return ret_value; } /* ecma_builtin_array_prototype_object_some */ +/** + * The Array.prototype object's 'filter' routine + * + * See also: + * ECMA-262 v5, 15.4.4.20 + * + * @return completion value + * Returned value must be freed with ecma_free_completion_value. + */ +static ecma_completion_value_t +ecma_builtin_array_prototype_object_filter (ecma_value_t this_arg, /**< this argument */ + ecma_value_t arg1, /**< callbackfn */ + ecma_value_t arg2) /**< thisArg */ +{ + 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); + + /* 3. */ + ECMA_OP_TO_NUMBER_TRY_CATCH (len_number, len_value, ret_value); + + uint32_t len = ecma_number_to_uint32 (len_number); + + /* 4. */ + if (!ecma_op_is_callable (arg1)) + { + ret_value = ecma_make_throw_obj_completion_value (ecma_new_standard_error (ECMA_ERROR_TYPE)); + } + else + { + ecma_value_t current_index; + ecma_number_t *num_p = ecma_alloc_number (); + ecma_object_t *func_object_p; + + /* 6. */ + ecma_completion_value_t new_array = ecma_op_create_array_object (NULL, 0, false); + JERRY_ASSERT (ecma_is_completion_value_normal (new_array)); + ecma_object_t *new_array_p = ecma_get_object_from_completion_value (new_array); + + /* We already checked that arg1 is callable, so it will always be an object. */ + JERRY_ASSERT (ecma_is_value_object (arg1)); + func_object_p = ecma_get_object_from_value (arg1); + + /* 8. */ + uint32_t new_array_index = 0; + + /* 9. */ + for (uint32_t index = 0; index < len && ecma_is_completion_value_empty (ret_value); index++) + { + /* 9.a */ + ecma_string_t *index_str_p = ecma_new_ecma_string_from_uint32 (index); + + /* 9.c */ + if (ecma_op_object_get_property (obj_p, index_str_p) != NULL) + { + /* 9.c.i */ + ECMA_TRY_CATCH (get_value, ecma_op_object_get (obj_p, index_str_p), ret_value); + + *num_p = ecma_uint32_to_number (index); + current_index = ecma_make_number_value (num_p); + + ecma_value_t call_args[] = { get_value, current_index, obj_this }; + /* 9.c.ii */ + ECMA_TRY_CATCH (call_value, ecma_op_function_call (func_object_p, arg2, call_args, 3), ret_value); + + /* 9.c.iii, ecma_op_to_boolean always returns a simple value, so no need to free. */ + if (ecma_is_value_true (ecma_op_to_boolean (call_value))) + { + ecma_string_t* to_index_string_p = ecma_new_ecma_string_from_uint32 (new_array_index); + /* + * By definition we should use [[DefineOwnProperty]] here, but since [[Put]] will create the + * same property that we need, we can use it for simplicity. No need for a try-catch block + * since it is called with is_throw = false. + */ + ecma_completion_value_t put_comp_value = ecma_op_object_put (new_array_p, + to_index_string_p, + get_value, + false); + JERRY_ASSERT (ecma_is_completion_value_normal_true (put_comp_value)); + ecma_free_completion_value (put_comp_value); + + ecma_deref_ecma_string (to_index_string_p); + new_array_index++; + } + + ECMA_FINALIZE (call_value); + ECMA_FINALIZE (get_value); + } + + ecma_deref_ecma_string (index_str_p); + } + + ecma_dealloc_number (num_p); + + if (ecma_is_completion_value_empty (ret_value)) + { + /* 10. */ + ret_value = new_array; + } + else + { + ecma_free_completion_value (new_array); + } + } + + 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_filter */ + /** * The Array.prototype object's 'slice' 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 7700f01881..7a65a8f988 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 @@ -74,6 +74,7 @@ ROUTINE (ECMA_MAGIC_STRING_EVERY, ecma_builtin_array_prototype_object_every, 2, ROUTINE (ECMA_MAGIC_STRING_SOME, ecma_builtin_array_prototype_object_some, 2, 1) ROUTINE (ECMA_MAGIC_STRING_SLICE, ecma_builtin_array_prototype_object_slice, 2, 2) ROUTINE (ECMA_MAGIC_STRING_SPLICE, ecma_builtin_array_prototype_object_splice, NON_FIXED, 2) +ROUTINE (ECMA_MAGIC_STRING_FILTER, ecma_builtin_array_prototype_object_filter, 2, 1) #undef OBJECT_ID #undef SIMPLE_VALUE diff --git a/tests/jerry/array_prototype_filter.js b/tests/jerry/array_prototype_filter.js new file mode 100644 index 0000000000..ce4f28268e --- /dev/null +++ b/tests/jerry/array_prototype_filter.js @@ -0,0 +1,72 @@ +// 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] + +function f(arg1, arg2, arg3) { + assert(arg1 === array[arg2]); + assert(arg3 === array); + return true; +} + +var filtered = array.filter(f); +assert(filtered.length === array.length); +for (i = 0; i < filtered.length; i++) { + assert(filtered[i] === array[i]); +} + +var array = [1, 2, 3, 4, 5, 6, 7, 8]; + +function g (arg1, arg2, arg3) { + if (arg2 % 2 === 0) { + return true; + } else { + return false; + } +} + +filtered = array.filter(g) +assert(filtered.length === 4); +assert(filtered[0] === 1); +assert(filtered[1] === 3); +assert(filtered[2] === 5); +assert(filtered[3] === 7); + +// Checking behavior when unable to get length +var obj = {}; +Object.defineProperty(obj, 'length', { 'get' : function () {throw new ReferenceError ("foo"); } }); +obj.filter = Array.prototype.filter; + +try { + obj.filter(f); + assert(false); +} catch (e) { + assert(e.message === "foo"); + assert(e instanceof ReferenceError); +} + +// Checking behavior when unable to get element +var obj = {} +obj.length = 1; +Object.defineProperty(obj, '0', { 'get' : function () {throw new ReferenceError ("foo"); } }); +obj.filter = Array.prototype.filter + +try { + obj.filter(f); + assert(false); +} catch (e) { + assert(e.message === "foo"); + assert(e instanceof ReferenceError); +}