Skip to content

[GR-63376] Implement new Wasm Polyglot API. #11558

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open

Conversation

graalvmbot
Copy link
Collaborator

@graalvmbot graalvmbot commented Jul 4, 2025

This PR introduces a breaking change to the implementation of the Polyglot API in GraalWasm!

Rationale
The current implementation of the Polyglot API in GraalWasm is fairly limited, in that it does not allow users to provide custom imports to WebAssembly modules. While there is a workaround via the JS API, I think there should be a way to provide custom imports via the Polyglot API as well.
Furthermore, the current implementation has some compatibility issues with the JS API. Currently, JS has to provide a hidden flag (string value) when evaluating a WebAssembly source to get a Wasm module (symbolic representation) instead of a Wasm instance (runtime representation). In addition, the layout of Wasm instances produced by the Polyglot API is structurally different from Wasm instances provided via the JS API.

Implementation
To address this issue, the following changes are made to the Polyglot API in GraalWasm.

  • Calling Context.eval on a WebAssembly source no longer returns a Wasm Instance, but a Wasm Module. This is necessary to provide custom imports when instantiating the module into an instance. In case the old behavior is needed, the new experimental option wasm.EvalReturnsInstance can be set to true for the time being.
  • Users can now call newInstance on the modules returned by Context.eval to instantiate the module into a Wasm instance. newInstance supports a single optional import object parameter. The import object has the format:
    { "module-1-name": { "member-1-name": func | value, "member-2-name": func | val, ... }, ... }.
  • A Wasm instance no longer provides direct access to its exported members, but instead introduces an indirection via the "exports" member. This is identical to the JS API. Members can now be accessed via instance.getMember("exports").getMember("member-name").

@oracle-contributor-agreement oracle-contributor-agreement bot added the OCA Verified All contributors have signed the Oracle Contributor Agreement. label Jul 4, 2025
@woess woess self-assigned this Jul 4, 2025
@graalvmbot graalvmbot force-pushed the fh/GR-63376 branch 3 times, most recently from b44a688 to 0c2862c Compare July 9, 2025 21:10
…ests accordingly. Added exports, references, and linkReferences to instance.
woess added 16 commits July 10, 2025 01:50
Fix detectEntryPoint.
Fix reading exports in WasmMemoryLeakSuite.
Simplify linkReferences.
Pass non-null import object to moduleInstantiate.
Remove unnecessary try-catch(NumberFormatException).
Instantiate newInstance() receiver module first, so that WASI will import its memory.
Allow all module arguments to resolve imports from the importObject.
Add test with WASI, an import object, and another module instantiated at once.
Get store from WasmInstance instead.
Link WASIp1 "memory" import to the importing module (rather than the first instance in the store).
… arguments.

Use newInstance(importObject) with the exports of the required modules passed via the importObject instead.
Extract common sources to static fields.
@graalvmbot graalvmbot force-pushed the fh/GR-63376 branch 4 times, most recently from 0f785a9 to 43f81a9 Compare July 10, 2025 12:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
OCA Verified All contributors have signed the Oracle Contributor Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants