-
-
Notifications
You must be signed in to change notification settings - Fork 57
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -308,7 +308,12 @@ class SwiftRuntime { | |||||
| // Cache the DataView as it's not a cheap operation | ||||||
| let cachedDataView = new DataView(wasmMemory.buffer); | ||||||
| let cachedUint8Array = new Uint8Array(wasmMemory.buffer); | ||||||
| if (typeof SharedArrayBuffer !== "undefined" && wasmMemory.buffer instanceof SharedArrayBuffer) { | ||||||
| // Check the constructor name of the buffer to determine if it's backed by a SharedArrayBuffer. | ||||||
| // We can't reference SharedArrayBuffer directly here because: | ||||||
| // 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") { | ||||||
|
||||||
| if (Object.getPrototypeOf(wasmMemory.buffer).constructor.name === "SharedArrayBuffer") { | |
| if (Object.getPrototypeOf(wasmMemory.buffer)?.constructor?.name === "SharedArrayBuffer") { |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -64,7 +64,13 @@ export class SwiftRuntime { | |||||||||||||||
| // Cache the DataView as it's not a cheap operation | ||||||||||||||||
| let cachedDataView = new DataView(wasmMemory.buffer); | ||||||||||||||||
| let cachedUint8Array = new Uint8Array(wasmMemory.buffer); | ||||||||||||||||
| if (typeof SharedArrayBuffer !== "undefined" && wasmMemory.buffer instanceof SharedArrayBuffer) { | ||||||||||||||||
|
|
||||||||||||||||
| // Check the constructor name of the buffer to determine if it's backed by a SharedArrayBuffer. | ||||||||||||||||
| // We can't reference SharedArrayBuffer directly here because: | ||||||||||||||||
| // 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") { | ||||||||||||||||
|
||||||||||||||||
| 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
AI
Jul 3, 2025
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") { |
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.namemay be brittle under minification or prototype mutation. A stronger check iswasmMemory.buffer.constructor === globalThis.SharedArrayBufferwith anObject.prototype.toStringfallback.