Skip to content

Add ArrayBuffer sample methods to TurboModule examples (#56690)#56690

Open
christophpurrer wants to merge 2 commits into
facebook:mainfrom
christophpurrer:export-D95652569
Open

Add ArrayBuffer sample methods to TurboModule examples (#56690)#56690
christophpurrer wants to merge 2 commits into
facebook:mainfrom
christophpurrer:export-D95652569

Conversation

@christophpurrer
Copy link
Copy Markdown
Contributor

@christophpurrer christophpurrer commented May 5, 2026

Summary:

Changelog:

[General] [Added] - Add ArrayBuffer sample methods to TurboModule examples

react-native-community/discussions-and-proposals#947

Add getArrayBuffer method to NativeCxxModuleExample and NativeSampleTurboModule
specs to demonstrate and validate ArrayBuffer support across all platforms.

  • JS Specs: Added getArrayBuffer: (arg: ArrayBuffer) => ArrayBuffer to both
    NativeCxxModuleExample.js and NativeSampleTurboModule.js
  • C++: Implemented in NativeCxxModuleExample.cpp using OwnedMutableBuffer to
    copy and return the ArrayBuffer
  • Java/Kotlin: Implemented in SampleTurboModule.kt with ByteBuffer param/return
  • ObjC: Implemented in RCTSampleTurboModule.mm (iOS + macOS) with NSData * param/return

Reviewed By: javache

Differential Revision: D95652569

@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 May 5, 2026
@facebook-github-tools facebook-github-tools Bot added p: Facebook Partner: Facebook Partner labels May 5, 2026
@meta-codesync
Copy link
Copy Markdown

meta-codesync Bot commented May 5, 2026

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

christophpurrer added a commit to christophpurrer/react-native-macos that referenced this pull request May 5, 2026
Summary:
Pull Request resolved: facebook#56690

## Changelog:
[General] [Added] - Add ArrayBuffer sample methods to TurboModule examples

react-native-community/discussions-and-proposals#947

Add `getArrayBuffer` method to NativeCxxModuleExample and NativeSampleTurboModule
specs to demonstrate and validate ArrayBuffer support across all platforms.

- **JS Specs:** Added `getArrayBuffer: (arg: ArrayBuffer) => ArrayBuffer` to both
  NativeCxxModuleExample.js and NativeSampleTurboModule.js
- **C++:** Implemented in NativeCxxModuleExample.cpp using `OwnedMutableBuffer` to
  copy and return the ArrayBuffer
- **Java/Kotlin:** Implemented in SampleTurboModule.kt with `ByteBuffer` param/return
- **ObjC:** Implemented in RCTSampleTurboModule.mm (iOS + macOS) with `NSData *` param/return

Differential Revision: D95652569
@meta-codesync meta-codesync Bot changed the title Add ArrayBuffer sample methods to TurboModule examples Add ArrayBuffer sample methods to TurboModule examples (#56690) May 5, 2026
christophpurrer added a commit to christophpurrer/react-native-macos that referenced this pull request May 5, 2026
Summary:
Pull Request resolved: facebook#56690

## Changelog:
[General] [Added] - Add ArrayBuffer sample methods to TurboModule examples

react-native-community/discussions-and-proposals#947

Add `getArrayBuffer` method to NativeCxxModuleExample and NativeSampleTurboModule
specs to demonstrate and validate ArrayBuffer support across all platforms.

- **JS Specs:** Added `getArrayBuffer: (arg: ArrayBuffer) => ArrayBuffer` to both
  NativeCxxModuleExample.js and NativeSampleTurboModule.js
- **C++:** Implemented in NativeCxxModuleExample.cpp using `OwnedMutableBuffer` to
  copy and return the ArrayBuffer
- **Java/Kotlin:** Implemented in SampleTurboModule.kt with `ByteBuffer` param/return
- **ObjC:** Implemented in RCTSampleTurboModule.mm (iOS + macOS) with `NSData *` param/return

Differential Revision: D95652569
christophpurrer added a commit to christophpurrer/react-native-macos that referenced this pull request May 7, 2026
Summary:
Pull Request resolved: facebook#56690

## Changelog:
[General] [Added] - Add ArrayBuffer sample methods to TurboModule examples

react-native-community/discussions-and-proposals#947

Add `getArrayBuffer` method to NativeCxxModuleExample and NativeSampleTurboModule
specs to demonstrate and validate ArrayBuffer support across all platforms.

- **JS Specs:** Added `getArrayBuffer: (arg: ArrayBuffer) => ArrayBuffer` to both
  NativeCxxModuleExample.js and NativeSampleTurboModule.js
- **C++:** Implemented in NativeCxxModuleExample.cpp using `OwnedMutableBuffer` to
  copy and return the ArrayBuffer
- **Java/Kotlin:** Implemented in SampleTurboModule.kt with `ByteBuffer` param/return
- **ObjC:** Implemented in RCTSampleTurboModule.mm (iOS + macOS) with `NSData *` param/return

Reviewed By: javache

Differential Revision: D95652569
christophpurrer added a commit to christophpurrer/react-native-macos that referenced this pull request May 7, 2026
Summary:
Pull Request resolved: facebook#56690

## Changelog:
[General] [Added] - Add ArrayBuffer sample methods to TurboModule examples

react-native-community/discussions-and-proposals#947

Add `getArrayBuffer` method to NativeCxxModuleExample and NativeSampleTurboModule
specs to demonstrate and validate ArrayBuffer support across all platforms.

- **JS Specs:** Added `getArrayBuffer: (arg: ArrayBuffer) => ArrayBuffer` to both
  NativeCxxModuleExample.js and NativeSampleTurboModule.js
- **C++:** Implemented in NativeCxxModuleExample.cpp using `OwnedMutableBuffer` to
  copy and return the ArrayBuffer
- **Java/Kotlin:** Implemented in SampleTurboModule.kt with `ByteBuffer` param/return
- **ObjC:** Implemented in RCTSampleTurboModule.mm (iOS + macOS) with `NSData *` param/return

Reviewed By: javache

Differential Revision: D95652569
@christophpurrer christophpurrer force-pushed the export-D95652569 branch 2 times, most recently from 820a422 to 2226f51 Compare May 7, 2026 05:00
christophpurrer added a commit to christophpurrer/react-native-macos that referenced this pull request May 7, 2026
Summary:
Pull Request resolved: facebook#56690

## Changelog:
[General] [Added] - Add ArrayBuffer sample methods to TurboModule examples

react-native-community/discussions-and-proposals#947

Add `getArrayBuffer` method to NativeCxxModuleExample and NativeSampleTurboModule
specs to demonstrate and validate ArrayBuffer support across all platforms.

- **JS Specs:** Added `getArrayBuffer: (arg: ArrayBuffer) => ArrayBuffer` to both
  NativeCxxModuleExample.js and NativeSampleTurboModule.js
- **C++:** Implemented in NativeCxxModuleExample.cpp using `OwnedMutableBuffer` to
  copy and return the ArrayBuffer
- **Java/Kotlin:** Implemented in SampleTurboModule.kt with `ByteBuffer` param/return
- **ObjC:** Implemented in RCTSampleTurboModule.mm (iOS + macOS) with `NSData *` param/return

Reviewed By: javache

Differential Revision: D95652569
christophpurrer added a commit to christophpurrer/react-native-macos that referenced this pull request May 7, 2026
Summary:
Pull Request resolved: facebook#56690

## Changelog:
[General] [Added] - Add ArrayBuffer sample methods to TurboModule examples

react-native-community/discussions-and-proposals#947

Add `getArrayBuffer` method to NativeCxxModuleExample and NativeSampleTurboModule
specs to demonstrate and validate ArrayBuffer support across all platforms.

- **JS Specs:** Added `getArrayBuffer: (arg: ArrayBuffer) => ArrayBuffer` to both
  NativeCxxModuleExample.js and NativeSampleTurboModule.js
- **C++:** Implemented in NativeCxxModuleExample.cpp using `OwnedMutableBuffer` to
  copy and return the ArrayBuffer
- **Java/Kotlin:** Implemented in SampleTurboModule.kt with `ByteBuffer` param/return
- **ObjC:** Implemented in RCTSampleTurboModule.mm (iOS + macOS) with `NSData *` param/return

Reviewed By: javache

Differential Revision: D95652569
christophpurrer and others added 2 commits May 7, 2026 08:44
…etween JavaScript and native

Summary:
## Changelog:
[General] [Added] - Add ArrayBuffer support for React Native Turbo Modules

react-native-community/discussions-and-proposals#947

Adds first-class `ArrayBuffer` support across the entire TurboModule pipeline, enabling efficient binary data transfer between JavaScript and native code without base64 encoding overhead.

**Schema & Parser:**
- Added `NativeModuleArrayBufferTypeAnnotation` to `CodegenSchema.js` and `.d.ts`
- Added `emitArrayBuffer` to `parsers-primitives.js` type map

**C++ (Bridging & Generator):**
- Added `ArrayBufferKind` to `TurboModuleMethodValueKind` enum
- Created `ArrayBuffer.h` bridging header with `OwnedMutableBuffer` and `Bridging<std::vector<uint8_t>>`
- Added `jsi::ArrayBuffer` conversion operators to `Convert.h`
- Mapped `ArrayBufferTypeAnnotation` → `jsi::ArrayBuffer` in `GenerateModuleH.js`

**Android (Java/JNI):**
- Mapped `ArrayBufferTypeAnnotation` → `java.nio.ByteBuffer` in Java spec generator
- Mapped to `Ljava/nio/ByteBuffer;` JNI signature in JNI generator
- Added `ByteBuffer` arg/return handling in `JavaTurboModule.cpp` via `NewDirectByteBuffer`/`GetDirectBufferAddress`

**iOS (ObjC):**
- Mapped `ArrayBufferTypeAnnotation` → `NSData *` in ObjC spec generator
- Added ArrayBuffer→NSData conversion in `convertJSIValueToObjCObject()`
- Added NSData→ArrayBuffer conversion in `convertReturnIdToJSIValue()`

**Flow-Schema:**
- Added `ArrayBuffer` to `JAVASCRIPT_BUILTINS` in `BoundaryTypes.js`

**Tests:**
- Added Flow and TypeScript parser fixtures (`NATIVE_MODULE_WITH_ARRAYBUFFER`)
- Added generator schema fixture (`ARRAYBUFFER_MODULE`)
- Added `emitArrayBuffer` unit tests

Differential Revision: D95649873
Summary:
Pull Request resolved: facebook#56690

## Changelog:
[General] [Added] - Add ArrayBuffer sample methods to TurboModule examples

react-native-community/discussions-and-proposals#947

Add `getArrayBuffer` method to NativeCxxModuleExample and NativeSampleTurboModule
specs to demonstrate and validate ArrayBuffer support across all platforms.

- **JS Specs:** Added `getArrayBuffer: (arg: ArrayBuffer) => ArrayBuffer` to both
  NativeCxxModuleExample.js and NativeSampleTurboModule.js
- **C++:** Implemented in NativeCxxModuleExample.cpp using `OwnedMutableBuffer` to
  copy and return the ArrayBuffer
- **Java/Kotlin:** Implemented in SampleTurboModule.kt with `ByteBuffer` param/return
- **ObjC:** Implemented in RCTSampleTurboModule.mm (iOS + macOS) with `NSData *` param/return

Reviewed By: javache

Differential Revision: D95652569
christophpurrer added a commit to christophpurrer/react-native-macos that referenced this pull request May 11, 2026
…cebook#56740)

Summary:
Pull Request resolved: facebook#56740

Introduce a feature flag `enableSyncVoidMethods` that allows TurboModule methods with void return types to be invoked synchronously on the JS thread instead of being dispatched asynchronously.

The flag only applies to pure TurboModules (not interop modules) to avoid impacting backward compatibility with legacy Native Modules. When enabled, void methods behave like other synchronous methods, improving performance for operations that don't need async dispatch.

# Behavior mismatch
Currently:
- C++ Turbo Modules run void methods `sync`
- Java / ObjC Turbo Modules run void methods `async`

# Rationale
JavaScript code like this
```
      const buffer = new ArrayBuffer(4);
      const view = new Uint8Array(buffer);
      view[0] = 1;
      view[1] = 2;
      view[2] = 3;
      view[3] = 4;
      const result = NativeCxxModuleExample.passArrayBuffer(buffer);
      /** In native code do something like:
      * std::span<uint8_t> bytes(arg.data(rt), arg.size(rt));
      * std::reverse(bytes.begin(), bytes.end());
      **/
      // ArrayBuffer was Shared with native code and value updates are reflected to JS code
      // view[0] = 4;
      // view[1] = 3;
      // view[2] = 2;
      // view[3] = 1;
```
for a TM spec as
```
 +passArrayBuffer: (arg: ArrayBuffer) => void;
```
works for C++ Turbo Modules
see: facebook#56690

but currently fails for Java / Objc Turbo Modules as calling
```
const result = NativeCxxModuleExample.passArrayBuffer(buffer); //
```
is done `async` for Java/ObjC Modules and hence we would need to copy the `ArrayBuffer` in native code.

Changelog: [Internal]

Differential Revision: D104331837
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. fb-exported meta-exported p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant