Skip to content

Commit

Permalink
Replace exported method registration with statically allocated struct
Browse files Browse the repository at this point in the history
Reviewed By: fromcelticpark

Differential Revision: D5389383

fbshipit-source-id: 9eb29b254b616574966b43ad24aa880d44589652
  • Loading branch information
javache authored and facebook-github-bot committed Jul 24, 2017
1 parent d94f3e4 commit cb12080
Show file tree
Hide file tree
Showing 15 changed files with 161 additions and 143 deletions.
21 changes: 11 additions & 10 deletions Libraries/RCTTest/RCTTestRunner.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
#define FB_REFERENCE_IMAGE_DIR ""
#endif

#define RCT_RUN_RUNLOOP_WHILE(CONDITION) \
{ \
NSDate *timeout = [NSDate dateWithTimeIntervalSinceNow:5]; \
while ((CONDITION)) { \
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]]; \
if ([timeout timeIntervalSinceNow] <= 0) { \
XCTFail(@"Runloop timed out before condition was met"); \
break; \
} \
} \
#define RCT_RUN_RUNLOOP_WHILE(CONDITION) \
{ \
NSDate *timeout = [NSDate dateWithTimeIntervalSinceNow:5]; \
NSRunLoop *runloop = [NSRunLoop mainRunLoop]; \
while ((CONDITION)) { \
[runloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.01]]; \
if ([timeout timeIntervalSinceNow] <= 0) { \
XCTFail(@"Runloop timed out before condition was met"); \
break; \
} \
} \
}

/**
Expand Down
22 changes: 12 additions & 10 deletions RNTester/RNTesterUnitTests/RCTAllocationTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,7 @@ - (void)testModulesAreInvalidated
AllocationTestModule *module = [AllocationTestModule new];
@autoreleasepool {
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:_bundleURL
moduleProvider:^{
return @[module];
}
moduleProvider:^{ return @[module]; }
launchOptions:nil];
XCTAssertTrue(module.isValid, @"AllocationTestModule should be valid");
(void)bridge;
Expand All @@ -130,12 +128,10 @@ - (void)testModulesAreDeallocated
@autoreleasepool {
AllocationTestModule *module = [AllocationTestModule new];
RCTBridge *bridge = [[RCTBridge alloc] initWithBundleURL:_bundleURL
moduleProvider:^{
return @[module];
}
moduleProvider:^{ return @[module]; }
launchOptions:nil];
XCTAssertNotNil(module, @"AllocationTestModule should have been created");
weakModule = module;
XCTAssertNotNil(weakModule, @"AllocationTestModule should have been created");
(void)bridge;
}

Expand All @@ -145,11 +141,18 @@ - (void)testModulesAreDeallocated

- (void)testModuleMethodsAreDeallocated
{
static RCTMethodInfo methodInfo = {
.objcName = "test:(NSString *)a :(nonnull NSNumber *)b :(RCTResponseSenderBlock)c :(RCTResponseErrorBlock)d",
.jsName = "",
.isSync = false
};

__weak RCTModuleMethod *weakMethod;
@autoreleasepool {
__autoreleasing RCTModuleMethod *method = [[RCTModuleMethod alloc] initWithMethodSignature:@"test:(NSString *)a :(nonnull NSNumber *)b :(RCTResponseSenderBlock)c :(RCTResponseErrorBlock)d" JSMethodName:@"" isSync:NO moduleClass:[AllocationTestModule class]];
weakMethod = method;
__autoreleasing RCTModuleMethod *method = [[RCTModuleMethod alloc] initWithExportedMethod:&methodInfo
moduleClass:[AllocationTestModule class]];
XCTAssertNotNil(method, @"RCTModuleMethod should have been created");
weakMethod = method;
}

RCT_RUN_RUNLOOP_WHILE(weakMethod)
Expand All @@ -172,7 +175,6 @@ - (void)testContentViewIsInvalidated
#if !TARGET_OS_TV // userInteractionEnabled is true for Apple TV views
XCTAssertFalse(rootContentView.userInteractionEnabled, @"RCTContentView should have been invalidated");
#endif

}

- (void)testUnderlyingBridgeIsDeallocated
Expand Down
30 changes: 15 additions & 15 deletions RNTester/RNTesterUnitTests/RCTMethodArgumentTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ @interface RCTMethodArgumentTests : XCTestCase

@implementation RCTMethodArgumentTests

extern SEL RCTParseMethodSignature(NSString *methodSignature, NSArray **argTypes);
extern SEL RCTParseMethodSignature(const char *methodSignature, NSArray **argTypes);

- (void)testOneArgument
{
NSArray *arguments;
NSString *methodSignature = @"foo:(NSInteger)foo";
const char *methodSignature = "foo:(NSInteger)foo";
SEL selector = RCTParseMethodSignature(methodSignature, &arguments);
XCTAssertEqualObjects(NSStringFromSelector(selector), @"foo:");
XCTAssertEqual(arguments.count, (NSUInteger)1);
Expand All @@ -34,7 +34,7 @@ - (void)testOneArgument
- (void)testTwoArguments
{
NSArray *arguments;
NSString *methodSignature = @"foo:(NSInteger)foo bar:(BOOL)bar";
const char *methodSignature = "foo:(NSInteger)foo bar:(BOOL)bar";
SEL selector = RCTParseMethodSignature(methodSignature, &arguments);
XCTAssertEqualObjects(NSStringFromSelector(selector), @"foo:bar:");
XCTAssertEqual(arguments.count, (NSUInteger)2);
Expand All @@ -45,7 +45,7 @@ - (void)testTwoArguments
- (void)testSpaces
{
NSArray *arguments;
NSString *methodSignature = @"foo : (NSInteger)foo bar : (BOOL) bar";
const char *methodSignature = "foo : (NSInteger)foo bar : (BOOL) bar";
SEL selector = RCTParseMethodSignature(methodSignature, &arguments);
XCTAssertEqualObjects(NSStringFromSelector(selector), @"foo:bar:");
XCTAssertEqual(arguments.count, (NSUInteger)2);
Expand All @@ -56,7 +56,7 @@ - (void)testSpaces
- (void)testNewlines
{
NSArray *arguments;
NSString *methodSignature = @"foo : (NSInteger)foo\nbar : (BOOL) bar";
const char *methodSignature = "foo : (NSInteger)foo\nbar : (BOOL) bar";
SEL selector = RCTParseMethodSignature(methodSignature, &arguments);
XCTAssertEqualObjects(NSStringFromSelector(selector), @"foo:bar:");
XCTAssertEqual(arguments.count, (NSUInteger)2);
Expand All @@ -67,7 +67,7 @@ - (void)testNewlines
- (void)testUnnamedArgs
{
NSArray *arguments;
NSString *methodSignature = @"foo:(NSInteger)foo:(BOOL)bar";
const char *methodSignature = "foo:(NSInteger)foo:(BOOL)bar";
SEL selector = RCTParseMethodSignature(methodSignature, &arguments);
XCTAssertEqualObjects(NSStringFromSelector(selector), @"foo::");
XCTAssertEqual(arguments.count, (NSUInteger)2);
Expand All @@ -78,7 +78,7 @@ - (void)testUnnamedArgs
- (void)testUntypedUnnamedArgs
{
NSArray *arguments;
NSString *methodSignature = @"foo:foo:bar:bar";
const char *methodSignature = "foo:foo:bar:bar";
SEL selector = RCTParseMethodSignature(methodSignature, &arguments);
XCTAssertEqualObjects(NSStringFromSelector(selector), @"foo:::");
XCTAssertEqual(arguments.count, (NSUInteger)3);
Expand All @@ -90,7 +90,7 @@ - (void)testUntypedUnnamedArgs
- (void)testAttributes
{
NSArray *arguments;
NSString *methodSignature = @"foo:(__attribute__((unused)) NSString *)foo bar:(__unused BOOL)bar";
const char *methodSignature = "foo:(__attribute__((unused)) NSString *)foo bar:(__unused BOOL)bar";
SEL selector = RCTParseMethodSignature(methodSignature, &arguments);
XCTAssertEqualObjects(NSStringFromSelector(selector), @"foo:bar:");
XCTAssertEqual(arguments.count, (NSUInteger)2);
Expand All @@ -101,7 +101,7 @@ - (void)testAttributes
- (void)testNullability
{
NSArray *arguments;
NSString *methodSignature = @"foo:(nullable NSString *)foo bar:(nonnull NSNumber *)bar baz:(id)baz";
const char *methodSignature = "foo:(nullable NSString *)foo bar:(nonnull NSNumber *)bar baz:(id)baz";
SEL selector = RCTParseMethodSignature(methodSignature, &arguments);
XCTAssertEqualObjects(NSStringFromSelector(selector), @"foo:bar:baz:");
XCTAssertEqual(arguments.count, (NSUInteger)3);
Expand All @@ -116,7 +116,7 @@ - (void)testNullability
- (void)testSemicolonStripping
{
NSArray *arguments;
NSString *methodSignature = @"foo:(NSString *)foo bar:(BOOL)bar;";
const char *methodSignature = "foo:(NSString *)foo bar:(BOOL)bar;";
SEL selector = RCTParseMethodSignature(methodSignature, &arguments);
XCTAssertEqualObjects(NSStringFromSelector(selector), @"foo:bar:");
XCTAssertEqual(arguments.count, (NSUInteger)2);
Expand All @@ -127,7 +127,7 @@ - (void)testSemicolonStripping
- (void)testUnused
{
NSArray *arguments;
NSString *methodSignature = @"foo:(__unused NSString *)foo bar:(NSNumber *)bar";
const char *methodSignature = "foo:(__unused NSString *)foo bar:(NSNumber *)bar";
SEL selector = RCTParseMethodSignature(methodSignature, &arguments);
XCTAssertEqualObjects(NSStringFromSelector(selector), @"foo:bar:");
XCTAssertEqual(arguments.count, (NSUInteger)2);
Expand All @@ -140,7 +140,7 @@ - (void)testUnused
- (void)testGenericArray
{
NSArray *arguments;
NSString *methodSignature = @"foo:(NSArray<NSString *> *)foo;";
const char *methodSignature = "foo:(NSArray<NSString *> *)foo;";
SEL selector = RCTParseMethodSignature(methodSignature, &arguments);
XCTAssertEqualObjects(NSStringFromSelector(selector), @"foo:");
XCTAssertEqual(arguments.count, (NSUInteger)1);
Expand All @@ -150,7 +150,7 @@ - (void)testGenericArray
- (void)testNestedGenericArray
{
NSArray *arguments;
NSString *methodSignature = @"foo:(NSArray<NSArray<NSString *> *> *)foo;";
const char *methodSignature = "foo:(NSArray<NSArray<NSString *> *> *)foo;";
SEL selector = RCTParseMethodSignature(methodSignature, &arguments);
XCTAssertEqualObjects(NSStringFromSelector(selector), @"foo:");
XCTAssertEqual(arguments.count, (NSUInteger)1);
Expand All @@ -160,7 +160,7 @@ - (void)testNestedGenericArray
- (void)testGenericSet
{
NSArray *arguments;
NSString *methodSignature = @"foo:(NSSet<NSNumber *> *)foo;";
const char *methodSignature = "foo:(NSSet<NSNumber *> *)foo;";
SEL selector = RCTParseMethodSignature(methodSignature, &arguments);
XCTAssertEqualObjects(NSStringFromSelector(selector), @"foo:");
XCTAssertEqual(arguments.count, (NSUInteger)1);
Expand All @@ -170,7 +170,7 @@ - (void)testGenericSet
- (void)testGenericDictionary
{
NSArray *arguments;
NSString *methodSignature = @"foo:(NSDictionary<NSString *, NSNumber *> *)foo;";
const char *methodSignature = "foo:(NSDictionary<NSString *, NSNumber *> *)foo;";
SEL selector = RCTParseMethodSignature(methodSignature, &arguments);
XCTAssertEqualObjects(NSStringFromSelector(selector), @"foo:");
XCTAssertEqual(arguments.count, (NSUInteger)1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,18 @@ @implementation RCTModuleMethodTests
CGRect _s;
}

static RCTModuleMethod *buildDefaultMethodWithMethodSignature(NSString *methodSignature) {
return [[RCTModuleMethod alloc] initWithMethodSignature:methodSignature
JSMethodName:nil
isSync:NO
moduleClass:[RCTModuleMethodTests class]];
static RCTModuleMethod *buildDefaultMethodWithMethodSignature(const char *methodSignature)
{
// This leaks a RCTMethodInfo, but it's a test, so...
RCTMethodInfo *methodInfo = new RCTMethodInfo {.objcName = methodSignature, .isSync = NO};
return [[RCTModuleMethod alloc] initWithExportedMethod:methodInfo moduleClass:[RCTModuleMethodTests class]];
}

static RCTModuleMethod *buildSyncMethodWithMethodSignature(NSString *methodSignature) {
return [[RCTModuleMethod alloc] initWithMethodSignature:methodSignature
JSMethodName:nil
isSync:YES
moduleClass:[RCTModuleMethodTests class]];
static RCTModuleMethod *buildSyncMethodWithMethodSignature(const char *methodSignature)
{
// This leaks a RCTMethodInfo, but it's a test, so...
RCTMethodInfo *methodInfo = new RCTMethodInfo {.objcName = methodSignature, .isSync = YES};
return [[RCTModuleMethod alloc] initWithExportedMethod:methodInfo moduleClass:[RCTModuleMethodTests class]];
}

+ (NSString *)moduleName { return nil; }
Expand All @@ -62,7 +62,7 @@ - (id)methodThatReturnsNil { return nil; }

- (void)testNonnull
{
NSString *methodSignature = @"doFooWithBar:(nonnull NSString *)bar";
const char *methodSignature = "doFooWithBar:(nonnull NSString *)bar";
RCTModuleMethod *method = buildDefaultMethodWithMethodSignature(methodSignature);
XCTAssertFalse(RCTLogsError(^{
[method invokeWithBridge:nil module:self arguments:@[@"Hello World"]];
Expand All @@ -85,31 +85,31 @@ - (void)testNumbersNonnull
{
// Specifying an NSNumber param without nonnull isn't allowed
XCTAssertTrue(RCTLogsError(^{
NSString *methodSignature = @"doFooWithNumber:(NSNumber *)n";
const char *methodSignature = "doFooWithNumber:(NSNumber *)n";
RCTModuleMethod *method = buildDefaultMethodWithMethodSignature(methodSignature);
// Invoke method to trigger parsing
[method invokeWithBridge:nil module:self arguments:@[@1]];
}));
}

{
NSString *methodSignature = @"doFooWithNumber:(nonnull NSNumber *)n";
const char *methodSignature = "doFooWithNumber:(nonnull NSNumber *)n";
RCTModuleMethod *method = buildDefaultMethodWithMethodSignature(methodSignature);
XCTAssertTrue(RCTLogsError(^{
[method invokeWithBridge:nil module:self arguments:@[[NSNull null]]];
}));
}

{
NSString *methodSignature = @"doFooWithDouble:(double)n";
const char *methodSignature = "doFooWithDouble:(double)n";
RCTModuleMethod *method = buildDefaultMethodWithMethodSignature(methodSignature);
XCTAssertTrue(RCTLogsError(^{
[method invokeWithBridge:nil module:self arguments:@[[NSNull null]]];
}));
}

{
NSString *methodSignature = @"doFooWithInteger:(NSInteger)n";
const char *methodSignature = "doFooWithInteger:(NSInteger)n";
RCTModuleMethod *method = buildDefaultMethodWithMethodSignature(methodSignature);
XCTAssertTrue(RCTLogsError(^{
[method invokeWithBridge:nil module:self arguments:@[[NSNull null]]];
Expand All @@ -119,7 +119,7 @@ - (void)testNumbersNonnull

- (void)testStructArgument
{
NSString *methodSignature = @"doFooWithCGRect:(CGRect)s";
const char *methodSignature = "doFooWithCGRect:(CGRect)s";
RCTModuleMethod *method = buildDefaultMethodWithMethodSignature(methodSignature);

CGRect r = CGRectMake(10, 20, 30, 40);
Expand All @@ -129,7 +129,7 @@ - (void)testStructArgument

- (void)testWhitespaceTolerance
{
NSString *methodSignature = @"doFoo : \t (NSString *)foo";
const char *methodSignature = "doFoo : \t (NSString *)foo";

__block RCTModuleMethod *method;
XCTAssertFalse(RCTLogsError(^{
Expand All @@ -146,19 +146,19 @@ - (void)testWhitespaceTolerance
- (void)testFunctionType
{
{
NSString *methodSignature = @"doFoo";
const char *methodSignature = "doFoo";
RCTModuleMethod *method = buildDefaultMethodWithMethodSignature(methodSignature);
XCTAssertTrue(method.functionType == RCTFunctionTypeNormal);
}

{
NSString *methodSignature = @"openURL:(NSURL *)URL resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject";
const char *methodSignature = "openURL:(NSURL *)URL resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject";
RCTModuleMethod *method = buildDefaultMethodWithMethodSignature(methodSignature);
XCTAssertTrue(method.functionType == RCTFunctionTypePromise);
}

{
NSString *methodSignature = @"echoString:(NSString *)input";
const char *methodSignature = "echoString:(NSString *)input";
RCTModuleMethod *method = buildSyncMethodWithMethodSignature(methodSignature);
XCTAssertTrue(method.functionType == RCTFunctionTypeSync);
}
Expand All @@ -167,14 +167,14 @@ - (void)testFunctionType
- (void)testReturnsValueForSyncFunction
{
{
NSString *methodSignature = @"echoString:(NSString *)input";
const char *methodSignature = "echoString:(NSString *)input";
RCTModuleMethod *method = buildSyncMethodWithMethodSignature(methodSignature);
id result = [method invokeWithBridge:nil module:self arguments:@[@"Test String Value"]];
XCTAssertEqualObjects(result, @"Test String Value");
}

{
NSString *methodSignature = @"methodThatReturnsNil";
const char *methodSignature = "methodThatReturnsNil";
RCTModuleMethod *method = buildSyncMethodWithMethodSignature(methodSignature);
id result = [method invokeWithBridge:nil module:self arguments:@[]];
XCTAssertNil(result);
Expand All @@ -183,7 +183,7 @@ - (void)testReturnsValueForSyncFunction

- (void)testReturnsNilForDefaultFunction
{
NSString *methodSignature = @"doFoo";
const char *methodSignature = "doFoo";
RCTModuleMethod *method = buildDefaultMethodWithMethodSignature(methodSignature);
id result = [method invokeWithBridge:nil module:self arguments:@[]];
XCTAssertNil(result);
Expand All @@ -192,7 +192,7 @@ - (void)testReturnsNilForDefaultFunction
- (void)testReturnTypeForSyncFunction
{
{
NSString *methodSignature = @"methodThatReturnsNil";
const char *methodSignature = "methodThatReturnsNil";
RCTModuleMethod *method = buildSyncMethodWithMethodSignature(methodSignature);
XCTAssertFalse(RCTLogsError(^{
// Invoke method to trigger parsing
Expand All @@ -201,7 +201,7 @@ - (void)testReturnTypeForSyncFunction
}

{
NSString *methodSignature = @"doFoo";
const char *methodSignature = "doFoo";
RCTModuleMethod *method = buildSyncMethodWithMethodSignature(methodSignature);
XCTAssertTrue(RCTLogsError(^{
// Invoke method to trigger parsing
Expand Down
2 changes: 1 addition & 1 deletion React/Base/RCTBatchedBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ - (id)callNativeModule:(NSUInteger)moduleID
}

NSString *message = [NSString stringWithFormat:
@"Exception '%@' was thrown while invoking %@ on target %@ with params %@",
@"Exception '%@' was thrown while invoking %s on target %@ with params %@",
exception, method.JSMethodName, moduleData.name, params];
RCTFatal(RCTErrorWithMessage(message));
return nil;
Expand Down
2 changes: 1 addition & 1 deletion React/Base/RCTBridgeMethod.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ static inline const char *RCTFunctionDescriptorFromType(RCTFunctionType type) {

@protocol RCTBridgeMethod <NSObject>

@property (nonatomic, copy, readonly) NSString *JSMethodName;
@property (nonatomic, readonly) const char *JSMethodName;
@property (nonatomic, readonly) RCTFunctionType functionType;

- (id)invokeWithBridge:(RCTBridge *)bridge
Expand Down
Loading

0 comments on commit cb12080

Please sign in to comment.