Expose ModuleRegistry from Instance

Reviewed By: javache

Differential Revision: D5586566

fbshipit-source-id: 786ecf2aa67f12861df7d98f0d32ab79b3d02962
This commit is contained in:
Riley Dulin 2017-08-10 10:44:10 -07:00 committed by Facebook Github Bot
parent a9eaf6f8a7
commit 1df198da02
2 changed files with 82 additions and 69 deletions

View File

@ -2,18 +2,18 @@
#include "Instance.h"
#include "JSBigString.h"
#include "JSExecutor.h"
#include "JSModulesUnbundle.h"
#include "MessageQueueThread.h"
#include "MethodCall.h"
#include "NativeToJsBridge.h"
#include "RecoverableError.h"
#include "SystraceSection.h"
#include "MessageQueueThread.h"
#include "NativeToJsBridge.h"
#include "JSBigString.h"
#include "JSModulesUnbundle.h"
#include <folly/json.h>
#include <folly/Memory.h>
#include <folly/MoveWrapper.h>
#include <folly/json.h>
#include <glog/logging.h>
@ -38,37 +38,38 @@ void Instance::initializeBridge(
callback_ = std::move(callback);
moduleRegistry_ = std::move(moduleRegistry);
jsQueue->runOnQueueSync(
[this, &jsef, jsQueue] () mutable {
nativeToJsBridge_ = folly::make_unique<NativeToJsBridge>(
jsef.get(), moduleRegistry_, jsQueue, callback_);
jsQueue->runOnQueueSync([this, &jsef, jsQueue]() mutable {
nativeToJsBridge_ = folly::make_unique<NativeToJsBridge>(
jsef.get(), moduleRegistry_, jsQueue, callback_);
std::lock_guard<std::mutex> lock(m_syncMutex);
m_syncReady = true;
m_syncCV.notify_all();
});
std::lock_guard<std::mutex> lock(m_syncMutex);
m_syncReady = true;
m_syncCV.notify_all();
});
CHECK(nativeToJsBridge_);
}
void Instance::loadApplication(
std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> string,
std::string sourceURL) {
void Instance::loadApplication(std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> string,
std::string sourceURL) {
callback_->incrementPendingJSCalls();
SystraceSection s("reactbridge_xplat_loadApplication", "sourceURL", sourceURL);
nativeToJsBridge_->loadApplication(std::move(unbundle), std::move(string), std::move(sourceURL));
SystraceSection s("reactbridge_xplat_loadApplication", "sourceURL",
sourceURL);
nativeToJsBridge_->loadApplication(std::move(unbundle), std::move(string),
std::move(sourceURL));
}
void Instance::loadApplicationSync(
std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> string,
std::string sourceURL) {
void Instance::loadApplicationSync(std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> string,
std::string sourceURL) {
std::unique_lock<std::mutex> lock(m_syncMutex);
m_syncCV.wait(lock, [this] { return m_syncReady; });
SystraceSection s("reactbridge_xplat_loadApplicationSync", "sourceURL", sourceURL);
nativeToJsBridge_->loadApplicationSync(std::move(unbundle), std::move(string), std::move(sourceURL));
SystraceSection s("reactbridge_xplat_loadApplicationSync", "sourceURL",
sourceURL);
nativeToJsBridge_->loadApplicationSync(std::move(unbundle), std::move(string),
std::move(sourceURL));
}
void Instance::setSourceURL(std::string sourceURL) {
@ -81,7 +82,8 @@ void Instance::setSourceURL(std::string sourceURL) {
void Instance::loadScriptFromString(std::unique_ptr<const JSBigString> string,
std::string sourceURL,
bool loadSynchronously) {
SystraceSection s("reactbridge_xplat_loadScriptFromString", "sourceURL", sourceURL);
SystraceSection s("reactbridge_xplat_loadScriptFromString", "sourceURL",
sourceURL);
if (loadSynchronously) {
loadApplicationSync(nullptr, std::move(string), std::move(sourceURL));
} else {
@ -97,31 +99,41 @@ void Instance::loadUnbundle(std::unique_ptr<JSModulesUnbundle> unbundle,
loadApplicationSync(std::move(unbundle), std::move(startupScript),
std::move(startupScriptSourceURL));
} else {
loadApplication(std::move(unbundle), std::move(startupScript),
std::move(startupScriptSourceURL));
loadApplication(std::move(unbundle), std::move(startupScript),
std::move(startupScriptSourceURL));
}
}
void Instance::setGlobalVariable(std::string propName,
std::unique_ptr<const JSBigString> jsonValue) {
nativeToJsBridge_->setGlobalVariable(std::move(propName), std::move(jsonValue));
nativeToJsBridge_->setGlobalVariable(std::move(propName),
std::move(jsonValue));
}
void *Instance::getJavaScriptContext() {
return nativeToJsBridge_ ? nativeToJsBridge_->getJavaScriptContext() : nullptr;
return nativeToJsBridge_ ? nativeToJsBridge_->getJavaScriptContext()
: nullptr;
}
void Instance::callJSFunction(std::string&& module, std::string&& method, folly::dynamic&& params) {
void Instance::callJSFunction(std::string &&module, std::string &&method,
folly::dynamic &&params) {
callback_->incrementPendingJSCalls();
nativeToJsBridge_->callFunction(std::move(module), std::move(method), std::move(params));
nativeToJsBridge_->callFunction(std::move(module), std::move(method),
std::move(params));
}
void Instance::callJSCallback(uint64_t callbackId, folly::dynamic&& params) {
void Instance::callJSCallback(uint64_t callbackId, folly::dynamic &&params) {
SystraceSection s("<callback>");
callback_->incrementPendingJSCalls();
nativeToJsBridge_->invokeCallback((double) callbackId, std::move(params));
nativeToJsBridge_->invokeCallback((double)callbackId, std::move(params));
}
const ModuleRegistry &Instance::getModuleRegistry() const {
return *moduleRegistry_;
}
ModuleRegistry &Instance::getModuleRegistry() { return *moduleRegistry_; }
#ifdef WITH_JSC_MEMORY_PRESSURE
void Instance::handleMemoryPressure(int pressureLevel) {
nativeToJsBridge_->handleMemoryPressure(pressureLevel);

View File

@ -13,7 +13,7 @@
#endif
namespace folly {
struct dynamic;
struct dynamic;
}
namespace facebook {
@ -33,52 +33,52 @@ struct InstanceCallback {
};
class RN_EXPORT Instance {
public:
public:
~Instance();
void initializeBridge(
std::unique_ptr<InstanceCallback> callback,
std::shared_ptr<JSExecutorFactory> jsef,
std::shared_ptr<MessageQueueThread> jsQueue,
std::shared_ptr<ModuleRegistry> moduleRegistry);
void initializeBridge(std::unique_ptr<InstanceCallback> callback,
std::shared_ptr<JSExecutorFactory> jsef,
std::shared_ptr<MessageQueueThread> jsQueue,
std::shared_ptr<ModuleRegistry> moduleRegistry);
void setSourceURL(std::string sourceURL);
void loadScriptFromString(
std::unique_ptr<const JSBigString> string,
std::string sourceURL,
bool loadSynchronously);
void loadUnbundle(
std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL,
bool loadSynchronously);
void loadScriptFromString(std::unique_ptr<const JSBigString> string,
std::string sourceURL, bool loadSynchronously);
void loadUnbundle(std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL, bool loadSynchronously);
bool supportsProfiling();
void setGlobalVariable(std::string propName, std::unique_ptr<const JSBigString> jsonValue);
void setGlobalVariable(std::string propName,
std::unique_ptr<const JSBigString> jsonValue);
void *getJavaScriptContext();
void callJSFunction(std::string&& module, std::string&& method, folly::dynamic&& params);
void callJSCallback(uint64_t callbackId, folly::dynamic&& params);
void callJSFunction(std::string &&module, std::string &&method,
folly::dynamic &&params);
void callJSCallback(uint64_t callbackId, folly::dynamic &&params);
// This method is experimental, and may be modified or removed.
template <typename T>
Value callFunctionSync(const std::string& module, const std::string& method, T&& args) {
Value callFunctionSync(const std::string &module, const std::string &method,
T &&args) {
CHECK(nativeToJsBridge_);
return nativeToJsBridge_->callFunctionSync(module, method, std::forward<T>(args));
return nativeToJsBridge_->callFunctionSync(module, method,
std::forward<T>(args));
}
#ifdef WITH_JSC_MEMORY_PRESSURE
void handleMemoryPressure(int pressureLevel);
#endif
const ModuleRegistry &getModuleRegistry() const;
ModuleRegistry &getModuleRegistry();
private:
void callNativeModules(folly::dynamic&& calls, bool isEndOfBatch);
void loadApplication(
std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL);
void loadApplicationSync(
std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL);
#ifdef WITH_JSC_MEMORY_PRESSURE
void handleMemoryPressure(int pressureLevel);
#endif
private:
void callNativeModules(folly::dynamic &&calls, bool isEndOfBatch);
void loadApplication(std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL);
void loadApplicationSync(std::unique_ptr<JSModulesUnbundle> unbundle,
std::unique_ptr<const JSBigString> startupScript,
std::string startupScriptSourceURL);
std::shared_ptr<InstanceCallback> callback_;
std::unique_ptr<NativeToJsBridge> nativeToJsBridge_;
@ -89,4 +89,5 @@ class RN_EXPORT Instance {
bool m_syncReady = false;
};
} }
} // namespace react
} // namespace facebook