Add ArrayBuffer and Uint8Array support to C++ TurboModules#56001
Open
hsjoberg wants to merge 6 commits intofacebook:mainfrom
Open
Add ArrayBuffer and Uint8Array support to C++ TurboModules#56001hsjoberg wants to merge 6 commits intofacebook:mainfrom
hsjoberg wants to merge 6 commits intofacebook:mainfrom
Conversation
144b1ca to
bff2e89
Compare
Currently only usable for C++ TurboModules.
bff2e89 to
7f25de4
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Hi. This pull request adds support for
ArrayBufferandUint8Arrayto C++ TurboModules.This is useful for native modules that need to pass raw byte data back and forth, where the alternative would otherwise be base64 encoding or other suboptimal solutions.
ArrayBuffersupport uses the existing JSI type directly, so C++ module methods can accept and returnjsi::ArrayBuffer.Uint8Arraysupport adds a new C++ helper class,facebook::react::Uint8Array, together with bridging logic:JS -> native: borrowed zero-copy over the JS-backed bytesnative -> JS: copy from owned native bytes when usingstd::vector<uint8_t>, or zero-copy when backed byjsi::MutableBufferNote: zero-copy
JS -> nativedoes not useMutableBuffer, as Hermes API changes such as facebook/hermes#1733 would need to land first. Lib authors are expected to copy data if they need the bytes to outlive the synchronous call or use them off-thread.If reviewers prefer, this can instead be changed to use
memcpyin the bridging path.This PR intentionally only focuses on C++ TurboModules, as I think this is a good starting point. It also keeps the scope lower.
Java/Kotlin and ObjC support can be followed up later.
Changelog:
[GENERAL] [ADDED] - Add ArrayBuffer and Uint8Array support to C++ TurboModulesTest Plan:
I added and tested
ArrayBufferandUint8Arrayin RNTester viaNativeCxxModuleExample.There are parser/codegen tests for the new reserved types.
I have written C++ unit tests, but I can't find targets for C++ tests in React Native OSS.
I also tested
Uint8Arraysupport in my own lib.Usage examples