mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-01-12 22:50:10 +08:00
Summary:
TurboModules depend on a getConstants method. Existing ObjectiveC modules do not have this method. Therefore, I moved the contents of `constantsToExport` to `getConstants` and then had `constantsToExports` call `getConstants`.
facebook
Since all NativeModules will eventually need to be migrated to the TurboModule system, I didn't restrict this to just the NativeModules in Marketplace.
```
const fs = require('fs');
if (process.argv.length < 3) {
throw new Error('Expected a file containing a list of native modules as the third param');
}
function read(filename) {
return fs.readFileSync(filename, 'utf8');
}
const nativeModuleFilenames = read(process.argv[2]).split('\n').filter(Boolean);
nativeModuleFilenames.forEach((fileName) => {
if (fileName.endsWith('.h')) {
return;
}
const absPath = `${process.env.HOME}/${fileName}`;
const fileSource = read(absPath);
if (/(\n|^)-\s*\((.+)\)getConstants/.test(fileSource)) {
return;
}
const constantsToExportRegex = /(\n|^)-\s*\((.+)\)constantsToExport/;
const result = constantsToExportRegex.exec(fileSource);
if (result == null) {
throw new Error(`Didn't find a constantsToExport function inside NativeModule ${fileName}`);
}
const returnType = result[2];
const newFileSource = fileSource.replace(
constantsToExportRegex,
'$1- ($2)constantsToExport\n' +
'{\n' +
` return ${returnType.includes('ModuleConstants') ? '($2)' : ''}[self getConstants];\n` +
'}\n' +
'\n' +
'- ($2)getConstants'
);
fs.writeFileSync(absPath, newFileSource);
});
```
```
> xbgs -l ')constantsToExport'
```
Reviewed By: fkgozali
Differential Revision: D13951197
fbshipit-source-id: 394a319d42aff466c56a3d748e17c335307a8f47
87 lines
1.8 KiB
Plaintext
87 lines
1.8 KiB
Plaintext
/**
|
|
* 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.
|
|
*/
|
|
|
|
#import "RCTCxxModule.h"
|
|
|
|
#import <React/RCTBridge.h>
|
|
#import <React/RCTFollyConvert.h>
|
|
#import <React/RCTLog.h>
|
|
#import <cxxreact/CxxModule.h>
|
|
|
|
#import "RCTCxxMethod.h"
|
|
|
|
using namespace facebook::react;
|
|
|
|
@implementation RCTCxxModule
|
|
{
|
|
std::unique_ptr<facebook::xplat::module::CxxModule> _module;
|
|
}
|
|
|
|
+ (NSString *)moduleName
|
|
{
|
|
return @"";
|
|
}
|
|
|
|
+ (BOOL)requiresMainQueueSetup
|
|
{
|
|
return NO;
|
|
}
|
|
|
|
- (void)lazyInit
|
|
{
|
|
if (!_module) {
|
|
_module = [self createModule];
|
|
|
|
if (_module) {
|
|
RCTAssert([RCTBridgeModuleNameForClass([self class]) isEqualToString:@(_module->getName().c_str())],
|
|
@"CxxModule class name %@ does not match runtime name %s",
|
|
RCTBridgeModuleNameForClass([self class]), _module->getName().c_str());
|
|
}
|
|
}
|
|
}
|
|
|
|
- (std::unique_ptr<facebook::xplat::module::CxxModule>)createModule
|
|
{
|
|
RCTAssert(NO, @"Subclass %@ must override createModule", [self class]);
|
|
return nullptr;
|
|
}
|
|
|
|
- (NSArray<id<RCTBridgeMethod>> *)methodsToExport;
|
|
{
|
|
[self lazyInit];
|
|
if (!_module) {
|
|
return nil;
|
|
}
|
|
|
|
NSMutableArray *moduleMethods = [NSMutableArray new];
|
|
for (const auto &method : _module->getMethods()) {
|
|
[moduleMethods addObject:[[RCTCxxMethod alloc] initWithCxxMethod:method]];
|
|
}
|
|
return moduleMethods;
|
|
}
|
|
|
|
- (NSDictionary<NSString *, id> *)constantsToExport
|
|
{
|
|
return [self getConstants];
|
|
}
|
|
|
|
- (NSDictionary<NSString *, id> *)getConstants;
|
|
{
|
|
[self lazyInit];
|
|
if (!_module) {
|
|
return nil;
|
|
}
|
|
|
|
NSMutableDictionary *moduleConstants = [NSMutableDictionary new];
|
|
for (const auto &c : _module->getConstants()) {
|
|
moduleConstants[@(c.first.c_str())] = convertFollyDynamicToId(c.second);
|
|
}
|
|
return moduleConstants;
|
|
}
|
|
|
|
@end
|