WASM Internals 1
Introduction
This first part introduces the most basic WASM module in textual format (.wat) and several insights in the generated bytecode file (.wasm). Then a more elaborated wasm program with a simple computation is shown. This computation will be the square of a given natural number. And to conclude, two different options are explored for being capable of executing the compiled wasm programs. One of these is execution over the Node runtime and the other inside the browser via Javascript interoperability.
Most basic webassembly module
File: empty_module.wat
Compile the textual representation of a webassembly program to bytecode via wat2wasm compiler.
e.g:
Compiler flags:
-o output_filename: Specifies the target generated file.
Inspect the contents of the wasm file to see what it looks like. UNIX command -> xxd
e.g: empty_module.wasm
For fine grained bytecode representation of the generated output file, specify the flag for verbose compilation. Opcodes underneath. will be shown.
Compiler flags:
-v
e.g: empty_module.wasm
NOTE: The previously opcodes shown as examples are universally present in every wasm file as main headers.
A webassembly module with a simple computation
Name: math.wat Description: Take a number a compute its square. Then call it from the browser and exectue it.
Detailed bytecode opcodes (math.wasm):
Execution procedures
Node runtime
npm init
Package.json
Execute the file index.js e.g: square(10)
Inside the browser
Create an Express.js server.
Create and instatiate the appropiated server process in a file named server.js.
Create a public folder and move math.wasm to its location.
4. Execution of the server program.
5. Create a standar HTML document and call the appropiate WASM module there.
5.1 Inside the browser go to: localhost:3000/test.html
5.2 Open the developer tools.
5.3 The output will be in the console

References
Chris Hay Youtube channel. Special thanks to him and his great work which led me to this field.
Programming WebAssembly with Rust, by Kevin Hoffman (book)
Understanding WebAssembly text format, MDN Web Docs
Converting WebAssembly text format to wasm, MDN Web Docs
Last updated

