Add sorted_iter_mut for NonstandardWitSection.adapters for determinism (#3851)

This commit is contained in:
Aaron Hill 2024-02-22 11:06:13 -05:00 committed by GitHub
parent 00ab174fcc
commit b5a74c8578
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 18 additions and 3 deletions

View File

@ -11,6 +11,11 @@
* Add bindings for `CanvasState.reset()`, affecting `CanvasRenderingContext2D` and `OffscreenCanvasRenderingContext2D`.
[#3844](https://github.com/rustwasm/wasm-bindgen/pull/3844)
### Fixed
* Make .wasm output deterministic when using `--reference-types`.
[#3851](https://github.com/rustwasm/wasm-bindgen/pull/3851)
--------------------------------------------------------------------------------
## [0.2.91](https://github.com/rustwasm/wasm-bindgen/compare/0.2.90...0.2.91)

View File

@ -25,7 +25,7 @@ pub fn process(module: &mut Module) -> Result<()> {
// Transform all exported functions in the module, using the bindings listed
// for each exported function.
for (id, adapter) in section.adapters.iter_mut() {
for (id, adapter) in crate::sorted_iter_mut(&mut section.adapters) {
let instructions = match &mut adapter.kind {
AdapterKind::Local { instructions } => instructions,
AdapterKind::Import { .. } => continue,
@ -77,7 +77,7 @@ pub fn process(module: &mut Module) -> Result<()> {
// Additionally we may need to update some adapter instructions other than
// those found for the externref pass. These are some general "fringe support"
// things necessary to get absolutely everything working.
for (_, adapter) in section.adapters.iter_mut() {
for (_, adapter) in crate::sorted_iter_mut(&mut section.adapters) {
let instrs = match &mut adapter.kind {
AdapterKind::Local { instructions } => instructions,
AdapterKind::Import { .. } => continue,

View File

@ -785,3 +785,13 @@ where
pairs.sort_by_key(|(k, _)| *k);
pairs.into_iter()
}
/// Like `sorted_iter`, but produces mutable references to the values
fn sorted_iter_mut<K, V>(map: &mut HashMap<K, V>) -> impl Iterator<Item = (&K, &mut V)>
where
K: Ord,
{
let mut pairs = map.iter_mut().collect::<Vec<_>>();
pairs.sort_by_key(|(k, _)| *k);
pairs.into_iter()
}

View File

@ -14,7 +14,7 @@ pub fn run(module: &mut Module) -> Result<(), Error> {
let mut to_xform = Vec::new();
let mut slots = Vec::new();
for (_, adapter) in adapters.adapters.iter_mut() {
for (_, adapter) in crate::sorted_iter_mut(&mut adapters.adapters) {
extract_xform(module, adapter, &mut to_xform, &mut slots);
}
if to_xform.is_empty() {