What is WebAssembly?

WebAssembly (wasm) is a simple machine model and executable format with anextensive specification. It is designed to be portable, compact, and executeat or near native speeds.

As a programming language, WebAssembly is comprised of two formats thatrepresent the same structures, albeit in different ways:

  • The .wat text format (called wat for "WebAssembly Text") usesS-expressions, and bears some resemblance to the Lisp family of languageslike Scheme and Clojure.

  • The .wasm binary format is lower-level and intended for consumptiondirectly by wasm virtual machines. It is conceptually similar to ELF andMach-O.

For reference, here is a factorial function in wat:

  1. (module
  2. (func $fac (param f64) (result f64)
  3. get_local 0
  4. f64.const 1
  5. f64.lt
  6. if (result f64)
  7. f64.const 1
  8. else
  9. get_local 0
  10. get_local 0
  11. f64.const 1
  12. f64.sub
  13. call $fac
  14. f64.mul
  15. end)
  16. (export "fac" (func $fac)))

If you're curious about what a wasm file looks like you can use the wat2wasmdemo with the above code.

Linear Memory

WebAssembly has a very simple memory model. A wasm module has access to asingle "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 Webcommunities in general, wasm makes no assumptions about its hostenvironment. Thus, it makes sense to speculate that wasm will become a "portableexecutable" format that is used in a variety of contexts in the future. As oftoday, however, wasm is mostly related to JavaScript (JS), which comes in manyflavors (including both on the Web and Node.js).