wasm-rquickjs 0.4.4

Tool for wrapping JavaScript modules as WebAssembly components using the QuickJS engine
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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
use crate::conversions::generate_conversions;
use crate::exports::generate_export_impls;
use crate::imports::generate_import_modules;
use crate::javascript::escape_js_ident;
use crate::skeleton::{copy_skeleton_lock, copy_skeleton_sources, generate_cargo_toml};
use crate::wit::{add_get_script_import, add_wizer_init_export};
use anyhow::{Context, anyhow};
use camino::{Utf8Path, Utf8PathBuf};
use heck::{ToLowerCamelCase, ToSnakeCase, ToUpperCamelCase};
use proc_macro2::{Ident, Span};
use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet, VecDeque};
use wit_parser::{
    Function, Interface, InterfaceId, PackageId, PackageName, PackageSourceMap, Resolve, TypeDef,
    TypeDefKind, TypeId, TypeOwner, WorldId, WorldItem, WorldKey,
};

/// WASI package namespaces whose interfaces are remapped to `wasip2::` in the generated code.
/// These correspond to interfaces provided by the `wasip2` crate and are mapped via the
/// `with:` block in `wit_bindgen::generate!`.
const WASI_REMAP_NAMESPACES: &[(&str, &str)] = &[
    ("cli", "cli"),
    ("clocks", "clocks"),
    ("filesystem", "filesystem"),
    ("http", "http"),
    ("io", "io"),
    ("random", "random"),
    ("sockets", "sockets"),
];

/// WASI package namespaces remapped to `wasip3::` in the WASI Preview 3 generation path.
///
/// Only the clock interfaces are remapped for now (Phase 1); every other WASI interface
/// present in a Preview 3 world keeps the bindings generated by `wit-bindgen` (i.e. it is
/// *not* treated as remapped). This list must stay consistent with the `with:` remap table
/// (`WASI_REMAPS_P3`) in `exports.rs`.
const WASI_REMAP_NAMESPACES_P3: &[(&str, &str)] = &[("clocks", "clocks")];

/// Selects which WASI generation the wrapper crate targets.
///
/// The default ([`GenerationTarget::WasiP2`]) reproduces the historical behavior:
/// synchronous exports/imports backed by the full Preview 2 skeleton (`wasip2`,
/// `wstd`, `golem-wasi-http`, Node.js builtins, Wizer pre-initialization).
///
/// [`GenerationTarget::WasiP3`] is the opt-in Preview 3 path. It uses the
/// **same** skeleton crate compiled with the `p3` Cargo feature instead of `p2`: a
/// minimal async runtime spine depending only on `rquickjs`, `wasip3` and a renamed
/// `wit-bindgen` (so the Node.js builtins are not duplicated). It supports synchronous
/// and asynchronous WIT exports and imports, including Wizer pre-initialization.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum GenerationTarget {
    /// WASI Preview 2 (default).
    #[default]
    WasiP2,
    /// WASI Preview 3 (opt-in, async).
    WasiP3,
}

impl GenerationTarget {
    pub fn is_p3(&self) -> bool {
        matches!(self, GenerationTarget::WasiP3)
    }
}

mod async_values;
mod conversions;
mod exports;
mod imports;
mod inject;
mod javascript;
#[cfg(feature = "optimize")]
mod optimize;
mod rust_bindgen;
mod skeleton;
mod types;
mod typescript;
mod wit;

pub use inject::{SLOT_END_MAGIC, SLOT_MAGIC, create_marker_file, inject_js_into_component};
#[cfg(feature = "optimize")]
pub use optimize::optimize_component;

/// Write `contents` to `path` only if the file doesn't exist or its current content differs.
/// This preserves file timestamps when content hasn't changed, avoiding unnecessary recompilation.
pub(crate) fn write_if_changed(
    path: impl AsRef<std::path::Path>,
    contents: impl AsRef<[u8]>,
) -> std::io::Result<()> {
    let path = path.as_ref();
    let contents = contents.as_ref();
    if let Ok(existing) = std::fs::read(path)
        && existing == contents
    {
        return Ok(());
    }
    std::fs::write(path, contents)
}

/// Copy a file from `src` to `dst` only if the destination doesn't exist or its content differs.
pub(crate) fn copy_if_changed(
    src: impl AsRef<std::path::Path>,
    dst: impl AsRef<std::path::Path>,
) -> std::io::Result<()> {
    let src = src.as_ref();
    let dst = dst.as_ref();
    let src_contents = std::fs::read(src)?;
    if let Ok(existing) = std::fs::read(dst)
        && existing == src_contents
    {
        return Ok(());
    }
    std::fs::write(dst, src_contents)
}

/// Specifies how a given user-defined JS module gets embedded into the generated Rust crate.
#[derive(Debug, Clone)]
pub enum EmbeddingMode {
    /// Points to a JS module file that is going to be embedded into the generated Rust crate
    EmbedFile(Utf8PathBuf),
    /// The JS module is going to be fetched run-time through an imported WIT interface
    Composition,
    /// Embeds a small marker in the compiled WASM component.
    /// After compilation, JS source can be injected into the marker via `inject_js_into_component`
    /// without recompiling the Rust crate. The injected JS can be any size — the WASM component
    /// is structurally rewritten to accommodate the new data.
    BinarySlot,
}

impl EmbeddingMode {
    pub fn is_binary_slot(&self) -> bool {
        matches!(self, EmbeddingMode::BinarySlot)
    }
}

/// Specifies a JS module to be evaluated in the generated component.
#[derive(Debug, Clone)]
pub struct JsModuleSpec {
    pub name: String,
    pub mode: EmbeddingMode,
}

impl JsModuleSpec {
    pub fn file_name(&self) -> String {
        self.name.replace('/', "_") + ".js"
    }
}

/// Generates a Rust wrapper crate for a combination of a WIT package and a JavaScript module.
///
/// The `wit` parameter should point to a WIT root (holding the WIT package of the component, with
/// optionally a `deps` subdirectory with an arbitrary number of dependencies).
///
/// The `js_modules` parameter must point to at least one JavaScript module that implements the WIT package,
/// and optionally additional modules that get imported during the initialization of the component. It is
/// always the first in the list that is considered the one containing the implementation of the WIT exports.
///
/// The `output` parameter is the root directory where the generated Rust crate's source code and
/// Cargo manifest is placed.
///
/// If `world` is `None`, the default world is selected and used, otherwise the specified one.
///
/// This always targets WASI Preview 2. Use [`generate_wrapper_crate_with_target`] to select the
/// (opt-in) WASI Preview 3 generation path.
pub fn generate_wrapper_crate(
    wit: &Utf8Path,
    js_modules: &[JsModuleSpec],
    output: &Utf8Path,
    world: Option<&str>,
) -> anyhow::Result<()> {
    generate_wrapper_crate_with_target(wit, js_modules, output, world, GenerationTarget::WasiP2)
}

/// Generates a Rust wrapper crate, selecting the WASI generation via `target`.
///
/// See [`generate_wrapper_crate`] for the description of the common parameters and
/// [`GenerationTarget`] for the differences between the Preview 2 and Preview 3 paths.
pub fn generate_wrapper_crate_with_target(
    wit: &Utf8Path,
    js_modules: &[JsModuleSpec],
    output: &Utf8Path,
    world: Option<&str>,
    target: GenerationTarget,
) -> anyhow::Result<()> {
    if target.is_p3() && uses_composition(js_modules) {
        anyhow::bail!(
            "Composition (@composition) JS modules are not supported by the WASI Preview 3 generation path yet"
        );
    }

    // Making sure the target directories exists
    std::fs::create_dir_all(output).context("Failed to create output directory")?;
    std::fs::create_dir_all(output.join("src")).context("Failed to create output/src directory")?;
    std::fs::create_dir_all(output.join("src").join("modules"))
        .context("Failed to create output/src/modules directory")?;

    // Resolving the WIT package (initial parse for Cargo.toml generation)
    let context = GeneratorContext::new(output, wit, world, target)?;

    // Generating the Cargo.toml file
    generate_cargo_toml(&context)?;

    // Copying the skeleton's Cargo.lock for faster dependency resolution
    copy_skeleton_lock(context.output, &context.world_name)
        .context("Failed to copy skeleton Cargo.lock")?;

    // Copying the skeleton files
    copy_skeleton_sources(context.output).context("Failed to copy skeleton sources")?;

    // Copying the WIT package to the output directory
    copy_wit_directory(wit, &context.output.join("wit"))
        .context("Failed to copy WIT package to output directory")?;

    if uses_composition(js_modules) {
        add_get_script_import(&context.output.join("wit"), world)
            .context("Failed to add get-script import to the WIT world")?;
    }

    // Add wizer-initialize export for pre-initialization support.
    add_wizer_init_export(&context.output.join("wit"), world, target.is_p3())
        .context("Failed to add wizer-initialize export to the WIT world")?;

    // Re-resolve the WIT package after modifications (wizer-initialize export was added)
    let modified_wit = output.join("wit");
    let context = GeneratorContext::new(output, &modified_wit, world, target)?;

    // Copying the JavaScript module to the output directory
    copy_js_modules(js_modules, context.output)
        .context("Failed to copy JavaScript module to output directory")?;

    // Generating the lib.rs file implementing the component exports
    generate_export_impls(&context, js_modules)
        .context("Failed to generate the component export implementations")?;

    // Generating the native modules implementing the component imports
    generate_import_modules(&context).context("Failed to generate the component import modules")?;

    // Generating the conversions.rs file implementing the IntoJs and FromJs typeclass instances
    // This step must be done after `generate_export_impls` to ensure all visited types are registered.
    generate_conversions(&context)
        .context("Failed to generate the IntoJs and FromJs typeclass instances")?;

    Ok(())
}

/// Generates TypeScript module definitions for a given (or default) world of a WIT package.
///
/// Returns the list of generated files.
pub fn generate_dts(
    wit: &Utf8Path,
    output: &Utf8Path,
    world: Option<&str>,
) -> anyhow::Result<Vec<Utf8PathBuf>> {
    generate_dts_with_target(wit, output, world, GenerationTarget::WasiP2)
}

/// Generates TypeScript module definitions, selecting the WASI generation via `target`.
///
/// Preview 2 permits a Promise-returning JavaScript implementation for every export. Preview 3
/// follows the WIT function kind: plain functions are synchronous and `async func` functions
/// return Promises.
pub fn generate_dts_with_target(
    wit: &Utf8Path,
    output: &Utf8Path,
    world: Option<&str>,
    target: GenerationTarget,
) -> anyhow::Result<Vec<Utf8PathBuf>> {
    // Making sure the target directories exist
    std::fs::create_dir_all(output).context("Failed to create output directory")?;

    let context = GeneratorContext::new(output, wit, world, target)?;

    let mut result = Vec::new();
    result.extend(
        typescript::generate_export_module(&context)
            .context("Failed to generate the TypeScript module definition for the exports")?,
    );

    // Generating the native modules implementing the component imports
    result.extend(typescript::generate_import_modules(&context).context(
        "Failed to generate the TypeScript module definitions for the imported modules",
    )?);

    Ok(result)
}

struct GeneratorContext<'a> {
    output: &'a Utf8Path,
    #[allow(dead_code)]
    wit_source_path: &'a Utf8Path,
    resolve: Resolve,
    root_package: PackageId,
    world: WorldId,
    #[allow(dead_code)]
    source_map: PackageSourceMap,
    visited_types: RefCell<BTreeSet<TypeId>>,
    world_name: String,
    types: wit_bindgen_core::Types,
    target: GenerationTarget,
}

impl<'a> GeneratorContext<'a> {
    fn new(
        output: &'a Utf8Path,
        wit: &'a Utf8Path,
        world: Option<&str>,
        target: GenerationTarget,
    ) -> anyhow::Result<Self> {
        let mut resolve = Resolve::default();
        let (root_package, source_map) = resolve
            .push_path(wit)
            .context("Failed to resolve WIT package")?;
        let world = resolve
            .select_world(std::slice::from_ref(&root_package), world)
            .context("Failed to select WIT world")?;

        if target.is_p3() {
            let mut unsupported = Vec::new();
            for (key, item) in &resolve.worlds[world].imports {
                let is_unsupported = match item {
                    WorldItem::Function(_) => true,
                    WorldItem::Type { id, .. } => resolve.types[*id].kind == TypeDefKind::Resource,
                    WorldItem::Interface { .. } => false,
                };
                if is_unsupported {
                    unsupported.push(match key {
                        WorldKey::Name(name) => name.clone(),
                        WorldKey::Interface(_) => "<resource>".to_string(),
                    });
                }
            }
            if !unsupported.is_empty() {
                return Err(anyhow!(
                    "Functions or resources declared directly in the world are not supported by \
                     the WASI Preview 3 generation path ({}); declare them inside an imported \
                     interface instead",
                    unsupported.join(", ")
                ));
            }
        }

        let world_name = resolve.worlds[world].name.clone();

        let mut types = wit_bindgen_core::Types::default();
        types.analyze(&resolve);

        Ok(Self {
            output,
            wit_source_path: wit,
            resolve,
            root_package,
            world,
            source_map,
            visited_types: RefCell::new(BTreeSet::new()),
            world_name,
            types,
            target,
        })
    }

    fn root_package_name(&self) -> String {
        self.resolve.packages[self.root_package].name.to_string()
    }

    fn record_visited_type(&self, type_id: TypeId) {
        self.visited_types.borrow_mut().insert(type_id);
    }

    fn is_exported_interface(&self, interface_id: InterfaceId) -> bool {
        let world = &self.resolve.worlds[self.world];
        world
            .exports
            .iter()
            .any(|(_, item)| matches!(item, WorldItem::Interface { id, .. } if id == &interface_id))
    }

    fn exported_interface_js_name(
        &self,
        interface_id: InterfaceId,
        export_name: &str,
    ) -> anyhow::Result<String> {
        let names = self.exported_interface_js_names()?;
        names
            .get(&interface_id)
            .cloned()
            .ok_or_else(|| anyhow!("Interface export not found: {export_name}"))
    }

    fn exported_interface_js_names(&self) -> anyhow::Result<BTreeMap<InterfaceId, String>> {
        let world = &self.resolve.worlds[self.world];
        let mut exported_interfaces = Vec::new();

        for (key, export) in &world.exports {
            if let WorldItem::Interface { id, .. } = export {
                let interface = &self.resolve.interfaces[*id];
                let export_name = match key {
                    WorldKey::Name(name) => name.as_str(),
                    WorldKey::Interface(_) => interface
                        .name
                        .as_deref()
                        .ok_or_else(|| anyhow!("Interface export does not have a name"))?,
                };
                let short_name = exported_interface_short_js_name(export_name);
                exported_interfaces.push((*id, export_name.to_string(), short_name));
            }
        }

        let mut short_name_counts = BTreeMap::<String, usize>::new();
        for (_, _, short_name) in &exported_interfaces {
            *short_name_counts.entry(short_name.clone()).or_default() += 1;
        }

        let mut result = BTreeMap::new();
        let mut used_names = BTreeMap::<String, InterfaceId>::new();
        for (interface_id, export_name, short_name) in exported_interfaces {
            let js_name = if short_name_counts.get(&short_name).copied().unwrap_or(0) > 1 {
                let interface = &self.resolve.interfaces[interface_id];
                exported_interface_qualified_js_name(self, interface, &export_name)?
            } else {
                short_name
            };

            if let Some(previous_id) = used_names.insert(js_name.clone(), interface_id) {
                anyhow::bail!(
                    "Exported WIT interfaces {previous_id:?} and {interface_id:?} both map to JavaScript export name '{js_name}'"
                );
            }

            result.insert(interface_id, js_name);
        }

        Ok(result)
    }

    fn is_exported_type(&self, type_id: TypeId) -> bool {
        if let Some(typ) = self.resolve.types.get(type_id) {
            match &typ.owner {
                TypeOwner::World(world_id) => {
                    if world_id == &self.world {
                        let world = &self.resolve.worlds[self.world];
                        world
                            .exports
                            .iter()
                            .any(|(_, item)| matches!(item, WorldItem::Type { id, .. } if id == &type_id))
                    } else {
                        false
                    }
                }
                TypeOwner::Interface(interface_id) => self.is_exported_interface(*interface_id),
                TypeOwner::None => false,
            }
        } else {
            false
        }
    }

    fn bindgen_type_info(&self, type_id: TypeId) -> wit_bindgen_core::TypeInfo {
        self.types.get(type_id)
    }

    fn get_imported_interface(
        &self,
        interface_id: &InterfaceId,
    ) -> anyhow::Result<ImportedInterface<'_>> {
        let interface = &self.resolve.interfaces[*interface_id];
        let name = interface
            .name
            .as_ref()
            .ok_or_else(|| anyhow!("Interface import does not have a name"))?
            .as_str();

        let functions = interface
            .functions
            .iter()
            .map(|(name, f)| (name.as_str(), f))
            .collect();

        let package_id = interface
            .package
            .ok_or_else(|| anyhow!("Anonymous interface imports are not supported yet"))?;
        let package = self
            .resolve
            .packages
            .get(package_id)
            .ok_or_else(|| anyhow!("Could not find package of imported interface {name}"))?;
        let package_name = &package.name;

        Ok(ImportedInterface {
            package_name: Some(package_name),
            name: name.to_string(),
            functions,
            interface: Some(interface),
            interface_id: Some(*interface_id),
        })
    }

    fn typ(&self, type_id: TypeId) -> anyhow::Result<&TypeDef> {
        self.resolve
            .types
            .get(type_id)
            .ok_or_else(|| anyhow!("Unknown type id: {type_id:?}"))
    }

    /// Returns `true` if the given package is a WASI package whose interfaces are remapped
    /// to `wasip2::` via the `with:` block in `wit_bindgen::generate!`.
    fn is_wasi_remapped_package(&self, package_id: PackageId) -> bool {
        let package = &self.resolve.packages[package_id];
        if package.name.namespace != "wasi" {
            return false;
        }
        if !self
            .wasi_remap_namespaces()
            .iter()
            .any(|(pkg_name, _)| *pkg_name == package.name.name.as_str())
        {
            return false;
        }
        // The remap only applies when the imported package's version matches the WASI
        // generation the runtime crate provides. The `wasip3` crate implements the
        // `wasi:*@0.3.x` interfaces, so a Preview 2 versioned import (e.g.
        // `wasi:clocks/monotonic-clock@0.2.3`) in a Preview 3 world must NOT be remapped —
        // its API surface differs and the bindings for it are generated by `wit-bindgen`
        // instead (via `generate_all`).
        if self.target.is_p3() {
            matches!(&package.name.version, Some(v) if v.major == 0 && v.minor == 3)
        } else {
            true
        }
    }

    /// The WASI namespace remap table for the selected generation target.
    fn wasi_remap_namespaces(&self) -> &'static [(&'static str, &'static str)] {
        if self.target.is_p3() {
            WASI_REMAP_NAMESPACES_P3
        } else {
            WASI_REMAP_NAMESPACES
        }
    }

    /// The Rust crate that remapped WASI interfaces resolve to for the selected target
    /// (`wasip2` for Preview 2, `wasip3` for Preview 3).
    fn wasi_remap_crate_ident(&self) -> Ident {
        let name = if self.target.is_p3() {
            "wasip3"
        } else {
            "wasip2"
        };
        Ident::new(name, Span::call_site())
    }

    /// Path to the `wit-bindgen` runtime module for the selected target. The Preview 3 path
    /// uses a renamed `wit-bindgen` dependency (`wit-bindgen-p3`) so that the two major
    /// versions can coexist in one skeleton crate.
    fn wit_bindgen_rt_path(&self) -> proc_macro2::TokenStream {
        if self.target.is_p3() {
            quote::quote! { wit_bindgen_p3::rt }
        } else {
            quote::quote! { wit_bindgen_rt }
        }
    }

    /// For a WASI-remapped resource type, returns the import module path and resource class
    /// name (e.g., `crate::modules::wasi_io_0_2_3_poll::Pollable`).
    fn wasi_resource_module_path(
        &self,
        type_id: TypeId,
    ) -> Option<(proc_macro2::TokenStream, Ident)> {
        let typ = self.resolve.types.get(type_id)?;
        let resource_name = typ.name.as_ref()?;
        let resource_ident = Ident::new(&resource_name.to_upper_camel_case(), Span::call_site());

        let interface_id = match &typ.owner {
            TypeOwner::Interface(id) => *id,
            _ => return None,
        };
        let interface = self.resolve.interfaces.get(interface_id)?;
        let interface_name = interface.name.as_ref()?;
        let package_id = interface.package?;
        let package = self.resolve.packages.get(package_id)?;
        let package_name = &package.name;

        let module_name = format!(
            "{}_{}",
            package_name.to_string().to_snake_case(),
            interface_name.to_snake_case()
        );
        let module_ident = Ident::new(&module_name, Span::call_site());

        Some((
            quote::quote! { crate::modules::#module_ident },
            resource_ident,
        ))
    }

    /// Returns `true` if the given type belongs to a WASI-remapped interface.
    fn is_wasi_remapped_type(&self, type_id: TypeId) -> bool {
        if let Some(typ) = self.resolve.types.get(type_id) {
            match &typ.owner {
                TypeOwner::Interface(interface_id) => {
                    if let Some(interface) = self.resolve.interfaces.get(*interface_id)
                        && let Some(package_id) = interface.package
                    {
                        return self.is_wasi_remapped_package(package_id);
                    }
                    false
                }
                _ => false,
            }
        } else {
            false
        }
    }
}

fn exported_interface_short_js_name(export_name: &str) -> String {
    escape_js_ident(export_name.to_lower_camel_case())
}

fn exported_interface_qualified_js_name(
    context: &GeneratorContext<'_>,
    interface: &Interface,
    export_name: &str,
) -> anyhow::Result<String> {
    let package_id = interface
        .package
        .ok_or_else(|| anyhow!("Anonymous interface exports cannot be qualified: {export_name}"))?;
    let package = context
        .resolve
        .packages
        .get(package_id)
        .ok_or_else(|| anyhow!("Unknown owner package of interface export: {export_name}"))?;
    let interface_name = interface.name.as_deref().unwrap_or(export_name);
    let module_name = format!(
        "{}_{}",
        package.name.to_string().to_snake_case(),
        interface_name.to_snake_case()
    );

    Ok(escape_js_ident(module_name.to_lower_camel_case()))
}

pub struct ImportedInterface<'a> {
    package_name: Option<&'a PackageName>,
    name: String,
    functions: Vec<(&'a str, &'a Function)>,
    interface: Option<&'a Interface>,
    interface_id: Option<InterfaceId>,
}

impl<'a> ImportedInterface<'a> {
    pub fn module_name(&self) -> anyhow::Result<String> {
        let package_name = self
            .package_name
            .ok_or_else(|| anyhow!("imported interface has no package name"))?;
        let interface_name = &self.name;

        Ok(format!(
            "{}_{}",
            package_name.to_string().to_snake_case(),
            interface_name.to_snake_case()
        ))
    }

    pub fn rust_interface_name(&self) -> Ident {
        let interface_name = format!("Js{}Module", self.name.to_upper_camel_case());
        Ident::new(&interface_name, Span::call_site())
    }

    pub fn name_and_interface(&self) -> Option<(&str, &Interface)> {
        self.interface
            .map(|interface| (self.name.as_str(), interface))
    }

    pub fn fully_qualified_interface_name(&self) -> String {
        if let Some(package_name) = &self.package_name {
            package_name.interface_id(&self.name)
        } else {
            self.name.clone()
        }
    }

    pub fn interface_stack(&self) -> VecDeque<InterfaceId> {
        self.interface_id.iter().cloned().collect()
    }
}

/// Recursively copies a WIT directory to `<output>/wit`.
fn copy_wit_directory(wit: &Utf8Path, output: &Utf8Path) -> anyhow::Result<()> {
    std::fs::create_dir_all(output)?;
    copy_dir_if_changed(wit.as_std_path(), output.as_std_path())
        .context("Failed to copy WIT directory")?;
    Ok(())
}

fn copy_dir_if_changed(src: &std::path::Path, dst: &std::path::Path) -> std::io::Result<()> {
    std::fs::create_dir_all(dst)?;
    for entry in std::fs::read_dir(src)? {
        let entry = entry?;
        let src_path = entry.path();
        let dst_path = dst.join(entry.file_name());
        if src_path.is_dir() {
            copy_dir_if_changed(&src_path, &dst_path)?;
        } else {
            copy_if_changed(&src_path, &dst_path)?;
        }
    }
    Ok(())
}

/// Copies the JS module files to `<output>/src/<name>.js` or generates slot files.
fn copy_js_modules(js_modules: &[JsModuleSpec], output: &Utf8Path) -> anyhow::Result<()> {
    let mut slot_index: u32 = 0;
    for module in js_modules {
        match &module.mode {
            EmbeddingMode::EmbedFile(source) => {
                let filename = module.file_name();
                let js_dest = output.join("src").join(filename);
                copy_if_changed(source, js_dest)
                    .context(format!("Failed to copy JavaScript module {}", module.name))?;
            }
            EmbeddingMode::BinarySlot => {
                let slot_filename = module.name.replace('/', "_") + ".slot";
                let slot_dest = output.join("src").join(slot_filename);
                let slot_data = inject::create_marker_file(slot_index);
                write_if_changed(slot_dest, slot_data).context(format!(
                    "Failed to create marker file for module {}",
                    module.name
                ))?;
                slot_index += 1;
            }
            EmbeddingMode::Composition => {}
        }
    }
    Ok(())
}

/// Checks if any of the provided JS modules uses composition mode.
fn uses_composition(js_module_spec: &[JsModuleSpec]) -> bool {
    js_module_spec
        .iter()
        .any(|m| matches!(m.mode, EmbeddingMode::Composition))
}