Skip to content

Commit

Permalink
Cleaning up names, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim Perkins committed Mar 7, 2019
1 parent a01b4e9 commit c5bd9aa
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
43 changes: 20 additions & 23 deletions lib/runtime-core/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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<String> = 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<IsExport>),
));
combined_namespace.map.extend(exports_a.drain().map(
|(export_name, export)| (export_name, Box::new(export) as Box<IsExport>),
));
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<IsExport>)
}));
namespace_ab
.map
.extend(exports_a.drain().map(|(export_name, export)| {
(export_name, Box::new(export) as Box<IsExport>)
}));
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
}
}

Expand Down
6 changes: 2 additions & 4 deletions lib/runtime-core/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down

0 comments on commit c5bd9aa

Please sign in to comment.