Repository navigation
Type WebAssembly.Memory buffers as ArrayBufferLike for shared memories - #2592
Martin Ferret (MartinFerret) wants to merge 5 commits into
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. |
| "name": "Memory", | ||
| "typeParameters": [ | ||
| { | ||
| "name": "TBuffer", |
There was a problem hiding this comment.
I'd actually use TArrayBuffer here, matching interface ArrayBufferView and the rest of the code.
There was a problem hiding this comment.
Good call, renamed it to TArrayBuffer everywhere. Thanks!
|
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
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.