diff options
| author | Henry <mail@henrygressmann.de> | 2026-08-25 18:05:11 +0200 |
|---|---|---|
| committer | Henry <mail@henrygressmann.de> | 2026-08-25 18:06:47 +0200 |
| commit | e5f234b1604511d0463252ee173caee6e9cd18f4 (patch) | |
| tree | 6bd77faa802b052a206851e69b4a60f393176572 /crates/types/src | |
| parent | 11052c6c2092db3388e33286ddba0f110b070bee (diff) | |
feat: add support for portable-atomic, add send feature
Signed-off-by: Henry <mail@henrygressmann.de>
Diffstat (limited to 'crates/types/src')
| -rw-r--r-- | crates/types/src/archive.rs | 21 | ||||
| -rw-r--r-- | crates/types/src/lib.rs | 28 | ||||
| -rw-r--r-- | crates/types/src/types.rs | 6 |
3 files changed, 36 insertions, 19 deletions
diff --git a/crates/types/src/archive.rs b/crates/types/src/archive.rs index b568155..db13ab0 100644 --- a/crates/types/src/archive.rs +++ b/crates/types/src/archive.rs @@ -66,10 +66,10 @@ mod tests { use super::*; use crate::{ AbstractHeapType, ConstInstruction, Global, GlobalType, Instruction, ModuleFuncIdx, ModuleInner, RefType, - V128Operand, WasmFunction, WasmType, + Shared, V128Operand, WasmFunction, WasmType, }; use crate::{OperandIdx, OperandType}; - use alloc::{boxed::Box, sync::Arc, vec}; + use alloc::boxed::Box; #[test] fn test_invalid_magic() { @@ -92,9 +92,9 @@ mod tests { let bytes = [0x00, 0x01, 0x02, 0x03, 0x7f, 0x80, 0xfe, 0xff, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x90]; let mut function = WasmFunction::default(); let constant = OperandIdx::new(0); - function.data.operands128 = vec![V128Operand { value: bytes }.encode()].into_boxed_slice(); - function.instructions = vec![Instruction::Const128(constant), Instruction::I8x16Shuffle(constant)].into(); - let module = Module::from(ModuleInner { funcs: Box::new([Arc::new(function)]), ..ModuleInner::default() }); + function.data.operands128 = Box::new([V128Operand { value: bytes }.encode()]); + function.instructions = Box::new([Instruction::Const128(constant), Instruction::I8x16Shuffle(constant)]); + let module = Module::from(ModuleInner { funcs: Box::new([Shared::new(function)]), ..ModuleInner::default() }); let archive = module.serialize_twasm().expect("serialize archive"); assert_eq!(&archive[..6], b"TWAS06"); @@ -114,21 +114,20 @@ mod tests { fn const_reference_expressions_round_trip_archive() { let null_type = RefType::new_abstract(true, AbstractHeapType::Extern); let module = Module::from(ModuleInner { - globals: vec![ + globals: Box::new([ Global { ty: GlobalType::new(WasmType::Ref(null_type), false), - init: vec![ConstInstruction::RefNull(null_type)].into_boxed_slice(), + init: Box::new([ConstInstruction::RefNull(null_type)]), }, Global { ty: GlobalType::new(WasmType::Ref(RefType::FUNCREF), false), - init: vec![ConstInstruction::RefFunc(ModuleFuncIdx::new(7))].into_boxed_slice(), + init: Box::new([ConstInstruction::RefFunc(ModuleFuncIdx::new(7))]), }, Global { ty: GlobalType::new(WasmType::Ref(RefType::new_abstract(false, AbstractHeapType::I31)), false), - init: vec![ConstInstruction::I32Const(42), ConstInstruction::RefI31].into_boxed_slice(), + init: Box::new([ConstInstruction::I32Const(42), ConstInstruction::RefI31]), }, - ] - .into_boxed_slice(), + ]), ..ModuleInner::default() }); diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs index 686433d..9f363d3 100644 --- a/crates/types/src/lib.rs +++ b/crates/types/src/lib.rs @@ -9,9 +9,27 @@ //! Types used by [`tinywasm`](https://docs.rs/tinywasm) and [`tinywasm_parser`](https://docs.rs/tinywasm_parser). extern crate alloc; -use alloc::{boxed::Box, sync::Arc}; +use alloc::boxed::Box; use core::ops::{Deref, Range}; +#[cfg(all(not(feature = "portable-atomic"), not(target_has_atomic = "ptr")))] +compile_error!( + "this target does not support atomic operations, enable the `portable-atomic` feature to use `portable-atomic`" +); + +/// An atomically reference-counted pointer. +#[cfg(not(feature = "portable-atomic"))] +pub use alloc::sync::Arc as Shared; +/// A non-owning pointer paired with [`Shared`]. +#[cfg(not(feature = "portable-atomic"))] +pub use alloc::sync::Weak as WeakShared; +/// An atomically reference-counted pointer. +#[cfg(feature = "portable-atomic")] +pub use portable_atomic_util::Arc as Shared; +/// A non-owning pointer paired with [`Shared`]. +#[cfg(feature = "portable-atomic")] +pub use portable_atomic_util::Weak as WeakShared; + // Memory defaults const MEM_PAGE_SIZE: u64 = 65536; const fn max_page_count(arch: MemoryArch, page_size: u64) -> u64 { @@ -56,11 +74,11 @@ pub mod archive { #[derive(Clone, Default, PartialEq)] #[cfg_attr(feature = "debug", derive(Debug))] #[cfg_attr(feature = "archive", derive(serde::Serialize, serde::Deserialize))] -pub struct Module(Arc<ModuleInner>); +pub struct Module(Shared<ModuleInner>); impl From<ModuleInner> for Module { fn from(inner: ModuleInner) -> Self { - Self(Arc::new(inner)) + Self(Shared::new(inner)) } } @@ -85,7 +103,7 @@ pub struct ModuleInner { /// Optimized WebAssembly functions /// /// Contains data from to the `code`, `func`, and `type` sections of the original WebAssembly module. - pub funcs: Box<[Arc<WasmFunction>]>, + pub funcs: Box<[Shared<WasmFunction>]>, /// The dense type definitions, indexed by `TypeAddr`. /// @@ -98,7 +116,7 @@ pub struct ModuleInner { /// Exported items of the WebAssembly module. /// /// Corresponds to the `export` section of the original WebAssembly module. - pub exports: Arc<[Export]>, + pub exports: Shared<[Export]>, /// Global components of the WebAssembly module. /// diff --git a/crates/types/src/types.rs b/crates/types/src/types.rs index 12386f1..273c762 100644 --- a/crates/types/src/types.rs +++ b/crates/types/src/types.rs @@ -1,6 +1,6 @@ -use alloc::{boxed::Box, sync::Arc}; +use alloc::boxed::Box; -use crate::{TypeAddr, WasmType}; +use crate::{Shared, TypeAddr, WasmType}; /// The dense type index space of a WebAssembly module. /// @@ -106,7 +106,7 @@ impl CompositeType { #[cfg_attr(feature = "debug", derive(Debug))] #[cfg_attr(feature = "archive", derive(serde::Serialize, serde::Deserialize))] pub struct FuncType { - data: Arc<[WasmType]>, + data: Shared<[WasmType]>, param_count: u16, } |
