Skip to main content

wasm_bindgen_cli_support/
lib.rs

1use anyhow::{bail, Context, Error};
2use std::collections::{hash_map::Entry, BTreeMap, HashMap, HashSet};
3use std::env;
4use std::fs;
5use std::mem;
6use std::path::{Path, PathBuf};
7use std::str;
8use walrus::Module;
9
10pub(crate) const PLACEHOLDER_MODULE: &str = "__wbindgen_placeholder__";
11
12mod decode;
13mod descriptor;
14mod descriptors;
15mod externref;
16mod interpreter;
17mod intrinsic;
18mod js;
19mod multivalue;
20mod suggest;
21mod transforms;
22pub mod wasm2es6js;
23mod wasm_conventions;
24mod wit;
25
26pub struct Bindgen {
27    input: Input,
28    out_name: Option<String>,
29    mode: OutputMode,
30    debug: bool,
31    typescript: bool,
32    omit_imports: bool,
33    demangle: bool,
34    keep_lld_exports: bool,
35    keep_debug: bool,
36    remove_name_section: bool,
37    remove_producers_section: bool,
38    omit_default_module_path: bool,
39    emit_start: bool,
40    externref: bool,
41    multi_value: bool,
42    encode_into: EncodeInto,
43    split_linked_modules: bool,
44    generate_reset_state: bool,
45    force_enable_abort_handler: bool,
46}
47
48pub struct Output {
49    module: walrus::Module,
50    stem: String,
51    generated: Generated,
52}
53
54struct Generated {
55    mode: OutputMode,
56    js: String,
57    ts: String,
58    start: Option<String>,
59    snippets: BTreeMap<String, Vec<String>>,
60    local_modules: HashMap<String, String>,
61    npm_dependencies: HashMap<String, (PathBuf, String)>,
62    typescript: bool,
63    /// For `OutputMode::Emscripten` only: the contents of a sidecar file
64    /// emcc loads via `--extern-pre-js`, containing ESM `import` statements
65    /// that must live at module top-level. Empty for other modes.
66    emscripten_extern_pre_js: String,
67}
68
69#[derive(Clone)]
70enum OutputMode {
71    Bundler { browser_only: bool },
72    Web,
73    NoModules { global: String },
74    Node { module: bool },
75    Deno,
76    Module,
77    Emscripten,
78}
79
80enum Input {
81    Path(PathBuf),
82    Module(Module, String),
83    Bytes(Vec<u8>, String),
84    None,
85}
86
87#[derive(Debug, Clone, Copy)]
88pub enum EncodeInto {
89    Test,
90    Always,
91    Never,
92}
93
94impl Bindgen {
95    pub fn new() -> Bindgen {
96        let externref =
97            env::var("WASM_BINDGEN_ANYREF").is_ok() || env::var("WASM_BINDGEN_EXTERNREF").is_ok();
98        let multi_value = env::var("WASM_BINDGEN_MULTI_VALUE").is_ok();
99        Bindgen {
100            input: Input::None,
101            out_name: None,
102            mode: OutputMode::Bundler {
103                browser_only: false,
104            },
105            debug: false,
106            typescript: false,
107            omit_imports: false,
108            demangle: true,
109            keep_lld_exports: false,
110            keep_debug: false,
111            remove_name_section: false,
112            remove_producers_section: false,
113            emit_start: true,
114            externref,
115            multi_value,
116            encode_into: EncodeInto::Test,
117            omit_default_module_path: true,
118            split_linked_modules: false,
119            generate_reset_state: false,
120            force_enable_abort_handler: false,
121        }
122    }
123
124    pub fn input_path<P: AsRef<Path>>(&mut self, path: P) -> &mut Bindgen {
125        self.input = Input::Path(path.as_ref().to_path_buf());
126        self
127    }
128
129    pub fn out_name(&mut self, name: &str) -> &mut Bindgen {
130        self.out_name = Some(name.to_string());
131        self
132    }
133
134    #[deprecated = "automatically detected via `-Ctarget-feature=+reference-types`"]
135    pub fn reference_types(&mut self, enable: bool) -> &mut Bindgen {
136        self.externref = enable;
137        self
138    }
139
140    /// Explicitly specify the already parsed input module.
141    pub fn input_module(&mut self, name: &str, module: Module) -> &mut Bindgen {
142        let name = name.to_string();
143        self.input = Input::Module(module, name);
144        self
145    }
146
147    /// Specify the input as the provided Wasm bytes.
148    pub fn input_bytes(&mut self, name: &str, bytes: Vec<u8>) -> &mut Bindgen {
149        let name = name.to_string();
150        self.input = Input::Bytes(bytes, name);
151        self
152    }
153
154    fn switch_mode(&mut self, mode: OutputMode, flag: &str) -> Result<(), Error> {
155        match self.mode {
156            OutputMode::Bundler { .. } => self.mode = mode,
157            _ => bail!("cannot specify `{flag}` with another output mode already specified"),
158        }
159        Ok(())
160    }
161
162    pub fn nodejs(&mut self, node: bool) -> Result<&mut Bindgen, Error> {
163        if node {
164            self.switch_mode(OutputMode::Node { module: false }, "--target nodejs")?;
165        }
166        Ok(self)
167    }
168
169    pub fn nodejs_module(&mut self, node: bool) -> Result<&mut Bindgen, Error> {
170        if node {
171            self.switch_mode(
172                OutputMode::Node { module: true },
173                "--target experimental-nodejs-module",
174            )?;
175        }
176        Ok(self)
177    }
178
179    pub fn bundler(&mut self, bundler: bool) -> Result<&mut Bindgen, Error> {
180        if bundler {
181            self.switch_mode(
182                OutputMode::Bundler {
183                    browser_only: false,
184                },
185                "--target bundler",
186            )?;
187        }
188        Ok(self)
189    }
190
191    pub fn web(&mut self, web: bool) -> Result<&mut Bindgen, Error> {
192        if web {
193            self.switch_mode(OutputMode::Web, "--target web")?;
194        }
195        Ok(self)
196    }
197
198    pub fn no_modules(&mut self, no_modules: bool) -> Result<&mut Bindgen, Error> {
199        if no_modules {
200            self.switch_mode(
201                OutputMode::NoModules {
202                    global: "wasm_bindgen".to_string(),
203                },
204                "--target no-modules",
205            )?;
206        }
207        Ok(self)
208    }
209
210    pub fn browser(&mut self, browser: bool) -> Result<&mut Bindgen, Error> {
211        if browser {
212            match &mut self.mode {
213                OutputMode::Bundler { browser_only } => *browser_only = true,
214                _ => bail!("cannot specify `--browser` with other output types"),
215            }
216        }
217        Ok(self)
218    }
219
220    pub fn deno(&mut self, deno: bool) -> Result<&mut Bindgen, Error> {
221        if deno {
222            self.switch_mode(OutputMode::Deno, "--target deno")?;
223            self.encode_into(EncodeInto::Always);
224        }
225        Ok(self)
226    }
227
228    pub fn module(&mut self, source_phase: bool) -> Result<&mut Bindgen, Error> {
229        if source_phase {
230            self.switch_mode(OutputMode::Module, "--target module")?;
231        }
232        Ok(self)
233    }
234
235    pub fn no_modules_global(&mut self, name: &str) -> Result<&mut Bindgen, Error> {
236        match &mut self.mode {
237            OutputMode::NoModules { global } => *global = name.to_string(),
238            _ => bail!("can only specify `--no-modules-global` with `--target no-modules`"),
239        }
240        Ok(self)
241    }
242
243    pub fn debug(&mut self, debug: bool) -> &mut Bindgen {
244        self.debug = debug;
245        self
246    }
247
248    pub fn typescript(&mut self, typescript: bool) -> &mut Bindgen {
249        self.typescript = typescript;
250        self
251    }
252
253    pub fn omit_imports(&mut self, omit_imports: bool) -> &mut Bindgen {
254        self.omit_imports = omit_imports;
255        self
256    }
257
258    pub fn demangle(&mut self, demangle: bool) -> &mut Bindgen {
259        self.demangle = demangle;
260        self
261    }
262
263    pub fn keep_lld_exports(&mut self, keep_lld_exports: bool) -> &mut Bindgen {
264        self.keep_lld_exports = keep_lld_exports;
265        self
266    }
267
268    pub fn keep_debug(&mut self, keep_debug: bool) -> &mut Bindgen {
269        self.keep_debug = keep_debug;
270        self
271    }
272
273    pub fn remove_name_section(&mut self, remove: bool) -> &mut Bindgen {
274        self.remove_name_section = remove;
275        self
276    }
277
278    pub fn remove_producers_section(&mut self, remove: bool) -> &mut Bindgen {
279        self.remove_producers_section = remove;
280        self
281    }
282
283    pub fn emit_start(&mut self, emit: bool) -> &mut Bindgen {
284        self.emit_start = emit;
285        self
286    }
287
288    pub fn encode_into(&mut self, mode: EncodeInto) -> &mut Bindgen {
289        self.encode_into = mode;
290        self
291    }
292
293    pub fn omit_default_module_path(&mut self, omit_default_module_path: bool) -> &mut Bindgen {
294        self.omit_default_module_path = omit_default_module_path;
295        self
296    }
297
298    pub fn split_linked_modules(&mut self, split_linked_modules: bool) -> &mut Bindgen {
299        self.split_linked_modules = split_linked_modules;
300        self
301    }
302
303    pub fn reset_state_function(&mut self, generate_reset_state: bool) -> &mut Bindgen {
304        self.generate_reset_state = generate_reset_state;
305        self
306    }
307
308    pub fn force_enable_abort_handler(&mut self, force_enable_abort_handler: bool) -> &mut Self {
309        self.force_enable_abort_handler = force_enable_abort_handler;
310        self
311    }
312
313    pub fn generate<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error> {
314        self.generate_output()?.emit(path.as_ref())
315    }
316
317    pub fn stem(&self) -> Result<&str, Error> {
318        Ok(match &self.input {
319            Input::None => bail!("must have an input by now"),
320            Input::Module(_, name) | Input::Bytes(_, name) => name,
321            Input::Path(path) => match &self.out_name {
322                Some(name) => name,
323                None => path.file_stem().unwrap().to_str().unwrap(),
324            },
325        })
326    }
327
328    pub fn generate_output(&mut self) -> Result<Output, Error> {
329        let mut module = match self.input {
330            Input::None => bail!("must have an input by now"),
331            Input::Module(ref mut m, _) => {
332                let blank_module = Module::default();
333                mem::replace(m, blank_module)
334            }
335            Input::Path(ref path) => {
336                let bytes = std::fs::read(path)
337                    .with_context(|| format!("failed reading '{}'", path.display()))?;
338                self.module_from_bytes(&bytes).with_context(|| {
339                    format!("failed getting Wasm module for '{}'", path.display())
340                })?
341            }
342            Input::Bytes(ref bytes, _) => self
343                .module_from_bytes(bytes)
344                .context("failed getting Wasm module")?,
345        };
346
347        if module
348            .customs
349            .remove_raw("__wasm_bindgen_emscripten_marker")
350            .is_some()
351        {
352            // Force the internal configuration to Emscripten mode.
353            self.mode = OutputMode::Emscripten;
354        }
355
356        // Enable reference type transformations if the module is already using it.
357        if let Ok(true) = wasm_conventions::target_feature(&module, "reference-types") {
358            self.externref = true;
359        }
360
361        // Enable multivalue transformations if the module is already using it.
362        if let Ok(true) = wasm_conventions::target_feature(&module, "multivalue") {
363            self.multi_value = true;
364        }
365
366        // Check that no exported symbol is called "default" if we target web.
367        if matches!(self.mode, OutputMode::Web)
368            && module.exports.iter().any(|export| export.name == "default")
369        {
370            bail!("exported symbol \"default\" not allowed for --target web")
371        }
372
373        // Check that reset_state is only used with --target module, web, or node
374        if self.generate_reset_state
375            && !matches!(
376                self.mode,
377                OutputMode::Module | OutputMode::Web | OutputMode::Node { module: false }
378            )
379        {
380            bail!("--experimental-reset-state-function is only supported for --target module, --target web, or --target nodejs")
381        }
382
383        let thread_count = transforms::threads::run(&mut module)
384            .with_context(|| "failed to prepare module for threading")?;
385
386        // If requested, turn all mangled symbols into prettier unmangled
387        // symbols with the help of `rustc-demangle`.
388        if self.demangle {
389            demangle(&mut module);
390        }
391        if !self.keep_lld_exports && !self.mode.emscripten() {
392            unexported_unused_lld_things(&mut module);
393        }
394        // Quick fix for https://github.com/wasm-bindgen/wasm-bindgen/pull/4931
395        // which is likely a compiler bug
396        {
397            let exn_import = module.imports.iter().find_map(|impt| match impt.kind {
398                walrus::ImportKind::Tag(id)
399                    if impt.module == "env" && impt.name == "__cpp_exception" =>
400                {
401                    Some((impt, id))
402                }
403                _ => None,
404            });
405            if let Some((import, id)) = exn_import {
406                let original_import_id = import.id();
407                let tag = module.tags.get_mut(id);
408                tag.kind = walrus::TagKind::Local;
409                module.imports.delete(original_import_id);
410                module.exports.add("__cpp_exception", tag.id);
411            }
412
413            // We're making quite a few changes, list ourselves as a producer.
414            module
415                .producers
416                .add_processed_by("wasm-bindgen", &wasm_bindgen_shared::version());
417        }
418        // Parse and remove our custom section before executing descriptors.
419        // That includes checking that the binary has the same schema version
420        // as this version of the CLI, which is why we do it first - to make
421        // sure that this binary was produced by a compatible version of the
422        // wasm-bindgen macro before attempting to interpret our unstable
423        // descriptor format. That way, we give a more helpful version mismatch
424        // error instead of an unhelpful panic if an incompatible descriptor is
425        // found.
426        let mut storage = Vec::new();
427        let programs = wit::extract_programs(&mut module, &mut storage)?;
428
429        // Learn about the type signatures of all wasm-bindgen imports and
430        // exports by executing `__wbindgen_describe_*` functions. This'll
431        // effectively move all the descriptor functions to their own custom
432        // sections.
433        descriptors::execute(&mut module)?;
434
435        // Process the custom section we extracted earlier. In its stead insert
436        // a forward-compatible Wasm interface types section as well as an
437        // auxiliary section for all sorts of miscellaneous information and
438        // features #[wasm_bindgen] supports that aren't covered by wasm
439        // interface types.
440        wit::process(self, &mut module, programs, thread_count)?;
441
442        // Now that we've got type information from the webidl processing pass,
443        // touch up the output of rustc to insert externref shims where necessary.
444        // This is only done if the externref pass is enabled, which it's
445        // currently off-by-default since `externref` is still in development in
446        // engines.
447        //
448        // If the externref pass isn't necessary, then we blanket delete the
449        // export of all our externref intrinsics which will get cleaned up in the
450        // GC pass before JS generation.
451        if self.externref {
452            externref::process(&mut module)?;
453        } else {
454            let ids = module
455                .exports
456                .iter()
457                .filter(|e| e.name.starts_with("__externref"))
458                .map(|e| e.id())
459                .collect::<Vec<_>>();
460            for id in ids {
461                module.exports.delete(id);
462            }
463            // Clean up element segments as well if they have holes in them
464            // after some of our transformations, because non-externref engines
465            // only support contiguous arrays of function references in element
466            // segments.
467            externref::force_contiguous_elements(&mut module)?;
468        }
469
470        // Using all of our metadata convert our module to a multi-value using
471        // module if applicable.
472        if self.multi_value {
473            multivalue::run(&mut module)
474                .context("failed to transform return pointers into multi-value Wasm")?;
475        }
476
477        // Generate Wasm catch wrappers for imports with #[wasm_bindgen(catch)].
478        // This runs after externref processing so that we have access to the
479        // externref table and allocation function.
480        //
481        // Emscripten output may contain wasm exception-handling instructions
482        // from linked libc++ / embind that have no relation to wasm-bindgen's
483        // `#[wasm_bindgen(catch)]` machinery, and the wasm-bindgen runtime
484        // intrinsics (`__externref_table`, `__externref_table_alloc`,
485        // `__wbindgen_exn_store`) may be absent. Skip the transform until
486        // proper emscripten-mode catch support lands.
487        if !matches!(self.mode, OutputMode::Emscripten) {
488            generate_wasm_catch_wrappers(&mut module, self.force_enable_abort_handler)?;
489        }
490
491        // We've done a whole bunch of transformations to the Wasm module, many
492        // of which leave "garbage" lying around, so let's prune out all our
493        // unnecessary things here.
494        gc_module_and_adapters(&mut module);
495
496        let stem = self.stem()?;
497
498        // Now we execute the JS generation passes to actually emit JS/TypeScript/etc.
499        let aux = module
500            .customs
501            .delete_typed::<wit::WasmBindgenAux>()
502            .expect("aux section should be present");
503        let adapters = module
504            .customs
505            .delete_typed::<wit::NonstandardWitSection>()
506            .unwrap();
507        let mut cx = js::Context::new(&mut module, self, &adapters, &aux)?;
508        cx.generate()?;
509        let js::FinalizedOutput {
510            js,
511            ts,
512            start,
513            emscripten_extern_pre_js,
514        } = cx.finalize(stem)?;
515        let generated = Generated {
516            snippets: aux.snippets.clone(),
517            local_modules: aux.local_modules.clone(),
518            mode: self.mode.clone(),
519            typescript: self.typescript,
520            npm_dependencies: cx.npm_dependencies.clone(),
521            js,
522            ts,
523            start,
524            emscripten_extern_pre_js,
525        };
526
527        Ok(Output {
528            module,
529            stem: stem.to_string(),
530            generated,
531        })
532    }
533
534    fn module_from_bytes(&self, bytes: &[u8]) -> Result<Module, Error> {
535        walrus::ModuleConfig::new()
536            // Skip validation of the module as LLVM's output is
537            // generally already well-formed and so we won't gain much
538            // from re-validating. Additionally LLVM's current output
539            // for threads includes atomic instructions but doesn't
540            // include shared memory, so it fails that part of
541            // validation!
542            .strict_validate(false)
543            .generate_dwarf(self.keep_debug)
544            .generate_name_section(!self.remove_name_section)
545            .generate_producers_section(!self.remove_producers_section)
546            .parse(bytes)
547            .context("failed to parse input as wasm")
548    }
549
550    fn local_module_name(&self, module: &str) -> String {
551        format!("./snippets/{module}")
552    }
553
554    fn inline_js_module_name(
555        &self,
556        unique_crate_identifier: &str,
557        snippet_idx_in_crate: usize,
558    ) -> String {
559        format!("./snippets/{unique_crate_identifier}/inline{snippet_idx_in_crate}.js",)
560    }
561}
562
563fn reset_indentation(s: &str) -> String {
564    let mut indent: u32 = 0;
565    let mut dst = String::new();
566
567    fn is_doc_comment(line: &str) -> bool {
568        line.starts_with("*")
569    }
570
571    static TAB: &str = "    ";
572
573    for line in s.trim().lines() {
574        let line = line.trim();
575
576        // handle doc comments separately
577        if is_doc_comment(line) {
578            for _ in 0..indent {
579                dst.push_str(TAB);
580            }
581            dst.push(' ');
582            dst.push_str(line);
583            dst.push('\n');
584            continue;
585        }
586
587        if line.starts_with('}') {
588            indent = indent.saturating_sub(1);
589        }
590
591        let extra = if line.starts_with(':') || line.starts_with('?') {
592            1
593        } else {
594            0
595        };
596        if !line.is_empty() {
597            for _ in 0..indent + extra {
598                dst.push_str(TAB);
599            }
600            dst.push_str(line);
601        }
602        dst.push('\n');
603
604        if line.ends_with('{') {
605            indent += 1;
606        }
607    }
608    dst
609}
610
611/// Since Rust will soon adopt v0 mangling as the default,
612/// and the `rustc_demangle` crate doesn't output closure disambiguators,
613/// duplicate symbols can appear. We handle this case manually.
614///
615/// issue: <https://github.com/wasm-bindgen/wasm-bindgen/issues/4820>
616fn demangle(module: &mut Module) {
617    let (lower, upper) = module.funcs.iter().size_hint();
618    let mut counter: HashMap<String, i32> = HashMap::with_capacity(upper.unwrap_or(lower));
619
620    for func in module.funcs.iter_mut() {
621        let Some(name) = &func.name else {
622            continue;
623        };
624
625        let Ok(sym) = rustc_demangle::try_demangle(name) else {
626            continue;
627        };
628
629        let demangled = sym.to_string();
630        match counter.entry(demangled) {
631            Entry::Occupied(mut entry) => {
632                func.name = Some(format!("{}[{}]", entry.key(), entry.get()));
633                *entry.get_mut() += 1;
634            }
635            Entry::Vacant(entry) => {
636                func.name = Some(entry.key().clone());
637                entry.insert(1);
638            }
639        }
640    }
641}
642
643impl OutputMode {
644    fn uses_es_modules(&self) -> bool {
645        matches!(
646            self,
647            OutputMode::Bundler { .. }
648                | OutputMode::Web
649                | OutputMode::Node { module: true }
650                | OutputMode::Deno
651                | OutputMode::Module
652        )
653    }
654
655    fn nodejs(&self) -> bool {
656        matches!(self, OutputMode::Node { .. })
657    }
658
659    fn no_modules(&self) -> bool {
660        matches!(self, OutputMode::NoModules { .. })
661    }
662
663    fn bundler(&self) -> bool {
664        matches!(self, OutputMode::Bundler { .. })
665    }
666
667    fn emscripten(&self) -> bool {
668        matches!(self, OutputMode::Emscripten)
669    }
670}
671
672/// Remove a number of internal exports that are synthesized by Rust's linker,
673/// LLD. These exports aren't typically ever needed and just add extra space to
674/// the binary.
675fn unexported_unused_lld_things(module: &mut Module) {
676    let mut to_remove = Vec::new();
677    for export in module.exports.iter() {
678        match export.name.as_str() {
679            "__heap_base" | "__data_end" | "__indirect_function_table" => {
680                to_remove.push(export.id());
681            }
682            _ => {}
683        }
684    }
685    for id in to_remove {
686        module.exports.delete(id);
687    }
688}
689
690impl Output {
691    pub fn js(&self) -> &str {
692        &self.generated.js
693    }
694
695    pub fn ts(&self) -> Option<&str> {
696        if self.generated.typescript {
697            Some(&self.generated.ts)
698        } else {
699            None
700        }
701    }
702
703    pub fn start(&self) -> Option<&String> {
704        self.generated.start.as_ref()
705    }
706
707    pub fn snippets(&self) -> &BTreeMap<String, Vec<String>> {
708        &self.generated.snippets
709    }
710
711    pub fn local_modules(&self) -> &HashMap<String, String> {
712        &self.generated.local_modules
713    }
714
715    pub fn npm_dependencies(&self) -> &HashMap<String, (PathBuf, String)> {
716        &self.generated.npm_dependencies
717    }
718
719    pub fn wasm(&self) -> &walrus::Module {
720        &self.module
721    }
722
723    pub fn wasm_mut(&mut self) -> &mut walrus::Module {
724        &mut self.module
725    }
726
727    pub fn emit(&mut self, out_dir: impl AsRef<Path>) -> Result<(), Error> {
728        self._emit(out_dir.as_ref())
729    }
730
731    fn _emit(&mut self, out_dir: &Path) -> Result<(), Error> {
732        let wasm_name = format!("{}_bg", self.stem);
733        let wasm_path = out_dir.join(&wasm_name).with_extension("wasm");
734        fs::create_dir_all(out_dir)?;
735
736        let wasm_bytes = self.module.emit_wasm();
737        fs::write(&wasm_path, wasm_bytes)
738            .with_context(|| format!("failed to write `{}`", wasm_path.display()))?;
739
740        let gen = &self.generated;
741
742        // Write out all local JS snippets to the final destination now that
743        // we've collected them from all the programs.
744        for (identifier, list) in gen.snippets.iter() {
745            for (i, js) in list.iter().enumerate() {
746                let name = format!("inline{i}.js");
747                let path = out_dir.join("snippets").join(identifier).join(name);
748                fs::create_dir_all(path.parent().unwrap())?;
749                fs::write(&path, js)
750                    .with_context(|| format!("failed to write `{}`", path.display()))?;
751            }
752        }
753
754        for (path, contents) in gen.local_modules.iter() {
755            let path = out_dir.join("snippets").join(path);
756            fs::create_dir_all(path.parent().unwrap())?;
757            fs::write(&path, contents)
758                .with_context(|| format!("failed to write `{}`", path.display()))?;
759        }
760
761        let is_genmode_nodemodule = matches!(gen.mode, OutputMode::Node { module: true });
762        if !gen.npm_dependencies.is_empty() || is_genmode_nodemodule {
763            #[derive(serde::Serialize)]
764            struct PackageJson<'a> {
765                #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
766                ty: Option<&'static str>,
767                dependencies: BTreeMap<&'a str, &'a str>,
768            }
769            let pj = PackageJson {
770                ty: is_genmode_nodemodule.then_some("module"),
771                dependencies: gen
772                    .npm_dependencies
773                    .iter()
774                    .map(|(k, v)| (k.as_str(), v.1.as_str()))
775                    .collect(),
776            };
777            let json = serde_json::to_string_pretty(&pj)?;
778            fs::write(out_dir.join("package.json"), json)?;
779        }
780
781        // And now that we've got all our JS and TypeScript, actually write it
782        // out to the filesystem.
783        let extension = "js";
784
785        fn write<P, C>(path: P, contents: C) -> Result<(), anyhow::Error>
786        where
787            P: AsRef<Path>,
788            C: AsRef<[u8]>,
789        {
790            fs::write(&path, contents)
791                .with_context(|| format!("failed to write `{}`", path.as_ref().display()))
792        }
793
794        let js_path = out_dir.join(&self.stem).with_extension(extension);
795        if matches!(self.generated.mode, OutputMode::Emscripten) {
796            let emscripten_js_path = out_dir.join("library_bindgen.js");
797            write(&emscripten_js_path, reset_indentation(&gen.js))?;
798            // When the user crate imports from an ESM module
799            // (`#[wasm_bindgen(module = "...")]`), we emit those imports to a
800            // sidecar `library_bindgen.extern-pre.js`. Consumers pass it to
801            // emcc with `--extern-pre-js`, which prepends it before emcc's
802            // modularize wrapper — ESM imports can only legally live there.
803            // Skip writing when empty so consumers don't accidentally pick
804            // up a stale file from a previous build.
805            let extern_pre_js_path = out_dir.join("library_bindgen.extern-pre.js");
806            if gen.emscripten_extern_pre_js.is_empty() {
807                let _ = fs::remove_file(&extern_pre_js_path);
808            } else {
809                write(
810                    &extern_pre_js_path,
811                    reset_indentation(&gen.emscripten_extern_pre_js),
812                )?;
813            }
814        } else {
815            write(&js_path, reset_indentation(&gen.js))?;
816        }
817
818        if let Some(start) = &gen.start {
819            let js_path = out_dir.join(wasm_name).with_extension(extension);
820            write(&js_path, reset_indentation(start))?;
821        }
822
823        if gen.typescript {
824            let ts_path = js_path.with_extension("d.ts");
825            fs::write(&ts_path, reset_indentation(&gen.ts))
826                .with_context(|| format!("failed to write `{}`", ts_path.display()))?;
827        }
828
829        if gen.typescript {
830            let ts_path = wasm_path.with_extension("wasm.d.ts");
831            let ts = wasm2es6js::typescript(&self.module)?;
832            fs::write(&ts_path, reset_indentation(&ts))
833                .with_context(|| format!("failed to write `{}`", ts_path.display()))?;
834        }
835
836        Ok(())
837    }
838}
839
840/// Generate Wasm catch wrappers for imports marked with `#[wasm_bindgen(catch)]`.
841///
842/// When exception handling instructions are available in the module, this generates
843/// Wasm wrapper functions that catch JavaScript exceptions using `WebAssembly.JSTag`
844/// instead of relying on JS `handleError` wrappers.
845fn generate_wasm_catch_wrappers(
846    module: &mut Module,
847    enable_abort_handler: bool,
848) -> Result<(), Error> {
849    let eh_version = transforms::detect_exception_handling_version(module, enable_abort_handler);
850    log::debug!("Exception handling version: {eh_version:?}");
851
852    if eh_version == transforms::ExceptionHandlingVersion::None {
853        return Ok(());
854    }
855
856    // We need to temporarily remove the custom sections to avoid borrow issues
857    let mut aux = module
858        .customs
859        .delete_typed::<wit::WasmBindgenAux>()
860        .expect("aux section should exist");
861    let wit = module
862        .customs
863        .delete_typed::<wit::NonstandardWitSection>()
864        .expect("wit section should exist");
865
866    log::debug!(
867        "Running catch handler: imports_with_catch={}, externref_table={:?}, externref_alloc={:?}, exn_store={:?}",
868        aux.imports_with_catch.len(),
869        aux.externref_table,
870        aux.externref_alloc,
871        aux.exn_store
872    );
873
874    let result = transforms::catch_handler::run(module, &mut aux, &wit, eh_version)
875        .context("failed to generate catch wrappers");
876
877    // Re-add the custom sections
878    module.customs.add(*wit);
879    module.customs.add(*aux);
880
881    result?;
882
883    Ok(())
884}
885
886fn gc_module_and_adapters(module: &mut Module) {
887    loop {
888        // Fist up, cleanup the native Wasm module. Note that roots can come
889        // from custom sections, namely our Wasm interface types custom section
890        // as well as the aux section.
891        walrus::passes::gc::run(module);
892
893        // ... and afterwards we can delete any `implements` directives for any
894        // imports that have been deleted.
895        let imports_remaining = module
896            .imports
897            .iter()
898            .map(|i| i.id())
899            .collect::<HashSet<_>>();
900        let mut section = module
901            .customs
902            .delete_typed::<wit::NonstandardWitSection>()
903            .unwrap();
904        section
905            .implements
906            .retain(|pair| imports_remaining.contains(&pair.0));
907
908        // ... and after we delete the `implements` directive we try to
909        // delete some adapters themselves. If nothing is deleted, then we're
910        // good to go. If something is deleted though then we may have free'd up
911        // some functions in the main module to get deleted, so go again to gc
912        // things.
913        let any_removed = section.gc();
914        module.customs.add(*section);
915        if !any_removed {
916            break;
917        }
918    }
919}
920
921/// Returns a sorted iterator over a hash map, sorted based on key.
922///
923/// The intention of this API is to be used whenever the iteration order of a
924/// `HashMap` might affect the generated JS bindings. We want to ensure that the
925/// generated output is deterministic and we do so by ensuring that iteration of
926/// hash maps is consistently sorted.
927fn sorted_iter<K, V>(map: &HashMap<K, V>) -> impl Iterator<Item = (&K, &V)>
928where
929    K: Ord,
930{
931    let mut pairs = map.iter().collect::<Vec<_>>();
932    pairs.sort_by_key(|(k, _)| *k);
933    pairs.into_iter()
934}