wry-bindgen-runtime 0.1.0-alpha.5

Wry runtime transport for wry-bindgen semantic bindings
Documentation
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use std::boxed::Box;

use alloc::string::String;
use alloc::vec::Vec;

use super::TypeDef;

#[derive(Clone, Copy)]
pub struct JsFunctionSpec {
    code: JsFunctionCode,
}

impl JsFunctionSpec {
    pub const fn new(js_code: fn() -> String) -> Self {
        Self {
            code: JsFunctionCode::Global(js_code),
        }
    }

    pub const fn with_module(module: &'static JsModuleSpec, js_code: fn(&str) -> String) -> Self {
        Self {
            code: JsFunctionCode::Module { module, js_code },
        }
    }
}

#[derive(Clone, Copy)]
enum JsFunctionCode {
    Global(fn() -> String),
    Module {
        module: &'static JsModuleSpec,
        js_code: fn(&str) -> String,
    },
}

inventory::collect!(JsFunctionSpec);

#[derive(Clone, Copy)]
pub struct JsReexportSpec {
    name: &'static str,
    namespace: &'static [&'static str],
    code: JsFunctionCode,
}

impl JsReexportSpec {
    pub const fn new(
        name: &'static str,
        namespace: &'static [&'static str],
        js_code: fn() -> String,
    ) -> Self {
        Self {
            name,
            namespace,
            code: JsFunctionCode::Global(js_code),
        }
    }

    pub const fn with_module(
        module: &'static JsModuleSpec,
        name: &'static str,
        namespace: &'static [&'static str],
        js_code: fn(&str) -> String,
    ) -> Self {
        Self {
            name,
            namespace,
            code: JsFunctionCode::Module { module, js_code },
        }
    }
}

impl JsReexportSpec {
    pub(crate) fn module(&self) -> Option<&'static JsModuleSpec> {
        match self.code {
            JsFunctionCode::Global(_) => None,
            JsFunctionCode::Module { module, .. } => Some(module),
        }
    }

    pub(crate) fn parts(&self) -> (&'static str, &'static [&'static str], String) {
        let value = match self.code {
            JsFunctionCode::Global(js_code) => js_code(),
            JsFunctionCode::Module { module, js_code } => {
                let module_binding = alloc::format!("module_{:x}", module.const_hash());
                js_code(&module_binding)
            }
        };
        (self.name, self.namespace, value)
    }
}

inventory::collect!(JsReexportSpec);

#[derive(Clone, Copy)]
pub struct JsModuleSpec {
    value: &'static str,
    raw: bool,
}

impl JsModuleSpec {
    pub const fn new(content: &'static str) -> Self {
        Self {
            value: content,
            raw: false,
        }
    }

    pub const fn raw(specifier: &'static str) -> Self {
        Self {
            value: specifier,
            raw: true,
        }
    }

    pub const fn const_hash(&self) -> u64 {
        const FNV_OFFSET_BASIS: u64 = 0xcbf29ce484222325;
        const FNV_PRIME: u64 = 0x100000001b3;

        let mut hash = FNV_OFFSET_BASIS;
        let tag = self.raw as u8;
        hash ^= tag as u64;
        hash = hash.wrapping_mul(FNV_PRIME);
        let mut i = 0;
        let bytes = self.value.as_bytes();
        while i < bytes.len() {
            hash ^= bytes[i] as u64;
            hash = hash.wrapping_mul(FNV_PRIME);
            i += 1;
        }
        hash
    }
}

#[derive(Clone, Copy)]
pub struct JsClassSpec {
    class_name: &'static str,
    js_name: &'static str,
    js_namespace: &'static [&'static str],
    private: bool,
    extends: Option<&'static str>,
    extends_js_class: Option<&'static str>,
    extends_js_namespace: &'static [&'static str],
    /// `#[wasm_bindgen(inspectable)]`: emit JS `toJSON`/`toString` methods (over
    /// the public field getters) unless the user defines their own.
    inspectable: bool,
    /// The JS names of the public (getter-exposed) fields, for the generated
    /// `toJSON` body when `inspectable`.
    public_fields: &'static [&'static str],
}

impl JsClassSpec {
    #[allow(clippy::too_many_arguments)]
    pub const fn new(
        class_name: &'static str,
        js_name: &'static str,
        js_namespace: &'static [&'static str],
        private: bool,
        extends: Option<&'static str>,
        extends_js_class: Option<&'static str>,
        extends_js_namespace: &'static [&'static str],
        inspectable: bool,
        public_fields: &'static [&'static str],
    ) -> Self {
        Self {
            class_name,
            js_name,
            js_namespace,
            private,
            extends,
            extends_js_class,
            extends_js_namespace,
            inspectable,
            public_fields,
        }
    }
}

pub(super) type JsClassParts = (
    &'static str,
    &'static str,
    &'static [&'static str],
    bool,
    Option<&'static str>,
    Option<&'static str>,
    &'static [&'static str],
    bool,
    &'static [&'static str],
);

impl JsClassSpec {
    pub(crate) fn parts(&self) -> JsClassParts {
        (
            self.class_name,
            self.js_name,
            self.js_namespace,
            self.private,
            self.extends,
            self.extends_js_class,
            self.extends_js_namespace,
            self.inspectable,
            self.public_fields,
        )
    }
}

inventory::collect!(JsClassSpec);

impl JsFunctionSpec {
    pub(crate) fn module(&self) -> Option<&'static JsModuleSpec> {
        match self.code {
            JsFunctionCode::Global(_) => None,
            JsFunctionCode::Module { module, .. } => Some(module),
        }
    }

    pub(crate) fn render_js_code(&self) -> String {
        match self.code {
            JsFunctionCode::Global(js_code) => js_code(),
            JsFunctionCode::Module { module, js_code } => {
                let module_binding = alloc::format!("module_{:x}", module.const_hash());
                js_code(&module_binding)
            }
        }
    }

    pub(crate) fn identity_eq(&self, other: &JsFunctionSpec) -> bool {
        match (self.code, other.code) {
            (JsFunctionCode::Global(a), JsFunctionCode::Global(b)) => a as usize == b as usize,
            (
                JsFunctionCode::Module {
                    module: module_a,
                    js_code: js_code_a,
                },
                JsFunctionCode::Module {
                    module: module_b,
                    js_code: js_code_b,
                },
            ) => core::ptr::eq(module_a, module_b) && js_code_a as usize == js_code_b as usize,
            _ => false,
        }
    }
}

impl JsModuleSpec {
    pub(crate) fn content(&self) -> Option<&'static str> {
        if self.raw { None } else { Some(self.value) }
    }

    pub(crate) fn raw_specifier(&self) -> Option<&'static str> {
        if self.raw { Some(self.value) } else { None }
    }
}

#[derive(Clone, Copy)]
#[non_exhaustive]
pub enum JsClassMemberKind {
    Constructor,
    Method,
    StaticMethod,
    Getter,
    Setter,
    StaticGetter,
    StaticSetter,
}

#[derive(Clone, Copy)]
pub struct JsClassMemberSpec {
    class_name: &'static str,
    member_name: &'static str,
    export_name: &'static str,
    arg_types: fn() -> Vec<TypeDef>,
    return_type: fn() -> TypeDef,
    kind: JsClassMemberKind,
    /// Whether the member takes `self` by value, consuming the receiver. The
    /// generated wrapper zeroes `this.__handle` after the call so a later use
    /// throws "Attempt to use a moved value" (wasm-bindgen's behavior).
    consumes_self: bool,
}

impl JsClassMemberSpec {
    pub const fn new(
        class_name: &'static str,
        member_name: &'static str,
        export_name: &'static str,
        arg_types: fn() -> Vec<TypeDef>,
        return_type: fn() -> TypeDef,
        kind: JsClassMemberKind,
        consumes_self: bool,
    ) -> Self {
        Self {
            class_name,
            member_name,
            export_name,
            arg_types,
            return_type,
            kind,
            consumes_self,
        }
    }
}

pub(super) type JsClassMemberParts = (
    &'static str,
    &'static str,
    &'static str,
    Vec<TypeDef>,
    TypeDef,
    JsClassMemberKind,
    bool,
);

impl JsClassMemberSpec {
    pub(crate) fn parts(&self) -> JsClassMemberParts {
        (
            self.class_name,
            self.member_name,
            self.export_name,
            (self.arg_types)(),
            (self.return_type)(),
            self.kind,
            self.consumes_self,
        )
    }
}

inventory::collect!(JsClassMemberSpec);

#[derive(Clone)]
pub struct JsFunctionArg {
    pub name: &'static str,
    pub ty: TypeDef,
}

#[derive(Clone)]
pub struct JsFunctionSignature {
    name: &'static str,
    namespace: &'static [&'static str],
    args: Vec<JsFunctionArg>,
    return_type: TypeDef,
    this: bool,
    public: bool,
    start: bool,
    /// Whether the export is `#[wasm_bindgen(variadic)]`: the generated JS
    /// wrapper collects its trailing arguments into the final parameter (a rest
    /// parameter) so a spread call like `f(...arr)` reaches Rust as one array.
    variadic: bool,
}

impl JsFunctionSignature {
    #[allow(clippy::too_many_arguments)]
    pub const fn new(
        name: &'static str,
        namespace: &'static [&'static str],
        args: Vec<JsFunctionArg>,
        return_type: TypeDef,
        this: bool,
        public: bool,
        start: bool,
        variadic: bool,
    ) -> Self {
        Self {
            name,
            namespace,
            args,
            return_type,
            this,
            public,
            start,
            variadic,
        }
    }

    pub(crate) fn name(&self) -> &'static str {
        self.name
    }

    pub(crate) fn namespace(&self) -> &'static [&'static str] {
        self.namespace
    }

    pub(crate) fn args(&self) -> &[JsFunctionArg] {
        &self.args
    }

    pub(crate) fn return_type(&self) -> &TypeDef {
        &self.return_type
    }

    pub(crate) fn this(&self) -> bool {
        self.this
    }

    pub(crate) fn public(&self) -> bool {
        self.public
    }

    pub(crate) fn start(&self) -> bool {
        self.start
    }

    pub(crate) fn variadic(&self) -> bool {
        self.variadic
    }
}

pub struct JsExportSpec {
    signature: JsFunctionSignature,
    handler: Box<dyn Fn(&mut super::DecodedData) -> Result<super::EncodedData, String>>,
}

impl JsExportSpec {
    pub fn new(
        signature: JsFunctionSignature,
        handler: impl Fn(&mut super::DecodedData) -> Result<super::EncodedData, String> + 'static,
    ) -> Self {
        Self {
            signature,
            handler: Box::new(handler),
        }
    }

    pub(crate) fn call(
        &self,
        data: &mut super::DecodedData<'_>,
    ) -> Result<super::EncodedData, String> {
        (self.handler)(data)
    }

    pub fn signature(&self) -> &JsFunctionSignature {
        &self.signature
    }
}

#[derive(Clone, Copy)]
pub struct JsExportSpecRegistration {
    register: fn() -> JsExportSpec,
}

impl JsExportSpecRegistration {
    pub const fn new(register: fn() -> JsExportSpec) -> Self {
        Self { register }
    }

    pub(crate) fn spec(&self) -> JsExportSpec {
        (self.register)()
    }
}

inventory::collect!(JsExportSpecRegistration);