1
1
/* Copyright 2014-2015 Samsung Electronics Co., Ltd.
2
+ * Copyright 2015 University of Szeged.
2
3
*
3
4
* Licensed under the Apache License, Version 2.0 (the "License");
4
5
* you may not use this file except in compliance with the License.
@@ -892,46 +893,20 @@ ecma_builtin_array_prototype_object_index_of (ecma_value_t this_arg, /**< this a
892
893
/* 5. */
893
894
ECMA_OP_TO_NUMBER_TRY_CATCH (arg_from_idx, arg2, ret_value);
894
895
895
- int32_t from_idx_int = ecma_number_to_int32 (arg_from_idx);
896
+ uint32_t from_idx = ecma_builtin_helper_array_index_normalize (arg_from_idx, len );
896
897
897
898
/* 6. */
898
- if (from_idx_int > 0 && ( uint32_t ) from_idx_int >= len)
899
+ if (from_idx >= len)
899
900
{
900
901
ret_value = ecma_make_normal_completion_value (ecma_make_number_value (num_p));
901
902
}
902
903
else
903
904
{
904
- uint32_t k ;
905
+ JERRY_ASSERT (from_idx < len) ;
905
906
906
- /* 7 */
907
- if (from_idx_int >= 0 )
907
+ for (; from_idx < len && *num_p < 0 && ecma_is_completion_value_empty (ret_value); from_idx++)
908
908
{
909
- k = (uint32_t ) from_idx_int;
910
- }
911
- /* 8. */
912
- else
913
- {
914
- from_idx_int = -from_idx_int;
915
-
916
- /* As opposed to the standard, we prevent k from being negative, so that we can use an uint32 */
917
- if ((uint32_t ) from_idx_int < len)
918
- {
919
- /* 8.a */
920
- k = len - (uint32_t ) from_idx_int;
921
- }
922
- /* If k would've been negative */
923
- else
924
- {
925
- /* 8.b */
926
- k = 0 ;
927
- }
928
-
929
- }
930
- JERRY_ASSERT (k < len);
931
-
932
- for (; k < len && *num_p < 0 && ecma_is_completion_value_empty (ret_value); k++)
933
- {
934
- ecma_string_t *idx_str_p = ecma_new_ecma_string_from_uint32 (k);
909
+ ecma_string_t *idx_str_p = ecma_new_ecma_string_from_uint32 (from_idx);
935
910
936
911
/* 9.a */
937
912
if (ecma_op_object_get_property (obj_p, idx_str_p) != NULL )
@@ -942,7 +917,7 @@ ecma_builtin_array_prototype_object_index_of (ecma_value_t this_arg, /**< this a
942
917
/* 9.b.ii */
943
918
if (ecma_op_strict_equality_compare (arg1, get_value))
944
919
{
945
- *num_p = ecma_uint32_to_number (k );
920
+ *num_p = ecma_uint32_to_number (from_idx );
946
921
}
947
922
948
923
ECMA_FINALIZE (get_value);
@@ -1019,59 +994,75 @@ ecma_builtin_array_prototype_object_last_index_of (ecma_value_t this_arg, /**< t
1019
994
}
1020
995
else
1021
996
{
1022
- uint32_t k = len - 1 ;
997
+ uint32_t from_idx = len - 1 ;
1023
998
1024
999
/* 5. */
1025
1000
if (!ecma_is_value_undefined (arg2))
1026
1001
{
1027
1002
ECMA_OP_TO_NUMBER_TRY_CATCH (arg_from_idx, arg2, ret_value);
1028
- int32_t n = ecma_number_to_int32 (arg_from_idx);
1029
1003
1030
- /* 6. */
1031
- if (n >= 0 )
1004
+ if (!ecma_number_is_nan (arg_from_idx))
1032
1005
{
1033
- /* min(n, len - 1) */
1034
- if (( uint32_t ) n > len - 1 )
1006
+
1007
+ if (ecma_number_is_infinity (arg_from_idx) )
1035
1008
{
1036
- k = len - 1 ;
1009
+ from_idx = ecma_number_is_negative (arg_from_idx) ? ( uint32_t ) - 1 : len - 1 ;
1037
1010
}
1038
1011
else
1039
1012
{
1040
- k = (uint32_t ) n;
1013
+ int32_t int_from_idx = ecma_number_to_int32 (arg_from_idx);
1014
+
1015
+ /* 6. */
1016
+ if (int_from_idx >= 0 )
1017
+ {
1018
+ /* min(int_from_idx, len - 1)*/
1019
+ if ((uint32_t ) int_from_idx > len - 1 )
1020
+ {
1021
+ from_idx = len - 1 ;
1022
+ }
1023
+ else
1024
+ {
1025
+ from_idx = (uint32_t ) int_from_idx;
1026
+ }
1027
+ }
1028
+ /* 7. */
1029
+ else
1030
+ {
1031
+ int_from_idx = -int_from_idx;
1032
+
1033
+ /* We prevent from_idx from being negative, so that we can use an uint32 */
1034
+ if ((uint32_t ) int_from_idx <= len)
1035
+ {
1036
+ from_idx = len - (uint32_t ) int_from_idx;
1037
+ }
1038
+ else
1039
+ {
1040
+ /*
1041
+ * If from_idx would be negative, we set it to UINT_MAX. See reasoning for this in the comment
1042
+ * at the for loop below.
1043
+ */
1044
+ from_idx = (uint32_t ) -1 ;
1045
+ }
1046
+ }
1041
1047
}
1042
1048
}
1043
- /* 7. */
1044
1049
else
1045
1050
{
1046
- n = -n;
1047
-
1048
- /* We prevent k from being negative, so that we can use an uint32 */
1049
- if ((uint32_t ) n <= len)
1050
- {
1051
- k = len - (uint32_t ) n;
1052
- }
1053
- else
1054
- {
1055
- /*
1056
- * If k would be negative, we set it to UINT_MAX. See reasoning for this in the comment
1057
- * at the for loop below.
1058
- */
1059
- k = (uint32_t ) -1 ;
1060
- }
1051
+ from_idx = 0 ;
1061
1052
}
1062
1053
1063
1054
ECMA_OP_TO_NUMBER_FINALIZE (arg_from_idx);
1064
1055
}
1065
1056
1066
1057
/* 8.
1067
- * We should break from the loop when k < 0. We can still use an uint32_t for k , and check
1068
- * for an underflow instead. This is safe, because k will always start in [0, len - 1],
1069
- * and len is in [0, UINT_MAX], so k >= len means we've had an underflow, and should stop.
1058
+ * We should break from the loop when from_idx < 0. We can still use an uint32_t for from_idx , and check
1059
+ * for an underflow instead. This is safe, because from_idx will always start in [0, len - 1],
1060
+ * and len is in [0, UINT_MAX], so from_idx >= len means we've had an underflow, and should stop.
1070
1061
*/
1071
- for (;k < len && *num_p < 0 && ecma_is_completion_value_empty (ret_value); k --)
1062
+ for (; from_idx < len && *num_p < 0 && ecma_is_completion_value_empty (ret_value); from_idx --)
1072
1063
{
1073
1064
/* 8.a */
1074
- ecma_string_t *idx_str_p = ecma_new_ecma_string_from_uint32 (k );
1065
+ ecma_string_t *idx_str_p = ecma_new_ecma_string_from_uint32 (from_idx );
1075
1066
1076
1067
/* 8.a */
1077
1068
if (ecma_op_object_get_property (obj_p, idx_str_p) != NULL )
@@ -1082,7 +1073,7 @@ ecma_builtin_array_prototype_object_last_index_of (ecma_value_t this_arg, /**< t
1082
1073
/* 8.b.ii */
1083
1074
if (ecma_op_strict_equality_compare (arg1, get_value))
1084
1075
{
1085
- *num_p = ecma_uint32_to_number (k );
1076
+ *num_p = ecma_uint32_to_number (from_idx );
1086
1077
}
1087
1078
1088
1079
ECMA_FINALIZE (get_value);
@@ -2029,30 +2020,8 @@ ecma_builtin_array_prototype_object_slice (ecma_value_t this_arg, /**< 'this' ar
2029
2020
2030
2021
/* 5. */
2031
2022
ECMA_OP_TO_NUMBER_TRY_CATCH (start_num, arg1, ret_value);
2032
- int32_t relative_start = ecma_number_to_int32 (start_num);
2033
2023
2034
- /* 6. */
2035
- if (relative_start < 0 )
2036
- {
2037
- uint32_t start_abs = (uint32_t ) -relative_start;
2038
-
2039
- if (start_abs > len)
2040
- {
2041
- start = 0 ;
2042
- }
2043
- else
2044
- {
2045
- start = len - start_abs;
2046
- }
2047
- }
2048
- else
2049
- {
2050
- start = (uint32_t ) relative_start;
2051
- if (start > len)
2052
- {
2053
- start = len;
2054
- }
2055
- }
2024
+ start = ecma_builtin_helper_array_index_normalize (start_num, len);
2056
2025
2057
2026
/* 7. */
2058
2027
if (ecma_is_value_undefined (arg2))
@@ -2062,30 +2031,9 @@ ecma_builtin_array_prototype_object_slice (ecma_value_t this_arg, /**< 'this' ar
2062
2031
else
2063
2032
{
2064
2033
/* 7. part 2*/
2065
- ECMA_OP_TO_NUMBER_TRY_CATCH (end_num, arg2, ret_value)
2066
- int32_t relative_end = ecma_number_to_int32 (end_num);
2034
+ ECMA_OP_TO_NUMBER_TRY_CATCH (end_num, arg2, ret_value);
2067
2035
2068
- if (relative_end < 0 )
2069
- {
2070
- uint32_t end_abs = (uint32_t ) -relative_end;
2071
-
2072
- if (end_abs > len)
2073
- {
2074
- end = 0 ;
2075
- }
2076
- else
2077
- {
2078
- end = len - end_abs;
2079
- }
2080
- }
2081
- else
2082
- {
2083
- end = (uint32_t ) relative_end;
2084
- if (end > len)
2085
- {
2086
- end = len;
2087
- }
2088
- }
2036
+ end = ecma_builtin_helper_array_index_normalize (end_num, len);
2089
2037
2090
2038
ECMA_OP_TO_NUMBER_FINALIZE (end_num);
2091
2039
}
@@ -2197,29 +2145,7 @@ ecma_builtin_array_prototype_object_splice (ecma_value_t this_arg, /**< this arg
2197
2145
args[0 ],
2198
2146
ret_value);
2199
2147
2200
- int32_t relative_start = ecma_number_to_int32 (start_num);
2201
-
2202
- /* 6. */
2203
- if (relative_start < 0 )
2204
- {
2205
- uint32_t start_abs = (uint32_t ) - relative_start;
2206
- if (start_abs > len)
2207
- {
2208
- start = 0 ;
2209
- }
2210
- else
2211
- {
2212
- start = len - start_abs;
2213
- }
2214
- }
2215
- else
2216
- {
2217
- start = (uint32_t ) relative_start;
2218
- if (start > len)
2219
- {
2220
- start = len;
2221
- }
2222
- }
2148
+ start = ecma_builtin_helper_array_index_normalize (start_num, len);
2223
2149
2224
2150
/*
2225
2151
* If there is only one argument, that will be the start argument,
@@ -2236,22 +2162,22 @@ ecma_builtin_array_prototype_object_splice (ecma_value_t this_arg, /**< this arg
2236
2162
args[1 ],
2237
2163
ret_value);
2238
2164
2239
- int32_t delete_count_int = ecma_number_to_int32 (delete_num);
2240
-
2241
- if (delete_count_int > 0 )
2165
+ if (!ecma_number_is_nan (delete_num))
2242
2166
{
2243
- delete_count = (uint32_t ) delete_count_int;
2167
+ if (ecma_number_is_negative (delete_num))
2168
+ {
2169
+ delete_count = 0 ;
2170
+ }
2171
+ else
2172
+ {
2173
+ delete_count = ecma_number_is_infinity (delete_num) ? len : ecma_number_to_uint32 (delete_num);
2174
+ }
2244
2175
}
2245
2176
else
2246
2177
{
2247
2178
delete_count = 0 ;
2248
2179
}
2249
2180
2250
- if (len - start < delete_count)
2251
- {
2252
- delete_count = len - start;
2253
- }
2254
-
2255
2181
ECMA_OP_TO_NUMBER_FINALIZE (delete_num);
2256
2182
}
2257
2183
0 commit comments