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
use crate::Source;
use std::collections::{BTreeMap, BTreeSet};

/// Tracks all of the import and intrinsics that a given codegen
/// requires and how to generate them when needed.
#[derive(Default)]
pub struct Dependencies {
    pub needs_clamp: bool,
    pub needs_store: bool,
    pub needs_load: bool,
    pub needs_validate_guest_char: bool,
    pub needs_expected: bool,
    pub needs_i32_to_f32: bool,
    pub needs_f32_to_i32: bool,
    pub needs_i64_to_f64: bool,
    pub needs_f64_to_i64: bool,
    pub needs_decode_utf8: bool,
    pub needs_encode_utf8: bool,
    pub needs_list_canon_lift: bool,
    pub needs_list_canon_lower: bool,
    pub needs_t_typevar: bool,
    pub needs_resources: bool,
    pub pyimports: BTreeMap<String, Option<BTreeSet<String>>>,
}

impl Dependencies {
    /// Record that a Python import is required
    ///
    /// Examples
    /// ```
    /// # use wai_bindgen_gen_wasmer_py::dependencies::Dependencies;
    /// # let mut deps = Dependencies::default();
    /// // Import a specific item from a module
    /// deps.pyimport("typing", "NamedTuple");
    /// // Import an entire module
    /// deps.pyimport("collections", None);
    /// ```
    pub fn pyimport<'a>(&mut self, module: &str, name: impl Into<Option<&'a str>>) {
        let name = name.into();
        let list = self
            .pyimports
            .entry(module.to_string())
            .or_insert(name.map(|_| BTreeSet::new()));
        match name {
            Some(name) => {
                assert!(list.is_some());
                list.as_mut().unwrap().insert(name.to_string());
            }
            None => assert!(list.is_none()),
        }
    }

    /// Create a `Source` containing all of the intrinsics
    /// required according to this `Dependencies` struct.
    pub fn intrinsics(&mut self) -> Source {
        let mut src = Source::default();

        if self.needs_clamp {
            src.push_str(
                "
                    def _clamp(i: int, min: int, max: int) -> int:
                        if i < min or i > max:
                            raise OverflowError(f'must be between {min} and {max}')
                        return i
                ",
            );
        }
        if self.needs_store {
            self.pyimport("typing", "Callable");
            // TODO: this uses native endianness
            src.push_str(
                "
                    def _store(make_view: Callable[[], Any], mem: wasmer.Memory, base: int, offset: int, val: Any) -> None:
                        ptr = (base & 0xffffffff) + offset
                        view = make_view()
                        if ptr + view.bytes_per_element > mem.data_size:
                            raise IndexError('out-of-bounds store')
                        view_ptr = ptr // view.bytes_per_element
                        view[view_ptr] = val
                ",
            );
        }
        if self.needs_load {
            self.pyimport("typing", "Callable");
            // TODO: this uses native endianness
            src.push_str(
                "
                    def _load(make_view: Callable[[], Any], mem: wasmer.Memory, base: int, offset: int) -> Any:
                        ptr = (base & 0xffffffff) + offset
                        view = make_view()
                        if ptr + view.bytes_per_element > mem.data_size:
                            raise IndexError('out-of-bounds load')
                        view_ptr = ptr // view.bytes_per_element
                        return view[view_ptr]
                ",
            );
        }
        if self.needs_validate_guest_char {
            src.push_str(
                "
                    def _validate_guest_char(i: int) -> str:
                        if i > 0x10ffff or (i >= 0xd800 and i <= 0xdfff):
                            raise TypeError('not a valid char');
                        return chr(i)
                ",
            );
        }
        if self.needs_expected {
            self.pyimport("dataclasses", "dataclass");
            self.pyimport("typing", "TypeVar");
            self.pyimport("typing", "Generic");
            self.pyimport("typing", "Union");
            self.needs_t_typevar = true;
            src.push_str(
                "
                    @dataclass
                    class Ok(Generic[T]):
                        value: T
                    E = TypeVar('E')
                    @dataclass
                    class Err(Generic[E]):
                        value: E

                    Expected = Union[Ok[T], Err[E]]
                ",
            );
        }
        if self.needs_i32_to_f32 || self.needs_f32_to_i32 {
            self.pyimport("ctypes", None);
            src.push_str("_i32_to_f32_i32 = ctypes.pointer(ctypes.c_int32(0))\n");
            src.push_str(
                "_i32_to_f32_f32 = ctypes.cast(_i32_to_f32_i32, ctypes.POINTER(ctypes.c_float))\n",
            );
            if self.needs_i32_to_f32 {
                src.push_str(
                    "
                        def _i32_to_f32(i: int) -> float:
                            _i32_to_f32_i32[0] = i     # type: ignore
                            return _i32_to_f32_f32[0]  # type: ignore
                    ",
                );
            }
            if self.needs_f32_to_i32 {
                src.push_str(
                    "
                        def _f32_to_i32(i: float) -> int:
                            _i32_to_f32_f32[0] = i    # type: ignore
                            return _i32_to_f32_i32[0] # type: ignore
                    ",
                );
            }
        }
        if self.needs_i64_to_f64 || self.needs_f64_to_i64 {
            self.pyimport("ctypes", None);
            src.push_str("_i64_to_f64_i64 = ctypes.pointer(ctypes.c_int64(0))\n");
            src.push_str(
                "_i64_to_f64_f64 = ctypes.cast(_i64_to_f64_i64, ctypes.POINTER(ctypes.c_double))\n",
            );
            if self.needs_i64_to_f64 {
                src.push_str(
                    "
                        def _i64_to_f64(i: int) -> float:
                            _i64_to_f64_i64[0] = i    # type: ignore
                            return _i64_to_f64_f64[0] # type: ignore
                    ",
                );
            }
            if self.needs_f64_to_i64 {
                src.push_str(
                    "
                        def _f64_to_i64(i: float) -> int:
                            _i64_to_f64_f64[0] = i    # type: ignore
                            return _i64_to_f64_i64[0] # type: ignore
                    ",
                );
            }
        }
        if self.needs_decode_utf8 {
            src.push_str(
                "
                    def _decode_utf8(mem: wasmer.Memory, ptr: int, len: int) -> str:
                        ptr = ptr & 0xffffffff
                        len = len & 0xffffffff
                        if ptr + len > mem.data_size:
                            raise IndexError('string out of bounds')
                        view = mem.uint8_view()
                        bytes = bytearray(view[ptr:ptr+len])
                        x = bytes.decode('utf8')
                        return x
                ",
            );
        }
        if self.needs_encode_utf8 {
            self.pyimport("typing", "Tuple");
            src.push_str(
                "
                    def _encode_utf8(val: str, realloc: wasmer.Function, mem: wasmer.Memory) -> Tuple[int, int]:
                        bytes = val.encode('utf8')
                        ptr = realloc(0, 0, 1, len(bytes))
                        assert(isinstance(ptr, int))
                        ptr = ptr & 0xffffffff
                        if ptr + len(bytes) > mem.data_size:
                            raise IndexError('string out of bounds')
                        view = mem.uint8_view()
                        view[ptr:ptr+len(bytes)] = bytes
                        return (ptr, len(bytes))
                ",
            );
        }
        if self.needs_list_canon_lift {
            self.pyimport("ctypes", None);
            self.pyimport("typing", "List");
            self.pyimport("typing", "Callable");
            // TODO: this is doing a native-endian read, not a little-endian
            // read
            src.push_str(
                "
                    def _list_canon_lift(ptr: int, len: int, size: int, make_view: Callable[[], Any], mem: wasmer.Memory) -> Any:
                        ptr = ptr & 0xffffffff
                        len = len & 0xffffffff
                        if ptr + len * size > mem.data_size:
                            raise IndexError('list out of bounds')
                        view = make_view()
                        assert(size == view.bytes_per_element)
                        view_ptr = ptr // view.bytes_per_element
                        if isinstance(view, wasmer.Uint8Array):
                            return bytearray(view[view_ptr:view_ptr+len])
                        return view[view_ptr:view_ptr + len]
                ",
            );
        }
        if self.needs_list_canon_lower {
            self.pyimport("typing", "List");
            self.pyimport("typing", "Tuple");
            self.pyimport("typing", "Callable");
            // TODO: this is doing a native-endian write, not a little-endian
            // write
            src.push_str(
                "
                    def _list_canon_lower(list: Any, make_view: Callable[[], Any], size: int, align: int, realloc: wasmer.Function, mem: wasmer.Memory) -> Tuple[int, int]:
                        total_size = size * len(list)
                        ptr = realloc(0, 0, align, total_size)
                        assert(isinstance(ptr, int))
                        ptr = ptr & 0xffffffff
                        if ptr + total_size > mem.data_size:
                            raise IndexError('list realloc return of bounds')
                        view = make_view()
                        assert(size == view.bytes_per_element)
                        view_ptr = ptr // view.bytes_per_element
                        view[view_ptr:view_ptr + len(list)] = list
                        return (ptr, len(list))
                ",
            );
        }

        if self.needs_resources {
            self.pyimport("typing", "TypeVar");
            self.pyimport("typing", "Generic");
            self.pyimport("typing", "List");
            self.pyimport("typing", "Optional");
            self.pyimport("dataclasses", "dataclass");
            self.needs_t_typevar = true;
            src.push_str(
                "
                    @dataclass
                    class SlabEntry(Generic[T]):
                        next: int
                        val: Optional[T]

                    class Slab(Generic[T]):
                        head: int
                        list: List[SlabEntry[T]]

                        def __init__(self) -> None:
                            self.list = []
                            self.head = 0

                        def insert(self, val: T) -> int:
                            if self.head >= len(self.list):
                                self.list.append(SlabEntry(next = len(self.list) + 1, val = None))
                            ret = self.head
                            slot = self.list[ret]
                            self.head = slot.next
                            slot.next = -1
                            slot.val = val
                            return ret

                        def get(self, idx: int) -> T:
                            if idx >= len(self.list):
                                raise IndexError('handle index not valid')
                            slot = self.list[idx]
                            if slot.next == -1:
                                assert(slot.val is not None)
                                return slot.val
                            raise IndexError('handle index not valid')

                        def remove(self, idx: int) -> T:
                            ret = self.get(idx)
                            slot = self.list[idx]
                            slot.val = None
                            slot.next = self.head
                            self.head = idx
                            return ret
                ",
            );
        }

        src
    }
}

#[cfg(test)]
mod test {
    use std::collections::{BTreeMap, BTreeSet};

    use super::Dependencies;

    #[test]
    fn test_pyimport_only_contents() {
        let mut deps = Dependencies::default();
        deps.pyimport("typing", None);
        deps.pyimport("typing", None);
        assert_eq!(deps.pyimports, BTreeMap::from([("typing".into(), None)]));
    }

    #[test]
    fn test_pyimport_only_module() {
        let mut deps = Dependencies::default();
        deps.pyimport("typing", "Union");
        deps.pyimport("typing", "List");
        deps.pyimport("typing", "NamedTuple");
        assert_eq!(
            deps.pyimports,
            BTreeMap::from([(
                "typing".into(),
                Some(BTreeSet::from([
                    "Union".into(),
                    "List".into(),
                    "NamedTuple".into()
                ]))
            )])
        );
    }

    #[test]
    #[should_panic]
    fn test_pyimport_conflicting() {
        let mut deps = Dependencies::default();
        deps.pyimport("typing", "NamedTuple");
        deps.pyimport("typing", None);
    }
}