-
-
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
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") { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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:
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||
// When the wasm memory is backed by a SharedArrayBuffer, growing the memory | ||||||
// doesn't invalidate the data view by setting the byte length to 0. Instead, | ||||||
// the data view points to an old buffer after growing the memory. So we have | ||||||
|
@@ -796,8 +801,9 @@ class SwiftRuntime { | |||||
throw new Error("threadChannel is not set in options given to SwiftRuntime. Please set it to request transferring objects."); | ||||||
} | ||||||
const broker = getMessageBroker(this.options.threadChannel); | ||||||
const sendingObjects = decodeObjectRefs(sending_objects, sending_objects_count, this.getDataView()); | ||||||
const transferringObjects = decodeObjectRefs(transferring_objects, transferring_objects_count, this.getDataView()); | ||||||
const dataView = this.getDataView(); | ||||||
const sendingObjects = decodeObjectRefs(sending_objects, sending_objects_count, dataView); | ||||||
const transferringObjects = decodeObjectRefs(transferring_objects, transferring_objects_count, dataView); | ||||||
broker.request({ | ||||||
type: "request", | ||||||
data: { | ||||||
|
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") { | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Relying on
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Accessing
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback |
||||||||||||||||
// When the wasm memory is backed by a SharedArrayBuffer, growing the memory | ||||||||||||||||
// doesn't invalidate the data view by setting the byte length to 0. Instead, | ||||||||||||||||
// the data view points to an old buffer after growing the memory. So we have | ||||||||||||||||
|
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 iswasmMemory.buffer.constructor === globalThis.SharedArrayBuffer
with anObject.prototype.toString
fallback.Copilot uses AI. Check for mistakes.