2.7 KiB
AssemblyScript
[AssemblyScript] has included support for targeting WASI since version 0.10.0. If you're not familiar with AssemblyScript, check out the docs or the discord server. To setup this demo, you need a valid installation of NodeJS, Deno, or Bun along with a installation of wasmtime.
For the rest of this documentation, we'll default to NPM as our package manager. Feel free to use the manager of your choice.
Let's walk through a simple hello world example using the latest AssemblyScript runtime (at the time of this writing, it is AssemblyScript runtime included in version 0.27.x):
Hello World!
Enabling WASI support in AssemblyScript requires some configuration and dependencies in order to compile with WASI support.
First, we'll install assemblyscript along with wasi-shim which is a plugin that adds support for WASI.
$ npm install --save-dev assemblyscript @assemblyscript/wasi-shim
Next, we'll use the built in asinit
command to create our project files. When prompted, type y
and return.
$ npx asinit .
Next, we need to configure our project to use WASI as a build target. Navigate to asconfig.json
and add the following line.
asconfig.json
{
// "targets": { ... },
"extends": "./node_modules/@assemblyscript/wasi-shim/asconfig.json"
}
With AssemblyScript now configured to use WASI, we can enter ./assembly/index.ts
and change it to the following. This will tell WASI to print the string to the terminal.
assembly/index.ts
console.log("Hello World!");
Now, compile our WASI module using the asc
command and run it using wasmtime
.
$ npx asc assembly/index.ts --target release
$ wasmtime ./build/release.wasm
Now that we know how to use WASI, we'll test the capabilities of WASI using a demo.
WASI Demo
First, clone the wasmtime repository and navigate to the docs/assemblyscript_demo
directory.
$ git clone https://github.com/bytecodealliance/wasmtime
$ cd wasmtime/docs/assemblyscript_demo
Install our dependencies with NPM or your preferred package manager.
$ npm install
Take a look at the code in docs/assemblyscript_demo/wasi-demo.ts
and then build the WASM/WASI binary by running
$ npx asc wasi-demo.ts --target wasi-demo
Lastly, run the demo using
$ wasmtime ./build/wasi-demo.wasm