Maxim
Maxim

Reputation: 243

Compilation to the web

What format does WebAssembly compile code to?

From official site:

WebAssembly or wasm is a new portable, size- and load-time-efficient format 
suitable for compilation to the web.

Upvotes: 1

Views: 113

Answers (1)

ColinE
ColinE

Reputation: 70160

WebAssembly is compiled to, and typically distributed in, a binary format.

For example, here is a simple C function:

int increment(int input) {
  return ++input;
}

Which compiles to the following:

00000000: 0061 736d 0100 0000 0106 0160 017f 017f  .asm.......`....
00000010: 0302 0100 0404 0170 0000 0503 0100 0107  .......p........
00000020: 1602 066d 656d 6f72 7902 0009 696e 6372  ...memory...incr
00000030: 656d 656e 7400 000a 0901 0700 2000 4101  ement....... .A.
00000040: 6a0b   

You can find a specification for this binary encoding in the WebAssembly design documents.

However, if you want to see the compiled output, you can use the wasm2wast tool to convert it into a more readable text format (S-expression). Here is the same code:

(module
 (table 0 anyfunc)
 (memory $0 1)
 (export "memory" (memory $0))
 (export "increment" (func $increment))
 (func $increment (param $0 i32) (result i32)
  (i32.add
   (get_local $0)
   (i32.const 1)
  )
 )
)

Upvotes: 4

Related Questions