Skip to content

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

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

f52985 wants to merge 1 commit 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
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
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.

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