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
1 change: 1 addition & 0 deletions crates/wit-component/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub use encoding::{ComponentEncoder, LibraryInfo, encode};
pub use linking::Linker;
pub use printing::*;
pub use targets::*;
pub use validation::AdapterModuleDidNotExport;
pub use wit_parser::decoding::{DecodedWasm, decode, decode_reader};

pub mod metadata;
Expand Down
20 changes: 19 additions & 1 deletion crates/wit-component/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::encoding::{Instance, Item, LibraryInfo, MainOrAdapter, ModuleImportMa
use crate::{ComponentEncoder, StringEncoding};
use anyhow::{Context, Result, anyhow, bail};
use indexmap::{IndexMap, IndexSet, map::Entry};
use std::fmt;
use std::hash::{Hash, Hasher};
use std::mem;
use wasm_encoder::ExportKind;
Expand Down Expand Up @@ -2523,14 +2524,31 @@ pub fn validate_adapter_module(
for (name, required_ty) in required_by_import {
let actual = match ret.exports.raw_exports.get(name) {
Some(ty) => ty,
None => bail!("adapter module did not export `{name}`"),
None => return Err(AdapterModuleDidNotExport(name.clone()).into()),
};
validate_func_sig(name, required_ty, &actual)?;
}

Ok(ret)
}

/// An error that can be returned from adapting a core Wasm module into a
/// component using an adapter module.
///
/// If the core Wasm module contained an import that it requires to be
/// satisfied by the adapter, and the adapter does not contain an export
/// with the same name, an instance of this error is returned.
#[derive(Debug, Clone)]
pub struct AdapterModuleDidNotExport(String);

impl fmt::Display for AdapterModuleDidNotExport {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "adapter module did not export `{}`", self.0)
}
}

impl std::error::Error for AdapterModuleDidNotExport {}

fn resource_test_for_interface<'a>(
resolve: &'a Resolve,
id: InterfaceId,
Expand Down