1#![doc(html_root_url = "https://docs.rs/wasm-bindgen-cli-support/0.2")]
2
3use anyhow::{bail, Context, Error};
4use std::collections::{BTreeMap, HashMap, HashSet};
5use std::env;
6use std::fs;
7use std::mem;
8use std::path::{Path, PathBuf};
9use std::str;
10use walrus::Module;
11
12pub(crate) const PLACEHOLDER_MODULE: &str = "__wbindgen_placeholder__";
13
14mod decode;
15mod descriptor;
16mod descriptors;
17mod externref;
18mod intrinsic;
19mod js;
20mod multivalue;
21pub mod wasm2es6js;
22mod wit;
23
24pub struct Bindgen {
25 input: Input,
26 out_name: Option<String>,
27 mode: OutputMode,
28 debug: bool,
29 typescript: bool,
30 omit_imports: bool,
31 demangle: bool,
32 keep_lld_exports: bool,
33 keep_debug: bool,
34 remove_name_section: bool,
35 remove_producers_section: bool,
36 omit_default_module_path: bool,
37 emit_start: bool,
38 externref: bool,
39 multi_value: bool,
40 encode_into: EncodeInto,
41 split_linked_modules: bool,
42 symbol_dispose: bool,
43}
44
45pub struct Output {
46 module: walrus::Module,
47 stem: String,
48 generated: Generated,
49}
50
51struct Generated {
52 mode: OutputMode,
53 js: String,
54 ts: String,
55 start: Option<String>,
56 snippets: HashMap<String, Vec<String>>,
57 local_modules: HashMap<String, String>,
58 npm_dependencies: HashMap<String, (PathBuf, String)>,
59 typescript: bool,
60}
61
62#[derive(Clone)]
63enum OutputMode {
64 Bundler { browser_only: bool },
65 Web,
66 NoModules { global: String },
67 Node { module: bool },
68 Deno,
69}
70
71enum Input {
72 Path(PathBuf),
73 Module(Module, String),
74 Bytes(Vec<u8>, String),
75 None,
76}
77
78pub enum EncodeInto {
79 Test,
80 Always,
81 Never,
82}
83
84impl Bindgen {
85 pub fn new() -> Bindgen {
86 let externref =
87 env::var("WASM_BINDGEN_ANYREF").is_ok() || env::var("WASM_BINDGEN_EXTERNREF").is_ok();
88 let multi_value = env::var("WASM_BINDGEN_MULTI_VALUE").is_ok();
89 let symbol_dispose = env::var("WASM_BINDGEN_EXPERIMENTAL_SYMBOL_DISPOSE").is_ok();
90 Bindgen {
91 input: Input::None,
92 out_name: None,
93 mode: OutputMode::Bundler {
94 browser_only: false,
95 },
96 debug: false,
97 typescript: false,
98 omit_imports: false,
99 demangle: true,
100 keep_lld_exports: false,
101 keep_debug: false,
102 remove_name_section: false,
103 remove_producers_section: false,
104 emit_start: true,
105 externref,
106 multi_value,
107 encode_into: EncodeInto::Test,
108 omit_default_module_path: true,
109 split_linked_modules: false,
110 symbol_dispose,
111 }
112 }
113
114 pub fn input_path<P: AsRef<Path>>(&mut self, path: P) -> &mut Bindgen {
115 self.input = Input::Path(path.as_ref().to_path_buf());
116 self
117 }
118
119 pub fn out_name(&mut self, name: &str) -> &mut Bindgen {
120 self.out_name = Some(name.to_string());
121 self
122 }
123
124 #[deprecated = "automatically detected via `-Ctarget-feature=+reference-types`"]
125 pub fn reference_types(&mut self, enable: bool) -> &mut Bindgen {
126 self.externref = enable;
127 self
128 }
129
130 pub fn input_module(&mut self, name: &str, module: Module) -> &mut Bindgen {
132 let name = name.to_string();
133 self.input = Input::Module(module, name);
134 self
135 }
136
137 pub fn input_bytes(&mut self, name: &str, bytes: Vec<u8>) -> &mut Bindgen {
139 let name = name.to_string();
140 self.input = Input::Bytes(bytes, name);
141 self
142 }
143
144 fn switch_mode(&mut self, mode: OutputMode, flag: &str) -> Result<(), Error> {
145 match self.mode {
146 OutputMode::Bundler { .. } => self.mode = mode,
147 _ => bail!(
148 "cannot specify `{}` with another output mode already specified",
149 flag
150 ),
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 no_modules_global(&mut self, name: &str) -> Result<&mut Bindgen, Error> {
222 match &mut self.mode {
223 OutputMode::NoModules { global } => *global = name.to_string(),
224 _ => bail!("can only specify `--no-modules-global` with `--target no-modules`"),
225 }
226 Ok(self)
227 }
228
229 pub fn debug(&mut self, debug: bool) -> &mut Bindgen {
230 self.debug = debug;
231 self
232 }
233
234 pub fn typescript(&mut self, typescript: bool) -> &mut Bindgen {
235 self.typescript = typescript;
236 self
237 }
238
239 pub fn omit_imports(&mut self, omit_imports: bool) -> &mut Bindgen {
240 self.omit_imports = omit_imports;
241 self
242 }
243
244 pub fn demangle(&mut self, demangle: bool) -> &mut Bindgen {
245 self.demangle = demangle;
246 self
247 }
248
249 pub fn keep_lld_exports(&mut self, keep_lld_exports: bool) -> &mut Bindgen {
250 self.keep_lld_exports = keep_lld_exports;
251 self
252 }
253
254 pub fn keep_debug(&mut self, keep_debug: bool) -> &mut Bindgen {
255 self.keep_debug = keep_debug;
256 self
257 }
258
259 pub fn remove_name_section(&mut self, remove: bool) -> &mut Bindgen {
260 self.remove_name_section = remove;
261 self
262 }
263
264 pub fn remove_producers_section(&mut self, remove: bool) -> &mut Bindgen {
265 self.remove_producers_section = remove;
266 self
267 }
268
269 pub fn emit_start(&mut self, emit: bool) -> &mut Bindgen {
270 self.emit_start = emit;
271 self
272 }
273
274 pub fn encode_into(&mut self, mode: EncodeInto) -> &mut Bindgen {
275 self.encode_into = mode;
276 self
277 }
278
279 pub fn omit_default_module_path(&mut self, omit_default_module_path: bool) -> &mut Bindgen {
280 self.omit_default_module_path = omit_default_module_path;
281 self
282 }
283
284 pub fn split_linked_modules(&mut self, split_linked_modules: bool) -> &mut Bindgen {
285 self.split_linked_modules = split_linked_modules;
286 self
287 }
288
289 pub fn generate<P: AsRef<Path>>(&mut self, path: P) -> Result<(), Error> {
290 self.generate_output()?.emit(path.as_ref())
291 }
292
293 pub fn stem(&self) -> Result<&str, Error> {
294 Ok(match &self.input {
295 Input::None => bail!("must have an input by now"),
296 Input::Module(_, name) | Input::Bytes(_, name) => name,
297 Input::Path(path) => match &self.out_name {
298 Some(name) => name,
299 None => path.file_stem().unwrap().to_str().unwrap(),
300 },
301 })
302 }
303
304 pub fn generate_output(&mut self) -> Result<Output, Error> {
305 let mut module = match self.input {
306 Input::None => bail!("must have an input by now"),
307 Input::Module(ref mut m, _) => {
308 let blank_module = Module::default();
309 mem::replace(m, blank_module)
310 }
311 Input::Path(ref path) => {
312 let bytes = std::fs::read(path)
313 .with_context(|| format!("failed reading '{}'", path.display()))?;
314 self.module_from_bytes(&bytes).with_context(|| {
315 format!("failed getting Wasm module for '{}'", path.display())
316 })?
317 }
318 Input::Bytes(ref bytes, _) => self
319 .module_from_bytes(bytes)
320 .context("failed getting Wasm module")?,
321 };
322
323 if let Ok(true) = wasm_bindgen_wasm_conventions::target_feature(&module, "reference-types")
325 {
326 self.externref = true;
327 }
328
329 if let Ok(true) = wasm_bindgen_wasm_conventions::target_feature(&module, "multivalue") {
331 self.multi_value = true;
332 }
333
334 if matches!(self.mode, OutputMode::Web)
336 && module.exports.iter().any(|export| export.name == "default")
337 {
338 bail!("exported symbol \"default\" not allowed for --target web")
339 }
340
341 let thread_count = wasm_bindgen_threads_xform::run(&mut module)
342 .with_context(|| "failed to prepare module for threading")?;
343
344 if self.demangle {
347 demangle(&mut module);
348 }
349 if !self.keep_lld_exports {
350 unexported_unused_lld_things(&mut module);
351 }
352
353 module
355 .producers
356 .add_processed_by("wasm-bindgen", &wasm_bindgen_shared::version());
357
358 let mut storage = Vec::new();
367 let programs = wit::extract_programs(&mut module, &mut storage)?;
368
369 descriptors::execute(&mut module)?;
374
375 wit::process(self, &mut module, programs, thread_count)?;
381
382 if self.externref {
392 externref::process(&mut module)?;
393 } else {
394 let ids = module
395 .exports
396 .iter()
397 .filter(|e| e.name.starts_with("__externref"))
398 .map(|e| e.id())
399 .collect::<Vec<_>>();
400 for id in ids {
401 module.exports.delete(id);
402 }
403 externref::force_contiguous_elements(&mut module)?;
408 }
409
410 if self.multi_value {
413 multivalue::run(&mut module)
414 .context("failed to transform return pointers into multi-value Wasm")?;
415 }
416
417 gc_module_and_adapters(&mut module);
421
422 let stem = self.stem()?;
423
424 let aux = module
426 .customs
427 .delete_typed::<wit::WasmBindgenAux>()
428 .expect("aux section should be present");
429 let adapters = module
430 .customs
431 .delete_typed::<wit::NonstandardWitSection>()
432 .unwrap();
433 let mut cx = js::Context::new(&mut module, self, &adapters, &aux)?;
434 cx.generate()?;
435 let (js, ts, start) = cx.finalize(stem)?;
436 let generated = Generated {
437 snippets: aux.snippets.clone(),
438 local_modules: aux.local_modules.clone(),
439 mode: self.mode.clone(),
440 typescript: self.typescript,
441 npm_dependencies: cx.npm_dependencies.clone(),
442 js,
443 ts,
444 start,
445 };
446
447 Ok(Output {
448 module,
449 stem: stem.to_string(),
450 generated,
451 })
452 }
453
454 fn module_from_bytes(&self, bytes: &[u8]) -> Result<Module, Error> {
455 walrus::ModuleConfig::new()
456 .strict_validate(false)
463 .generate_dwarf(self.keep_debug)
464 .generate_name_section(!self.remove_name_section)
465 .generate_producers_section(!self.remove_producers_section)
466 .parse(bytes)
467 .context("failed to parse input as wasm")
468 }
469
470 fn local_module_name(&self, module: &str) -> String {
471 format!("./snippets/{}", module)
472 }
473
474 fn inline_js_module_name(
475 &self,
476 unique_crate_identifier: &str,
477 snippet_idx_in_crate: usize,
478 ) -> String {
479 format!(
480 "./snippets/{}/inline{}.js",
481 unique_crate_identifier, snippet_idx_in_crate,
482 )
483 }
484}
485
486fn reset_indentation(s: &str) -> String {
487 let mut indent: u32 = 0;
488 let mut dst = String::new();
489
490 fn is_doc_comment(line: &str) -> bool {
491 line.starts_with("*")
492 }
493
494 for line in s.lines() {
495 let line = line.trim();
496
497 if is_doc_comment(line) {
499 for _ in 0..indent {
500 dst.push_str(" ");
501 }
502 dst.push(' ');
503 dst.push_str(line);
504 dst.push('\n');
505 continue;
506 }
507
508 if line.starts_with('}') {
509 indent = indent.saturating_sub(1);
510 }
511
512 let extra = if line.starts_with(':') || line.starts_with('?') {
513 1
514 } else {
515 0
516 };
517 if !line.is_empty() {
518 for _ in 0..indent + extra {
519 dst.push_str(" ");
520 }
521 dst.push_str(line);
522 }
523 dst.push('\n');
524
525 if line.ends_with('{') {
526 indent += 1;
527 }
528 }
529 dst
530}
531
532fn demangle(module: &mut Module) {
533 for func in module.funcs.iter_mut() {
534 let name = match &func.name {
535 Some(name) => name,
536 None => continue,
537 };
538 if let Ok(sym) = rustc_demangle::try_demangle(name) {
539 func.name = Some(sym.to_string());
540 }
541 }
542}
543
544impl OutputMode {
545 fn uses_es_modules(&self) -> bool {
546 matches!(
547 self,
548 OutputMode::Bundler { .. }
549 | OutputMode::Web
550 | OutputMode::Node { module: true }
551 | OutputMode::Deno
552 )
553 }
554
555 fn nodejs(&self) -> bool {
556 matches!(self, OutputMode::Node { .. })
557 }
558
559 fn no_modules(&self) -> bool {
560 matches!(self, OutputMode::NoModules { .. })
561 }
562
563 fn esm_integration(&self) -> bool {
564 matches!(
565 self,
566 OutputMode::Bundler { .. } | OutputMode::Node { module: true }
567 )
568 }
569}
570
571fn unexported_unused_lld_things(module: &mut Module) {
575 let mut to_remove = Vec::new();
576 for export in module.exports.iter() {
577 match export.name.as_str() {
578 "__heap_base" | "__data_end" | "__indirect_function_table" => {
579 to_remove.push(export.id());
580 }
581 _ => {}
582 }
583 }
584 for id in to_remove {
585 module.exports.delete(id);
586 }
587}
588
589impl Output {
590 pub fn js(&self) -> &str {
591 &self.generated.js
592 }
593
594 pub fn ts(&self) -> Option<&str> {
595 if self.generated.typescript {
596 Some(&self.generated.ts)
597 } else {
598 None
599 }
600 }
601
602 pub fn start(&self) -> Option<&String> {
603 self.generated.start.as_ref()
604 }
605
606 pub fn snippets(&self) -> &HashMap<String, Vec<String>> {
607 &self.generated.snippets
608 }
609
610 pub fn local_modules(&self) -> &HashMap<String, String> {
611 &self.generated.local_modules
612 }
613
614 pub fn npm_dependencies(&self) -> &HashMap<String, (PathBuf, String)> {
615 &self.generated.npm_dependencies
616 }
617
618 pub fn wasm(&self) -> &walrus::Module {
619 &self.module
620 }
621
622 pub fn wasm_mut(&mut self) -> &mut walrus::Module {
623 &mut self.module
624 }
625
626 pub fn emit(&mut self, out_dir: impl AsRef<Path>) -> Result<(), Error> {
627 self._emit(out_dir.as_ref())
628 }
629
630 fn _emit(&mut self, out_dir: &Path) -> Result<(), Error> {
631 let wasm_name = format!("{}_bg", self.stem);
632 let wasm_path = out_dir.join(&wasm_name).with_extension("wasm");
633 fs::create_dir_all(out_dir)?;
634 let wasm_bytes = self.module.emit_wasm();
635 fs::write(&wasm_path, wasm_bytes)
636 .with_context(|| format!("failed to write `{}`", wasm_path.display()))?;
637
638 let gen = &self.generated;
639
640 for (identifier, list) in gen.snippets.iter() {
643 for (i, js) in list.iter().enumerate() {
644 let name = format!("inline{}.js", i);
645 let path = out_dir.join("snippets").join(identifier).join(name);
646 fs::create_dir_all(path.parent().unwrap())?;
647 fs::write(&path, js)
648 .with_context(|| format!("failed to write `{}`", path.display()))?;
649 }
650 }
651
652 for (path, contents) in gen.local_modules.iter() {
653 let path = out_dir.join("snippets").join(path);
654 fs::create_dir_all(path.parent().unwrap())?;
655 fs::write(&path, contents)
656 .with_context(|| format!("failed to write `{}`", path.display()))?;
657 }
658
659 let is_genmode_nodemodule = matches!(gen.mode, OutputMode::Node { module: true });
660 if !gen.npm_dependencies.is_empty() || is_genmode_nodemodule {
661 #[derive(serde::Serialize)]
662 struct PackageJson<'a> {
663 #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
664 ty: Option<&'static str>,
665 dependencies: BTreeMap<&'a str, &'a str>,
666 }
667 let pj = PackageJson {
668 ty: is_genmode_nodemodule.then_some("module"),
669 dependencies: gen
670 .npm_dependencies
671 .iter()
672 .map(|(k, v)| (k.as_str(), v.1.as_str()))
673 .collect(),
674 };
675 let json = serde_json::to_string_pretty(&pj)?;
676 fs::write(out_dir.join("package.json"), json)?;
677 }
678
679 let extension = "js";
682
683 fn write<P, C>(path: P, contents: C) -> Result<(), anyhow::Error>
684 where
685 P: AsRef<Path>,
686 C: AsRef<[u8]>,
687 {
688 fs::write(&path, contents)
689 .with_context(|| format!("failed to write `{}`", path.as_ref().display()))
690 }
691
692 let js_path = out_dir.join(&self.stem).with_extension(extension);
693
694 if gen.mode.esm_integration() {
695 let js_name = format!("{}_bg.{}", self.stem, extension);
696
697 let start = gen.start.as_deref().unwrap_or("");
698
699 if matches!(gen.mode, OutputMode::Node { .. }) {
700 write(
701 &js_path,
702 format!(
703 "\
704{start}
705export * from \"./{js_name}\";",
706 ),
707 )?;
708 } else {
709 write(
710 &js_path,
711 format!(
712 "\
713import * as wasm from \"./{wasm_name}.wasm\";
714export * from \"./{js_name}\";
715{start}"
716 ),
717 )?;
718 }
719 write(out_dir.join(&js_name), reset_indentation(&gen.js))?;
720 } else {
721 write(&js_path, reset_indentation(&gen.js))?;
722 }
723
724 if gen.typescript {
725 let ts_path = js_path.with_extension("d.ts");
726 fs::write(&ts_path, &gen.ts)
727 .with_context(|| format!("failed to write `{}`", ts_path.display()))?;
728 }
729
730 if gen.typescript {
731 let ts_path = wasm_path.with_extension("wasm.d.ts");
732 let ts = wasm2es6js::typescript(&self.module)?;
733 fs::write(&ts_path, ts)
734 .with_context(|| format!("failed to write `{}`", ts_path.display()))?;
735 }
736
737 Ok(())
738 }
739}
740
741fn gc_module_and_adapters(module: &mut Module) {
742 loop {
743 walrus::passes::gc::run(module);
747
748 let imports_remaining = module
751 .imports
752 .iter()
753 .map(|i| i.id())
754 .collect::<HashSet<_>>();
755 let mut section = module
756 .customs
757 .delete_typed::<wit::NonstandardWitSection>()
758 .unwrap();
759 section
760 .implements
761 .retain(|pair| imports_remaining.contains(&pair.0));
762
763 let aux = module.customs.get_typed::<wit::WasmBindgenAux>().unwrap();
769 let any_removed = section.gc(aux);
770 module.customs.add(*section);
771 if !any_removed {
772 break;
773 }
774 }
775}
776
777fn sorted_iter<K, V>(map: &HashMap<K, V>) -> impl Iterator<Item = (&K, &V)>
784where
785 K: Ord,
786{
787 let mut pairs = map.iter().collect::<Vec<_>>();
788 pairs.sort_by_key(|(k, _)| *k);
789 pairs.into_iter()
790}