/ src / what-is-webassembly.md
what-is-webassembly.md
 1  # What is WebAssembly?
 2  
 3  WebAssembly (wasm) is a simple machine model and executable format with an
 4  [extensive specification]. It is designed to be portable, compact, and execute
 5  at or near native speeds.
 6  
 7  As a programming language, WebAssembly is comprised of two formats that
 8  represent the same structures, albeit in different ways:
 9  
10  1. The `.wat` text format (called `wat` for "**W**eb**A**ssembly **T**ext") uses
11     [S-expressions], and bears some resemblance to the Lisp family of languages
12     like Scheme and Clojure.
13  
14  2. The `.wasm` binary format is lower-level and intended for consumption
15     directly by wasm virtual machines. It is conceptually similar to ELF and
16     Mach-O.
17  
18  For reference, here is a factorial function in `wat`:
19  
20  ```
21  (module
22    (func $fac (param f64) (result f64)
23      local.get 0
24      f64.const 1
25      f64.lt
26      if (result f64)
27        f64.const 1
28      else
29        local.get 0
30        local.get 0
31        f64.const 1
32        f64.sub
33        call $fac
34        f64.mul
35      end)
36    (export "fac" (func $fac)))
37  ```
38  
39  If you're curious about what a `wasm` file looks like you can use the [wat2wasm
40  demo] with the above code.
41  
42  ## Linear Memory
43  
44  WebAssembly has a very simple [memory model]. A wasm module has access to a
45  single "linear memory", which is essentially a flat array of bytes. This
46  [memory can be grown] by a multiple of the page size (64K). It cannot be shrunk.
47  
48  ## Is WebAssembly Just for the Web?
49  
50  Although it has currently gathered attention in the JavaScript and Web
51  communities in general, wasm makes no assumptions about its host
52  environment. Thus, it makes sense to speculate that wasm will become a "portable
53  executable" format that is used in a variety of contexts in the future. As of
54  *today*, however, wasm is mostly related to JavaScript (JS), which comes in many
55  flavors (including both on the Web and [Node.js]).
56  
57  [memory model]: https://webassembly.github.io/spec/core/syntax/modules.html#syntax-mem
58  [memory can be grown]: https://webassembly.github.io/spec/core/syntax/instructions.html#syntax-instr-memory
59  [extensive specification]: https://webassembly.github.io/spec/
60  [value types]: https://webassembly.github.io/spec/core/syntax/types.html#value-types
61  [Node.js]: https://nodejs.org
62  [S-expressions]: https://en.wikipedia.org/wiki/S-expression
63  [wat2wasm demo]: https://webassembly.github.io/wabt/demo/wat2wasm/