Skip to content

Commit 1387720

Browse files
committed
Add toLength and toInteger operations
JerryScript-DCO-1.0-Signed-off-by: Daniella Barsony [email protected]
1 parent 9ab4872 commit 1387720

File tree

7 files changed

+739
-2
lines changed

7 files changed

+739
-2
lines changed

jerry-core/ecma/base/ecma-globals.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,14 +1152,14 @@ typedef double ecma_number_t;
11521152
*/
11531153
# define ECMA_NUMBER_EPSILON ((ecma_number_t) 2.2204460492503130808472633361816e-16)
11541154
#endif /* ENABLED (JERRY_ES2015_BUILTIN) */
1155-
#if ENABLED (JERRY_ES2015_BUILTIN)
1155+
#if ENABLED (JERRY_ES2015)
11561156
/**
11571157
* Number.MAX_SAFE_INTEGER
11581158
*
11591159
* See also: ECMA_262 v6, 20.1.2.1
11601160
*/
11611161
# define ECMA_NUMBER_MAX_SAFE_INTEGER ((ecma_number_t) 0x1FFFFFFFFFFFFF)
1162-
#endif /* ENABLED (JERRY_ES2015_BUILTIN) */
1162+
#endif /* ENABLED (JERRY_ES2015) */
11631163
/**
11641164
* Euler number
11651165
*/

jerry-core/ecma/operations/ecma-conversion.c

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* Implementation of ECMA-defined conversion routines
1818
*/
1919

20+
#include <math.h>
21+
2022
#include "ecma-alloc.h"
2123
#include "ecma-boolean-object.h"
2224
#include "ecma-conversion.h"
@@ -910,6 +912,122 @@ ecma_op_to_property_descriptor (ecma_value_t obj_value, /**< object value */
910912
return ret_value;
911913
} /* ecma_op_to_property_descriptor */
912914

915+
/**
916+
* ToInteger operation.
917+
*
918+
* See also:
919+
* ECMA-262 v5, 9.4
920+
* ECMA-262 v6, 7.1.4
921+
*
922+
* @return ECMA_VALUE_EMPTY if successful
923+
* conversion error otherwise
924+
*/
925+
ecma_value_t
926+
ecma_op_to_integer (ecma_value_t value, /**< ecma value*/
927+
ecma_number_t *number_p) /**< [out] ecma number */
928+
{
929+
930+
if (ECMA_IS_VALUE_ERROR (value))
931+
{
932+
return value;
933+
}
934+
935+
/* 1 */
936+
ecma_value_t to_number = ecma_get_number (value, number_p);
937+
938+
/* 2 */
939+
if (ECMA_IS_VALUE_ERROR (to_number))
940+
{
941+
return to_number;
942+
}
943+
944+
ecma_number_t number = *number_p;
945+
946+
/* 3 */
947+
if (ecma_number_is_nan (number))
948+
{
949+
*number_p = ECMA_NUMBER_ZERO;
950+
return ECMA_VALUE_EMPTY;
951+
}
952+
953+
/* 4 */
954+
if (ecma_number_is_zero (number) || ecma_number_is_infinity (number))
955+
{
956+
return ECMA_VALUE_EMPTY;
957+
}
958+
959+
ecma_number_t floor_fabs = floor (fabs (number));
960+
961+
/* 5 */
962+
*number_p = ecma_number_is_negative (number) ? -floor_fabs : floor_fabs;
963+
return ECMA_VALUE_EMPTY;
964+
} /* ecma_op_to_integer */
965+
966+
/**
967+
* ToLength operation.
968+
*
969+
* See also:
970+
* ECMA-262 v6, 7.1.15
971+
*
972+
* @return ECMA_VALUE_EMPTY if successful
973+
* conversion error otherwise
974+
*/
975+
ecma_value_t
976+
ecma_op_to_length (ecma_value_t value, /**< ecma value*/
977+
uint32_t *length) /**< [out] ecma number */
978+
{
979+
/* 1 */
980+
if (ECMA_IS_VALUE_ERROR (value))
981+
{
982+
return value;
983+
}
984+
985+
#if ENABLED (JERRY_ES2015)
986+
987+
ecma_number_t num;
988+
989+
/* 2 */
990+
ecma_value_t length_num = ecma_op_to_integer (value, &num);
991+
992+
/* 3 */
993+
if (ECMA_IS_VALUE_ERROR (length_num))
994+
{
995+
return length_num;
996+
}
997+
998+
/* 4 */
999+
if (num <= 0.0f)
1000+
{
1001+
*length = 0;
1002+
return ECMA_VALUE_EMPTY;
1003+
}
1004+
1005+
/* 5 */
1006+
if (ecma_number_is_infinity (num))
1007+
{
1008+
*length = UINT32_MAX;
1009+
return ECMA_VALUE_EMPTY;
1010+
}
1011+
1012+
/* 6 */
1013+
*length = (uint32_t) JERRY_MIN (num, UINT32_MAX);
1014+
return ECMA_VALUE_EMPTY;
1015+
#else /* !ENABLED (JERRY_ES2015) */
1016+
/* In the case of ES5, ToLength(ES6) operation is the same as ToUint32(ES5) */
1017+
ecma_number_t num;
1018+
ecma_value_t to_number = ecma_get_number (value, &num);
1019+
1020+
/* 2 */
1021+
if (ECMA_IS_VALUE_ERROR (to_number))
1022+
{
1023+
return to_number;
1024+
}
1025+
1026+
*length = ecma_number_to_uint32 (num);
1027+
return ECMA_VALUE_EMPTY;
1028+
#endif /* ENABLED (JERRY_ES2015) */
1029+
} /* ecma_op_to_length */
1030+
9131031
/**
9141032
* @}
9151033
* @}

jerry-core/ecma/operations/ecma-conversion.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ ecma_value_t ecma_get_number (ecma_value_t value, ecma_number_t *number_p);
4949
ecma_value_t ecma_op_to_string (ecma_value_t value);
5050
ecma_string_t *ecma_op_to_prop_name (ecma_value_t value);
5151
ecma_value_t ecma_op_to_object (ecma_value_t value);
52+
ecma_value_t ecma_op_to_integer (ecma_value_t value, ecma_number_t *number_p);
53+
ecma_value_t ecma_op_to_length (ecma_value_t value, uint32_t *length);
5254

5355
ecma_object_t *ecma_op_from_property_descriptor (const ecma_property_descriptor_t *src_prop_desc_p);
5456
ecma_value_t ecma_op_to_property_descriptor (ecma_value_t obj_value, ecma_property_descriptor_t *out_prop_desc_p);

0 commit comments

Comments
 (0)