Extract module registry creation to helper

Reviewed By: mhorowitz

Differential Revision: D4721817

fbshipit-source-id: 2fa17ca5317a57d429aa75d6c1865932af27e02f
This commit is contained in:
Pieter De Baets
2017-03-17 06:55:44 -07:00
committed by Facebook Github Bot
parent ea069b69de
commit ce270220e4
6 changed files with 86 additions and 56 deletions

View File

@@ -0,0 +1,40 @@
/**
* 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.
*/
#include <cxxreact/MessageQueueThread.h>
namespace facebook {
namespace react {
// RCTNativeModule arranges for native methods to be invoked on a queue which
// is not the JS thread. C++ modules don't use RCTNativeModule, so this little
// adapter does the work.
class DispatchMessageQueueThread : public MessageQueueThread {
public:
DispatchMessageQueueThread(RCTModuleData *moduleData)
: moduleData_(moduleData) {}
void runOnQueue(std::function<void()>&& func) override {
dispatch_async(moduleData_.methodQueue, [func=std::move(func)] {
func();
});
}
void runOnQueueSync(std::function<void()>&& func) override {
LOG(FATAL) << "Unsupported operation";
}
void quitSynchronous() override {
LOG(FATAL) << "Unsupported operation";
}
private:
RCTModuleData *moduleData_;
};
} }

View File

@@ -7,12 +7,18 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/
#include <memory>
#import <React/RCTConvert.h>
#include <JavaScriptCore/JavaScriptCore.h>
#include <cxxreact/JSCExecutor.h>
#include <cxxreact/ModuleRegistry.h>
#include <folly/dynamic.h>
#include <jschelpers/JavaScriptCore.h>
@class RCTBridge;
@class RCTModuleData;
@interface RCTConvert (folly)
+ (folly::dynamic)folly_dynamic:(id)json;
@@ -22,6 +28,10 @@
namespace facebook {
namespace react {
class Instance;
std::shared_ptr<ModuleRegistry> buildModuleRegistry(NSArray<RCTModuleData *> *modules, RCTBridge *bridge, const std::shared_ptr<Instance> &instance);
JSContext *contextForGlobalContextRef(JSGlobalContextRef contextRef);
/*

View File

@@ -10,10 +10,15 @@
#import "RCTCxxUtils.h"
#import <React/RCTFollyConvert.h>
#import <React/RCTModuleData.h>
#import <React/RCTUtils.h>
#include <cxxreact/CxxNativeModule.h>
#include <jschelpers/Value.h>
#import "DispatchMessageQueueThread.h"
#import "RCTCxxModule.h"
#import "RCTNativeModule.h"
using namespace facebook::react;
@implementation RCTConvert (folly)
@@ -36,6 +41,30 @@ using namespace facebook::react;
namespace facebook {
namespace react {
std::shared_ptr<ModuleRegistry> buildModuleRegistry(NSArray<RCTModuleData *> *modules, RCTBridge *bridge, const std::shared_ptr<Instance> &instance)
{
std::vector<std::unique_ptr<NativeModule>> nativeModules;
for (RCTModuleData *moduleData in modules) {
if ([moduleData.moduleClass isSubclassOfClass:[RCTCxxModule class]]) {
// If a module does not support automatic instantiation, and
// wasn't provided as an extra module, it may not have an
// instance. If so, skip it.
if (![moduleData hasInstance]) {
continue;
}
nativeModules.emplace_back(std::make_unique<CxxNativeModule>(
instance,
[moduleData.name UTF8String],
[moduleData] { return [(RCTCxxModule *)(moduleData.instance) createModule]; },
std::make_shared<DispatchMessageQueueThread>(moduleData)));
} else {
nativeModules.emplace_back(std::make_unique<RCTNativeModule>(bridge, moduleData));
}
}
return std::make_shared<ModuleRegistry>(std::move(nativeModules));
}
JSContext *contextForGlobalContextRef(JSGlobalContextRef contextRef)
{
static std::mutex s_mutex;
@@ -61,7 +90,7 @@ JSContext *contextForGlobalContextRef(JSGlobalContextRef contextRef)
return ctx;
}
static NSError *errorWithException(const std::exception& e)
static NSError *errorWithException(const std::exception &e)
{
NSString *msg = @(e.what());
NSMutableDictionary *errorInfo = [NSMutableDictionary dictionary];
@@ -75,7 +104,7 @@ static NSError *errorWithException(const std::exception& e)
NSError *nestedError;
try {
std::rethrow_if_nested(e);
} catch(const std::exception& e) {
} catch(const std::exception &e) {
nestedError = errorWithException(e);
} catch(...) {}

View File

@@ -0,0 +1,34 @@
/**
* 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 <React/RCTModuleData.h>
#import <cxxreact/NativeModule.h>
namespace facebook {
namespace react {
class RCTNativeModule : public NativeModule {
public:
RCTNativeModule(RCTBridge *bridge, RCTModuleData *moduleData);
std::string getName() override;
std::vector<MethodDescriptor> getMethods() override;
folly::dynamic getConstants() override;
bool supportsWebWorkers() override;
void invoke(ExecutorToken token, unsigned int methodId, folly::dynamic &&params) override;
MethodCallResult callSerializableNativeHook(ExecutorToken token, unsigned int reactMethodId,
folly::dynamic &&params) override;
private:
__weak RCTBridge *m_bridge;
RCTModuleData *m_moduleData;
};
}
}

View File

@@ -0,0 +1,128 @@
/**
* 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 "RCTNativeModule.h"
#import <React/RCTBridge.h>
#import <React/RCTBridgeMethod.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTCxxUtils.h>
#import <React/RCTFollyConvert.h>
#import <React/RCTProfile.h>
#import <React/RCTUtils.h>
namespace facebook {
namespace react {
RCTNativeModule::RCTNativeModule(RCTBridge *bridge, RCTModuleData *moduleData)
: m_bridge(bridge)
, m_moduleData(moduleData) {}
std::string RCTNativeModule::getName() {
return [m_moduleData.name UTF8String];
}
std::vector<MethodDescriptor> RCTNativeModule::getMethods() {
std::vector<MethodDescriptor> descs;
for (id<RCTBridgeMethod> method in m_moduleData.methods) {
descs.emplace_back(
method.JSMethodName.UTF8String,
method.functionType == RCTFunctionTypePromise ? "promise" : "async"
);
}
return descs;
}
folly::dynamic RCTNativeModule::getConstants() {
// TODO mhorowitz #10487027: This does unnecessary work since it
// only needs constants. Think about refactoring RCTModuleData or
// NativeModule to make this more natural.
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways,
@"[RCTNativeModule getConstants] moduleData.config", nil);
NSArray *config = m_moduleData.config;
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
if (!config || config == (id)kCFNull) {
return nullptr;
}
id constants = config[1];
if (![constants isKindOfClass:[NSDictionary class]]) {
return nullptr;
}
RCT_PROFILE_BEGIN_EVENT(RCTProfileTagAlways,
@"[RCTNativeModule getConstants] convert", nil);
folly::dynamic ret = [RCTConvert folly_dynamic:constants];
RCT_PROFILE_END_EVENT(RCTProfileTagAlways, @"");
return ret;
}
bool RCTNativeModule::supportsWebWorkers() {
return false;
}
void RCTNativeModule::invoke(ExecutorToken token, unsigned int methodId, folly::dynamic &&params) {
// The BatchedBridge version of this buckets all the callbacks by thread, and
// queues one block on each. This is much simpler; we'll see how it goes and
// iterate.
// There is no flow event handling here until I can understand it.
auto sparams = std::make_shared<folly::dynamic>(std::move(params));
__weak RCTBridge *bridge = m_bridge;
dispatch_block_t block = ^{
if (!bridge || !bridge.valid) {
return;
}
id<RCTBridgeMethod> method = m_moduleData.methods[methodId];
if (RCT_DEBUG && !method) {
RCTLogError(@"Unknown methodID: %ud for module: %@",
methodId, m_moduleData.name);
}
NSArray *objcParams = convertFollyDynamicToId(*sparams);
@try {
[method invokeWithBridge:bridge module:m_moduleData.instance arguments:objcParams];
}
@catch (NSException *exception) {
// Pass on JS exceptions
if ([exception.name hasPrefix:RCTFatalExceptionName]) {
@throw exception;
}
NSString *message = [NSString stringWithFormat:
@"Exception '%@' was thrown while invoking %@ on target %@ with params %@",
exception, method.JSMethodName, m_moduleData.name, objcParams];
RCTFatal(RCTErrorWithMessage(message));
}
};
dispatch_queue_t queue = m_moduleData.methodQueue;
if (queue == RCTJSThread) {
block();
} else if (queue) {
dispatch_async(queue, block);
}
}
MethodCallResult RCTNativeModule::callSerializableNativeHook(
ExecutorToken token, unsigned int reactMethodId, folly::dynamic &&params) {
RCTFatal(RCTErrorWithMessage(@"callSerializableNativeHook is not yet supported on iOS"));
return folly::none;
}
}
}