wasm_bindgen/
describe.rs

1//! This is an internal module, no stability guarantees are provided. Use at
2//! your own risk.
3
4#![doc(hidden)]
5
6use alloc::boxed::Box;
7use alloc::string::String;
8use alloc::vec::Vec;
9use core::ptr::NonNull;
10
11use crate::{Clamped, JsError, JsObject, JsValue};
12use cfg_if::cfg_if;
13
14macro_rules! tys {
15    ($($a:ident)*) => (tys! { @ ($($a)*) 0 });
16    (@ () $v:expr) => {};
17    (@ ($a:ident $($b:ident)*) $v:expr) => {
18        pub const $a: u32 = $v;
19        tys!(@ ($($b)*) $v+1);
20    }
21}
22
23// NB: this list must be kept in sync with `crates/cli-support/src/descriptor.rs`
24tys! {
25    I8
26    U8
27    I16
28    U16
29    I32
30    U32
31    I64
32    U64
33    F32
34    F64
35    BOOLEAN
36    FUNCTION
37    CLOSURE
38    CACHED_STRING
39    STRING
40    REF
41    REFMUT
42    LONGREF
43    SLICE
44    VECTOR
45    EXTERNREF
46    NAMED_EXTERNREF
47    ENUM
48    STRING_ENUM
49    RUST_STRUCT
50    CHAR
51    OPTIONAL
52    RESULT
53    UNIT
54    CLAMPED
55    NONNULL
56}
57
58#[inline(always)] // see the wasm-interpreter crate
59#[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
60pub fn inform(a: u32) {
61    unsafe { super::__wbindgen_describe(a) }
62}
63
64pub trait WasmDescribe {
65    fn describe();
66}
67
68/// Trait for element types to implement WasmDescribe for vectors of
69/// themselves.
70pub trait WasmDescribeVector {
71    fn describe_vector();
72}
73
74macro_rules! simple {
75    ($($t:ident => $d:ident)*) => ($(
76        impl WasmDescribe for $t {
77            #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
78            fn describe() { inform($d) }
79        }
80    )*)
81}
82
83simple! {
84    i8 => I8
85    u8 => U8
86    i16 => I16
87    u16 => U16
88    i32 => I32
89    u32 => U32
90    i64 => I64
91    u64 => U64
92    isize => I32
93    usize => U32
94    f32 => F32
95    f64 => F64
96    bool => BOOLEAN
97    char => CHAR
98    JsValue => EXTERNREF
99}
100
101cfg_if! {
102    if #[cfg(feature = "enable-interning")] {
103        simple! {
104            str => CACHED_STRING
105        }
106
107    } else {
108        simple! {
109            str => STRING
110        }
111    }
112}
113
114impl<T> WasmDescribe for *const T {
115    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
116    fn describe() {
117        inform(U32)
118    }
119}
120
121impl<T> WasmDescribe for *mut T {
122    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
123    fn describe() {
124        inform(U32)
125    }
126}
127
128impl<T> WasmDescribe for NonNull<T> {
129    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
130    fn describe() {
131        inform(NONNULL)
132    }
133}
134
135impl<T: WasmDescribe> WasmDescribe for [T] {
136    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
137    fn describe() {
138        inform(SLICE);
139        T::describe();
140    }
141}
142
143impl<'a, T: WasmDescribe + ?Sized> WasmDescribe for &'a T {
144    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
145    fn describe() {
146        inform(REF);
147        T::describe();
148    }
149}
150
151impl<'a, T: WasmDescribe + ?Sized> WasmDescribe for &'a mut T {
152    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
153    fn describe() {
154        inform(REFMUT);
155        T::describe();
156    }
157}
158
159cfg_if! {
160    if #[cfg(feature = "enable-interning")] {
161        simple! {
162            String => CACHED_STRING
163        }
164
165    } else {
166        simple! {
167            String => STRING
168        }
169    }
170}
171
172impl WasmDescribeVector for JsValue {
173    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
174    fn describe_vector() {
175        inform(VECTOR);
176        JsValue::describe();
177    }
178}
179
180impl<T: JsObject> WasmDescribeVector for T {
181    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
182    fn describe_vector() {
183        inform(VECTOR);
184        T::describe();
185    }
186}
187
188impl<T: WasmDescribeVector> WasmDescribe for Box<[T]> {
189    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
190    fn describe() {
191        T::describe_vector();
192    }
193}
194
195impl<T> WasmDescribe for Vec<T>
196where
197    Box<[T]>: WasmDescribe,
198{
199    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
200    fn describe() {
201        <Box<[T]>>::describe();
202    }
203}
204
205impl<T: WasmDescribe> WasmDescribe for Option<T> {
206    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
207    fn describe() {
208        inform(OPTIONAL);
209        T::describe();
210    }
211}
212
213impl WasmDescribe for () {
214    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
215    fn describe() {
216        inform(UNIT)
217    }
218}
219
220impl<T: WasmDescribe, E: Into<JsValue>> WasmDescribe for Result<T, E> {
221    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
222    fn describe() {
223        inform(RESULT);
224        T::describe();
225    }
226}
227
228impl<T: WasmDescribe> WasmDescribe for Clamped<T> {
229    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
230    fn describe() {
231        inform(CLAMPED);
232        T::describe();
233    }
234}
235
236impl WasmDescribe for JsError {
237    #[cfg_attr(wasm_bindgen_unstable_test_coverage, coverage(off))]
238    fn describe() {
239        JsValue::describe();
240    }
241}