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
//! Access to the `ToJsValue` trait for converting types into
//! `wasm_bindgen::JsValue`s.

use std::collections::HashMap;

use js_sys::Array;
use wasm_bindgen::{prelude::Closure, JsValue};

use crate::imports::Object;

/// This trait is used to provide an implementation for converting a given type
/// into a `wasm_bindgen::JsValue`.
pub trait ToJsValue {
    /// Convert the current type to a `wasm_bindgen::JsValue`;
    fn to_js_value(&self) -> JsValue;
}

impl ToJsValue for String {
    fn to_js_value(&self) -> JsValue {
        JsValue::from_str(self)
    }
}

impl ToJsValue for &'static str {
    fn to_js_value(&self) -> JsValue {
        JsValue::from_str(self)
    }
}

impl ToJsValue for bool {
    fn to_js_value(&self) -> JsValue {
        JsValue::from_bool(*self)
    }
}

// It would be a nice exercise to write a macro for these...

impl ToJsValue for usize {
    fn to_js_value(&self) -> JsValue {
        JsValue::from_f64(*self as f64)
    }
}

impl ToJsValue for isize {
    fn to_js_value(&self) -> JsValue {
        JsValue::from_f64(*self as f64)
    }
}

impl ToJsValue for u64 {
    fn to_js_value(&self) -> JsValue {
        JsValue::from_f64(*self as f64)
    }
}

impl ToJsValue for i64 {
    fn to_js_value(&self) -> JsValue {
        JsValue::from_f64(*self as f64)
    }
}

impl ToJsValue for u32 {
    fn to_js_value(&self) -> JsValue {
        JsValue::from_f64(*self as f64)
    }
}

impl ToJsValue for i32 {
    fn to_js_value(&self) -> JsValue {
        JsValue::from_f64(*self as f64)
    }
}

impl ToJsValue for u16 {
    fn to_js_value(&self) -> JsValue {
        JsValue::from_f64(*self as f64)
    }
}

impl ToJsValue for i16 {
    fn to_js_value(&self) -> JsValue {
        JsValue::from_f64(*self as f64)
    }
}

impl ToJsValue for u8 {
    fn to_js_value(&self) -> JsValue {
        JsValue::from_f64(*self as f64)
    }
}

impl ToJsValue for i8 {
    fn to_js_value(&self) -> JsValue {
        JsValue::from_f64(*self as f64)
    }
}

impl ToJsValue for f32 {
    fn to_js_value(&self) -> JsValue {
        JsValue::from_f64(*self as f64)
    }
}

impl ToJsValue for f64 {
    fn to_js_value(&self) -> JsValue {
        JsValue::from_f64(*self as f64)
    }
}

impl<T> ToJsValue for Option<T>
where
    T: ToJsValue,
{
    fn to_js_value(&self) -> JsValue {
        match self {
            Some(val) => val.to_js_value(),
            None => JsValue::null(),
        }
    }
}

impl<T> ToJsValue for Vec<T>
where
    T: ToJsValue,
{
    fn to_js_value(&self) -> JsValue {
        self.iter()
            .map(|v| v.to_js_value())
            .collect::<Array>()
            .into()
    }
}

impl<V> ToJsValue for HashMap<String, V>
where
    V: ToJsValue,
{
    fn to_js_value(&self) -> JsValue {
        let obj = Object::new();
        for (k, v) in self {
            obj.set(k, v.to_js_value())
        }
        obj.into()
    }
}

impl<T> ToJsValue for Closure<T>
where
    T: ?Sized,
{
    fn to_js_value(&self) -> JsValue {
        // I'm not sure if this messes up the memory management of `Closure`. Normally,
        // to release it to the JS side completely, `Closure::into_js_value()` is
        // called, which drops the (owned) `Closure` in Rust and returns just the
        // `JsValue`. Here we are getting a `JsValue`, but we are only working
        // on a reference to the `Closure`.
        self.as_ref().to_owned()
    }
}

impl ToJsValue for JsValue {
    fn to_js_value(&self) -> JsValue {
        self.to_owned()
    }
}

impl ToJsValue for () {
    fn to_js_value(&self) -> JsValue {
        JsValue::undefined()
    }
}