Skip to content

[js-api] Add the missing realm argument - #2261

Open
f52985 wants to merge 2 commits into
WebAssembly:mainfrom
kaist-plrg:fix/js-api-new-object-missing-realm
Open

f52985 wants to merge 2 commits into
WebAssembly:mainfrom
kaist-plrg:fix/js-api-new-object-missing-realm

Conversation

@f52985

@f52985 f52985 commented Oct 2, 2026

Copy link
Copy Markdown
Collaborator

The WebIDL algorithms, create a new promise and create a new object implementing the interface, require a realm as their argument:

To create a new Promise<T> in a realm |realm|, perform the following steps: ...

To create a new object implementing the interface |interface|, with a realm |realm|, perform the following steps: ...

but the specification was lacking it.

This PR supplies the missing realm argument, using the [=current Realm=] notation.


I'm not entirely sure what the exact phrasing should be, though:
there are other call sites of these two algorithms within the WebIDL document itself, and they're not entirely consistent.

Sometimes it uses a simple phrase like a new Environment Object "in" realm,
and sometimes a more verbose one like a new DOMException "created in" the current realm
— and neither quite matches the declaration itself, which uses the preposition with.

For promises, it even elides the realm entirely and just uses a new promise in a few places (which seems like the same bug of WebIDL document).

For now I went with the simplest form, but happy to match whichever phrasing.

@Ms2ger Ms2ger left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this. Unfortunately you stepped into one of the… less pleasant corners of the web ecosystem.

  • I don't have the brain cycles right now to confirm that the current realm is the right choice in principle (lots of history in whatwg/webidl#135).
  • Please check the web-api spec as well.

Comment thread document/js-api/index.bs Outdated
1. [=Instantiate the core of a WebAssembly module=] |module| with |imports|, and let |instance| be the result.
If this throws an exception, catch it, [=reject=] |promise| with the exception, and terminate these substeps.
1. Let |instanceObject| be a [=/new=] {{Instance}}.
1. Let |instanceObject| be a [=/new=] {{Instance}} in the [=current Realm=].

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't have a current Realm when running the steps in a queued task, I'm afraid. It's only available when you're being called directly from JS. You'll need to capture the realm at the top of this algorithm (assuming it exists there - check the callers) and use the captured variable within the task.

Comment thread document/js-api/index.bs Outdated
1. If |map|[|memaddr|] [=map/exists=],
1. Return |map|[|memaddr|].
1. Let |memory| be a [=/new=] {{Memory}}.
1. Let |memory| be a [=/new=] {{Memory}} in the [=current Realm=].

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One of the call stacks for this algorithm "create an exports object" → "initialize an instance object" → the bit of "asynchronously instantiate a WebAssembly module" that doesn't have access to the current Realm. So you'll need to pass the realm through explicitly from there.

@f52985
f52985 force-pushed the fix/js-api-new-object-missing-realm branch from 7838c3a to 8c1c4a8 Compare November 25, 2026 06:47
@f52985

f52985 commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

Thanks for the review, and sorry for the delay: it took me a while to dive deep and understand this.. less pleasant corner of the web.

  • Capturing the realm before queueing tasks. asynchronously compile a WebAssembly module and asynchronously instantiate a WebAssembly module now capture the current Realm at their top. I believe that the current Realm indeed exists at that point: their callers are either method steps called from JS, or steps run from react which runs them from built-in functions created in the current Realm of the time react was called.

  • Passing the realm through create an exports object. asynchronously instantiate a WebAssembly module now passes the realm it captured (and for the Instance constructor, the current Realm) through initialize an instance object and create an exports object to the algorithms that create the exported objects, which now take a realm. Their other callers are synchronous and pass the current Realm.

  • Checking the Web API. AFAIK, the only object created there is the promise in compile a potential WebAssembly response, which runs synchronously from compileStreaming/instantiateStreaming, so it now uses the current Realm.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants