Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Merged
merged 2 commits into from
Jul 3, 2025

Conversation

kateinoigakukun
Copy link
Member

No description provided.

@kateinoigakukun kateinoigakukun requested a review from Copilot July 3, 2025 10:15
Copy link

@Copilot Copilot AI left a 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 to decodeObjectRefs

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") {
Copy link
Preview

Copilot AI Jul 3, 2025

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.

Suggested change
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") {
Copy link
Preview

Copilot AI Jul 3, 2025

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.

Suggested change
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") {
Copy link
Preview

Copilot AI Jul 3, 2025

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'.

Suggested change
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") {
Copy link
Preview

Copilot AI Jul 3, 2025

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'.

Suggested change
if (Object.getPrototypeOf(wasmMemory.buffer).constructor.name === "SharedArrayBuffer") {
if (Object.getPrototypeOf(wasmMemory.buffer)?.constructor?.name === "SharedArrayBuffer") {

Copilot uses AI. Check for mistakes.

@kateinoigakukun kateinoigakukun merged commit e854719 into main Jul 3, 2025
6 checks passed
@kateinoigakukun kateinoigakukun deleted the yt/fix-sab-check-on-android branch July 3, 2025 10:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants