2015-03-23 20:28:42 +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.
|
|
|
|
*/
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
#import "RCTBridge.h"
|
|
|
|
|
|
|
|
#import <dlfcn.h>
|
|
|
|
#import <objc/message.h>
|
|
|
|
#import <objc/runtime.h>
|
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
#import <mach-o/dyld.h>
|
|
|
|
#import <mach-o/getsect.h>
|
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
#import "RCTContextExecutor.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
#import "RCTConvert.h"
|
|
|
|
#import "RCTEventDispatcher.h"
|
2015-04-02 14:33:21 +00:00
|
|
|
#import "RCTJavaScriptLoader.h"
|
|
|
|
#import "RCTKeyCommands.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
#import "RCTLog.h"
|
2015-04-07 14:36:26 +00:00
|
|
|
#import "RCTRedBox.h"
|
2015-04-02 14:33:21 +00:00
|
|
|
#import "RCTRootView.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
#import "RCTSparseArray.h"
|
|
|
|
#import "RCTUtils.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Must be kept in sync with `MessageQueue.js`.
|
|
|
|
*/
|
|
|
|
typedef NS_ENUM(NSUInteger, RCTBridgeFields) {
|
|
|
|
RCTBridgeFieldRequestModuleIDs = 0,
|
|
|
|
RCTBridgeFieldMethodIDs,
|
|
|
|
RCTBridgeFieldParamss,
|
|
|
|
RCTBridgeFieldResponseCBIDs,
|
|
|
|
RCTBridgeFieldResponseReturnValues,
|
|
|
|
RCTBridgeFieldFlushDateMillis
|
|
|
|
};
|
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
NSString *const RCTReloadBridge = @"RCTReloadBridge";
|
|
|
|
|
2015-04-07 14:36:26 +00:00
|
|
|
NSString *RCTBridgeModuleNameForClass(Class cls)
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
|
|
|
return [cls respondsToSelector:@selector(moduleName)] ? [cls moduleName] : NSStringFromClass(cls);
|
|
|
|
}
|
|
|
|
|
2015-02-24 17:06:57 +00:00
|
|
|
/**
|
|
|
|
* This function scans all classes available at runtime and returns an array
|
|
|
|
* of all JSMethods registered.
|
|
|
|
*/
|
|
|
|
static NSArray *RCTJSMethods(void)
|
|
|
|
{
|
|
|
|
static NSArray *JSMethods;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
NSMutableSet *uniqueMethods = [NSMutableSet set];
|
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
RCTEnumerateClasses(^(__unsafe_unretained Class cls) {
|
2015-02-24 17:06:57 +00:00
|
|
|
if (RCTClassOverridesClassMethod(cls, @selector(JSMethods))) {
|
|
|
|
[uniqueMethods addObjectsFromArray:[cls JSMethods]];
|
|
|
|
}
|
2015-03-01 23:33:55 +00:00
|
|
|
});
|
2015-02-24 17:06:57 +00:00
|
|
|
|
|
|
|
JSMethods = [uniqueMethods allObjects];
|
|
|
|
});
|
|
|
|
|
|
|
|
return JSMethods;
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
|
|
|
* This function scans all classes available at runtime and returns an array
|
|
|
|
* of all classes that implement the RTCBridgeModule protocol.
|
|
|
|
*/
|
|
|
|
static NSArray *RCTModuleNamesByID;
|
|
|
|
static NSArray *RCTBridgeModuleClassesByModuleID(void)
|
|
|
|
{
|
|
|
|
static NSArray *modules;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
modules = [NSMutableArray array];
|
|
|
|
RCTModuleNamesByID = [NSMutableArray array];
|
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
RCTEnumerateClasses(^(__unsafe_unretained Class cls) {
|
|
|
|
if ([cls conformsToProtocol:@protocol(RCTBridgeModule)]) {
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
// Add module
|
|
|
|
[(NSMutableArray *)modules addObject:cls];
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
// Add module name
|
2015-04-07 14:36:26 +00:00
|
|
|
NSString *moduleName = RCTBridgeModuleNameForClass(cls);
|
2015-03-01 23:33:55 +00:00
|
|
|
[(NSMutableArray *)RCTModuleNamesByID addObject:moduleName];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
2015-03-01 23:33:55 +00:00
|
|
|
});
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
modules = [modules copy];
|
|
|
|
RCTModuleNamesByID = [RCTModuleNamesByID copy];
|
|
|
|
});
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
return modules;
|
|
|
|
}
|
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
@interface RCTBridge ()
|
|
|
|
|
|
|
|
- (void)_invokeAndProcessModule:(NSString *)module
|
|
|
|
method:(NSString *)method
|
|
|
|
arguments:(NSArray *)args;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This private class is used as a container for exported method info
|
|
|
|
*/
|
|
|
|
@interface RCTModuleMethod : NSObject
|
|
|
|
|
|
|
|
@property (nonatomic, copy, readonly) NSString *moduleClassName;
|
|
|
|
@property (nonatomic, copy, readonly) NSString *JSMethodName;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation RCTModuleMethod
|
|
|
|
{
|
|
|
|
BOOL _isClassMethod;
|
|
|
|
Class _moduleClass;
|
|
|
|
SEL _selector;
|
|
|
|
NSMethodSignature *_methodSignature;
|
|
|
|
NSArray *_argumentBlocks;
|
|
|
|
NSString *_methodName;
|
|
|
|
}
|
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
static Class _globalExecutorClass;
|
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
- (instancetype)initWithMethodName:(NSString *)methodName
|
|
|
|
JSMethodName:(NSString *)JSMethodName
|
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
|
|
|
|
|
|
|
_methodName = methodName;
|
|
|
|
NSArray *parts = [[methodName substringWithRange:NSMakeRange(2, methodName.length - 3)] componentsSeparatedByString:@" "];
|
|
|
|
|
|
|
|
// Parse class and method
|
|
|
|
_moduleClassName = parts[0];
|
|
|
|
NSRange categoryRange = [_moduleClassName rangeOfString:@"("];
|
|
|
|
if (categoryRange.length)
|
|
|
|
{
|
|
|
|
_moduleClassName = [_moduleClassName substringToIndex:categoryRange.location];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract class and method details
|
|
|
|
_isClassMethod = [methodName characterAtIndex:0] == '+';
|
|
|
|
_moduleClass = NSClassFromString(_moduleClassName);
|
|
|
|
_selector = NSSelectorFromString(parts[1]);
|
|
|
|
_JSMethodName = JSMethodName ?: [NSStringFromSelector(_selector) componentsSeparatedByString:@":"][0];
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
|
|
|
// Sanity check
|
|
|
|
RCTAssert([_moduleClass conformsToProtocol:@protocol(RCTBridgeModule)],
|
|
|
|
@"You are attempting to export the method %@, but %@ does not \
|
|
|
|
conform to the RCTBridgeModule Protocol", methodName, _moduleClassName);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Get method signature
|
|
|
|
_methodSignature = _isClassMethod ?
|
|
|
|
[_moduleClass methodSignatureForSelector:_selector] :
|
|
|
|
[_moduleClass instanceMethodSignatureForSelector:_selector];
|
|
|
|
|
|
|
|
// Process arguments
|
|
|
|
NSUInteger numberOfArguments = _methodSignature.numberOfArguments;
|
|
|
|
NSMutableArray *argumentBlocks = [[NSMutableArray alloc] initWithCapacity:numberOfArguments - 2];
|
|
|
|
for (NSUInteger i = 2; i < numberOfArguments; i++) {
|
|
|
|
const char *argumentType = [_methodSignature getArgumentTypeAtIndex:i];
|
|
|
|
switch (argumentType[0]) {
|
|
|
|
|
|
|
|
#define RCT_ARG_BLOCK(_logic) \
|
|
|
|
[argumentBlocks addObject:^(RCTBridge *bridge, NSInvocation *invocation, NSUInteger index, id json) { \
|
|
|
|
_logic \
|
|
|
|
[invocation setArgument:&value atIndex:index]; \
|
|
|
|
}]; \
|
|
|
|
|
|
|
|
#define RCT_CASE(_value, _class, _logic) \
|
|
|
|
case _value: { \
|
|
|
|
RCT_ARG_BLOCK( \
|
|
|
|
if (json && ![json isKindOfClass:[_class class]]) { \
|
|
|
|
RCTLogError(@"Argument %tu (%@) of %@.%@ should be of type %@", index, \
|
2015-04-07 14:36:26 +00:00
|
|
|
json, RCTBridgeModuleNameForClass(_moduleClass), _JSMethodName, [_class class]); \
|
2015-03-01 23:33:55 +00:00
|
|
|
return; \
|
|
|
|
} \
|
|
|
|
_logic \
|
|
|
|
) \
|
|
|
|
break; \
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_CASE(':', NSString, SEL value = NSSelectorFromString(json); );
|
|
|
|
RCT_CASE('*', NSString, const char *value = [json UTF8String]; );
|
|
|
|
|
|
|
|
#define RCT_SIMPLE_CASE(_value, _type, _selector) \
|
|
|
|
case _value: { \
|
|
|
|
RCT_ARG_BLOCK( \
|
|
|
|
if (json && ![json respondsToSelector:@selector(_selector)]) { \
|
|
|
|
RCTLogError(@"Argument %tu (%@) of %@.%@ does not respond to selector: %@", \
|
2015-04-07 14:36:26 +00:00
|
|
|
index, json, RCTBridgeModuleNameForClass(_moduleClass), _JSMethodName, @#_selector); \
|
2015-03-01 23:33:55 +00:00
|
|
|
return; \
|
|
|
|
} \
|
|
|
|
_type value = [json _selector]; \
|
|
|
|
) \
|
|
|
|
break; \
|
|
|
|
}
|
|
|
|
|
|
|
|
RCT_SIMPLE_CASE('c', char, charValue)
|
|
|
|
RCT_SIMPLE_CASE('C', unsigned char, unsignedCharValue)
|
|
|
|
RCT_SIMPLE_CASE('s', short, shortValue)
|
|
|
|
RCT_SIMPLE_CASE('S', unsigned short, unsignedShortValue)
|
|
|
|
RCT_SIMPLE_CASE('i', int, intValue)
|
|
|
|
RCT_SIMPLE_CASE('I', unsigned int, unsignedIntValue)
|
|
|
|
RCT_SIMPLE_CASE('l', long, longValue)
|
|
|
|
RCT_SIMPLE_CASE('L', unsigned long, unsignedLongValue)
|
|
|
|
RCT_SIMPLE_CASE('q', long long, longLongValue)
|
|
|
|
RCT_SIMPLE_CASE('Q', unsigned long long, unsignedLongLongValue)
|
|
|
|
RCT_SIMPLE_CASE('f', float, floatValue)
|
|
|
|
RCT_SIMPLE_CASE('d', double, doubleValue)
|
|
|
|
RCT_SIMPLE_CASE('B', BOOL, boolValue)
|
|
|
|
|
|
|
|
default: {
|
|
|
|
static const char *blockType = @encode(typeof(^{}));
|
|
|
|
if (!strcmp(argumentType, blockType)) {
|
|
|
|
RCT_ARG_BLOCK(
|
|
|
|
if (json && ![json isKindOfClass:[NSNumber class]]) {
|
|
|
|
RCTLogError(@"Argument %tu (%@) of %@.%@ should be a number", index,
|
2015-04-07 14:36:26 +00:00
|
|
|
json, RCTBridgeModuleNameForClass(_moduleClass), _JSMethodName);
|
2015-03-01 23:33:55 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Marked as autoreleasing, because NSInvocation doesn't retain arguments
|
|
|
|
__autoreleasing id value = (json ? ^(NSArray *args) {
|
|
|
|
[bridge _invokeAndProcessModule:@"BatchedBridge"
|
|
|
|
method:@"invokeCallbackAndReturnFlushedQueue"
|
|
|
|
arguments:@[json, args]];
|
|
|
|
} : ^(NSArray *unused) {});
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
RCT_ARG_BLOCK( id value = json; )
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_argumentBlocks = [argumentBlocks copy];
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invokeWithBridge:(RCTBridge *)bridge
|
|
|
|
module:(id)module
|
|
|
|
arguments:(NSArray *)arguments
|
|
|
|
{
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
|
|
|
// Sanity check
|
|
|
|
RCTAssert([module class] == _moduleClass, @"Attempted to invoke method \
|
|
|
|
%@ on a module of class %@", _methodName, [module class]);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Safety check
|
|
|
|
if (arguments.count != _argumentBlocks.count) {
|
|
|
|
RCTLogError(@"%@.%@ was called with %zd arguments, but expects %zd",
|
2015-04-07 14:36:26 +00:00
|
|
|
RCTBridgeModuleNameForClass(_moduleClass), _JSMethodName,
|
2015-03-01 23:33:55 +00:00
|
|
|
arguments.count, _argumentBlocks.count);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create invocation (we can't re-use this as it wouldn't be thread-safe)
|
|
|
|
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:_methodSignature];
|
|
|
|
[invocation setArgument:&_selector atIndex:1];
|
|
|
|
|
|
|
|
// Set arguments
|
|
|
|
NSUInteger index = 0;
|
|
|
|
for (id json in arguments) {
|
|
|
|
id arg = (json == [NSNull null]) ? nil : json;
|
|
|
|
void (^block)(RCTBridge *, NSInvocation *, NSUInteger, id) = _argumentBlocks[index];
|
|
|
|
block(bridge, invocation, index + 2, arg);
|
|
|
|
index ++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invoke method
|
|
|
|
[invocation invokeWithTarget:_isClassMethod ? [module class] : module];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)description
|
|
|
|
{
|
|
|
|
return [NSString stringWithFormat:@"<%@: %p; exports %@ as %@;>", NSStringFromClass(self.class), self, _methodName, _JSMethodName];
|
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
|
|
|
* This function parses the exported methods inside RCTBridgeModules and
|
|
|
|
* generates an array of arrays of RCTModuleMethod objects, keyed
|
|
|
|
* by module index.
|
|
|
|
*/
|
|
|
|
static RCTSparseArray *RCTExportedMethodsByModuleID(void)
|
|
|
|
{
|
|
|
|
static RCTSparseArray *methodsByModuleID;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
|
|
|
|
Dl_info info;
|
|
|
|
dladdr(&RCTExportedMethodsByModuleID, &info);
|
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
#ifdef __LP64__
|
|
|
|
typedef uint64_t RCTExportValue;
|
|
|
|
typedef struct section_64 RCTExportSection;
|
|
|
|
#define RCTGetSectByNameFromHeader getsectbynamefromheader_64
|
|
|
|
#else
|
|
|
|
typedef uint32_t RCTExportValue;
|
|
|
|
typedef struct section RCTExportSection;
|
|
|
|
#define RCTGetSectByNameFromHeader getsectbynamefromheader
|
|
|
|
#endif
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
const RCTExportValue mach_header = (RCTExportValue)info.dli_fbase;
|
|
|
|
const RCTExportSection *section = RCTGetSectByNameFromHeader((void *)mach_header, "__DATA", "RCTExport");
|
|
|
|
|
|
|
|
if (section == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSArray *classes = RCTBridgeModuleClassesByModuleID();
|
|
|
|
NSMutableDictionary *methodsByModuleClassName = [NSMutableDictionary dictionaryWithCapacity:[classes count]];
|
|
|
|
|
|
|
|
for (RCTExportValue addr = section->offset;
|
|
|
|
addr < section->offset + section->size;
|
2015-03-01 23:33:55 +00:00
|
|
|
addr += sizeof(const char **) * 2) {
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
// Get data entry
|
|
|
|
const char **entries = (const char **)(mach_header + addr);
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
// Create method
|
2015-02-20 04:10:52 +00:00
|
|
|
RCTModuleMethod *moduleMethod =
|
2015-03-01 23:33:55 +00:00
|
|
|
[[RCTModuleMethod alloc] initWithMethodName:@(entries[0])
|
|
|
|
JSMethodName:strlen(entries[1]) ? @(entries[1]) : nil];
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
// Cache method
|
|
|
|
NSArray *methods = methodsByModuleClassName[moduleMethod.moduleClassName];
|
|
|
|
methodsByModuleClassName[moduleMethod.moduleClassName] =
|
|
|
|
methods ? [methods arrayByAddingObject:moduleMethod] : @[moduleMethod];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
methodsByModuleID = [[RCTSparseArray alloc] initWithCapacity:[classes count]];
|
|
|
|
[classes enumerateObjectsUsingBlock:^(Class moduleClass, NSUInteger moduleID, BOOL *stop) {
|
|
|
|
methodsByModuleID[moduleID] = methodsByModuleClassName[NSStringFromClass(moduleClass)];
|
|
|
|
}];
|
|
|
|
});
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
return methodsByModuleID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This constructs the remote modules configuration data structure,
|
|
|
|
* which represents the native modules and methods that will be called
|
|
|
|
* by JS. A numeric ID is assigned to each module and method, which will
|
|
|
|
* be used to communicate via the bridge. The structure of each
|
|
|
|
* module is as follows:
|
|
|
|
*
|
|
|
|
* "ModuleName1": {
|
|
|
|
* "moduleID": 0,
|
|
|
|
* "methods": {
|
|
|
|
* "methodName1": {
|
|
|
|
* "methodID": 0,
|
|
|
|
* "type": "remote"
|
|
|
|
* },
|
|
|
|
* "methodName2": {
|
|
|
|
* "methodID": 1,
|
|
|
|
* "type": "remote"
|
|
|
|
* },
|
|
|
|
* etc...
|
|
|
|
* },
|
|
|
|
* "constants": {
|
|
|
|
* ...
|
|
|
|
* }
|
|
|
|
* },
|
|
|
|
* etc...
|
|
|
|
*/
|
|
|
|
static NSDictionary *RCTRemoteModulesConfig(NSDictionary *modulesByName)
|
|
|
|
{
|
|
|
|
static NSMutableDictionary *remoteModuleConfigByClassName;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
|
|
|
|
remoteModuleConfigByClassName = [[NSMutableDictionary alloc] init];
|
|
|
|
[RCTBridgeModuleClassesByModuleID() enumerateObjectsUsingBlock:^(Class moduleClass, NSUInteger moduleID, BOOL *stop) {
|
|
|
|
|
|
|
|
NSArray *methods = RCTExportedMethodsByModuleID()[moduleID];
|
|
|
|
NSMutableDictionary *methodsByName = [NSMutableDictionary dictionaryWithCapacity:methods.count];
|
2015-02-24 17:06:57 +00:00
|
|
|
[methods enumerateObjectsUsingBlock:^(RCTModuleMethod *method, NSUInteger methodID, BOOL *_stop) {
|
2015-02-20 04:10:52 +00:00
|
|
|
methodsByName[method.JSMethodName] = @{
|
|
|
|
@"methodID": @(methodID),
|
|
|
|
@"type": @"remote",
|
|
|
|
};
|
|
|
|
}];
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
NSDictionary *module = @{
|
|
|
|
@"moduleID": @(moduleID),
|
|
|
|
@"methods": methodsByName
|
|
|
|
};
|
|
|
|
|
|
|
|
remoteModuleConfigByClassName[NSStringFromClass(moduleClass)] = module;
|
|
|
|
}];
|
|
|
|
});
|
|
|
|
|
|
|
|
// Create config
|
|
|
|
NSMutableDictionary *moduleConfig = [[NSMutableDictionary alloc] init];
|
|
|
|
[modulesByName enumerateKeysAndObjectsUsingBlock:^(NSString *moduleName, id<RCTBridgeModule> module, BOOL *stop) {
|
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
// Add constants
|
2015-02-20 04:10:52 +00:00
|
|
|
NSMutableDictionary *config = remoteModuleConfigByClassName[NSStringFromClass([module class])];
|
2015-03-01 23:33:55 +00:00
|
|
|
if ([module respondsToSelector:@selector(constantsToExport)]) {
|
|
|
|
NSDictionary *constants = [module constantsToExport];
|
|
|
|
if (constants) {
|
|
|
|
NSMutableDictionary *mutableConfig = [NSMutableDictionary dictionaryWithDictionary:config];
|
|
|
|
mutableConfig[@"constants"] = constants; // There's no real need to copy this
|
|
|
|
config = mutableConfig; // Nor this - receiver is unlikely to mutate it
|
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
moduleConfig[moduleName] = config;
|
|
|
|
}];
|
|
|
|
|
|
|
|
return moduleConfig;
|
|
|
|
}
|
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
/**
|
|
|
|
* As above, but for local modules/methods, which represent JS classes
|
|
|
|
* and methods that will be called by the native code via the bridge.
|
|
|
|
* Structure is essentially the same as for remote modules:
|
|
|
|
*
|
|
|
|
* "ModuleName1": {
|
|
|
|
* "moduleID": 0,
|
|
|
|
* "methods": {
|
|
|
|
* "methodName1": {
|
|
|
|
* "methodID": 0,
|
|
|
|
* "type": "local"
|
|
|
|
* },
|
|
|
|
* "methodName2": {
|
|
|
|
* "methodID": 1,
|
|
|
|
* "type": "local"
|
|
|
|
* },
|
|
|
|
* etc...
|
|
|
|
* }
|
|
|
|
* },
|
|
|
|
* etc...
|
|
|
|
*/
|
|
|
|
static NSMutableDictionary *RCTLocalModuleIDs;
|
|
|
|
static NSMutableDictionary *RCTLocalMethodIDs;
|
|
|
|
static NSDictionary *RCTLocalModulesConfig()
|
|
|
|
{
|
|
|
|
static NSMutableDictionary *localModules;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
2015-02-24 17:06:57 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
RCTLocalModuleIDs = [[NSMutableDictionary alloc] init];
|
|
|
|
RCTLocalMethodIDs = [[NSMutableDictionary alloc] init];
|
2015-02-24 17:06:57 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
localModules = [[NSMutableDictionary alloc] init];
|
2015-02-24 17:06:57 +00:00
|
|
|
for (NSString *moduleDotMethod in RCTJSMethods()) {
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
NSArray *parts = [moduleDotMethod componentsSeparatedByString:@"."];
|
|
|
|
RCTCAssert(parts.count == 2, @"'%@' is not a valid JS method definition - expected 'Module.method' format.", moduleDotMethod);
|
2015-02-24 17:06:57 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
// Add module if it doesn't already exist
|
|
|
|
NSString *moduleName = parts[0];
|
|
|
|
NSDictionary *module = localModules[moduleName];
|
|
|
|
if (!module) {
|
|
|
|
module = @{
|
|
|
|
@"moduleID": @(localModules.count),
|
|
|
|
@"methods": [[NSMutableDictionary alloc] init]
|
|
|
|
};
|
|
|
|
localModules[moduleName] = module;
|
|
|
|
}
|
2015-02-24 17:06:57 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
// Add method if it doesn't already exist
|
|
|
|
NSString *methodName = parts[1];
|
|
|
|
NSMutableDictionary *methods = module[@"methods"];
|
|
|
|
if (!methods[methodName]) {
|
|
|
|
methods[methodName] = @{
|
|
|
|
@"methodID": @(methods.count),
|
|
|
|
@"type": @"local"
|
|
|
|
};
|
|
|
|
}
|
2015-02-24 17:06:57 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
// Add module and method lookup
|
|
|
|
RCTLocalModuleIDs[moduleDotMethod] = module[@"moduleID"];
|
|
|
|
RCTLocalMethodIDs[moduleDotMethod] = methods[methodName][@"methodID"];
|
|
|
|
}
|
|
|
|
});
|
2015-02-24 17:06:57 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
return localModules;
|
|
|
|
}
|
|
|
|
|
|
|
|
@implementation RCTBridge
|
|
|
|
{
|
|
|
|
RCTSparseArray *_modulesByID;
|
2015-02-24 17:06:57 +00:00
|
|
|
NSDictionary *_modulesByName;
|
2015-02-20 04:10:52 +00:00
|
|
|
id<RCTJavaScriptExecutor> _javaScriptExecutor;
|
2015-04-02 14:33:21 +00:00
|
|
|
Class _executorClass;
|
|
|
|
NSString *_bundlePath;
|
|
|
|
NSDictionary *_launchOptions;
|
2015-03-26 01:59:42 +00:00
|
|
|
RCTBridgeModuleProviderBlock _moduleProvider;
|
2015-04-02 14:33:21 +00:00
|
|
|
BOOL _loaded;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static id<RCTJavaScriptExecutor> _latestJSExecutor;
|
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
- (instancetype)initWithBundlePath:(NSString *)bundlePath
|
2015-03-26 01:59:42 +00:00
|
|
|
moduleProvider:(RCTBridgeModuleProviderBlock)block
|
|
|
|
launchOptions:(NSDictionary *)launchOptions
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
|
|
|
if ((self = [super init])) {
|
2015-04-02 14:33:21 +00:00
|
|
|
_bundlePath = bundlePath;
|
2015-03-26 01:59:42 +00:00
|
|
|
_moduleProvider = block;
|
|
|
|
_launchOptions = launchOptions;
|
2015-04-02 14:33:21 +00:00
|
|
|
[self setUp];
|
|
|
|
[self bindKeys];
|
2015-03-26 01:59:42 +00:00
|
|
|
}
|
2015-04-02 14:33:21 +00:00
|
|
|
|
2015-03-26 01:59:42 +00:00
|
|
|
return self;
|
|
|
|
}
|
2015-04-02 14:33:21 +00:00
|
|
|
- (void)setUp
|
2015-03-26 01:59:42 +00:00
|
|
|
{
|
2015-04-02 14:33:21 +00:00
|
|
|
Class executorClass = _executorClass ?: _globalExecutorClass ?: [RCTContextExecutor class];
|
2015-04-08 00:53:56 +00:00
|
|
|
_javaScriptExecutor = [[executorClass alloc] init];
|
2015-03-26 01:59:42 +00:00
|
|
|
_latestJSExecutor = _javaScriptExecutor;
|
2015-04-02 14:33:21 +00:00
|
|
|
_eventDispatcher = [[RCTEventDispatcher alloc] initWithBridge:self];
|
|
|
|
_shadowQueue = dispatch_queue_create("com.facebook.ReactKit.ShadowQueue", DISPATCH_QUEUE_SERIAL);
|
2015-02-24 17:06:57 +00:00
|
|
|
|
2015-03-26 01:59:42 +00:00
|
|
|
// Register passed-in module instances
|
|
|
|
NSMutableDictionary *preregisteredModules = [[NSMutableDictionary alloc] init];
|
|
|
|
for (id<RCTBridgeModule> module in _moduleProvider ? _moduleProvider() : nil) {
|
2015-04-07 14:36:26 +00:00
|
|
|
preregisteredModules[RCTBridgeModuleNameForClass([module class])] = module;
|
2015-03-26 01:59:42 +00:00
|
|
|
}
|
2015-02-24 17:06:57 +00:00
|
|
|
|
2015-03-26 01:59:42 +00:00
|
|
|
// Instantiate modules
|
|
|
|
_modulesByID = [[RCTSparseArray alloc] init];
|
|
|
|
NSMutableDictionary *modulesByName = [preregisteredModules mutableCopy];
|
|
|
|
[RCTBridgeModuleClassesByModuleID() enumerateObjectsUsingBlock:^(Class moduleClass, NSUInteger moduleID, BOOL *stop) {
|
|
|
|
NSString *moduleName = RCTModuleNamesByID[moduleID];
|
|
|
|
// Check if module instance has already been registered for this name
|
|
|
|
if ((_modulesByID[moduleID] = modulesByName[moduleName])) {
|
|
|
|
// Preregistered instances takes precedence, no questions asked
|
|
|
|
if (!preregisteredModules[moduleName]) {
|
|
|
|
// It's OK to have a name collision as long as the second instance is nil
|
|
|
|
RCTAssert([[moduleClass alloc] init] == nil,
|
|
|
|
@"Attempted to register RCTBridgeModule class %@ for the name '%@', \
|
|
|
|
but name was already registered by class %@", moduleClass,
|
|
|
|
moduleName, [modulesByName[moduleName] class]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Module name hasn't been used before, so go ahead and instantiate
|
|
|
|
id<RCTBridgeModule> module = [[moduleClass alloc] init];
|
|
|
|
if (module) {
|
|
|
|
_modulesByID[moduleID] = modulesByName[moduleName] = module;
|
2015-03-01 23:33:55 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-26 01:59:42 +00:00
|
|
|
}];
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-03-26 01:59:42 +00:00
|
|
|
// Store modules
|
|
|
|
_modulesByName = [modulesByName copy];
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-03-26 01:59:42 +00:00
|
|
|
// Set bridge
|
|
|
|
for (id<RCTBridgeModule> module in _modulesByName.allValues) {
|
|
|
|
if ([module respondsToSelector:@selector(setBridge:)]) {
|
|
|
|
module.bridge = self;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-03-26 01:59:42 +00:00
|
|
|
// Inject module data into JS context
|
|
|
|
NSString *configJSON = RCTJSONStringify(@{
|
|
|
|
@"remoteModuleConfig": RCTRemoteModulesConfig(_modulesByName),
|
|
|
|
@"localModulesConfig": RCTLocalModulesConfig()
|
|
|
|
}, NULL);
|
|
|
|
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
|
|
|
|
[_javaScriptExecutor injectJSONText:configJSON asGlobalObjectNamed:@"__fbBatchedBridgeConfig" callback:^(id err) {
|
|
|
|
dispatch_semaphore_signal(semaphore);
|
|
|
|
}];
|
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
|
2015-04-02 20:34:50 +00:00
|
|
|
if (_javaScriptExecutor == nil) {
|
|
|
|
/**
|
|
|
|
* HACK (tadeu): If it failed to connect to the debugger, set loaded to true so we can
|
|
|
|
* reload
|
|
|
|
*/
|
|
|
|
_loaded = YES;
|
|
|
|
} else if (_bundlePath != nil) { // Allow testing without a script
|
2015-04-02 14:33:21 +00:00
|
|
|
RCTJavaScriptLoader *loader = [[RCTJavaScriptLoader alloc] initWithBridge:self];
|
|
|
|
[loader loadBundleAtURL:[NSURL URLWithString:_bundlePath]
|
|
|
|
onComplete:^(NSError *error) {
|
|
|
|
_loaded = YES;
|
|
|
|
if (error != nil) {
|
|
|
|
NSArray *stack = [[error userInfo] objectForKey:@"stack"];
|
|
|
|
if (stack) {
|
|
|
|
[[RCTRedBox sharedInstance] showErrorMessage:[error localizedDescription] withStack:stack];
|
|
|
|
} else {
|
|
|
|
[[RCTRedBox sharedInstance] showErrorMessage:[error localizedDescription] withDetails:[error localizedFailureReason]];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:RCTJavaScriptDidLoadNotification
|
|
|
|
object:self];
|
|
|
|
[[NSNotificationCenter defaultCenter] addObserver:self
|
|
|
|
selector:@selector(reload)
|
|
|
|
name:RCTReloadNotification
|
|
|
|
object:nil];
|
|
|
|
}
|
|
|
|
}];
|
2015-03-26 01:59:42 +00:00
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
- (void)bindKeys
|
|
|
|
{
|
|
|
|
#if TARGET_IPHONE_SIMULATOR
|
2015-04-08 02:18:41 +00:00
|
|
|
__weak RCTBridge *weakSelf = self;
|
|
|
|
|
2015-04-02 23:41:33 +00:00
|
|
|
// Workaround around the first cmd+r not working: http://openradar.appspot.com/19613391
|
|
|
|
// You can register just the cmd key and do nothing. This will trigger the bug and cmd+r
|
|
|
|
// will work like a charm!
|
|
|
|
[[RCTKeyCommands sharedInstance] registerKeyCommandWithInput:@""
|
|
|
|
modifierFlags:UIKeyModifierCommand
|
|
|
|
action:^(UIKeyCommand *command) {
|
|
|
|
// Do nothing
|
|
|
|
}];
|
2015-04-02 14:33:21 +00:00
|
|
|
[[RCTKeyCommands sharedInstance] registerKeyCommandWithInput:@"r"
|
|
|
|
modifierFlags:UIKeyModifierCommand
|
|
|
|
action:^(UIKeyCommand *command) {
|
2015-04-08 02:18:41 +00:00
|
|
|
[weakSelf reload];
|
2015-04-02 14:33:21 +00:00
|
|
|
}];
|
|
|
|
[[RCTKeyCommands sharedInstance] registerKeyCommandWithInput:@"n"
|
|
|
|
modifierFlags:UIKeyModifierCommand
|
|
|
|
action:^(UIKeyCommand *command) {
|
2015-04-08 02:18:41 +00:00
|
|
|
RCTBridge *strongSelf = weakSelf;
|
|
|
|
if (!strongSelf) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
strongSelf->_executorClass = Nil;
|
|
|
|
[strongSelf reload];
|
2015-04-02 14:33:21 +00:00
|
|
|
}];
|
|
|
|
[[RCTKeyCommands sharedInstance] registerKeyCommandWithInput:@"d"
|
|
|
|
modifierFlags:UIKeyModifierCommand
|
|
|
|
action:^(UIKeyCommand *command) {
|
2015-04-08 02:18:41 +00:00
|
|
|
RCTBridge *strongSelf = weakSelf;
|
|
|
|
if (!strongSelf) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
strongSelf->_executorClass = NSClassFromString(@"RCTWebSocketExecutor");
|
|
|
|
if (!strongSelf->_executorClass) {
|
2015-04-02 14:33:21 +00:00
|
|
|
RCTLogError(@"WebSocket debugger is not available. Did you forget to include RCTWebSocketExecutor?");
|
|
|
|
}
|
2015-04-08 02:18:41 +00:00
|
|
|
[strongSelf reload];
|
2015-04-02 14:33:21 +00:00
|
|
|
}];
|
|
|
|
#endif
|
|
|
|
}
|
2015-03-26 01:59:42 +00:00
|
|
|
|
2015-02-24 17:06:57 +00:00
|
|
|
- (NSDictionary *)modules
|
|
|
|
{
|
|
|
|
RCTAssert(_modulesByName != nil, @"Bridge modules have not yet been initialized. \
|
|
|
|
You may be trying to access a module too early in the startup procedure.");
|
|
|
|
|
|
|
|
return _modulesByName;
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
- (void)dealloc
|
|
|
|
{
|
2015-04-08 02:18:41 +00:00
|
|
|
[self invalidate];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - RCTInvalidating
|
|
|
|
|
|
|
|
- (BOOL)isValid
|
|
|
|
{
|
|
|
|
return _javaScriptExecutor != nil;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)invalidate
|
|
|
|
{
|
2015-04-08 02:18:41 +00:00
|
|
|
if (!self.isValid && _modulesByID == nil) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (![NSThread isMainThread]) {
|
|
|
|
[self performSelectorOnMainThread:@selector(invalidate) withObject:nil waitUntilDone:YES];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] removeObserver:self];
|
|
|
|
|
|
|
|
// Wait for queued methods to finish
|
|
|
|
dispatch_sync(self.shadowQueue, ^{
|
|
|
|
// Make sure all dispatchers have been executed before continuing
|
|
|
|
});
|
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
// Release executor
|
2015-02-20 04:10:52 +00:00
|
|
|
if (_latestJSExecutor == _javaScriptExecutor) {
|
|
|
|
_latestJSExecutor = nil;
|
|
|
|
}
|
|
|
|
[_javaScriptExecutor invalidate];
|
|
|
|
_javaScriptExecutor = nil;
|
2015-03-01 23:33:55 +00:00
|
|
|
|
|
|
|
// Invalidate modules
|
2015-02-20 04:10:52 +00:00
|
|
|
for (id target in _modulesByID.allObjects) {
|
|
|
|
if ([target respondsToSelector:@selector(invalidate)]) {
|
|
|
|
[(id<RCTInvalidating>)target invalidate];
|
|
|
|
}
|
|
|
|
}
|
2015-03-01 23:33:55 +00:00
|
|
|
|
|
|
|
// Release modules (breaks retain cycle if module has strong bridge reference)
|
|
|
|
_modulesByID = nil;
|
|
|
|
_modulesByName = nil;
|
2015-04-02 14:33:21 +00:00
|
|
|
_loaded = NO;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* - TODO (#5906496): When we build a `MessageQueue.m`, handling all the requests could
|
|
|
|
* cause both a queue of "responses". We would flush them here. However, we
|
|
|
|
* currently just expect each objc block to handle its own response sending
|
|
|
|
* using a `RCTResponseSenderBlock`.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma mark - RCTBridge methods
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Like JS::call, for objective-c.
|
|
|
|
*/
|
|
|
|
- (void)enqueueJSCall:(NSString *)moduleDotMethod args:(NSArray *)args
|
|
|
|
{
|
|
|
|
NSNumber *moduleID = RCTLocalModuleIDs[moduleDotMethod];
|
|
|
|
RCTAssert(moduleID != nil, @"Module '%@' not registered.",
|
|
|
|
[[moduleDotMethod componentsSeparatedByString:@"."] firstObject]);
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
NSNumber *methodID = RCTLocalMethodIDs[moduleDotMethod];
|
|
|
|
RCTAssert(methodID != nil, @"Method '%@' not registered.", moduleDotMethod);
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
if (self.loaded) {
|
2015-02-20 04:10:52 +00:00
|
|
|
[self _invokeAndProcessModule:@"BatchedBridge"
|
|
|
|
method:@"callFunctionReturnFlushedQueue"
|
2015-02-24 17:06:57 +00:00
|
|
|
arguments:@[moduleID, methodID, args ?: @[]]];
|
2015-04-02 14:33:21 +00:00
|
|
|
}
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)enqueueApplicationScript:(NSString *)script url:(NSURL *)url onComplete:(RCTJavaScriptCompleteBlock)onComplete
|
|
|
|
{
|
|
|
|
RCTAssert(onComplete != nil, @"onComplete block passed in should be non-nil");
|
|
|
|
[_javaScriptExecutor executeApplicationScript:script sourceURL:url onComplete:^(NSError *scriptLoadError) {
|
|
|
|
if (scriptLoadError) {
|
|
|
|
onComplete(scriptLoadError);
|
|
|
|
return;
|
|
|
|
}
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
[_javaScriptExecutor executeJSCall:@"BatchedBridge"
|
|
|
|
method:@"flushedQueue"
|
|
|
|
arguments:@[]
|
2015-03-01 23:33:55 +00:00
|
|
|
callback:^(id json, NSError *error) {
|
|
|
|
[self _handleBuffer:json];
|
2015-02-20 04:10:52 +00:00
|
|
|
onComplete(error);
|
|
|
|
}];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Payload Generation
|
|
|
|
|
|
|
|
- (void)_invokeAndProcessModule:(NSString *)module method:(NSString *)method arguments:(NSArray *)args
|
|
|
|
{
|
2015-03-23 18:01:02 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:@"JS_PERF_ENQUEUE" object:nil userInfo:nil];
|
2015-03-01 23:33:55 +00:00
|
|
|
|
|
|
|
RCTJavaScriptCallback processResponse = ^(id json, NSError *error) {
|
2015-03-23 18:01:02 +00:00
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:@"JS_PERF_DEQUEUE" object:nil userInfo:nil];
|
2015-03-01 23:33:55 +00:00
|
|
|
[self _handleBuffer:json];
|
2015-02-20 04:10:52 +00:00
|
|
|
};
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
[_javaScriptExecutor executeJSCall:module
|
|
|
|
method:method
|
|
|
|
arguments:args
|
|
|
|
callback:processResponse];
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma mark - Payload Processing
|
|
|
|
|
|
|
|
- (void)_handleBuffer:(id)buffer
|
|
|
|
{
|
|
|
|
if (buffer == nil || buffer == (id)kCFNull) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
if (![buffer isKindOfClass:[NSArray class]]) {
|
|
|
|
RCTLogError(@"Buffer must be an instance of NSArray, got %@", NSStringFromClass([buffer class]));
|
|
|
|
return;
|
|
|
|
}
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
NSArray *requestsArray = (NSArray *)buffer;
|
|
|
|
NSUInteger bufferRowCount = [requestsArray count];
|
|
|
|
NSUInteger expectedFieldsCount = RCTBridgeFieldResponseReturnValues + 1;
|
|
|
|
if (bufferRowCount != expectedFieldsCount) {
|
|
|
|
RCTLogError(@"Must pass all fields to buffer - expected %zd, saw %zd", expectedFieldsCount, bufferRowCount);
|
|
|
|
return;
|
|
|
|
}
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
for (NSUInteger fieldIndex = RCTBridgeFieldRequestModuleIDs; fieldIndex <= RCTBridgeFieldParamss; fieldIndex++) {
|
|
|
|
id field = [requestsArray objectAtIndex:fieldIndex];
|
|
|
|
if (![field isKindOfClass:[NSArray class]]) {
|
|
|
|
RCTLogError(@"Field at index %zd in buffer must be an instance of NSArray, got %@", fieldIndex, NSStringFromClass([field class]));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
NSArray *moduleIDs = requestsArray[RCTBridgeFieldRequestModuleIDs];
|
|
|
|
NSArray *methodIDs = requestsArray[RCTBridgeFieldMethodIDs];
|
|
|
|
NSArray *paramsArrays = requestsArray[RCTBridgeFieldParamss];
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
NSUInteger numRequests = [moduleIDs count];
|
|
|
|
BOOL allSame = numRequests == [methodIDs count] && numRequests == [paramsArrays count];
|
|
|
|
if (!allSame) {
|
|
|
|
RCTLogError(@"Invalid data message - all must be length: %zd", numRequests);
|
|
|
|
return;
|
|
|
|
}
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
for (NSUInteger i = 0; i < numRequests; i++) {
|
|
|
|
@autoreleasepool {
|
|
|
|
[self _handleRequestNumber:i
|
|
|
|
moduleID:[moduleIDs[i] integerValue]
|
|
|
|
methodID:[methodIDs[i] integerValue]
|
|
|
|
params:paramsArrays[i]];
|
|
|
|
}
|
|
|
|
}
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
// TODO: only used by RCTUIManager - can we eliminate this special case?
|
2015-03-01 23:33:55 +00:00
|
|
|
dispatch_async(self.shadowQueue, ^{
|
2015-02-20 04:10:52 +00:00
|
|
|
for (id module in _modulesByID.allObjects) {
|
|
|
|
if ([module respondsToSelector:@selector(batchDidComplete)]) {
|
|
|
|
[module batchDidComplete];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)_handleRequestNumber:(NSUInteger)i
|
|
|
|
moduleID:(NSUInteger)moduleID
|
|
|
|
methodID:(NSUInteger)methodID
|
|
|
|
params:(NSArray *)params
|
|
|
|
{
|
|
|
|
if (![params isKindOfClass:[NSArray class]]) {
|
|
|
|
RCTLogError(@"Invalid module/method/params tuple for request #%zd", i);
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
// Look up method
|
2015-02-20 04:10:52 +00:00
|
|
|
NSArray *methods = RCTExportedMethodsByModuleID()[moduleID];
|
|
|
|
if (methodID >= methods.count) {
|
|
|
|
RCTLogError(@"Unknown methodID: %zd for module: %zd (%@)", methodID, moduleID, RCTModuleNamesByID[moduleID]);
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
RCTModuleMethod *method = methods[methodID];
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
__weak RCTBridge *weakSelf = self;
|
2015-03-01 23:33:55 +00:00
|
|
|
dispatch_async(self.shadowQueue, ^{
|
2015-02-20 04:10:52 +00:00
|
|
|
__strong RCTBridge *strongSelf = weakSelf;
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
if (!strongSelf.isValid) {
|
|
|
|
// strongSelf has been invalidated since the dispatch_async call and this
|
|
|
|
// invocation should not continue.
|
|
|
|
return;
|
|
|
|
}
|
2015-03-01 23:33:55 +00:00
|
|
|
|
|
|
|
// Look up module
|
|
|
|
id module = strongSelf->_modulesByID[moduleID];
|
|
|
|
if (!module) {
|
|
|
|
RCTLogError(@"No module found for name '%@'", RCTModuleNamesByID[moduleID]);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
@try {
|
2015-03-01 23:33:55 +00:00
|
|
|
[method invokeWithBridge:strongSelf module:module arguments:params];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
@catch (NSException *exception) {
|
2015-03-01 23:33:55 +00:00
|
|
|
RCTLogError(@"Exception thrown while invoking %@ on target %@ with params %@: %@", method.JSMethodName, module, params, exception);
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-03-01 23:33:55 +00:00
|
|
|
return YES;
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
2015-04-02 14:33:21 +00:00
|
|
|
- (void)reload
|
|
|
|
{
|
|
|
|
if (_loaded) {
|
|
|
|
// If the bridge has not loaded yet, the context will be already invalid at
|
|
|
|
// the time the javascript gets executed.
|
|
|
|
// It will crash the javascript, and even the next `load` won't render.
|
|
|
|
[self invalidate];
|
|
|
|
[self setUp];
|
|
|
|
[[NSNotificationCenter defaultCenter] postNotificationName:RCTReloadViewsNotification
|
|
|
|
object:self];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-20 04:10:52 +00:00
|
|
|
+ (BOOL)hasValidJSExecutor
|
|
|
|
{
|
|
|
|
return (_latestJSExecutor != nil && [_latestJSExecutor isValid]);
|
|
|
|
}
|
|
|
|
|
2015-04-07 14:36:26 +00:00
|
|
|
+ (void)logMessage:(NSString *)message level:(NSString *)level
|
2015-02-20 04:10:52 +00:00
|
|
|
{
|
|
|
|
if (!_latestJSExecutor || ![_latestJSExecutor isValid]) {
|
2015-04-07 14:36:26 +00:00
|
|
|
RCTLogError(@"ERROR: No valid JS executor to log '%@'.", message);
|
2015-02-20 04:10:52 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-03-01 23:33:55 +00:00
|
|
|
|
2015-04-07 14:36:26 +00:00
|
|
|
// Note: the js executor could get invalidated while we're trying to call
|
|
|
|
// this...need to watch out for that.
|
2015-02-20 04:10:52 +00:00
|
|
|
[_latestJSExecutor executeJSCall:@"RCTLog"
|
|
|
|
method:@"logIfNoNativeHook"
|
2015-04-07 14:36:26 +00:00
|
|
|
arguments:@[level, message]
|
2015-03-01 23:33:55 +00:00
|
|
|
callback:^(id json, NSError *error) {}];
|
2015-02-20 04:10:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|