From 9fc19589031c8f4c214865f45f35c291f1f5375e Mon Sep 17 00:00:00 2001 From: Kristof Kosztyo Date: Wed, 8 Jul 2015 11:12:08 +0200 Subject: [PATCH] Fix the builtin Math.min and Math.max function JerryScript-DCO-1.0-Signed-off-by: Kristof Kosztyo kkosztyo.u-szeged@partner.samsung.com --- .../builtin-objects/ecma-builtin-math.cpp | 56 ++++++++----------- tests/jerry/math-max.js | 6 +- tests/jerry/math-min.js | 6 +- 3 files changed, 26 insertions(+), 42 deletions(-) diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-math.cpp b/jerry-core/ecma/builtin-objects/ecma-builtin-math.cpp index 061cc1e60..7dbfa6c09 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-math.cpp +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-math.cpp @@ -332,20 +332,20 @@ ecma_builtin_math_object_max (ecma_value_t this_arg __attr_unused___, /**< 'this ecma_number_t ret_num = ecma_number_make_infinity (true); - bool is_just_convert = false; + bool is_NaN = false; for (ecma_length_t arg_index = 0; - arg_index < args_number; + arg_index < args_number && ecma_is_completion_value_empty (ret_value); arg_index++) { ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, args[arg_index], ret_value); - if (!is_just_convert) + if (!is_NaN) { if (unlikely (ecma_number_is_nan (arg_num))) { ret_num = arg_num; - is_just_convert = true; + is_NaN = true; } else if (ecma_number_is_zero (arg_num) /* both numbers are zeroes */ && ecma_number_is_zero (ret_num)) @@ -360,7 +360,6 @@ ecma_builtin_math_object_max (ecma_value_t this_arg __attr_unused___, /**< 'this if (!ecma_number_is_negative (arg_num)) { ret_num = arg_num; - is_just_convert = true; } } else if (ecma_number_is_infinity (ret_num)) /* ret_num is negative infinity */ @@ -384,21 +383,16 @@ ecma_builtin_math_object_max (ecma_value_t this_arg __attr_unused___, /**< 'this } ECMA_OP_TO_NUMBER_FINALIZE (arg_num); - - if (ecma_is_completion_value_throw (ret_value)) - { - return ret_value; - } - - JERRY_ASSERT (ecma_is_completion_value_empty (ret_value)); } - JERRY_ASSERT (ecma_is_completion_value_empty (ret_value)); + if (ecma_is_completion_value_empty (ret_value)) + { + ecma_number_t *num_p = ecma_alloc_number (); + *num_p = ret_num; + ret_value = ecma_make_normal_completion_value (ecma_make_number_value (num_p)); + } - ecma_number_t *num_p = ecma_alloc_number (); - *num_p = ret_num; - - return ecma_make_normal_completion_value (ecma_make_number_value (num_p)); + return ret_value; } /* ecma_builtin_math_object_max */ /** @@ -419,20 +413,20 @@ ecma_builtin_math_object_min (ecma_value_t this_arg __attr_unused___, /**< 'this ecma_number_t ret_num = ecma_number_make_infinity (false); - bool is_just_convert = false; + bool is_NaN = false; for (ecma_length_t arg_index = 0; - arg_index < args_number; + arg_index < args_number && ecma_is_completion_value_empty (ret_value); arg_index++) { ECMA_OP_TO_NUMBER_TRY_CATCH (arg_num, args[arg_index], ret_value); - if (!is_just_convert) + if (!is_NaN) { if (unlikely (ecma_number_is_nan (arg_num))) { ret_num = arg_num; - is_just_convert = true; + is_NaN = true; } else if (ecma_number_is_zero (arg_num) /* both numbers are zeroes */ && ecma_number_is_zero (ret_num)) @@ -447,7 +441,6 @@ ecma_builtin_math_object_min (ecma_value_t this_arg __attr_unused___, /**< 'this if (ecma_number_is_negative (arg_num)) { ret_num = arg_num; - is_just_convert = true; } } else if (ecma_number_is_infinity (ret_num)) /* ret_num is positive infinity */ @@ -471,21 +464,16 @@ ecma_builtin_math_object_min (ecma_value_t this_arg __attr_unused___, /**< 'this } ECMA_OP_TO_NUMBER_FINALIZE (arg_num); - - if (ecma_is_completion_value_throw (ret_value)) - { - return ret_value; - } - - JERRY_ASSERT (ecma_is_completion_value_empty (ret_value)); } - JERRY_ASSERT (ecma_is_completion_value_empty (ret_value)); + if (ecma_is_completion_value_empty (ret_value)) + { + ecma_number_t *num_p = ecma_alloc_number (); + *num_p = ret_num; + ret_value = ecma_make_normal_completion_value (ecma_make_number_value (num_p)); + } - ecma_number_t *num_p = ecma_alloc_number (); - *num_p = ret_num; - - return ecma_make_normal_completion_value (ecma_make_number_value (num_p)); + return ret_value; } /* ecma_builtin_math_object_min */ /** diff --git a/tests/jerry/math-max.js b/tests/jerry/math-max.js index 243f60cf7..8cf8df0ff 100644 --- a/tests/jerry/math-max.js +++ b/tests/jerry/math-max.js @@ -14,6 +14,8 @@ assert(isNaN (Math['max'] (1.0, NaN))); assert(isNaN (Math['max'] (NaN, 1.0))); +assert(isNaN (Math['max'] (Infinity, NaN))); +assert(isNaN (Math['max'] (NaN, Infinity))); assert(Math['max'] (1.0, 3.0, 0.0) === 3.0); assert(Math['max'] (1.0, 3.0, Infinity) === Infinity); assert(Math['max'] (1.0, 3.0, -Infinity) === 3.0); @@ -23,9 +25,5 @@ assert(Math['max'] (Infinity, Infinity) === Infinity); assert(Math['max'] (-Infinity, -Infinity) === -Infinity); assert(Math['max'] () === -Infinity); -/* - FIXME: Uncomment when unary minus support appears. - assert(Math['max'] (0.0, -0.0) === 0.0); assert(Math['max'] (-0.0, 0.0) === 0.0); -*/ diff --git a/tests/jerry/math-min.js b/tests/jerry/math-min.js index f81d98f46..78cfbd5ab 100644 --- a/tests/jerry/math-min.js +++ b/tests/jerry/math-min.js @@ -14,6 +14,8 @@ assert(isNaN (Math['min'] (1.0, NaN))); assert(isNaN (Math['min'] (NaN, 1.0))); +assert(isNaN (Math['min'] (-Infinity, NaN))); +assert(isNaN (Math['min'] (NaN, -Infinity))); assert(Math['min'] (1.0, 3.0, 0.0) === 0.0); assert(Math['min'] (1.0, 3.0, Infinity) === 1.0); assert(Math['min'] (1.0, 3.0, -Infinity) === -Infinity); @@ -23,9 +25,5 @@ assert(Math['min'] (Infinity, Infinity) === Infinity); assert(Math['min'] (-Infinity, -Infinity) === -Infinity); assert(Math['min'] () === Infinity); -/* - FIXME: Uncomment when unary minus support appears. - assert(Math['min'] (0.0, -0.0) === -0.0); assert(Math['min'] (-0.0, 0.0) === -0.0); -*/