Transforming Modules
Note: The final code for this example can be found on GitHub.
Irrespective of whether your JavaScript code runs on the client or the server, the statement shown below to transform a WASI module will be always needed until browsers land BigInt
support in WebAssembly.
Setup Instructions
Please repeat the step-by-step instructions given in the Hello World example, but with the following changes:
Call your project
wasmer-js-transforming-wasi
Download the Wasm module
clocktimeget.wasm
and store it in thestatic
directory
JavaScript Coding
The coding seen below is very similar to the coding used for the previous Hello World example — but with one very important difference!
Inside function startWasiTask
, we fetch the Wasm file contents and convert it to a Uint8Array
as before, but then there is the additional line:
The call to function lowerI64Imports
performs the all-important transformation that allows a JavaScript BigInt
to be transferred to a WebAssembly i64
.
Now that the interface has been transformed, we can instantiate the WebAssembly module and invoke it as before.
On the both the browser screen and the JavaScript console, you should see the text Done!
.
Known Limitation
This example is somewhat contrived because the WebAssembly module has been hard-coded to return the text string Done!
rather than the time value from clock_time_get
.
Anything written to standard out should be a printable string followed by a carriage return character, not the raw i32
value returned from clock_time_get
. Therefore, before being able to return the actual clock time, this WebAssembly module would additionally need to convert the raw i32
value to a printable string before then writing it to standard out.
Next, let's look at handling input and output via WASI.
If you want to run the examples from the docs codebase directly, you can also do:
Last updated