| Crates.io | wat-fmt |
| lib.rs | wat-fmt |
| version | 0.0.8 |
| created_at | 2025-04-15 02:47:20.666511+00 |
| updated_at | 2025-04-15 02:47:20.666511+00 |
| description | A pretty formatter for WebAssembly Text Format |
| homepage | https://inferara.com |
| repository | https://github.com/Inferara/inf-wasm-tools |
| max_upload_size | |
| id | 1633866 |
| size | 22,581 |
wat-fmtA pretty formatter for WebAssembly Text Format. It is fast, flexible, and supports Inference non-deterministic instructions.
wat-fmt is a one file crate with #[no_std] support. It can be built for different targets. The standard cargo build command will build the crate for the host target.
Since wat-fmt is a no_std crate, it can be built for WASM. This is useful for running the formatter in the browser or in a WebAssembly runtime.
Prerequisites:
cargo install wasm-pack
Alternatively, you can use another tool to build WASM binaries.
Uncomment the crate type in the Cargo.toml file:
[lib]
crate-type = ["cdylib"]
To build the crate for WASM, run the following command with the wasm feature:
wasm-pack build --target web --features wasm
Source: (module (func $add (param $a i32) (param $b i32) (result i32) (local $c i32) i32.uzumaki local.set $c local.get $a local.get $c i32.add) (export "add" (func $add) ) )
Formatted:
(module
(func $add (param $a i32) (param $b i32) (result i32)
(local $c i32)
i32.uzumaki
local.set $c
local.get $a
local.get $c
i32.add
)
(export "add" (func $add) )
)
index.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>WAT Formatter</title>
<script type="module">
import {handleButtonClick} from './main.js';
</script>
</head>
<body>
<h1>WAT Formatter by <a href="https://inferara.com/">Inferara</a></h1>
<textarea id="input" rows="10" cols="30">(module (func $add (param $a i32) (param $b i32) (result i32) (local $c i32) i32.uzumaki local.set $c local.get $a local.get $c i32.add) (export "add" (func $add) ) )</textarea>
<button onclick="handleButtonClick()">Format Input</button>
<h2>Output:</h2>
<pre id="output"></pre>
<script type="module" src="./main.js"></script>
</body>
</html>
main.js
import init, { format } from '../pkg/wat_fmt.js';
let wasmInitialized = false;
export async function initWasm() {
await init();
wasmInitialized = true;
}
export async function handleButtonClick() {
if (!wasmInitialized) {
console.error("WebAssembly module is not initialized.");
return;
}
const input = document.getElementById('input').value;
try {
const result = format(input);
document.getElementById('output').textContent = result;
} catch (error) {
console.error("Error calling format function:", error);
}
}
window.onload = initWasm;
window.handleButtonClick = handleButtonClick;
MIT