Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,8 @@ function translateFunctionParamToJavaType(
imports.add('com.facebook.react.bridge.Callback');
return wrapOptional('Callback', isRequired);
case 'ArrayBufferTypeAnnotation':
throw new Error(
`${createErrorMessage(realTypeAnnotation.type)} ArrayBuffer is only supported for C++ TurboModules.`,
);
imports.add('java.nio.ByteBuffer');
return wrapOptional('ByteBuffer', isRequired);
default:
realTypeAnnotation.type as 'MixedTypeAnnotation';
throw new Error(createErrorMessage(realTypeAnnotation.type));
Expand Down Expand Up @@ -366,9 +365,8 @@ function translateFunctionReturnTypeToJavaType(
imports.add('com.facebook.react.bridge.WritableArray');
return wrapOptional('WritableArray', isRequired);
case 'ArrayBufferTypeAnnotation':
throw new Error(
`${createErrorMessage(realTypeAnnotation.type)} ArrayBuffer is only supported for C++ TurboModules.`,
);
imports.add('java.nio.ByteBuffer');
return wrapOptional('ByteBuffer', isRequired);
default:
realTypeAnnotation.type as 'MixedTypeAnnotation';
throw new Error(createErrorMessage(realTypeAnnotation.type));
Expand Down Expand Up @@ -452,9 +450,7 @@ function getFalsyReturnStatementFromReturnType(
case 'ArrayTypeAnnotation':
return 'return null;';
case 'ArrayBufferTypeAnnotation':
throw new Error(
`${createErrorMessage(realTypeAnnotation.type)} ArrayBuffer is only supported for C++ TurboModules.`,
);
return 'return null;';
default:
realTypeAnnotation.type as 'MixedTypeAnnotation';
throw new Error(createErrorMessage(realTypeAnnotation.type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ type JSReturnType =
| 'NumberKind'
| 'PromiseKind'
| 'ObjectKind'
| 'ArrayKind';
| 'ArrayKind'
| 'ArrayBufferKind';

const HostFunctionTemplate = ({
hasteModuleName,
Expand Down Expand Up @@ -217,7 +218,7 @@ function translateReturnTypeToKind(
case 'ArrayTypeAnnotation':
return 'ArrayKind';
case 'ArrayBufferTypeAnnotation':
throw new Error('ArrayBuffer is only supported for C++ TurboModules.');
return 'ArrayBufferKind';
default:
realTypeAnnotation.type as 'MixedTypeAnnotation';
throw new Error(
Expand Down Expand Up @@ -306,7 +307,7 @@ function translateParamTypeToJniType(
case 'FunctionTypeAnnotation':
return 'Lcom/facebook/react/bridge/Callback;';
case 'ArrayBufferTypeAnnotation':
throw new Error('ArrayBuffer is only supported for C++ TurboModules.');
return 'Ljava/nio/ByteBuffer;';
default:
realTypeAnnotation.type as 'MixedTypeAnnotation';
throw new Error(
Expand Down Expand Up @@ -392,7 +393,7 @@ function translateReturnTypeToJniType(
case 'ArrayTypeAnnotation':
return 'Lcom/facebook/react/bridge/WritableArray;';
case 'ArrayBufferTypeAnnotation':
throw new Error('ArrayBuffer is only supported for C++ TurboModules.');
return 'Ljava/nio/ByteBuffer;';
default:
realTypeAnnotation.type as 'MixedTypeAnnotation';
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ type ReturnJSType =
| 'ObjectKind'
| 'ArrayKind'
| 'NumberKind'
| 'StringKind';
| 'StringKind'
| 'ArrayBufferKind';

export type MethodSerializationOutput = Readonly<{
methodName: string,
Expand Down Expand Up @@ -219,6 +220,9 @@ function getParamObjCType(
*/
return notStruct(wrapOptional('NSArray *', !nullable));
}
case 'ArrayBufferTypeAnnotation': {
return notStruct(wrapOptional('NSMutableData *', !nullable));
}
}

const [structTypeAnnotation] = unwrapNullable(
Expand Down Expand Up @@ -388,9 +392,7 @@ function getReturnObjCType(
case 'GenericObjectTypeAnnotation':
return wrapOptional('NSDictionary *', isRequired);
case 'ArrayBufferTypeAnnotation':
throw new Error(
`Unsupported return type for ${methodName}: ArrayBuffer is only supported for C++ TurboModules.`,
);
return wrapOptional('NSMutableData *', isRequired);
default:
typeAnnotation.type as 'MixedTypeAnnotation';
throw new Error(
Expand Down Expand Up @@ -464,9 +466,7 @@ function getReturnJSType(
throw new Error(`Unsupported union member types`);
}
case 'ArrayBufferTypeAnnotation':
throw new Error(
`Unsupported return type for ${methodName}: ArrayBuffer is only supported for C++ TurboModules.`,
);
return 'ArrayBufferKind';
default:
typeAnnotation.type as 'MixedTypeAnnotation';
throw new Error(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2678,7 +2678,7 @@ const ARRAY_BUFFER_NATIVE_MODULE: SchemaType = {
],
},
moduleName: 'SampleTurboModule',
excludedPlatforms: ['iOS', 'android'],
excludedPlatforms: ['android'],
},
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,34 @@ Map {
#import <vector>


@protocol NativeSampleTurboModuleSpec <RCTBridgeModule, RCTTurboModule>

- (NSMutableData *)getArrayBuffer;
- (void)voidArrayBuffer:(NSMutableData *)arg;
- (void)voidNullableArrayBuffer:(NSMutableData * _Nullable)arg;
- (void)promiseArrayBuffer:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject;

@end

@interface NativeSampleTurboModuleSpecBase : NSObject {
@protected
facebook::react::EventEmitterCallback _eventEmitterCallback;
}
- (void)setEventEmitterCallback:(EventEmitterCallbackWrapper *)eventEmitterCallbackWrapper;


@end

namespace facebook::react {
/**
* ObjC++ class for module 'NativeSampleTurboModule'
*/
class JSI_EXPORT NativeSampleTurboModuleSpecJSI : public ObjCTurboModule {
public:
NativeSampleTurboModuleSpecJSI(const ObjCTurboModule::InitParams &params);
};
} // namespace facebook::react

#endif // array_buffer_native_module_H
",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,50 @@ Map {
#import \\"array_buffer_native_module.h\\"


@implementation NativeSampleTurboModuleSpecBase


- (void)setEventEmitterCallback:(EventEmitterCallbackWrapper *)eventEmitterCallbackWrapper
{
_eventEmitterCallback = std::move(eventEmitterCallbackWrapper->_eventEmitterCallback);
}
@end


namespace facebook::react {

static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getArrayBuffer(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, ArrayBufferKind, \\"getArrayBuffer\\", @selector(getArrayBuffer), args, count);
}

static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_voidArrayBuffer(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, VoidKind, \\"voidArrayBuffer\\", @selector(voidArrayBuffer:), args, count);
}

static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_voidNullableArrayBuffer(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, VoidKind, \\"voidNullableArrayBuffer\\", @selector(voidNullableArrayBuffer:), args, count);
}

static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_promiseArrayBuffer(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {
return static_cast<ObjCTurboModule&>(turboModule).invokeObjCMethod(rt, PromiseKind, \\"promiseArrayBuffer\\", @selector(promiseArrayBuffer:reject:), args, count);
}

NativeSampleTurboModuleSpecJSI::NativeSampleTurboModuleSpecJSI(const ObjCTurboModule::InitParams &params)
: ObjCTurboModule(params) {

methodMap_[\\"getArrayBuffer\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getArrayBuffer};


methodMap_[\\"voidArrayBuffer\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_voidArrayBuffer};


methodMap_[\\"voidNullableArrayBuffer\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_voidNullableArrayBuffer};


methodMap_[\\"promiseArrayBuffer\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_promiseArrayBuffer};

}
} // namespace facebook::react
",
}
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ enum TurboModuleMethodValueKind {
ArrayKind,
FunctionKind,
PromiseKind,
ArrayBufferKind
};

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ class JSI_EXPORT ObjCInteropTurboModule : public ObjCTurboModule {
const jsi::Value &arg,
size_t i,
NSInvocation *inv,
NSMutableArray *retainedObjectsForInvocation) override;
NSMutableArray *retainedObjectsForInvocation,
BOOL isSync) override;

private:
std::vector<MethodDescriptor> methodDescriptors_;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,8 @@ T RCTConvertTo(SEL selector, id json)
const jsi::Value &jsiArg,
size_t index,
NSInvocation *inv,
NSMutableArray *retainedObjectsForInvocation)
NSMutableArray *retainedObjectsForInvocation,
BOOL isSync)
{
NSString *methodName = @(methodNameCStr);
std::string methodJsSignature = name_ + "." + methodNameCStr + "()";
Expand All @@ -373,7 +374,7 @@ T RCTConvertTo(SEL selector, id json)
SEL selector = selectorForType(argumentType);

if ([RCTConvert respondsToSelector:selector]) {
id objCArg = TurboModuleConvertUtils::convertJSIValueToObjCObject(runtime, jsiArg, jsInvoker_, YES);
id objCArg = TurboModuleConvertUtils::convertJSIValueToObjCObject(runtime, jsiArg, jsInvoker_, YES, isSync);

if (objCArgType == @encode(char)) {
char arg = RCTConvertTo<char>(selector, objCArg);
Expand Down Expand Up @@ -582,7 +583,7 @@ T RCTConvertTo(SEL selector, id json)
runtime, errorPrefix + "JavaScript argument must be a plain object. Got " + getType(runtime, jsiArg));
}

id arg = TurboModuleConvertUtils::convertJSIValueToObjCObject(runtime, jsiArg, jsInvoker_, YES);
id arg = TurboModuleConvertUtils::convertJSIValueToObjCObject(runtime, jsiArg, jsInvoker_, YES, isSync);

RCTManagedPointer *(*convert)(id, SEL, id) = (__typeof__(convert))objc_msgSend;
RCTManagedPointer *box = convert([RCTCxxConvert class], selector, arg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ id convertJSIValueToObjCObject(
jsi::Runtime &runtime,
const jsi::Value &value,
const std::shared_ptr<CallInvoker> &jsInvoker,
BOOL useNSNull = NO);
BOOL useNSNull = NO,
BOOL isSync = NO);
} // namespace TurboModuleConvertUtils

template <>
Expand Down Expand Up @@ -121,7 +122,8 @@ class JSI_EXPORT ObjCTurboModule : public TurboModule {
const jsi::Value &arg,
size_t i,
NSInvocation *inv,
NSMutableArray *retainedObjectsForInvocation);
NSMutableArray *retainedObjectsForInvocation,
BOOL isSync);

private:
// Does the NativeModule dispatch async methods to the JS thread?
Expand Down
Loading
Loading