Skip to content

Use NSData for the JS->ObjC ArrayBuffer argument path#57596

Open
christophpurrer wants to merge 1 commit into
react:mainfrom
christophpurrer:export-D112582384
Open

Use NSData for the JS->ObjC ArrayBuffer argument path#57596
christophpurrer wants to merge 1 commit into
react:mainfrom
christophpurrer:export-D112582384

Conversation

@christophpurrer

Copy link
Copy Markdown
Contributor

Summary:
The original implementation used NSMutableData for both directions of the
JSI<->ObjC ArrayBuffer conversion. That is the wrong contract on the argument
(JS -> ObjC/Native) side: an inbound buffer is owned by the caller, not the
native module, and NSMutableData must own a resizable backing store it can
realloc/free, so it cannot durably alias a foreign buffer.

This diff keeps NSMutableData for the ObjC/Native -> JS (return) path, but
switches the JS -> ObjC/Native (argument) path to the immutable NSData:

  • Codegen: getParamObjCType now maps ArrayBufferTypeAnnotation params to
    NSData * (return type stays NSMutableData *).
  • Runtime: convertJSIArrayBufferToNSMutableData is renamed to
    convertJSIArrayBufferToNSData and returns an immutable NSData. The bytes
    are still eagerly copied, which keeps the result safe to retain, store, or
    dispatch to another thread regardless of whether the source bytes were owned
    by JS or by a native MutableBuffer.
  • Updated the ObjC unit tests and the codegen GenerateModuleHObjCpp snapshot.

Changelog: [INTERNAL]

Differential Revision: D112582384

Summary:
The original implementation used `NSMutableData` for both directions of the
JSI<->ObjC ArrayBuffer conversion. That is the wrong contract on the argument
(JS -> ObjC/Native) side: an inbound buffer is owned by the caller, not the
native module, and `NSMutableData` must own a resizable backing store it can
`realloc`/`free`, so it cannot durably alias a foreign buffer.

This diff keeps `NSMutableData` for the ObjC/Native -> JS (return) path, but
switches the JS -> ObjC/Native (argument) path to the immutable `NSData`:

- Codegen: `getParamObjCType` now maps `ArrayBufferTypeAnnotation` params to
  `NSData *` (return type stays `NSMutableData *`).
- Runtime: `convertJSIArrayBufferToNSMutableData` is renamed to
  `convertJSIArrayBufferToNSData` and returns an immutable `NSData`. The bytes
  are still eagerly copied, which keeps the result safe to retain, store, or
  dispatch to another thread regardless of whether the source bytes were owned
  by JS or by a native `MutableBuffer`.
- Updated the ObjC unit tests and the codegen `GenerateModuleHObjCpp` snapshot.

Changelog: [INTERNAL]

Differential Revision: D112582384
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 17, 2026
@meta-codesync

meta-codesync Bot commented Jul 17, 2026

Copy link
Copy Markdown

@christophpurrer has exported this pull request. If you are a Meta employee, you can view the originating Diff in D112582384.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. meta-exported p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant