Skip to content

Commit

Permalink
Prepare the bridge for C++
Browse files Browse the repository at this point in the history
Reviewed By: @nicklockwood

Differential Revision: D2432291
  • Loading branch information
mhorowitz authored and facebook-github-bot-8 committed Sep 18, 2015
1 parent 18a7e36 commit a87ba4a
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 23 deletions.
15 changes: 7 additions & 8 deletions React/Base/RCTBatchedBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#import "RCTLog.h"
#import "RCTModuleData.h"
#import "RCTModuleMap.h"
#import "RCTModuleMethod.h"
#import "RCTBridgeMethod.h"
#import "RCTPerformanceLogger.h"
#import "RCTPerfStats.h"
#import "RCTProfile.h"
Expand Down Expand Up @@ -767,7 +767,7 @@ - (BOOL)_handleRequestNumber:(NSUInteger)i
return NO;
}

RCTModuleMethod *method = moduleData.methods[methodID];
id<RCTBridgeMethod> method = moduleData.methods[methodID];
if (RCT_DEBUG && !method) {
RCTLogError(@"Unknown methodID: %zd for module: %zd (%@)", methodID, moduleID, moduleData.name);
return NO;
Expand All @@ -783,12 +783,11 @@ - (BOOL)_handleRequestNumber:(NSUInteger)i
}
}

RCTProfileEndEvent(0, @"objc_call", @{
@"module": NSStringFromClass(method.moduleClass),
@"method": method.JSMethodName,
@"selector": NSStringFromSelector(method.selector),
@"args": RCTJSONStringify(RCTNullIfNil(params), NULL),
});
NSMutableDictionary *args = [method.profileArgs mutableCopy];
[args setValue:method.JSMethodName forKey:@"method"];
[args setValue:RCTJSONStringify(RCTNullIfNil(params), NULL) forKey:@"args"];

RCTProfileEndEvent(0, @"objc_call", args);

return YES;
}
Expand Down
29 changes: 29 additions & 0 deletions React/Base/RCTBridgeMethod.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* 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 <Foundation/Foundation.h>

@class RCTBridge;

typedef NS_ENUM(NSUInteger, RCTFunctionType) {
RCTFunctionTypeNormal,
RCTFunctionTypePromise,
};

@protocol RCTBridgeMethod <NSObject>

@property (nonatomic, copy, readonly) NSString *JSMethodName;
@property (nonatomic, copy, readonly) NSDictionary *profileArgs;
@property (nonatomic, readonly) RCTFunctionType functionType;

- (void)invokeWithBridge:(RCTBridge *)bridge
module:(id)module
arguments:(NSArray *)arguments;

@end
18 changes: 12 additions & 6 deletions React/Base/RCTBridgeModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,21 @@ RCT_EXTERN void RCTRegisterModule(Class); \
#define RCT_EXTERN_REMAP_METHOD(js_name, method) \
+ (NSArray *)RCT_CONCAT(__rct_export__, RCT_CONCAT(js_name, RCT_CONCAT(__LINE__, __COUNTER__))) { \
return @[@#js_name, @#method]; \
} \
}

/**
* Injects methods into JS. Entries in this array are used in addition to any
* methods defined using the macros above. This method is called only once,
* before registration.
*/
- (NSArray *)methodsToExport;

/**
* Injects constants into JS. These constants are made accessible via
* NativeModules.ModuleName.X. This method is called when the module is
* registered by the bridge. It is only called once for the lifetime of the
* bridge, so it is not suitable for returning dynamic values, but may be
* used for long-lived values such as session keys, that are regenerated only
* as part of a reload of the entire React application.
* NativeModules.ModuleName.X. It is only called once for the lifetime of the
* bridge, so it is not suitable for returning dynamic values, but may be used
* for long-lived values such as session keys, that are regenerated only as
* part of a reload of the entire React application.
*/
- (NSDictionary *)constantsToExport;

Expand Down
9 changes: 7 additions & 2 deletions React/Base/RCTModuleData.m
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ - (NSArray *)methods
{
if (!_methods) {
NSMutableArray *moduleMethods = [NSMutableArray new];

if ([_instance respondsToSelector:@selector(methodsToExport)]) {
[moduleMethods addObjectsFromArray:[_instance methodsToExport]];
}

unsigned int methodCount;
Method *methods = class_copyMethodList(object_getClass(_moduleClass), &methodCount);

Expand All @@ -58,7 +63,7 @@ - (NSArray *)methods
if ([NSStringFromSelector(selector) hasPrefix:@"__rct_export__"]) {
IMP imp = method_getImplementation(method);
NSArray *entries = ((NSArray *(*)(id, SEL))imp)(_moduleClass, selector);
RCTModuleMethod *moduleMethod =
id<RCTBridgeMethod> moduleMethod =
[[RCTModuleMethod alloc] initWithObjCMethodName:entries[1]
JSMethodName:entries[0]
moduleClass:_moduleClass];
Expand All @@ -84,7 +89,7 @@ - (NSDictionary *)config
}

NSMutableDictionary *methodconfig = [NSMutableDictionary new];
[self.methods enumerateObjectsUsingBlock:^(RCTModuleMethod *method, NSUInteger idx, __unused BOOL *stop) {
[self.methods enumerateObjectsUsingBlock:^(id<RCTBridgeMethod> method, NSUInteger idx, __unused BOOL *stop) {
methodconfig[method.JSMethodName] = @{
@"methodID": @(idx),
@"type": method.functionType == RCTFunctionTypePromise ? @"remoteAsync" : @"remote",
Expand Down
10 changes: 3 additions & 7 deletions React/Base/RCTModuleMethod.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@

#import <Foundation/Foundation.h>

@class RCTBridge;
#import "RCTBridgeMethod.h"

typedef NS_ENUM(NSUInteger, RCTFunctionType) {
RCTFunctionTypeNormal,
RCTFunctionTypePromise,
};
@class RCTBridge;

typedef NS_ENUM(NSUInteger, RCTNullability) {
RCTNullabilityUnspecified,
Expand All @@ -30,9 +27,8 @@ typedef NS_ENUM(NSUInteger, RCTNullability) {

@end

@interface RCTModuleMethod : NSObject
@interface RCTModuleMethod : NSObject <RCTBridgeMethod>

@property (nonatomic, copy, readonly) NSString *JSMethodName;
@property (nonatomic, readonly) Class moduleClass;
@property (nonatomic, readonly) SEL selector;
@property (nonatomic, readonly) RCTFunctionType functionType;
Expand Down
16 changes: 16 additions & 0 deletions React/Base/RCTModuleMethod.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,11 @@ @implementation RCTModuleMethod
NSArray *_argumentBlocks;
NSString *_objCMethodName;
SEL _selector;
NSDictionary *_profileArgs;
}

@synthesize JSMethodName = _JSMethodName;

static void RCTLogArgumentError(RCTModuleMethod *method, NSUInteger index,
id valueOrType, const char *issue)
{
Expand Down Expand Up @@ -370,6 +373,19 @@ - (SEL)selector
return _selector;
}

- (NSDictionary *)profileArgs
{
if (_profileArgs) {
// This sets _selector
[self processMethodSignature];
_profileArgs = @{
@"module": NSStringFromClass(_moduleClass),
@"selector": NSStringFromSelector(_selector),
};
}
return _profileArgs;
}

- (void)invokeWithBridge:(RCTBridge *)bridge
module:(id)module
arguments:(NSArray *)arguments
Expand Down

0 comments on commit a87ba4a

Please sign in to comment.