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
//! Typed JavaScript collection wrappers.
use std::{
    cmp::{Eq, PartialEq},
    fmt,
    marker::PhantomData,
    str::FromStr,
};

use js_sys::{Array, JsString, Object};
use wasm_bindgen::{prelude::*, JsCast};

use crate::{game, local::RawObjectIdParseError, prelude::*};

#[wasm_bindgen]
extern "C" {
    #[derive(Clone)]
    #[wasm_bindgen(extends = Object)]
    pub(crate) type ObjectExt;

    #[wasm_bindgen(method, structural, indexing_setter)]
    pub(crate) fn set(this: &ObjectExt, prop: &str, val: &JsValue);

    #[wasm_bindgen(method, structural, indexing_setter)]
    pub(crate) fn set_value(this: &ObjectExt, prop: &JsValue, val: &JsValue);

    #[wasm_bindgen(method, structural, indexing_getter)]
    pub(crate) fn get_value(this: &ObjectExt, prop: &JsValue) -> JsValue;
}

pub trait JsCollectionIntoValue {
    fn into_value(self) -> JsValue;
}

pub trait JsCollectionFromValue {
    fn from_value(val: JsValue) -> Self;
}

/// Container holding a reference to an [`Object`] in JavaScript as well as
/// expected types for both the keys and values.
pub struct JsHashMap<K, V> {
    map: Object,
    _phantom: PhantomData<(K, V)>,
}

impl<K, V> JsHashMap<K, V>
where
    K: JsCollectionFromValue,
{
    pub fn keys(&self) -> impl Iterator<Item = K> {
        let array = Object::keys(self.map.unchecked_ref());

        OwnedArrayIter::new(array)
    }
}

impl<K, V> JsHashMap<K, V>
where
    V: JsCollectionFromValue,
{
    pub fn values(&self) -> impl Iterator<Item = V> {
        let array = Object::values(self.map.unchecked_ref());

        OwnedArrayIter::new(array)
    }
}

impl<K, V> JsHashMap<K, V>
where
    K: JsCollectionIntoValue,
    V: JsCollectionFromValue,
{
    pub fn get(&self, key: K) -> Option<V> {
        let key = key.into_value();
        let val = JsCast::unchecked_ref::<ObjectExt>(&self.map).get_value(&key);
        if val.is_null() || val.is_undefined() {
            return None;
        }
        let val = V::from_value(val);

        Some(val)
    }
}

impl<K, V> JsHashMap<K, V>
where
    K: JsCollectionIntoValue,
    V: JsCollectionIntoValue,
{
    pub fn set(&self, key: K, value: V) {
        let key = key.into_value();
        let value = value.into_value();
        JsCast::unchecked_ref::<ObjectExt>(&self.map).set_value(&key, &value);
    }
}

impl<K, V> From<Object> for JsHashMap<K, V> {
    fn from(map: Object) -> Self {
        Self {
            map,
            _phantom: Default::default(),
        }
    }
}

impl<K, V> From<JsValue> for JsHashMap<K, V> {
    fn from(val: JsValue) -> Self {
        Self {
            map: val.into(),
            _phantom: Default::default(),
        }
    }
}

#[derive(Debug, Clone)]
pub struct OwnedArrayIter<T> {
    range: std::ops::Range<u32>,
    array: Array,
    _phantom: PhantomData<T>,
}

impl<T> OwnedArrayIter<T> {
    pub fn new(array: Array) -> Self {
        OwnedArrayIter {
            range: 0..array.length(),
            array,
            _phantom: Default::default(),
        }
    }
}

impl<T> std::iter::Iterator for OwnedArrayIter<T>
where
    T: JsCollectionFromValue,
{
    type Item = T;

    fn next(&mut self) -> Option<Self::Item> {
        let index = self.range.next()?;
        let val = self.array.get(index);
        let val = T::from_value(val);
        Some(val)
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        self.range.size_hint()
    }
}

impl<T> std::iter::DoubleEndedIterator for OwnedArrayIter<T>
where
    T: JsCollectionFromValue,
{
    fn next_back(&mut self) -> Option<Self::Item> {
        let index = self.range.next_back()?;
        let val = self.array.get(index);
        let val = T::from_value(val);
        Some(val)
    }
}

impl<T> std::iter::FusedIterator for OwnedArrayIter<T> where T: JsCollectionFromValue {}

impl<T> std::iter::ExactSizeIterator for OwnedArrayIter<T> where T: JsCollectionFromValue {}

/// Represents a reference to an Object ID string in JavaScript memory, typed
/// according to the object type Rust expects for the object after resolving.
///
/// Use [`ObjectId`] if a value stored in Rust memory is preferred; the
/// JavaScript representation can be harder to work with in Rust code due to
/// lack of visibility on the underlying string and lack of most trait
/// implementations, and consumes more memory, but is faster to resolve and may
/// be useful with objects you plan to resolve frequently.
///
/// This object ID is typed, but not strictly, and can be converted into
/// referring into another type of object with [`JsObjectId::into_type`].
///
/// [`ObjectId`]: crate::local::ObjectId
// Copy, Clone, Debug, PartialEq, Eq, Hash, PartialEq, Eq implemented manually
// below
pub struct JsObjectId<T> {
    pub raw: JsString,
    phantom: PhantomData<T>,
}

// traits implemented manually so they don't depend on `T` implementing them.
impl<T> Clone for JsObjectId<T> {
    fn clone(&self) -> JsObjectId<T> {
        JsObjectId {
            raw: self.raw.clone(),
            phantom: PhantomData,
        }
    }
}
impl<T> fmt::Debug for JsObjectId<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.raw.fmt(f)
    }
}
impl<T> PartialEq for JsObjectId<T> {
    fn eq(&self, o: &JsObjectId<T>) -> bool {
        self.raw.eq(&o.raw)
    }
}
impl<T> Eq for JsObjectId<T> {}

impl<T> FromStr for JsObjectId<T> {
    type Err = RawObjectIdParseError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let js_string: JsString = s.into();
        Ok(js_string.into())
    }
}

impl<T> fmt::Display for JsObjectId<T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        String::from(&self.raw).fmt(f)
    }
}

impl<T> JsObjectId<T> {
    /// Changes the type this [`JsObjectId`] points to, unchecked.
    ///
    /// This will allow changing to any type - `JsObjectId` makes no guarantees
    /// about its ID matching the type of any object in the game that it
    /// actually points to.
    pub fn into_type<U>(self) -> JsObjectId<U> {
        JsObjectId {
            raw: self.raw,
            phantom: PhantomData,
        }
    }

    /// Resolves this ID into an object, assuming the type `T` is the correct
    /// type of object that this ID refers to. If the ID has been converted to
    /// an invalid type, using the returned object in a way not valid for its
    /// type will cause a panic.
    ///
    /// Will return `None` if this object no longer exists, or is in a room we
    /// don't have vision for.
    pub fn resolve(&self) -> Option<T>
    where
        T: MaybeHasId + JsCast,
    {
        game::get_object_by_js_id_typed(self)
    }
}

impl<T> From<JsString> for JsObjectId<T> {
    fn from(raw: JsString) -> Self {
        JsObjectId {
            raw,
            phantom: PhantomData,
        }
    }
}

impl<T> From<JsObjectId<T>> for JsString {
    fn from(id: JsObjectId<T>) -> Self {
        id.raw
    }
}

impl<T> From<JsObjectId<T>> for String {
    fn from(id: JsObjectId<T>) -> Self {
        id.to_string()
    }
}

//
// Utility conversions for collections.
//
impl JsCollectionIntoValue for JsString {
    fn into_value(self) -> JsValue {
        self.unchecked_into()
    }
}

impl JsCollectionFromValue for JsString {
    fn from_value(val: JsValue) -> JsString {
        val.unchecked_into()
    }
}

impl JsCollectionIntoValue for String {
    fn into_value(self) -> JsValue {
        self.into()
    }
}

impl JsCollectionFromValue for String {
    fn from_value(val: JsValue) -> String {
        let val: JsString = val.unchecked_into();

        val.into()
    }
}

impl JsCollectionIntoValue for u8 {
    fn into_value(self) -> JsValue {
        JsValue::from_f64(self as f64)
    }
}

impl JsCollectionFromValue for u8 {
    fn from_value(val: JsValue) -> u8 {
        if let Some(val) = val.as_string() {
            val.parse::<u8>().expect("expected parseable u8 string")
        } else {
            val.as_f64().expect("expected number value") as u8
        }
    }
}