Skip to content

Commit

Permalink
Start using getConstants
Browse files Browse the repository at this point in the history
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
  • Loading branch information
RSNara authored and facebook-github-bot committed Feb 5, 2019
1 parent ffc9908 commit f370933
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Libraries/Blob/RCTBlobManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ + (BOOL)requiresMainQueueSetup
}

- (NSDictionary<NSString *, id> *)constantsToExport
{
return [self getConstants];
}

- (NSDictionary<NSString *, id> *)getConstants
{
return @{
@"BLOB_URI_SCHEME": kBlobURIScheme,
Expand Down
5 changes: 5 additions & 0 deletions Libraries/Settings/RCTSettingsManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ - (void)dealloc
}

- (NSDictionary<NSString *, id> *)constantsToExport
{
return [self getConstants];
}

- (NSDictionary<NSString *, id> *)getConstants
{
return @{@"settings": RCTJSONClean([_defaults dictionaryRepresentation])};
}
Expand Down
5 changes: 5 additions & 0 deletions RNTester/RNTesterUnitTests/RCTModuleInitTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ @implementation RCTTestExportConstantsModule
RCT_EXPORT_MODULE()

- (NSDictionary<NSString *, id> *)constantsToExport
{
return [self getConstants];
}

- (NSDictionary<NSString *, id> *)getConstants
{
_exportedConstants = YES;
_exportedConstantsOnMainQueue = RCTIsMainQueue();
Expand Down
5 changes: 5 additions & 0 deletions React/Base/RCTPlatform.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ + (BOOL)requiresMainQueueSetup
}

- (NSDictionary<NSString *, id> *)constantsToExport
{
return [self getConstants];
}

- (NSDictionary<NSString *, id> *)getConstants
{
UIDevice *device = [UIDevice currentDevice];
return @{
Expand Down
7 changes: 6 additions & 1 deletion React/CxxModule/RCTCxxModule.mm
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@ - (void)lazyInit
return moduleMethods;
}

- (NSDictionary<NSString *, id> *)constantsToExport;
- (NSDictionary<NSString *, id> *)constantsToExport
{
return [self getConstants];
}

- (NSDictionary<NSString *, id> *)getConstants;
{
[self lazyInit];
if (!_module) {
Expand Down
5 changes: 5 additions & 0 deletions React/Modules/RCTAppState.m
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ - (dispatch_queue_t)methodQueue
}

- (NSDictionary *)constantsToExport
{
return [self getConstants];
}

- (NSDictionary *)getConstants
{
return @{@"initialAppState": RCTCurrentAppBackgroundState()};
}
Expand Down
5 changes: 5 additions & 0 deletions React/Modules/RCTDeviceInfo.m
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ - (void)invalidate
}

- (NSDictionary<NSString *, id> *)constantsToExport
{
return [self getConstants];
}

- (NSDictionary<NSString *, id> *)getConstants
{
return @{
@"Dimensions": RCTExportedDimensions(_bridge),
Expand Down
5 changes: 5 additions & 0 deletions React/Modules/RCTI18nManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ + (BOOL)requiresMainQueueSetup
}

- (NSDictionary *)constantsToExport
{
return [self getConstants];
}

- (NSDictionary *)getConstants
{
return @{
@"isRTL": @([[RCTI18nUtil sharedInstance] isRTL]),
Expand Down
5 changes: 5 additions & 0 deletions React/Modules/RCTSourceCode.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ + (BOOL)requiresMainQueueSetup
}

- (NSDictionary<NSString *, id> *)constantsToExport
{
return [self getConstants];
}

- (NSDictionary<NSString *, id> *)getConstants
{
return @{
@"scriptURL": self.bridge.bundleURL.absoluteString ?: @"",
Expand Down
5 changes: 5 additions & 0 deletions React/Modules/RCTUIManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,11 @@ static void RCTMeasureLayout(RCTShadowView *view,
}

- (NSDictionary<NSString *, id> *)constantsToExport
{
return [self getConstants];
}

- (NSDictionary<NSString *, id> *)getConstants
{
NSMutableDictionary<NSString *, NSDictionary *> *constants = [NSMutableDictionary new];
NSMutableDictionary<NSString *, NSDictionary *> *directEvents = [NSMutableDictionary new];
Expand Down

0 comments on commit f370933

Please sign in to comment.