...

/

Converting WASM into C

Converting WASM into C

Learn how to convert WASM into C using the wasm2c command.

We'll cover the following...

WABT has a wasm2c converter that converts WASM into C source code and a header.

Let’s create a simple.wat file:

Press + to interact
touch simple.wat

Add the following contents to simple.wat:

Press + to interact
(module
(func $uanswer (result i32)
i32.const 22
i32.const 20
i32.add
)
)

wat here defines a uanswer function that adds 22 and 20 to give 42 as the answer. Let's create a WebAssembly binary using wat2wasm:

Press + to interact
main.sh
simple.wat
/path/to/build/directory/of/wabt/wat2wasm simple.wat -o simple.wasm

This generates the simple.wasm binary. Now, convert the binary into C code using wasm2c:

Press + to interact
main.sh
simple.wat
(module
(func $uanswer (result i32)
i32.const 22
i32.const 20
i32.add
)
)

This generates simple.c and simple.h.

Note: Both simple.c and simple.h might look huge. ...