LinuxCommandLibrary

wat2wasm

Convert WebAssembly text format to binary

TLDR

Parse and check a file for errors

$ wat2wasm [file.wat]
copy

Write the output binary to a given file
$ wat2wasm [file.wat] [[-o|--output]] [file.wasm]
copy

Display simplified representation of every byte
$ wat2wasm [[-v|--verbose]] [file.wat]
copy

SYNOPSIS

wat2wasm [options] <input.wat> [-o <output.wasm>]

The command takes a WebAssembly Text Format (.wat) file as input. If the -o or --output option is specified, the resulting WebAssembly Binary Format (.wasm) is written to the given output file; otherwise, it is written to standard output.

PARAMETERS

-h, --help
    Displays a help message with available options.

-v, --version
    Shows the version information of the wat2wasm utility.

-o FILE, --output=FILE
    Specifies the output file for the WebAssembly binary. If omitted, the output is written to standard output (stdout).

-r, --relocatable
    Generates a relocatable WebAssembly binary, suitable for linking with other modules later.

-d, --debug-names
    Adds debug names to the WebAssembly binary, aiding in debugging and inspection. This is the default if no source map is generated.

--no-debug-names
    Explicitly prevents the addition of debug names to the WebAssembly binary.

--inline-source-map
    Embeds the source map directly into the WebAssembly binary. This is the default if no output file is specified.

--no-inline-source-map
    Disables embedding the source map into the WebAssembly binary.

--source-map-url=URL
    Specifies a URL for the external source map file. This option is used when the source map is not inlined.

--source-map-url-prologue=PROLOGUE
    Prepends custom text to the source map URL, useful for CDN paths or specific server configurations.

--enable-feature=<feature>
    Enables a specific experimental WebAssembly feature (e.g., simd, threads, reference-types). Can be specified multiple times.

--disable-feature=<feature>
    Disables a specific WebAssembly feature. Can be specified multiple times.

DESCRIPTION

wat2wasm is a command-line utility provided as part of the WebAssembly Binary Toolkit (WABT). Its primary function is to compile WebAssembly Text Format (.wat) files into WebAssembly Binary Format (.wasm) files. The .wat format is a human-readable and writable textual representation of WebAssembly modules, often used for manual authoring, debugging, or as an intermediate target for compilers. Conversely, the .wasm format is a compact, efficient, and executable binary format that WebAssembly runtimes (like those in web browsers or standalone environments) consume directly. This tool is indispensable in the WebAssembly development workflow, bridging the gap between human-friendly text and machine-optimized binary code, enabling developers to iteratively design, test, and deploy WebAssembly modules.

CAVEATS

wat2wasm is a low-level utility focused purely on the WebAssembly Text to Binary conversion. It does not compile higher-level languages (like C/C++, Rust, or AssemblyScript) directly into WebAssembly; for that, one would use compilers like Clang with LLVM, Rustc, or Emscripten. The tool relies on the input .wat file adhering strictly to the WebAssembly Text Format syntax. Support for WebAssembly features via --enable-feature and --disable-feature depends on the version of WABT being used, and experimental features might change or be removed in future versions.

INPUT/OUTPUT BEHAVIOR

When the -o or --output option is omitted, wat2wasm will write the resulting WebAssembly binary to standard output (stdout). This allows for piping the output to other commands or redirecting it easily. Similarly, if the --inline-source-map option is used without specifying an output file (i.e., outputting to stdout), the source map will be inlined by default.

WEBASSEMBLY FEATURES

The --enable-feature and --disable-feature options are used to control support for specific WebAssembly proposals and experimental features. Examples of such features include SIMD (Single Instruction, Multiple Data), threads, reference types, exception handling, and tail calls. Using these options allows developers to compile WebAssembly modules that leverage newer capabilities, but it's important to ensure that the target WebAssembly runtime also supports these features.

HISTORY

wat2wasm is a foundational component of the WebAssembly Binary Toolkit (WABT), an open-source project providing a suite of tools for working with WebAssembly. WABT was created and is maintained by members of the WebAssembly community to provide essential utilities for parsing, validating, encoding, and decoding WebAssembly modules. As WebAssembly itself emerged as a crucial web standard (and now an increasingly important runtime outside browsers) for high-performance, secure, and portable code, tools like wat2wasm became indispensable. Its development has closely tracked the evolution of the WebAssembly specification, ensuring compliance with new features and best practices, and facilitating the transition from human-readable text to the compact binary format required for deployment and execution.

SEE ALSO

Related tools often used in conjunction with wat2wasm or in the broader WebAssembly ecosystem include:, wasm2wat(1): The inverse operation, converting WebAssembly binary to text format., wasm-objdump(1): A utility for displaying information about WebAssembly object files., wasm-interp(1): An interpreter for WebAssembly binaries, useful for testing., clang(1): A C/C++/Objective-C/Objective-C++ compiler front-end that can target WebAssembly via LLVM., emcc(1): The Emscripten compiler front-end, used to compile C/C++ code into WebAssembly and JavaScript.

Copied to clipboard