wip: check for missing functions when linking

This commit is contained in:
zach
2024-12-02 16:15:16 -08:00
parent 91b7cd7d3a
commit 097079fab9

View File

@@ -1,6 +1,6 @@
use std::{
any::Any,
collections::{BTreeMap, BTreeSet},
collections::{BTreeMap, BTreeSet, HashMap},
};
use anyhow::Context;
@@ -253,6 +253,48 @@ fn add_module<T: 'static>(
return Ok(());
}
let mut imports: HashMap<&str, Vec<(&str, ExternType)>> = HashMap::new();
for module_import in module.imports() {
let import_module = module_import.module();
let import_name = module_import.name();
let import_type = module_import.ty();
if imports.contains_key(import_module) {
imports
.get_mut(import_module)
.unwrap()
.push((import_name, import_type));
} else {
imports.insert(import_module, vec![(import_name, import_type)]);
}
}
for (m, v) in imports.into_iter() {
if let Some(src) = modules.get(m) {
for (name, ty) in v {
match src.get_export(name) {
None => anyhow::bail!("missing import: {m}::{name}"),
Some(ex) => match (&ex, &ty) {
(ExternType::Func(a), ExternType::Func(b)) => {
if !a.matches(b) {
anyhow::bail!(
"import type mismatch {m}::{name}, got {ty:?} expected {ex:?}"
)
}
}
(ExternType::Global(a), ExternType::Global(b)) => {
if !a.content().matches(&b.content()) {
anyhow::bail!(
"import type mismatch {m}::{name}, got {ty:?} expected {ex:?}"
)
}
}
_ => (), // todo: table/memory types,
},
}
}
}
}
for import in module.imports() {
let module = import.module();