From cfaffd0ac27e7db7c13f35c64f5a4c7a61816e96 Mon Sep 17 00:00:00 2001 From: Christof Petig Date: Sun, 18 Jun 2023 19:38:00 +0200 Subject: [PATCH] implement up to two slices in write_bytes (gather) --- common/state/src/plugin/memory_manager.rs | 24 +++++++++++++++-------- common/state/src/plugin/module.rs | 22 +-------------------- 2 files changed, 17 insertions(+), 29 deletions(-) diff --git a/common/state/src/plugin/memory_manager.rs b/common/state/src/plugin/memory_manager.rs index f811bd7818..e6332a702a 100644 --- a/common/state/src/plugin/memory_manager.rs +++ b/common/state/src/plugin/memory_manager.rs @@ -147,7 +147,10 @@ impl MemoryManager { store, memory, allocator, - &bincode::serialize(object).map_err(PluginModuleError::Encoding)?, + ( + &bincode::serialize(object).map_err(PluginModuleError::Encoding)?, + &[], + ), ) } @@ -184,7 +187,7 @@ impl MemoryManager { ::Offset, WasmPtr, >, - bytes: &[u8], + bytes: (&[u8], &[u8]), ) -> Result< ( WasmPtr, @@ -192,15 +195,22 @@ impl MemoryManager { ), PluginModuleError, > { - let len = bytes.len() as ::Offset; + let len = (bytes.0.len() + bytes.1.len()) as ::Offset; let ptr = self .get_pointer(store, len, allocator) .map_err(PluginModuleError::MemoryAllocation)?; ptr.slice( &memory.view(store), - bytes.len() as ::Offset, + len as ::Offset, ) - .and_then(|s| s.write_slice(bytes)) + .and_then(|s| { + if !bytes.1.is_empty() { + s.subslice(0..bytes.0.len() as u64).write_slice(bytes.0)?; + s.subslice(bytes.0.len() as u64..len).write_slice(bytes.1) + } else { + s.write_slice(bytes.0) + } + }) .map_err(|_| PluginModuleError::InvalidPointer)?; Ok((ptr, len)) } @@ -221,9 +231,7 @@ impl MemoryManager { bytes: &[u8], ) -> Result, PluginModuleError> { let len = bytes.len() as ::Offset; - let new_bytes = [&len.to_le_bytes(), bytes].concat(); - // TODO: could make write_bytes take an IntoIterator to avoid this concat? - self.write_bytes(store, memory, allocator, &new_bytes) + self.write_bytes(store, memory, allocator, (&len.to_le_bytes(), &bytes)) .map(|val| val.0) } } diff --git a/common/state/src/plugin/module.rs b/common/state/src/plugin/module.rs index b1a471fc76..54a1533068 100644 --- a/common/state/src/plugin/module.rs +++ b/common/state/src/plugin/module.rs @@ -223,7 +223,7 @@ fn execute_raw( &mut module.store.as_store_mut(), &module.memory, &module.allocator, - bytes, + (bytes, &[]), )?; // This gets the event function from module exports @@ -270,26 +270,6 @@ fn execute_raw( result_len, )?; Ok(bytes) - // Waiting for `multi-value` to be added to LLVM. So we encode a pointer to - // a u128 that represent [u64; 2] - - // let u128_pointer = from_i64( - // function_result[0] - // .i64() - // .ok_or_else(PluginModuleError::InvalidArgumentType)?, - // ); - - // let bytes = memory_manager::read_bytes(&module.memory, u128_pointer, 16); - - // We read the return object and deserialize it - - // The first 8 bytes are encoded as le and represent the pointer to the data - // The next 8 bytes are encoded as le and represent the length of the data - // Ok(memory_manager::read_bytes( - // &module.memory, - // u64::from_le_bytes(bytes[0..8].try_into().unwrap()), - // u64::from_le_bytes(bytes[8..16].try_into().unwrap()), - // )) } fn retrieve_action(