Skip to main content

wasm_bindgen/convert/
slices.rs

1use alloc::boxed::Box;
2use alloc::string::String;
3use alloc::vec::Vec;
4use core::mem::{self, MaybeUninit};
5use core::ops::{Deref, DerefMut};
6use core::str;
7
8use crate::__rt::{marker::ErasableGeneric, WasmWord};
9use crate::__wbindgen_copy_to_typed_array;
10use crate::convert::{
11    js_value_vector_from_abi, js_value_vector_into_abi, FromWasmAbi, IntoWasmAbi,
12    LongRefFromWasmAbi, OptionFromWasmAbi, OptionIntoWasmAbi, RefFromWasmAbi, RefMutFromWasmAbi,
13    UpcastFrom, VectorFromWasmAbi, VectorIntoWasmAbi, WasmAbi,
14};
15use crate::describe::*;
16use crate::JsValue;
17
18use cfg_if::cfg_if;
19
20/// # ⚠️ Unstable
21///
22/// This is part of the internal [`convert`](crate::convert) module, **no
23/// stability guarantees** are provided. Use at your own risk. See its
24/// documentation for more details.
25// note: `WasmAbi` types do not need to be FFI-safe themselves, it's just more
26// convenient to directly write `WasmSlice` in some of the manually-written FFI
27// functions in `lib.rs` rather than `WasmRet<WasmSlice>`.
28#[repr(C)]
29#[derive(Clone, Copy)]
30pub struct WasmSlice {
31    pub ptr: WasmWord,
32    pub len: WasmWord,
33}
34
35impl WasmSlice {
36    #[inline]
37    pub fn from_usize(ptr: usize, len: usize) -> Self {
38        Self {
39            ptr: WasmWord::from_usize(ptr),
40            len: WasmWord::from_usize(len),
41        }
42    }
43}
44
45impl WasmAbi for WasmSlice {
46    /// `self.ptr`
47    type Prim1 = <WasmWord as WasmAbi>::Prim1;
48    /// `self.len`
49    type Prim2 = <WasmWord as WasmAbi>::Prim1;
50    type Prim3 = ();
51    type Prim4 = ();
52
53    #[inline]
54    fn split(self) -> (Self::Prim1, Self::Prim2, (), ()) {
55        (self.ptr.split().0, self.len.split().0, (), ())
56    }
57
58    #[inline]
59    fn join(ptr: Self::Prim1, len: Self::Prim2, _: (), _: ()) -> Self {
60        Self {
61            ptr: WasmWord::join(ptr, (), (), ()),
62            len: WasmWord::join(len, (), (), ()),
63        }
64    }
65}
66
67#[inline]
68fn null_slice() -> WasmSlice {
69    WasmSlice::from_usize(0, 0)
70}
71
72pub struct WasmMutSlice {
73    pub slice: WasmSlice,
74    pub idx: u32,
75}
76
77impl WasmAbi for WasmMutSlice {
78    /// `self.slice.ptr`
79    type Prim1 = <WasmSlice as WasmAbi>::Prim1;
80    /// `self.slice.len`
81    type Prim2 = <WasmSlice as WasmAbi>::Prim2;
82    /// `self.idx`
83    type Prim3 = u32;
84    type Prim4 = ();
85
86    #[inline]
87    fn split(self) -> (Self::Prim1, Self::Prim2, u32, ()) {
88        let (ptr, len, (), ()) = self.slice.split();
89        (ptr, len, self.idx, ())
90    }
91
92    #[inline]
93    fn join(ptr: Self::Prim1, len: Self::Prim2, idx: u32, _: ()) -> Self {
94        Self {
95            slice: WasmSlice::join(ptr, len, (), ()),
96            idx,
97        }
98    }
99}
100
101/// The representation of a mutable slice passed from JS to Rust.
102pub struct MutSlice<T> {
103    /// A copy of the data in the JS typed array.
104    contents: Box<[T]>,
105    /// A reference to the original JS typed array.
106    js: JsValue,
107}
108
109impl<T> Drop for MutSlice<T> {
110    fn drop(&mut self) {
111        let byte_slice = unsafe {
112            core::slice::from_raw_parts(
113                self.contents.as_ptr() as *const u8,
114                self.contents.len() * mem::size_of::<T>(),
115            )
116        };
117        __wbindgen_copy_to_typed_array(byte_slice, &self.js);
118    }
119}
120
121impl<T> Deref for MutSlice<T> {
122    type Target = [T];
123
124    fn deref(&self) -> &[T] {
125        &self.contents
126    }
127}
128
129impl<T> DerefMut for MutSlice<T> {
130    fn deref_mut(&mut self) -> &mut [T] {
131        &mut self.contents
132    }
133}
134
135macro_rules! vectors {
136    ($($t:ty)*) => ($(
137        vectors_internal!($t);
138        vectors_internal!(MaybeUninit<$t>);
139    )*)
140}
141
142macro_rules! vectors_internal {
143    ($t:ty) => {
144        impl WasmDescribeVector for $t {
145            #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
146            fn describe_vector() {
147                inform(VECTOR);
148                <$t>::describe();
149            }
150        }
151
152        impl VectorIntoWasmAbi for $t {
153            type Abi = WasmSlice;
154
155            #[inline]
156            fn vector_into_abi(vector: Box<[$t]>) -> WasmSlice {
157                let ptr = vector.as_ptr();
158                let len = vector.len();
159                mem::forget(vector);
160                WasmSlice::from_usize(ptr as usize, len)
161            }
162        }
163
164        impl VectorFromWasmAbi for $t {
165            type Abi = WasmSlice;
166
167            #[inline]
168            unsafe fn vector_from_abi(js: WasmSlice) -> Box<[$t]> {
169                let ptr = js.ptr.into_usize() as *mut $t;
170                let len = js.len.into_usize();
171                Vec::from_raw_parts(ptr, len, len).into_boxed_slice()
172            }
173        }
174
175        impl<'a> IntoWasmAbi for &'a [$t] {
176            type Abi = WasmSlice;
177
178            #[inline]
179            fn into_abi(self) -> WasmSlice {
180                WasmSlice::from_usize(self.as_ptr() as usize, self.len())
181            }
182        }
183
184        impl<'a> OptionIntoWasmAbi for &'a [$t] {
185            #[inline]
186            fn none() -> WasmSlice {
187                null_slice()
188            }
189        }
190
191        impl<'a> IntoWasmAbi for &'a mut [$t] {
192            type Abi = WasmSlice;
193
194            #[inline]
195            fn into_abi(self) -> WasmSlice {
196                (&*self).into_abi()
197            }
198        }
199
200        impl<'a> OptionIntoWasmAbi for &'a mut [$t] {
201            #[inline]
202            fn none() -> WasmSlice {
203                null_slice()
204            }
205        }
206
207        impl RefFromWasmAbi for [$t] {
208            type Abi = WasmSlice;
209            type Anchor = Box<[$t]>;
210
211            #[inline]
212            unsafe fn ref_from_abi(js: WasmSlice) -> Box<[$t]> {
213                <Box<[$t]>>::from_abi(js)
214            }
215        }
216
217        impl RefMutFromWasmAbi for [$t] {
218            type Abi = WasmMutSlice;
219            type Anchor = MutSlice<$t>;
220
221            #[inline]
222            unsafe fn ref_mut_from_abi(js: WasmMutSlice) -> MutSlice<$t> {
223                let contents = <Box<[$t]>>::from_abi(js.slice);
224                let js = JsValue::from_abi(js.idx);
225                MutSlice { contents, js }
226            }
227        }
228
229        impl LongRefFromWasmAbi for [$t] {
230            type Abi = WasmSlice;
231            type Anchor = Box<[$t]>;
232
233            #[inline]
234            unsafe fn long_ref_from_abi(js: WasmSlice) -> Box<[$t]> {
235                Self::ref_from_abi(js)
236            }
237        }
238    };
239}
240
241vectors! {
242    u8 i8 u16 i16 u32 i32 u64 i64 usize isize f32 f64
243}
244
245impl WasmDescribeVector for String {
246    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
247    fn describe_vector() {
248        inform(VECTOR);
249        inform(NAMED_EXTERNREF);
250        // Trying to use an actual loop for this breaks the Wasm interpreter.
251        inform(6);
252        inform('s' as u32);
253        inform('t' as u32);
254        inform('r' as u32);
255        inform('i' as u32);
256        inform('n' as u32);
257        inform('g' as u32);
258    }
259}
260
261impl VectorIntoWasmAbi for String {
262    type Abi = <Box<[JsValue]> as IntoWasmAbi>::Abi;
263
264    fn vector_into_abi(vector: Box<[Self]>) -> Self::Abi {
265        js_value_vector_into_abi(vector)
266    }
267}
268
269impl VectorFromWasmAbi for String {
270    type Abi = <Box<[JsValue]> as FromWasmAbi>::Abi;
271
272    unsafe fn vector_from_abi(js: Self::Abi) -> Box<[Self]> {
273        js_value_vector_from_abi(js)
274    }
275}
276
277cfg_if! {
278    if #[cfg(feature = "enable-interning")] {
279        #[inline]
280        fn unsafe_get_cached_str(x: &str) -> Option<WasmSlice> {
281            // This uses 0 for the ptr as an indication that it is a JsValue and not a str.
282            crate::cache::intern::unsafe_get_str(x).map(|x| WasmSlice::from_usize(0, x as usize))
283        }
284
285    } else {
286        #[inline]
287        fn unsafe_get_cached_str(_x: &str) -> Option<WasmSlice> {
288            None
289        }
290    }
291}
292
293impl<T> IntoWasmAbi for Vec<T>
294where
295    Box<[T]>: IntoWasmAbi<Abi = WasmSlice>,
296{
297    type Abi = <Box<[T]> as IntoWasmAbi>::Abi;
298
299    #[inline]
300    fn into_abi(self) -> Self::Abi {
301        self.into_boxed_slice().into_abi()
302    }
303}
304
305impl<T> OptionIntoWasmAbi for Vec<T>
306where
307    Box<[T]>: IntoWasmAbi<Abi = WasmSlice>,
308{
309    #[inline]
310    fn none() -> WasmSlice {
311        null_slice()
312    }
313}
314
315impl<T> FromWasmAbi for Vec<T>
316where
317    Box<[T]>: FromWasmAbi<Abi = WasmSlice>,
318{
319    type Abi = <Box<[T]> as FromWasmAbi>::Abi;
320
321    #[inline]
322    unsafe fn from_abi(js: Self::Abi) -> Self {
323        <Box<[T]>>::from_abi(js).into()
324    }
325}
326
327impl<T> OptionFromWasmAbi for Vec<T>
328where
329    Box<[T]>: FromWasmAbi<Abi = WasmSlice>,
330{
331    #[inline]
332    fn is_none(abi: &WasmSlice) -> bool {
333        abi.ptr.is_zero()
334    }
335}
336
337impl IntoWasmAbi for String {
338    type Abi = <Vec<u8> as IntoWasmAbi>::Abi;
339
340    #[inline]
341    fn into_abi(self) -> Self::Abi {
342        // This is safe because the JsValue is immediately looked up in the heap and
343        // then returned, so use-after-free cannot occur.
344        unsafe_get_cached_str(&self).unwrap_or_else(|| self.into_bytes().into_abi())
345    }
346}
347
348impl OptionIntoWasmAbi for String {
349    #[inline]
350    fn none() -> Self::Abi {
351        null_slice()
352    }
353}
354
355impl FromWasmAbi for String {
356    type Abi = <Vec<u8> as FromWasmAbi>::Abi;
357
358    #[inline]
359    unsafe fn from_abi(js: Self::Abi) -> Self {
360        String::from_utf8_unchecked(<Vec<u8>>::from_abi(js))
361    }
362}
363
364impl OptionFromWasmAbi for String {
365    #[inline]
366    fn is_none(slice: &WasmSlice) -> bool {
367        slice.ptr.is_zero()
368    }
369}
370
371impl<'a> IntoWasmAbi for &'a str {
372    type Abi = <&'a [u8] as IntoWasmAbi>::Abi;
373
374    #[inline]
375    fn into_abi(self) -> Self::Abi {
376        // This is safe because the JsValue is immediately looked up in the heap and
377        // then returned, so use-after-free cannot occur.
378        unsafe_get_cached_str(self).unwrap_or_else(|| self.as_bytes().into_abi())
379    }
380}
381
382impl OptionIntoWasmAbi for &str {
383    #[inline]
384    fn none() -> Self::Abi {
385        null_slice()
386    }
387}
388
389impl RefFromWasmAbi for str {
390    type Abi = <[u8] as RefFromWasmAbi>::Abi;
391    type Anchor = Box<str>;
392
393    #[inline]
394    unsafe fn ref_from_abi(js: Self::Abi) -> Self::Anchor {
395        mem::transmute::<Box<[u8]>, Box<str>>(<Box<[u8]>>::from_abi(js))
396    }
397}
398
399impl LongRefFromWasmAbi for str {
400    type Abi = <[u8] as RefFromWasmAbi>::Abi;
401    type Anchor = Box<str>;
402
403    #[inline]
404    unsafe fn long_ref_from_abi(js: Self::Abi) -> Self::Anchor {
405        Self::ref_from_abi(js)
406    }
407}
408
409unsafe impl ErasableGeneric for &str {
410    type Repr = &'static str;
411}
412
413unsafe impl<T: ErasableGeneric> ErasableGeneric for Box<[T]> {
414    type Repr = Box<[T::Repr]>;
415}
416
417impl UpcastFrom<&str> for &str {}
418
419impl<T, Target> UpcastFrom<Box<[T]>> for Box<[Target]> where Target: UpcastFrom<T> {}
420
421unsafe impl<T: ErasableGeneric> ErasableGeneric for Vec<T> {
422    type Repr = Vec<T::Repr>;
423}
424
425impl<T, Target> UpcastFrom<Vec<T>> for Vec<Target> where Target: UpcastFrom<T> {}
426
427impl<T: VectorIntoWasmAbi> IntoWasmAbi for Box<[T]> {
428    type Abi = <T as VectorIntoWasmAbi>::Abi;
429
430    fn into_abi(self) -> Self::Abi {
431        T::vector_into_abi(self)
432    }
433}
434
435impl<T> OptionIntoWasmAbi for Box<[T]>
436where
437    Self: IntoWasmAbi<Abi = WasmSlice>,
438{
439    fn none() -> WasmSlice {
440        null_slice()
441    }
442}
443
444impl<T: VectorFromWasmAbi> FromWasmAbi for Box<[T]> {
445    type Abi = <T as VectorFromWasmAbi>::Abi;
446
447    unsafe fn from_abi(js: Self::Abi) -> Self {
448        T::vector_from_abi(js)
449    }
450}
451
452impl<T> OptionFromWasmAbi for Box<[T]>
453where
454    Self: FromWasmAbi<Abi = WasmSlice>,
455{
456    fn is_none(slice: &WasmSlice) -> bool {
457        slice.ptr.is_zero()
458    }
459}
460
461impl<T: ErasableGeneric<Repr = JsValue> + WasmDescribe> VectorFromWasmAbi for T {
462    type Abi = WasmSlice;
463
464    #[inline]
465    unsafe fn vector_from_abi(js: WasmSlice) -> Box<[Self]> {
466        let ptr = js.ptr.into_usize() as *mut T;
467        let len = js.len.into_usize();
468        Vec::from_raw_parts(ptr, len, len).into_boxed_slice()
469    }
470}
471
472impl<T: ErasableGeneric<Repr = JsValue> + WasmDescribe> VectorIntoWasmAbi for T {
473    type Abi = WasmSlice;
474
475    #[inline]
476    fn vector_into_abi(vector: Box<[T]>) -> WasmSlice {
477        let ptr = vector.as_ptr();
478        let len = vector.len();
479        mem::forget(vector);
480        WasmSlice::from_usize(ptr as usize, len)
481    }
482}
483
484// JsValue-like slice support (Rust-to-JS only)
485// JsValue-like are repr(transparent) over u32, so &[JsValue] is a contiguous array of heap indices
486
487unsafe impl<T: ErasableGeneric> ErasableGeneric for &[T] {
488    type Repr = &'static [T::Repr];
489}
490
491impl<'a, T, Target> UpcastFrom<&'a [T]> for &'a [Target] where Target: UpcastFrom<T> {}
492
493impl<T: ErasableGeneric<Repr = JsValue> + WasmDescribe> IntoWasmAbi for &[T] {
494    type Abi = WasmSlice;
495
496    #[inline]
497    fn into_abi(self) -> WasmSlice {
498        WasmSlice::from_usize(self.as_ptr() as usize, self.len())
499    }
500}
501
502impl<T: ErasableGeneric<Repr = JsValue> + WasmDescribe> OptionIntoWasmAbi for &[T] {
503    #[inline]
504    fn none() -> WasmSlice {
505        null_slice()
506    }
507}