Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasmprinter] support custom indent text #1963

Merged
merged 9 commits into from
Dec 30, 2024
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>,
oovm marked this conversation as resolved.
Show resolved Hide resolved
/// 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
)
)
Loading