From 466dc903fbafb02e53574a531c781dd379db275c Mon Sep 17 00:00:00 2001 From: Daniella Barsony Date: Thu, 3 Oct 2019 14:05:23 +0200 Subject: [PATCH] Add toLength and toInteger operations (#3146) JerryScript-DCO-1.0-Signed-off-by: Daniella Barsony bella@inf.u-szeged.hu --- jerry-core/ecma/operations/ecma-conversion.c | 115 +++++++++++++++ jerry-core/ecma/operations/ecma-conversion.h | 2 + ...er-to-integer.c => test-number-to-int32.c} | 0 tests/unit-core/test-to-integer.c | 134 ++++++++++++++++++ tests/unit-core/test-to-length.c | 120 ++++++++++++++++ 5 files changed, 371 insertions(+) rename tests/unit-core/{test-number-to-integer.c => test-number-to-int32.c} (100%) create mode 100644 tests/unit-core/test-to-integer.c create mode 100644 tests/unit-core/test-to-length.c diff --git a/jerry-core/ecma/operations/ecma-conversion.c b/jerry-core/ecma/operations/ecma-conversion.c index ccfa9f269..c528a9254 100644 --- a/jerry-core/ecma/operations/ecma-conversion.c +++ b/jerry-core/ecma/operations/ecma-conversion.c @@ -17,6 +17,8 @@ * Implementation of ECMA-defined conversion routines */ +#include + #include "ecma-alloc.h" #include "ecma-boolean-object.h" #include "ecma-conversion.h" @@ -910,6 +912,119 @@ ecma_op_to_property_descriptor (ecma_value_t obj_value, /**< object value */ return ret_value; } /* ecma_op_to_property_descriptor */ +/** + * ToInteger operation. + * + * See also: + * ECMA-262 v5, 9.4 + * ECMA-262 v6, 7.1.4 + * + * @return ECMA_VALUE_EMPTY if successful + * conversion error otherwise + */ +ecma_value_t +ecma_op_to_integer (ecma_value_t value, /**< ecma value*/ + ecma_number_t *number_p) /**< [out] ecma number */ +{ + if (ECMA_IS_VALUE_ERROR (value)) + { + return value; + } + + /* 1 */ + ecma_value_t to_number = ecma_get_number (value, number_p); + + /* 2 */ + if (ECMA_IS_VALUE_ERROR (to_number)) + { + return to_number; + } + + ecma_number_t number = *number_p; + + /* 3 */ + if (ecma_number_is_nan (number)) + { + *number_p = ECMA_NUMBER_ZERO; + return ECMA_VALUE_EMPTY; + } + + /* 4 */ + if (ecma_number_is_zero (number) || ecma_number_is_infinity (number)) + { + return ECMA_VALUE_EMPTY; + } + + ecma_number_t floor_fabs = floor (fabs (number)); + + /* 5 */ + *number_p = ecma_number_is_negative (number) ? -floor_fabs : floor_fabs; + return ECMA_VALUE_EMPTY; +} /* ecma_op_to_integer */ + +/** + * ToLength operation. + * + * See also: + * ECMA-262 v6, 7.1.15 + * + * @return ECMA_VALUE_EMPTY if successful + * conversion error otherwise + */ +ecma_value_t +ecma_op_to_length (ecma_value_t value, /**< ecma value*/ + uint32_t *length) /**< [out] ecma number */ +{ + /* 1 */ + if (ECMA_IS_VALUE_ERROR (value)) + { + return value; + } + +#if ENABLED (JERRY_ES2015) + /* 2 */ + ecma_number_t num; + ecma_value_t length_num = ecma_op_to_integer (value, &num); + + /* 3 */ + if (ECMA_IS_VALUE_ERROR (length_num)) + { + return length_num; + } + + /* 4 */ + if (num <= 0.0f) + { + *length = 0; + return ECMA_VALUE_EMPTY; + } + + /* 5 */ + if (num >= (ecma_number_t) UINT32_MAX) + { + *length = UINT32_MAX; + return ECMA_VALUE_EMPTY; + } + + /* 6 */ + *length = (uint32_t) num; + return ECMA_VALUE_EMPTY; +#else /* !ENABLED (JERRY_ES2015) */ + /* In the case of ES5, ToLength(ES6) operation is the same as ToUint32(ES5) */ + ecma_number_t num; + ecma_value_t to_number = ecma_get_number (value, &num); + + /* 2 */ + if (ECMA_IS_VALUE_ERROR (to_number)) + { + return to_number; + } + + *length = ecma_number_to_uint32 (num); + return ECMA_VALUE_EMPTY; +#endif /* ENABLED (JERRY_ES2015) */ +} /* ecma_op_to_length */ + /** * @} * @} diff --git a/jerry-core/ecma/operations/ecma-conversion.h b/jerry-core/ecma/operations/ecma-conversion.h index 80fafea76..61e511774 100644 --- a/jerry-core/ecma/operations/ecma-conversion.h +++ b/jerry-core/ecma/operations/ecma-conversion.h @@ -49,6 +49,8 @@ ecma_value_t ecma_get_number (ecma_value_t value, ecma_number_t *number_p); ecma_value_t ecma_op_to_string (ecma_value_t value); ecma_string_t *ecma_op_to_prop_name (ecma_value_t value); ecma_value_t ecma_op_to_object (ecma_value_t value); +ecma_value_t ecma_op_to_integer (ecma_value_t value, ecma_number_t *number_p); +ecma_value_t ecma_op_to_length (ecma_value_t value, uint32_t *length); ecma_object_t *ecma_op_from_property_descriptor (const ecma_property_descriptor_t *src_prop_desc_p); ecma_value_t ecma_op_to_property_descriptor (ecma_value_t obj_value, ecma_property_descriptor_t *out_prop_desc_p); diff --git a/tests/unit-core/test-number-to-integer.c b/tests/unit-core/test-number-to-int32.c similarity index 100% rename from tests/unit-core/test-number-to-integer.c rename to tests/unit-core/test-number-to-int32.c diff --git a/tests/unit-core/test-to-integer.c b/tests/unit-core/test-to-integer.c new file mode 100644 index 000000000..191e0d53e --- /dev/null +++ b/tests/unit-core/test-to-integer.c @@ -0,0 +1,134 @@ +/* + * 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. + */ + +#include "ecma-globals.h" +#include "ecma-helpers.h" +#include "ecma-conversion.h" +#include "ecma-init-finalize.h" +#include "ecma-exceptions.h" +#include "jerryscript.h" +#include "jcontext.h" + +#include "test-common.h" + +/** + * Unit test's main function. + */ +int +main (void) +{ + TEST_INIT (); + + jmem_init (); + ecma_init (); + + ecma_number_t num; + + ecma_value_t int_num = ecma_make_int32_value (123); + + ecma_number_t result = ecma_op_to_integer (int_num, &num); + + ecma_free_value (int_num); + + TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); + TEST_ASSERT (num == 123); + + /* 2 */ + ecma_value_t error = ecma_raise_type_error (ECMA_ERR_MSG ("I am a neat little error message")); + + result = ecma_op_to_integer (error, &num); + + ecma_free_value (JERRY_CONTEXT (error_value)); + + TEST_ASSERT (ECMA_IS_VALUE_ERROR (result)); + + /* 3 */ + ecma_value_t nan = ecma_make_nan_value (); + + result = ecma_op_to_integer (nan, &num); + + ecma_free_value (nan); + + TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); + TEST_ASSERT (num == 0); + + /* 4 */ + /* -0 */ + ecma_value_t negative_zero = ecma_make_number_value (-0.0f); + + result = ecma_op_to_integer (negative_zero, &num); + + ecma_free_value (negative_zero); + + TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); + TEST_ASSERT (1.0f / num == ecma_number_make_infinity (true)); + + /* +0 */ + ecma_value_t positive_zero = ecma_make_number_value (+0.0f); + + result = ecma_op_to_integer (positive_zero, &num); + + ecma_free_value (positive_zero); + + TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); + TEST_ASSERT (1.0f / num == ecma_number_make_infinity (false)); + + /* -infinity */ + ecma_value_t negative_infinity = ecma_make_number_value (ecma_number_make_infinity (true)); + + result = ecma_op_to_integer (negative_infinity, &num); + + ecma_free_value (negative_infinity); + + TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); + TEST_ASSERT (num == ecma_number_make_infinity (true)); + + /* +infinity */ + ecma_value_t positive_infinity = ecma_make_number_value (ecma_number_make_infinity (false)); + + result = ecma_op_to_integer (positive_infinity, &num); + + ecma_free_value (positive_infinity); + + TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); + TEST_ASSERT (num == ecma_number_make_infinity (false)); + + /* 5 */ + ecma_value_t floor = ecma_make_number_value (3.001f); + + result = ecma_op_to_integer (floor, &num); + + ecma_free_value (floor); + + TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); + TEST_ASSERT (num == 3); + + ecma_value_t floor2 = ecma_make_number_value (-26.5973); + + result = ecma_op_to_integer (floor2, &num); + + ecma_free_value (floor2); + + TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); + TEST_ASSERT (num == -26); + + ecma_finalize (); + jmem_finalize (); + + return 0; +} /* main */ + + diff --git a/tests/unit-core/test-to-length.c b/tests/unit-core/test-to-length.c new file mode 100644 index 000000000..4c6c2976c --- /dev/null +++ b/tests/unit-core/test-to-length.c @@ -0,0 +1,120 @@ +/* + * 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. + */ + +#include "ecma-globals.h" +#include "ecma-helpers.h" +#include "ecma-conversion.h" +#include "ecma-init-finalize.h" +#include "ecma-exceptions.h" +#include "jerryscript.h" +#include "jcontext.h" + +#include "test-common.h" + +/** + * Unit test's main function. + */ +int +main (void) +{ + TEST_INIT (); + + jmem_init (); + ecma_init (); + + uint32_t num; + + ecma_value_t int_num = ecma_make_int32_value (123); + + uint32_t result = ecma_op_to_length (int_num, &num); + + ecma_free_value (int_num); + + TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); + TEST_ASSERT (num == 123); + + /* 1, 3 */ + ecma_value_t error_throw = ecma_raise_type_error (ECMA_ERR_MSG ("I'm an error")); + + result = ecma_op_to_length (error_throw, &num); + + ecma_free_value (JERRY_CONTEXT (error_value)); + + TEST_ASSERT (ECMA_IS_VALUE_ERROR (result)); + + /* zero */ + ecma_value_t zero = ecma_make_int32_value (0); + + result = ecma_op_to_length (zero, &num); + + ecma_free_value (zero); + + TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); + TEST_ASSERT (num == 0); + + /* negative */ + ecma_value_t negative = ecma_make_number_value (-26.5973f); + + result = ecma_op_to_length (negative, &num); + + ecma_free_value (negative); + + TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); +#if ENABLED (JERRY_ES2015) + TEST_ASSERT (num == 0); +#else /* !ENABLED (JERRY_ES2015) */ + TEST_ASSERT (num == 4294967270); +#endif /* ENABLED (JERRY_ES2015) */ + + /* +infinity */ + ecma_value_t positive_infinity = ecma_make_number_value (ecma_number_make_infinity (false)); + + result = ecma_op_to_length (positive_infinity, &num); + + ecma_free_value (positive_infinity); + + TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); +#if ENABLED (JERRY_ES2015) + TEST_ASSERT (num == UINT32_MAX); +#else /* !ENABLED (JERRY_ES2015) */ + TEST_ASSERT (num == 0); +#endif /* ENABLED (JERRY_ES2015) */ + + /* -infinity */ + ecma_value_t negative_infinity = ecma_make_number_value (ecma_number_make_infinity (true)); + + result = ecma_op_to_length (negative_infinity, &num); + + ecma_free_value (negative_infinity); + + TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); + TEST_ASSERT (num == 0); + + /* NaN */ + ecma_value_t nan = ecma_make_nan_value (); + + result = ecma_op_to_length (nan, &num); + + ecma_free_value (nan); + + TEST_ASSERT (!ECMA_IS_VALUE_ERROR (result)); + TEST_ASSERT (num == 0); + + ecma_finalize (); + jmem_finalize (); + + return 0; +} /* main */