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
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
// ┃ This file is part of the Perspective library, distributed under the terms ┃
// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

/// A macro for implementing the `wasm_bindgen` boilerplate for types which
/// implement `serde::{Serialize, Deserialize}`.
///
/// # Examples
///
/// ```
/// struct MyStruct { .. }
/// derive_wasm_abi!(MyStruct, FromWasmAbi);
///
/// #[wasm_bindgen]
/// pub fn process_my_struct(s: MyStruct) {}
/// ```
#[macro_export]
macro_rules! derive_wasm_abi {
    ($type:ty) => {
        impl wasm_bindgen::describe::WasmDescribe for $type {
            fn describe() {
                <js_sys::Object as wasm_bindgen::describe::WasmDescribe>::describe()
            }
        }
    };

    ($type:ty, FromWasmAbi $(, $symbols:tt)*) => {
        impl wasm_bindgen::convert::FromWasmAbi for $type {
            type Abi = <js_sys::Object as wasm_bindgen::convert::IntoWasmAbi>::Abi;
            #[inline]
            unsafe fn from_abi(js: Self::Abi) -> Self {
                let obj = js_sys::Object::from_abi(js);
                use ::perspective_js::utils::JsValueSerdeExt;
                wasm_bindgen::JsValue::from(obj).into_serde_ext().unwrap()
            }
        }

        derive_wasm_abi!($type $(, $symbols)*);
    };

    ($type:ty, IntoWasmAbi $(, $symbols:tt)*) => {
        impl wasm_bindgen::convert::IntoWasmAbi for $type {
            type Abi = <js_sys::Object as wasm_bindgen::convert::IntoWasmAbi>::Abi;
            #[inline]
            fn into_abi(self) -> Self::Abi {
                use wasm_bindgen::JsCast;
                <wasm_bindgen::JsValue as ::perspective_js::utils::JsValueSerdeExt>::from_serde_ext(&self).unwrap().unchecked_into::<js_sys::Object>().into_abi()
            }
        }

        derive_wasm_abi!($type $(, $symbols)*);
    };
}