Hello World
Note: The final code for this example can be found on GitHub.
In this introductory example, we will develop a Browser-based application that uses the following call chain.
JavaScript --> WebAssembly --> Native "OS" function
In this case, we will invoke the a simple WASI module that does nothing more than writing hello world to standard out.
Under the Hood
The Wasm module calls the native "OS" function fd_write that writes data to a particular file descriptor (hence fd in the function name)
However, interaction with file descriptors such as "standard in" and "standard out" is not normally possible for a WebAssembly module, since this type of functionality belongs to the underlying "OS". Therefore, we must make use of the following two packages:
Package Name
Description
@wasmer/wasi
A set of JavaScript polyfills to bridge the gap between the black-box world of a WebAssembly module and functionality available in the host environment
@wasmer/wasmfs
Provide access to a sand-boxed filesystem with which @wasmer/wasi can interact
Reminder
The term "OS" used above is in quotes because in this particular case, the native function called by our WebAssembly module that writes to standard out, belongs to the JavaScript runtime, and not the actual underlying operating system.
This example will be bundled and served by Parcel and run in the browser.
Setup Instructions
Prerequisites
Make sure Parcel has been installed and is available from the command line
Mac usersBefore the installation of Parcel will work on a Mac, you must first install the Xcode Command Line Tools
Step-By-Step Guide
Change into some development directory
Create and then change into a new project directory, then run
npm initAfter answering all the questions from
npm init, you will have a configuredpackage.jsonfile.For the purposes of testing, we need to install both the
parcel-bundlerandparcel-plugin-static-files-copypackages.These packages allow
parcelto serve our Wasm files as static assets:This command both installs the required packages and updates the
devDependenciessection of yourpackage.jsonfile.Create a bare-bones
index.htmlfile that contains nothing more than the request to load the JavaScript fileindex.js:Create the file
index.jsand add the following single line of code:Let's test that the basic file structure of our project is correct:
Point your browser to
http://localhost:1234and you should see a blank page.Open your browser's Developer Tools and look at the JavaScript console. Here, you should see
"I am working", which means everything is working!Now that the basic file structure of our project has been set up correctly, we must next declare the use of packages
@wasmer/wasiand@wasmer/wasmfs.To install these packages as runtime dependencies to our project, run the following command:
Create a new directory called
staticDownload the WebAssembly module
helloworld.wasmand store it in this directoryNow we need to change the contents of
index.jsto implement the required functionality.Code SampleSeeing as this is demo code, it uses meaningful variable names and contains additional explanatory comments — features that are often sadly missing from production code...
Please take some time to read and understand these comments as they explain how the functionality has been constructed.
Also, please read the comment explaining the use of
@wasmer/wasm-transformer; we will cover this very important detail in a later example.As long as
parcelis still running, after savingindex.js, your browser should automatically refresh and you should seeStandard Output: Hello World!appear both on the browser screen and in the JavaScript console.
Next, let's take a look at transforming WASI modules that require transformations.
Last updated
Was this helpful?