diff --git a/lib/runtime-core/src/import.rs b/lib/runtime-core/src/import.rs index 2661821be09..d0a714a5dae 100644 --- a/lib/runtime-core/src/import.rs +++ b/lib/runtime-core/src/import.rs @@ -81,44 +81,41 @@ impl ImportObject { self.map.get(namespace).map(|namespace| &**namespace) } - pub fn merged(mut imports_a: ImportObject, mut imports_b: ImportObject) -> Self { - let all_names: HashSet = imports_a - .map - .keys() - .chain(imports_b.map.keys()) - .cloned() - .collect(); - let mut combined_imports = ImportObject::new(); - for name in all_names { + pub fn merge(mut imports_a: ImportObject, mut imports_b: ImportObject) -> Self { + let names_a = imports_a.map.keys(); + let names_b = imports_b.map.keys(); + let names_ab: HashSet = names_a.chain(names_b).cloned().collect(); + let mut merged_imports = ImportObject::new(); + for name in names_ab { match (imports_a.map.remove(&name), imports_b.map.remove(&name)) { (Some(namespace_a), Some(namespace_b)) => { // Create a combined namespace - let mut combined_namespace = Namespace { - map: HashMap::new(), - }; + let mut namespace_ab = Namespace::new(); let mut exports_a = namespace_a.get_all_exports(); let mut exports_b = namespace_b.get_all_exports(); // Import from A will win over B - combined_namespace.map.extend(exports_b.drain().map( - |(export_name, export)| (export_name, Box::new(export) as Box), - )); - combined_namespace.map.extend(exports_a.drain().map( - |(export_name, export)| (export_name, Box::new(export) as Box), - )); - combined_imports + namespace_ab .map - .insert(name, Box::new(combined_namespace)); + .extend(exports_b.drain().map(|(export_name, export)| { + (export_name, Box::new(export) as Box) + })); + namespace_ab + .map + .extend(exports_a.drain().map(|(export_name, export)| { + (export_name, Box::new(export) as Box) + })); + merged_imports.map.insert(name, Box::new(namespace_ab)); } (Some(namespace_a), None) => { - combined_imports.map.insert(name, namespace_a); + merged_imports.map.insert(name, namespace_a); } (None, Some(namespace_b)) => { - combined_imports.map.insert(name, namespace_b); + merged_imports.map.insert(name, namespace_b); } (None, None) => panic!("Unreachable"), } } - combined_imports + merged_imports } } diff --git a/lib/runtime-core/src/instance.rs b/lib/runtime-core/src/instance.rs index 64a237b69c2..2e193b89098 100644 --- a/lib/runtime-core/src/instance.rs +++ b/lib/runtime-core/src/instance.rs @@ -423,10 +423,8 @@ impl LikeNamespace for Instance { .exports .iter() .map(|(name, export_index)| { - ( - name.to_string(), - self.inner.get_export_from_index(&self.module, export_index), - ) + let export = self.inner.get_export_from_index(&self.module, export_index); + (name.to_string(), export) }) .collect() }