Repository navigation
Type WebAssembly.Memory buffers as ArrayBufferLike for shared memories - #2592
Conversation
|
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. |
|
This is definitely something I think we're going to have to run extended tests on before doing |
|
I totally agree. The main risk in my opinion is code using a plain |
|
Jake Bailey (@jakebailey) are you willing to do the test? 👀 |
|
Running on microsoft/TypeScript#65437 |
|
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).
I also tried the opposite default locally ( So I'd keep the current approach and send a DT PR for the three tests. WDYT? |
|
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. |
|
Kagami Sascha Rosylight (@saschanaz) any concerns with this? I think you said SGTM on the issue. |
| }, | ||
| // 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 |
There was a problem hiding this comment.
Please add spec URL than typescript issue URL as a context: https://webassembly.github.io/threads/js-api/index.html#dom-memory-memory
There was a problem hiding this comment.
Done, it now links to the spec.
| "constructor": { | ||
| "overrideSignatures": [ | ||
| "new(descriptor: MemoryDescriptor & { shared?: false }): Memory<ArrayBuffer>", | ||
| "new(descriptor: MemoryDescriptor): Memory<ArrayBufferLike>" |
There was a problem hiding this comment.
This sounds like if MemoryDescriptor is not inlined (e.g. new Memory(descriptor)) then it would default to ArrayBufferLike? Is that ok for compatibility?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Can you update the test in that case?
There was a problem hiding this comment.
I've added the three cases: a plain variable, a variable with shared: boolean and one typed as MemoryDescriptor
|
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? |
|
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? |
|
Jake Bailey (@jakebailey) #2592 is good to go. For #2596, I found a bug: it emitted Fixed in b210600: the old outputs now get a plain The WebTransport follow-up is independent, it can wait until after 7.1. |
|
This is probably not going to be in 7.1, was too late in the cycle to do |
|
No problem, thanks for merging it! |
Fixes #2447
As we talked about in the issue, when you create a memory with
shared: true, it gives you aSharedArrayBufferin Firefox and Chrome. But lib.dom always said it was anArrayBuffer, so TypeScript narrowedinstanceof SharedArrayBufferchecks tonever.Now,
Memoryis generic over its buffer type. Regular memories still getArrayBuffer, while shared ones getArrayBufferLike. I didn't nameSharedArrayBufferdirectly so lib.dom still compiles on ES5/ES6 targets (as required). The default isArrayBufferLike, just like with typed arrays, so existing code keeps working.toFixedLengthBuffer()andtoResizableBuffer()now follow the same type.I've added a unit test that fails without this change, and
npm testpasses.