Skip to content

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

Merged
Jake Bailey (jakebailey) merged 5 commits into
microsoft:mainfrom
MartinFerret:fix-2447-wasm-memory-shared-buffer
Nov 30, 2026
Merged

Jake Bailey (jakebailey) merged 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.

@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

@MartinFerret

Copy link
Copy Markdown
Contributor Author

DefinitelyTyped/DefinitelyTyped#76141 is merged, so the blake2b-wasm, sha256-wasm and sha512-wasm tests now accept the generic Memory. The three non-inlined descriptor cases are in the test too. Is there anything else you'd like me to change?

@jakebailey

Copy link
Copy Markdown
Member

We are trying to cut for 7.1. If this is good, I'd like to get it and #2596 in before then. Are there any remaining concerns?

Are there followups missing that build on top of these two that will mean that merging just those two is a bad idea?

@MartinFerret

Copy link
Copy Markdown
Contributor Author

Checking one thing on #2596 before it lands, please hold it for a bit. I'll follow up shortly.
#2592 is good to go from my side.

@MartinFerret

Martin Ferret (MartinFerret) commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor Author

Jake Bailey (@jakebailey) #2592 is good to go.

For #2596, I found a bug: it emitted Uint8Array<ArrayBuffer> in the ts5.5/ts5.6 outputs, where Uint8Array isn't generic, so those versions failed to compile.

Fixed in b210600: the old outputs now get a plain Uint8Array. I checked them with TS 5.5.4 and 5.6.3, and npm test passes.

The WebTransport follow-up is independent, it can wait until after 7.1.

@jakebailey
Jake Bailey (jakebailey) merged commit f95b644 into microsoft:main Nov 30, 2026
6 checks passed
@jakebailey

Copy link
Copy Markdown
Member

This is probably not going to be in 7.1, was too late in the cycle to do

@MartinFerret

Copy link
Copy Markdown
Contributor Author

No problem, thanks for merging it!

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