Skip to content

Commit

Permalink
[wasmprinter] support custom indent text (#1963)
Browse files Browse the repository at this point in the history
* [wasmprinter] support custom indent text

* [wasm-tools] support config `--indent-text` for `print` command

* [wasm-tools] support config `--indent-text` for `print` command

* [wasm-tools] support config `--indent-text` for `print` command

* [wasmprinter] Add custom-indent test cases

* [wasmprinter] Add custom-indent test cases

* [wasm-tools] support config `--indent` for `print` command

* [wasm-tools] test the precedence of ``--indent-text` and ``--indent`

* [wasm-tools] remove indent text test
  • Loading branch information
oovm authored Dec 30, 2024
1 parent 8229bc6 commit 760a03d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
28 changes: 26 additions & 2 deletions crates/wasmprinter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,25 @@ pub fn print_bytes(wasm: impl AsRef<[u8]>) -> Result<String> {
///
/// 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.
Expand Down Expand Up @@ -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<String>) -> &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
Expand Down Expand Up @@ -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(())
}
Expand Down
4 changes: 1 addition & 3 deletions crates/wasmprinter/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
17 changes: 17 additions & 0 deletions src/bin/wasm-tools/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// Number of spaces used for indentation, has lower priority than `--indent-text`
#[clap(long)]
indent: Option<usize>,
}

impl Opts {
Expand All @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions tests/cli/print-custom-indent-width.wat
Original file line number Diff line number Diff line change
@@ -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))
)
7 changes: 7 additions & 0 deletions tests/cli/print-custom-indent-width.wat.stdout
Original file line number Diff line number Diff line change
@@ -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
)
)

0 comments on commit 760a03d

Please sign in to comment.