RCTCxxModule.mm 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright (c) Facebook, Inc. and its affiliates.
  3. *
  4. * This source code is licensed under the MIT license found in the
  5. * LICENSE file in the root directory of this source tree.
  6. */
  7. #import "RCTCxxModule.h"
  8. #import <React/RCTBridge.h>
  9. #import <React/RCTFollyConvert.h>
  10. #import <React/RCTLog.h>
  11. #import <cxxreact/CxxModule.h>
  12. #import "RCTCxxMethod.h"
  13. using namespace facebook::react;
  14. @implementation RCTCxxModule {
  15. std::unique_ptr<facebook::xplat::module::CxxModule> _module;
  16. }
  17. + (NSString *)moduleName
  18. {
  19. return @"";
  20. }
  21. + (BOOL)requiresMainQueueSetup
  22. {
  23. return NO;
  24. }
  25. - (void)lazyInit
  26. {
  27. if (!_module) {
  28. _module = [self createModule];
  29. if (_module) {
  30. RCTAssert(
  31. [RCTBridgeModuleNameForClass([self class]) isEqualToString:@(_module->getName().c_str())],
  32. @"CxxModule class name %@ does not match runtime name %s",
  33. RCTBridgeModuleNameForClass([self class]),
  34. _module->getName().c_str());
  35. }
  36. }
  37. }
  38. - (std::unique_ptr<facebook::xplat::module::CxxModule>)createModule
  39. {
  40. RCTAssert(NO, @"Subclass %@ must override createModule", [self class]);
  41. return nullptr;
  42. }
  43. - (NSArray<id<RCTBridgeMethod>> *)methodsToExport
  44. {
  45. [self lazyInit];
  46. if (!_module) {
  47. return nil;
  48. }
  49. NSMutableArray *moduleMethods = [NSMutableArray new];
  50. for (const auto &method : _module->getMethods()) {
  51. [moduleMethods addObject:[[RCTCxxMethod alloc] initWithCxxMethod:method]];
  52. }
  53. return moduleMethods;
  54. }
  55. - (NSDictionary<NSString *, id> *)constantsToExport
  56. {
  57. return [self getConstants];
  58. }
  59. - (NSDictionary<NSString *, id> *)getConstants
  60. {
  61. [self lazyInit];
  62. if (!_module) {
  63. return nil;
  64. }
  65. NSMutableDictionary *moduleConstants = [NSMutableDictionary new];
  66. for (const auto &c : _module->getConstants()) {
  67. moduleConstants[@(c.first.c_str())] = convertFollyDynamicToId(c.second);
  68. }
  69. return moduleConstants;
  70. }
  71. @end