Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make wasmi::Module implement Clone #1130

Merged
merged 1 commit into from
Jul 13, 2024
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
11 changes: 7 additions & 4 deletions crates/wasmi/src/module/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use super::{
ModuleHeader,
ModuleHeaderInner,
ModuleImports,
ModuleInner,
};
use crate::{
collections::Map,
Expand Down Expand Up @@ -409,10 +410,12 @@ impl ModuleBuilder {
/// Finishes construction of the WebAssembly [`Module`].
pub fn finish(self, engine: &Engine) -> Module {
Module {
engine: engine.clone(),
header: self.header,
data_segments: self.data_segments.finish(),
custom_sections: self.custom_sections.finish(),
inner: Arc::new(ModuleInner {
engine: engine.clone(),
header: self.header,
data_segments: self.data_segments.finish(),
custom_sections: self.custom_sections.finish(),
}),
}
}
}
2 changes: 1 addition & 1 deletion crates/wasmi/src/module/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
/// Creates a new [`ModuleExportsIter`] from the given [`Module`].
pub(super) fn new(module: &'module Module) -> Self {
Self {
exports: module.header.inner.exports.iter(),
exports: module.module_header().exports.iter(),

Check warning on line 139 in crates/wasmi/src/module/export.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/src/module/export.rs#L139

Added line #L139 was not covered by tests
module,
}
}
Expand Down
8 changes: 4 additions & 4 deletions crates/wasmi/src/module/instantiate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ impl Module {

/// Extracts the Wasm exports from the module and registers them into the [`Instance`].
fn extract_exports(&self, builder: &mut InstanceEntityBuilder) {
for (field, idx) in &self.header.inner.exports {
for (field, idx) in &self.module_header().exports {
let external = match idx {
export::ExternIdx::Func(func_index) => {
let func_index = func_index.into_u32();
Expand Down Expand Up @@ -288,7 +288,7 @@ impl Module {

/// Extracts the optional start function for the build instance.
fn extract_start_fn(&self, builder: &mut InstanceEntityBuilder) {
if let Some(start_fn) = self.header.inner.start {
if let Some(start_fn) = self.module_header().start {
builder.set_start(start_fn)
}
}
Expand All @@ -299,7 +299,7 @@ impl Module {
mut context: impl AsContextMut,
builder: &mut InstanceEntityBuilder,
) -> Result<(), Error> {
for segment in &self.header.inner.element_segments[..] {
for segment in &self.module_header().element_segments[..] {
let element = ElementSegment::new(context.as_context_mut(), segment);
if let ElementSegmentKind::Active(active) = segment.kind() {
let dst_index = u32::from(Self::eval_init_expr(
Expand Down Expand Up @@ -346,7 +346,7 @@ impl Module {
mut context: impl AsContextMut,
builder: &mut InstanceEntityBuilder,
) -> Result<(), Error> {
for segment in &self.data_segments {
for segment in &self.inner.data_segments {
let segment = match segment {
InitDataSegment::Active {
memory_index,
Expand Down
77 changes: 47 additions & 30 deletions crates/wasmi/src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,14 @@
use wasmparser::{FuncValidatorAllocations, Parser, ValidPayload, Validator};

/// A parsed and validated WebAssembly module.
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Module {
inner: Arc<ModuleInner>,
}

/// The internal data of a [`Module`].
#[derive(Debug)]
struct ModuleInner {
engine: Engine,
header: ModuleHeader,
data_segments: DataSegments,
Expand Down Expand Up @@ -282,7 +288,12 @@

/// Returns the [`Engine`] used during creation of the [`Module`].
pub fn engine(&self) -> &Engine {
&self.engine
&self.inner.engine
}

/// Returns a shared reference to the [`ModuleHeaderInner`].
fn module_header(&self) -> &ModuleHeaderInner {
&self.inner.header.inner
}

/// Validates `wasm` as a WebAssembly binary given the configuration (via [`Config`]) in `engine`.
Expand Down Expand Up @@ -320,19 +331,19 @@

/// Returns the number of non-imported functions of the [`Module`].
pub(crate) fn len_funcs(&self) -> usize {
self.header.inner.funcs.len()
self.module_header().funcs.len()
}
/// Returns the number of non-imported tables of the [`Module`].
pub(crate) fn len_tables(&self) -> usize {
self.header.inner.tables.len()
self.module_header().tables.len()
}
/// Returns the number of non-imported linear memories of the [`Module`].
pub(crate) fn len_memories(&self) -> usize {
self.header.inner.memories.len()
self.module_header().memories.len()
}
/// Returns the number of non-imported global variables of the [`Module`].
pub(crate) fn len_globals(&self) -> usize {
self.header.inner.globals.len()
self.module_header().globals.len()
}

/// Returns a slice to the function types of the [`Module`].
Expand All @@ -341,33 +352,35 @@
///
/// The slice is stored in a `Arc` so that this operation is very cheap.
pub(crate) fn func_types_cloned(&self) -> Arc<[DedupFuncType]> {
self.header.inner.func_types.clone()
self.module_header().func_types.clone()
}

/// Returns an iterator over the imports of the [`Module`].
pub fn imports(&self) -> ModuleImportsIter {
let len_imported_funcs = self.header.inner.imports.len_funcs;
let len_imported_globals = self.header.inner.imports.len_globals;
let header = self.module_header();
let len_imported_funcs = header.imports.len_funcs;
let len_imported_globals = header.imports.len_globals;
ModuleImportsIter {
engine: self.engine(),
names: self.header.inner.imports.items.iter(),
funcs: self.header.inner.funcs[..len_imported_funcs].iter(),
tables: self.header.inner.tables.iter(),
memories: self.header.inner.memories.iter(),
globals: self.header.inner.globals[..len_imported_globals].iter(),
names: header.imports.items.iter(),
funcs: header.funcs[..len_imported_funcs].iter(),
tables: header.tables.iter(),
memories: header.memories.iter(),
globals: header.globals[..len_imported_globals].iter(),
}
}

/// Returns an iterator over the internally defined [`Func`].
///
/// [`Func`]: [`crate::Func`]
pub(crate) fn internal_funcs(&self) -> InternalFuncsIter {
let len_imported = self.header.inner.imports.len_funcs;
let header = self.module_header();
let len_imported = header.imports.len_funcs;
// We skip the first `len_imported` elements in `funcs`
// since they refer to imported and not internally defined
// functions.
let funcs = &self.header.inner.funcs[len_imported..];
let engine_funcs = self.header.inner.engine_funcs.iter();
let funcs = &header.funcs[len_imported..];
let engine_funcs = header.engine_funcs.iter();
assert_eq!(funcs.len(), engine_funcs.len());
InternalFuncsIter {
iter: funcs.iter().zip(engine_funcs),
Expand All @@ -376,32 +389,35 @@

/// Returns an iterator over the [`MemoryType`] of internal linear memories.
fn internal_memories(&self) -> SliceIter<MemoryType> {
let len_imported = self.header.inner.imports.len_memories;
let header = self.module_header();
let len_imported = header.imports.len_memories;
// We skip the first `len_imported` elements in `memories`
// since they refer to imported and not internally defined
// linear memories.
let memories = &self.header.inner.memories[len_imported..];
let memories = &header.memories[len_imported..];
memories.iter()
}

/// Returns an iterator over the [`TableType`] of internal tables.
fn internal_tables(&self) -> SliceIter<TableType> {
let len_imported = self.header.inner.imports.len_tables;
let header = self.module_header();
let len_imported = header.imports.len_tables;
// We skip the first `len_imported` elements in `memories`
// since they refer to imported and not internally defined
// linear memories.
let tables = &self.header.inner.tables[len_imported..];
let tables = &header.tables[len_imported..];
tables.iter()
}

/// Returns an iterator over the internally defined [`Global`].
fn internal_globals(&self) -> InternalGlobalsIter {
let len_imported = self.header.inner.imports.len_globals;
let header = self.module_header();
let len_imported = header.imports.len_globals;
// We skip the first `len_imported` elements in `globals`
// since they refer to imported and not internally defined
// global variables.
let globals = self.header.inner.globals[len_imported..].iter();
let global_inits = self.header.inner.globals_init.iter();
let globals = header.globals[len_imported..].iter();
let global_inits = header.globals_init.iter();
InternalGlobalsIter {
iter: globals.zip(global_inits),
}
Expand All @@ -420,7 +436,7 @@
///
/// This function will return the type of an export with the given `name`.
pub fn get_export(&self, name: &str) -> Option<ExternType> {
let idx = self.header.inner.exports.get(name).copied()?;
let idx = self.module_header().exports.get(name).copied()?;

Check warning on line 439 in crates/wasmi/src/module/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/src/module/mod.rs#L439

Added line #L439 was not covered by tests
let ty = self.get_extern_type(idx);
Some(ty)
}
Expand All @@ -431,22 +447,23 @@
///
/// This function assumes that the given [`ExternType`] is valid.
fn get_extern_type(&self, idx: ExternIdx) -> ExternType {
let header = self.module_header();

Check warning on line 450 in crates/wasmi/src/module/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/src/module/mod.rs#L450

Added line #L450 was not covered by tests
match idx {
ExternIdx::Func(index) => {
let dedup = &self.header.inner.funcs[index.into_u32() as usize];
let dedup = &header.funcs[index.into_u32() as usize];

Check warning on line 453 in crates/wasmi/src/module/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/src/module/mod.rs#L453

Added line #L453 was not covered by tests
let func_type = self.engine().resolve_func_type(dedup, Clone::clone);
ExternType::Func(func_type)
}
ExternIdx::Table(index) => {
let table_type = self.header.inner.tables[index.into_u32() as usize];
let table_type = header.tables[index.into_u32() as usize];

Check warning on line 458 in crates/wasmi/src/module/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/src/module/mod.rs#L458

Added line #L458 was not covered by tests
ExternType::Table(table_type)
}
ExternIdx::Memory(index) => {
let memory_type = self.header.inner.memories[index.into_u32() as usize];
let memory_type = header.memories[index.into_u32() as usize];

Check warning on line 462 in crates/wasmi/src/module/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/src/module/mod.rs#L462

Added line #L462 was not covered by tests
ExternType::Memory(memory_type)
}
ExternIdx::Global(index) => {
let global_type = self.header.inner.globals[index.into_u32() as usize];
let global_type = header.globals[index.into_u32() as usize];

Check warning on line 466 in crates/wasmi/src/module/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/src/module/mod.rs#L466

Added line #L466 was not covered by tests
ExternType::Global(global_type)
}
}
Expand All @@ -463,7 +480,7 @@
/// [`Config::ignore_custom_sections`]: crate::Config::ignore_custom_sections
#[inline]
pub fn custom_sections(&self) -> CustomSectionsIter {
self.custom_sections.iter()
self.inner.custom_sections.iter()

Check warning on line 483 in crates/wasmi/src/module/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/wasmi/src/module/mod.rs#L483

Added line #L483 was not covered by tests
}
}

Expand Down
Loading