2017-02-01 22:10:41 +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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTObjcExecutor.h"
|
|
|
|
|
|
|
|
#import <React/RCTCxxUtils.h>
|
2017-02-16 14:45:40 +00:00
|
|
|
#import <React/RCTFollyConvert.h>
|
2017-02-01 22:10:41 +00:00
|
|
|
#import <React/RCTJavaScriptExecutor.h>
|
|
|
|
#import <React/RCTLog.h>
|
|
|
|
#import <React/RCTProfile.h>
|
|
|
|
#import <React/RCTUtils.h>
|
2017-06-23 23:49:55 +00:00
|
|
|
#import <cxxreact/JSBigString.h>
|
|
|
|
#import <cxxreact/JSExecutor.h>
|
2017-06-13 16:30:36 +00:00
|
|
|
#import <cxxreact/MessageQueueThread.h>
|
2017-02-01 22:10:41 +00:00
|
|
|
#import <cxxreact/ModuleRegistry.h>
|
|
|
|
#import <folly/json.h>
|
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class JSEException : public std::runtime_error {
|
|
|
|
public:
|
|
|
|
JSEException(NSError *error)
|
|
|
|
: runtime_error([[error description] UTF8String]) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
class RCTObjcExecutor : public JSExecutor {
|
|
|
|
public:
|
2017-06-13 16:30:36 +00:00
|
|
|
RCTObjcExecutor(id<RCTJavaScriptExecutor> jse,
|
|
|
|
RCTJavaScriptCompleteBlock errorBlock,
|
|
|
|
std::shared_ptr<MessageQueueThread> jsThread,
|
|
|
|
std::shared_ptr<ExecutorDelegate> delegate)
|
2017-02-01 22:10:41 +00:00
|
|
|
: m_jse(jse)
|
|
|
|
, m_errorBlock(errorBlock)
|
2017-06-13 16:30:36 +00:00
|
|
|
, m_jsThread(std::move(jsThread))
|
|
|
|
, m_delegate(std::move(delegate))
|
2017-02-01 22:10:41 +00:00
|
|
|
{
|
|
|
|
m_jsCallback = ^(id json, NSError *error) {
|
|
|
|
if (error) {
|
2018-01-18 23:21:01 +00:00
|
|
|
// Do not use "m_errorBlock" here as the bridge might be in the middle
|
|
|
|
// of invalidation as a result of error handling and "this" can be
|
|
|
|
// already deallocated.
|
|
|
|
errorBlock(error);
|
2017-02-01 22:10:41 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-13 16:30:36 +00:00
|
|
|
m_jsThread->runOnQueue([this, json]{
|
2017-07-10 12:21:03 +00:00
|
|
|
m_delegate->callNativeModules(*this, convertIdToFollyDynamic(json), true);
|
2017-06-13 16:30:36 +00:00
|
|
|
});
|
2017-02-01 22:10:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Synchronously initialize the executor
|
|
|
|
[jse setUp];
|
|
|
|
|
|
|
|
folly::dynamic nativeModuleConfig = folly::dynamic::array;
|
2017-06-13 16:30:36 +00:00
|
|
|
auto moduleRegistry = m_delegate->getModuleRegistry();
|
2017-02-01 22:10:41 +00:00
|
|
|
for (const auto &name : moduleRegistry->moduleNames()) {
|
|
|
|
auto config = moduleRegistry->getConfig(name);
|
|
|
|
nativeModuleConfig.push_back(config ? config->config : nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
folly::dynamic config =
|
|
|
|
folly::dynamic::object("remoteModuleConfig", std::move(nativeModuleConfig));
|
|
|
|
|
|
|
|
setGlobalVariable(
|
|
|
|
"__fbBatchedBridgeConfig",
|
|
|
|
std::make_unique<JSBigStdString>(folly::toJson(config)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void loadApplicationScript(
|
|
|
|
std::unique_ptr<const JSBigString> script,
|
|
|
|
std::string sourceURL) override {
|
|
|
|
RCTProfileBeginFlowEvent();
|
|
|
|
[m_jse executeApplicationScript:[NSData dataWithBytes:script->c_str() length:script->size()]
|
|
|
|
sourceURL:[[NSURL alloc]
|
|
|
|
initWithString:@(sourceURL.c_str())]
|
|
|
|
onComplete:^(NSError *error) {
|
|
|
|
RCTProfileEndFlowEvent();
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
m_errorBlock(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[m_jse flushedQueue:m_jsCallback];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2017-09-22 16:56:50 +00:00
|
|
|
void setBundleRegistry(std::unique_ptr<RAMBundleRegistry>) override {
|
|
|
|
RCTAssert(NO, @"RAM bundles are not supported in RCTObjcExecutor");
|
2017-02-01 22:10:41 +00:00
|
|
|
}
|
|
|
|
|
2017-11-09 19:55:37 +00:00
|
|
|
void registerBundle(uint32_t bundleId, const std::string &bundlePath) override {
|
|
|
|
RCTAssert(NO, @"RAM bundles are not supported in RCTObjcExecutor");
|
|
|
|
}
|
|
|
|
|
2017-02-01 22:10:41 +00:00
|
|
|
void callFunction(const std::string &module, const std::string &method,
|
|
|
|
const folly::dynamic &arguments) override {
|
|
|
|
[m_jse callFunctionOnModule:@(module.c_str())
|
|
|
|
method:@(method.c_str())
|
2017-02-16 14:45:40 +00:00
|
|
|
arguments:convertFollyDynamicToId(arguments)
|
2017-02-01 22:10:41 +00:00
|
|
|
callback:m_jsCallback];
|
|
|
|
}
|
|
|
|
|
|
|
|
void invokeCallback(double callbackId, const folly::dynamic &arguments) override {
|
|
|
|
[m_jse invokeCallbackID:@(callbackId)
|
2017-02-16 14:45:40 +00:00
|
|
|
arguments:convertFollyDynamicToId(arguments)
|
2017-02-01 22:10:41 +00:00
|
|
|
callback:m_jsCallback];
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void setGlobalVariable(
|
|
|
|
std::string propName,
|
|
|
|
std::unique_ptr<const JSBigString> jsonValue) override {
|
|
|
|
[m_jse injectJSONText:@(jsonValue->c_str())
|
|
|
|
asGlobalObjectNamed:@(propName.c_str())
|
|
|
|
callback:m_errorBlock];
|
|
|
|
}
|
|
|
|
|
2017-09-05 21:47:06 +00:00
|
|
|
virtual std::string getDescription() override {
|
|
|
|
return [NSStringFromClass([m_jse class]) UTF8String];
|
|
|
|
}
|
|
|
|
|
2017-02-01 22:10:41 +00:00
|
|
|
private:
|
|
|
|
id<RCTJavaScriptExecutor> m_jse;
|
|
|
|
RCTJavaScriptCompleteBlock m_errorBlock;
|
2017-06-13 16:30:36 +00:00
|
|
|
std::shared_ptr<ExecutorDelegate> m_delegate;
|
|
|
|
std::shared_ptr<MessageQueueThread> m_jsThread;
|
2017-02-01 22:10:41 +00:00
|
|
|
RCTJavaScriptCallback m_jsCallback;
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
RCTObjcExecutorFactory::RCTObjcExecutorFactory(
|
|
|
|
id<RCTJavaScriptExecutor> jse, RCTJavaScriptCompleteBlock errorBlock)
|
|
|
|
: m_jse(jse)
|
|
|
|
, m_errorBlock(errorBlock) {}
|
|
|
|
|
|
|
|
std::unique_ptr<JSExecutor> RCTObjcExecutorFactory::createJSExecutor(
|
|
|
|
std::shared_ptr<ExecutorDelegate> delegate,
|
|
|
|
std::shared_ptr<MessageQueueThread> jsQueue) {
|
|
|
|
return std::unique_ptr<JSExecutor>(
|
2017-06-13 16:30:36 +00:00
|
|
|
new RCTObjcExecutor(m_jse, m_errorBlock, jsQueue, delegate));
|
2017-02-01 22:10:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|