1use emscripten_val_sys::bind::*;
2use std::sync::Once;
3use std::ffi::CString;
4
5use crate::utils::get_type_id;
6
7pub struct GenericWireType(pub f64);
8
9pub trait JsType {
10 fn id() -> crate::TYPEID;
11 fn signature() -> char {
12 'p'
13 }
14 fn from_generic_wire_type(v: GenericWireType) -> Self;
15}
16
17impl JsType for bool {
18 fn id() -> crate::TYPEID {
19 static INIT: Once = Once::new();
20 let type_id = get_type_id::<bool>();
21
22 unsafe {
23 INIT.call_once(|| {
24 let name = CString::new("rust_bool").unwrap();
25 _embind_register_bool(type_id, name.as_ptr(), true, false);
26 });
27 type_id
28 }
29 }
30
31 fn signature() -> char {
32 'i'
33 }
34
35 fn from_generic_wire_type(v: GenericWireType) -> Self {
36 v.0 != 0f64
37 }
38}
39
40macro_rules! register_rust_int {
41 ($t:ty, $name:expr) => {
42 impl JsType for $t {
43 fn id() -> crate::TYPEID {
44 static INIT: Once = Once::new();
45 let type_id = get_type_id::<$t>();
46
47 unsafe {
48 INIT.call_once(|| {
49 let name_cstr = CString::new($name).unwrap();
50 _embind_register_integer(
51 type_id,
52 name_cstr.as_ptr(),
53 std::mem::size_of::<$t>(),
54 <$t>::MIN as _,
55 <$t>::MAX as _,
56 );
57 });
58 type_id
59 }
60 }
61
62 fn signature() -> char {
63 'i'
64 }
65
66 fn from_generic_wire_type(v: GenericWireType) -> Self {
67 v.0 as _
68 }
69 }
70 };
71}
72
73macro_rules! register_rust_float {
74 ($t:ty, $name:expr) => {
75 impl JsType for $t {
76 fn id() -> crate::TYPEID {
77 static INIT: Once = Once::new();
78 let type_id = get_type_id::<$t>();
79
80 unsafe {
81 INIT.call_once(|| {
82 let name_cstr = CString::new($name).unwrap();
83 _embind_register_float(
84 type_id,
85 name_cstr.as_ptr(),
86 std::mem::size_of::<$t>()
87 );
88 });
89 type_id
90 }
91 }
92
93 fn signature() -> char {
94 'd'
95 }
96
97 fn from_generic_wire_type(v: GenericWireType) -> Self {
98 v.0 as _
99 }
100 }
101 };
102}
103
104register_rust_int!(u8, "rust_u8");
105register_rust_int!(u16, "rust_u16");
106register_rust_int!(u32, "rust_u32");
107register_rust_int!(i8, "rust_i8");
108register_rust_int!(i16, "rust_i16");
109register_rust_int!(i32, "rust_i32");
110register_rust_int!(usize, "rust_usize");
111register_rust_int!(isize, "rust_isize");
112register_rust_float!(f32, "rust_f32");
113register_rust_float!(f64, "rust_f64");