Skip to content

Type WebAssembly.Memory buffers as ArrayBufferLike for shared memories - #2592

Open
Martin Ferret (MartinFerret) wants to merge 5 commits into
microsoft:mainfrom
MartinFerret:fix-2447-wasm-memory-shared-buffer
Open

Martin Ferret (MartinFerret) wants to merge 5 commits into
microsoft:mainfrom
MartinFerret:fix-2447-wasm-memory-shared-buffer

Conversation

@MartinFerret

Copy link
Copy Markdown
Contributor

Fixes #2447

As we talked about in the issue, when you create a memory with shared: true, it gives you a SharedArrayBuffer in Firefox and Chrome. But lib.dom always said it was an ArrayBuffer, so TypeScript narrowed instanceof SharedArrayBuffer checks to never.

Now, Memory is generic over its buffer type. Regular memories still get ArrayBuffer, while shared ones get ArrayBufferLike. I didn't name SharedArrayBuffer directly so lib.dom still compiles on ES5/ES6 targets (as required). The default is ArrayBufferLike, just like with typed arrays, so existing code keeps working. toFixedLengthBuffer() and toResizableBuffer() now follow the same type.

I've added a unit test that fails without this change, and npm test passes.

@github-actions

github-actions Bot commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Thanks for the PR!

This section of the codebase is owned by Kagami Sascha Rosylight (@saschanaz) - if they write a comment saying "LGTM" then it will be merged.

@jakebailey

Copy link
Copy Markdown
Member

This is definitely something I think we're going to have to run extended tests on before doing

@MartinFerret

Copy link
Copy Markdown
Contributor Author

I totally agree. The main risk in my opinion is code using a plain WebAssembly.Memory's buffer as an ArrayBuffer. If you need me to adjust, I will

@saschanaz

Copy link
Copy Markdown
Contributor

Jake Bailey (@jakebailey) are you willing to do the test? 👀

@jakebailey

Copy link
Copy Markdown
Member

Running on microsoft/TypeScript#65437

@MartinFerret

Copy link
Copy Markdown
Contributor Author

Thanks for running the extended tests Jake Bailey (@jakebailey) ! I looked at all four breaks (sha512-wasm fails the same way as the other two DT packages).

  • blake2b-wasm, sha256-wasm, sha512-wasm: their tests check $ExpectType Memory. Once Memory is generic, TypeScript prints the type argument even when it's the default, so this fails whatever the default is. It needs a small DT update, e.g. $ExpectType Memory || Memory<ArrayBufferLike>, like blake2b already does for Uint8Array.
  • terax-ai: it annotates WebAssembly.Memory and passes memory.buffer to a function that takes an ArrayBuffer, so the ArrayBufferLike default breaks it.

I also tried the opposite default locally (ArrayBuffer, with only shared: true returning Memory<ArrayBufferLike>). It fixes terax-ai, but a shared memory then stops being a WebAssembly.Memory or an ImportValue, so passing it as an import (the main use case for shared memory) no longer compiles. That felt worse than the terax break, which is a one-line fix on their side (WebAssembly.Memory<ArrayBuffer>).

So I'd keep the current approach and send a DT PR for the three tests. WDYT?

@MartinFerret

Copy link
Copy Markdown
Contributor Author

Opened DefinitelyTyped/DefinitelyTyped#76141 for the three tests. It passes with the current TypeScript and with this PR's lib, so it can land first.

Comment thread inputfiles/overridingTypes.jsonc Outdated
"name": "Memory",
"typeParameters": [
{
"name": "TBuffer",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd actually use TArrayBuffer here, matching interface ArrayBufferView and the rest of the code.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good call, renamed it to TArrayBuffer everywhere. Thanks!

@jakebailey

Copy link
Copy Markdown
Member

Kagami Sascha Rosylight (@saschanaz) any concerns with this? I think you said SGTM on the issue.

Comment thread inputfiles/overridingTypes.jsonc Outdated
},
// Shared memories (threads proposal) return SharedArrayBuffer in Firefox and Chrome,
// expressed as ArrayBufferLike to stay pre-ES2017 friendly
// https://github.com/microsoft/TypeScript-DOM-lib-generator/issues/2447

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please add spec URL than typescript issue URL as a context: https://webassembly.github.io/threads/js-api/index.html#dom-memory-memory

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Done, it now links to the spec.

"constructor": {
"overrideSignatures": [
"new(descriptor: MemoryDescriptor & { shared?: false }): Memory<ArrayBuffer>",
"new(descriptor: MemoryDescriptor): Memory<ArrayBufferLike>"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This sounds like if MemoryDescriptor is not inlined (e.g. new Memory(descriptor)) then it would default to ArrayBufferLike? Is that ok for compatibility?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Only when the descriptor is typed as MemoryDescriptor, or has shared: boolean. A plain variable without shared still gets Memory<ArrayBuffer>, I checked it against the generated lib. I think the fallback is right: the memory may be shared, and it matches the typed array default.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you update the test in that case?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've added the three cases: a plain variable, a variable with shared: boolean and one typed as MemoryDescriptor

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.

[Web API type definition issue] Invalid WebAssembly.Memory.buffer type (ArrayBuffer -> ArrayBufferLike)

3 participants