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