What is WebAssembly? - Rust and WebAssembly (original) (raw)
- 1. Introduction
- 2. Why Rust and WebAssembly?
- 3. Background And Concepts
- 4. Tutorial
- 5. Reference
- 5.1. Crates You Should Know
- 5.2. Tools You Should Know
- 5.3. Project Templates
- 5.4. Debugging
- 5.5. Time Profiling
- 5.6. Shrinking .wasm Size
- 5.7. JavaScript Interoperation
- 5.8. Which Crates Will Work Off-the-Shelf with WebAssembly?
- 5.9. How to Add WebAssembly Support to a General-Purpose Crate
- 5.10. Deploying Rust and WebAssembly to Production
Rust and WebAssembly
WebAssembly (wasm) is a simple machine model and executable format with anextensive specification. It is designed to be portable, compact, and execute at or near native speeds.
As a programming language, WebAssembly is comprised of two formats that represent the same structures, albeit in different ways:
- The
.wat
text format (calledwat
for "WebAssembly Text") usesS-expressions, and bears some resemblance to the Lisp family of languages like Scheme and Clojure. - The
.wasm
binary format is lower-level and intended for consumption directly by wasm virtual machines. It is conceptually similar to ELF and Mach-O.
For reference, here is a factorial function in wat
:
(module
(func $fac (param f64) (result f64)
local.get 0
f64.const 1
f64.lt
if (result f64)
f64.const 1
else
local.get 0
local.get 0
f64.const 1
f64.sub
call $fac
f64.mul
end)
(export "fac" (func $fac)))
If you're curious about what a wasm
file looks like you can use the wat2wasm demo with the above code.
WebAssembly has a very simple memory model. A wasm module has access to a single "linear memory", which is essentially a flat array of bytes. Thismemory can be grown by a multiple of the page size (64K). It cannot be shrunk.
Is WebAssembly Just for the Web?
Although it has currently gathered attention in the JavaScript and Web communities in general, wasm makes no assumptions about its host environment. Thus, it makes sense to speculate that wasm will become a "portable executable" format that is used in a variety of contexts in the future. As of_today_, however, wasm is mostly related to JavaScript (JS), which comes in many flavors (including both on the Web and Node.js).