Skip to content

Commit

Permalink
avm2: Also use avmplus' qsort in Vector.sort
Browse files Browse the repository at this point in the history
This was forgotten in #17846.
  • Loading branch information
moulins authored and torokati44 committed Sep 13, 2024
1 parent 24b0c8b commit e6e92dd
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
4 changes: 3 additions & 1 deletion core/src/avm2/globals/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down
29 changes: 14 additions & 15 deletions core/src/avm2/globals/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit e6e92dd

Please sign in to comment.