Support optional types for C++ TurboModules

Summary:
Update C++ TurboModule codegen to wrap nullable types in `std::optional` whereas before the conversion would cause a crash.

Changelog:
Internal

Reviewed By: mdvacca, nlutsenko

Differential Revision: D35299708

fbshipit-source-id: 7daa50fe8b16879c5b3a55a633aa3f724dc5be30
This commit is contained in:
Scott Kyle
2022-04-01 16:58:52 -07:00
committed by Facebook GitHub Bot
parent 370f1ca0cd
commit 6e0fa5f15e
5 changed files with 73 additions and 19 deletions
@@ -105,19 +105,26 @@ function serializeArg(
index: number,
resolveAlias: AliasResolver,
): string {
function wrap(suffix) {
return `args[${index}]${suffix}`;
}
const {typeAnnotation: nullableTypeAnnotation} = arg;
const [typeAnnotation] = unwrapNullable<NativeModuleParamTypeAnnotation>(
nullableTypeAnnotation,
);
const [typeAnnotation, nullable] =
unwrapNullable<NativeModuleParamTypeAnnotation>(nullableTypeAnnotation);
let realTypeAnnotation = typeAnnotation;
if (realTypeAnnotation.type === 'TypeAliasTypeAnnotation') {
realTypeAnnotation = resolveAlias(realTypeAnnotation.name);
}
function wrap(suffix) {
const val = `args[${index}]`;
const expression = `${val}${suffix}`;
if (nullable) {
return `${val}.isNull() || ${val}.isUndefined() ? std::nullopt : std::make_optional(${expression})`;
}
return expression;
}
switch (realTypeAnnotation.type) {
case 'ReservedTypeAnnotation':
switch (realTypeAnnotation.name) {