1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
// this module looks like a mess, lots of doc(hidden) attributes since rust traits cant have private functions

mod private {
    #[doc(hidden)]
    pub struct Seal;
}

#[cfg(feature = "use-32bit-slots")]
#[inline(always)]
unsafe fn read_u32_from_stack(stack: *mut ffi::m3slot_t) -> u32 {
    *stack
}

#[cfg(not(feature = "use-32bit-slots"))]
#[inline(always)]
unsafe fn read_u32_from_stack(stack: *mut ffi::m3slot_t) -> u32 {
    (*stack & 0xFFFF_FFFF) as u32
}

#[cfg(feature = "use-32bit-slots")]
#[inline(always)]
unsafe fn read_u64_from_stack(stack: *mut ffi::m3slot_t) -> u64 {
    *stack.cast::<u64>()
}

#[cfg(not(feature = "use-32bit-slots"))]
#[inline(always)]
unsafe fn read_u64_from_stack(stack: *mut ffi::m3slot_t) -> u64 {
    *stack
}

#[cfg(feature = "use-32bit-slots")]
#[inline(always)]
unsafe fn write_u32_to_stack(stack: *mut ffi::m3slot_t, val: u32) {
    *stack = val;
}

#[cfg(not(feature = "use-32bit-slots"))]
#[inline(always)]
unsafe fn write_u32_to_stack(stack: *mut ffi::m3slot_t, val: u32) {
    *stack.cast::<ffi::m3slot_t>() = val as ffi::m3slot_t;
}

#[cfg(feature = "use-32bit-slots")]
#[inline(always)]
unsafe fn write_u64_to_stack(stack: *mut ffi::m3slot_t, val: u64) {
    *stack.cast::<u64>() = val;
}

#[cfg(not(feature = "use-32bit-slots"))]
#[inline(always)]
unsafe fn write_u64_to_stack(stack: *mut ffi::m3slot_t, val: u64) {
    *stack = val;
}

#[cfg(feature = "use-32bit-slots")]
const SIZE_IN_SLOT_COUNT: usize = 2;
#[cfg(not(feature = "use-32bit-slots"))]
const SIZE_IN_SLOT_COUNT: usize = 1;

/// Trait implemented by types that can be passed to and from wasm.

pub trait WasmType: Sized {
    #[doc(hidden)]
    const TYPE_INDEX: u8;
    #[doc(hidden)]
    const SIZE_IN_SLOT_COUNT: usize;
    #[doc(hidden)]
    unsafe fn pop_from_stack(stack: *mut ffi::m3slot_t) -> Self;
    #[doc(hidden)]
    unsafe fn push_on_stack(self, stack: *mut ffi::m3slot_t);
    #[doc(hidden)]
    fn sealed_() -> private::Seal;
}

/// Tait implemented by types that can be passed to wasm.

pub trait WasmArg: WasmType {}

/// Helper tait implemented by tuples to emulate "variadic generics".

pub trait WasmArgs {
    #[doc(hidden)]
    unsafe fn push_on_stack(self, stack: *mut [ffi::m3slot_t]);
    #[doc(hidden)]
    // required for closure linking

    unsafe fn pop_from_stack(stack: *mut [ffi::m3slot_t]) -> Self;
    #[doc(hidden)]
    fn validate_types(types: &[u8]) -> bool;
    #[doc(hidden)]
    fn sealed_() -> private::Seal;
}

impl WasmArg for i32 {}
impl WasmType for i32 {
    #[doc(hidden)]
    const TYPE_INDEX: u8 = ffi::_bindgen_ty_1::c_m3Type_i32 as u8;
    #[doc(hidden)]
    const SIZE_IN_SLOT_COUNT: usize = SIZE_IN_SLOT_COUNT;
    #[doc(hidden)]
    unsafe fn pop_from_stack(stack: *mut ffi::m3slot_t) -> Self {
        read_u32_from_stack(stack) as i32
    }
    #[doc(hidden)]
    unsafe fn push_on_stack(self, stack: *mut ffi::m3slot_t) {
        write_u32_to_stack(stack, self as u32);
    }
    #[doc(hidden)]
    fn sealed_() -> private::Seal {
        private::Seal
    }
}

impl WasmArg for u32 {}
impl WasmType for u32 {
    #[doc(hidden)]
    const TYPE_INDEX: u8 = ffi::_bindgen_ty_1::c_m3Type_i32 as u8;
    #[doc(hidden)]
    const SIZE_IN_SLOT_COUNT: usize = SIZE_IN_SLOT_COUNT;
    #[doc(hidden)]
    unsafe fn pop_from_stack(stack: *mut ffi::m3slot_t) -> Self {
        read_u32_from_stack(stack)
    }
    #[doc(hidden)]
    unsafe fn push_on_stack(self, stack: *mut ffi::m3slot_t) {
        write_u32_to_stack(stack, self);
    }
    #[doc(hidden)]
    fn sealed_() -> private::Seal {
        private::Seal
    }
}

impl WasmArg for i64 {}
impl WasmType for i64 {
    #[doc(hidden)]
    const TYPE_INDEX: u8 = ffi::_bindgen_ty_1::c_m3Type_i64 as u8;
    #[doc(hidden)]
    const SIZE_IN_SLOT_COUNT: usize = SIZE_IN_SLOT_COUNT;

    #[doc(hidden)]
    unsafe fn pop_from_stack(stack: *mut ffi::m3slot_t) -> Self {
        read_u64_from_stack(stack) as i64
    }
    #[doc(hidden)]
    unsafe fn push_on_stack(self, stack: *mut ffi::m3slot_t) {
        write_u64_to_stack(stack, self as u64);
    }
    #[doc(hidden)]
    fn sealed_() -> private::Seal {
        private::Seal
    }
}

impl WasmArg for u64 {}
impl WasmType for u64 {
    #[doc(hidden)]
    const TYPE_INDEX: u8 = ffi::_bindgen_ty_1::c_m3Type_i64 as u8;
    #[doc(hidden)]
    const SIZE_IN_SLOT_COUNT: usize = SIZE_IN_SLOT_COUNT;
    #[doc(hidden)]
    unsafe fn pop_from_stack(stack: *mut ffi::m3slot_t) -> Self {
        read_u64_from_stack(stack)
    }
    #[doc(hidden)]
    unsafe fn push_on_stack(self, stack: *mut ffi::m3slot_t) {
        write_u64_to_stack(stack, self);
    }
    #[doc(hidden)]
    fn sealed_() -> private::Seal {
        private::Seal
    }
}

impl WasmArg for f32 {}
impl WasmType for f32 {
    #[doc(hidden)]
    const TYPE_INDEX: u8 = ffi::_bindgen_ty_1::c_m3Type_f32 as u8;
    #[doc(hidden)]
    const SIZE_IN_SLOT_COUNT: usize = SIZE_IN_SLOT_COUNT;
    #[doc(hidden)]
    unsafe fn pop_from_stack(stack: *mut ffi::m3slot_t) -> Self {
        f32::from_ne_bytes(read_u32_from_stack(stack).to_ne_bytes())
    }
    #[doc(hidden)]
    unsafe fn push_on_stack(self, stack: *mut ffi::m3slot_t) {
        write_u32_to_stack(stack, u32::from_ne_bytes(self.to_ne_bytes()));
    }
    #[doc(hidden)]
    fn sealed_() -> private::Seal {
        private::Seal
    }
}

impl WasmArg for f64 {}
impl WasmType for f64 {
    #[doc(hidden)]
    const TYPE_INDEX: u8 = ffi::_bindgen_ty_1::c_m3Type_f64 as u8;
    #[doc(hidden)]
    const SIZE_IN_SLOT_COUNT: usize = SIZE_IN_SLOT_COUNT;
    #[doc(hidden)]
    unsafe fn pop_from_stack(stack: *mut ffi::m3slot_t) -> Self {
        f64::from_ne_bytes(read_u64_from_stack(stack).to_ne_bytes())
    }
    #[doc(hidden)]
    unsafe fn push_on_stack(self, stack: *mut ffi::m3slot_t) {
        write_u64_to_stack(stack, u64::from_ne_bytes(self.to_ne_bytes()));
    }
    #[doc(hidden)]
    fn sealed_() -> private::Seal {
        private::Seal
    }
}

impl WasmType for () {
    #[doc(hidden)]
    const TYPE_INDEX: u8 = ffi::_bindgen_ty_1::c_m3Type_none as u8;
    #[doc(hidden)]
    const SIZE_IN_SLOT_COUNT: usize = 0;
    #[doc(hidden)]
    unsafe fn pop_from_stack(_: *mut ffi::m3slot_t) -> Self {}
    #[doc(hidden)]
    unsafe fn push_on_stack(self, _: *mut ffi::m3slot_t) {}
    #[doc(hidden)]
    fn sealed_() -> private::Seal {
        private::Seal
    }
}

impl WasmArgs for () {
    #[doc(hidden)]
    unsafe fn push_on_stack(self, _: *mut [ffi::m3slot_t]) {}
    #[doc(hidden)]
    unsafe fn pop_from_stack(_: *mut [ffi::m3slot_t]) -> Self {}
    #[doc(hidden)]
    fn validate_types(types: &[u8]) -> bool {
        types.is_empty()
    }
    #[doc(hidden)]
    fn sealed_() -> private::Seal {
        private::Seal
    }
}

/// Unary functions

impl<T> WasmArgs for T
where
    T: WasmArg,
{
    #[doc(hidden)]
    unsafe fn push_on_stack(self, stack: *mut [ffi::m3slot_t]) {
        WasmType::push_on_stack(self, stack.cast());
    }
    #[doc(hidden)]
    unsafe fn pop_from_stack(stack: *mut [ffi::m3slot_t]) -> Self {
        WasmType::pop_from_stack(stack.cast())
    }
    #[doc(hidden)]
    fn validate_types(types: &[u8]) -> bool {
        types
            .get(0)
            .map(|&idx| idx == T::TYPE_INDEX)
            .unwrap_or(false)
    }
    #[doc(hidden)]
    fn sealed_() -> private::Seal {
        private::Seal
    }
}

macro_rules! args_impl {
    ($($types:ident),*) => { args_impl!(@rec [$($types,)*] []); };
    (@rec [] [$($types:ident,)*]) => { args_impl!(@do_impl $($types,)*); };
    (@rec [$head:ident, $($tail:ident,)*] [$($types:ident,)*]) => {
        args_impl!(@do_impl $($types,)*);
        args_impl!(@rec [$($tail,)*] [$($types,)* $head,]);
    };
    (@do_impl) => {/* catch the () case, since its implementation differs slightly */};
    (@do_impl $($types:ident,)*) => {
        #[allow(clippy::eval_order_dependence)]
        #[allow(unused_assignments)]
        impl<$($types,)*> WasmArgs for ($($types,)*)
        where $($types: WasmArg,)* {
            #[doc(hidden)]
            unsafe fn push_on_stack(self, stack: *mut [ffi::m3slot_t]) {
                // reborrowing might be UB here due to aliasing, but there is currently no other stable way to get the metadata of a raw fat pointer

                let mut stack = &mut *stack;
                #[allow(non_snake_case)]
                let ($($types,)*) = self;

                let req_size = 0 $(
                    + $types::SIZE_IN_SLOT_COUNT
                )*;
                assert!(req_size <= stack.len(), "wasm stack was too small");

                $(
                    $types.push_on_stack(stack.as_mut_ptr());
                    stack = &mut stack[$types::SIZE_IN_SLOT_COUNT..];
                )*
            }
            #[doc(hidden)]
            unsafe fn pop_from_stack(stack: *mut [ffi::m3slot_t]) -> Self {
                // reborrowing might be UB here due to aliasing, but there is currently no other stable way to get the metadata of a raw fat pointer

                let mut stack = &mut *stack;
                let req_size = 0 $(
                    + $types::SIZE_IN_SLOT_COUNT
                )*;
                assert!(req_size <= stack.len(), "wasm stack was too small");
                ($(
                    {
                        let val = $types::pop_from_stack(stack.as_mut_ptr());
                        stack = &mut stack[$types::SIZE_IN_SLOT_COUNT..];
                        val
                    },
                )*)
            }
            #[doc(hidden)]
            fn validate_types(types: &[u8]) -> bool {
                let mut ty_iter = types.iter();
                $(
                    ty_iter.next().map(|&ty| ty == $types::TYPE_INDEX).unwrap_or(false)
                )&&*
            }
            #[doc(hidden)]
            fn sealed_() -> private::Seal { private::Seal }
        }
    };
}
args_impl!(A, B, C, D, E, F, G, H, J, K, L, M, N, O, P, Q);

#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn test_validate_types_single() {
        assert!(f64::validate_types(&[
            ffi::_bindgen_ty_1::c_m3Type_f64 as u8
        ]));
    }

    #[test]
    fn test_validate_types_single_fail() {
        assert!(!f32::validate_types(&[
            ffi::_bindgen_ty_1::c_m3Type_f64 as u8
        ]));
    }

    #[test]
    fn test_validate_types_double() {
        assert!(<(f64, u32)>::validate_types(&[
            ffi::_bindgen_ty_1::c_m3Type_f64 as u8,
            ffi::_bindgen_ty_1::c_m3Type_i32 as u8
        ]));
    }

    #[test]
    fn test_validate_types_double_fail() {
        assert!(!<(f32, u64)>::validate_types(&[
            ffi::_bindgen_ty_1::c_m3Type_i64 as u8,
            ffi::_bindgen_ty_1::c_m3Type_f32 as u8
        ]));
    }

    #[test]
    fn test_validate_types_quintuple() {
        assert!(<(f64, u32, i32, i64, f32)>::validate_types(&[
            ffi::_bindgen_ty_1::c_m3Type_f64 as u8,
            ffi::_bindgen_ty_1::c_m3Type_i32 as u8,
            ffi::_bindgen_ty_1::c_m3Type_i32 as u8,
            ffi::_bindgen_ty_1::c_m3Type_i64 as u8,
            ffi::_bindgen_ty_1::c_m3Type_f32 as u8
        ]));
    }

    #[test]
    fn test_validate_types_quintuple_fail() {
        assert!(!<(f64, u32, i32, i64, f32)>::validate_types(&[
            ffi::_bindgen_ty_1::c_m3Type_i32 as u8,
            ffi::_bindgen_ty_1::c_m3Type_i64 as u8,
            ffi::_bindgen_ty_1::c_m3Type_i32 as u8,
            ffi::_bindgen_ty_1::c_m3Type_f32 as u8,
            ffi::_bindgen_ty_1::c_m3Type_f64 as u8
        ]));
    }
}