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}
72
73enum Input {
74 Path(PathBuf),
75 Module(Module, String),
76 Bytes(Vec<u8>, String),
77 None,
78}
79
80#[derive(Debug, Clone, Copy)]
81pub enum EncodeInto {
82 Test,
83 Always,
84 Never,
85}
86
87impl Bindgen {
88 pub fn new() -> Bindgen {
89 let externref =
90 env::var("WASM_BINDGEN_ANYREF").is_ok() || env::var("WASM_BINDGEN_EXTERNREF").is_ok();
91 let multi_value = env::var("WASM_BINDGEN_MULTI_VALUE").is_ok();
92 Bindgen {
93 input: Input::None,
94 out_name: None,
95 mode: OutputMode::Bundler {
96 browser_only: false,
97 },
98 debug: false,
99 typescript: false,
100 omit_imports: false,
101 demangle: true,
102 keep_lld_exports: false,
103 keep_debug: false,
104 remove_name_section: false,
105 remove_producers_section: false,
106 emit_start: true,
107 externref,
108 multi_value,
109 encode_into: EncodeInto::Test,
110 omit_default_module_path: true,
111 split_linked_modules: false,
112 generate_reset_state: false,
113 }
114 }
115
116 pub fn input_path<P: AsRef<Path>>(&mut self, path: P) -> &mut Bindgen {
117 self.input = Input::Path(path.as_ref().to_path_buf());
118 self
119 }
120
121 pub fn out_name(&mut self, name: &str) -> &mut Bindgen {
122 self.out_name = Some(name.to_string());
123 self
124 }
125
126 #[deprecated = "automatically detected via `-Ctarget-feature=+reference-types`"]
127 pub fn reference_types(&mut self, enable: bool) -> &mut Bindgen {
128 self.externref = enable;
129 self
130 }
131
132 pub fn input_module(&mut self, name: &str, module: Module) -> &mut Bindgen {
134 let name = name.to_string();
135 self.input = Input::Module(module, name);
136 self
137 }
138
139 pub fn input_bytes(&mut self, name: &str, bytes: Vec<u8>) -> &mut Bindgen {
141 let name = name.to_string();
142 self.input = Input::Bytes(bytes, name);
143 self
144 }
145
146 fn switch_mode(&mut self, mode: OutputMode, flag: &str) -> Result<(), Error> {
147 match self.mode {
148 OutputMode::Bundler { .. } => self.mode = mode,
149 _ => bail!("cannot specify `{flag}` with another output mode already specified"),
150 }
151 Ok(())
152 }
153
154 pub fn nodejs(&mut self, node: bool) -> Result<&mut Bindgen, Error> {
155 if node {
156 self.switch_mode(OutputMode::Node { module: false }, "--target nodejs")?;
157 }
158 Ok(self)
159 }
160
161 pub fn nodejs_module(&mut self, node: bool) -> Result<&mut Bindgen, Error> {
162 if node {
163 self.switch_mode(
164 OutputMode::Node { module: true },
165 "--target experimental-nodejs-module",
166 )?;
167 }
168 Ok(self)
169 }
170
171 pub fn bundler(&mut self, bundler: bool) -> Result<&mut Bindgen, Error> {
172 if bundler {
173 self.switch_mode(
174 OutputMode::Bundler {
175 browser_only: false,
176 },
177 "--target bundler",
178 )?;
179 }
180 Ok(self)
181 }
182
183 pub fn web(&mut self, web: bool) -> Result<&mut Bindgen, Error> {
184 if web {
185 self.switch_mode(OutputMode::Web, "--target web")?;
186 }
187 Ok(self)
188 }
189
190 pub fn no_modules(&mut self, no_modules: bool) -> Result<&mut Bindgen, Error> {
191 if no_modules {
192 self.switch_mode(
193 OutputMode::NoModules {
194 global: "wasm_bindgen".to_string(),
195 },
196 "--target no-modules",
197 )?;
198 }
199 Ok(self)
200 }
201
202 pub fn browser(&mut self, browser: bool) -> Result<&mut Bindgen, Error> {
203 if browser {
204 match &mut self.mode {
205 OutputMode::Bundler { browser_only } => *browser_only = true,
206 _ => bail!("cannot specify `--browser` with other output types"),
207 }
208 }
209 Ok(self)
210 }
211
212 pub fn deno(&mut self, deno: bool) -> Result<&mut Bindgen, Error> {
213 if deno {
214 self.switch_mode(OutputMode::Deno, "--target deno")?;
215 self.encode_into(EncodeInto::Always);
216 }
217 Ok(self)
218 }
219
220 pub fn module(&mut self, source_phase: bool) -> Result<&mut Bindgen, Error> {
221 if source_phase {
222 self.switch_mode(OutputMode::Module, "--target module")?;
223 }
224 Ok(self)
225 }
226
227 pub fn no_modules_global(&mut self, name: &str) -> Result<&mut Bindgen, Error> {
228 match &mut self.mode {
229 OutputMode::NoModules { global } => *global = name.to_string(),
230 _ => bail!("can only specify `--no-modules-global` with `--target no-modules`"),
231 }
232 Ok(self)
233 }
234
235 pub fn debug(&mut self, debug: bool) -> &mut Bindgen {
236 self.debug = debug;
237 self
238 }
239
240 pub fn typescript(&mut self, typescript: bool) -> &mut Bindgen {
241 self.typescript = typescript;
242 self
243 }
244
245 pub fn omit_imports(&mut self, omit_imports: bool) -> &mut Bindgen {
246 self.omit_imports = omit_imports;
247 self
248 }
249
250 pub fn demangle(&mut self, demangle: bool) -> &mut Bindgen {
251 self.demangle = demangle;
252 self
253 }
254
255 pub fn keep_lld_exports(&mut self, keep_lld_exports: bool) -> &mut Bindgen {
256 self.keep_lld_exports = keep_lld_exports;
257 self
258 }
259
260 pub fn keep_debug(&mut self, keep_debug: bool) -> &mut Bindgen {
261 self.keep_debug = keep_debug;
262 self
263 }
264
265 pub fn remove_name_section(&mut self, remove: bool) -> &mut Bindgen {
266 self.remove_name_section = remove;
267 self
268 }
269
270 pub fn remove_producers_section(&mut self, remove: bool) -> &mut Bindgen {
271 self.remove_producers_section = remove;
272 self
273 }
274
275 pub fn emit_start(&mut self, emit: bool) -> &mut Bindgen {
276 self.emit_start = emit;
277 self
278 }
279
280 pub fn encode_into(&mut self, mode: EncodeInto) -> &mut Bindgen {
281 self.encode_into = mode;
282 self
283 }
284
285 pub fn omit_default_module_path(&mut self, omit_default_module_path: bool) -> &mut Bindgen {
286 self.omit_default_module_path = omit_default_module_path;
287 self
288 }
289
290 pub fn split_linked_modules(&mut self, split_linked_modules: bool) -> &mut Bindgen {
291 self.split_linked_modules = split_linked_modules;
292 self
293 }
294
295 pub fn reset_state_function(&mut self, generate_reset_state: bool) -> &mut Bindgen {
296 self.generate_reset_state = generate_reset_state;
297 self
298 }
299
300 pub fn generate<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error> {
301 self.generate_output()?.emit(path.as_ref())
302 }
303
304 pub fn stem(&self) -> Result<&str, Error> {
305 Ok(match &self.input {
306 Input::None => bail!("must have an input by now"),
307 Input::Module(_, name) | Input::Bytes(_, name) => name,
308 Input::Path(path) => match &self.out_name {
309 Some(name) => name,
310 None => path.file_stem().unwrap().to_str().unwrap(),
311 },
312 })
313 }
314
315 pub fn generate_output(&mut self) -> Result<Output, Error> {
316 let mut module = match self.input {
317 Input::None => bail!("must have an input by now"),
318 Input::Module(ref mut m, _) => {
319 let blank_module = Module::default();
320 mem::replace(m, blank_module)
321 }
322 Input::Path(ref path) => {
323 let bytes = std::fs::read(path)
324 .with_context(|| format!("failed reading '{}'", path.display()))?;
325 self.module_from_bytes(&bytes).with_context(|| {
326 format!("failed getting Wasm module for '{}'", path.display())
327 })?
328 }
329 Input::Bytes(ref bytes, _) => self
330 .module_from_bytes(bytes)
331 .context("failed getting Wasm module")?,
332 };
333
334 if let Ok(true) = wasm_conventions::target_feature(&module, "reference-types") {
336 self.externref = true;
337 }
338
339 if let Ok(true) = wasm_conventions::target_feature(&module, "multivalue") {
341 self.multi_value = true;
342 }
343
344 if matches!(self.mode, OutputMode::Web)
346 && module.exports.iter().any(|export| export.name == "default")
347 {
348 bail!("exported symbol \"default\" not allowed for --target web")
349 }
350
351 if self.generate_reset_state
353 && !matches!(
354 self.mode,
355 OutputMode::Module | OutputMode::Web | OutputMode::Node { module: false }
356 )
357 {
358 bail!("--experimental-reset-state-function is only supported for --target module, --target web, or --target nodejs")
359 }
360
361 let thread_count = transforms::threads::run(&mut module)
362 .with_context(|| "failed to prepare module for threading")?;
363
364 if self.demangle {
367 demangle(&mut module);
368 }
369 if !self.keep_lld_exports {
370 unexported_unused_lld_things(&mut module);
371 }
372 {
375 let exn_import = module.imports.iter().find_map(|impt| match impt.kind {
376 walrus::ImportKind::Tag(id)
377 if impt.module == "env" && impt.name == "__cpp_exception" =>
378 {
379 Some((impt, id))
380 }
381 _ => None,
382 });
383 if let Some((import, id)) = exn_import {
384 let original_import_id = import.id();
385 let tag = module.tags.get_mut(id);
386 tag.kind = walrus::TagKind::Local;
387 module.imports.delete(original_import_id);
388 module.exports.add("__cpp_exception", tag.id);
389 }
390
391 module
393 .producers
394 .add_processed_by("wasm-bindgen", &wasm_bindgen_shared::version());
395 }
396 let mut storage = Vec::new();
405 let programs = wit::extract_programs(&mut module, &mut storage)?;
406
407 descriptors::execute(&mut module)?;
412
413 wit::process(self, &mut module, programs, thread_count)?;
419
420 if self.externref {
430 externref::process(&mut module)?;
431 } else {
432 let ids = module
433 .exports
434 .iter()
435 .filter(|e| e.name.starts_with("__externref"))
436 .map(|e| e.id())
437 .collect::<Vec<_>>();
438 for id in ids {
439 module.exports.delete(id);
440 }
441 externref::force_contiguous_elements(&mut module)?;
446 }
447
448 if self.multi_value {
451 multivalue::run(&mut module)
452 .context("failed to transform return pointers into multi-value Wasm")?;
453 }
454
455 generate_wasm_catch_wrappers(&mut module)?;
459
460 gc_module_and_adapters(&mut module);
464
465 let stem = self.stem()?;
466
467 let aux = module
469 .customs
470 .delete_typed::<wit::WasmBindgenAux>()
471 .expect("aux section should be present");
472 let adapters = module
473 .customs
474 .delete_typed::<wit::NonstandardWitSection>()
475 .unwrap();
476 let mut cx = js::Context::new(&mut module, self, &adapters, &aux)?;
477 cx.generate()?;
478 let (js, ts, start) = cx.finalize(stem)?;
479 let generated = Generated {
480 snippets: aux.snippets.clone(),
481 local_modules: aux.local_modules.clone(),
482 mode: self.mode.clone(),
483 typescript: self.typescript,
484 npm_dependencies: cx.npm_dependencies.clone(),
485 js,
486 ts,
487 start,
488 };
489
490 Ok(Output {
491 module,
492 stem: stem.to_string(),
493 generated,
494 })
495 }
496
497 fn module_from_bytes(&self, bytes: &[u8]) -> Result<Module, Error> {
498 walrus::ModuleConfig::new()
499 .strict_validate(false)
506 .generate_dwarf(self.keep_debug)
507 .generate_name_section(!self.remove_name_section)
508 .generate_producers_section(!self.remove_producers_section)
509 .parse(bytes)
510 .context("failed to parse input as wasm")
511 }
512
513 fn local_module_name(&self, module: &str) -> String {
514 format!("./snippets/{module}")
515 }
516
517 fn inline_js_module_name(
518 &self,
519 unique_crate_identifier: &str,
520 snippet_idx_in_crate: usize,
521 ) -> String {
522 format!("./snippets/{unique_crate_identifier}/inline{snippet_idx_in_crate}.js",)
523 }
524}
525
526fn reset_indentation(s: &str) -> String {
527 let mut indent: u32 = 0;
528 let mut dst = String::new();
529
530 fn is_doc_comment(line: &str) -> bool {
531 line.starts_with("*")
532 }
533
534 static TAB: &str = " ";
535
536 for line in s.trim().lines() {
537 let line = line.trim();
538
539 if is_doc_comment(line) {
541 for _ in 0..indent {
542 dst.push_str(TAB);
543 }
544 dst.push(' ');
545 dst.push_str(line);
546 dst.push('\n');
547 continue;
548 }
549
550 if line.starts_with('}') {
551 indent = indent.saturating_sub(1);
552 }
553
554 let extra = if line.starts_with(':') || line.starts_with('?') {
555 1
556 } else {
557 0
558 };
559 if !line.is_empty() {
560 for _ in 0..indent + extra {
561 dst.push_str(TAB);
562 }
563 dst.push_str(line);
564 }
565 dst.push('\n');
566
567 if line.ends_with('{') {
568 indent += 1;
569 }
570 }
571 dst
572}
573
574fn demangle(module: &mut Module) {
580 let (lower, upper) = module.funcs.iter().size_hint();
581 let mut counter: HashMap<String, i32> = HashMap::with_capacity(upper.unwrap_or(lower));
582
583 for func in module.funcs.iter_mut() {
584 let Some(name) = &func.name else {
585 continue;
586 };
587
588 let Ok(sym) = rustc_demangle::try_demangle(name) else {
589 continue;
590 };
591
592 let demangled = sym.to_string();
593 match counter.entry(demangled) {
594 Entry::Occupied(mut entry) => {
595 func.name = Some(format!("{}[{}]", entry.key(), entry.get()));
596 *entry.get_mut() += 1;
597 }
598 Entry::Vacant(entry) => {
599 func.name = Some(entry.key().clone());
600 entry.insert(1);
601 }
602 }
603 }
604}
605
606impl OutputMode {
607 fn uses_es_modules(&self) -> bool {
608 matches!(
609 self,
610 OutputMode::Bundler { .. }
611 | OutputMode::Web
612 | OutputMode::Node { module: true }
613 | OutputMode::Deno
614 | OutputMode::Module
615 )
616 }
617
618 fn nodejs(&self) -> bool {
619 matches!(self, OutputMode::Node { .. })
620 }
621
622 fn no_modules(&self) -> bool {
623 matches!(self, OutputMode::NoModules { .. })
624 }
625
626 fn bundler(&self) -> bool {
627 matches!(self, OutputMode::Bundler { .. })
628 }
629}
630
631fn unexported_unused_lld_things(module: &mut Module) {
635 let mut to_remove = Vec::new();
636 for export in module.exports.iter() {
637 match export.name.as_str() {
638 "__heap_base" | "__data_end" | "__indirect_function_table" => {
639 to_remove.push(export.id());
640 }
641 _ => {}
642 }
643 }
644 for id in to_remove {
645 module.exports.delete(id);
646 }
647}
648
649impl Output {
650 pub fn js(&self) -> &str {
651 &self.generated.js
652 }
653
654 pub fn ts(&self) -> Option<&str> {
655 if self.generated.typescript {
656 Some(&self.generated.ts)
657 } else {
658 None
659 }
660 }
661
662 pub fn start(&self) -> Option<&String> {
663 self.generated.start.as_ref()
664 }
665
666 pub fn snippets(&self) -> &BTreeMap<String, Vec<String>> {
667 &self.generated.snippets
668 }
669
670 pub fn local_modules(&self) -> &HashMap<String, String> {
671 &self.generated.local_modules
672 }
673
674 pub fn npm_dependencies(&self) -> &HashMap<String, (PathBuf, String)> {
675 &self.generated.npm_dependencies
676 }
677
678 pub fn wasm(&self) -> &walrus::Module {
679 &self.module
680 }
681
682 pub fn wasm_mut(&mut self) -> &mut walrus::Module {
683 &mut self.module
684 }
685
686 pub fn emit(&mut self, out_dir: impl AsRef<Path>) -> Result<(), Error> {
687 self._emit(out_dir.as_ref())
688 }
689
690 fn _emit(&mut self, out_dir: &Path) -> Result<(), Error> {
691 let wasm_name = format!("{}_bg", self.stem);
692 let wasm_path = out_dir.join(&wasm_name).with_extension("wasm");
693 fs::create_dir_all(out_dir)?;
694
695 let wasm_bytes = self.module.emit_wasm();
696 fs::write(&wasm_path, wasm_bytes)
697 .with_context(|| format!("failed to write `{}`", wasm_path.display()))?;
698
699 let gen = &self.generated;
700
701 for (identifier, list) in gen.snippets.iter() {
704 for (i, js) in list.iter().enumerate() {
705 let name = format!("inline{i}.js");
706 let path = out_dir.join("snippets").join(identifier).join(name);
707 fs::create_dir_all(path.parent().unwrap())?;
708 fs::write(&path, js)
709 .with_context(|| format!("failed to write `{}`", path.display()))?;
710 }
711 }
712
713 for (path, contents) in gen.local_modules.iter() {
714 let path = out_dir.join("snippets").join(path);
715 fs::create_dir_all(path.parent().unwrap())?;
716 fs::write(&path, contents)
717 .with_context(|| format!("failed to write `{}`", path.display()))?;
718 }
719
720 let is_genmode_nodemodule = matches!(gen.mode, OutputMode::Node { module: true });
721 if !gen.npm_dependencies.is_empty() || is_genmode_nodemodule {
722 #[derive(serde::Serialize)]
723 struct PackageJson<'a> {
724 #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
725 ty: Option<&'static str>,
726 dependencies: BTreeMap<&'a str, &'a str>,
727 }
728 let pj = PackageJson {
729 ty: is_genmode_nodemodule.then_some("module"),
730 dependencies: gen
731 .npm_dependencies
732 .iter()
733 .map(|(k, v)| (k.as_str(), v.1.as_str()))
734 .collect(),
735 };
736 let json = serde_json::to_string_pretty(&pj)?;
737 fs::write(out_dir.join("package.json"), json)?;
738 }
739
740 let extension = "js";
743
744 fn write<P, C>(path: P, contents: C) -> Result<(), anyhow::Error>
745 where
746 P: AsRef<Path>,
747 C: AsRef<[u8]>,
748 {
749 fs::write(&path, contents)
750 .with_context(|| format!("failed to write `{}`", path.as_ref().display()))
751 }
752
753 let js_path = out_dir.join(&self.stem).with_extension(extension);
754 write(&js_path, reset_indentation(&gen.js))?;
755
756 if let Some(start) = &gen.start {
757 let js_path = out_dir.join(wasm_name).with_extension(extension);
758 write(&js_path, reset_indentation(start))?;
759 }
760
761 if gen.typescript {
762 let ts_path = js_path.with_extension("d.ts");
763 fs::write(&ts_path, reset_indentation(&gen.ts))
764 .with_context(|| format!("failed to write `{}`", ts_path.display()))?;
765 }
766
767 if gen.typescript {
768 let ts_path = wasm_path.with_extension("wasm.d.ts");
769 let ts = wasm2es6js::typescript(&self.module)?;
770 fs::write(&ts_path, reset_indentation(&ts))
771 .with_context(|| format!("failed to write `{}`", ts_path.display()))?;
772 }
773
774 Ok(())
775 }
776}
777
778fn generate_wasm_catch_wrappers(module: &mut Module) -> Result<(), Error> {
784 let eh_version = transforms::detect_exception_handling_version(module);
785 log::debug!("Exception handling version: {eh_version:?}");
786
787 if eh_version == transforms::ExceptionHandlingVersion::None {
788 return Ok(());
789 }
790
791 let mut aux = module
793 .customs
794 .delete_typed::<wit::WasmBindgenAux>()
795 .expect("aux section should exist");
796 let wit = module
797 .customs
798 .delete_typed::<wit::NonstandardWitSection>()
799 .expect("wit section should exist");
800
801 log::debug!(
802 "Running catch handler: imports_with_catch={}, externref_table={:?}, externref_alloc={:?}, exn_store={:?}",
803 aux.imports_with_catch.len(),
804 aux.externref_table,
805 aux.externref_alloc,
806 aux.exn_store
807 );
808
809 let result = transforms::catch_handler::run(module, &mut aux, &wit, eh_version)
810 .context("failed to generate catch wrappers");
811
812 module.customs.add(*wit);
814 module.customs.add(*aux);
815
816 result?;
817
818 Ok(())
819}
820
821fn gc_module_and_adapters(module: &mut Module) {
822 loop {
823 walrus::passes::gc::run(module);
827
828 let imports_remaining = module
831 .imports
832 .iter()
833 .map(|i| i.id())
834 .collect::<HashSet<_>>();
835 let mut section = module
836 .customs
837 .delete_typed::<wit::NonstandardWitSection>()
838 .unwrap();
839 section
840 .implements
841 .retain(|pair| imports_remaining.contains(&pair.0));
842
843 let any_removed = section.gc();
849 module.customs.add(*section);
850 if !any_removed {
851 break;
852 }
853 }
854}
855
856fn sorted_iter<K, V>(map: &HashMap<K, V>) -> impl Iterator<Item = (&K, &V)>
863where
864 K: Ord,
865{
866 let mut pairs = map.iter().collect::<Vec<_>>();
867 pairs.sort_by_key(|(k, _)| *k);
868 pairs.into_iter()
869}