avm2: Also use avmplus' qsort in `Vector.sort`

This was forgotten in #17846.
This commit is contained in:
Moulins 2024-09-13 20:22:37 +02:00 committed by TÖRÖK Attila
parent 24b0c8b01e
commit e6e92dd175
2 changed files with 17 additions and 16 deletions

View File

@ -903,7 +903,9 @@ where
/// will permute the slice arbitrarily, but won't return an error.
///
/// Original code: https://github.com/adobe/avmplus/blob/master/core/ArrayClass.cpp#L637
fn qsort<T, E>(
///
/// NOTE: this is `pub(super)` so it can be called by `vector::sort`.
pub(super) fn qsort<T, E>(
slice: &mut [T],
cmp: &mut impl FnMut(&T, &T) -> Result<Ordering, E>,
) -> Result<(), E> {

View File

@ -764,6 +764,8 @@ pub fn slice<'gc>(
}
/// Implements `Vector.sort`
///
/// TODO: Consider sharing this code with `globals::array::sort`?
pub fn sort<'gc>(
activation: &mut Activation<'_, 'gc>,
this: Object<'gc>,
@ -813,21 +815,18 @@ pub fn sort<'gc>(
drop(vs);
let mut unique_sort_satisfied = true;
let mut error_signal = Ok(());
values.sort_unstable_by(|a, b| match compare(activation, *a, *b) {
Ok(Ordering::Equal) => {
unique_sort_satisfied = false;
Ordering::Equal
}
Ok(v) if options.contains(SortOptions::DESCENDING) => v.reverse(),
Ok(v) => v,
Err(e) => {
error_signal = Err(e);
Ordering::Less
}
});
error_signal?;
super::array::qsort(&mut values, &mut |a, b| {
compare(activation, *a, *b).map(|cmp| {
if cmp == Ordering::Equal {
unique_sort_satisfied = false;
Ordering::Equal
} else if options.contains(SortOptions::DESCENDING) {
cmp.reverse()
} else {
cmp
}
})
})?;
//NOTE: RETURNINDEXEDARRAY does NOT actually return anything useful.
//The actual sorting still happens, but the results are discarded.