Revert "Use NSData for the JS->ObjC ArrayBuffer argument path (#57596)"

This reverts commit 5e86c323ce.

# Conflicts:
#	packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleHObjCpp-test.js.snap
#	packages/react-native/ReactCommon/react/nativemodule/core/iostests/RCTTurboModuleTests.mm
This commit is contained in:
Fabrizio Cucci committed 2026-09-21 12:17:17 +01:00
1 parent 639cdedb50
commit 19a3184981
5 files changed
+32 -31

No files matched your search

@@ -227,7 +227,7 @@ function getParamObjCType(
return notStruct(wrapOptional('NSArray *', !nullable));
}
case 'ArrayBufferTypeAnnotation': {
return notStruct(wrapOptional('NSData *', !nullable));
return notStruct(wrapOptional('NSMutableData *', !nullable));
}
}
@@ -103,8 +103,8 @@ Map {
@protocol NativeSampleTurboModuleSpec <RCTBridgeModule, RCTTurboModule>
- (NSMutableData *)getArrayBuffer;
- (void)voidArrayBuffer:(NSData *)arg;
- (void)voidNullableArrayBuffer:(NSData * _Nullable)arg;
- (void)voidArrayBuffer:(NSMutableData *)arg;
- (void)voidNullableArrayBuffer:(NSMutableData * _Nullable)arg;
@end
@@ -319,11 +319,12 @@ class ExceptionCapturingNativeMethodCallInvoker : public NativeMethodCallInvoker
XCTAssertNotNil(original.callStackSymbols);
}
// 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
// A JS ArrayBuffer converts to an NSMutableData that owns an independent copy of
// the bytes — NSMutableData cannot alias a foreign buffer, so the result stays
// valid and is safe to mutate 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)testArrayBufferConvertsToIndependentNSMutableData
{
constexpr size_t kBufferSize = 64 * 1024;
@@ -337,15 +338,15 @@ class ExceptionCapturingNativeMethodCallInvoker : public NativeMethodCallInvoker
id converted =
TurboModuleConvertUtils::convertJSIValueToObjCObject(*rt, facebook::jsi::Value(*rt, arrayBuffer), nullptr);
XCTAssertTrue([converted isKindOfClass:[NSData class]]);
NSData *data = (NSData *)converted;
XCTAssertTrue([converted isKindOfClass:[NSMutableData class]]);
NSMutableData *data = (NSMutableData *)converted;
XCTAssertEqual(data.length, (NSUInteger)kBufferSize);
XCTAssertEqual(*static_cast<const uint8_t *>(data.bytes), 0xAB);
// Independent copy: mutating the source MutableBuffer must not write through to
// the NSData.
*buffer->data() = 0xCD;
XCTAssertEqual(*static_cast<const uint8_t *>(data.bytes), 0xAB, @"NSData must not alias the source buffer");
// Independent copy: mutating the NSMutableData must not write through to the
// source MutableBuffer.
*static_cast<uint8_t *>(data.mutableBytes) = 0xCD;
XCTAssertEqual(*buffer->data(), 0xAB, @"NSMutableData must not alias the source buffer");
}
@end
@@ -201,15 +201,16 @@ convertJSIFunctionToCallback(jsi::Runtime &rt, jsi::Function &&function, const s
};
}
// 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)
// Copy the ArrayBuffer's bytes into an NSMutableData. A zero-copy wrap is not
// possible here: NSMutableData needs its own resizable backing store and cannot
// alias a foreign buffer (even via initWithBytesNoCopy:length:deallocator:, which
// copies eagerly for the mutable subclass). Copying also makes the result 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 NSMutableData *convertJSIArrayBufferToNSMutableData(jsi::Runtime &rt, const jsi::ArrayBuffer &value)
{
return [NSData dataWithBytes:value.data(rt) length:value.size(rt)];
return [NSMutableData dataWithBytes:value.data(rt) length:value.size(rt)];
}
id convertJSIValueToObjCObject(
@@ -242,7 +243,7 @@ id convertJSIValueToObjCObject(
return convertJSIFunctionToCallback(runtime, o.getFunction(runtime), jsInvoker);
}
if (o.isArrayBuffer(runtime)) {
return convertJSIArrayBufferToNSData(runtime, o.getArrayBuffer(runtime));
return convertJSIArrayBufferToNSMutableData(runtime, o.getArrayBuffer(runtime));
}
return convertJSIObjectToNSDictionary(runtime, o, jsInvoker, useNSNull);
}
@@ -105,17 +105,16 @@ class QueueingNativeMethodCallInvoker final : public NativeMethodCallInvoker {
RCT_EXPORT_MODULE()
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSMutableData *, testMethodWhichTransformsArrayBuffer : (NSData *)buffer)
RCT_EXPORT_SYNCHRONOUS_TYPED_METHOD(NSMutableData *, testMethodWhichMutatesArrayBuffer : (NSMutableData *)buffer)
{
NSMutableData *result = [buffer mutableCopy];
auto *bytes = static_cast<uint8_t *>(result.mutableBytes);
for (NSUInteger i = 0; i < result.length; ++i) {
auto *bytes = static_cast<uint8_t *>(buffer.mutableBytes);
for (NSUInteger i = 0; i < buffer.length; ++i) {
bytes[i] = static_cast<uint8_t>((i + 1) * 10);
}
return result;
return buffer;
}
RCT_EXPORT_METHOD(testMethodWhichStoresArrayBuffer : (NSData *)payload)
RCT_EXPORT_METHOD(testMethodWhichStoresArrayBuffer : (NSMutableData *)payload)
{
self.lastReceivedPayload = [payload copy];
}
@@ -170,8 +169,8 @@ RCT_EXPORT_METHOD(
auto result = module.invokeObjCMethod(
*rt,
ArrayBufferKind,
"testMethodWhichTransformsArrayBuffer",
@selector(testMethodWhichTransformsArrayBuffer:),
"testMethodWhichMutatesArrayBuffer",
@selector(testMethodWhichMutatesArrayBuffer:),
args,
1);