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

View File

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