Subtract Module - Wat Typing CST Test
Loading…
Subtract Module — Wat Code
A WAT module that subtracts two numbers.
# wat/demo/subtract.wat
(module
(func $subtract (param $a i32) (param $b i32) (result i32)
local.get $a
local.get $b
i32.sub)
(export "subtract" (func $subtract))
)Wat Language Guide
WAT (WebAssembly Text Format) is the human-readable, assembly-like syntax used to represent WebAssembly binaries. It allows developers to write, inspect, debug, and understand Wasm modules using a clear, text-based format before compiling to .wasm.
Primary Use Cases
- ▸Learning WebAssembly internals
- ▸Debugging or inspecting Wasm modules
- ▸Creating tiny hand-crafted Wasm binaries
- ▸Reverse engineering WebAssembly
- ▸Testing Wasm instructions or host bindings
Notable Features
- ▸Human-readable text representation of Wasm
- ▸Direct 1:1 mapping to WebAssembly binary
- ▸Minimal syntax with S-expression format
- ▸Precise control over memory, tables, types, and functions
- ▸Perfect for inspection, debugging, and educational use
Origin & Creator
WebAssembly was created by engineers from Mozilla, Google, Microsoft, and Apple. WAT is part of the core WebAssembly specification managed by the W3C WebAssembly Community Group.
Industrial Note
WAT is essential for low-level debugging, reverse engineering, creating minimal Wasm modules, and understanding how higher-level languages compile to WebAssembly.
Quick Explain
- ▸WAT provides a low-level, readable, LISP-like syntax for WebAssembly modules.
- ▸It directly maps to the binary WebAssembly format, instruction-by-instruction.
- ▸Used primarily for debugging, hand-crafted Wasm, education, and minimal runtime experimentation.
- ▸Supports fine-grained control over Wasm memory, functions, imports, exports, and types.
- ▸Typically compiled into Wasm using wat2wasm tools or integrated toolchains.
Core Features
- ▸Module definitions using `(module ...)`
- ▸Functions with explicit param/returns
- ▸Linear memory definition and access
- ▸Imports and exports
- ▸Instructions for control flow, arithmetic, memory, tables
Learning Path
- ▸Understand Wasm basics
- ▸Learn WAT syntax
- ▸Use wat2wasm / wasm2wat tools
- ▸Write low-level modules
- ▸Integrate with JavaScript
Practical Examples
- ▸Basic arithmetic Wasm module
- ▸Load/store operations in linear memory
- ▸Custom WebAssembly types
- ▸Host imports (JS -> Wasm)
- ▸Binary dissection & optimizations
Comparisons
- ▸WAT vs Wasm Binary: WAT is readable; binary is compact and executable
- ▸WAT vs Rust-Wasm: WAT is manual; Rust generates Wasm automatically
- ▸WAT vs AssemblyScript: WAT is low-level; AS is high-level TS-like
- ▸WAT vs C/C++-Wasm: WAT is hand-crafted; C compiles high-level code
- ▸WAT vs Go/TinyGo: WAT avoids runtimes entirely
Strengths
- ▸Extremely lightweight and minimal
- ▸Perfect for learning WebAssembly internals
- ▸Direct control over Wasm structure and instructions
- ▸Readable and easy to experiment with
- ▸Useful for debugging compiler output
Limitations
- ▸Not suitable for large-scale application development
- ▸No high-level abstractions (loops, structs, variables)
- ▸Verbose for anything beyond small modules
- ▸Hard to maintain manually
- ▸Manual memory management required
When NOT to Use
- ▸Large-scale applications
- ▸Anything requiring high-level abstractions
- ▸UI-heavy web apps
- ▸Projects expecting fast development cycles
- ▸Situations requiring strong type systems
Cheat Sheet
- ▸(module ... ) - define module
- ▸(func (param i32) (result i32) ... )
- ▸(memory 1) - 1 page (64KiB)
- ▸i32.load / i32.store - memory ops
- ▸export - expose functions
FAQ
- ▸Is WAT mandatory for using WebAssembly?
- ▸No - it's optional, used for debugging and learning.
- ▸Is WAT slower than binary?
- ▸No - WAT is compiled to binary before execution.
- ▸Can you build entire apps in WAT?
- ▸Not practical - WAT is too low-level.
- ▸Does WAT support variables?
- ▸Local variables exist but operations still use stack.
- ▸Can WAT interact with JS?
- ▸Yes - via import/export declarations.
30-Day Skill Plan
- ▸Week 1: WAT syntax, module structure
- ▸Week 2: Instructions & stack machine
- ▸Week 3: Memory + tables + imports
- ▸Week 4: Host integration & debugging
- ▸Week 5: Performance tuning & binary inspection
Final Summary
- ▸WAT is the human-readable text format for WebAssembly.
- ▸It offers full low-level control over functions, memory, and types.
- ▸Ideal for debugging, learning, and crafting minimal Wasm binaries.
- ▸Not suited for large applications but excellent for performance insights.
- ▸Essential tool for anyone working deeply with WebAssembly internals.
Project Structure
- ▸module.wat - main Wasm text module
- ▸module.wasm - compiled binary
- ▸index.js - loader/interop script
- ▸tests/ - validate Wasm output
- ▸examples/ - sample WAT modules
Monetization
- ▸Build ultra-small libraries for performance-critical apps
- ▸Offer Wasm microservices
- ▸Educational material (courses, books)
- ▸Wasm debugging/optimization tools
- ▸Security auditing of Wasm binaries
Productivity Tips
- ▸Use wasm2wat to learn compiler output
- ▸Write small modules to test ideas
- ▸Use locals to avoid stack complexity
- ▸Automate wat2wasm builds
- ▸Visualize control flow using Wasm explorer
Basic Concepts
- ▸Stack machine - operations push/pop values
- ▸Linear memory - explicit memory access
- ▸S-expressions - LISP-like syntax
- ▸Instructions - Wasm opcodes in text form
- ▸Module structure - imports, exports, functions, types