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