Skip to content

Commit

Permalink
Merge pull request #4180 from wasmerio/native-engine-ext-deserialize-…
Browse files Browse the repository at this point in the history
…module

NativeEngineExt::deserialize now returns Module
  • Loading branch information
theduke authored Aug 31, 2023
2 parents 240157c + 719c917 commit 5ebbe65
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
23 changes: 13 additions & 10 deletions lib/api/src/sys/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ pub trait NativeEngineExt {
unsafe fn deserialize_from_mmapped_file_unchecked(
&self,
file_ref: &Path,
) -> Result<Arc<Artifact>, DeserializeError>;
) -> Result<crate::Module, DeserializeError>;

/// Load a serialized WebAssembly module from a memory mapped file and deserialize it.
///
Expand All @@ -105,7 +105,7 @@ pub trait NativeEngineExt {
unsafe fn deserialize_from_mmapped_file(
&self,
file_ref: &Path,
) -> Result<Arc<Artifact>, DeserializeError>;
) -> Result<crate::Module, DeserializeError>;
}

impl NativeEngineExt for crate::engine::Engine {
Expand Down Expand Up @@ -133,23 +133,26 @@ impl NativeEngineExt for crate::engine::Engine {
unsafe fn deserialize_from_mmapped_file_unchecked(
&self,
file_ref: &Path,
) -> Result<Arc<Artifact>, DeserializeError> {
) -> Result<crate::Module, DeserializeError> {
let bytes = std::fs::read(file_ref)?;
Ok(Arc::new(Artifact::deserialize_unchecked(
&self.0,
bytes.into(),
)?))
let artifact = Arc::new(Artifact::deserialize_unchecked(&self.0, bytes.into())?);
Ok(crate::Module(super::module::Module::from_artifact(
artifact,
)))
}

unsafe fn deserialize_from_mmapped_file(
&self,
file_ref: &Path,
) -> Result<Arc<Artifact>, DeserializeError> {
) -> Result<crate::Module, DeserializeError> {
let file = std::fs::File::open(file_ref)?;
Ok(Arc::new(Artifact::deserialize(
let artifact = Arc::new(Artifact::deserialize(
&self.0,
OwnedBuffer::from_file(&file)
.map_err(|e| DeserializeError::Generic(format!("{e:?}")))?,
)?))
)?);
Ok(crate::Module(super::module::Module::from_artifact(
artifact,
)))
}
}
2 changes: 1 addition & 1 deletion lib/api/src/sys/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl Module {
Ok(Self::from_artifact(artifact))
}

fn from_artifact(artifact: Arc<Artifact>) -> Self {
pub(super) fn from_artifact(artifact: Arc<Artifact>) -> Self {
Self { artifact }
}

Expand Down

0 comments on commit 5ebbe65

Please sign in to comment.