Introduction
Til now, the only way to build a WebAssembly (wasm) app for exaequOS was to use the fork of emscripten toolchain (github). It has been adapted to the system calls of the EXA microkernel of exaequOS. Moreover it is used to build the kernel itself (github). exaequOS aims at being a development platform and then shall contain a compiler. However embedding emscripten in a Web browser is not an easy task as it relies, among others, on llvm project. If successful, the result could (will) be heavy.
With WASI (WebAssembly System Interface, see website), I found a simpler way to both compile and execute wasm binaries. As the interface between wasm and Javascript is specified, there is no more need of custom JS file: the WASI runtime implements the interface. Moreover there is an increasingly number of compilers that target WASI. So it offers a wider variety of programming languages for the developpers that want to create Web apps (and not only).
I took three options for building WASI binary:
- WebAssembly text format
- AssemblyScript language
- xcc C compiler
WebAssembly text format
It is the simplest option as the source code is already in WebAssembly WASI (preview 1). The tool wat2wasm (see wabt) that converts text format to binary format is built into JS/WebAssembly and then can easily be integrated into exaequOS.
Example of source code for "hello world" (hello.wat):
For building hello.wasm from hello.wat, you have to type in a terminal:
exaequos:~ $ wat2wasm hello.wat -o hello.wasm
For executing hello.wasm, you need to use 'wex' WASI runtime for exaequOS. You have to type in a terminal:
exaequos:~ $ wex hello.wasm
, and then you should get:
Hello, World!
wex is the new WASI runtime (0.1 and 0.2) of exaequOS and will be the topic of a coming blog. For editing files in exaequOS, you can choose one of the following tools: ed, vim, nano, monaco (mc) (see developer's guide). An alternative is to import files from host machine using 'exio' tool. This tool can also export files to the host machine, for saving/sharing files or for running wasm binaries with wasmtime or any other WASI runtime.
AssemblyScript
Writing assembly directly may seem to low level for some people. So I have added AssemblyScript that provides a compiler written in Typescript and already working in a Web browser. In exaequOS, 'asc' command line tool uses a default asconfig.json for generating WASI. You can extend it with the 'extends' option ("extends": asconfig-wasi.json).
Example of source code for "hello world" (hello.ts):
For building hello.wasm (WASI preview 1) from hello.ts, you have to type in a terminal:
exaequos:~ $ asc hello.ts -o hello.wasm
xcc C compiler
The third way of creating a WASI binary is to use xcc C compiler that can be built for generating WASI and that be built in WASI as well ! So that is a perfect fit for exaequOS
Example of source code for "hello world" (hello.c):
For building hello.wasm (WASI preview 1) from hello.c, you have to type in a terminal:
exaequos:~ $ wex -d /tmp -d . -d /usr/include/xcc::/usr/include -d /usr/lib/xcc::/usr/lib /usr/bin/wasm/cc hello.c -o hello.wasm