Skip to content

ArrayBuffer support to Java TurboModules - #57062

Closed
paradowstack wants to merge 5 commits into
react:mainfrom
paradowstack:feat/array-buffers-java
Closed

paradowstack wants to merge 5 commits into
react:mainfrom
paradowstack:feat/array-buffers-java

Conversation

@paradowstack

@paradowstack paradowstack commented Jun 3, 2026

Copy link
Copy Markdown

Summary

Adds ArrayBuffer support to Java/Kotlin TurboModules, following the C++ ArrayBuffer PR (#56729) and the ObjC ArrayBuffer PR (#56986).

  • Codegen support for ArrayBufferTypeAnnotation in Java module specs (java.nio.ByteBuffer params/returns, ArrayBufferKind in JNI host functions.
  • JMutableDataBufferjsi::MutableBuffer backed by a direct ByteBuffer for zero-copy ArrayBuffer return to JS (with ThreadScope in the destructor for Hermes GC finalization)
  • JZeroCopyByteBufferHolder — retains a native jsi::MutableBuffer while JNI passes a zero-copy ByteBuffer view into Java
  • Three-path JSI↔Java conversion in JavaTurboModule: native-backed zero-copy (holder + view), sync JS-backed borrow (JByteBuffer::wrapBytes), async copy (allocateDirect + memcpy)
  • Sync vs async strategy matches ObjC/C++: isSync when return type is not void / Promise (VoidKind / PromiseKind copy JS-heap inputs; sync methods may borrow for the duration of the call)
  • CxxCallbackImpl fast path for a single direct ByteBuffer on Promise.resolve / Callback.invoke (bypasses folly::dynamic); CxxCallbackRejectImpl for reject-only callbacks

This PR touches some Objective-C++ parts only to get the project building. As soon as Objective-C++ work lands, this PR will be rebased.

Changelog:

[ANDROID] [ADDED] - Add ArrayBuffer support to Java TurboModules

Test plan

  • Codegen: Jest snapshot tests updated for array_buffer_native_module (GenerateModuleJavaSpec, GenerateModuleJniCpp, GenerateModuleJniH)
  • rn-tester (Android): getArrayBuffer, createNativeBuffer, processAsyncBuffer, getAsyncBuffer on NativeSampleTurboModule (SampleTurboModuleExample.js)
  • C++ bridging behavior unchanged; covered by existing BridgingTest.cpp ArrayBuffer / AsyncArrayBuffer tests from the C++ PR

@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 Jun 3, 2026
@facebook-github-tools facebook-github-tools Bot added p: Callstack Partner: Callstack Partner labels Jun 3, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Jun 3, 2026
@meta-codesync

meta-codesync Bot commented Jun 3, 2026

Copy link
Copy Markdown

@christophpurrer has imported this pull request. If you are a Meta employee, you can view this in D107411163.

meta-codesync Bot pushed a commit that referenced this pull request Aug 6, 2026
Summary:
Changelog:
[ANDROID] [ADDED] - Add ArrayBuffer support to Java TurboModules

- Codegen: ArrayBufferTypeAnnotation → ByteBuffer / ArrayBufferKind / Ljava/nio/ByteBuffer; in GenerateModuleJavaSpec and GenerateModuleJniCpp. Guard in Utils.js rejects Promise<ArrayBuffer> on Android at codegen time — drops async return to avoid CxxCallbackImpl.kt / JCallback / PromiseImpl.kt changes.

- Runtime: New JByteBufferMutableBuffer.h — jsi::MutableBuffer wrapping global_ref<JByteBuffer> direct ByteBuffer, holds Java object alive for JS ArrayBuffer lifetime; dtor uses ThreadScope to attach JNI thread (Hermes GC finalizes off-thread). Zero-copy native→JS return mirrors iOS NSMutableDataBuffer. Args JS→Java always copied into Java-owned direct ByteBuffer via allocateDirect+memcpy (mirrors iOS NSData copy), no sync/async branching — safe to retain/dispatch past GC.

- No changes to CxxCallbackImpl.kt, JCallback, PromiseImpl.kt. Nested ArrayBuffers still via folly::dynamic, deferred like ObjC/C++.

- Samples: NativeSampleTurboModule.js spec, SampleTurboModule.kt (getArrayBuffer/ createNativeBuffer/ processAsyncBuffer), RCTSampleTurboModule.mm. (NSData param / NSMutableData return), RNTester UI SampleTurboModuleExample.js.

X-link: #57062

Reviewed By: javache

Differential Revision: D107411163

Pulled By: christophpurrer

fbshipit-source-id: 67cc5da796a87f6918f0382debc5c5594f76661b
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. p: Callstack Partner: Callstack Partner Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant