A good question would be asking why WebAssembly outside a browser, as it was idealized to be run in one. I know, I also did it. Searching around on the internet I have found many articles talking about it and its utilities, but in resume, security, speed and portability.
Security because it does not have access direct to the "host" both browser and out from it.
Speed by its "low level"
Portability by the ability to be system agnostic
Being simplistic, It has use cases in:
Serverless
Blockchain: Ethereum 2.0
IoT
Games
Disclaimer: In this blog I omit cores about WebAssembly just to be simple and direct to build a "Getting started" to the theme. At the end of the blog, I provide many links what has good explanations for a further leaning.
What is WebAssembly?
As WebAssembly official site define:
WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable compilation target for programming languages, enabling deployment on the web for client and server applications.
I could simplify this definition as:
A binary code what can be run in the browser with high performance.
Obviously, it is too flat, but it is enough for now.
That definition has a little prank, the WebAssembly does not only run in the browser, it can be run in a runtime, like Node.js, offering a good performance and security.
What is wasmtime
?
To run your WebAssembly code out from a browser, you will need a runtime calledwasmtime
. The wasmtime
is a project from the Bytecode Alliance powered to run WebAssembly as a command-line utility or a lib in another project. Theoretically, basing by the nature of WebAssembly
it does not have access to the "host" and the API from the system, it is where WASI
come out.
What is WASI
?
The WASI
stands for WebAssembly System Interface, and it is a common system-level for WebAssembly, making it easier to connect the "host" with the runtime.
youtube.com/watch?v=ggtEJC0Jv8A
Installing wasmtime
In Linux distributions and macOS, just run this command, follow the steps and the installations should go well.
curl <https://wasmtime.dev/install.sh> -sSf | bash
Compiling
The project maintains officially WASI
for C/C++ and Rust. Here I will use Rust to demonstrate how it works. First we add the wasm32-wasi
to build our Rust code to WASM
and enable it to be run with the wasmtime
rustup target add wasm32-wasi
If everything is okay, you can now compile your Rust project with target to the wasm32-wasi
cargo build --target wasm32-wasi
The compiled module is in target/wasm32-wasi/debug
with the name what you defined.
The WASI
API is not working fully currently, but it is a matter of time.
Running
For executing a simple Rust hello world, just run:
wasmtime run target/wasm32-wasi/debug/program.wasm
Without run
wasmtime target/wasm32-wasi/debug/program.wasm
The running process has some issues about the sandbox
of the wasmtime
like the permission for access directories.
You can confer by yourself on the wasmtime
repository or in the documentation
As this little blog is just a getting started, I have not deepened in the concepts, but I provide the source from the content for you to learn more.
I read about that in a free course in the Edx available for the Linux Foundation. This is a free course, so you go there and take it and learn more.
Thanks for reading and I hope you liked it. Feel free to either like, comment, correct me or just say a hi, I will like too.