Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,32 @@ export default {

When invoked, this Worker should log `Hello from JavaScript: 42` and return `Success: 42`, demonstrating the ability to invoke Wasm methods with arguments from JavaScript and vice versa.

## Use of Emscripten Modules from Javascript

WebAssembly modules compiled with Emscripten require a few considerations for use with Workers. Specifically, they should be compiled with `MODULARIZE=1`, `EXPORT_ES6=1`, `ENVIRONMENT=web`, and `DYNAMIC_EXECUTION=0` to avoid the workers' limitations with nodejs compatibility and dynamic code execution.

Then, the module can be instantiated in your Worker with:
```javascript
import { M } from './myModule.js';
import mod from './myModule.wasm';
/** @typedef {import("./myModule.js").MyModuleToplevel} MyModuleToplevel */

M({
instantiateWasm: (imports, callback) => {
const instance = new WebAssembly.Instance(mod, imports);
callback(instance);
return instance.exports;
}
}).then(instantiatedModule => {
/** @type {MyModuleToplevel} */
const myModule = instantiatedModule;
}).catch(error => {
console.error("Failed to initialize the module:", error);
});
```

Please refer to [this example](https://github.com/cloudflare/worker-emscripten-template) for details.

## Next steps

In practice, you will likely compile a language of your choice (such as Rust) to WebAssembly binaries. Many languages provide a `bindgen` to simplify the interaction between JavaScript and Wasm. These tools may integrate with your JavaScript bundler, and provide an API other than the WebAssembly API for initializing and invoking your Wasm module. As an example, refer to the [Rust `wasm-bindgen` documentation](https://rustwasm.github.io/wasm-bindgen/examples/without-a-bundler.html).
Expand Down