2017-02-01 22:10:41 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTCxxModule.h"
|
|
|
|
|
|
|
|
#import <React/RCTBridge.h>
|
2017-02-16 14:45:40 +00:00
|
|
|
#import <React/RCTFollyConvert.h>
|
|
|
|
#import <React/RCTLog.h>
|
2017-02-01 22:10:41 +00:00
|
|
|
#import <cxxreact/CxxModule.h>
|
|
|
|
|
|
|
|
#import "RCTCxxMethod.h"
|
2017-02-16 14:45:40 +00:00
|
|
|
|
|
|
|
using namespace facebook::react;
|
2017-02-01 22:10:41 +00:00
|
|
|
|
|
|
|
@implementation RCTCxxModule
|
|
|
|
{
|
|
|
|
std::unique_ptr<facebook::xplat::module::CxxModule> _module;
|
|
|
|
}
|
|
|
|
|
2017-03-17 13:55:43 +00:00
|
|
|
+ (NSString *)moduleName
|
2017-02-01 22:10:41 +00:00
|
|
|
{
|
2017-03-17 13:55:43 +00:00
|
|
|
return @"";
|
2017-02-01 22:10:41 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 13:55:43 +00:00
|
|
|
- (void)lazyInit
|
2017-02-01 22:10:41 +00:00
|
|
|
{
|
2017-03-17 13:55:43 +00:00
|
|
|
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());
|
|
|
|
}
|
2017-02-01 22:10:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-17 13:55:43 +00:00
|
|
|
- (std::unique_ptr<facebook::xplat::module::CxxModule>)createModule
|
2017-02-01 22:10:41 +00:00
|
|
|
{
|
2017-03-17 13:55:43 +00:00
|
|
|
RCTAssert(NO, @"Subclass %@ must override createModule", [self class]);
|
|
|
|
return nullptr;
|
2017-02-01 22:10:41 +00:00
|
|
|
}
|
|
|
|
|
2017-03-17 13:55:43 +00:00
|
|
|
- (NSArray<id<RCTBridgeMethod>> *)methodsToExport;
|
2017-02-01 22:10:41 +00:00
|
|
|
{
|
2017-03-17 13:55:43 +00:00
|
|
|
[self lazyInit];
|
|
|
|
if (!_module) {
|
|
|
|
return nil;
|
|
|
|
}
|
2017-02-01 22:10:41 +00:00
|
|
|
|
|
|
|
NSMutableArray *moduleMethods = [NSMutableArray new];
|
|
|
|
for (const auto &method : _module->getMethods()) {
|
|
|
|
[moduleMethods addObject:[[RCTCxxMethod alloc] initWithCxxMethod:method]];
|
|
|
|
}
|
|
|
|
return moduleMethods;
|
|
|
|
}
|
|
|
|
|
2017-03-17 13:55:43 +00:00
|
|
|
- (NSDictionary<NSString *, id> *)constantsToExport;
|
2017-02-01 22:10:41 +00:00
|
|
|
{
|
2017-03-17 13:55:43 +00:00
|
|
|
[self lazyInit];
|
|
|
|
if (!_module) {
|
|
|
|
return nil;
|
|
|
|
}
|
2017-02-01 22:10:41 +00:00
|
|
|
|
|
|
|
NSMutableDictionary *moduleConstants = [NSMutableDictionary new];
|
|
|
|
for (const auto &c : _module->getConstants()) {
|
2017-02-16 14:45:40 +00:00
|
|
|
moduleConstants[@(c.first.c_str())] = convertFollyDynamicToId(c.second);
|
2017-02-01 22:10:41 +00:00
|
|
|
}
|
|
|
|
return moduleConstants;
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|