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-11-03 12:17:23 +00:00
|
|
|
#include "JSBundleType.h"
|
2017-06-23 23:49:55 +00:00
|
|
|
#include "JSExecutor.h"
|
2017-08-10 17:44:10 +00:00
|
|
|
#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-09-22 16:56:50 +00:00
|
|
|
#include "RAMBundleRegistry.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
|
|
|
|
2017-11-03 12:17:23 +00:00
|
|
|
#include <cxxreact/JSIndexedRAMBundle.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>
|
2017-11-03 12:17:23 +00:00
|
|
|
#include <fstream>
|
2016-05-04 02:29:58 +00:00
|
|
|
#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-09-22 16:56:50 +00:00
|
|
|
void Instance::loadApplication(std::unique_ptr<RAMBundleRegistry> bundleRegistry,
|
2017-08-10 17:44:10 +00:00
|
|
|
std::unique_ptr<const JSBigString> string,
|
|
|
|
std::string sourceURL) {
|
2016-05-04 02:29:58 +00:00
|
|
|
callback_->incrementPendingJSCalls();
|
2017-08-30 10:36:23 +00:00
|
|
|
SystraceSection s("Instance::loadApplication", "sourceURL",
|
2017-08-10 17:44:10 +00:00
|
|
|
sourceURL);
|
2017-09-22 16:56:50 +00:00
|
|
|
nativeToJsBridge_->loadApplication(std::move(bundleRegistry), std::move(string),
|
2017-08-10 17:44:10 +00:00
|
|
|
std::move(sourceURL));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 16:56:50 +00:00
|
|
|
void Instance::loadApplicationSync(std::unique_ptr<RAMBundleRegistry> bundleRegistry,
|
2017-08-10 17:44:10 +00:00
|
|
|
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-30 10:36:23 +00:00
|
|
|
SystraceSection s("Instance::loadApplicationSync", "sourceURL",
|
2017-08-10 17:44:10 +00:00
|
|
|
sourceURL);
|
2017-09-22 16:56:50 +00:00
|
|
|
nativeToJsBridge_->loadApplicationSync(std::move(bundleRegistry), std::move(string),
|
2017-08-10 17:44:10 +00:00
|
|
|
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-08-30 10:36:23 +00:00
|
|
|
SystraceSection s("Instance::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-30 10:36:23 +00:00
|
|
|
SystraceSection s("Instance::loadScriptFromString", "sourceURL",
|
2017-08-10 17:44:10 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-03 12:17:23 +00:00
|
|
|
bool Instance::isIndexedRAMBundle(const char *sourcePath) {
|
|
|
|
std::ifstream bundle_stream(sourcePath, std::ios_base::in);
|
|
|
|
BundleHeader header;
|
|
|
|
|
|
|
|
if (!bundle_stream ||
|
|
|
|
!bundle_stream.read(reinterpret_cast<char *>(&header), sizeof(header))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return parseTypeFromHeader(header) == ScriptTag::RAMBundle;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Instance::loadRAMBundleFromFile(const std::string& sourcePath,
|
|
|
|
const std::string& sourceURL,
|
|
|
|
bool loadSynchronously) {
|
|
|
|
auto bundle = folly::make_unique<JSIndexedRAMBundle>(sourcePath.c_str());
|
|
|
|
auto startupScript = bundle->getStartupCode();
|
2017-11-09 19:55:41 +00:00
|
|
|
auto registry = RAMBundleRegistry::multipleBundlesRegistry(std::move(bundle), JSIndexedRAMBundle::buildFactory());
|
2017-11-03 12:17:23 +00:00
|
|
|
loadRAMBundle(
|
|
|
|
std::move(registry),
|
|
|
|
std::move(startupScript),
|
|
|
|
sourceURL,
|
|
|
|
loadSynchronously);
|
|
|
|
}
|
|
|
|
|
2017-09-22 16:56:50 +00:00
|
|
|
void Instance::loadRAMBundle(std::unique_ptr<RAMBundleRegistry> bundleRegistry,
|
|
|
|
std::unique_ptr<const JSBigString> startupScript,
|
|
|
|
std::string startupScriptSourceURL,
|
|
|
|
bool loadSynchronously) {
|
2017-05-24 14:27:06 +00:00
|
|
|
if (loadSynchronously) {
|
2017-09-22 16:56:50 +00:00
|
|
|
loadApplicationSync(std::move(bundleRegistry), std::move(startupScript),
|
2017-05-24 14:27:06 +00:00
|
|
|
std::move(startupScriptSourceURL));
|
|
|
|
} else {
|
2017-09-22 16:56:50 +00:00
|
|
|
loadApplication(std::move(bundleRegistry), std::move(startupScript),
|
2017-08-10 17:44:10 +00:00
|
|
|
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-12-18 21:19:37 +00:00
|
|
|
bool Instance::isInspectable() {
|
|
|
|
return nativeToJsBridge_ ? nativeToJsBridge_->isInspectable() : false;
|
|
|
|
}
|
|
|
|
|
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) {
|
2017-08-30 10:36:23 +00:00
|
|
|
SystraceSection s("Instance::callJSCallback");
|
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-11-09 19:55:37 +00:00
|
|
|
void Instance::registerBundle(uint32_t bundleId, const std::string& bundlePath) {
|
|
|
|
nativeToJsBridge_->registerBundle(bundleId, bundlePath);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
void Instance::handleMemoryPressure(int pressureLevel) {
|
|
|
|
nativeToJsBridge_->handleMemoryPressure(pressureLevel);
|
2016-05-26 18:07:41 +00:00
|
|
|
}
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
} // namespace react
|
|
|
|
} // namespace facebook
|