RCTModuleMethod.mm 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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 "RCTModuleMethod.h"
  8. #import <objc/message.h>
  9. #import "RCTAssert.h"
  10. #import "RCTBridge+Private.h"
  11. #import "RCTBridge.h"
  12. #import "RCTConvert.h"
  13. #import "RCTCxxConvert.h"
  14. #import "RCTLog.h"
  15. #import "RCTManagedPointer.h"
  16. #import "RCTParserUtils.h"
  17. #import "RCTProfile.h"
  18. #import "RCTUtils.h"
  19. typedef BOOL (^RCTArgumentBlock)(RCTBridge *, NSUInteger, id);
  20. /**
  21. * Get the converter function for the specified type
  22. */
  23. static SEL selectorForType(NSString *type)
  24. {
  25. const char *input = type.UTF8String;
  26. return NSSelectorFromString([RCTParseType(&input) stringByAppendingString:@":"]);
  27. }
  28. @implementation RCTMethodArgument
  29. - (instancetype)initWithType:(NSString *)type nullability:(RCTNullability)nullability unused:(BOOL)unused
  30. {
  31. if (self = [super init]) {
  32. _type = [type copy];
  33. _nullability = nullability;
  34. _unused = unused;
  35. }
  36. return self;
  37. }
  38. @end
  39. @implementation RCTModuleMethod {
  40. Class _moduleClass;
  41. const RCTMethodInfo *_methodInfo;
  42. NSString *_JSMethodName;
  43. SEL _selector;
  44. NSInvocation *_invocation;
  45. NSArray<RCTArgumentBlock> *_argumentBlocks;
  46. NSMutableArray *_retainedObjects;
  47. }
  48. static void RCTLogArgumentError(RCTModuleMethod *method, NSUInteger index, id valueOrType, const char *issue)
  49. {
  50. RCTLogError(
  51. @"Argument %tu (%@) of %@.%s %s",
  52. index,
  53. valueOrType,
  54. RCTBridgeModuleNameForClass(method->_moduleClass),
  55. method.JSMethodName,
  56. issue);
  57. }
  58. RCT_NOT_IMPLEMENTED(-(instancetype)init)
  59. RCT_EXTERN_C_BEGIN
  60. // returns YES if the selector ends in a colon (indicating that there is at
  61. // least one argument, and maybe more selector parts) or NO if it doesn't.
  62. static BOOL RCTParseSelectorPart(const char **input, NSMutableString *selector)
  63. {
  64. NSString *selectorPart;
  65. if (RCTParseSelectorIdentifier(input, &selectorPart)) {
  66. [selector appendString:selectorPart];
  67. }
  68. RCTSkipWhitespace(input);
  69. if (RCTReadChar(input, ':')) {
  70. [selector appendString:@":"];
  71. RCTSkipWhitespace(input);
  72. return YES;
  73. }
  74. return NO;
  75. }
  76. static BOOL RCTParseUnused(const char **input)
  77. {
  78. return RCTReadString(input, "__attribute__((unused))") || RCTReadString(input, "__attribute__((__unused__))") ||
  79. RCTReadString(input, "__unused");
  80. }
  81. static RCTNullability RCTParseNullability(const char **input)
  82. {
  83. if (RCTReadString(input, "nullable")) {
  84. return RCTNullable;
  85. } else if (RCTReadString(input, "nonnull")) {
  86. return RCTNonnullable;
  87. }
  88. return RCTNullabilityUnspecified;
  89. }
  90. static RCTNullability RCTParseNullabilityPostfix(const char **input)
  91. {
  92. if (RCTReadString(input, "_Nullable") || RCTReadString(input, "__nullable")) {
  93. return RCTNullable;
  94. } else if (RCTReadString(input, "_Nonnull") || RCTReadString(input, "__nonnull")) {
  95. return RCTNonnullable;
  96. }
  97. return RCTNullabilityUnspecified;
  98. }
  99. // returns YES if execution is safe to proceed (enqueue callback invocation), NO if callback has already been invoked
  100. #if RCT_DEBUG
  101. static BOOL checkCallbackMultipleInvocations(BOOL *didInvoke)
  102. {
  103. if (*didInvoke) {
  104. RCTFatal(RCTErrorWithMessage(
  105. @"Illegal callback invocation from native module. This callback type only permits a single invocation from native code."));
  106. return NO;
  107. } else {
  108. *didInvoke = YES;
  109. return YES;
  110. }
  111. }
  112. #endif
  113. NSString *RCTParseMethodSignature(const char *input, NSArray<RCTMethodArgument *> **arguments)
  114. {
  115. RCTSkipWhitespace(&input);
  116. NSMutableArray *args;
  117. NSMutableString *selector = [NSMutableString new];
  118. while (RCTParseSelectorPart(&input, selector)) {
  119. if (!args) {
  120. args = [NSMutableArray new];
  121. }
  122. // Parse type
  123. if (RCTReadChar(&input, '(')) {
  124. RCTSkipWhitespace(&input);
  125. // 5 cases that both nullable and __unused exist
  126. // 1: foo:(nullable __unused id)foo 2: foo:(nullable id __unused)foo
  127. // 3: foo:(__unused id _Nullable)foo 4: foo:(id __unused _Nullable)foo
  128. // 5: foo:(id _Nullable __unused)foo
  129. RCTNullability nullability = RCTParseNullability(&input);
  130. RCTSkipWhitespace(&input);
  131. BOOL unused = RCTParseUnused(&input);
  132. RCTSkipWhitespace(&input);
  133. NSString *type = RCTParseType(&input);
  134. RCTSkipWhitespace(&input);
  135. if (nullability == RCTNullabilityUnspecified) {
  136. nullability = RCTParseNullabilityPostfix(&input);
  137. RCTSkipWhitespace(&input);
  138. if (!unused) {
  139. unused = RCTParseUnused(&input);
  140. RCTSkipWhitespace(&input);
  141. if (unused && nullability == RCTNullabilityUnspecified) {
  142. nullability = RCTParseNullabilityPostfix(&input);
  143. RCTSkipWhitespace(&input);
  144. }
  145. }
  146. } else if (!unused) {
  147. unused = RCTParseUnused(&input);
  148. RCTSkipWhitespace(&input);
  149. }
  150. [args addObject:[[RCTMethodArgument alloc] initWithType:type nullability:nullability unused:unused]];
  151. RCTSkipWhitespace(&input);
  152. RCTReadChar(&input, ')');
  153. RCTSkipWhitespace(&input);
  154. } else {
  155. // Type defaults to id if unspecified
  156. [args addObject:[[RCTMethodArgument alloc] initWithType:@"id" nullability:RCTNullable unused:NO]];
  157. }
  158. // Argument name
  159. RCTParseArgumentIdentifier(&input, NULL);
  160. RCTSkipWhitespace(&input);
  161. }
  162. *arguments = [args copy];
  163. return selector;
  164. }
  165. RCT_EXTERN_C_END
  166. - (instancetype)initWithExportedMethod:(const RCTMethodInfo *)exportedMethod moduleClass:(Class)moduleClass
  167. {
  168. if (self = [super init]) {
  169. _moduleClass = moduleClass;
  170. _methodInfo = exportedMethod;
  171. }
  172. return self;
  173. }
  174. - (void)processMethodSignature
  175. {
  176. NSArray<RCTMethodArgument *> *arguments;
  177. _selector = NSSelectorFromString(RCTParseMethodSignature(_methodInfo->objcName, &arguments));
  178. RCTAssert(_selector, @"%s is not a valid selector", _methodInfo->objcName);
  179. // Create method invocation
  180. NSMethodSignature *methodSignature = [_moduleClass instanceMethodSignatureForSelector:_selector];
  181. RCTAssert(methodSignature, @"%s is not a recognized Objective-C method.", sel_getName(_selector));
  182. NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSignature];
  183. invocation.selector = _selector;
  184. _invocation = invocation;
  185. NSMutableArray *retainedObjects = [NSMutableArray array];
  186. _retainedObjects = retainedObjects;
  187. // Process arguments
  188. NSUInteger numberOfArguments = methodSignature.numberOfArguments;
  189. NSMutableArray<RCTArgumentBlock> *argumentBlocks = [[NSMutableArray alloc] initWithCapacity:numberOfArguments - 2];
  190. #if RCT_DEBUG
  191. __weak RCTModuleMethod *weakSelf = self;
  192. #endif
  193. #define RCT_RETAINED_ARG_BLOCK(_logic) \
  194. [argumentBlocks addObject:^(__unused __weak RCTBridge * bridge, NSUInteger index, id json) { \
  195. _logic [invocation setArgument:&value atIndex:(index) + 2]; \
  196. if (value) { \
  197. [retainedObjects addObject:value]; \
  198. } \
  199. return YES; \
  200. }]
  201. #define __PRIMITIVE_CASE(_type, _nullable) \
  202. { \
  203. isNullableType = _nullable; \
  204. _type (*convert)(id, SEL, id) = (__typeof__(convert))objc_msgSend; \
  205. [argumentBlocks addObject:^(__unused RCTBridge * bridge, NSUInteger index, id json) { \
  206. _type value = convert([RCTConvert class], selector, json); \
  207. [invocation setArgument:&value atIndex:(index) + 2]; \
  208. return YES; \
  209. }]; \
  210. break; \
  211. }
  212. #define PRIMITIVE_CASE(_type) __PRIMITIVE_CASE(_type, NO)
  213. #define NULLABLE_PRIMITIVE_CASE(_type) __PRIMITIVE_CASE(_type, YES)
  214. // Explicitly copy the block
  215. #define __COPY_BLOCK(block...) \
  216. id value = [block copy]; \
  217. if (value) { \
  218. [retainedObjects addObject:value]; \
  219. }
  220. #if RCT_DEBUG
  221. #define BLOCK_CASE(_block_args, _block) \
  222. RCT_RETAINED_ARG_BLOCK(if (json && ![json isKindOfClass:[NSNumber class]]) { \
  223. RCTLogArgumentError(weakSelf, index, json, "should be a function"); \
  224. return NO; \
  225. } __block BOOL didInvoke = NO; \
  226. __COPY_BLOCK(^_block_args { \
  227. if (checkCallbackMultipleInvocations(&didInvoke)) \
  228. _block \
  229. });)
  230. #else
  231. #define BLOCK_CASE(_block_args, _block) \
  232. RCT_RETAINED_ARG_BLOCK(__COPY_BLOCK(^_block_args{ \
  233. _block});)
  234. #endif
  235. for (NSUInteger i = 2; i < numberOfArguments; i++) {
  236. const char *objcType = [methodSignature getArgumentTypeAtIndex:i];
  237. BOOL isNullableType = NO;
  238. RCTMethodArgument *argument = arguments[i - 2];
  239. NSString *typeName = argument.type;
  240. SEL selector = selectorForType(typeName);
  241. if ([RCTConvert respondsToSelector:selector]) {
  242. switch (objcType[0]) {
  243. // Primitives
  244. case _C_CHR:
  245. PRIMITIVE_CASE(char)
  246. case _C_UCHR:
  247. PRIMITIVE_CASE(unsigned char)
  248. case _C_SHT:
  249. PRIMITIVE_CASE(short)
  250. case _C_USHT:
  251. PRIMITIVE_CASE(unsigned short)
  252. case _C_INT:
  253. PRIMITIVE_CASE(int)
  254. case _C_UINT:
  255. PRIMITIVE_CASE(unsigned int)
  256. case _C_LNG:
  257. PRIMITIVE_CASE(long)
  258. case _C_ULNG:
  259. PRIMITIVE_CASE(unsigned long)
  260. case _C_LNG_LNG:
  261. PRIMITIVE_CASE(long long)
  262. case _C_ULNG_LNG:
  263. PRIMITIVE_CASE(unsigned long long)
  264. case _C_FLT:
  265. PRIMITIVE_CASE(float)
  266. case _C_DBL:
  267. PRIMITIVE_CASE(double)
  268. case _C_BOOL:
  269. PRIMITIVE_CASE(BOOL)
  270. case _C_SEL:
  271. NULLABLE_PRIMITIVE_CASE(SEL)
  272. case _C_CHARPTR:
  273. NULLABLE_PRIMITIVE_CASE(const char *)
  274. case _C_PTR:
  275. NULLABLE_PRIMITIVE_CASE(void *)
  276. case _C_ID: {
  277. isNullableType = YES;
  278. id (*convert)(id, SEL, id) = (__typeof__(convert))objc_msgSend;
  279. RCT_RETAINED_ARG_BLOCK(id value = convert([RCTConvert class], selector, json););
  280. break;
  281. }
  282. case _C_STRUCT_B: {
  283. NSMethodSignature *typeSignature = [RCTConvert methodSignatureForSelector:selector];
  284. NSInvocation *typeInvocation = [NSInvocation invocationWithMethodSignature:typeSignature];
  285. typeInvocation.selector = selector;
  286. typeInvocation.target = [RCTConvert class];
  287. [argumentBlocks addObject:^(__unused RCTBridge *bridge, NSUInteger index, id json) {
  288. void *returnValue = malloc(typeSignature.methodReturnLength);
  289. if (!returnValue) {
  290. // CWE - 391 : Unchecked error condition
  291. // https://www.cvedetails.com/cwe-details/391/Unchecked-Error-Condition.html
  292. // https://eli.thegreenplace.net/2009/10/30/handling-out-of-memory-conditions-in-c
  293. abort();
  294. }
  295. [typeInvocation setArgument:&json atIndex:2];
  296. [typeInvocation invoke];
  297. [typeInvocation getReturnValue:returnValue];
  298. [invocation setArgument:returnValue atIndex:index + 2];
  299. free(returnValue);
  300. return YES;
  301. }];
  302. break;
  303. }
  304. default: {
  305. static const char *blockType = @encode(__typeof__(^{
  306. }));
  307. if (!strcmp(objcType, blockType)) {
  308. BLOCK_CASE((NSArray * args), { [bridge enqueueCallback:json args:args]; });
  309. } else {
  310. RCTLogError(@"Unsupported argument type '%@' in method %@.", typeName, [self methodName]);
  311. }
  312. }
  313. }
  314. } else if ([typeName isEqualToString:@"RCTResponseSenderBlock"]) {
  315. BLOCK_CASE((NSArray * args), { [bridge enqueueCallback:json args:args]; });
  316. } else if ([typeName isEqualToString:@"RCTResponseErrorBlock"]) {
  317. BLOCK_CASE((NSError * error), { [bridge enqueueCallback:json args:@[ RCTJSErrorFromNSError(error) ]]; });
  318. } else if ([typeName isEqualToString:@"RCTPromiseResolveBlock"]) {
  319. RCTAssert(
  320. i == numberOfArguments - 2,
  321. @"The RCTPromiseResolveBlock must be the second to last parameter in %@",
  322. [self methodName]);
  323. BLOCK_CASE((id result), { [bridge enqueueCallback:json args:result ? @[ result ] : @[]]; });
  324. } else if ([typeName isEqualToString:@"RCTPromiseRejectBlock"]) {
  325. RCTAssert(
  326. i == numberOfArguments - 1, @"The RCTPromiseRejectBlock must be the last parameter in %@", [self methodName]);
  327. BLOCK_CASE((NSString * code, NSString * message, NSError * error), {
  328. NSDictionary *errorJSON = RCTJSErrorFromCodeMessageAndNSError(code, message, error);
  329. [bridge enqueueCallback:json args:@[ errorJSON ]];
  330. });
  331. } else if ([typeName hasPrefix:@"JS::"]) {
  332. NSString *selectorNameForCxxType =
  333. [[typeName stringByReplacingOccurrencesOfString:@"::" withString:@"_"] stringByAppendingString:@":"];
  334. selector = NSSelectorFromString(selectorNameForCxxType);
  335. [argumentBlocks addObject:^(__unused RCTBridge *bridge, NSUInteger index, id json) {
  336. RCTManagedPointer *(*convert)(id, SEL, id) = (__typeof__(convert))objc_msgSend;
  337. RCTManagedPointer *box = convert([RCTCxxConvert class], selector, json);
  338. void *pointer = box.voidPointer;
  339. [invocation setArgument:&pointer atIndex:index + 2];
  340. [retainedObjects addObject:box];
  341. return YES;
  342. }];
  343. } else {
  344. // Unknown argument type
  345. RCTLogError(
  346. @"Unknown argument type '%@' in method %@. Extend RCTConvert to support this type.",
  347. typeName,
  348. [self methodName]);
  349. }
  350. #if RCT_DEBUG
  351. RCTNullability nullability = argument.nullability;
  352. if (!isNullableType) {
  353. if (nullability == RCTNullable) {
  354. RCTLogArgumentError(
  355. weakSelf,
  356. i - 2,
  357. typeName,
  358. "is marked as "
  359. "nullable, but is not a nullable type.");
  360. }
  361. nullability = RCTNonnullable;
  362. }
  363. /**
  364. * Special case - Numbers are not nullable in Android, so we
  365. * don't support this for now. In future we may allow it.
  366. */
  367. if ([typeName isEqualToString:@"NSNumber"]) {
  368. BOOL unspecified = (nullability == RCTNullabilityUnspecified);
  369. if (!argument.unused && (nullability == RCTNullable || unspecified)) {
  370. RCTLogArgumentError(
  371. weakSelf,
  372. i - 2,
  373. typeName,
  374. [unspecified ? @"has unspecified nullability" : @"is marked as nullable"
  375. stringByAppendingString:@" but React requires that all NSNumber "
  376. "arguments are explicitly marked as `nonnull` to ensure "
  377. "compatibility with Android."]
  378. .UTF8String);
  379. }
  380. nullability = RCTNonnullable;
  381. }
  382. if (nullability == RCTNonnullable) {
  383. RCTArgumentBlock oldBlock = argumentBlocks[i - 2];
  384. argumentBlocks[i - 2] = ^(RCTBridge *bridge, NSUInteger index, id json) {
  385. if (json != nil) {
  386. if (!oldBlock(bridge, index, json)) {
  387. return NO;
  388. }
  389. if (isNullableType) {
  390. // Check converted value wasn't null either, as method probably
  391. // won't gracefully handle a nil value for a nonull argument
  392. void *value;
  393. [invocation getArgument:&value atIndex:index + 2];
  394. if (value == NULL) {
  395. return NO;
  396. }
  397. }
  398. return YES;
  399. }
  400. RCTLogArgumentError(weakSelf, index, typeName, "must not be null");
  401. return NO;
  402. };
  403. }
  404. #endif
  405. }
  406. #if RCT_DEBUG
  407. const char *objcType = _invocation.methodSignature.methodReturnType;
  408. if (_methodInfo->isSync && objcType[0] != _C_ID) {
  409. RCTLogError(
  410. @"Return type of %@.%s should be (id) as the method is \"sync\"",
  411. RCTBridgeModuleNameForClass(_moduleClass),
  412. self.JSMethodName);
  413. }
  414. #endif
  415. _argumentBlocks = argumentBlocks;
  416. }
  417. - (SEL)selector
  418. {
  419. if (_selector == NULL) {
  420. RCT_PROFILE_BEGIN_EVENT(
  421. RCTProfileTagAlways,
  422. @"",
  423. (@{@"module" : NSStringFromClass(_moduleClass), @"method" : @(_methodInfo->objcName)}));
  424. [self processMethodSignature];
  425. RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
  426. }
  427. return _selector;
  428. }
  429. - (const char *)JSMethodName
  430. {
  431. NSString *methodName = _JSMethodName;
  432. if (!methodName) {
  433. const char *jsName = _methodInfo->jsName;
  434. if (jsName && strlen(jsName) > 0) {
  435. methodName = @(jsName);
  436. } else {
  437. methodName = @(_methodInfo->objcName);
  438. NSRange colonRange = [methodName rangeOfString:@":"];
  439. if (colonRange.location != NSNotFound) {
  440. methodName = [methodName substringToIndex:colonRange.location];
  441. }
  442. methodName = [methodName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  443. RCTAssert(
  444. methodName.length,
  445. @"%s is not a valid JS function name, please"
  446. " supply an alternative using RCT_REMAP_METHOD()",
  447. _methodInfo->objcName);
  448. }
  449. _JSMethodName = methodName;
  450. }
  451. return methodName.UTF8String;
  452. }
  453. - (RCTFunctionType)functionType
  454. {
  455. if (strstr(_methodInfo->objcName, "RCTPromise") != NULL) {
  456. RCTAssert(!_methodInfo->isSync, @"Promises cannot be used in sync functions");
  457. return RCTFunctionTypePromise;
  458. } else if (_methodInfo->isSync) {
  459. return RCTFunctionTypeSync;
  460. } else {
  461. return RCTFunctionTypeNormal;
  462. }
  463. }
  464. - (id)invokeWithBridge:(RCTBridge *)bridge module:(id)module arguments:(NSArray *)arguments
  465. {
  466. if (_argumentBlocks == nil) {
  467. [self processMethodSignature];
  468. }
  469. #if RCT_DEBUG
  470. // Sanity check
  471. RCTAssert([module class] == _moduleClass, @"Attempted to invoke method \
  472. %@ on a module of class %@", [self methodName], [module class]);
  473. // Safety check
  474. if (arguments.count != _argumentBlocks.count) {
  475. NSInteger actualCount = arguments.count;
  476. NSInteger expectedCount = _argumentBlocks.count;
  477. // Subtract the implicit Promise resolver and rejecter functions for implementations of async functions
  478. if (self.functionType == RCTFunctionTypePromise) {
  479. actualCount -= 2;
  480. expectedCount -= 2;
  481. }
  482. RCTLogError(
  483. @"%@.%s was called with %lld arguments but expects %lld arguments. "
  484. @"If you haven\'t changed this method yourself, this usually means that "
  485. @"your versions of the native code and JavaScript code are out of sync. "
  486. @"Updating both should make this error go away.",
  487. RCTBridgeModuleNameForClass(_moduleClass),
  488. self.JSMethodName,
  489. (long long)actualCount,
  490. (long long)expectedCount);
  491. return nil;
  492. }
  493. #endif
  494. // Set arguments
  495. NSUInteger index = 0;
  496. for (id json in arguments) {
  497. RCTArgumentBlock block = _argumentBlocks[index];
  498. if (!block(bridge, index, RCTNilIfNull(json))) {
  499. // Invalid argument, abort
  500. RCTLogArgumentError(self, index, json, "could not be processed. Aborting method call.");
  501. return nil;
  502. }
  503. index++;
  504. }
  505. // Invoke method
  506. #ifdef RCT_MAIN_THREAD_WATCH_DOG_THRESHOLD
  507. if (RCTIsMainQueue()) {
  508. CFTimeInterval start = CACurrentMediaTime();
  509. [_invocation invokeWithTarget:module];
  510. CFTimeInterval duration = CACurrentMediaTime() - start;
  511. if (duration > RCT_MAIN_THREAD_WATCH_DOG_THRESHOLD) {
  512. RCTLogWarn(
  513. @"Main Thread Watchdog: Invocation of %@ blocked the main thread for %dms. "
  514. "Consider using background-threaded modules and asynchronous calls "
  515. "to spend less time on the main thread and keep the app's UI responsive.",
  516. [self methodName],
  517. (int)(duration * 1000));
  518. }
  519. } else {
  520. [_invocation invokeWithTarget:module];
  521. }
  522. #else
  523. [_invocation invokeWithTarget:module];
  524. #endif
  525. [_retainedObjects removeAllObjects];
  526. if (_methodInfo->isSync) {
  527. void *returnValue;
  528. [_invocation getReturnValue:&returnValue];
  529. return (__bridge id)returnValue;
  530. }
  531. return nil;
  532. }
  533. - (NSString *)methodName
  534. {
  535. if (!_selector) {
  536. [self processMethodSignature];
  537. }
  538. return [NSString stringWithFormat:@"-[%@ %s]", _moduleClass, sel_getName(_selector)];
  539. }
  540. - (NSString *)description
  541. {
  542. return [NSString stringWithFormat:@"<%@: %p; exports %@ as %s(); type: %s>",
  543. [self class],
  544. self,
  545. [self methodName],
  546. self.JSMethodName,
  547. RCTFunctionDescriptorFromType(self.functionType)];
  548. }
  549. @end