|
17 | 17 | * Implementation of ECMA-defined conversion routines |
18 | 18 | */ |
19 | 19 |
|
| 20 | +#include <math.h> |
| 21 | + |
20 | 22 | #include "ecma-alloc.h" |
21 | 23 | #include "ecma-boolean-object.h" |
22 | 24 | #include "ecma-conversion.h" |
@@ -910,6 +912,122 @@ ecma_op_to_property_descriptor (ecma_value_t obj_value, /**< object value */ |
910 | 912 | return ret_value; |
911 | 913 | } /* ecma_op_to_property_descriptor */ |
912 | 914 |
|
| 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 | + |
913 | 1031 | /** |
914 | 1032 | * @} |
915 | 1033 | * @} |
|
0 commit comments