Skip to content

Commit

Permalink
Make Module::imports iterator return FuncType instead of `DedupFu…
Browse files Browse the repository at this point in the history
…ncType` (#583)

make Module::imports iterator return FuncType instead of DedupFuncType
  • Loading branch information
Robbepop authored Nov 29, 2022
1 parent 986fb4b commit 31a8272
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 21 deletions.
12 changes: 1 addition & 11 deletions crates/wasmi/src/engine/func_types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use super::{EngineIdx, Guarded};
use crate::{FuncType, Store};
use crate::FuncType;
use wasmi_arena::{DedupArena, GuardedEntity, Index};
use wasmi_core::ValueType;

/// A raw index to a function signature entity.
#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
Expand Down Expand Up @@ -49,15 +48,6 @@ impl DedupFuncType {
pub(super) fn into_inner(self) -> GuardedEntity<EngineIdx, DedupFuncTypeIdx> {
self.0
}

/// Creates a new function signature to the store.
pub fn new<T, I, O>(ctx: &mut Store<T>, inputs: I, outputs: O) -> Self
where
I: IntoIterator<Item = ValueType>,
O: IntoIterator<Item = ValueType>,
{
ctx.alloc_func_type(FuncType::new(inputs, outputs))
}
}

/// A [`FuncType`] registry that efficiently deduplicate stored function types.
Expand Down
5 changes: 3 additions & 2 deletions crates/wasmi/src/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,12 @@ impl<T> Linker<T> {
ModuleImportType::Func(expected_func_type) => {
let func = resolved.and_then(Extern::into_func).ok_or_else(make_err)?;
let actual_func_type = func.signature(&context);
let actual_func_type = context.store.resolve_func_type(actual_func_type);
if &actual_func_type != expected_func_type {
return Err(LinkerError::FuncTypeMismatch {
name: import.name().clone(),
expected: context.store.resolve_func_type(*expected_func_type),
actual: context.store.resolve_func_type(actual_func_type),
expected: expected_func_type.clone(),
actual: actual_func_type,
})
.map_err(Into::into);
}
Expand Down
6 changes: 3 additions & 3 deletions crates/wasmi/src/module/instantiate/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::ModuleImportType;
use crate::{
engine::DedupFuncType,
errors::{MemoryError, TableError},
global::GlobalError,
Extern,
FuncType,
Table,
};
use core::{fmt, fmt::Display};
Expand All @@ -25,9 +25,9 @@ pub enum InstantiationError {
/// Caused when a function has a mismatching signature.
SignatureMismatch {
/// The expected function signature for the function import.
expected: DedupFuncType,
expected: FuncType,
/// The actual function signature for the function import.
actual: DedupFuncType,
actual: FuncType,
},
/// Occurs when an imported table does not satisfy the required table type.
Table(TableError),
Expand Down
6 changes: 5 additions & 1 deletion crates/wasmi/src/module/instantiate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::{
Error,
Extern,
FuncEntity,
FuncType,
Global,
Instance,
InstanceEntity,
Expand Down Expand Up @@ -121,14 +122,17 @@ impl Module {
match (import.item_type(), external) {
(ModuleImportType::Func(expected_signature), Extern::Func(func)) => {
let actual_signature = func.signature(context.as_context());
let actual_signature = self
.engine
.resolve_func_type(actual_signature, FuncType::clone);
// Note: We can compare function signatures without resolving them because
// we deduplicate them before registering. Therefore two equal instances of
// [`SignatureEntity`] will be associated to the same [`Signature`].
if &actual_signature != expected_signature {
// Note: In case of error we could resolve the signatures for better error readability.
return Err(InstantiationError::SignatureMismatch {
actual: actual_signature,
expected: *expected_signature,
expected: expected_signature.clone(),
});
}
builder.push_func(func);
Expand Down
12 changes: 8 additions & 4 deletions crates/wasmi/src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ use crate::{
engine::{DedupFuncType, FuncBody},
Engine,
Error,
FuncType,
GlobalType,
MemoryType,
TableType,
Expand Down Expand Up @@ -182,6 +183,7 @@ impl Module {
let len_imported_funcs = self.imports.len_funcs;
let len_imported_globals = self.imports.len_globals;
ModuleImportsIter {
engine: &self.engine,
names: self.imports.items.iter(),
funcs: self.funcs[..len_imported_funcs].iter(),
tables: self.tables.iter(),
Expand Down Expand Up @@ -228,6 +230,7 @@ impl Module {
/// An iterator over the imports of a [`Module`].
#[derive(Debug)]
pub struct ModuleImportsIter<'a> {
engine: &'a Engine,
names: SliceIter<'a, Imported>,
funcs: SliceIter<'a, DedupFuncType>,
tables: SliceIter<'a, TableType>,
Expand All @@ -246,7 +249,8 @@ impl<'a> Iterator for ModuleImportsIter<'a> {
let func_type = self.funcs.next().unwrap_or_else(|| {
panic!("unexpected missing imported function for {name:?}")
});
ModuleImport::new(name, *func_type)
let func_type = self.engine.resolve_func_type(*func_type, FuncType::clone);
ModuleImport::new(name, func_type)
}
Imported::Table(name) => {
let table_type = self.tables.next().unwrap_or_else(|| {
Expand Down Expand Up @@ -330,7 +334,7 @@ pub enum ModuleImportType {
/// An imported [`Func`].
///
/// [`Func`]: [`crate::Func`]
Func(DedupFuncType),
Func(FuncType),
/// An imported [`Table`].
///
/// [`Table`]: [`crate::Table`]
Expand All @@ -345,8 +349,8 @@ pub enum ModuleImportType {
Global(GlobalType),
}

impl From<DedupFuncType> for ModuleImportType {
fn from(func_type: DedupFuncType) -> Self {
impl From<FuncType> for ModuleImportType {
fn from(func_type: FuncType) -> Self {
Self::Func(func_type)
}
}
Expand Down

0 comments on commit 31a8272

Please sign in to comment.