### Folded instructions example Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/lib.rs.html?search= Example output of folded instructions for a simple add function, as shown in the fold_instructions documentation. ```wasm (module (func $foo (param i32 i32) (result i32) (i32.add (local.get 0) (local.get 1)) ) ) ``` -------------------------------- ### Start print_instantiation_arg function Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/component.rs.html?search=std%3A%3Avec Starts the function that prints a single core instantiation argument. ```rust pub(crate) fn print_instantiation_arg( &mut self, state: &State, arg: &InstantiationArg, ) -> Result<()> { ``` -------------------------------- ### print_component_start Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/component.rs.html?search= Prints a component start function: writes a newline, starts a "start " group, prints the function index, then iterates over arguments and results, printing "value " and "result value " groups. Increments the component values counter for each result. ```rust self.newline(offset)?; self.start_group("start ")?; self.print_idx(&state.component.func_names, start.func_index)?; for arg in start.arguments.iter() { self.result.write_str(" ")?; self.start_group("value ")?; self.print_idx(&state.component.value_names, *arg)?; self.end_group()?; } for _ in 0..start.results { self.result.write_str(" ")?; self.start_group("result ")?; self.start_group("value ")?; self.print_name(&state.component.value_names, state.component.values)?; self.end_group()?; self.end_group()?; state.component.values += 1; } self.end_group()?; // start Ok(()) ``` -------------------------------- ### PrintFmtWrite::start_line Source: https://docs.rs/wasmprinter/latest/wasmprinter/struct.PrintFmtWrite.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Indicates that a new line in the output is starting at the binary_offset provided. ```APIDOC ## [METHOD] [ENDPOINT] ### Description Indicates that a new line in the output is starting at the `binary_offset` provided. ### Method N/A ### Endpoint N/A ### Parameters #### Path Parameters - **binary_offset** (Option) - Required - The binary offset of the new line. ### Request Example N/A ### Response #### Success Response (200) - **result** (Result) - The result of the start line operation. #### Response Example N/A ``` -------------------------------- ### Config::offsets_and_lines Source: https://docs.rs/wasmprinter/latest/wasmprinter/struct.Config.html?search= Gets line-by-line WAT disassembly with binary offsets for each line. ```APIDOC ## Config::offsets_and_lines ### Description Get the line-by-line WAT disassembly for the given Wasm, along with the binary offsets for each line. ### Method `pub fn offsets_and_lines<'a>(&self, wasm: &[u8], storage: &'a mut String) -> Result, &'a str)> + 'a>` ### Parameters - **wasm** (&[u8]) - Required - The WebAssembly binary to disassemble. - **storage** (&'a mut String) - Required - A mutable string buffer used to store the output. ### Returns - `Result, &'a str)> + 'a>` - An iterator yielding pairs of optional binary offset and line text. ``` -------------------------------- ### PrintIoWrite::start_line Source: https://docs.rs/wasmprinter/latest/wasmprinter/struct.PrintIoWrite.html?search= Indicates that a new line in the output is starting at the binary_offset provided. ```APIDOC ## start_line ### Description Indicates that a new line in the output is starting at the `binary_offset` provided. ### Method `fn start_line(&mut self, binary_offset: Option)` ### Parameters - `binary_offset` (Option) - The binary offset of the start of the line. ### Returns - No return value. ``` -------------------------------- ### Print::start_line Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/print.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Indicates that a new line in the output is starting at the provided binary offset. Enables correlating binary offsets to lines in the output. ```APIDOC ## [TRAIT_METHOD] Print::start_line ### Description Indicates that a new line in the output is starting at the `binary_offset` provided. Not all new lines have a binary offset associated with them but this method should be called for new lines in the output. This enables correlating binary offsets to lines in the output. ### Method `fn start_line(&mut self, binary_offset: Option)` ### Parameters - **binary_offset** (`Option`) - Required - The binary offset of the new line, if any. ### Returns - `()` - No return value. ### Default Implementation Does nothing. ### Example ```rust use wasmprinter::Print; struct MyPrinter; impl Print for MyPrinter { fn start_line(&mut self, binary_offset: Option) { // handle line start } } ``` ``` -------------------------------- ### print_core_instantiation_arg Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/component.rs.html?search= Prints a core instantiation argument by starting a group with "with ", writing the argument name, then handling the instance kind by printing the instance index. Ends the group and returns Ok. ```rust self.start_group("with ")?; self.print_str(arg.name)?; self.result.write_str(" ")?; match arg.kind { InstantiationArgKind::Instance => { self.start_group("instance ")?; self.print_idx(&state.core.instance_names, arg.index)?; self.end_group()?; } } self.end_group()?; Ok(()) ``` -------------------------------- ### Config::new constructor Source: https://docs.rs/wasmprinter/latest/wasmprinter/struct.Config.html?search= Creates a new Config object that's ready to start printing wasm binaries to strings. ```rust pub fn new() -> Self ``` -------------------------------- ### PrintTermcolor::start_line Source: https://docs.rs/wasmprinter/latest/wasmprinter/struct.PrintTermcolor.html?search=std%3A%3Avec Indicates that a new line in the output is starting at the `binary_offset` provided. ```APIDOC ## start_line ### Description Indicates that a new line in the output is starting at the `binary_offset` provided. ### Method `fn start_line(&mut self, binary_offset: Option)` ### Parameters - `binary_offset` (Option) - Required - The binary offset of the new line. ### Returns None. ``` -------------------------------- ### Start a group in wasmprinter output Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/lib.rs.html?search=std%3A%3Avec Starts a new group in the output by writing an opening parenthesis, the group name, and resetting color. Increments nesting and records the current line for later group closing. ```rust fn start_group(&mut self, name: &str) -> Result<()> { write!(self.result, "(")?; self.result.start_keyword()?; write!(self.result, "{name}")?; self.result.reset_color()?; self.nesting += 1; self.group_lines.push(self.line); Ok(()) } ``` -------------------------------- ### Start group for component external kind Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/component.rs.html?search=std%3A%3Avec Starts a group with a label corresponding to the given component external kind. The label is used as a prefix for the printed name. ```rust match kind { ComponentExternalKind::Module => { self.start_group("core module ")?; } ComponentExternalKind::Component => { self.start_group("component ")?; } ComponentExternalKind::Instance => { self.start_group("instance ")?; } ComponentExternalKind::Func => { self.start_group("func ")?; } ComponentExternalKind::Value => { self.start_group("value ")?; } ComponentExternalKind::Type => { self.start_group("type ")?; } } Ok(()) ``` -------------------------------- ### NamedLocalPrinter::start_local Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/lib.rs.html Starts printing a local, handling named locals and group management. ```rust fn start_local( &mut self, names_for: ParameterNames, local: u32, dst: &mut Printer, state: &State, ) -> Result<()> { let name = match names_for { ParameterNames::Type(ty) => state.core.parameter_names.index_to_name.get(&(ty, local)), ParameterNames::Tag(ty) => state .core .tag_parameter_names .index_to_name .get(&(ty, local)), ParameterNames::Locals(func) => { state.core.local_names.index_to_name.get(&(func, local)) } ParameterNames::None => None, }; // Named locals must be in their own group, so if we have a name we need // to terminate the previous group. if name.is_some() && self.in_group { dst.end_group()?; self.in_group = false; } if self.first { self.first = false; } else { dst.result.write_str(" ")?; } // Next we either need a separator if we're already in a group or we // need to open a group for our new local. if !self.in_group { dst.start_group(self.group_name)?; dst.result.write_str(" ")?; self.in_group = true; } // Print the optional name if given... match name { Some(name) => { name.write(dst)?; dst.result.write_str(" ")?; self.end_group_after_local = true; } None if dst.config.name_unnamed && matches!(names_for, ParameterNames::Locals(_)) => { write!(dst.result, "$#local{local} ")?; self.end_group_after_local = true; } None => { self.end_group_after_local = false; } } Ok(()) } ``` -------------------------------- ### print_component_instantiation_arg Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/component.rs.html?search= Prints a component instantiation argument by starting a group with "with ", writing the argument name, then printing the component external kind. Ends the group and returns Ok. ```rust self.start_group("with ")?; self.print_str(arg.name)?; self.result.write_str(" ")?; self.print_component_external_kind(state, arg.kind, arg.index)?; self.end_group()?; Ok(()) ``` -------------------------------- ### Config::offsets_and_lines method Source: https://docs.rs/wasmprinter/latest/wasmprinter/struct.Config.html?search= Get the line-by-line WAT disassembly for the given Wasm, along with the binary offsets for each line. ```rust pub fn offsets_and_lines<'a>( &self, wasm: &[u8], storage: &'a mut String, ) -> Result, &'a str)> + 'a> ``` -------------------------------- ### Print::start_line Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/print.rs.html?search=std%3A%3Avec Indicates that a new line in the output is starting at the `binary_offset` provided. Not all new lines have a binary offset associated with them but this method should be called for new lines in the output. ```APIDOC ## [TRAIT_METHOD] Print::start_line ### Description Indicates that a new line in the output is starting at the `binary_offset` provided. Not all new lines have a binary offset associated with them but this method should be called for new lines in the output. This enables correlating binary offsets to lines in the output. ### Method `fn start_line(&mut self, binary_offset: Option)` ### Parameters - **binary_offset** (`Option`) - Required - The binary offset of the new line, if any. ### Returns - `()` - No return value. ### Default Implementation Does nothing. ### Example ```rust use wasmprinter::Print; struct MyPrinter; impl Print for MyPrinter { fn start_line(&mut self, binary_offset: Option) { if let Some(offset) = binary_offset { println!("Line at offset {}", offset); } } } ``` ``` -------------------------------- ### print_import method Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/lib.rs.html?search= Prints a single import by starting an import group, writing module, name, and import type. ```rust fn print_import(&mut self, state: &State, import: &Import<'_>, index: bool) -> Result<()> { self.start_group("import ")?; self.print_str(import.module)?; self.result.write_str(" ")?; self.print_str(import.name)?; self.result.write_str(" ")?; self.print_import_ty(state, &import.ty, index)?; self.end_group()?; Ok(()) } ``` -------------------------------- ### start_group and end_group helper functions Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/lib.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E These functions manage the opening and closing of groups in the output. `start_group` writes an opening parenthesis, starts a keyword, writes the group name, resets color, increments nesting, and records the current line. `end_group` decrements nesting, pops the recorded line, adds a newline if needed, and writes a closing parenthesis. ```rust fn start_group(&mut self, name: &str) -> Result<()> { write!(self.result, "(")?; self.result.start_keyword()?; write!(self.result, "{name}")?; self.result.reset_color()?; self.nesting += 1; self.group_lines.push(self.line); Ok(()) } fn end_group(&mut self) -> Result<()> { self.nesting -= 1; if let Some(line) = self.group_lines.pop() { if line != self.line { self.newline_unknown_pos()?; } } self.result.write_str(")")?; Ok(()) } ``` -------------------------------- ### Get line-by-line WAT disassembly with binary offsets Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/lib.rs.html?search= Returns an iterator over (binary offset, line) pairs for the WAT disassembly. Uses a custom Print implementation to track line start positions and offsets. ```rust pub fn offsets_and_lines<'a>( &self, wasm: &[u8], storage: &'a mut String, ) -> Result, &'a str)> + 'a> { struct TrackingPrint<'a> { dst: &'a mut String, lines: Vec, line_offsets: Vec>, } impl Print for TrackingPrint<'_> { fn write_str(&mut self, s: &str) -> io::Result<()> { self.dst.push_str(s); Ok(()) } fn start_line(&mut self, offset: Option) { self.lines.push(self.dst.len()); self.line_offsets.push(offset); } } let mut output = TrackingPrint { dst: storage, lines: Vec::new(), line_offsets: Vec::new(), }; self.print(wasm, &mut output)?; let TrackingPrint { dst, lines, line_offsets, } = output; let end = dst.len(); let dst = &dst[..]; let mut offsets = line_offsets.into_iter(); let mut lines = lines.into_iter().peekable(); Ok(std::iter::from_fn(move || { let offset = offsets.next()?; let i = lines.next()?; let j = lines.peek().copied().unwrap_or(end); let line = &dst[i..j]; Some((offset, line)) })) } ``` -------------------------------- ### Print::start_keyword Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/print.rs.html?search=std%3A%3Avec Sets the colors settings for a keyword (`(module ...)`) to be printed. ```APIDOC ## [TRAIT_METHOD] Print::start_keyword ### Description Sets the colors settings for a keyword (`(module ...)`) to be printed. ### Method `fn start_keyword(&mut self) -> io::Result<()>` ### Parameters None. ### Returns - `io::Result<()>` - Ok(()) on success, or an I/O error. ### Default Implementation Does nothing. ### Example ```rust use wasmprinter::Print; struct MyPrinter; impl Print for MyPrinter { fn start_keyword(&mut self) -> std::io::Result<()> { print!("\x1b[34m"); // blue Ok(()) } } ``` ``` -------------------------------- ### Print::start_keyword Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/print.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Sets the colors settings for a keyword (`(module ...)`) to be printed. ```APIDOC ## [TRAIT_METHOD] Print::start_keyword ### Description Sets the colors settings for a keyword (`(module ...)`) to be printed. ### Method `fn start_keyword(&mut self) -> io::Result<()>` ### Parameters None ### Returns - `io::Result<()>` - Ok(()) on success, or an I/O error. ### Default Implementation Does nothing. ### Example ```rust use wasmprinter::Print; use std::io; struct MyPrinter; impl Print for MyPrinter { fn start_keyword(&mut self) -> io::Result<()> { // set color for keywords Ok(()) } } ``` ``` -------------------------------- ### PrintIoWrite::start_keyword Source: https://docs.rs/wasmprinter/latest/wasmprinter/struct.PrintIoWrite.html?search= Sets the colors settings for a keyword (`(module ...)`) to be printed. ```APIDOC ## start_keyword ### Description Sets the colors settings for a keyword (`(module ...)`) to be printed. ### Method `fn start_keyword(&mut self) -> Result<()>` ### Returns - `Result<()>` - Ok if successful, or an error. ``` -------------------------------- ### Config::print Source: https://docs.rs/wasmprinter/latest/wasmprinter/struct.Config.html?search= Prints a WebAssembly binary to the given output in the WebAssembly Text Format. ```APIDOC ## Config::print ### Description Print a WebAssembly binary. This function takes an entire `wasm` binary blob and prints it to the `result` in the WebAssembly Text Format. ### Method `pub fn print(&self, wasm: &[u8], result: &mut impl Print) -> Result<()>` ### Parameters - **wasm** (&[u8]) - Required - The WebAssembly binary to print. - **result** (&mut impl Print) - Required - The output sink to write the text format to. ### Returns - `Result<()>` - Ok if printing succeeded, or an error. ``` -------------------------------- ### Operand stack types example Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/lib.rs.html?search= Example output showing operand stack types within function bodies, as shown in the print_operand_stack documentation. ```wasm (module (type (;0;) (func)) (func (;0;) (type 0) i32.const 4 ;; [i32] i32.const 5 ;; [i32 i32] i32.add ;; [i32] drop ;; [] ) ) ``` -------------------------------- ### PrintFmtWrite::start_keyword Source: https://docs.rs/wasmprinter/latest/wasmprinter/struct.PrintFmtWrite.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Sets the colors settings for a keyword ((module ...)) to be printed. ```APIDOC ## [METHOD] [ENDPOINT] ### Description Sets the colors settings for a keyword (`(module ...)`) to be printed. ### Method N/A ### Endpoint N/A ### Parameters None ### Request Example N/A ### Response #### Success Response (200) - **result** (Result) - The result of the color setting operation. #### Response Example N/A ``` -------------------------------- ### Print intrinsic function Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/component.rs.html?search=std%3A%3Avec Prints an intrinsic function by starting a 'core func' group, printing the function name, then starting the provided group, invoking the body closure, and ending both groups. ```rust self.start_group("core func ")?; self.print_name(&state.core.func_names, state.core.funcs)?; self.result.write_str(" ")?; self.start_group(group)?; body(self, state)?; self.end_group()?; self.end_group()?; ``` -------------------------------- ### Print::start_name Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/print.rs.html?search=std%3A%3Avec Sets the colors settings for a name (`$foo`) to be printed. ```APIDOC ## [TRAIT_METHOD] Print::start_name ### Description Sets the colors settings for a name (`$foo`) to be printed. ### Method `fn start_name(&mut self) -> io::Result<()>` ### Parameters None. ### Returns - `io::Result<()>` - Ok(()) on success, or an I/O error. ### Default Implementation Does nothing. ### Example ```rust use wasmprinter::Print; struct MyPrinter; impl Print for MyPrinter { fn start_name(&mut self) -> std::io::Result<()> { print!("\x1b[36m"); // cyan Ok(()) } } ``` ``` -------------------------------- ### Print::start_name Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/print.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Sets the colors settings for a name (`$foo`) to be printed. ```APIDOC ## [TRAIT_METHOD] Print::start_name ### Description Sets the colors settings for a name (`$foo`) to be printed. ### Method `fn start_name(&mut self) -> io::Result<()>` ### Parameters None ### Returns - `io::Result<()>` - Ok(()) on success, or an I/O error. ### Default Implementation Does nothing. ### Example ```rust use wasmprinter::Print; use std::io; struct MyPrinter; impl Print for MyPrinter { fn start_name(&mut self) -> io::Result<()> { // set color for names Ok(()) } } ``` ``` -------------------------------- ### PrintIoWrite::start_name Source: https://docs.rs/wasmprinter/latest/wasmprinter/struct.PrintIoWrite.html?search= Sets the colors settings for a name ($foo) to be printed. ```APIDOC ## start_name ### Description Sets the colors settings for a name (`$foo`) to be printed. ### Method `fn start_name(&mut self) -> Result<()>` ### Returns - `Result<()>` - Ok if successful, or an error. ``` -------------------------------- ### Config::new and builder methods Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/lib.rs.html Provides the Config constructor and builder methods for print_offsets, print_skeleton, name_unnamed, fold_instructions, print_operand_stack, and indent_text. Each returns &mut Self for chaining. ```rust impl Config { /// Creates a new [`Config`] object that's ready to start printing wasm /// binaries to strings. pub fn new() -> Self { Self::default() } /// Whether or not to print binary offsets of each item as comments in the /// text format whenever a newline is printed. pub fn print_offsets(&mut self, print: bool) -> &mut Self { self.print_offsets = print; self } /// Whether or not to print only a "skeleton" which skips function bodies, /// data segment contents, element segment contents, etc. pub fn print_skeleton(&mut self, print: bool) -> &mut Self { self.print_skeleton = print; self } /// Assign names to all unnamed items. /// /// If enabled then any previously unnamed item will have a name synthesized /// that looks like `$#func10` for example. The leading `#` indicates that /// it's `wasmprinter`-generated. The `func` is the namespace of the name /// and provides extra context about the item when referenced. The 10 is the /// local index of the item. /// /// Note that if the resulting text output is converted back to binary the /// resulting `name` custom section will not be the same as before. pub fn name_unnamed(&mut self, enable: bool) -> &mut Self { self.name_unnamed = enable; self } /// Print instructions in folded form where possible. /// /// This will cause printing to favor the s-expression (parenthesized) form /// of WebAssembly instructions. For example this output would be generated /// for a simple `add` function: /// /// ```wasm /// (module /// (func $foo (param i32 i32) (result i32) /// (i32.add /// (local.get 0) /// (local.get 1)) /// ) /// ) /// ``` pub fn fold_instructions(&mut self, enable: bool) -> &mut Self { self.fold_instructions = enable; self } /// Print the operand stack types within function bodies, /// flagging newly pushed operands when color output is enabled. E.g.: /// /// ```wasm /// (module /// (type (;0;) (func)) /// (func (;0;) (type 0) /// i32.const 4 /// ;; [i32] /// i32.const 5 /// ;; [i32 i32] /// i32.add /// ;; [i32] /// drop /// ;; [] /// ) /// ) /// ``` #[cfg(feature = "validate")] pub fn print_operand_stack(&mut self, enable: bool) -> &mut Self { self.print_operand_stack = enable; 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 } /// Print a WebAssembly binary. /// /// This function takes an entire `wasm` binary blob and prints it to the /// `result` in the WebAssembly Text Format. pub fn print(&self, wasm: &[u8], result: &mut impl Print) -> Result<()> { ``` -------------------------------- ### Initialize Printer and print WAT disassembly Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/lib.rs.html?search= Initializes a Printer with default settings and calls print_contents to generate WAT disassembly for the given Wasm bytes. ```rust result, code_section_hints: Vec::new(), group_lines: Vec::new(), line: 0, nesting: 0, } .print_contents(wasm) ``` -------------------------------- ### print_type_keyword method Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/lib.rs.html?search= Prints a type keyword by starting a type group, writing the keyword, and resetting color. ```rust fn print_type_keyword(&mut self, keyword: &str) -> Result<()> { self.result.start_type()?; self.result.write_str(keyword)?; self.result.reset_color()?; Ok(()) } ``` -------------------------------- ### PrintFmtWrite::start_name Source: https://docs.rs/wasmprinter/latest/wasmprinter/struct.PrintFmtWrite.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Sets the colors settings for a name ($foo) to be printed. ```APIDOC ## [METHOD] [ENDPOINT] ### Description Sets the colors settings for a name (`$foo`) to be printed. ### Method N/A ### Endpoint N/A ### Parameters None ### Request Example N/A ### Response #### Success Response (200) - **result** (Result) - The result of the color setting operation. #### Response Example N/A ``` -------------------------------- ### Config::new Source: https://docs.rs/wasmprinter/latest/wasmprinter/struct.Config.html?search= Creates a new Config object ready to print wasm binaries. ```APIDOC ## Config::new ### Description Creates a new `Config` object that’s ready to start printing wasm binaries to strings. ### Method `pub fn new() -> Self` ### Parameters None. ### Returns - `Self` - A new `Config` instance. ``` -------------------------------- ### Config Source: https://docs.rs/wasmprinter/latest/wasmprinter/struct.Config.html Configuration for printing WebAssembly binaries to text format. ```APIDOC ## Config::new ### Description Creates a new `Config` object that's ready to start printing wasm binaries to strings. ### Method `pub fn new() -> Self` ### Parameters None. ### Returns A new `Config` instance. ``` ```APIDOC ## Config::print_offsets ### Description Sets whether to print binary offsets of each item as comments in the text format whenever a newline is printed. ### Method `pub fn print_offsets(&mut self, print: bool) -> &mut Self` ### Parameters - **print** (bool) - Required - Whether to print offsets. ### Returns A mutable reference to `self` for chaining. ``` ```APIDOC ## Config::print_skeleton ### Description Sets whether to print only a "skeleton" which skips function bodies, data segment contents, element segment contents, etc. ### Method `pub fn print_skeleton(&mut self, print: bool) -> &mut Self` ### Parameters - **print** (bool) - Required - Whether to print skeleton. ### Returns A mutable reference to `self` for chaining. ``` ```APIDOC ## Config::name_unnamed ### Description Assign names to all unnamed items. If enabled, any previously unnamed item will have a name synthesized that looks like `$#func10` for example. The leading `#` indicates that it's `wasmprinter`-generated. The `func` is the namespace of the name and provides extra context about the item when referenced. The `10` is the local index of the item. ### Method `pub fn name_unnamed(&mut self, enable: bool) -> &mut Self` ### Parameters - **enable** (bool) - Required - Whether to enable naming of unnamed items. ### Returns A mutable reference to `self` for chaining. ``` ```APIDOC ## Config::fold_instructions ### Description Print instructions in folded form where possible. This will cause printing to favor the s-expression (parenthesized) form of WebAssembly instructions. ### Method `pub fn fold_instructions(&mut self, enable: bool) -> &mut Self` ### Parameters - **enable** (bool) - Required - Whether to enable folded instruction printing. ### Returns A mutable reference to `self` for chaining. ``` ```APIDOC ## Config::print_operand_stack ### Description Print the operand stack types within function bodies, flagging newly pushed operands when color output is enabled. Available on **crate feature `validate`** only. ### Method `pub fn print_operand_stack(&mut self, enable: bool) -> &mut Self` ### Parameters - **enable** (bool) - Required - Whether to enable operand stack printing. ### Returns A mutable reference to `self` for chaining. ``` ```APIDOC ## Config::indent_text ### Description 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 `" "`. ### Method `pub fn indent_text(&mut self, text: impl Into) -> &mut Self` ### Parameters - **text** (impl Into) - Required - The string to use for indentation. ### Returns A mutable reference to `self` for chaining. ``` ```APIDOC ## Config::print ### Description Print a WebAssembly binary. This function takes an entire `wasm` binary blob and prints it to the `result` in the WebAssembly Text Format. ### Method `pub fn print(&self, wasm: &[u8], result: &mut impl Print) -> Result<()>` ### Parameters - **wasm** (&[u8]) - Required - The WebAssembly binary to print. - **result** (&mut impl Print) - Required - The output sink to print to. ### Returns A `Result<()>` indicating success or failure. ``` ```APIDOC ## Config::offsets_and_lines ### Description Get the line-by-line WAT disassembly for the given Wasm, along with the binary offsets for each line. ### Method `pub fn offsets_and_lines<'a>(&self, wasm: &[u8], storage: &'a mut String) -> Result, &'a str)> + 'a>` ### Parameters - **wasm** (&[u8]) - Required - The WebAssembly binary to disassemble. - **storage** (&'a mut String) - Required - A mutable string to store the disassembly output. ### Returns A `Result` containing an iterator of `(Option, &'a str)` pairs, where the first element is the binary offset (if available) and the second is the line of WAT text. ``` -------------------------------- ### Print resume_table (resume_table) Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/operator.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Prints a resume_table instruction with its on clauses. Temporarily adjusts nesting for label handling and prints each handler variant. ```rust fn resume_table(&mut self, table: ResumeTable) -> Result<()> { // The start_group("resume/resume_throw") have already // increased the nesting depth, but the labels are defined // above this `resume` or `resume_throw`. Therefore we // temporarily decrement this nesting count and increase it // below after printing the on clauses. self.printer.nesting -= 1; for handle in table.handlers { self.result().write_str(" ")?; self.printer.start_group("on")?; match handle { Handle::OnLabel { tag, label } => { self.tag_index(tag)?; self.relative_depth(label)?; } Handle::OnSwitch { tag } => { self.tag_index(tag)?; self.result().write_str(" switch")?; } } self.printer.end_group()?; } self.printer.nesting += 1; Ok(()) } ``` -------------------------------- ### Payload rules for I32Const and I64Const Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/operator.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Payload rules for I32Const and I64Const. They start a literal, write the value with a leading space, and reset the color. ```rust 632 (payload $self:ident I32Const $val:ident) => ( 633 $self.result().start_literal()?; 634 write!($self.result(), " {}", $val)?; 635 $self.result().reset_color()?; 636 ); 637 (payload $self:ident I64Const $val:ident) => ( 638 $self.result().start_literal()?; 639 write!($self.result(), " {}", $val)?; 640 $self.result().reset_color()?; 641 ); ``` -------------------------------- ### Print component type reference Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/component.rs.html Prints a component type reference by starting a group, printing the index using the component type names, and ending the group. ```rust pub(crate) fn print_component_type_ref(&mut self, state: &State, idx: u32) -> Result<()> { self.start_group("type ")?; self.print_idx(&state.component.type_names, idx)?; self.end_group()?; Ok(()) } ``` -------------------------------- ### End a group at a given offset Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/lib.rs.html?search=std%3A%3Avec Ends a group at a specific byte offset, adding a newline if offsets are printed or if the group started on a different line. Writes a closing parenthesis. ```rust fn end_group_at_pos(&mut self, offset: u64) -> Result<()> { self.nesting -= 1; let start_group_line = self.group_lines.pop(); if self.config.print_offsets { self.newline(offset)?; } else if let Some(line) = start_group_line { if line != self.line { self.newline(offset)?; } } self.result.write_str(")")?; Ok(()) } ``` -------------------------------- ### Print::start_type Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/print.rs.html?search=std%3A%3Avec Sets the colors settings for a type (`(param i32)`) to be printed. ```APIDOC ## [TRAIT_METHOD] Print::start_type ### Description Sets the colors settings for a type (`(param i32)`) to be printed. ### Method `fn start_type(&mut self) -> io::Result<()>` ### Parameters None. ### Returns - `io::Result<()>` - Ok(()) on success, or an I/O error. ### Default Implementation Does nothing. ### Example ```rust use wasmprinter::Print; struct MyPrinter; impl Print for MyPrinter { fn start_type(&mut self) -> std::io::Result<()> { print!("\x1b[35m"); // magenta Ok(()) } } ``` ``` -------------------------------- ### Print::start_type Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/print.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Sets the colors settings for a type (`(param i32)`) to be printed. ```APIDOC ## [TRAIT_METHOD] Print::start_type ### Description Sets the colors settings for a type (`(param i32)`) to be printed. ### Method `fn start_type(&mut self) -> io::Result<()>` ### Parameters None ### Returns - `io::Result<()>` - Ok(()) on success, or an I/O error. ### Default Implementation Does nothing. ### Example ```rust use wasmprinter::Print; use std::io; struct MyPrinter; impl Print for MyPrinter { fn start_type(&mut self) -> io::Result<()> { // set color for types Ok(()) } } ``` ``` -------------------------------- ### define_visit! macro: payload rules for I32Const and I64Const Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/operator.rs.html These rules print constant values as literals, starting a literal group, writing the value with a leading space, and resetting the color. ```rust (payload $self:ident I32Const $val:ident) => ( $self.result().start_literal()?; write!($self.result(), " {}", $val)?; $self.result().reset_color()?; ); (payload $self:ident I64Const $val:ident) => ( $self.result().start_literal()?; write!($self.result(), " {}", $val)?; $self.result().reset_color()?; ); ``` -------------------------------- ### print_types: Iterate over type section entries Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/lib.rs.html?search=std%3A%3Avec This snippet shows the print_types function that iterates over a TypeSectionReader, printing each type with its offset. It uses into_iter_with_offsets to get offset and rec_group pairs. ```rust fn print_types(&mut self, state: &mut State, parser: TypeSectionReader<'_>) -> Result<()> { for ty in parser.into_iter_with_offsets() { let (offset, rec_group) = ty?; self.newline(offset)?; self.print_rec(state, Some(offset), rec_group, false)?; } Ok(()) } ``` -------------------------------- ### print_file Source: https://docs.rs/wasmprinter/latest/wasmprinter/fn.print_file.html Reads a WebAssembly file from the filesystem and prints it into an in-memory `String`. ```APIDOC ## Function `print_file` ### Description Reads a WebAssembly file from the filesystem and prints it into an in-memory `String`. ### Signature ```rust pub fn print_file(file: impl AsRef) -> Result ``` ### Parameters - **file** (`impl AsRef`) - Required - The path to the WebAssembly file to read and print. ### Returns - `Result` - The printed WebAssembly representation as a `String`, or an error if reading or printing fails. ``` -------------------------------- ### End a group in wasmprinter output Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/lib.rs.html?search=std%3A%3Avec Ends a group by decrementing nesting, adding a newline if the group started on a different line, and writing a closing parenthesis. Uses `newline_unknown_pos` when the position is not known. ```rust fn end_group(&mut self) -> Result<()> { self.nesting -= 1; if let Some(line) = self.group_lines.pop() { if line != self.line { self.newline_unknown_pos()?; } } self.result.write_str(")")?; Ok(()) } ``` -------------------------------- ### Config::print method Source: https://docs.rs/wasmprinter/latest/wasmprinter/struct.Config.html?search= Print a WebAssembly binary. This function takes an entire `wasm` binary blob and prints it to the `result` in the WebAssembly Text Format. ```rust pub fn print(&self, wasm: &[u8], result: &mut impl Print) -> Result<()> ``` -------------------------------- ### Print Component Type Declarations Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/component.rs.html?search=std%3A%3Avec Prints a component type by pushing a new state, starting a 'component' group, and iterating over declarations. Handles core type, type, alias, and export declarations. ```rust pub(crate) fn print_component_type<'a>( &mut self, states: &mut Vec, decls: Vec>, ) -> Result<()> { states.push(State::new(Encoding::Component)); self.newline_unknown_pos()?; self.start_group("component")?; for decl in decls { self.newline_unknown_pos()?; match decl { ComponentTypeDeclaration::CoreType(ty) => self.print_core_type(states, ty)?, ComponentTypeDeclaration::Type(ty) => self.print_component_type_def(states, ty)?, ComponentTypeDeclaration::Alias(alias) => { self.print_component_alias(states, alias)?; } ComponentTypeDeclaration::Export { name, ty } => { self.start_group("export ")?; self.print_component_kind_name(states.last_mut().unwrap(), ty.kind())?; self.result.write_str(" ")?; self.print_component_extern_name(&name)?; } } } self.end_group()?; states.pop(); Ok(()) } ``` -------------------------------- ### Print Module Type Declarations Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/component.rs.html?search=std%3A%3Avec Prints a module type by pushing a new state, starting a 'module' group, and iterating over declarations. Handles type, outer alias, export, and import declarations. ```rust pub(crate) fn print_module_type( &mut self, states: &mut Vec, decls: Vec, ) -> Result<()> { states.push(State::new(Encoding::Module)); self.newline_unknown_pos()?; self.start_group("module")?; for decl in decls { self.newline_unknown_pos()?; match decl { ModuleTypeDeclaration::Type(rec) => { self.print_rec(states.last_mut().unwrap(), None, rec, false)? } ModuleTypeDeclaration::OuterAlias { kind, count, index } => { self.print_outer_alias(states, kind, count, index)?; } ModuleTypeDeclaration::Export { name, ty } => { self.start_group("export ")?; self.print_str(name)?; self.result.write_str(" ")?; self.print_import_ty(states.last_mut().unwrap(), &ty, false)?; self.end_group()?; } ModuleTypeDeclaration::Import(import) => { self.print_import(states.last_mut().unwrap(), &import, false)?; } } } self.end_group()?; states.pop(); Ok(()) } ``` -------------------------------- ### Print component type reference Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/component.rs.html?search=std%3A%3Avec Prints a component type reference by starting a group labeled "type ", printing the index using the component type names, and ending the group. ```rust pub(crate) fn print_component_type_ref(&mut self, state: &State, idx: u32) -> Result<()> { self.start_group("type ")?; self.print_idx(&state.component.type_names, idx)?; self.end_group()?; Ok(()) } ``` -------------------------------- ### PrintFmtWrite::start_type Source: https://docs.rs/wasmprinter/latest/wasmprinter/struct.PrintFmtWrite.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Sets the colors settings for a type ((param i32)) to be printed. ```APIDOC ## [METHOD] [ENDPOINT] ### Description Sets the colors settings for a type (`(param i32)`) to be printed. ### Method N/A ### Endpoint N/A ### Parameters None ### Request Example N/A ### Response #### Success Response (200) - **result** (Result) - The result of the color setting operation. #### Response Example N/A ``` -------------------------------- ### Macros for StructGet, StructGetS, StructGetU, and StructSet payload printing Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/operator.rs.html?search=std%3A%3Avec Print the struct type index and field index for struct get and set operations. All four macros share the same printing logic. ```rust (payload $self:ident StructGet $ty:ident $field:ident) => ( $self.struct_type_index($ty)?; $self.push_str(" ")?; $self.printer.print_field_idx($self.state, $ty, $field)?; ); ``` ```rust (payload $self:ident StructGetS $ty:ident $field:ident) => ( $self.struct_type_index($ty)?; $self.push_str(" ")?; $self.printer.print_field_idx($self.state, $ty, $field)?; ); ``` ```rust (payload $self:ident StructGetU $ty:ident $field:ident) => ( $self.struct_type_index($ty)?; $self.push_str(" ")?; $self.printer.print_field_idx($self.state, $ty, $field)?; ); ``` ```rust (payload $self:ident StructSet $ty:ident $field:ident) => ( $self.struct_type_index($ty)?; $self.push_str(" ")?; $self.printer.print_field_idx($self.state, $ty, $field)?; ); ``` -------------------------------- ### PrintTermcolor::start_keyword Source: https://docs.rs/wasmprinter/latest/wasmprinter/struct.PrintTermcolor.html?search=std%3A%3Avec Sets the colors settings for a keyword (`(module ...)`) to be printed. ```APIDOC ## start_keyword ### Description Sets the colors settings for a keyword (`(module ...)`) to be printed. ### Method `fn start_keyword(&mut self) -> Result<()>` ### Parameters None. ### Returns - `Result<()>` - Ok(()) on success, or an error. ``` -------------------------------- ### PrintIoWrite::start_type Source: https://docs.rs/wasmprinter/latest/wasmprinter/struct.PrintIoWrite.html?search= Sets the colors settings for a type (`(param i32)`) to be printed. ```APIDOC ## start_type ### Description Sets the colors settings for a type (`(param i32)`) to be printed. ### Method `fn start_type(&mut self) -> Result<()>` ### Returns - `Result<()>` - Ok if successful, or an error. ``` -------------------------------- ### end_group_at_pos helper function Source: https://docs.rs/wasmprinter/latest/src/wasmprinter/lib.rs.html?search=Option%3CT%3E%2C+%28T+-%3E+U%29+-%3E+Option%3CU%3E Closes a group at a specific offset. If `print_offsets` is enabled, it always adds a newline at the offset; otherwise, it adds a newline only if the group started on a different line. Then writes a closing parenthesis. ```rust fn end_group_at_pos(&mut self, offset: u64) -> Result<()> { self.nesting -= 1; let start_group_line = self.group_lines.pop(); if self.config.print_offsets { self.newline(offset)?; } else if let Some(line) = start_group_line { if line != self.line { self.newline(offset)?; } } self.result.write_str(")")?; Ok(()) } ```