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