@@ -52,7 +52,6 @@ namespace typed_array {
5252 sizeLog2: uintptr;
5353 kind: ElementsKind;
5454 }
55- extern runtime TypedArraySortFast(Context, JSAny): JSTypedArray;
5655 extern runtime TypedArrayCopyElements(Context, JSTypedArray, Object, Number):
5756 void;
5857 extern macro TypedArrayBuiltinsAssembler::ValidateTypedArray(
@@ -272,138 +271,4 @@ namespace typed_array {
272271 }
273272 return kStoreSucceded;
274273 }
275-
276- transitioning macro CallCompare(
277- implicit context: Context, array: JSTypedArray,
278- comparefn: Callable)(a: JSAny, b: JSAny): Number {
279- // a. Let v be ? ToNumber(? Call(comparefn, undefined, x, y)).
280- const v: Number =
281- ToNumber_Inline(Call(context, comparefn, Undefined, a, b));
282-
283- // b. If IsDetachedBuffer(buffer) is true, throw a TypeError exception.
284- if (IsDetachedBuffer(array.buffer)) {
285- ThrowTypeError(kDetachedOperation, '%TypedArray%.prototype.sort');
286- }
287-
288- // c. If v is NaN, return +0.
289- if (NumberIsNaN(v)) return 0;
290-
291- // d. return v.
292- return v;
293- }
294-
295- // Merges two sorted runs [from, middle) and [middle, to)
296- // from "source" into "target".
297- transitioning macro
298- TypedArrayMerge(
299- implicit context: Context, array: JSTypedArray, comparefn: Callable)(
300- source: FixedArray, from: uintptr, middle: uintptr, to: uintptr,
301- target: FixedArray) {
302- let left: uintptr = from;
303- let right: uintptr = middle;
304-
305- for (let targetIndex: uintptr = from; targetIndex < to; ++targetIndex) {
306- if (left < middle && right >= to) {
307- // If the left run has elements, but the right does not, we take
308- // from the left.
309- target.objects[targetIndex] = source.objects[left++];
310- } else if (left < middle) {
311- // If both have elements, we need to compare.
312- const leftElement = UnsafeCast<JSAny>(source.objects[left]);
313- const rightElement = UnsafeCast<JSAny>(source.objects[right]);
314- if (CallCompare(leftElement, rightElement) <= 0) {
315- target.objects[targetIndex] = leftElement;
316- left++;
317- } else {
318- target.objects[targetIndex] = rightElement;
319- right++;
320- }
321- } else {
322- // No elements on the left, but the right does, so we take
323- // from the right.
324- assert(left == middle);
325- target.objects[targetIndex] = source.objects[right++];
326- }
327- }
328- }
329-
330- transitioning builtin
331- TypedArrayMergeSort(implicit context: Context)(
332- source: FixedArray, from: uintptr, to: uintptr, target: FixedArray,
333- array: JSTypedArray, comparefn: Callable): JSAny {
334- assert(to - from > 1);
335- const middle: uintptr = from + ((to - from) >>> 1);
336-
337- // On the next recursion step source becomes target and vice versa.
338- // This saves the copy of the relevant range from the original
339- // array into a work array on each recursion step.
340- if (middle - from > 1) {
341- TypedArrayMergeSort(target, from, middle, source, array, comparefn);
342- }
343- if (to - middle > 1) {
344- TypedArrayMergeSort(target, middle, to, source, array, comparefn);
345- }
346-
347- TypedArrayMerge(source, from, middle, to, target);
348-
349- return Undefined;
350- }
351-
352- // https://tc39.github.io/ecma262/#sec-%typedarray%.prototype.sort
353- transitioning javascript builtin TypedArrayPrototypeSort(
354- js-implicit context: Context,
355- receiver: JSAny)(...arguments): JSTypedArray {
356- // 1. If comparefn is not undefined and IsCallable(comparefn) is false,
357- // throw a TypeError exception.
358- const comparefnObj: JSAny = arguments.length > 0 ? arguments[0] : Undefined;
359- if (comparefnObj != Undefined && !TaggedIsCallable(comparefnObj)) {
360- ThrowTypeError(kBadSortComparisonFunction, comparefnObj);
361- }
362-
363- // 2. Let obj be the this value.
364- const obj: JSAny = receiver;
365-
366- // 3. Let buffer be ? ValidateTypedArray(obj).
367- // ValidateTypedArray currently returns the array, not the ViewBuffer.
368- const array: JSTypedArray =
369- ValidateTypedArray(context, obj, '%TypedArray%.prototype.sort');
370-
371- // Default sorting is done in C++ using std::sort
372- if (comparefnObj == Undefined) {
373- return TypedArraySortFast(context, obj);
374- }
375-
376- // 4. Let len be obj.[[ArrayLength]].
377- const len: uintptr = array.length;
378-
379- // Arrays of length 1 or less are considered sorted.
380- if (len < 2) return array;
381-
382- const comparefn: Callable =
383- Cast<Callable>(comparefnObj) otherwise unreachable;
384- const accessor: TypedArrayAccessor =
385- GetTypedArrayAccessor(array.elements_kind);
386-
387- // Prepare the two work arrays. All numbers are converted to tagged
388- // objects first, and merge sorted between the two FixedArrays.
389- // The result is then written back into the JSTypedArray.
390- const work1: FixedArray = AllocateZeroedFixedArray(Convert<intptr>(len));
391- const work2: FixedArray = AllocateZeroedFixedArray(Convert<intptr>(len));
392-
393- for (let i: uintptr = 0; i < len; ++i) {
394- const element: Numeric = accessor.LoadNumeric(context, array, i);
395- work1.objects[i] = element;
396- work2.objects[i] = element;
397- }
398-
399- TypedArrayMergeSort(work2, 0, len, work1, array, comparefn);
400-
401- // work1 contains the sorted numbers. Write them back.
402- for (let i: uintptr = 0; i < len; ++i) {
403- accessor.StoreNumeric(
404- context, array, i, UnsafeCast<Numeric>(work1.objects[i]));
405- }
406-
407- return array;
408- }
409274}
0 commit comments