js-component-bindgen 2.18.0

JS component bindgen for transpiling WebAssembly components into JavaScript
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
//! Runtime that imports named exports from an external Component Model runtime
//! package, rendering every intrinsic it does not import inline. Keep public suffix
//! selection and base resolution here.

use anyhow::{Result, bail, ensure};

use super::component::ComponentIntrinsic;
use super::conversion::ConversionIntrinsic;
use super::inline::InlineRuntime;
use super::js_helper::JsHelperIntrinsic;
use super::lift::LiftIntrinsic;
use super::lower::LowerIntrinsic;
use super::p3::async_future::AsyncFutureIntrinsic;
use super::p3::async_stream::AsyncStreamIntrinsic;
use super::p3::async_task::AsyncTaskIntrinsic;
use super::p3::error_context::ErrCtxIntrinsic;
use super::p3::host::HostIntrinsic;
use super::p3::waitable::WaitableIntrinsic;
use super::resource::ResourceIntrinsic;
use super::runtime::{self, BaseIntrinsic, IntrinsicBinding, IntrinsicRuntime, Provides};
use super::string::StringIntrinsic;
use super::webidl::WebIdlIntrinsic;
use super::{Intrinsic, RenderIntrinsicsArgs};
use crate::{names::js_string_literal, source::Source, uwriteln};
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::Write;

pub(crate) const CM_RUNTIME_API_VERSION: u32 = 1;
pub(crate) const VERSION_LOCAL: &str = "_jcoRuntimeApiVersion";
pub(crate) const TABLE_GET_CONTEXT: &str = "_jcoResourceTableGetContext";

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub(crate) enum RuntimeContext {
    ResourceTableGet,
}

impl RuntimeContext {
    pub(crate) fn local_name(self) -> &'static str {
        match self {
            Self::ResourceTableGet => TABLE_GET_CONTEXT,
        }
    }

    pub(crate) fn render(self, output: &mut Source) {
        match self {
            Self::ResourceTableGet => uwriteln!(
                output,
                "const {TABLE_GET_CONTEXT} = {{ RuntimeError: WebAssembly.RuntimeError }};"
            ),
        }
    }
}

/// A named call export. Context requirements are separate from the public name.
/// Add value/constructor/state descriptors when such exports are implemented.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub(crate) struct RuntimeImport {
    pub(crate) suffix: &'static str,
    pub(crate) export: &'static str,
    pub(crate) context: Option<RuntimeContext>,
}

/// Imports what the external runtime implements and delegates the rest to
/// [`InlineRuntime`]. To move an intrinsic into the runtime package, return its
/// import from the family's `*_import` table and give that family an explicit
/// [`Provides`] impl.
#[derive(Debug, Default, Copy, Clone)]
pub(crate) struct ExternalRuntime;

impl ExternalRuntime {
    /// Exports of `<base>/intrinsics/resource`.
    fn resource_import(intrinsic: ResourceIntrinsic) -> Option<RuntimeImport> {
        match intrinsic {
            ResourceIntrinsic::ResourceTableGet => Some(RuntimeImport {
                suffix: "intrinsics/resource",
                export: "tableGet",
                context: Some(RuntimeContext::ResourceTableGet),
            }),
            _ => None,
        }
    }

    fn import(intrinsic: Intrinsic) -> Option<RuntimeImport> {
        match intrinsic {
            Intrinsic::Resource(i) => Self::resource_import(i),
            _ => None,
        }
    }

    fn imports(
        intrinsics: &BTreeSet<Intrinsic>,
    ) -> impl Iterator<Item = (Intrinsic, RuntimeImport)> + '_ {
        intrinsics
            .iter()
            .filter_map(|&intrinsic| Some((intrinsic, Self::import(intrinsic)?)))
    }

    /// Called after dependency discovery, so transitive inline calls also get their imports.
    pub(crate) fn render_imports(
        &self,
        output: &mut Source,
        intrinsics: &BTreeSet<Intrinsic>,
        base: &str,
    ) {
        let mut modules = BTreeMap::<&str, BTreeSet<(&str, &str)>>::new();
        for (intrinsic, import) in Self::imports(intrinsics) {
            modules
                .entry(import.suffix)
                .or_default()
                .insert((import.export, intrinsic.name()));
        }
        if modules.is_empty() {
            return;
        }
        modules
            .entry("runtime")
            .or_default()
            .insert(("CM_RUNTIME_API_VERSION", VERSION_LOCAL));
        for (suffix, names) in modules {
            let names = names
                .into_iter()
                .map(|(export, local)| format!("{export} as {local}"))
                .collect::<Vec<_>>()
                .join(", ");
            let specifier =
                module_specifier(base, suffix).expect("runtime base was validated before codegen");
            uwriteln!(
                output,
                "import {{ {names} }} from {};",
                js_string_literal(&specifier)
            );
        }
    }
}

impl Provides<ResourceIntrinsic> for ExternalRuntime {
    fn binding(&self, intrinsic: ResourceIntrinsic) -> IntrinsicBinding {
        match Self::resource_import(intrinsic) {
            Some(_) => IntrinsicBinding::Imported {
                local_name: intrinsic.name(),
            },
            None => InlineRuntime.binding(intrinsic),
        }
    }

    fn render(
        &self,
        intrinsic: ResourceIntrinsic,
        output: &mut Source,
        args: &RenderIntrinsicsArgs<'_>,
    ) {
        if Self::resource_import(intrinsic).is_none() {
            InlineRuntime.render(intrinsic, output, args);
        }
    }
}

/// Families the external runtime does not implement yet.
macro_rules! delegate_to_inline {
    ($($family:ty),+ $(,)?) => {
        $(
            impl Provides<$family> for ExternalRuntime {
                fn binding(&self, intrinsic: $family) -> IntrinsicBinding {
                    InlineRuntime.binding(intrinsic)
                }

                fn render(
                    &self,
                    intrinsic: $family,
                    output: &mut Source,
                    args: &RenderIntrinsicsArgs<'_>,
                ) {
                    InlineRuntime.render(intrinsic, output, args);
                }
            }
        )+
    };
}

delegate_to_inline! {
    BaseIntrinsic,
    JsHelperIntrinsic,
    WebIdlIntrinsic,
    ConversionIntrinsic,
    StringIntrinsic,
    ErrCtxIntrinsic,
    AsyncTaskIntrinsic,
    WaitableIntrinsic,
    LiftIntrinsic,
    LowerIntrinsic,
    AsyncStreamIntrinsic,
    AsyncFutureIntrinsic,
    ComponentIntrinsic,
    HostIntrinsic,
}

impl IntrinsicRuntime for ExternalRuntime {
    fn render_prelude(&self, intrinsics: &BTreeSet<Intrinsic>, output: &mut Source) {
        let mut imports = Self::imports(intrinsics).peekable();
        if imports.peek().is_none() {
            return;
        }
        uwriteln!(
            output,
            r#"
                if ({VERSION_LOCAL} !== {CM_RUNTIME_API_VERSION}) {{
                    throw new Error(`incompatible Jco JavaScript Component Model runtime API: requested {CM_RUNTIME_API_VERSION}, supported ${{{VERSION_LOCAL}}}`);
                }}
            "#,
        );
        let mut contexts = BTreeSet::new();
        for (_, import) in imports {
            if let Some(context) = import.context
                && contexts.insert(context.local_name())
            {
                context.render(output);
            }
        }
    }

    /// Insert only the context required by this operation.
    fn call(&self, intrinsic: Intrinsic, operands: &str) -> String {
        let name = runtime::binding(self, intrinsic).local_name();
        match Self::import(intrinsic).and_then(|import| import.context) {
            Some(context) => {
                let context = context.local_name();
                let separator = if operands.is_empty() { "" } else { ", " };
                format!("{name}({context}{separator}{operands})")
            }
            None => format!("{name}({operands})"),
        }
    }
}

/// Resolve an informal runtime package/directory contract without consulting the filesystem.
pub(crate) fn module_specifier(base: &str, suffix: &str) -> Result<String> {
    ensure!(
        !base.trim().is_empty() && !base.chars().any(char::is_control),
        "cmRuntimeEsModule must be a non-empty base specifier without control characters"
    );
    let directory_path = base == "."
        || base == ".."
        || base.starts_with("./")
        || base.starts_with("../")
        || base.starts_with('/');
    let url = if !directory_path
        && let Some((scheme, _)) = base
            .split_once(':')
            .filter(|(prefix, _)| !prefix.contains('/'))
    {
        ensure!(
            ["file", "http", "https"]
                .iter()
                .any(|supported| scheme.eq_ignore_ascii_case(supported))
                && base.starts_with(&format!("{scheme}://")),
            "cmRuntimeEsModule has an unsupported URL scheme; use a package, directory, or file/HTTP(S) URL (including file URLs on Windows)"
        );
        true
    } else {
        false
    };
    let directory = url || directory_path;
    let base = base.strip_suffix('/').unwrap_or(base);
    if directory {
        ensure!(
            !base.contains(['?', '#']),
            "cmRuntimeEsModule directory bases must not contain a query or fragment"
        );
        if [".js", ".mjs", ".cjs"]
            .iter()
            .any(|ext| base.ends_with(ext))
        {
            bail!(
                "cmRuntimeEsModule now identifies a runtime directory base, not a provider file; replace the file specifier with a directory containing runtime.js and intrinsics/*.js"
            );
        }
    }
    Ok(format!(
        "{base}/{suffix}{}",
        if directory { ".js" } else { "" }
    ))
}

#[cfg(all(test, feature = "external-runtime"))]
mod tests {
    use super::*;
    use crate::TranspileOpts;
    use crate::intrinsics::{RenderIntrinsicsArgs, render_intrinsics, resource::ResourceIntrinsic};

    #[test]
    fn transitive_named_calls_keep_inline_dependencies_and_deduplicate_imports() {
        let mut intrinsics = BTreeSet::from([Intrinsic::Resource(
            ResourceIntrinsic::ResourceTransferBorrow,
        )]);
        let opts = TranspileOpts::builder().name("mixed".into()).build();
        let source = render_intrinsics(
            RenderIntrinsicsArgs::builder()
                .intrinsics(&mut intrinsics)
                .transpile_opts(&opts)
                .external_runtime(true)
                .build(),
        );
        assert!(source.contains("rscTableGet(_jcoResourceTableGetContext, fromTable, handle)"));
        assert!(source.contains("function rscTableCreateBorrow("));
        assert!(source.contains("const HANDLE_TABLES"));
        assert!(!source.contains("function rscTableGet("));
        assert!(!source.contains("_jcoRuntimeProvider"));
        let mut imports = Source::default();
        ExternalRuntime.render_imports(&mut imports, &intrinsics, "custom-runtime");
        assert_eq!(imports.matches("tableGet as rscTableGet").count(), 1);
        assert_eq!(imports.matches("CM_RUNTIME_API_VERSION as").count(), 1);
    }

    #[test]
    fn resource_context_needs_no_unrelated_initializers() {
        let mut intrinsics =
            BTreeSet::from([Intrinsic::Resource(ResourceIntrinsic::ResourceTableGet)]);
        let opts = TranspileOpts::builder().name("resource".into()).build();
        let source = render_intrinsics(
            RenderIntrinsicsArgs::builder()
                .intrinsics(&mut intrinsics)
                .transpile_opts(&opts)
                .external_runtime(true)
                .build(),
        );
        assert_eq!(intrinsics.len(), 1);
        assert!(source.contains("RuntimeError: WebAssembly.RuntimeError"));
        for unrelated in [
            "T_FLAG",
            "HANDLE_TABLES",
            "TextEncoder",
            "TextDecoder",
            "WebAssembly.Global",
            "Suspending",
            "new Map",
            "new Int32Array",
        ] {
            assert!(!source.contains(unrelated), "{unrelated}");
        }
    }

    #[test]
    fn resolves_runtime_bases() {
        for (base, expected) in [
            ("@scope/runtime", "@scope/runtime/intrinsics/resource"),
            ("custom-runtime/", "custom-runtime/intrinsics/resource"),
            ("something.js", "something.js/intrinsics/resource"),
            ("custom/nested", "custom/nested/intrinsics/resource"),
            ("./runtime", "./runtime/intrinsics/resource.js"),
            ("../runtime/", "../runtime/intrinsics/resource.js"),
            ("/runtime/", "/runtime/intrinsics/resource.js"),
            ("/", "/intrinsics/resource.js"),
            (
                "./runtime:version",
                "./runtime:version/intrinsics/resource.js",
            ),
            (".", "./intrinsics/resource.js"),
            (
                "file:///C:/runtime/",
                "file:///C:/runtime/intrinsics/resource.js",
            ),
            ("file:///a%20b/", "file:///a%20b/intrinsics/resource.js"),
            (
                "https://example.test/runtime/",
                "https://example.test/runtime/intrinsics/resource.js",
            ),
            (
                "HTTPS://example.test/runtime/",
                "HTTPS://example.test/runtime/intrinsics/resource.js",
            ),
            (
                "http://example.test/a//b",
                "http://example.test/a//b/intrinsics/resource.js",
            ),
            ("./a b'\"", "./a b'\"/intrinsics/resource.js"),
        ] {
            assert_eq!(
                module_specifier(base, "intrinsics/resource").unwrap(),
                expected
            );
        }
    }

    #[test]
    fn rejects_unsupported_bases() {
        for base in [
            "",
            " ",
            "a\nb",
            "a\0b",
            "data:text/javascript,",
            "ftp://example.test/a",
            "C:\\runtime",
            "./a?b",
            "file:///a#b",
            "https://example.test/a?q",
        ] {
            assert!(module_specifier(base, "runtime").is_err(), "{base}");
        }
        for base in [
            "./provider.js",
            "../provider.mjs/",
            "file:///provider.cjs",
            "https://example.test/provider.js",
        ] {
            assert!(
                module_specifier(base, "runtime")
                    .unwrap_err()
                    .to_string()
                    .contains("directory base")
            );
        }
    }
}