diff --git a/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js b/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js index e6b63f725a22d7..940b721ad41647 100644 --- a/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js +++ b/packages/react-native-codegen/src/generators/modules/GenerateModuleJavaSpec.js @@ -212,13 +212,13 @@ function translateFunctionReturnTypeToJavaType( return nullable ? 'Boolean' : 'boolean'; case 'ObjectTypeAnnotation': imports.add('com.facebook.react.bridge.WritableMap'); - return 'WritableMap'; + return wrapIntoNullableIfNeeded('WritableMap'); case 'GenericObjectTypeAnnotation': imports.add('com.facebook.react.bridge.WritableMap'); - return 'WritableMap'; + return wrapIntoNullableIfNeeded('WritableMap'); case 'ArrayTypeAnnotation': imports.add('com.facebook.react.bridge.WritableArray'); - return 'WritableArray'; + return wrapIntoNullableIfNeeded('WritableArray'); default: (realTypeAnnotation.type: empty); throw new Error(createErrorMessage(realTypeAnnotation.type)); diff --git a/packages/react-native-codegen/src/generators/modules/__test_fixtures__/fixtures.js b/packages/react-native-codegen/src/generators/modules/__test_fixtures__/fixtures.js index c7090ef8953103..48647b993c0105 100644 --- a/packages/react-native-codegen/src/generators/modules/__test_fixtures__/fixtures.js +++ b/packages/react-native-codegen/src/generators/modules/__test_fixtures__/fixtures.js @@ -655,6 +655,49 @@ const COMPLEX_OBJECTS: SchemaType = { ], }, }, + { + name: 'getNullableObject', + optional: false, + typeAnnotation: { + type: 'FunctionTypeAnnotation', + returnTypeAnnotation: { + type: 'NullableTypeAnnotation', + typeAnnotation: { + type: 'ObjectTypeAnnotation', + properties: [], + }, + }, + params: [], + }, + }, + { + name: 'getNullableGenericObject', + optional: false, + typeAnnotation: { + type: 'FunctionTypeAnnotation', + returnTypeAnnotation: { + type: 'NullableTypeAnnotation', + typeAnnotation: { + type: 'GenericObjectTypeAnnotation', + }, + }, + params: [], + }, + }, + { + name: 'getNullableArray', + optional: false, + typeAnnotation: { + type: 'FunctionTypeAnnotation', + returnTypeAnnotation: { + type: 'NullableTypeAnnotation', + typeAnnotation: { + type: 'ArrayTypeAnnotation', + }, + }, + params: [], + }, + }, ], }, moduleNames: ['SampleTurboModule'], diff --git a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleCpp-test.js.snap b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleCpp-test.js.snap index 66c423e0937b73..c4713fb7431cf1 100644 --- a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleCpp-test.js.snap +++ b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleCpp-test.js.snap @@ -61,6 +61,15 @@ static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getArrays(jsi static_cast(&turboModule)->getArrays(rt, args[0].asObject(rt)); return jsi::Value::undefined(); } +static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getNullableObject(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getNullableObject(rt); +} +static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getNullableGenericObject(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getNullableGenericObject(rt); +} +static jsi::Value __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getNullableArray(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) { + return static_cast(&turboModule)->getNullableArray(rt); +} NativeSampleTurboModuleCxxSpecJSI::NativeSampleTurboModuleCxxSpecJSI(std::shared_ptr jsInvoker) : TurboModule(\\"SampleTurboModule\\", jsInvoker) { @@ -68,6 +77,9 @@ NativeSampleTurboModuleCxxSpecJSI::NativeSampleTurboModuleCxxSpecJSI(std::shared methodMap_[\\"optionals\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_optionals}; methodMap_[\\"optionalMethod\\"] = MethodMetadata {3, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_optionalMethod}; methodMap_[\\"getArrays\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getArrays}; + methodMap_[\\"getNullableObject\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getNullableObject}; + methodMap_[\\"getNullableGenericObject\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getNullableGenericObject}; + methodMap_[\\"getNullableArray\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleCxxSpecJSI_getNullableArray}; } diff --git a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleH-test.js.snap b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleH-test.js.snap index 8db29d323c6fa4..5bbd47d7380f70 100644 --- a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleH-test.js.snap +++ b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleH-test.js.snap @@ -89,6 +89,9 @@ public: virtual void optionals(jsi::Runtime &rt, jsi::Object A) = 0; virtual void optionalMethod(jsi::Runtime &rt, jsi::Object options, jsi::Function callback, jsi::Array extras) = 0; virtual void getArrays(jsi::Runtime &rt, jsi::Object options) = 0; + virtual std::optional getNullableObject(jsi::Runtime &rt) = 0; + virtual std::optional getNullableGenericObject(jsi::Runtime &rt) = 0; + virtual std::optional getNullableArray(jsi::Runtime &rt) = 0; }; @@ -142,6 +145,30 @@ private: return bridging::callFromJs( rt, &T::getArrays, jsInvoker_, instance_, std::move(options)); } + std::optional getNullableObject(jsi::Runtime &rt) override { + static_assert( + bridging::getParameterCount(&T::getNullableObject) == 1, + \\"Expected getNullableObject(...) to have 1 parameters\\"); + + return bridging::callFromJs>( + rt, &T::getNullableObject, jsInvoker_, instance_); + } + std::optional getNullableGenericObject(jsi::Runtime &rt) override { + static_assert( + bridging::getParameterCount(&T::getNullableGenericObject) == 1, + \\"Expected getNullableGenericObject(...) to have 1 parameters\\"); + + return bridging::callFromJs>( + rt, &T::getNullableGenericObject, jsInvoker_, instance_); + } + std::optional getNullableArray(jsi::Runtime &rt) override { + static_assert( + bridging::getParameterCount(&T::getNullableArray) == 1, + \\"Expected getNullableArray(...) to have 1 parameters\\"); + + return bridging::callFromJs>( + rt, &T::getNullableArray, jsInvoker_, instance_); + } private: T *instance_; 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 8dd68c0147fcef..d17ebebe5f6644 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 @@ -192,6 +192,9 @@ namespace JS { callback:(RCTResponseSenderBlock)callback extras:(NSArray *)extras; - (void)getArrays:(JS::NativeSampleTurboModule::SpecGetArraysOptions &)options; +- (NSDictionary * _Nullable)getNullableObject; +- (NSDictionary * _Nullable)getNullableGenericObject; +- (NSArray> * _Nullable)getNullableArray; @end namespace facebook { diff --git a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleJavaSpec-test.js.snap b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleJavaSpec-test.js.snap index f6d505050f16aa..fed306c0cea645 100644 --- a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleJavaSpec-test.js.snap +++ b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleJavaSpec-test.js.snap @@ -58,8 +58,10 @@ import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.ReactModuleWithSpec; import com.facebook.react.bridge.ReadableArray; import com.facebook.react.bridge.ReadableMap; +import com.facebook.react.bridge.WritableArray; import com.facebook.react.bridge.WritableMap; import com.facebook.react.turbomodule.core.interfaces.TurboModule; +import javax.annotation.Nullable; public abstract class NativeSampleTurboModuleSpec extends ReactContextBaseJavaModule implements ReactModuleWithSpec, TurboModule { public NativeSampleTurboModuleSpec(ReactApplicationContext reactContext) { @@ -81,6 +83,18 @@ public abstract class NativeSampleTurboModuleSpec extends ReactContextBaseJavaMo @ReactMethod @DoNotStrip public abstract void getArrays(ReadableMap options); + + @ReactMethod(isBlockingSynchronousMethod = true) + @DoNotStrip + public abstract @Nullable WritableMap getNullableObject(); + + @ReactMethod(isBlockingSynchronousMethod = true) + @DoNotStrip + public abstract @Nullable WritableMap getNullableGenericObject(); + + @ReactMethod(isBlockingSynchronousMethod = true) + @DoNotStrip + public abstract @Nullable WritableArray getNullableArray(); } ", } diff --git a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleJniCpp-test.js.snap b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleJniCpp-test.js.snap index bcec9b13ef8271..efffaf9fffe7c1 100644 --- a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleJniCpp-test.js.snap +++ b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleJniCpp-test.js.snap @@ -70,12 +70,27 @@ static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getArr return static_cast(turboModule).invokeJavaMethod(rt, VoidKind, \\"getArrays\\", \\"(Lcom/facebook/react/bridge/ReadableMap;)V\\", args, count); } +static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableObject(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) { + return static_cast(turboModule).invokeJavaMethod(rt, ObjectKind, \\"getNullableObject\\", \\"()Lcom/facebook/react/bridge/WritableMap;\\", args, count); +} + +static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableGenericObject(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) { + return static_cast(turboModule).invokeJavaMethod(rt, ObjectKind, \\"getNullableGenericObject\\", \\"()Lcom/facebook/react/bridge/WritableMap;\\", args, count); +} + +static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableArray(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) { + return static_cast(turboModule).invokeJavaMethod(rt, ArrayKind, \\"getNullableArray\\", \\"()Lcom/facebook/react/bridge/WritableArray;\\", args, count); +} + NativeSampleTurboModuleSpecJSI::NativeSampleTurboModuleSpecJSI(const JavaTurboModule::InitParams ¶ms) : JavaTurboModule(params) { methodMap_[\\"difficult\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_difficult}; methodMap_[\\"optionals\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_optionals}; methodMap_[\\"optionalMethod\\"] = MethodMetadata {3, __hostFunction_NativeSampleTurboModuleSpecJSI_optionalMethod}; methodMap_[\\"getArrays\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_getArrays}; + methodMap_[\\"getNullableObject\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableObject}; + methodMap_[\\"getNullableGenericObject\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableGenericObject}; + methodMap_[\\"getNullableArray\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableArray}; } std::shared_ptr complex_objects_ModuleProvider(const std::string moduleName, const JavaTurboModule::InitParams ¶ms) { diff --git a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleMm-test.js.snap b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleMm-test.js.snap index 6e9a1acec3870f..584f1017ea5c05 100644 --- a/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleMm-test.js.snap +++ b/packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleMm-test.js.snap @@ -104,6 +104,18 @@ namespace facebook { return static_cast(turboModule).invokeObjCMethod(rt, VoidKind, \\"getArrays\\", @selector(getArrays:), args, count); } + static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableObject(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) { + return static_cast(turboModule).invokeObjCMethod(rt, ObjectKind, \\"getNullableObject\\", @selector(getNullableObject), args, count); + } + + static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableGenericObject(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) { + return static_cast(turboModule).invokeObjCMethod(rt, ObjectKind, \\"getNullableGenericObject\\", @selector(getNullableGenericObject), args, count); + } + + static facebook::jsi::Value __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableArray(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) { + return static_cast(turboModule).invokeObjCMethod(rt, ArrayKind, \\"getNullableArray\\", @selector(getNullableArray), args, count); + } + NativeSampleTurboModuleSpecJSI::NativeSampleTurboModuleSpecJSI(const ObjCTurboModule::InitParams ¶ms) : ObjCTurboModule(params) { @@ -118,6 +130,15 @@ namespace facebook { methodMap_[\\"getArrays\\"] = MethodMetadata {1, __hostFunction_NativeSampleTurboModuleSpecJSI_getArrays}; setMethodArgConversionSelector(@\\"getArrays\\", 0, @\\"JS_NativeSampleTurboModule_SpecGetArraysOptions:\\"); + + methodMap_[\\"getNullableObject\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableObject}; + + + methodMap_[\\"getNullableGenericObject\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableGenericObject}; + + + methodMap_[\\"getNullableArray\\"] = MethodMetadata {0, __hostFunction_NativeSampleTurboModuleSpecJSI_getNullableArray}; + } } // namespace react } // namespace facebook