2016-05-04 02:29:58 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
|
|
|
#include "Instance.h"
|
|
|
|
|
2017-08-10 17:44:10 +00:00
|
|
|
#include "JSBigString.h"
|
2017-06-23 23:49:55 +00:00
|
|
|
#include "JSExecutor.h"
|
2017-08-10 17:44:10 +00:00
|
|
|
#include "JSModulesUnbundle.h"
|
|
|
|
#include "MessageQueueThread.h"
|
2016-05-04 02:29:58 +00:00
|
|
|
#include "MethodCall.h"
|
2017-08-10 17:44:10 +00:00
|
|
|
#include "NativeToJsBridge.h"
|
2017-01-10 15:04:52 +00:00
|
|
|
#include "RecoverableError.h"
|
2016-05-14 00:15:06 +00:00
|
|
|
#include "SystraceSection.h"
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
#include <folly/Memory.h>
|
2016-05-18 19:46:01 +00:00
|
|
|
#include <folly/MoveWrapper.h>
|
2017-08-10 17:44:10 +00:00
|
|
|
#include <folly/json.h>
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
#include <glog/logging.h>
|
|
|
|
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
|
|
|
Instance::~Instance() {
|
2016-05-18 19:46:01 +00:00
|
|
|
if (nativeToJsBridge_) {
|
|
|
|
nativeToJsBridge_->destroy();
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::initializeBridge(
|
|
|
|
std::unique_ptr<InstanceCallback> callback,
|
|
|
|
std::shared_ptr<JSExecutorFactory> jsef,
|
|
|
|
std::shared_ptr<MessageQueueThread> jsQueue,
|
2016-05-11 18:38:47 +00:00
|
|
|
std::shared_ptr<ModuleRegistry> moduleRegistry) {
|
2016-05-04 02:29:58 +00:00
|
|
|
callback_ = std::move(callback);
|
2017-06-21 18:56:08 +00:00
|
|
|
moduleRegistry_ = std::move(moduleRegistry);
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2017-08-10 17:44:10 +00:00
|
|
|
jsQueue->runOnQueueSync([this, &jsef, jsQueue]() mutable {
|
|
|
|
nativeToJsBridge_ = folly::make_unique<NativeToJsBridge>(
|
|
|
|
jsef.get(), moduleRegistry_, jsQueue, callback_);
|
2016-09-26 23:01:42 +00:00
|
|
|
|
2017-08-10 17:44:10 +00:00
|
|
|
std::lock_guard<std::mutex> lock(m_syncMutex);
|
|
|
|
m_syncReady = true;
|
|
|
|
m_syncCV.notify_all();
|
|
|
|
});
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
CHECK(nativeToJsBridge_);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 17:44:10 +00:00
|
|
|
void Instance::loadApplication(std::unique_ptr<JSModulesUnbundle> unbundle,
|
|
|
|
std::unique_ptr<const JSBigString> string,
|
|
|
|
std::string sourceURL) {
|
2016-05-04 02:29:58 +00:00
|
|
|
callback_->incrementPendingJSCalls();
|
2017-08-10 17:44:10 +00:00
|
|
|
SystraceSection s("reactbridge_xplat_loadApplication", "sourceURL",
|
|
|
|
sourceURL);
|
|
|
|
nativeToJsBridge_->loadApplication(std::move(unbundle), std::move(string),
|
|
|
|
std::move(sourceURL));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 17:44:10 +00:00
|
|
|
void Instance::loadApplicationSync(std::unique_ptr<JSModulesUnbundle> unbundle,
|
|
|
|
std::unique_ptr<const JSBigString> string,
|
|
|
|
std::string sourceURL) {
|
2016-09-26 23:01:42 +00:00
|
|
|
std::unique_lock<std::mutex> lock(m_syncMutex);
|
|
|
|
m_syncCV.wait(lock, [this] { return m_syncReady; });
|
|
|
|
|
2017-08-10 17:44:10 +00:00
|
|
|
SystraceSection s("reactbridge_xplat_loadApplicationSync", "sourceURL",
|
|
|
|
sourceURL);
|
|
|
|
nativeToJsBridge_->loadApplicationSync(std::move(unbundle), std::move(string),
|
|
|
|
std::move(sourceURL));
|
2016-09-26 23:01:42 +00:00
|
|
|
}
|
|
|
|
|
2017-05-24 14:27:06 +00:00
|
|
|
void Instance::setSourceURL(std::string sourceURL) {
|
2017-01-10 15:04:52 +00:00
|
|
|
callback_->incrementPendingJSCalls();
|
2017-05-24 14:27:06 +00:00
|
|
|
SystraceSection s("reactbridge_xplat_setSourceURL", "sourceURL", sourceURL);
|
2017-01-10 15:04:52 +00:00
|
|
|
|
2017-05-24 14:27:06 +00:00
|
|
|
nativeToJsBridge_->loadApplication(nullptr, nullptr, std::move(sourceURL));
|
|
|
|
}
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2017-05-24 14:27:06 +00:00
|
|
|
void Instance::loadScriptFromString(std::unique_ptr<const JSBigString> string,
|
|
|
|
std::string sourceURL,
|
|
|
|
bool loadSynchronously) {
|
2017-08-10 17:44:10 +00:00
|
|
|
SystraceSection s("reactbridge_xplat_loadScriptFromString", "sourceURL",
|
|
|
|
sourceURL);
|
2017-05-24 14:27:06 +00:00
|
|
|
if (loadSynchronously) {
|
|
|
|
loadApplicationSync(nullptr, std::move(string), std::move(sourceURL));
|
|
|
|
} else {
|
2017-05-25 23:37:38 +00:00
|
|
|
loadApplication(nullptr, std::move(string), std::move(sourceURL));
|
2017-05-24 14:27:06 +00:00
|
|
|
}
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::loadUnbundle(std::unique_ptr<JSModulesUnbundle> unbundle,
|
2016-05-14 00:15:06 +00:00
|
|
|
std::unique_ptr<const JSBigString> startupScript,
|
2017-05-24 14:27:06 +00:00
|
|
|
std::string startupScriptSourceURL,
|
|
|
|
bool loadSynchronously) {
|
|
|
|
if (loadSynchronously) {
|
|
|
|
loadApplicationSync(std::move(unbundle), std::move(startupScript),
|
|
|
|
std::move(startupScriptSourceURL));
|
|
|
|
} else {
|
2017-08-10 17:44:10 +00:00
|
|
|
loadApplication(std::move(unbundle), std::move(startupScript),
|
|
|
|
std::move(startupScriptSourceURL));
|
2017-05-24 14:27:06 +00:00
|
|
|
}
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-14 00:15:06 +00:00
|
|
|
void Instance::setGlobalVariable(std::string propName,
|
|
|
|
std::unique_ptr<const JSBigString> jsonValue) {
|
2017-08-10 17:44:10 +00:00
|
|
|
nativeToJsBridge_->setGlobalVariable(std::move(propName),
|
|
|
|
std::move(jsonValue));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-10-26 10:43:05 +00:00
|
|
|
void *Instance::getJavaScriptContext() {
|
2017-08-10 17:44:10 +00:00
|
|
|
return nativeToJsBridge_ ? nativeToJsBridge_->getJavaScriptContext()
|
|
|
|
: nullptr;
|
2016-10-26 10:43:05 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 17:44:10 +00:00
|
|
|
void Instance::callJSFunction(std::string &&module, std::string &&method,
|
|
|
|
folly::dynamic &¶ms) {
|
2016-05-04 02:29:58 +00:00
|
|
|
callback_->incrementPendingJSCalls();
|
2017-08-10 17:44:10 +00:00
|
|
|
nativeToJsBridge_->callFunction(std::move(module), std::move(method),
|
|
|
|
std::move(params));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 17:44:10 +00:00
|
|
|
void Instance::callJSCallback(uint64_t callbackId, folly::dynamic &¶ms) {
|
2016-05-14 00:15:06 +00:00
|
|
|
SystraceSection s("<callback>");
|
2016-05-04 02:29:58 +00:00
|
|
|
callback_->incrementPendingJSCalls();
|
2017-08-10 17:44:10 +00:00
|
|
|
nativeToJsBridge_->invokeCallback((double)callbackId, std::move(params));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-10 17:44:10 +00:00
|
|
|
const ModuleRegistry &Instance::getModuleRegistry() const {
|
|
|
|
return *moduleRegistry_;
|
|
|
|
}
|
|
|
|
|
|
|
|
ModuleRegistry &Instance::getModuleRegistry() { return *moduleRegistry_; }
|
|
|
|
|
2017-06-26 12:48:40 +00:00
|
|
|
#ifdef WITH_JSC_MEMORY_PRESSURE
|
|
|
|
void Instance::handleMemoryPressure(int pressureLevel) {
|
|
|
|
nativeToJsBridge_->handleMemoryPressure(pressureLevel);
|
2016-05-26 18:07:41 +00:00
|
|
|
}
|
2017-06-26 12:48:40 +00:00
|
|
|
#endif
|
2016-05-26 18:07:41 +00:00
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
} // namespace react
|
|
|
|
} // namespace facebook
|