diff --git a/crates/wasmprinter/src/lib.rs b/crates/wasmprinter/src/lib.rs index b7844bf8b4..ac9deb254e 100644 --- a/crates/wasmprinter/src/lib.rs +++ b/crates/wasmprinter/src/lib.rs @@ -50,12 +50,25 @@ pub fn print_bytes(wasm: impl AsRef<[u8]>) -> Result { /// /// This structure is used to control the overal structure of how wasm binaries /// are printed and tweaks various ways that configures the output. -#[derive(Debug, Default)] +#[derive(Debug)] pub struct Config { print_offsets: bool, print_skeleton: bool, name_unnamed: bool, fold_instructions: bool, + indent_text: String, +} + +impl Default for Config { + fn default() -> Self { + Self { + print_offsets: false, + print_skeleton: false, + name_unnamed: false, + fold_instructions: false, + indent_text: " ".to_string(), + } + } } /// This structure is the actual structure that prints WebAssembly binaries. @@ -226,6 +239,17 @@ impl Config { self } + /// Select the string to use when indenting. + /// + /// The indent allowed here are arbitrary and unchecked. You should enter + /// blank text like `" "` or `"\t"`, rather than something like `"(;;)"`. + /// + /// The default setting is double spaces `" "` + pub fn indent_text(&mut self, text: impl Into) -> &mut Self { + self.indent_text = text.into(); + self + } + /// Prints a WebAssembly binary into a `String` /// /// This function takes an entire `wasm` binary blob and will print it to @@ -1413,7 +1437,7 @@ impl Printer<'_, '_> { // reasonable to avoid generating hundreds of megabytes of whitespace // for small-ish modules that have deep-ish nesting. for _ in 0..self.nesting.min(MAX_NESTING_TO_PRINT) { - self.result.write_str(" ")?; + self.result.write_str(&self.config.indent_text)?; } Ok(()) } diff --git a/crates/wasmprinter/src/operator.rs b/crates/wasmprinter/src/operator.rs index 5b6f728dfc..a87df5066a 100644 --- a/crates/wasmprinter/src/operator.rs +++ b/crates/wasmprinter/src/operator.rs @@ -1453,10 +1453,8 @@ impl OpPrinter for PrintOperatorFolded<'_, '_, '_, '_> { let mut buf_color = PrintTermcolor(Ansi::new(Vec::new())); let mut buf_nocolor = PrintTermcolor(NoColor::new(Vec::new())); let internal_config = Config { - print_offsets: false, - print_skeleton: false, name_unnamed: self.printer.config.name_unnamed, - fold_instructions: false, + ..Default::default() }; let mut internal_printer = Printer { config: &internal_config, diff --git a/src/bin/wasm-tools/print.rs b/src/bin/wasm-tools/print.rs index 08a15446f0..a88e2de9a2 100644 --- a/src/bin/wasm-tools/print.rs +++ b/src/bin/wasm-tools/print.rs @@ -30,6 +30,13 @@ pub struct Opts { /// Print instructions in the folded format. #[clap(short, long)] fold_instructions: bool, + + /// The string to use when indenting. + #[clap(long)] + indent_text: Option, + /// Number of spaces used for indentation, has lower priority than `--indent-text` + #[clap(long)] + indent: Option, } impl Opts { @@ -45,6 +52,16 @@ impl Opts { config.print_skeleton(self.skeleton); config.name_unnamed(self.name_unnamed); config.fold_instructions(self.fold_instructions); + match self.indent_text.as_ref() { + Some(s) => { + config.indent_text(s); + } + None => { + if let Some(s) = self.indent { + config.indent_text(&" ".repeat(s)); + } + } + } self.io.output(wasm_tools::Output::Wat { wasm: &wasm, config, diff --git a/tests/cli/print-custom-indent-width.wat b/tests/cli/print-custom-indent-width.wat new file mode 100644 index 0000000000..b14203c668 --- /dev/null +++ b/tests/cli/print-custom-indent-width.wat @@ -0,0 +1,9 @@ +;; RUN: print --indent 4 % + +(;@0 ;) (module +(;@b ;) (type (;0;) (func (param i32) (result i32))) +(;@1f ;) (func (;0;) (type 0) (param i32) (result i32) +(;@20 ;) local.get 0 + ) +(;@17 ;) (export "f" (func 0)) + ) \ No newline at end of file diff --git a/tests/cli/print-custom-indent-width.wat.stdout b/tests/cli/print-custom-indent-width.wat.stdout new file mode 100644 index 0000000000..11b324786b --- /dev/null +++ b/tests/cli/print-custom-indent-width.wat.stdout @@ -0,0 +1,7 @@ +(module + (type (;0;) (func (param i32) (result i32))) + (export "f" (func 0)) + (func (;0;) (type 0) (param i32) (result i32) + local.get 0 + ) +) \ No newline at end of file