Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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