From 430ea402911855e8b8579f0e97b10699f22f758d Mon Sep 17 00:00:00 2001 From: Henry Date: Wed, 25 Mar 2026 23:41:50 +0100 Subject: chore: atomic module data Signed-off-by: Henry --- crates/types/src/instructions.rs | 4 +-- crates/types/src/lib.rs | 71 +++++++++++++++++++++++++++++++++------- 2 files changed, 61 insertions(+), 14 deletions(-) (limited to 'crates/types/src') diff --git a/crates/types/src/instructions.rs b/crates/types/src/instructions.rs index aa57dfa..ee99601 100644 --- a/crates/types/src/instructions.rs +++ b/crates/types/src/instructions.rs @@ -68,7 +68,7 @@ pub enum Instruction { Block(EndOffset), BlockWithType(ValType, EndOffset), BlockWithFuncType(TypeAddr, EndOffset), - + Loop(EndOffset), LoopWithType(ValType, EndOffset), LoopWithFuncType(TypeAddr, EndOffset), @@ -141,7 +141,7 @@ pub enum Instruction { RefNull(ValType), RefFunc(FuncAddr), RefIsNull, - + // > Numeric Instructions // See I32Eqz, I32Eq, I32Ne, I32LtS, I32LtU, I32GtS, I32GtU, I32LeS, I32LeU, I32GeS, I32GeU, diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs index beaa828..395c056 100644 --- a/crates/types/src/lib.rs +++ b/crates/types/src/lib.rs @@ -9,8 +9,11 @@ //! Types used by [`tinywasm`](https://docs.rs/tinywasm) and [`tinywasm_parser`](https://docs.rs/tinywasm_parser). extern crate alloc; -use alloc::boxed::Box; -use core::{fmt::Debug, ops::Range}; +use alloc::{boxed::Box, sync::Arc}; +use core::{ + fmt::Debug, + ops::{Deref, Range}, +}; // Memory defaults const MEM_PAGE_SIZE: u64 = 65536; @@ -73,47 +76,47 @@ pub struct TinyWasmModule { /// Optimized and validated WebAssembly functions /// /// Contains data from to the `code`, `func`, and `type` sections of the original WebAssembly module. - pub funcs: Box<[WasmFunction]>, + pub funcs: ArcSlice, /// A vector of type definitions, indexed by `TypeAddr` /// /// Corresponds to the `type` section of the original WebAssembly module. - pub func_types: Box<[FuncType]>, + pub func_types: ArcSlice, /// Exported items of the WebAssembly module. /// /// Corresponds to the `export` section of the original WebAssembly module. - pub exports: Box<[Export]>, + pub exports: ArcSlice, /// Global components of the WebAssembly module. /// /// Corresponds to the `global` section of the original WebAssembly module. - pub globals: Box<[Global]>, + pub globals: ArcSlice, /// Table components of the WebAssembly module used to initialize tables. /// /// Corresponds to the `table` section of the original WebAssembly module. - pub table_types: Box<[TableType]>, + pub table_types: ArcSlice, /// Memory components of the WebAssembly module used to initialize memories. /// /// Corresponds to the `memory` section of the original WebAssembly module. - pub memory_types: Box<[MemoryType]>, + pub memory_types: ArcSlice, /// Imports of the WebAssembly module. /// /// Corresponds to the `import` section of the original WebAssembly module. - pub imports: Box<[Import]>, + pub imports: ArcSlice, /// Data segments of the WebAssembly module. /// /// Corresponds to the `data` section of the original WebAssembly module. - pub data: Box<[Data]>, + pub data: ArcSlice, /// Element segments of the WebAssembly module. /// /// Corresponds to the `elem` section of the original WebAssembly module. - pub elements: Box<[Element]>, + pub elements: ArcSlice, } /// A WebAssembly External Kind. @@ -249,13 +252,57 @@ impl<'a, T: IntoIterator> From for ValueCountsSmall { #[derive(Debug, Clone, PartialEq, Default)] #[cfg_attr(feature = "archive", derive(serde::Serialize, serde::Deserialize))] pub struct WasmFunction { - pub instructions: Box<[Instruction]>, + pub instructions: ArcSlice, pub data: WasmFunctionData, pub locals: ValueCounts, pub params: ValueCountsSmall, pub ty: FuncType, } +#[derive(Clone, PartialEq)] +#[doc(hidden)] +// wrapper around Arc<[T]> to support serde serialization and deserialization +pub struct ArcSlice(pub Arc<[T]>); + +impl From> for ArcSlice { + fn from(vec: alloc::vec::Vec) -> Self { + Self(Arc::from(vec)) + } +} + +impl Default for ArcSlice { + fn default() -> Self { + Self(Arc::from([])) + } +} + +impl Deref for ArcSlice { + type Target = [T]; + + fn deref(&self) -> &Self::Target { + self.0.as_ref() + } +} + +impl Debug for ArcSlice { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + self.0.as_ref().fmt(f) + } +} + +impl serde::Serialize for ArcSlice { + fn serialize(&self, serializer: S) -> Result { + self.0.as_ref().serialize(serializer) + } +} + +impl<'de, T: serde::Deserialize<'de> + Debug> serde::Deserialize<'de> for ArcSlice { + fn deserialize>(deserializer: D) -> Result { + let vec: alloc::vec::Vec = alloc::vec::Vec::deserialize(deserializer)?; + Ok(Self(Arc::from(vec))) + } +} + #[derive(Debug, Clone, PartialEq, Eq, Default)] #[cfg_attr(feature = "archive", derive(serde::Serialize, serde::Deserialize))] pub struct WasmFunctionData { -- cgit v1.3.1