Skip to content

Commit

Permalink
Import folly and adjust whitespace to match old codegen
Browse files Browse the repository at this point in the history
Summary:
Import folly to handle optionals (`folly::Optional<__type__>`)

Sort modules and indent generated code to match output from the old codegen. While not strictly necessary as these are generated files that should not be edited by hand, I found that matching the old codegen in this regard made it less of a chore when it came to comparing the output of both codebases.

Changelog: [Internal]

Reviewed By: RSNara

Differential Revision: D21395231

fbshipit-source-id: 289d617d7a2d93724456c80afea57a49c108cb9b
  • Loading branch information
hramos authored and facebook-github-bot committed May 5, 2020
1 parent de7f69a commit e8060ae
Show file tree
Hide file tree
Showing 4 changed files with 296 additions and 222 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ const {

type FilesOutput = Map<string, string>;

const moduleTemplate = `
class JSI_EXPORT Native::_MODULE_NAME_::SpecJSI : public ObjCTurboModule {
public:
Native::_MODULE_NAME_::SpecJSI(const ObjCTurboModule::InitParams &params);
};`;
const moduleTemplate = ` /**
* ObjC++ class for module '::_MODULE_NAME_::'
*/
class JSI_EXPORT Native::_MODULE_NAME_::SpecJSI : public ObjCTurboModule {
public:
Native::_MODULE_NAME_::SpecJSI(const ObjCTurboModule::InitParams &params);
};`;

const protocolTemplate = `
::_STRUCTS_::
const protocolTemplate = `::_STRUCTS_::
@protocol Native::_MODULE_NAME_::Spec <RCTBridgeModule, RCTTurboModule>
::_MODULE_PROPERTIES_::
Expand All @@ -53,30 +54,36 @@ const template = `
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* ${'@'}generated by codegen project: GenerateModuleHObjCpp.js
*/
// NOTE: This entire file should be codegen'ed.
#ifndef __cplusplus
#error This file must be compiled as Obj-C++. If you are importing it, you must change your file extension to .mm.
#endif
#import <vector>
#import <Foundation/Foundation.h>
#import <React/RCTBridgeModule.h>
#import <folly/Optional.h>
#import <ReactCommon/RCTTurboModule.h>
#import <RCTRequired/RCTRequired.h>
#import <RCTTypeSafety/RCTConvertHelpers.h>
#import <RCTTypeSafety/RCTTypedModuleConstants.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTCxxConvert.h>
#import <React/RCTManagedPointer.h>
#import <RCTTypeSafety/RCTConvertHelpers.h>
#import <ReactCommon/RCTTurboModule.h>
::_PROTOCOLS_::
namespace facebook {
namespace react {
namespace react {
::_MODULES_::
} // namespace react
} // namespace react
} // namespace facebook
`;

Expand Down Expand Up @@ -184,6 +191,7 @@ module.exports = {
moduleSpecName: string,
): FilesOutput {
const nativeModules = Object.keys(schema.modules)
.sort()
.map(moduleName => {
const modules = schema.modules[moduleName].nativeModules;
if (modules == null) {
Expand All @@ -200,6 +208,7 @@ module.exports = {
.join('\n');

const protocols = Object.keys(nativeModules)
.sort()
.map(name => {
const objectForGeneratingStructs: Array<ObjectForGeneratingStructs> = [];
const {properties} = nativeModules[name];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,56 +18,57 @@ const {flatObjects} = require('./ObjCppUtils/Utils');
type FilesOutput = Map<string, string>;

const propertyHeaderTemplate =
'static facebook::jsi::Value __hostFunction_Native::_MODULE_NAME_::SpecJSI_::_PROPERTY_NAME_::(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {';
' static facebook::jsi::Value __hostFunction_Native::_MODULE_NAME_::SpecJSI_::_PROPERTY_NAME_::(facebook::jsi::Runtime& rt, TurboModule &turboModule, const facebook::jsi::Value* args, size_t count) {';

const propertyCastTemplate = `static_cast<ObjCTurboModule &>(turboModule)
.invokeObjCMethod(rt, ::_KIND_::, "::_PROPERTY_NAME_::", @selector(::_PROPERTY_NAME_::::_ARGS_::), args, count);`;

const propertyTemplate = `
${propertyHeaderTemplate}
return ${propertyCastTemplate}
}`.trim();
return ${propertyCastTemplate}
}`;

const proprertyDefTemplate =
' methodMap_["::_PROPERTY_NAME_::"] = MethodMetadata {::_ARGS_COUNT_::, __hostFunction_Native::_MODULE_NAME_::SpecJSI_::_PROPERTY_NAME_::};';
const propertyDefTemplate =
' methodMap_["::_PROPERTY_NAME_::"] = MethodMetadata {::_ARGS_COUNT_::, __hostFunction_Native::_MODULE_NAME_::SpecJSI_::_PROPERTY_NAME_::};';

const moduleTemplate = `
::_TURBOMODULE_METHOD_INVOKERS_::
::_TURBOMODULE_METHOD_INVOKERS_::
Native::_MODULE_NAME_::SpecJSI::Native::_MODULE_NAME_::SpecJSI(const ObjCTurboModule::InitParams &params)
: ObjCTurboModule(params) {
::_PROPERTIES_MAP_::::_CONVERSION_SELECTORS_::
}`.trim();
Native::_MODULE_NAME_::SpecJSI::Native::_MODULE_NAME_::SpecJSI(const ObjCTurboModule::InitParams &params)
: ObjCTurboModule(params) {
::_PROPERTIES_MAP_::::_CONVERSION_SELECTORS_::
}`.trim();

const getterTemplate = `
@implementation RCTCxxConvert (Native::_MODULE_NAME_::_Spec::_GETTER_NAME_::)
@implementation RCTCxxConvert (Native::_MODULE_NAME_::_Spec::_GETTER_NAME_::)
+ (RCTManagedPointer *)JS_Native::_MODULE_NAME_::_Spec::_GETTER_NAME_:::(id)json
{
return facebook::react::managedPointer<JS::Native::_MODULE_NAME_::::Spec::_GETTER_NAME_::>(json);
}
@end
`.trim();
`;

const argConvertionTemplate =
'\n setMethodArgConversionSelector(@"::_ARG_NAME_::", ::_ARG_NUMBER_::, @"JS_Native::_MODULE_NAME_::_Spec::_SELECTOR_NAME_:::");';
'\n setMethodArgConversionSelector(@"::_ARG_NAME_::", ::_ARG_NUMBER_::, @"JS_Native::_MODULE_NAME_::_Spec::_SELECTOR_NAME_:::");';

const template = `
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* ${'@'}generated by codegen project: GenerateModuleMm.js
*/
#include <::_INCLUDE_::>
#import <folly/Optional.h>
::_GETTERS_::
namespace facebook {
namespace react {
namespace react {
::_MODULES_::
} // namespace react
} // namespace react
} // namespace facebook
`;

Expand Down Expand Up @@ -242,7 +243,7 @@ module.exports = {
returnTypeAnnotation.properties &&
returnTypeAnnotation.properties.length === 0
? ''
: proprertyDefTemplate
: propertyDefTemplate
.replace(/::_PROPERTY_NAME_::/g, propertyName)
.replace(/::_ARGS_COUNT_::/g, params.length.toString()),
)
Expand Down
Loading

0 comments on commit e8060ae

Please sign in to comment.