Skip to content

Commit e854719

Browse files
Merge pull request #381 from swiftwasm/yt/fix-sab-check-on-android
Check if the memory is backed by a SAB by checking the constructor name
2 parents 387347a + 1993735 commit e854719

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

Plugins/PackageToJS/Templates/runtime.mjs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,12 @@ class SwiftRuntime {
308308
// Cache the DataView as it's not a cheap operation
309309
let cachedDataView = new DataView(wasmMemory.buffer);
310310
let cachedUint8Array = new Uint8Array(wasmMemory.buffer);
311-
if (typeof SharedArrayBuffer !== "undefined" && wasmMemory.buffer instanceof SharedArrayBuffer) {
311+
// Check the constructor name of the buffer to determine if it's backed by a SharedArrayBuffer.
312+
// We can't reference SharedArrayBuffer directly here because:
313+
// 1. It may not be available in the global scope if the context is not cross-origin isolated.
314+
// 2. The underlying buffer may be still backed by SAB even if the context is not cross-origin
315+
// isolated (e.g. localhost on Chrome on Android).
316+
if (Object.getPrototypeOf(wasmMemory.buffer).constructor.name === "SharedArrayBuffer") {
312317
// When the wasm memory is backed by a SharedArrayBuffer, growing the memory
313318
// doesn't invalidate the data view by setting the byte length to 0. Instead,
314319
// the data view points to an old buffer after growing the memory. So we have
@@ -796,8 +801,9 @@ class SwiftRuntime {
796801
throw new Error("threadChannel is not set in options given to SwiftRuntime. Please set it to request transferring objects.");
797802
}
798803
const broker = getMessageBroker(this.options.threadChannel);
799-
const sendingObjects = decodeObjectRefs(sending_objects, sending_objects_count, this.getDataView());
800-
const transferringObjects = decodeObjectRefs(transferring_objects, transferring_objects_count, this.getDataView());
804+
const dataView = this.getDataView();
805+
const sendingObjects = decodeObjectRefs(sending_objects, sending_objects_count, dataView);
806+
const transferringObjects = decodeObjectRefs(transferring_objects, transferring_objects_count, dataView);
801807
broker.request({
802808
type: "request",
803809
data: {

Runtime/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,13 @@ export class SwiftRuntime {
6464
// Cache the DataView as it's not a cheap operation
6565
let cachedDataView = new DataView(wasmMemory.buffer);
6666
let cachedUint8Array = new Uint8Array(wasmMemory.buffer);
67-
if (typeof SharedArrayBuffer !== "undefined" && wasmMemory.buffer instanceof SharedArrayBuffer) {
67+
68+
// Check the constructor name of the buffer to determine if it's backed by a SharedArrayBuffer.
69+
// We can't reference SharedArrayBuffer directly here because:
70+
// 1. It may not be available in the global scope if the context is not cross-origin isolated.
71+
// 2. The underlying buffer may be still backed by SAB even if the context is not cross-origin
72+
// isolated (e.g. localhost on Chrome on Android).
73+
if (Object.getPrototypeOf(wasmMemory.buffer).constructor.name === "SharedArrayBuffer") {
6874
// When the wasm memory is backed by a SharedArrayBuffer, growing the memory
6975
// doesn't invalidate the data view by setting the byte length to 0. Instead,
7076
// the data view points to an old buffer after growing the memory. So we have

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy