-
-
Notifications
You must be signed in to change notification settings - Fork 53
Check if the memory is backed by a SAB by checking the constructor name #381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR replaces the instanceof SharedArrayBuffer
check with a constructor-name–based detection for SharedArrayBuffer backing and improves DataView reuse in the generated runtime.
- Use
Object.getPrototypeOf(...).constructor.name
to detect SAB backing in both TS and MJS runtimes - Add detailed comments explaining why direct
SharedArrayBuffer
references aren’t used - Cache
this.getDataView()
in a local variable before passing todecodeObjectRefs
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
File | Description |
---|---|
Runtime/src/index.ts | Swapped SAB detection from instanceof to constructor-name check |
Plugins/PackageToJS/Templates/runtime.mjs | Mirrored SAB detection update and cached getDataView() for calls |
Comments suppressed due to low confidence (2)
Runtime/src/index.ts:68
- Add unit tests covering the new constructor-name detection path, including scenarios where
globalThis.SharedArrayBuffer
is undefined and where the buffer is SAB-backed in non–cross-origin isolated contexts.
// Check the constructor name of the buffer to determine if it's backed by a SharedArrayBuffer.
Plugins/PackageToJS/Templates/runtime.mjs:311
- Include tests for the generated runtime’s SAB detection logic, verifying correct behavior when SharedArrayBuffer isn’t available and when it is present.
// Check the constructor name of the buffer to determine if it's backed by a SharedArrayBuffer.
// 1. It may not be available in the global scope if the context is not cross-origin isolated. | ||
// 2. The underlying buffer may be still backed by SAB even if the context is not cross-origin | ||
// isolated (e.g. localhost on Chrome on Android). | ||
if (Object.getPrototypeOf(wasmMemory.buffer).constructor.name === "SharedArrayBuffer") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Relying on constructor.name
can break if code is minified or prototypes are altered. Consider using a direct equality check when globalThis.SharedArrayBuffer
is available (wasmMemory.buffer.constructor === globalThis.SharedArrayBuffer
) or fall back to Object.prototype.toString.call(wasmMemory.buffer) === '[object SharedArrayBuffer]'
for more robust detection.
if (Object.getPrototypeOf(wasmMemory.buffer).constructor.name === "SharedArrayBuffer") { | |
if ( | |
(globalThis.SharedArrayBuffer && wasmMemory.buffer.constructor === globalThis.SharedArrayBuffer) || | |
Object.prototype.toString.call(wasmMemory.buffer) === '[object SharedArrayBuffer]' | |
) { |
Copilot uses AI. Check for mistakes.
// 1. It may not be available in the global scope if the context is not cross-origin isolated. | ||
// 2. The underlying buffer may be still backed by SAB even if the context is not cross-origin | ||
// isolated (e.g. localhost on Chrome on Android). | ||
if (Object.getPrototypeOf(wasmMemory.buffer).constructor.name === "SharedArrayBuffer") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here: using constructor.name
may be brittle under minification or prototype mutation. A stronger check is wasmMemory.buffer.constructor === globalThis.SharedArrayBuffer
with an Object.prototype.toString
fallback.
if (Object.getPrototypeOf(wasmMemory.buffer).constructor.name === "SharedArrayBuffer") { | |
if (wasmMemory.buffer.constructor === globalThis.SharedArrayBuffer || | |
Object.prototype.toString.call(wasmMemory.buffer) === "[object SharedArrayBuffer]") { |
Copilot uses AI. Check for mistakes.
// 1. It may not be available in the global scope if the context is not cross-origin isolated. | ||
// 2. The underlying buffer may be still backed by SAB even if the context is not cross-origin | ||
// isolated (e.g. localhost on Chrome on Android). | ||
if (Object.getPrototypeOf(wasmMemory.buffer).constructor.name === "SharedArrayBuffer") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Accessing Object.getPrototypeOf(...).constructor
without checking for null/undefined can throw if buffer
has a null prototype. Add optional chaining: Object.getPrototypeOf(wasmMemory.buffer)?.constructor?.name === 'SharedArrayBuffer'
.
if (Object.getPrototypeOf(wasmMemory.buffer).constructor.name === "SharedArrayBuffer") { | |
if (Object.getPrototypeOf(wasmMemory.buffer)?.constructor?.name === "SharedArrayBuffer") { |
Copilot uses AI. Check for mistakes.
// 1. It may not be available in the global scope if the context is not cross-origin isolated. | ||
// 2. The underlying buffer may be still backed by SAB even if the context is not cross-origin | ||
// isolated (e.g. localhost on Chrome on Android). | ||
if (Object.getPrototypeOf(wasmMemory.buffer).constructor.name === "SharedArrayBuffer") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wrap prototype access in optional chaining to avoid runtime errors if the prototype is null: Object.getPrototypeOf(wasmMemory.buffer)?.constructor?.name === 'SharedArrayBuffer'
.
if (Object.getPrototypeOf(wasmMemory.buffer).constructor.name === "SharedArrayBuffer") { | |
if (Object.getPrototypeOf(wasmMemory.buffer)?.constructor?.name === "SharedArrayBuffer") { |
Copilot uses AI. Check for mistakes.
No description provided.