Made Module::serialize to gives Bytes instead of Vec

This commit is contained in:
ptitSeb
2022-09-01 17:18:13 +02:00
parent 436bec735b
commit 3528555869
3 changed files with 9 additions and 11 deletions

View File

@@ -105,7 +105,7 @@ pub struct Module {
// WebAssembly type hints // WebAssembly type hints
type_hints: Option<ModuleTypeHints>, type_hints: Option<ModuleTypeHints>,
#[cfg(feature = "js-serializable-module")] #[cfg(feature = "js-serializable-module")]
raw_bytes: Option<Vec<u8>>, raw_bytes: Option<Bytes>,
} }
impl Module { impl Module {
@@ -209,7 +209,7 @@ impl Module {
_store: &impl AsStoreRef, _store: &impl AsStoreRef,
binary: &[u8], binary: &[u8],
) -> Result<Self, CompileError> { ) -> Result<Self, CompileError> {
let js_bytes = Uint8Array::view(&binary[..]); let js_bytes = Uint8Array::view(binary);
let module = WebAssembly::Module::new(&js_bytes.into()).unwrap(); let module = WebAssembly::Module::new(&js_bytes.into()).unwrap();
// The module is now validated, so we can safely parse it's types // The module is now validated, so we can safely parse it's types
@@ -241,7 +241,7 @@ impl Module {
type_hints, type_hints,
name, name,
#[cfg(feature = "js-serializable-module")] #[cfg(feature = "js-serializable-module")]
raw_bytes: Some(binary.to_vec()), raw_bytes: Some(binary),
}) })
} }
@@ -252,8 +252,7 @@ impl Module {
/// WebAssembly features in the Store Engine to assure deterministic /// WebAssembly features in the Store Engine to assure deterministic
/// validation of the Module. /// validation of the Module.
pub fn validate(_store: &impl AsStoreRef, binary: &[u8]) -> Result<(), CompileError> { pub fn validate(_store: &impl AsStoreRef, binary: &[u8]) -> Result<(), CompileError> {
let binary = binary.into_bytes(); let js_bytes = unsafe { Uint8Array::view(binary) };
let js_bytes = unsafe { Uint8Array::view(&binary[..]) };
match WebAssembly::validate(&js_bytes.into()) { match WebAssembly::validate(&js_bytes.into()) {
Ok(true) => Ok(()), Ok(true) => Ok(()),
_ => Err(CompileError::Validate("Invalid Wasm file".to_owned())), _ => Err(CompileError::Validate("Invalid Wasm file".to_owned())),
@@ -306,7 +305,7 @@ impl Module {
/// can later process via [`Module::deserialize`]. /// can later process via [`Module::deserialize`].
/// ///
#[cfg(feature = "js-serializable-module")] #[cfg(feature = "js-serializable-module")]
pub fn serialize(&self) -> Result<Vec<u8>, SerializeError> { pub fn serialize(&self) -> Result<Bytes, SerializeError> {
self.raw_bytes.clone().ok_or(SerializeError::Generic( self.raw_bytes.clone().ok_or(SerializeError::Generic(
"Not able to serialize module".to_string(), "Not able to serialize module".to_string(),
)) ))

View File

@@ -226,11 +226,10 @@ impl Module {
#[cfg(feature = "compiler")] #[cfg(feature = "compiler")]
fn compile(store: &impl AsStoreRef, binary: &[u8]) -> Result<Self, CompileError> { fn compile(store: &impl AsStoreRef, binary: &[u8]) -> Result<Self, CompileError> {
let binary = binary.into_bytes();
let artifact = store let artifact = store
.as_store_ref() .as_store_ref()
.engine() .engine()
.compile(&binary[..], store.as_store_ref().tunables())?; .compile(binary, store.as_store_ref().tunables())?;
Ok(Self::from_artifact(artifact)) Ok(Self::from_artifact(artifact))
} }
@@ -248,8 +247,8 @@ impl Module {
/// # Ok(()) /// # Ok(())
/// # } /// # }
/// ``` /// ```
pub fn serialize(&self) -> Result<Vec<u8>, SerializeError> { pub fn serialize(&self) -> Result<Bytes, SerializeError> {
self.artifact.serialize() self.artifact.serialize().map(|bytes| bytes.into())
} }
/// Serializes a module into a file that the `Engine` /// Serializes a module into a file that the `Engine`

View File

@@ -475,7 +475,7 @@ pub unsafe extern "C" fn wasm_module_deserialize(
#[no_mangle] #[no_mangle]
pub unsafe extern "C" fn wasm_module_serialize(module: &wasm_module_t, out: &mut wasm_byte_vec_t) { pub unsafe extern "C" fn wasm_module_serialize(module: &wasm_module_t, out: &mut wasm_byte_vec_t) {
let byte_vec = c_try!(module.inner.serialize(); otherwise ()); let byte_vec = c_try!(module.inner.serialize(); otherwise ());
out.set_buffer(byte_vec); out.set_buffer(byte_vec.to_vec());
} }
#[cfg(test)] #[cfg(test)]