|
| 1 | +// Copyright (c) Zefchain Labs, Inc. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//! Implementations of the custom traits for slice types. |
| 5 | +
|
| 6 | +use std::{borrow::Cow, ops::Deref, rc::Rc, sync::Arc}; |
| 7 | + |
| 8 | +use frunk::{hlist, hlist_pat, HList}; |
| 9 | + |
| 10 | +use crate::{ |
| 11 | + GuestPointer, InstanceWithMemory, Layout, Memory, Runtime, RuntimeError, RuntimeMemory, |
| 12 | + WitLoad, WitStore, WitType, |
| 13 | +}; |
| 14 | + |
| 15 | +/// A macro to implement [`WitType`], [`WitLoad`] and [`WitStore`] for a slice wrapper type. |
| 16 | +/// |
| 17 | +/// This assumes that: |
| 18 | +/// - The type is `$wrapper<[T]>` |
| 19 | +/// - The type implements `From<Box<[T]>>` |
| 20 | +/// - The type implements `Deref<Target = [T]>` |
| 21 | +macro_rules! impl_wit_traits_for_slice_wrapper { |
| 22 | + ($wrapper:ident) => { |
| 23 | + impl_wit_type_as_slice!($wrapper); |
| 24 | + impl_wit_store_as_slice!($wrapper); |
| 25 | + impl_wit_load_as_boxed_slice!($wrapper); |
| 26 | + }; |
| 27 | +} |
| 28 | + |
| 29 | +/// A macro to implement [`WitType`] for a slice wrapper type. |
| 30 | +/// |
| 31 | +/// This assumes that: |
| 32 | +/// - The type is `$wrapper<[T]>` |
| 33 | +macro_rules! impl_wit_type_as_slice { |
| 34 | + ($wrapper:ident) => { |
| 35 | + impl<T> WitType for $wrapper<[T]> |
| 36 | + where |
| 37 | + T: WitType, |
| 38 | + { |
| 39 | + const SIZE: u32 = <[T] as WitType>::SIZE; |
| 40 | + |
| 41 | + type Layout = <[T] as WitType>::Layout; |
| 42 | + type Dependencies = <[T] as WitType>::Dependencies; |
| 43 | + |
| 44 | + fn wit_type_name() -> Cow<'static, str> { |
| 45 | + <[T] as WitType>::wit_type_name() |
| 46 | + } |
| 47 | + |
| 48 | + fn wit_type_declaration() -> Cow<'static, str> { |
| 49 | + <[T] as WitType>::wit_type_declaration() |
| 50 | + } |
| 51 | + } |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +/// A macro to implement [`WitStore`] for a slice wrapper type. |
| 56 | +/// |
| 57 | +/// This assumes that: |
| 58 | +/// - The type is `$wrapper<[T]>` |
| 59 | +/// - The type implements `Deref<Target = [T]>` |
| 60 | +macro_rules! impl_wit_store_as_slice { |
| 61 | + ($wrapper:ident) => { |
| 62 | + impl<T> WitStore for $wrapper<[T]> |
| 63 | + where |
| 64 | + T: WitStore, |
| 65 | + { |
| 66 | + fn store<Instance>( |
| 67 | + &self, |
| 68 | + memory: &mut Memory<'_, Instance>, |
| 69 | + location: GuestPointer, |
| 70 | + ) -> Result<(), RuntimeError> |
| 71 | + where |
| 72 | + Instance: InstanceWithMemory, |
| 73 | + <Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>, |
| 74 | + { |
| 75 | + self.deref().store(memory, location) |
| 76 | + } |
| 77 | + |
| 78 | + fn lower<Instance>( |
| 79 | + &self, |
| 80 | + memory: &mut Memory<'_, Instance>, |
| 81 | + ) -> Result<Self::Layout, RuntimeError> |
| 82 | + where |
| 83 | + Instance: InstanceWithMemory, |
| 84 | + <Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>, |
| 85 | + { |
| 86 | + self.deref().lower(memory) |
| 87 | + } |
| 88 | + } |
| 89 | + }; |
| 90 | +} |
| 91 | + |
| 92 | +/// A macro to implement [`WitLoad`] for a slice wrapper type. |
| 93 | +/// |
| 94 | +/// This assumes that: |
| 95 | +/// - The type is `$wrapper<[T]>` |
| 96 | +/// - The type implements `From<Box<[T]>>` |
| 97 | +macro_rules! impl_wit_load_as_boxed_slice { |
| 98 | + ($wrapper:ident) => { |
| 99 | + impl<T> WitLoad for $wrapper<[T]> |
| 100 | + where |
| 101 | + T: WitLoad, |
| 102 | + { |
| 103 | + fn load<Instance>( |
| 104 | + memory: &Memory<'_, Instance>, |
| 105 | + location: GuestPointer, |
| 106 | + ) -> Result<Self, RuntimeError> |
| 107 | + where |
| 108 | + Instance: InstanceWithMemory, |
| 109 | + <Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>, |
| 110 | + { |
| 111 | + <Box<[T]> as WitLoad>::load(memory, location).map($wrapper::from) |
| 112 | + } |
| 113 | + |
| 114 | + fn lift_from<Instance>( |
| 115 | + flat_layout: <Self::Layout as Layout>::Flat, |
| 116 | + memory: &Memory<'_, Instance>, |
| 117 | + ) -> Result<Self, RuntimeError> |
| 118 | + where |
| 119 | + Instance: InstanceWithMemory, |
| 120 | + <Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>, |
| 121 | + { |
| 122 | + <Box<[T]> as WitLoad>::lift_from(flat_layout, memory).map($wrapper::from) |
| 123 | + } |
| 124 | + } |
| 125 | + }; |
| 126 | +} |
| 127 | + |
| 128 | +impl<T> WitType for [T] |
| 129 | +where |
| 130 | + T: WitType, |
| 131 | +{ |
| 132 | + const SIZE: u32 = 8; |
| 133 | + |
| 134 | + type Layout = HList![i32, i32]; |
| 135 | + type Dependencies = HList![T]; |
| 136 | + |
| 137 | + fn wit_type_name() -> Cow<'static, str> { |
| 138 | + format!("list<{}>", T::wit_type_name()).into() |
| 139 | + } |
| 140 | + |
| 141 | + fn wit_type_declaration() -> Cow<'static, str> { |
| 142 | + // The native `list` type doesn't need to be declared |
| 143 | + "".into() |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +impl<T> WitStore for [T] |
| 148 | +where |
| 149 | + T: WitStore, |
| 150 | +{ |
| 151 | + fn store<Instance>( |
| 152 | + &self, |
| 153 | + memory: &mut Memory<'_, Instance>, |
| 154 | + location: GuestPointer, |
| 155 | + ) -> Result<(), RuntimeError> |
| 156 | + where |
| 157 | + Instance: InstanceWithMemory, |
| 158 | + <Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>, |
| 159 | + { |
| 160 | + // There's no need to account for padding between the elements, because the element's size |
| 161 | + // is always aligned: |
| 162 | + // https://github.com/WebAssembly/component-model/blob/cbdd15d9033446558571824af52a78022aaa3f58/design/mvp/CanonicalABI.md#element-size |
| 163 | + let length = u32::try_from(self.len())?; |
| 164 | + let size = length * T::SIZE; |
| 165 | + |
| 166 | + let destination = memory.allocate(size, <T::Layout as Layout>::ALIGNMENT)?; |
| 167 | + |
| 168 | + destination.store(memory, location)?; |
| 169 | + length.store(memory, location.after::<GuestPointer>())?; |
| 170 | + |
| 171 | + self.iter() |
| 172 | + .zip(0..) |
| 173 | + .try_for_each(|(element, index)| element.store(memory, destination.index::<T>(index))) |
| 174 | + } |
| 175 | + |
| 176 | + fn lower<Instance>( |
| 177 | + &self, |
| 178 | + memory: &mut Memory<'_, Instance>, |
| 179 | + ) -> Result<Self::Layout, RuntimeError> |
| 180 | + where |
| 181 | + Instance: InstanceWithMemory, |
| 182 | + <Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>, |
| 183 | + { |
| 184 | + // There's no need to account for padding between the elements, because the element's size |
| 185 | + // is always aligned: |
| 186 | + // https://github.com/WebAssembly/component-model/blob/cbdd15d9033446558571824af52a78022aaa3f58/design/mvp/CanonicalABI.md#element-size |
| 187 | + let length = u32::try_from(self.len())?; |
| 188 | + let size = length * T::SIZE; |
| 189 | + |
| 190 | + let destination = memory.allocate(size, <T::Layout as Layout>::ALIGNMENT)?; |
| 191 | + |
| 192 | + self.iter().zip(0..).try_for_each(|(element, index)| { |
| 193 | + element.store(memory, destination.index::<T>(index)) |
| 194 | + })?; |
| 195 | + |
| 196 | + Ok(destination.lower(memory)? + hlist![length as i32]) |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +impl_wit_type_as_slice!(Box); |
| 201 | +impl_wit_store_as_slice!(Box); |
| 202 | + |
| 203 | +impl<T> WitLoad for Box<[T]> |
| 204 | +where |
| 205 | + T: WitLoad, |
| 206 | +{ |
| 207 | + fn load<Instance>( |
| 208 | + memory: &Memory<'_, Instance>, |
| 209 | + location: GuestPointer, |
| 210 | + ) -> Result<Self, RuntimeError> |
| 211 | + where |
| 212 | + Instance: InstanceWithMemory, |
| 213 | + <Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>, |
| 214 | + { |
| 215 | + let address = GuestPointer::load(memory, location)?; |
| 216 | + let length = u32::load(memory, location.after::<GuestPointer>())?; |
| 217 | + |
| 218 | + (0..length) |
| 219 | + .map(|index| T::load(memory, address.index::<T>(index))) |
| 220 | + .collect() |
| 221 | + } |
| 222 | + |
| 223 | + fn lift_from<Instance>( |
| 224 | + hlist_pat![address, length]: <Self::Layout as Layout>::Flat, |
| 225 | + memory: &Memory<'_, Instance>, |
| 226 | + ) -> Result<Self, RuntimeError> |
| 227 | + where |
| 228 | + Instance: InstanceWithMemory, |
| 229 | + <Instance::Runtime as Runtime>::Memory: RuntimeMemory<Instance>, |
| 230 | + { |
| 231 | + let address = GuestPointer(address.try_into()?); |
| 232 | + let length = length as u32; |
| 233 | + |
| 234 | + (0..length) |
| 235 | + .map(|index| T::load(memory, address.index::<T>(index))) |
| 236 | + .collect() |
| 237 | + } |
| 238 | +} |
| 239 | + |
| 240 | +impl_wit_traits_for_slice_wrapper!(Rc); |
| 241 | +impl_wit_traits_for_slice_wrapper!(Arc); |
0 commit comments