From 639cdedb506d6441e27eeb579225aa6a7def3ff9 Mon Sep 17 00:00:00 2001 From: Fabrizio Cucci Date: Mon, 21 Sep 2026 12:15:49 +0100 Subject: [PATCH] Revert "RCTArrayBuffer zero-copy class for ObjC TM (#57879)" This reverts commit 11f9a7f4491eb1b01955298851d5a87a3bb311cc. # Conflicts: # packages/react-native/ReactCommon/react/nativemodule/core/iostests/RCTTurboModuleTests.mm # packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.mm --- .../GenerateModuleObjCpp-test.js.snap | 2 - .../modules/GenerateModuleObjCpp/index.js | 1 - .../GenerateModuleObjCpp/serializeMethod.js | 4 +- .../GenerateModuleHObjCpp-test.js.snap | 19 +- .../react-native/React/Base/RCTArrayBuffer.h | 64 ----- .../react-native/React/Base/RCTArrayBuffer.mm | 127 --------- .../core/iostests/RCTTurboModuleTests.mm | 80 ++---- .../ios/ReactCommon/RCTInteropTurboModule.h | 3 +- .../ios/ReactCommon/RCTInteropTurboModule.mm | 3 +- .../platform/ios/ReactCommon/RCTTurboModule.h | 11 +- .../ios/ReactCommon/RCTTurboModule.mm | 103 +++---- .../ios/ReactCommon/RCTSampleTurboModule.mm | 27 +- .../RCTTurboModuleArrayBufferTests.mm | 266 +++++------------- .../api-snapshots/ReactAppleDebugCxx.api | 22 +- .../api-snapshots/ReactAppleNewarchCxx.api | 22 +- .../api-snapshots/ReactAppleReleaseCxx.api | 22 +- 16 files changed, 150 insertions(+), 626 deletions(-) delete mode 100644 packages/react-native/React/Base/RCTArrayBuffer.h delete mode 100644 packages/react-native/React/Base/RCTArrayBuffer.mm diff --git a/packages/react-native-codegen/e2e/deep_imports/__tests__/modules/__snapshots__/GenerateModuleObjCpp-test.js.snap b/packages/react-native-codegen/e2e/deep_imports/__tests__/modules/__snapshots__/GenerateModuleObjCpp-test.js.snap index e0bd0c195c0..9bf2025a3a4 100644 --- a/packages/react-native-codegen/e2e/deep_imports/__tests__/modules/__snapshots__/GenerateModuleObjCpp-test.js.snap +++ b/packages/react-native-codegen/e2e/deep_imports/__tests__/modules/__snapshots__/GenerateModuleObjCpp-test.js.snap @@ -26,7 +26,6 @@ exports[`GenerateModuleObjCpp can generate a header file NativeModule specs 1`] #import #import #import -#import #import #import #import @@ -1579,7 +1578,6 @@ exports[`GenerateModuleObjCpp can generate a header file NativeModule specs with #import #import #import -#import #import #import #import diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/index.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/index.js index 1bf4e32b327..8d93d9ad29c 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/index.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/index.js @@ -97,7 +97,6 @@ const HeaderFileTemplate = ({ #import #import #import -#import #import #import #import diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js index 397516dc5c2..6811646078e 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/serializeMethod.js @@ -227,7 +227,7 @@ function getParamObjCType( return notStruct(wrapOptional('NSArray *', !nullable)); } case 'ArrayBufferTypeAnnotation': { - return notStruct(wrapOptional('RCTArrayBuffer *', !nullable)); + return notStruct(wrapOptional('NSData *', !nullable)); } } @@ -398,7 +398,7 @@ function getReturnObjCType( case 'GenericObjectTypeAnnotation': return wrapOptional('NSDictionary *', isRequired); case 'ArrayBufferTypeAnnotation': - return wrapOptional('RCTArrayBuffer *', isRequired); + return wrapOptional('NSMutableData *', isRequired); default: typeAnnotation.type as 'MixedTypeAnnotation'; throw new Error( diff --git a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleHObjCpp-test.js.snap b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleHObjCpp-test.js.snap index b1ce7953d5f..8ed200aa17d 100644 --- a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleHObjCpp-test.js.snap +++ b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleHObjCpp-test.js.snap @@ -27,7 +27,6 @@ Map { #import #import #import -#import #import #import #import @@ -93,7 +92,6 @@ Map { #import #import #import -#import #import #import #import @@ -104,9 +102,9 @@ Map { @protocol NativeSampleTurboModuleSpec -- (RCTArrayBuffer *)getArrayBuffer; -- (void)voidArrayBuffer:(RCTArrayBuffer *)arg; -- (void)voidNullableArrayBuffer:(RCTArrayBuffer * _Nullable)arg; +- (NSMutableData *)getArrayBuffer; +- (void)voidArrayBuffer:(NSData *)arg; +- (void)voidNullableArrayBuffer:(NSData * _Nullable)arg; @end @@ -161,7 +159,6 @@ Map { #import #import #import -#import #import #import #import @@ -203,7 +200,6 @@ Map { #import #import #import -#import #import #import #import @@ -479,7 +475,6 @@ Map { #import #import #import -#import #import #import #import @@ -521,7 +516,6 @@ Map { #import #import #import -#import #import #import #import @@ -587,7 +581,6 @@ Map { #import #import #import -#import #import #import #import @@ -661,7 +654,6 @@ Map { #import #import #import -#import #import #import #import @@ -847,7 +839,6 @@ Map { #import #import #import -#import #import #import #import @@ -1116,7 +1107,6 @@ Map { #import #import #import -#import #import #import #import @@ -1246,7 +1236,6 @@ Map { #import #import #import -#import #import #import #import @@ -1312,7 +1301,6 @@ Map { #import #import #import -#import #import #import #import @@ -1404,7 +1392,6 @@ Map { #import #import #import -#import #import #import #import diff --git a/packages/react-native/React/Base/RCTArrayBuffer.h b/packages/react-native/React/Base/RCTArrayBuffer.h deleted file mode 100644 index c8d80878819..00000000000 --- a/packages/react-native/React/Base/RCTArrayBuffer.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import - -NS_ASSUME_NONNULL_BEGIN - -/** - * A fixed-length byte buffer shared between JS `ArrayBuffer`s and ObjC TurboModules. Fixed - * length lets the backing store alias JS memory; `NSMutableData` cannot do that. - * - * `isOwningBytes`: - * - `YES` — safe to retain and use from any thread (synchronize if aliasing JS - * memory). - * - `NO` — valid only during the synchronous call on the calling thread; copy with - * `arrayBufferWithCopiedBytes:length:` to keep the bytes. - */ -@interface RCTArrayBuffer : NSObject - -/** - * NULL when `length` is 0, non-NULL otherwise, matching `NSData.bytes`. - */ -@property (nonatomic, readonly, nullable) void *mutableBytes NS_RETURNS_INNER_POINTER; - -@property (nonatomic, readonly) NSUInteger length; - -/** Whether the buffer owns its bytes. See the class comment. */ -@property (nonatomic, readonly, getter=isOwningBytes) BOOL owningBytes; - -/** - * A new zero-filled owning buffer. - */ -+ (instancetype)arrayBufferWithLength:(NSUInteger)length; - -/** - * A new owning buffer holding a copy of `bytes`. Passing NULL zero-fills. - */ -+ (instancetype)arrayBufferWithCopiedBytes:(const void *_Nullable)bytes length:(NSUInteger)length; - -/** - * An owning buffer aliasing `bytes` without copying. If `cleanup` is non-nil, it runs on dealloc, - * like `-[NSData dataWithBytesNoCopy:length:freeWhenDone:YES]`; if nil, the caller must keep - * `bytes` valid until dealloc. NULL `bytes` with non-zero `length` raises NSInvalidArgumentException. - */ -+ (instancetype)arrayBufferWithOwnedBytes:(nullable void *)bytes - length:(NSUInteger)length - cleanup:(nullable void (^)(void))cleanup; - -/** - * A non-owning, zero-copy alias of `bytes`. See the class comment for lifetime rules. NULL `bytes` - * with non-zero `length` raises NSInvalidArgumentException. - */ -+ (instancetype)arrayBufferWithUnownedBytes:(nullable void *)bytes length:(NSUInteger)length; - -- (instancetype)init NS_UNAVAILABLE; -+ (instancetype)new NS_UNAVAILABLE; - -@end - -NS_ASSUME_NONNULL_END diff --git a/packages/react-native/React/Base/RCTArrayBuffer.mm b/packages/react-native/React/Base/RCTArrayBuffer.mm deleted file mode 100644 index c4270b927be..00000000000 --- a/packages/react-native/React/Base/RCTArrayBuffer.mm +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -#import "RCTArrayBuffer.h" - -#include -#include -#include -#include - -@interface RCTArrayBuffer () - -- (instancetype)initWithBytesNoCopy:(nullable void *)bytes - length:(NSUInteger)length - owningBytes:(BOOL)owningBytes - cleanup:(nullable void (^)(void))cleanup NS_DESIGNATED_INITIALIZER; -- (instancetype)initWithCopiedBytes:(const void *_Nullable)bytes length:(NSUInteger)length; - -@end - -@implementation RCTArrayBuffer { - void *_bytes; - NSUInteger _length; - BOOL _owningBytes; - void (^_cleanup)(void); - std::vector _copiedBytes; -} - -#pragma mark - Initializers - -- (instancetype)initWithBytesNoCopy:(void *)bytes - length:(NSUInteger)length - owningBytes:(BOOL)owningBytes - cleanup:(void (^)(void))cleanup -{ - if (bytes == NULL && length != 0) { - [NSException raise:NSInvalidArgumentException - format:@"RCTArrayBuffer: NULL bytes with length %lu", (unsigned long)length]; - } - - if (self = [super init]) { - _bytes = bytes; - _length = length; - _owningBytes = owningBytes; - _cleanup = [cleanup copy]; - } - return self; -} - -- (instancetype)initWithCopiedBytes:(const void *)bytes length:(NSUInteger)length -{ - if (length == 0) { - return [self initWithBytesNoCopy:NULL length:0 owningBytes:YES cleanup:nil]; - } - - std::vector copy(length); - if (bytes != NULL) { - std::memcpy(copy.data(), bytes, length); - } - - // Moving a vector hands over its heap buffer, so `data()` stays valid in `_copiedBytes`. - if (self = [self initWithBytesNoCopy:copy.data() length:length owningBytes:YES cleanup:nil]) { - _copiedBytes = std::move(copy); - } - return self; -} - -+ (instancetype)arrayBufferWithLength:(NSUInteger)length -{ - return [[self alloc] initWithCopiedBytes:NULL length:length]; -} - -+ (instancetype)arrayBufferWithCopiedBytes:(const void *)bytes length:(NSUInteger)length -{ - return [[self alloc] initWithCopiedBytes:bytes length:length]; -} - -+ (instancetype)arrayBufferWithOwnedBytes:(void *)bytes - length:(NSUInteger)length - cleanup:(nullable void (^)(void))cleanup -{ - return [[self alloc] initWithBytesNoCopy:bytes length:length owningBytes:YES cleanup:cleanup]; -} - -+ (instancetype)arrayBufferWithUnownedBytes:(void *)bytes length:(NSUInteger)length -{ - return [[self alloc] initWithBytesNoCopy:bytes length:length owningBytes:NO cleanup:nil]; -} - -#pragma mark - Accessors - -- (void *)mutableBytes -{ - return _bytes; -} - -- (NSUInteger)length -{ - return _length; -} - -- (BOOL)isOwningBytes -{ - return _owningBytes; -} - -- (NSString *)description -{ - return [NSString stringWithFormat:@"<%@: %p; length = %lu; owningBytes = %@>", - NSStringFromClass([self class]), - self, - (unsigned long)_length, - _owningBytes ? @"YES" : @"NO"]; -} - -- (void)dealloc -{ - if (_cleanup != nil) { - _cleanup(); - } -} - -@end diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/iostests/RCTTurboModuleTests.mm b/packages/react-native/ReactCommon/react/nativemodule/core/iostests/RCTTurboModuleTests.mm index 693cddd7d39..0c6705ffcfc 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/iostests/RCTTurboModuleTests.mm +++ b/packages/react-native/ReactCommon/react/nativemodule/core/iostests/RCTTurboModuleTests.mm @@ -7,10 +7,8 @@ #import -#import #import #import -#import #import #import @@ -94,39 +92,6 @@ class TestMutableBuffer : public facebook::jsi::MutableBuffer { std::vector bytes_; }; -// `jsi::Runtime::tryGetMutableBuffer` is optional — the Hermes branch linked into apps returns -// nullptr for every ArrayBuffer — so decorate the runtime to answer it for the buffers created -// through it. That keeps the coverage of the native-backed path independent of the engine. -// The registry is weak, so the only owners of a backing store remain the JS ArrayBuffer and -// whatever the conversion retains. -class MutableBufferAwareRuntime final : public facebook::jsi::RuntimeDecorator { - public: - explicit MutableBufferAwareRuntime(facebook::jsi::Runtime &plain) : RuntimeDecorator(plain) {} - - facebook::jsi::ArrayBuffer createArrayBuffer(std::shared_ptr buffer) override - { - auto arrayBuffer = RuntimeDecorator::createArrayBuffer(buffer); - buffers_.push_back(buffer); - return arrayBuffer; - } - - std::shared_ptr tryGetMutableBuffer( - const facebook::jsi::ArrayBuffer &arrayBuffer) override - { - uint8_t *data = arrayBuffer.data(*this); - for (const auto &weakBuffer : buffers_) { - auto buffer = weakBuffer.lock(); - if (buffer != nullptr && buffer->data() == data) { - return buffer; - } - } - return nullptr; - } - - private: - std::vector> buffers_; -}; - class StubNativeMethodCallInvoker : public NativeMethodCallInvoker { public: void invokeAsync(const std::string &methodName, NativeMethodCallFunc &&func) noexcept override @@ -354,46 +319,33 @@ class ExceptionCapturingNativeMethodCallInvoker : public NativeMethodCallInvoker XCTAssertNotNil(original.callStackSymbols); } -// A native-backed ArrayBuffer is aliased rather than copied, and the RCTArrayBuffer retains -// the backing MutableBuffer, so the alias outlives the JS object. -- (void)testNativeBackedArrayBufferIsAliasedAndKeepsBackingStoreAlive +// A JS ArrayBuffer converts (on the argument path) to an immutable NSData that +// owns an independent copy of the bytes, so the result stays valid after the +// source buffer is gone. This covers the ArrayBuffer-backed-by-native-MutableBuffer +// case, which is the one that could in principle have been aliased zero-copy. +- (void)testArrayBufferConvertsToIndependentNSData { constexpr size_t kBufferSize = 64 * 1024; auto hermesRuntime = facebook::hermes::makeHermesRuntime(); - MutableBufferAwareRuntime runtime(*hermesRuntime); + facebook::jsi::Runtime *rt = hermesRuntime.get(); auto buffer = std::make_shared(kBufferSize); *buffer->data() = 0xAB; - const uint8_t *sourceBytes = buffer->data(); - RCTArrayBuffer *converted = nil; - { - facebook::jsi::ArrayBuffer arrayBuffer(runtime, buffer); - const long ownersBeforeConversion = buffer.use_count(); + facebook::jsi::ArrayBuffer arrayBuffer(*rt, buffer); + id converted = + TurboModuleConvertUtils::convertJSIValueToObjCObject(*rt, facebook::jsi::Value(*rt, arrayBuffer), nullptr); - id result = TurboModuleConvertUtils::convertJSIValueToObjCObject( - runtime, facebook::jsi::Value(runtime, arrayBuffer), nullptr, NO, NO); - XCTAssertTrue([result isKindOfClass:[RCTArrayBuffer class]]); - converted = (RCTArrayBuffer *)result; + XCTAssertTrue([converted isKindOfClass:[NSData class]]); + NSData *data = (NSData *)converted; + XCTAssertEqual(data.length, (NSUInteger)kBufferSize); + XCTAssertEqual(*static_cast(data.bytes), 0xAB); - XCTAssertEqual( - buffer.use_count(), ownersBeforeConversion + 1, @"The RCTArrayBuffer must retain the backing MutableBuffer"); - } - - XCTAssertTrue(converted.isOwningBytes, @"A native-backed buffer must be safe to retain"); - XCTAssertEqual(converted.length, (NSUInteger)kBufferSize); - XCTAssertEqual(converted.mutableBytes, (void *)sourceBytes, @"Bytes must be aliased, not copied"); - - // Writes through the source are visible, and vice versa: one shared allocation. + // Independent copy: mutating the source MutableBuffer must not write through to + // the NSData. *buffer->data() = 0xCD; - XCTAssertEqual(*static_cast(converted.mutableBytes), 0xCD); - *static_cast(converted.mutableBytes) = 0xEF; - XCTAssertEqual(*buffer->data(), 0xEF); - - // Dropping the caller's reference leaves the bytes valid. - buffer.reset(); - XCTAssertEqual(*static_cast(converted.mutableBytes), 0xEF); + XCTAssertEqual(*static_cast(data.bytes), 0xAB, @"NSData must not alias the source buffer"); } @end diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTInteropTurboModule.h b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTInteropTurboModule.h index 62a82b7e61f..f170c5e0e6a 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTInteropTurboModule.h +++ b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTInteropTurboModule.h @@ -75,8 +75,7 @@ class JSI_EXPORT ObjCInteropTurboModule : public ObjCTurboModule { const jsi::Value &arg, size_t i, NSInvocation *inv, - NSMutableArray *retainedObjectsForInvocation, - bool mustCopyBytes) override; + NSMutableArray *retainedObjectsForInvocation) override; private: std::vector methodDescriptors_; diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTInteropTurboModule.mm b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTInteropTurboModule.mm index c18326ae45d..b4c11eafaa5 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTInteropTurboModule.mm +++ b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTInteropTurboModule.mm @@ -363,8 +363,7 @@ void ObjCInteropTurboModule::setInvocationArg( const jsi::Value &jsiArg, size_t index, NSInvocation *inv, - NSMutableArray *retainedObjectsForInvocation, - [[maybe_unused]] bool mustCopyBytes) + NSMutableArray *retainedObjectsForInvocation) { NSString *methodName = @(methodNameCStr); std::string methodJsSignature = name_ + "." + methodNameCStr + "()"; diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.h b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.h index fcd3231c383..d3fc87e6f14 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.h +++ b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.h @@ -35,8 +35,7 @@ id convertJSIValueToObjCObject( jsi::Runtime &runtime, const jsi::Value &value, const std::shared_ptr &jsInvoker, - BOOL useNSNull = NO, - BOOL mustCopyBytes = YES); + BOOL useNSNull = NO); } // namespace TurboModuleConvertUtils template <> @@ -114,9 +113,6 @@ class JSI_EXPORT ObjCTurboModule : public TurboModule { * ObjCInteropTurboModule relies heavily on RCTConvert to convert arguments from JavaScript values to Objective C * values. ObjCTurboModule tries to minimize reliance on RCTConvert: RCTConvert uses the RCT_EXPORT_METHOD macros, * which we want to remove long term from React Native. - * - * mustCopyBytes says whether the invocation may outlive the JS call, in which case ArrayBuffer arguments must be - * copied rather than aliased. */ virtual void setInvocationArg( jsi::Runtime &runtime, @@ -125,8 +121,7 @@ class JSI_EXPORT ObjCTurboModule : public TurboModule { const jsi::Value &arg, size_t i, NSInvocation *inv, - NSMutableArray *retainedObjectsForInvocation, - bool mustCopyBytes); + NSMutableArray *retainedObjectsForInvocation); private: // Does the NativeModule dispatch async methods to the JS thread? @@ -142,13 +137,11 @@ class JSI_EXPORT ObjCTurboModule : public TurboModule { NSDictionary *> *methodArgumentTypeNames_; bool isMethodSync(TurboModuleMethodValueKind returnType); - bool mustCopyJSHeapArrayBufferBytes(TurboModuleMethodValueKind returnType); BOOL hasMethodArgConversionSelector(NSString *methodName, size_t argIndex); SEL getMethodArgConversionSelector(NSString *methodName, size_t argIndex); NSInvocation *createMethodInvocation( jsi::Runtime &runtime, bool isSync, - bool mustCopyBytes, const char *methodName, SEL selector, const jsi::Value *args, diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.mm b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.mm index b8b5f2db3cd..9f7877c5f8a 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.mm +++ b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModule.mm @@ -7,7 +7,6 @@ #import "RCTTurboModule.h" -#import #import #import #import @@ -90,43 +89,35 @@ static std::vector convertNSArrayToStdVector(jsi::Runtime &runtime, return result; } -static jsi::ArrayBuffer convertRCTArrayBufferToJSIArrayBuffer(jsi::Runtime &runtime, RCTArrayBuffer *value) +static jsi::ArrayBuffer convertNSMutableDataToJSIArrayBuffer(jsi::Runtime &runtime, NSMutableData *value) { - class RCTArrayBufferMutableBuffer final : public jsi::MutableBuffer { + class NSMutableDataBuffer final : public jsi::MutableBuffer { public: - explicit RCTArrayBufferMutableBuffer(RCTArrayBuffer *buffer) - : buffer_(buffer), data_(static_cast(buffer.mutableBytes)), size_(buffer.length) - { - } + explicit NSMutableDataBuffer(NSMutableData *data) : data_(data) {} size_t size() const override { - return size_; + return data_.length; } uint8_t *data() override { - return data_; + return static_cast(data_.mutableBytes); } private: - // Never read: retains the buffer so `data_` stays valid for the ArrayBuffer's lifetime. - [[maybe_unused]] RCTArrayBuffer *buffer_; - uint8_t *data_; - size_t size_; + NSMutableData *data_; }; - // nil would yield a NULL pointer via ObjC nil-messaging and corrupt the ArrayBuffer. + // A nil NSMutableData would silently yield a NULL `mutableBytes` pointer + // (ObjC nil-messaging) and corrupt the resulting ArrayBuffer. Substitute a + // fresh empty NSMutableData so the wrapped pointer is always well-defined. if (value == nil) { - RCTLogWarn(@"convertRCTArrayBufferToJSIArrayBuffer: received nil RCTArrayBuffer; returning empty ArrayBuffer"); - value = [RCTArrayBuffer arrayBufferWithLength:0]; + RCTLogWarn(@"convertNSMutableDataToJSIArrayBuffer: received nil NSMutableData; returning empty ArrayBuffer"); + value = [NSMutableData data]; } - - if (!value.isOwningBytes) { - value = [RCTArrayBuffer arrayBufferWithCopiedBytes:value.mutableBytes length:value.length]; - } - - return {runtime, std::make_shared(value)}; + auto buffer = std::make_shared(value); + return {runtime, std::move(buffer)}; } jsi::Value convertObjCObjectToJSIValue(jsi::Runtime &runtime, id value) @@ -142,8 +133,8 @@ jsi::Value convertObjCObjectToJSIValue(jsi::Runtime &runtime, id value) return convertNSDictionaryToJSIObject(runtime, (NSDictionary *)value); } else if ([value isKindOfClass:[NSArray class]]) { return convertNSArrayToJSIArray(runtime, (NSArray *)value); - } else if ([value isKindOfClass:[RCTArrayBuffer class]]) { - return convertRCTArrayBufferToJSIArrayBuffer(runtime, (RCTArrayBuffer *)value); + } else if ([value isKindOfClass:[NSMutableData class]]) { + return convertNSMutableDataToJSIArrayBuffer(runtime, (NSMutableData *)value); } else if (value == (id)kCFNull) { return jsi::Value::null(); } @@ -210,41 +201,22 @@ convertJSIFunctionToCallback(jsi::Runtime &rt, jsi::Function &&function, const s }; } -// Native-backed buffers are aliased and keep their backing store alive, so they stay valid for -// as long as the module holds them. JS-heap buffers have nothing to retain — their bytes are -// freed when the ArrayBuffer is collected or detached — so `mustCopyBytes` is set whenever the -// invocation may outlive the JS call, and they are aliased only when it cannot. Engines that -// don't hand out the backing MutableBuffer fall back to those JS-heap rules for every buffer. -static RCTArrayBuffer * -convertJSIArrayBufferToRCTArrayBuffer(jsi::Runtime &rt, const jsi::ArrayBuffer &arrayBuffer, BOOL mustCopyBytes) +// Copy the ArrayBuffer's bytes into an immutable NSData. An inbound buffer is +// owned by the caller, not the native module, so NSData (not NSMutableData) is +// the correct read-only contract. Copying makes the NSData self-contained and +// safe to retain in a block, store, or dispatch to another thread, regardless of +// whether the bytes were owned by JS (valid only for this callstack) or by a +// native MutableBuffer (which the JS ArrayBuffer may GC concurrently). +static NSData *convertJSIArrayBufferToNSData(jsi::Runtime &rt, const jsi::ArrayBuffer &value) { - if (auto nativeBuffer = arrayBuffer.tryGetMutableBuffer(rt)) { - auto keepAlive = std::move(nativeBuffer); - return [RCTArrayBuffer arrayBufferWithOwnedBytes:keepAlive->data() - length:keepAlive->size() - cleanup:^{ - (void)keepAlive; - }]; - } - - void *bytes = arrayBuffer.data(rt); - size_t size = arrayBuffer.size(rt); - - // The bytes belong to the JS heap -> async call. - if (mustCopyBytes) { - return [RCTArrayBuffer arrayBufferWithCopiedBytes:bytes length:size]; - } - - // The bytes belong to the JS heap -> sync call. - return [RCTArrayBuffer arrayBufferWithUnownedBytes:bytes length:size]; + return [NSData dataWithBytes:value.data(rt) length:value.size(rt)]; } id convertJSIValueToObjCObject( jsi::Runtime &runtime, const jsi::Value &value, const std::shared_ptr &jsInvoker, - BOOL useNSNull, - BOOL mustCopyBytes) + BOOL useNSNull) { if (value.isUndefined() || (value.isNull() && !useNSNull)) { return nil; @@ -270,7 +242,7 @@ id convertJSIValueToObjCObject( return convertJSIFunctionToCallback(runtime, o.getFunction(runtime), jsInvoker); } if (o.isArrayBuffer(runtime)) { - return convertJSIArrayBufferToRCTArrayBuffer(runtime, o.getArrayBuffer(runtime), mustCopyBytes); + return convertJSIArrayBufferToNSData(runtime, o.getArrayBuffer(runtime)); } return convertJSIObjectToNSDictionary(runtime, o, jsInvoker, useNSNull); } @@ -629,11 +601,11 @@ jsi::Value ObjCTurboModule::convertReturnIdToJSIValue( break; } case ArrayBufferKind: { - if (result != nil && ![result isKindOfClass:[RCTArrayBuffer class]]) { - RCTLogError(@"convertReturnIdToJSIValue: expected RCTArrayBuffer for ArrayBufferKind, got %@", [result class]); + if (result != nil && ![result isKindOfClass:[NSMutableData class]]) { + RCTLogError(@"convertReturnIdToJSIValue: expected NSMutableData for ArrayBufferKind, got %@", [result class]); break; } - returnValue = convertRCTArrayBufferToJSIArrayBuffer(runtime, (RCTArrayBuffer *)result); + returnValue = convertNSMutableDataToJSIArrayBuffer(runtime, (NSMutableData *)result); break; } case FunctionKind: @@ -710,8 +682,7 @@ void ObjCTurboModule::setInvocationArg( const jsi::Value &arg, size_t i, NSInvocation *inv, - NSMutableArray *retainedObjectsForInvocation, - bool mustCopyBytes) + NSMutableArray *retainedObjectsForInvocation) { if (arg.isBool()) { bool v = arg.getBool(); @@ -754,15 +725,13 @@ void ObjCTurboModule::setInvocationArg( * Convert arg to ObjC objects. */ BOOL enableModuleArgumentNSNullConversionIOS = ReactNativeFeatureFlags::enableModuleArgumentNSNullConversionIOS(); - id objCArg = - convertJSIValueToObjCObject(runtime, arg, jsInvoker_, enableModuleArgumentNSNullConversionIOS, mustCopyBytes); + id objCArg = convertJSIValueToObjCObject(runtime, arg, jsInvoker_, enableModuleArgumentNSNullConversionIOS); // A JS `null` in argument position must reach ObjC as `nil`; only nulls nested inside arrays and // dictionaries are preserved as `kCFNull`. Skipping `setArgument:` leaves the slot zeroed. if (enableModuleArgumentNSNullConversionIOS && objCArg == (id)kCFNull) { return; } - if (objCArg != nullptr) { NSString *methodNameNSString = @(methodName); @@ -823,7 +792,6 @@ void ObjCTurboModule::setInvocationArg( NSInvocation *ObjCTurboModule::createMethodInvocation( jsi::Runtime &runtime, bool isSync, - bool mustCopyBytes, const char *methodName, SEL selector, const jsi::Value *args, @@ -852,7 +820,7 @@ NSInvocation *ObjCTurboModule::createMethodInvocation( for (size_t i = 0; i < count; i++) { const jsi::Value &arg = args[i]; const std::string objCArgType = [methodSignature getArgumentTypeAtIndex:i + 2]; - setInvocationArg(runtime, methodName, objCArgType, arg, i, inv, retainedObjectsForInvocation, mustCopyBytes); + setInvocationArg(runtime, methodName, objCArgType, arg, i, inv, retainedObjectsForInvocation); } if (isSync) { @@ -873,12 +841,6 @@ bool ObjCTurboModule::isMethodSync(TurboModuleMethodValueKind returnType) return returnType != VoidKind && returnType != PromiseKind; } -bool ObjCTurboModule::mustCopyJSHeapArrayBufferBytes(TurboModuleMethodValueKind returnType) -{ - // Void always dispatches via invokeAsync and promise may. - return returnType == VoidKind || returnType == PromiseKind; -} - ObjCTurboModule::ObjCTurboModule(const InitParams ¶ms) : TurboModule(params.moduleName, params.jsInvoker), instance_(params.instance), @@ -899,7 +861,6 @@ jsi::Value ObjCTurboModule::invokeObjCMethod( const char *methodName = methodNameStr.c_str(); bool isSyncInvocation = isMethodSync(returnType); - bool mustCopyBytes = mustCopyJSHeapArrayBufferBytes(returnType); if (isSyncInvocation) { TurboModulePerfLogger::syncMethodCallStart(moduleName, methodName); @@ -909,7 +870,7 @@ jsi::Value ObjCTurboModule::invokeObjCMethod( NSMutableArray *retainedObjectsForInvocation = [NSMutableArray arrayWithCapacity:count + 2]; NSInvocation *inv = createMethodInvocation( - runtime, isSyncInvocation, mustCopyBytes, methodName, selector, args, count, retainedObjectsForInvocation); + runtime, isSyncInvocation, methodName, selector, args, count, retainedObjectsForInvocation); jsi::Value returnValue = jsi::Value::undefined(); diff --git a/packages/react-native/ReactCommon/react/nativemodule/samples/platform/ios/ReactCommon/RCTSampleTurboModule.mm b/packages/react-native/ReactCommon/react/nativemodule/samples/platform/ios/ReactCommon/RCTSampleTurboModule.mm index 6ec73228f76..e2b9d4bf42c 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/samples/platform/ios/ReactCommon/RCTSampleTurboModule.mm +++ b/packages/react-native/ReactCommon/react/nativemodule/samples/platform/ios/ReactCommon/RCTSampleTurboModule.mm @@ -6,8 +6,6 @@ */ #import "RCTSampleTurboModule.h" - -#import #import "RCTSampleTurboModulePlugin.h" #import @@ -16,8 +14,6 @@ #import #import -#include - using namespace facebook::react; @interface RCTSampleTurboModule () @@ -149,27 +145,20 @@ RCT_EXPORT_MODULE() }; } -// The argument aliases the JS ArrayBuffer's bytes, so mutating in place is visible to JS. -- (RCTArrayBuffer *)getArrayBuffer:(RCTArrayBuffer *)buffer +// Arguments arrive as an immutable NSData, but an ArrayBuffer return must be +// NSMutableData: it is handed to JS as a jsi::MutableBuffer, whose data() is +// non-const. Echoing the argument back therefore needs a mutable copy. +- (NSMutableData *)getArrayBuffer:(NSData *)buffer { - auto *bytes = static_cast(buffer.mutableBytes); - if (bytes == nullptr) { - return buffer; - } - - std::span byteSpan(bytes, static_cast(buffer.length)); - for (auto &byte : byteSpan) { - byte = static_cast(byte * 2); - } - return buffer; + return [buffer mutableCopy]; } -- (RCTArrayBuffer *)createNativeBuffer:(double)size +- (NSMutableData *)createNativeBuffer:(double)size { - return [RCTArrayBuffer arrayBufferWithLength:(NSUInteger)size]; + return [NSMutableData dataWithLength:(NSUInteger)size]; } -- (void)processAsyncBuffer:(RCTArrayBuffer *)payload +- (void)processAsyncBuffer:(NSData *)payload resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject { diff --git a/packages/rn-tester/RNTesterUnitTests/RCTTurboModuleArrayBufferTests.mm b/packages/rn-tester/RNTesterUnitTests/RCTTurboModuleArrayBufferTests.mm index 14f32b48e84..156af187618 100644 --- a/packages/rn-tester/RNTesterUnitTests/RCTTurboModuleArrayBufferTests.mm +++ b/packages/rn-tester/RNTesterUnitTests/RCTTurboModuleArrayBufferTests.mm @@ -7,7 +7,6 @@ #import -#import #import #import #import @@ -30,14 +29,14 @@ std::shared_ptr createHermesRuntime(bool enableMicrotask return facebook::hermes::makeHermesRuntime(::hermes::vm::RuntimeConfig::Builder().withMicrotaskQueue(true).build()); } -RCTArrayBuffer *createIntegerSequenceBuffer(NSUInteger size) +NSMutableData *createIntegerSequenceData(NSUInteger size) { - RCTArrayBuffer *buffer = [RCTArrayBuffer arrayBufferWithLength:size]; - auto *bytes = static_cast(buffer.mutableBytes); + NSMutableData *data = [NSMutableData dataWithLength:size]; + auto *bytes = static_cast(data.mutableBytes); for (NSUInteger i = 0; i < size; ++i) { bytes[i] = static_cast(i); } - return buffer; + return data; } std::vector bytesFromData(NSData *data) @@ -99,8 +98,6 @@ class QueueingNativeMethodCallInvoker final : public NativeMethodCallInvoker { @interface RCTTestArrayBufferTurboModule : NSObject @property (nonatomic, copy) NSData *lastReceivedPayload; -@property (nonatomic, assign) BOOL sawAliasedBytes; -@property (nonatomic, assign) BOOL sawUnownedBytes; @end @@ -108,54 +105,36 @@ class QueueingNativeMethodCallInvoker final : public NativeMethodCallInvoker { RCT_EXPORT_MODULE() -- (RCTArrayBuffer *)testMethodWhichTransformsArrayBuffer:(RCTArrayBuffer *)buffer +RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSMutableData *, testMethodWhichTransformsArrayBuffer : (NSData *)buffer) { - RCTArrayBuffer *result = [RCTArrayBuffer arrayBufferWithLength:buffer.length]; - auto *destinationBytes = static_cast(result.mutableBytes); - for (NSUInteger i = 0; i < buffer.length; ++i) { - destinationBytes[i] = static_cast((i + 1) * 10); + NSMutableData *result = [buffer mutableCopy]; + auto *bytes = static_cast(result.mutableBytes); + for (NSUInteger i = 0; i < result.length; ++i) { + bytes[i] = static_cast((i + 1) * 10); } return result; } -- (RCTArrayBuffer *)testMethodWhichReturnsItsArgument:(RCTArrayBuffer *)buffer +RCT_EXPORT_METHOD(testMethodWhichStoresArrayBuffer : (NSData *)payload) { - auto *bytes = static_cast(buffer.mutableBytes); - for (NSUInteger i = 0; i < buffer.length && i < 3; ++i) { - bytes[i] = static_cast(99 - i); - } - return buffer; + self.lastReceivedPayload = [payload copy]; } -- (NSNumber *)testMethodWhichChecksArrayBufferAliasing:(RCTArrayBuffer *)buffer +RCT_EXPORT_METHOD(testMethodWhichStoresNestedArrayBuffer : (NSDictionary *)payload) { - // An observable in-place write proves the bytes were aliased, not copied on the way in. - auto *bytes = static_cast(buffer.mutableBytes); - bytes[0] = 77; - self.sawAliasedBytes = buffer.length == 3 && bytes[0] == 77; - self.sawUnownedBytes = !buffer.isOwningBytes; - return @(YES); + NSDictionary *nestedPayload = payload[@"nested"]; + self.lastReceivedPayload = [nestedPayload[@"buffer"] copy]; } -- (void)testMethodWhichStoresArrayBuffer:(RCTArrayBuffer *)payload -{ - self.lastReceivedPayload = [NSData dataWithBytes:payload.mutableBytes length:payload.length]; -} - -- (void)testMethodWhichCallsBackWithArrayBuffer:(double)size callback:(RCTResponseSenderBlock)callback -{ - callback(@[ createIntegerSequenceBuffer(static_cast(size)) ]); -} - -- (void)testMethodWhichReturnsArrayBuffer:(double)size - resolve:(RCTPromiseResolveBlock)resolve - reject:(RCTPromiseRejectBlock)reject +RCT_EXPORT_METHOD( + testMethodWhichReturnsArrayBuffer : (double)size resolve : (RCTPromiseResolveBlock) + resolve reject : (RCTPromiseRejectBlock)reject) { if (resolve == nil || reject == nil) { return; } - resolve(createIntegerSequenceBuffer(static_cast(size))); + resolve(createIntegerSequenceData(static_cast(size))); } @end @@ -207,116 +186,6 @@ RCT_EXPORT_MODULE() XCTAssertEqual(returnedBytes[2], 30); } -// The sync argument is a live alias: an in-place write lands on the JS ArrayBuffer itself. -- (void)testJSBackedArrayBufferIsNotCopiedDuringTheCall -{ - auto hermesRuntime = createHermesRuntime(); - facebook::jsi::Runtime *rt = hermesRuntime.get(); - auto *instance = [RCTTestArrayBufferTurboModule new]; - - ObjCTurboModule::InitParams params = { - .moduleName = "TestModule", - .instance = instance, - .jsInvoker = nullptr, - .nativeMethodCallInvoker = std::make_shared(), - .isSyncModule = false, - }; - ObjCTurboModule module(params); - - auto sourceBuffer = rt->global() - .getPropertyAsFunction(*rt, "eval") - .call(*rt, "new Uint8Array([1, 2, 3]).buffer") - .asObject(*rt) - .getArrayBuffer(*rt); - facebook::jsi::Value args[1] = {facebook::jsi::Value(*rt, sourceBuffer)}; - - module.invokeObjCMethod( - *rt, - BooleanKind, - "testMethodWhichChecksArrayBufferAliasing", - @selector(testMethodWhichChecksArrayBufferAliasing:), - args, - 1); - - XCTAssertTrue(instance.sawAliasedBytes, @"The argument must alias the JS ArrayBuffer's bytes"); - XCTAssertTrue(instance.sawUnownedBytes, @"A JS-heap argument to a sync method must not own its bytes"); - XCTAssertEqual(bytesFromArrayBuffer(*rt, sourceBuffer)[0], 77, @"The native write must land on the JS ArrayBuffer"); -} - -// Returning the argument must hand JS the mutated bytes: it has to stay valid through the -// return-value conversion. -- (void)testSyncMethodCanReturnItsOwnArgument -{ - auto hermesRuntime = createHermesRuntime(); - facebook::jsi::Runtime *rt = hermesRuntime.get(); - auto *instance = [RCTTestArrayBufferTurboModule new]; - - ObjCTurboModule::InitParams params = { - .moduleName = "TestModule", - .instance = instance, - .jsInvoker = nullptr, - .nativeMethodCallInvoker = std::make_shared(), - .isSyncModule = false, - }; - ObjCTurboModule module(params); - - auto sourceBuffer = rt->global() - .getPropertyAsFunction(*rt, "eval") - .call(*rt, "new Uint8Array([1, 2, 3]).buffer") - .asObject(*rt) - .getArrayBuffer(*rt); - facebook::jsi::Value args[1] = {facebook::jsi::Value(*rt, sourceBuffer)}; - - auto result = module.invokeObjCMethod( - *rt, - ArrayBufferKind, - "testMethodWhichReturnsItsArgument", - @selector(testMethodWhichReturnsItsArgument:), - args, - 1); - - XCTAssertTrue(result.isObject()); - XCTAssertTrue(result.asObject(*rt).isArrayBuffer(*rt)); - auto returnedBytes = bytesFromArrayBuffer(*rt, result.asObject(*rt).getArrayBuffer(*rt)); - XCTAssertEqual(returnedBytes.size(), 3u, @"Returning the argument must not yield an empty buffer"); - XCTAssertEqual(returnedBytes[0], 99); - XCTAssertEqual(returnedBytes[1], 98); - XCTAssertEqual(returnedBytes[2], 97); -} - -// A zero-length ArrayBuffer must round-trip, NULL bytes pointer and all. -- (void)testEmptyArrayBufferRoundTrip -{ - auto hermesRuntime = createHermesRuntime(); - facebook::jsi::Runtime *rt = hermesRuntime.get(); - auto *instance = [RCTTestArrayBufferTurboModule new]; - - ObjCTurboModule::InitParams params = { - .moduleName = "TestModule", - .instance = instance, - .jsInvoker = nullptr, - .nativeMethodCallInvoker = std::make_shared(), - .isSyncModule = false, - }; - ObjCTurboModule module(params); - - auto sourceBuffer = - rt->global().getPropertyAsFunction(*rt, "eval").call(*rt, "new ArrayBuffer(0)").asObject(*rt).getArrayBuffer(*rt); - facebook::jsi::Value args[1] = {facebook::jsi::Value(*rt, sourceBuffer)}; - - auto result = module.invokeObjCMethod( - *rt, - ArrayBufferKind, - "testMethodWhichTransformsArrayBuffer", - @selector(testMethodWhichTransformsArrayBuffer:), - args, - 1); - - XCTAssertTrue(result.isObject()); - XCTAssertTrue(result.asObject(*rt).isArrayBuffer(*rt)); - XCTAssertEqual(result.asObject(*rt).getArrayBuffer(*rt).size(*rt), 0u); -} - - (void)testAsyncJSBackedArrayBufferIsCopied { auto hermesRuntime = createHermesRuntime(); @@ -357,6 +226,55 @@ RCT_EXPORT_MODULE() XCTAssertEqual(receivedBytes[2], 3); } +- (void)testAsyncNestedJSBackedArrayBufferIsCopied +{ + auto hermesRuntime = createHermesRuntime(); + facebook::jsi::Runtime *rt = hermesRuntime.get(); + auto nativeInvoker = std::make_shared(); + auto *instance = [RCTTestArrayBufferTurboModule new]; + + ObjCTurboModule::InitParams params = { + .moduleName = "TestModule", + .instance = instance, + .jsInvoker = nullptr, + .nativeMethodCallInvoker = nativeInvoker, + .isSyncModule = false, + }; + ObjCTurboModule module(params); + + auto sourceObject = rt->global() + .getPropertyAsFunction(*rt, "eval") + .call(*rt, "({nested: {buffer: new Uint8Array([1, 2, 3]).buffer}})") + .asObject(*rt); + auto sourceBuffer = sourceObject.getProperty(*rt, "nested") + .asObject(*rt) + .getProperty(*rt, "buffer") + .asObject(*rt) + .getArrayBuffer(*rt); + facebook::jsi::Value args[1] = {facebook::jsi::Value(*rt, sourceObject)}; + + module.invokeObjCMethod( + *rt, + VoidKind, + "testMethodWhichStoresNestedArrayBuffer", + @selector(testMethodWhichStoresNestedArrayBuffer:), + args, + 1); + + auto *sourceBytes = sourceBuffer.data(*rt); + sourceBytes[0] = 9; + sourceBytes[1] = 8; + sourceBytes[2] = 7; + + nativeInvoker->flushQueue(); + + auto receivedBytes = bytesFromData(instance.lastReceivedPayload); + XCTAssertEqual(receivedBytes.size(), 3u); + XCTAssertEqual(receivedBytes[0], 1); + XCTAssertEqual(receivedBytes[1], 2); + XCTAssertEqual(receivedBytes[2], 3); +} + - (void)testAsyncNativeBackedArrayBufferRetainsBackingStore { auto hermesRuntime = createHermesRuntime(); @@ -394,56 +312,6 @@ RCT_EXPORT_MODULE() XCTAssertEqual(receivedBytes[3], 7); } -// A buffer handed to a callback is delivered to JS after the native call has returned. -- (void)testCallbackDeliversArrayBuffer -{ - auto hermesRuntime = createHermesRuntime(); - facebook::jsi::Runtime *rt = hermesRuntime.get(); - auto jsInvoker = std::make_shared(*rt); - auto *instance = [RCTTestArrayBufferTurboModule new]; - - ObjCTurboModule::InitParams params = { - .moduleName = "TestModule", - .instance = instance, - .jsInvoker = jsInvoker, - .nativeMethodCallInvoker = std::make_shared(), - .isSyncModule = false, - }; - ObjCTurboModule module(params); - - std::vector callbackBytes; - auto onCallback = facebook::jsi::Function::createFromHostFunction( - *rt, - facebook::jsi::PropNameID::forAscii(*rt, "onCallback"), - 1, - [&callbackBytes]( - facebook::jsi::Runtime &runtime, - const facebook::jsi::Value &, - const facebook::jsi::Value *callbackArgs, - size_t count) -> facebook::jsi::Value { - if (count == 1 && callbackArgs[0].isObject() && callbackArgs[0].asObject(runtime).isArrayBuffer(runtime)) { - callbackBytes = bytesFromArrayBuffer(runtime, callbackArgs[0].asObject(runtime).getArrayBuffer(runtime)); - } - return facebook::jsi::Value::undefined(); - }); - facebook::jsi::Value args[2] = {facebook::jsi::Value(3.0), facebook::jsi::Value(*rt, onCallback)}; - - module.invokeObjCMethod( - *rt, - VoidKind, - "testMethodWhichCallsBackWithArrayBuffer", - @selector(testMethodWhichCallsBackWithArrayBuffer:callback:), - args, - 2); - - jsInvoker->flushQueue(); - - XCTAssertEqual(callbackBytes.size(), 3u); - XCTAssertEqual(callbackBytes[0], 0); - XCTAssertEqual(callbackBytes[1], 1); - XCTAssertEqual(callbackBytes[2], 2); -} - - (void)testPromiseResolvesArrayBuffer { auto hermesRuntime = createHermesRuntime(true); diff --git a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api index c23703c476a..4b927bf1ac6 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api @@ -654,16 +654,6 @@ interface RCTAppearance : public RCTEventEmitter { public virtual instancetype init(); } -interface RCTArrayBuffer : public NSObject { - public @property (assign, readonly) BOOL owningBytes; - public @property (assign, readonly) NSUInteger length; - public @property (assign, readonly) void* mutableBytes NS_RETURNS_INNER_POINTER; - public virtual static instancetype arrayBufferWithCopiedBytes:length:(const void* _Nullable bytes, NSUInteger length); - public virtual static instancetype arrayBufferWithLength:(NSUInteger length); - public virtual static instancetype arrayBufferWithOwnedBytes:length:cleanup:(_Nullable void* bytes, NSUInteger length, _Nullable void(^)(void) cleanup); - public virtual static instancetype arrayBufferWithUnownedBytes:length:(_Nullable void* bytes, NSUInteger length); -} - interface RCTBackedTextFieldDelegateAdapter : public NSObject { public virtual instancetype initWithTextField:(UITextField* backedTextInputView); public virtual void selectedTextRangeWasSet(); @@ -2512,19 +2502,19 @@ protocol NativeSampleTurboModuleSpec : public NSObjectRCTBridgeModule, public RC public virtual NSDictionary* getObjectThrows:(NSDictionary* arg); public virtual NSDictionary* getUnsafeObject:(NSDictionary* arg); public virtual NSDictionary* getValue:y:z:(double x, NSString* y, NSDictionary* z); + public virtual NSMutableData* createNativeBuffer:(double size); + public virtual NSMutableData* getArrayBuffer:(NSData* buffer); public virtual NSNumber* getBool:(BOOL arg); public virtual NSNumber* getEnum:(double arg); public virtual NSNumber* getNumber:(double arg); public virtual NSNumber* getRootTag:(double arg); public virtual NSString* getString:(NSString* arg); - public virtual RCTArrayBuffer* createNativeBuffer:(double size); - public virtual RCTArrayBuffer* getArrayBuffer:(RCTArrayBuffer* buffer); public virtual facebook::react::ModuleConstants constantsToExport(); public virtual facebook::react::ModuleConstants getConstants(); public virtual void getImageUrl:reject:(RCTPromiseResolveBlock resolve, RCTPromiseRejectBlock reject); public virtual void getValueWithCallback:(RCTResponseSenderBlock callback); public virtual void getValueWithPromise:resolve:reject:(BOOL error, RCTPromiseResolveBlock resolve, RCTPromiseRejectBlock reject); - public virtual void processAsyncBuffer:resolve:reject:(RCTArrayBuffer* payload, RCTPromiseResolveBlock resolve, RCTPromiseRejectBlock reject); + public virtual void processAsyncBuffer:resolve:reject:(NSData* payload, RCTPromiseResolveBlock resolve, RCTPromiseRejectBlock reject); public virtual void promiseAssert:reject:(RCTPromiseResolveBlock resolve, RCTPromiseRejectBlock reject); public virtual void promiseThrows:reject:(RCTPromiseResolveBlock resolve, RCTPromiseRejectBlock reject); public virtual void voidFunc(); @@ -6169,7 +6159,7 @@ class facebook::react::ObjCInteropTurboModule : public facebook::react::ObjCTurb protected virtual NSString* getArgumentTypeName(facebook::jsi::Runtime& runtime, NSString* methodName, int argIndex) override; protected virtual facebook::jsi::Value convertReturnIdToJSIValue(facebook::jsi::Runtime& runtime, const char* methodName, facebook::react::TurboModuleMethodValueKind returnType, id result) override; protected virtual facebook::jsi::Value create(facebook::jsi::Runtime& runtime, const facebook::jsi::PropNameID& propName) override; - protected virtual void setInvocationArg(facebook::jsi::Runtime& runtime, const char* methodName, const std::string& objCArgType, const facebook::jsi::Value& arg, size_t i, NSInvocation* inv, NSMutableArray* retainedObjectsForInvocation, bool mustCopyBytes) override; + protected virtual void setInvocationArg(facebook::jsi::Runtime& runtime, const char* methodName, const std::string& objCArgType, const facebook::jsi::Value& arg, size_t i, NSInvocation* inv, NSMutableArray* retainedObjectsForInvocation) override; public ObjCInteropTurboModule(const facebook::react::ObjCTurboModule::InitParams& params); public virtual std::vector getPropertyNames(facebook::jsi::Runtime& runtime) override; } @@ -6184,7 +6174,7 @@ struct facebook::react::ObjCInteropTurboModule::MethodDescriptor { class facebook::react::ObjCTurboModule : public facebook::react::TurboModule { protected virtual NSString* getArgumentTypeName(facebook::jsi::Runtime& runtime, NSString* methodName, int argIndex); protected virtual facebook::jsi::Value convertReturnIdToJSIValue(facebook::jsi::Runtime& runtime, const char* methodName, facebook::react::TurboModuleMethodValueKind returnType, id result); - protected virtual void setInvocationArg(facebook::jsi::Runtime& runtime, const char* methodName, const std::string& objCArgType, const facebook::jsi::Value& arg, size_t i, NSInvocation* inv, NSMutableArray* retainedObjectsForInvocation, bool mustCopyBytes); + protected virtual void setInvocationArg(facebook::jsi::Runtime& runtime, const char* methodName, const std::string& objCArgType, const facebook::jsi::Value& arg, size_t i, NSInvocation* inv, NSMutableArray* retainedObjectsForInvocation); protected void setEventEmitterCallback(facebook::react::EventEmitterCallback eventEmitterCallback); protected void setMethodArgConversionSelector(NSString* methodName, size_t argIndex, NSString* fnName); public ObjCTurboModule(const facebook::react::ObjCTurboModule::InitParams& params); @@ -13661,7 +13651,7 @@ struct facebook::react::dom::RNMeasureRect { facebook::jsi::Value facebook::react::TurboModuleConvertUtils::convertObjCObjectToJSIValue(facebook::jsi::Runtime& runtime, id value); -id facebook::react::TurboModuleConvertUtils::convertJSIValueToObjCObject(facebook::jsi::Runtime& runtime, const facebook::jsi::Value& value, const std::shared_ptr& jsInvoker, BOOL useNSNull = NO, BOOL mustCopyBytes = YES); +id facebook::react::TurboModuleConvertUtils::convertJSIValueToObjCObject(facebook::jsi::Runtime& runtime, const facebook::jsi::Value& value, const std::shared_ptr& jsInvoker, BOOL useNSNull = NO); static const facebook::react::Color facebook::react::HostPlatformColor::UndefinedColor; diff --git a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api index 95f5e8b375f..bec93c59aa3 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api @@ -654,16 +654,6 @@ interface RCTAppearance : public RCTEventEmitter { public virtual instancetype init(); } -interface RCTArrayBuffer : public NSObject { - public @property (assign, readonly) BOOL owningBytes; - public @property (assign, readonly) NSUInteger length; - public @property (assign, readonly) void* mutableBytes NS_RETURNS_INNER_POINTER; - public virtual static instancetype arrayBufferWithCopiedBytes:length:(const void* _Nullable bytes, NSUInteger length); - public virtual static instancetype arrayBufferWithLength:(NSUInteger length); - public virtual static instancetype arrayBufferWithOwnedBytes:length:cleanup:(_Nullable void* bytes, NSUInteger length, _Nullable void(^)(void) cleanup); - public virtual static instancetype arrayBufferWithUnownedBytes:length:(_Nullable void* bytes, NSUInteger length); -} - interface RCTBackedTextFieldDelegateAdapter : public NSObject { public virtual instancetype initWithTextField:(UITextField* backedTextInputView); public virtual void selectedTextRangeWasSet(); @@ -2505,19 +2495,19 @@ protocol NativeSampleTurboModuleSpec : public NSObjectRCTBridgeModule, public RC public virtual NSDictionary* getObjectThrows:(NSDictionary* arg); public virtual NSDictionary* getUnsafeObject:(NSDictionary* arg); public virtual NSDictionary* getValue:y:z:(double x, NSString* y, NSDictionary* z); + public virtual NSMutableData* createNativeBuffer:(double size); + public virtual NSMutableData* getArrayBuffer:(NSData* buffer); public virtual NSNumber* getBool:(BOOL arg); public virtual NSNumber* getEnum:(double arg); public virtual NSNumber* getNumber:(double arg); public virtual NSNumber* getRootTag:(double arg); public virtual NSString* getString:(NSString* arg); - public virtual RCTArrayBuffer* createNativeBuffer:(double size); - public virtual RCTArrayBuffer* getArrayBuffer:(RCTArrayBuffer* buffer); public virtual facebook::react::ModuleConstants constantsToExport(); public virtual facebook::react::ModuleConstants getConstants(); public virtual void getImageUrl:reject:(RCTPromiseResolveBlock resolve, RCTPromiseRejectBlock reject); public virtual void getValueWithCallback:(RCTResponseSenderBlock callback); public virtual void getValueWithPromise:resolve:reject:(BOOL error, RCTPromiseResolveBlock resolve, RCTPromiseRejectBlock reject); - public virtual void processAsyncBuffer:resolve:reject:(RCTArrayBuffer* payload, RCTPromiseResolveBlock resolve, RCTPromiseRejectBlock reject); + public virtual void processAsyncBuffer:resolve:reject:(NSData* payload, RCTPromiseResolveBlock resolve, RCTPromiseRejectBlock reject); public virtual void promiseAssert:reject:(RCTPromiseResolveBlock resolve, RCTPromiseRejectBlock reject); public virtual void promiseThrows:reject:(RCTPromiseResolveBlock resolve, RCTPromiseRejectBlock reject); public virtual void voidFunc(); @@ -6055,7 +6045,7 @@ class facebook::react::ObjCInteropTurboModule : public facebook::react::ObjCTurb protected virtual NSString* getArgumentTypeName(facebook::jsi::Runtime& runtime, NSString* methodName, int argIndex) override; protected virtual facebook::jsi::Value convertReturnIdToJSIValue(facebook::jsi::Runtime& runtime, const char* methodName, facebook::react::TurboModuleMethodValueKind returnType, id result) override; protected virtual facebook::jsi::Value create(facebook::jsi::Runtime& runtime, const facebook::jsi::PropNameID& propName) override; - protected virtual void setInvocationArg(facebook::jsi::Runtime& runtime, const char* methodName, const std::string& objCArgType, const facebook::jsi::Value& arg, size_t i, NSInvocation* inv, NSMutableArray* retainedObjectsForInvocation, bool mustCopyBytes) override; + protected virtual void setInvocationArg(facebook::jsi::Runtime& runtime, const char* methodName, const std::string& objCArgType, const facebook::jsi::Value& arg, size_t i, NSInvocation* inv, NSMutableArray* retainedObjectsForInvocation) override; public ObjCInteropTurboModule(const facebook::react::ObjCTurboModule::InitParams& params); public virtual std::vector getPropertyNames(facebook::jsi::Runtime& runtime) override; } @@ -6070,7 +6060,7 @@ struct facebook::react::ObjCInteropTurboModule::MethodDescriptor { class facebook::react::ObjCTurboModule : public facebook::react::TurboModule { protected virtual NSString* getArgumentTypeName(facebook::jsi::Runtime& runtime, NSString* methodName, int argIndex); protected virtual facebook::jsi::Value convertReturnIdToJSIValue(facebook::jsi::Runtime& runtime, const char* methodName, facebook::react::TurboModuleMethodValueKind returnType, id result); - protected virtual void setInvocationArg(facebook::jsi::Runtime& runtime, const char* methodName, const std::string& objCArgType, const facebook::jsi::Value& arg, size_t i, NSInvocation* inv, NSMutableArray* retainedObjectsForInvocation, bool mustCopyBytes); + protected virtual void setInvocationArg(facebook::jsi::Runtime& runtime, const char* methodName, const std::string& objCArgType, const facebook::jsi::Value& arg, size_t i, NSInvocation* inv, NSMutableArray* retainedObjectsForInvocation); protected void setEventEmitterCallback(facebook::react::EventEmitterCallback eventEmitterCallback); protected void setMethodArgConversionSelector(NSString* methodName, size_t argIndex, NSString* fnName); public ObjCTurboModule(const facebook::react::ObjCTurboModule::InitParams& params); @@ -13340,7 +13330,7 @@ struct facebook::react::dom::RNMeasureRect { facebook::jsi::Value facebook::react::TurboModuleConvertUtils::convertObjCObjectToJSIValue(facebook::jsi::Runtime& runtime, id value); -id facebook::react::TurboModuleConvertUtils::convertJSIValueToObjCObject(facebook::jsi::Runtime& runtime, const facebook::jsi::Value& value, const std::shared_ptr& jsInvoker, BOOL useNSNull = NO, BOOL mustCopyBytes = YES); +id facebook::react::TurboModuleConvertUtils::convertJSIValueToObjCObject(facebook::jsi::Runtime& runtime, const facebook::jsi::Value& value, const std::shared_ptr& jsInvoker, BOOL useNSNull = NO); static const facebook::react::Color facebook::react::HostPlatformColor::UndefinedColor; diff --git a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api index 9e9d12b2d5f..481f3569b85 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api @@ -654,16 +654,6 @@ interface RCTAppearance : public RCTEventEmitter { public virtual instancetype init(); } -interface RCTArrayBuffer : public NSObject { - public @property (assign, readonly) BOOL owningBytes; - public @property (assign, readonly) NSUInteger length; - public @property (assign, readonly) void* mutableBytes NS_RETURNS_INNER_POINTER; - public virtual static instancetype arrayBufferWithCopiedBytes:length:(const void* _Nullable bytes, NSUInteger length); - public virtual static instancetype arrayBufferWithLength:(NSUInteger length); - public virtual static instancetype arrayBufferWithOwnedBytes:length:cleanup:(_Nullable void* bytes, NSUInteger length, _Nullable void(^)(void) cleanup); - public virtual static instancetype arrayBufferWithUnownedBytes:length:(_Nullable void* bytes, NSUInteger length); -} - interface RCTBackedTextFieldDelegateAdapter : public NSObject { public virtual instancetype initWithTextField:(UITextField* backedTextInputView); public virtual void selectedTextRangeWasSet(); @@ -2512,19 +2502,19 @@ protocol NativeSampleTurboModuleSpec : public NSObjectRCTBridgeModule, public RC public virtual NSDictionary* getObjectThrows:(NSDictionary* arg); public virtual NSDictionary* getUnsafeObject:(NSDictionary* arg); public virtual NSDictionary* getValue:y:z:(double x, NSString* y, NSDictionary* z); + public virtual NSMutableData* createNativeBuffer:(double size); + public virtual NSMutableData* getArrayBuffer:(NSData* buffer); public virtual NSNumber* getBool:(BOOL arg); public virtual NSNumber* getEnum:(double arg); public virtual NSNumber* getNumber:(double arg); public virtual NSNumber* getRootTag:(double arg); public virtual NSString* getString:(NSString* arg); - public virtual RCTArrayBuffer* createNativeBuffer:(double size); - public virtual RCTArrayBuffer* getArrayBuffer:(RCTArrayBuffer* buffer); public virtual facebook::react::ModuleConstants constantsToExport(); public virtual facebook::react::ModuleConstants getConstants(); public virtual void getImageUrl:reject:(RCTPromiseResolveBlock resolve, RCTPromiseRejectBlock reject); public virtual void getValueWithCallback:(RCTResponseSenderBlock callback); public virtual void getValueWithPromise:resolve:reject:(BOOL error, RCTPromiseResolveBlock resolve, RCTPromiseRejectBlock reject); - public virtual void processAsyncBuffer:resolve:reject:(RCTArrayBuffer* payload, RCTPromiseResolveBlock resolve, RCTPromiseRejectBlock reject); + public virtual void processAsyncBuffer:resolve:reject:(NSData* payload, RCTPromiseResolveBlock resolve, RCTPromiseRejectBlock reject); public virtual void promiseAssert:reject:(RCTPromiseResolveBlock resolve, RCTPromiseRejectBlock reject); public virtual void promiseThrows:reject:(RCTPromiseResolveBlock resolve, RCTPromiseRejectBlock reject); public virtual void voidFunc(); @@ -6166,7 +6156,7 @@ class facebook::react::ObjCInteropTurboModule : public facebook::react::ObjCTurb protected virtual NSString* getArgumentTypeName(facebook::jsi::Runtime& runtime, NSString* methodName, int argIndex) override; protected virtual facebook::jsi::Value convertReturnIdToJSIValue(facebook::jsi::Runtime& runtime, const char* methodName, facebook::react::TurboModuleMethodValueKind returnType, id result) override; protected virtual facebook::jsi::Value create(facebook::jsi::Runtime& runtime, const facebook::jsi::PropNameID& propName) override; - protected virtual void setInvocationArg(facebook::jsi::Runtime& runtime, const char* methodName, const std::string& objCArgType, const facebook::jsi::Value& arg, size_t i, NSInvocation* inv, NSMutableArray* retainedObjectsForInvocation, bool mustCopyBytes) override; + protected virtual void setInvocationArg(facebook::jsi::Runtime& runtime, const char* methodName, const std::string& objCArgType, const facebook::jsi::Value& arg, size_t i, NSInvocation* inv, NSMutableArray* retainedObjectsForInvocation) override; public ObjCInteropTurboModule(const facebook::react::ObjCTurboModule::InitParams& params); public virtual std::vector getPropertyNames(facebook::jsi::Runtime& runtime) override; } @@ -6181,7 +6171,7 @@ struct facebook::react::ObjCInteropTurboModule::MethodDescriptor { class facebook::react::ObjCTurboModule : public facebook::react::TurboModule { protected virtual NSString* getArgumentTypeName(facebook::jsi::Runtime& runtime, NSString* methodName, int argIndex); protected virtual facebook::jsi::Value convertReturnIdToJSIValue(facebook::jsi::Runtime& runtime, const char* methodName, facebook::react::TurboModuleMethodValueKind returnType, id result); - protected virtual void setInvocationArg(facebook::jsi::Runtime& runtime, const char* methodName, const std::string& objCArgType, const facebook::jsi::Value& arg, size_t i, NSInvocation* inv, NSMutableArray* retainedObjectsForInvocation, bool mustCopyBytes); + protected virtual void setInvocationArg(facebook::jsi::Runtime& runtime, const char* methodName, const std::string& objCArgType, const facebook::jsi::Value& arg, size_t i, NSInvocation* inv, NSMutableArray* retainedObjectsForInvocation); protected void setEventEmitterCallback(facebook::react::EventEmitterCallback eventEmitterCallback); protected void setMethodArgConversionSelector(NSString* methodName, size_t argIndex, NSString* fnName); public ObjCTurboModule(const facebook::react::ObjCTurboModule::InitParams& params); @@ -13515,7 +13505,7 @@ struct facebook::react::dom::RNMeasureRect { facebook::jsi::Value facebook::react::TurboModuleConvertUtils::convertObjCObjectToJSIValue(facebook::jsi::Runtime& runtime, id value); -id facebook::react::TurboModuleConvertUtils::convertJSIValueToObjCObject(facebook::jsi::Runtime& runtime, const facebook::jsi::Value& value, const std::shared_ptr& jsInvoker, BOOL useNSNull = NO, BOOL mustCopyBytes = YES); +id facebook::react::TurboModuleConvertUtils::convertJSIValueToObjCObject(facebook::jsi::Runtime& runtime, const facebook::jsi::Value& value, const std::shared_ptr& jsInvoker, BOOL useNSNull = NO); static const facebook::react::Color facebook::react::HostPlatformColor::UndefinedColor;