oracle / graaljs

GraalJS – A high-performance, ECMAScript compliant, and embeddable JavaScript runtime for Java
https://www.graalvm.org/javascript/
Universal Permissive License v1.0
1.82k stars 191 forks source link

[GR-53477] Implement WebAssembly/ES Module Integration #850

Open fniephaus opened 2 months ago

fniephaus commented 2 months ago

TL;DR

We plan to implement WebAssembly/ES Module Integration in GraalJS. This allows WebAssembly modules to be imported similarly to ES modules to improve the ergonomics of WebAssembly module instantiation.

Details

Currently, there is an imperative JS API for instantiating WebAssembly modules. This requires users to manually fetch a module file, wire up imports, and run WebAssembly.instantiate or WebAssembly.instantiateStreaming.

A declarative API would improve the ergonomics by making this work happen implicitly.

import { foo } from "./myModule.wasm";
foo();

Then by integrating with Source Phase Imports, arbitrary instantiations with custom imports can still be supported.

import source myModule from "./myModule.wasm";

const { foo: foo1 } = new WebAssembly.Instance(myModule, { ...imports1 });
foo1();

const { foo: foo2 } = new WebAssembly.Instance(myModule, { ...imports2 });
foo2();

For dynamically loaded modules, dynamic import() integration is also supported for both phases.

const { foo: foo1 } = await import("./myModule.wasm");

const myModule = await import.source("./myModule.wasm");
const { foo: foo2 } = await WebAssembly.instantiate(myModule, { ...imports });