From 3c2088cf7b5a31f36370fbf1188b58c67d272c28 Mon Sep 17 00:00:00 2001 From: Virag Orkenyi Date: Thu, 11 Jul 2019 19:42:27 +0200 Subject: [PATCH] Math.sign and Math.trunc (#2943) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit JerryScript-DCO-1.0-Signed-off-by: Örkényi Virág orkvi@inf.u-szeged.hu --- .../ecma/builtin-objects/ecma-builtin-math.c | 71 +++++++++++++++++++ .../builtin-objects/ecma-builtin-math.inc.h | 4 ++ jerry-core/lit/lit-magic-strings.inc.h | 6 ++ jerry-core/lit/lit-magic-strings.ini | 2 + tests/jerry/es2015/math-sign.js | 23 ++++++ tests/jerry/es2015/math-trunc.js | 31 ++++++++ 6 files changed, 137 insertions(+) create mode 100644 tests/jerry/es2015/math-sign.js create mode 100644 tests/jerry/es2015/math-trunc.js diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-math.c b/jerry-core/ecma/builtin-objects/ecma-builtin-math.c index 943fad376..602ffd4cf 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-math.c +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-math.c @@ -55,6 +55,10 @@ enum ECMA_MATH_OBJECT_EXP, /* ECMA-262 v5, 15.8.2.8 */ ECMA_MATH_OBJECT_FLOOR, /* ECMA-262 v5, 15.8.2.9 */ ECMA_MATH_OBJECT_LOG, /* ECMA-262 v5, 15.8.2.10 */ +#if ENABLED (JERRY_ES2015_BUILTIN) + ECMA_MATH_OBJECT_TRUNC, /* ECMA-262 v6, 20.2.2.35 */ + ECMA_MATH_OBJECT_SIGN, /* ECMA-262 v6, 20.2.2.29 */ +#endif /* ENABLED (JERRY_ES2015_BUILTIN) */ ECMA_MATH_OBJECT_ROUND, /* ECMA-262 v5, 15.8.2.15 */ ECMA_MATH_OBJECT_SIN, /* ECMA-262 v5, 15.8.2.16 */ ECMA_MATH_OBJECT_SQRT, /* ECMA-262 v5, 15.8.2.17 */ @@ -153,6 +157,61 @@ ecma_builtin_math_object_max_min (bool is_max, /**< 'max' or 'min' operation */ return ecma_make_number_value (result_num); } /* ecma_builtin_math_object_max_min */ +#if ENABLED (JERRY_ES2015_BUILTIN) +/** + * The Math object's 'trunc' routine + * + * See also: + * ECMA-262 v6, 20.2.2.35 + * + * @return ecma number + */ +static ecma_number_t +ecma_builtin_math_object_trunc (ecma_number_t arg) +{ + if (ecma_number_is_nan (arg) || ecma_number_is_infinity (arg) || ecma_number_is_zero (arg)) + { + return arg; + } + + if ((arg > 0) && (arg < 1)) + { + return (ecma_number_t) 0; + } + + if ((arg < 0) && (arg > -1)) + { + return (ecma_number_t) -0; + } + + return (ecma_number_t) arg - fmod (arg, 1); +} /* ecma_builtin_math_object_trunc */ + +/** + * The Math object's 'sign' routine + * + * See also: + * ECMA-262 v6, 20.2.2.29 + * + * @return ecma number + */ +static ecma_number_t +ecma_builtin_math_object_sign (ecma_number_t arg) +{ + if (ecma_number_is_nan (arg) || ecma_number_is_zero (arg)) + { + return arg; + } + + if (ecma_number_is_negative (arg)) + { + return (ecma_number_t) -1.0; + } + + return (ecma_number_t) 1.0; +} /* ecma_builtin_math_object_sign */ +#endif /* ENABLED (JERRY_ES2015_BUILTIN) */ + /** * The Math object's 'random' routine. * @@ -282,6 +341,18 @@ ecma_builtin_math_dispatch_routine (uint16_t builtin_routine_id, /**< built-in w x = DOUBLE_TO_ECMA_NUMBER_T (log (x)); break; } +#if ENABLED (JERRY_ES2015_BUILTIN) + case ECMA_MATH_OBJECT_TRUNC: + { + x = ecma_builtin_math_object_trunc (x); + break; + } + case ECMA_MATH_OBJECT_SIGN: + { + x = ecma_builtin_math_object_sign (x); + break; + } +#endif /* ENABLED (JERRY_ES2015_BUILTIN) */ case ECMA_MATH_OBJECT_ROUND: { if (ecma_number_is_nan (x) diff --git a/jerry-core/ecma/builtin-objects/ecma-builtin-math.inc.h b/jerry-core/ecma/builtin-objects/ecma-builtin-math.inc.h index 423ea70dc..f889b5477 100644 --- a/jerry-core/ecma/builtin-objects/ecma-builtin-math.inc.h +++ b/jerry-core/ecma/builtin-objects/ecma-builtin-math.inc.h @@ -91,6 +91,10 @@ ROUTINE (LIT_MAGIC_STRING_ROUND, ECMA_MATH_OBJECT_ROUND, 1, 1) ROUTINE (LIT_MAGIC_STRING_SIN, ECMA_MATH_OBJECT_SIN, 1, 1) ROUTINE (LIT_MAGIC_STRING_SQRT, ECMA_MATH_OBJECT_SQRT, 1, 1) ROUTINE (LIT_MAGIC_STRING_TAN, ECMA_MATH_OBJECT_TAN, 1, 1) +#if ENABLED (JERRY_ES2015_BUILTIN) +ROUTINE (LIT_MAGIC_STRING_SIGN, ECMA_MATH_OBJECT_SIGN, 1, 1) +ROUTINE (LIT_MAGIC_STRING_TRUNC, ECMA_MATH_OBJECT_TRUNC, 1, 1) +#endif /* ENABLED (JERRY_ES2015_BUILTIN) */ #endif /* ENABLED (JERRY_BUILTIN_MATH) */ diff --git a/jerry-core/lit/lit-magic-strings.inc.h b/jerry-core/lit/lit-magic-strings.inc.h index 763700e0b..17ae3967a 100644 --- a/jerry-core/lit/lit-magic-strings.inc.h +++ b/jerry-core/lit/lit-magic-strings.inc.h @@ -160,6 +160,9 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_PUSH, "push") LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_RACE, "race") #endif LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SEAL, "seal") +#if ENABLED (JERRY_BUILTIN_MATH) && ENABLED (JERRY_ES2015_BUILTIN) +LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SIGN, "sign") +#endif #if ENABLED (JERRY_ES2015_BUILTIN_MAP) \ || ENABLED (JERRY_ES2015_BUILTIN_SET) LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SIZE, "size") @@ -238,6 +241,9 @@ LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SLICE, "slice") || ENABLED (JERRY_ES2015_BUILTIN_SYMBOL) LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_SPLIT, "split") #endif +#if ENABLED (JERRY_BUILTIN_MATH) && ENABLED (JERRY_ES2015_BUILTIN) +LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_TRUNC, "trunc") +#endif LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_VALUE, "value") #if ENABLED (JERRY_LINE_INFO) || ENABLED (JERRY_ERROR_MESSAGES) && ENABLED (JERRY_PARSER) LIT_MAGIC_STRING_DEF (LIT_MAGIC_STRING_RESOURCE_EVAL, "") diff --git a/jerry-core/lit/lit-magic-strings.ini b/jerry-core/lit/lit-magic-strings.ini index 57e521562..1a37d1249 100644 --- a/jerry-core/lit/lit-magic-strings.ini +++ b/jerry-core/lit/lit-magic-strings.ini @@ -83,6 +83,7 @@ LIT_MAGIC_STRING_NEXT = "next" LIT_MAGIC_STRING_PUSH = "push" LIT_MAGIC_STRING_RACE = "race" LIT_MAGIC_STRING_SEAL = "seal" +LIT_MAGIC_STRING_SIGN = "sign" LIT_MAGIC_STRING_SIZE = "size" LIT_MAGIC_STRING_SOME = "some" LIT_MAGIC_STRING_SORT = "sort" @@ -111,6 +112,7 @@ LIT_MAGIC_STRING_ROUND = "round" LIT_MAGIC_STRING_SHIFT = "shift" LIT_MAGIC_STRING_SLICE = "slice" LIT_MAGIC_STRING_SPLIT = "split" +LIT_MAGIC_STRING_TRUNC = "trunc" LIT_MAGIC_STRING_VALUE = "value" LIT_MAGIC_STRING_RESOURCE_EVAL = "" LIT_MAGIC_STRING_LOG10E_U = "LOG10E" diff --git a/tests/jerry/es2015/math-sign.js b/tests/jerry/es2015/math-sign.js new file mode 100644 index 000000000..7d2fe8f42 --- /dev/null +++ b/tests/jerry/es2015/math-sign.js @@ -0,0 +1,23 @@ +// Copyright JS Foundation and other contributors, http://js.foundation +// +// 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 nan = NaN; +var p_zero = 0.0; +var m_zero = -p_zero; + +assert (isNaN(Math['sign'](NaN))); +assert (Math['sign'](p_zero) === p_zero); +assert (Math['sign'](m_zero) === m_zero); +assert (Math['sign'](-5) === -1); +assert (Math['sign'](5) === 1); diff --git a/tests/jerry/es2015/math-trunc.js b/tests/jerry/es2015/math-trunc.js new file mode 100644 index 000000000..958e83dc9 --- /dev/null +++ b/tests/jerry/es2015/math-trunc.js @@ -0,0 +1,31 @@ +// Copyright JS Foundation and other contributors, http://js.foundation +// +// 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 nan = NaN; +var p_zero = 0.0; +var m_zero = -p_zero; +var p_inf = Infinity; +var m_inf = -p_inf; + +assert (isNaN(Math['trunc'](NaN))); +assert (Math['trunc'](p_zero) === p_zero); +assert (Math['trunc'](m_zero) === m_zero); +assert (Math['trunc'](p_inf) === p_inf); +assert (Math['trunc'](m_inf) === m_inf); +assert (Math['trunc'](0.5) === p_zero); +assert (Math['trunc'](-0.5) === m_zero); +assert (Math['trunc'](1.2) === 1); +assert (Math['trunc'](-1.5) === -1); +assert (Math['trunc'](65.7) === 65); +assert (Math['trunc'](-24.9) === -24);