Skip to content

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

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

Martin Ferret (MartinFerret) wants to merge 1 commit 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

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