mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Support optional return types
Summary: This fixes an issue in the C++ TurboModule codegen where optional return types would result in a compiler error because they were not being defaulted to `null` when converting to `jsi::Value`. Changelog: Internal Reviewed By: javache Differential Revision: D36989312 fbshipit-source-id: 525f9ce7a5638ba5a655fa69ba9647978030ab0b
This commit is contained in:
committed by
Facebook GitHub Bot
parent
47bd78f64f
commit
68e4e91bd4
@@ -17,6 +17,7 @@ import type {
|
||||
NativeModulePropertyShape,
|
||||
NativeModuleFunctionTypeAnnotation,
|
||||
NativeModuleParamTypeAnnotation,
|
||||
NativeModuleTypeAnnotation,
|
||||
} from '../../CodegenSchema';
|
||||
|
||||
import type {AliasResolver} from './Utils';
|
||||
@@ -28,21 +29,33 @@ type FilesOutput = Map<string, string>;
|
||||
const HostFunctionTemplate = ({
|
||||
hasteModuleName,
|
||||
methodName,
|
||||
isVoid,
|
||||
returnTypeAnnotation,
|
||||
args,
|
||||
}: $ReadOnly<{
|
||||
hasteModuleName: string,
|
||||
methodName: string,
|
||||
isVoid: boolean,
|
||||
returnTypeAnnotation: Nullable<NativeModuleTypeAnnotation>,
|
||||
args: Array<string>,
|
||||
}>) => {
|
||||
const isNullable = returnTypeAnnotation.type === 'NullableTypeAnnotation';
|
||||
const isVoid = returnTypeAnnotation.type === 'VoidTypeAnnotation';
|
||||
const methodCallArgs = ['rt', ...args].join(', ');
|
||||
const methodCall = `static_cast<${hasteModuleName}CxxSpecJSI *>(&turboModule)->${methodName}(${methodCallArgs});`;
|
||||
const methodCall = `static_cast<${hasteModuleName}CxxSpecJSI *>(&turboModule)->${methodName}(${methodCallArgs})`;
|
||||
|
||||
return `static jsi::Value __hostFunction_${hasteModuleName}CxxSpecJSI_${methodName}(jsi::Runtime &rt, TurboModule &turboModule, const jsi::Value* args, size_t count) {${
|
||||
isVoid ? `\n ${methodCall}` : ''
|
||||
isVoid
|
||||
? `\n ${methodCall};`
|
||||
: isNullable
|
||||
? `\n auto result = ${methodCall};`
|
||||
: ''
|
||||
}
|
||||
return ${isVoid ? 'jsi::Value::undefined();' : methodCall}
|
||||
return ${
|
||||
isVoid
|
||||
? 'jsi::Value::undefined()'
|
||||
: isNullable
|
||||
? 'result ? jsi::Value(std::move(*result)) : jsi::Value::null()'
|
||||
: methodCall
|
||||
};
|
||||
}`;
|
||||
};
|
||||
|
||||
@@ -173,13 +186,11 @@ function serializePropertyIntoHostFunction(
|
||||
): string {
|
||||
const [propertyTypeAnnotation] =
|
||||
unwrapNullable<NativeModuleFunctionTypeAnnotation>(property.typeAnnotation);
|
||||
const isVoid =
|
||||
propertyTypeAnnotation.returnTypeAnnotation.type === 'VoidTypeAnnotation';
|
||||
|
||||
return HostFunctionTemplate({
|
||||
hasteModuleName,
|
||||
methodName: property.name,
|
||||
isVoid,
|
||||
returnTypeAnnotation: propertyTypeAnnotation.returnTypeAnnotation,
|
||||
args: propertyTypeAnnotation.params.map((p, i) =>
|
||||
serializeArg(p, i, resolveAlias),
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user