Skip to content

Commit 2f47e28

Browse files
committed
avm2: Also use avmplus' qsort in Vector.sort
This was forgotten in ruffle-rs#17846.
1 parent 24b0c8b commit 2f47e28

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

core/src/avm2/globals/array.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,9 @@ where
903903
/// will permute the slice arbitrarily, but won't return an error.
904904
///
905905
/// Original code: https://github.com/adobe/avmplus/blob/master/core/ArrayClass.cpp#L637
906-
fn qsort<T, E>(
906+
///
907+
/// NOTE: this is `pub(super)` so it can be called by `vector::sort`.
908+
pub(super) fn qsort<T, E>(
907909
slice: &mut [T],
908910
cmp: &mut impl FnMut(&T, &T) -> Result<Ordering, E>,
909911
) -> Result<(), E> {

core/src/avm2/globals/vector.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,8 @@ pub fn slice<'gc>(
764764
}
765765

766766
/// Implements `Vector.sort`
767+
///
768+
/// TODO: Consider sharing this code with `globals::array::sort`?
767769
pub fn sort<'gc>(
768770
activation: &mut Activation<'_, 'gc>,
769771
this: Object<'gc>,
@@ -813,21 +815,18 @@ pub fn sort<'gc>(
813815
drop(vs);
814816

815817
let mut unique_sort_satisfied = true;
816-
let mut error_signal = Ok(());
817-
values.sort_unstable_by(|a, b| match compare(activation, *a, *b) {
818-
Ok(Ordering::Equal) => {
819-
unique_sort_satisfied = false;
820-
Ordering::Equal
821-
}
822-
Ok(v) if options.contains(SortOptions::DESCENDING) => v.reverse(),
823-
Ok(v) => v,
824-
Err(e) => {
825-
error_signal = Err(e);
826-
Ordering::Less
827-
}
828-
});
829-
830-
error_signal?;
818+
super::array::qsort(&mut values, &mut |a, b| {
819+
compare(activation, *a, *b).map(|cmp| {
820+
if cmp == Ordering::Equal {
821+
unique_sort_satisfied = false;
822+
Ordering::Equal
823+
} else if options.contains(SortOptions::DESCENDING) {
824+
cmp.reverse()
825+
} else {
826+
cmp
827+
}
828+
})
829+
})?;
831830

832831
//NOTE: RETURNINDEXEDARRAY does NOT actually return anything useful.
833832
//The actual sorting still happens, but the results are discarded.

0 commit comments

Comments
 (0)