From f22f3c50e77f87ef024b49d63996c15ceff51e17 Mon Sep 17 00:00:00 2001 From: Laszlo Vidacs Date: Wed, 27 May 2015 11:05:08 +0200 Subject: [PATCH] Array.prototype.reduce() tests, minor fixes. JerryScript-DCO-1.0-Signed-off-by: Laszlo Vidacs lvidacs.u-szeged@partner.samsung.com --- .../ecma-builtin-array-prototype.cpp | 6 +- tests/jerry/array_prototype_reduce.js | 55 +++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 tests/jerry/array_prototype_reduce.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 0be427f6b..4d45c089e 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.cpp @@ -2560,13 +2560,17 @@ ecma_builtin_array_prototype_object_reduce (ecma_value_t this_arg, /**< this arg current_index = ecma_make_number_value (num_p); ecma_value_t prev_value = ecma_get_completion_value_value (accumulator); ecma_value_t call_args[] = {prev_value, current_value, current_index, obj_this}; + ecma_value_t undefined_value = ecma_make_simple_value (ECMA_SIMPLE_VALUE_UNDEFINED); ECMA_TRY_CATCH (call_value, - ecma_op_function_call (func_object_p, ECMA_SIMPLE_VALUE_UNDEFINED, call_args, 4), + ecma_op_function_call (func_object_p, undefined_value, call_args, 4), ret_value); + + ecma_free_completion_value (accumulator); accumulator = ecma_copy_completion_value (call_value); ECMA_FINALIZE (call_value); + ecma_free_value (undefined_value, false); ECMA_FINALIZE (current_value); } ecma_deref_ecma_string (index_str_p); diff --git a/tests/jerry/array_prototype_reduce.js b/tests/jerry/array_prototype_reduce.js new file mode 100644 index 000000000..0bbdd2f3b --- /dev/null +++ b/tests/jerry/array_prototype_reduce.js @@ -0,0 +1,55 @@ +// 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 func = function(a, b) { + return a + b; +} + +// check function type +try { + [0].reduce(new Object()); + assert(false); +} catch(e) { + assert(e instanceof TypeError); +} + +// check for init value +try { + [].reduce(func); +} +catch(e) { + assert(e instanceof TypeError); +} + +// various checks +assert ([].reduce(func, 1) === 1); + +assert ([0].reduce(func) === 0); + +assert ([0, 1].reduce(func) === 1); + +assert ([0, 1].reduce(func, 1) === 2); + +assert ([0, 1, 2, 3].reduce(func, 1) === 7); + +assert (["A","B"].reduce(func) === "AB"); + +assert (["A","B"].reduce(func, "Init:") === "Init:AB"); + +assert ([0, 1].reduce(func, 3.2) === 4.2); + +assert ([0, "x", 1].reduce(func) === "0x1"); + +assert ([0, "x", 1].reduce(func, 3.2) === "3.2x1");