Skip to content

Commit dad959b

Browse files
Add %TypedArray%.prototype.sort([ compareFunction ]) support.
JerryScript-DCO-1.0-Signed-off-by: Anthony Calandra anthony@anthony-calandra.com
1 parent 0c6b5ea commit dad959b

File tree

6 files changed

+449
-142
lines changed

6 files changed

+449
-142
lines changed
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/* Copyright JS Foundation and other contributors, http://js.foundation
2+
*
3+
* Licensed under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License.
5+
* You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software
10+
* distributed under the License is distributed on an "AS IS" BASIS
11+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
* See the License for the specific language governing permissions and
13+
* limitations under the License.
14+
*/
15+
16+
#include "ecma-globals.h"
17+
#include "ecma-helpers.h"
18+
#include "ecma-try-catch-macro.h"
19+
20+
/**
21+
* Function used to reconstruct the ordered binary tree.
22+
* Shifts 'index' down in the tree until it is in the correct position.
23+
*
24+
* @return ecma value
25+
* Returned value must be freed with ecma_free_value.
26+
*/
27+
static ecma_value_t
28+
ecma_helpers_array_to_heap (ecma_value_t array[], /**< heap data array */
29+
int index, /**< current item index */
30+
int right, /**< right index is a maximum index */
31+
ecma_value_t comparefn, /**< compare function */
32+
const ecma_sort_compare_helper_t sortfn) /**< sorting function */
33+
{
34+
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
35+
36+
/* Left child of the current index. */
37+
int child = index * 2 + 1;
38+
ecma_value_t swap = array[index];
39+
bool should_break = false;
40+
41+
while (child <= right && ecma_is_value_empty (ret_value) && !should_break)
42+
{
43+
if (child < right)
44+
{
45+
/* Compare the two child nodes. */
46+
ECMA_TRY_CATCH (child_compare_value, sortfn (array[child], array[child + 1], comparefn),
47+
ret_value);
48+
49+
JERRY_ASSERT (ecma_is_value_number (child_compare_value));
50+
51+
/* Use the child that is greater. */
52+
if (ecma_get_number_from_value (child_compare_value) < ECMA_NUMBER_ZERO)
53+
{
54+
child++;
55+
}
56+
57+
ECMA_FINALIZE (child_compare_value);
58+
}
59+
60+
if (ecma_is_value_empty (ret_value))
61+
{
62+
JERRY_ASSERT (child <= right);
63+
64+
/* Compare current child node with the swap (tree top). */
65+
ECMA_TRY_CATCH (swap_compare_value, sortfn (array[child], swap, comparefn), ret_value);
66+
JERRY_ASSERT (ecma_is_value_number (swap_compare_value));
67+
68+
if (ecma_get_number_from_value (swap_compare_value) <= ECMA_NUMBER_ZERO)
69+
{
70+
/* Break from loop if current child is less than swap (tree top) */
71+
should_break = true;
72+
}
73+
else
74+
{
75+
/* We have to move 'swap' lower in the tree, so shift current child up in the hierarchy. */
76+
int parent = (child - 1) / 2;
77+
JERRY_ASSERT (parent >= 0 && parent <= right);
78+
array[parent] = array[child];
79+
80+
/* Update child to be the left child of the current node. */
81+
child = child * 2 + 1;
82+
}
83+
84+
ECMA_FINALIZE (swap_compare_value);
85+
}
86+
}
87+
88+
/*
89+
* Loop ended, either current child does not exist, or is less than swap.
90+
* This means that 'swap' should be placed in the parent node.
91+
*/
92+
int parent = (child - 1) / 2;
93+
JERRY_ASSERT (parent >= 0 && parent <= right);
94+
array[parent] = swap;
95+
96+
if (ecma_is_value_empty (ret_value))
97+
{
98+
ret_value = ECMA_VALUE_UNDEFINED;
99+
}
100+
101+
return ret_value;
102+
} /* ecma_helpers_array_to_heap */
103+
104+
/**
105+
* Heapsort function
106+
*
107+
* @return ecma value
108+
* Returned value must be freed with ecma_free_value.
109+
*/
110+
ecma_value_t
111+
ecma_helpers_array_heap_sort_helper (ecma_value_t array[], /**< array to sort */
112+
int right, /**< right index */
113+
ecma_value_t comparefn, /**< compare function */
114+
const ecma_sort_compare_helper_t sortfn) /**< sorting function */
115+
{
116+
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
117+
118+
/* First, construct the ordered binary tree from the array. */
119+
for (int i = right / 2; i >= 0 && ecma_is_value_empty (ret_value); i--)
120+
{
121+
ECMA_TRY_CATCH (value,
122+
ecma_helpers_array_to_heap (array, i, right, comparefn, sortfn),
123+
ret_value);
124+
ECMA_FINALIZE (value);
125+
}
126+
127+
/* Sorting elements. */
128+
for (int i = right; i > 0 && ecma_is_value_empty (ret_value); i--)
129+
{
130+
/*
131+
* The top element will always contain the largest value.
132+
* Move top to the end, and remove it from the tree.
133+
*/
134+
ecma_value_t swap = array[0];
135+
array[0] = array[i];
136+
array[i] = swap;
137+
138+
/* Rebuild binary tree from the remaining elements. */
139+
ECMA_TRY_CATCH (value,
140+
ecma_helpers_array_to_heap (array, 0, i - 1, comparefn, sortfn),
141+
ret_value);
142+
ECMA_FINALIZE (value);
143+
}
144+
145+
return ret_value;
146+
} /* ecma_helpers_array_heap_sort_helper */

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,15 @@ lit_utf8_size_t ecma_number_to_utf8_string (ecma_number_t num, lit_utf8_byte_t *
375375
/* ecma-helpers-errol.c */
376376
lit_utf8_size_t ecma_errol0_dtoa (double val, lit_utf8_byte_t *buffer_p, int32_t *exp_p);
377377

378+
/* ecma-helpers-sort.c */
379+
typedef ecma_value_t (*ecma_sort_compare_helper_t)(ecma_value_t lhs,
380+
ecma_value_t rhs,
381+
ecma_value_t comparefn);
382+
ecma_value_t ecma_helpers_array_heap_sort_helper (ecma_value_t array[],
383+
int right,
384+
ecma_value_t comparefn,
385+
const ecma_sort_compare_helper_t sortfn);
386+
378387
/**
379388
* @}
380389
* @}

jerry-core/ecma/builtin-objects/ecma-builtin-array-prototype.c

Lines changed: 5 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -991,145 +991,6 @@ ecma_builtin_array_prototype_object_sort_compare_helper (ecma_value_t j, /**< le
991991
return ret_value;
992992
} /* ecma_builtin_array_prototype_object_sort_compare_helper */
993993

994-
/**
995-
* Function used to reconstruct the ordered binary tree.
996-
* Shifts 'index' down in the tree until it is in the correct position.
997-
*
998-
* @return ecma value
999-
* Returned value must be freed with ecma_free_value.
1000-
*/
1001-
static ecma_value_t
1002-
ecma_builtin_array_prototype_object_array_to_heap_helper (ecma_value_t array[], /**< heap data array */
1003-
int index, /**< current item index */
1004-
int right, /**< right index is a maximum index */
1005-
ecma_value_t comparefn) /**< compare function */
1006-
{
1007-
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
1008-
1009-
/* Left child of the current index. */
1010-
int child = index * 2 + 1;
1011-
ecma_value_t swap = array[index];
1012-
bool should_break = false;
1013-
1014-
while (child <= right && ecma_is_value_empty (ret_value) && !should_break)
1015-
{
1016-
if (child < right)
1017-
{
1018-
/* Compare the two child nodes. */
1019-
ECMA_TRY_CATCH (child_compare_value,
1020-
ecma_builtin_array_prototype_object_sort_compare_helper (array[child],
1021-
array[child + 1],
1022-
comparefn),
1023-
ret_value);
1024-
1025-
JERRY_ASSERT (ecma_is_value_number (child_compare_value));
1026-
1027-
/* Use the child that is greater. */
1028-
if (ecma_get_number_from_value (child_compare_value) < ECMA_NUMBER_ZERO)
1029-
{
1030-
child++;
1031-
}
1032-
1033-
ECMA_FINALIZE (child_compare_value);
1034-
}
1035-
1036-
if (ecma_is_value_empty (ret_value))
1037-
{
1038-
JERRY_ASSERT (child <= right);
1039-
1040-
/* Compare current child node with the swap (tree top). */
1041-
ECMA_TRY_CATCH (swap_compare_value,
1042-
ecma_builtin_array_prototype_object_sort_compare_helper (array[child],
1043-
swap,
1044-
comparefn),
1045-
ret_value);
1046-
JERRY_ASSERT (ecma_is_value_number (swap_compare_value));
1047-
1048-
if (ecma_get_number_from_value (swap_compare_value) <= ECMA_NUMBER_ZERO)
1049-
{
1050-
/* Break from loop if current child is less than swap (tree top) */
1051-
should_break = true;
1052-
}
1053-
else
1054-
{
1055-
/* We have to move 'swap' lower in the tree, so shift current child up in the hierarchy. */
1056-
int parent = (child - 1) / 2;
1057-
JERRY_ASSERT (parent >= 0 && parent <= right);
1058-
array[parent] = array[child];
1059-
1060-
/* Update child to be the left child of the current node. */
1061-
child = child * 2 + 1;
1062-
}
1063-
1064-
ECMA_FINALIZE (swap_compare_value);
1065-
}
1066-
}
1067-
1068-
/*
1069-
* Loop ended, either current child does not exist, or is less than swap.
1070-
* This means that 'swap' should be placed in the parent node.
1071-
*/
1072-
int parent = (child - 1) / 2;
1073-
JERRY_ASSERT (parent >= 0 && parent <= right);
1074-
array[parent] = swap;
1075-
1076-
if (ecma_is_value_empty (ret_value))
1077-
{
1078-
ret_value = ECMA_VALUE_UNDEFINED;
1079-
}
1080-
1081-
return ret_value;
1082-
} /* ecma_builtin_array_prototype_object_array_to_heap_helper */
1083-
1084-
/**
1085-
* Heapsort function
1086-
*
1087-
* @return ecma value
1088-
* Returned value must be freed with ecma_free_value.
1089-
*/
1090-
static ecma_value_t
1091-
ecma_builtin_array_prototype_object_array_heap_sort_helper (ecma_value_t array[], /**< array to sort */
1092-
int right, /**< right index */
1093-
ecma_value_t comparefn) /**< compare function */
1094-
{
1095-
ecma_value_t ret_value = ECMA_VALUE_EMPTY;
1096-
1097-
/* First, construct the ordered binary tree from the array. */
1098-
for (int i = right / 2; i >= 0 && ecma_is_value_empty (ret_value); i--)
1099-
{
1100-
ECMA_TRY_CATCH (value,
1101-
ecma_builtin_array_prototype_object_array_to_heap_helper (array,
1102-
i,
1103-
right,
1104-
comparefn),
1105-
ret_value);
1106-
ECMA_FINALIZE (value);
1107-
}
1108-
1109-
/* Sorting elements. */
1110-
for (int i = right; i > 0 && ecma_is_value_empty (ret_value); i--)
1111-
{
1112-
/*
1113-
* The top element will always contain the largest value.
1114-
* Move top to the end, and remove it from the tree.
1115-
*/
1116-
ecma_value_t swap = array[0];
1117-
array[0] = array[i];
1118-
array[i] = swap;
1119-
1120-
/* Rebuild binary tree from the remaining elements. */
1121-
ECMA_TRY_CATCH (value,
1122-
ecma_builtin_array_prototype_object_array_to_heap_helper (array,
1123-
0,
1124-
i - 1,
1125-
comparefn),
1126-
ret_value);
1127-
ECMA_FINALIZE (value);
1128-
}
1129-
1130-
return ret_value;
1131-
} /* ecma_builtin_array_prototype_object_array_heap_sort_helper */
1132-
1133994
/**
1134995
* The Array.prototype object's 'sort' routine
1135996
*
@@ -1218,10 +1079,12 @@ ecma_builtin_array_prototype_object_sort (ecma_value_t this_arg, /**< this argum
12181079
/* Sorting. */
12191080
if (copied_num > 1 && ecma_is_value_empty (ret_value))
12201081
{
1082+
const ecma_sort_compare_helper_t sortfn = &ecma_builtin_array_prototype_object_sort_compare_helper;
12211083
ECMA_TRY_CATCH (sort_value,
1222-
ecma_builtin_array_prototype_object_array_heap_sort_helper (values_buffer,
1223-
(int)(copied_num - 1),
1224-
arg1),
1084+
ecma_helpers_array_heap_sort_helper (values_buffer,
1085+
(int)(copied_num - 1),
1086+
arg1,
1087+
sortfn),
12251088
ret_value);
12261089
ECMA_FINALIZE (sort_value);
12271090
}

0 commit comments

Comments
 (0)