// Copyright 2004-present Facebook. All Rights Reserved. #include "Instance.h" #include "Executor.h" #include "MethodCall.h" #include "RecoverableError.h" #include "SystraceSection.h" #include #include #include #include #include #include #include namespace facebook { namespace react { using namespace detail; Instance::~Instance() { if (nativeToJsBridge_) { nativeToJsBridge_->destroy(); } } void Instance::initializeBridge( std::unique_ptr callback, std::shared_ptr jsef, std::shared_ptr jsQueue, std::unique_ptr nativeQueue, std::shared_ptr moduleRegistry) { callback_ = std::move(callback); jsQueue->runOnQueueSync( [this, &jsef, moduleRegistry, jsQueue, nativeQueue=folly::makeMoveWrapper(std::move(nativeQueue))] () mutable { nativeToJsBridge_ = folly::make_unique( jsef.get(), moduleRegistry, jsQueue, nativeQueue.move(), callback_); std::lock_guard lock(m_syncMutex); m_syncReady = true; m_syncCV.notify_all(); }); CHECK(nativeToJsBridge_); } void Instance::setSourceURL(std::string sourceURL) { callback_->incrementPendingJSCalls(); SystraceSection s("reactbridge_xplat_setSourceURL", "sourceURL", sourceURL); nativeToJsBridge_->loadApplication(nullptr, nullptr, std::move(sourceURL)); } void Instance::loadScriptFromString(std::unique_ptr string, std::string sourceURL) { callback_->incrementPendingJSCalls(); SystraceSection s("reactbridge_xplat_loadScriptFromString", "sourceURL", sourceURL); nativeToJsBridge_->loadApplication(nullptr, std::move(string), std::move(sourceURL)); } void Instance::loadScriptFromStringSync(std::unique_ptr string, std::string sourceURL) { std::unique_lock lock(m_syncMutex); m_syncCV.wait(lock, [this] { return m_syncReady; }); nativeToJsBridge_->loadApplicationSync(nullptr, std::move(string), std::move(sourceURL)); } void Instance::loadScriptFromFile(const std::string& filename, const std::string& sourceURL) { callback_->incrementPendingJSCalls(); SystraceSection s("reactbridge_xplat_loadScriptFromFile", "fileName", filename); std::unique_ptr script; RecoverableError::runRethrowingAsRecoverable( [&filename, &script]() { script = JSBigFileString::fromPath(filename); }); nativeToJsBridge_->loadApplication(nullptr, std::move(script), sourceURL); } void Instance::loadUnbundle(std::unique_ptr unbundle, std::unique_ptr startupScript, std::string startupScriptSourceURL) { callback_->incrementPendingJSCalls(); nativeToJsBridge_->loadApplication(std::move(unbundle), std::move(startupScript), std::move(startupScriptSourceURL)); } void Instance::loadUnbundleSync(std::unique_ptr unbundle, std::unique_ptr startupScript, std::string startupScriptSourceURL) { std::unique_lock lock(m_syncMutex); m_syncCV.wait(lock, [this] { return m_syncReady; }); SystraceSection s("reactbridge_xplat_loadApplicationSync"); nativeToJsBridge_->loadApplicationSync(std::move(unbundle), std::move(startupScript), std::move(startupScriptSourceURL)); } bool Instance::supportsProfiling() { return nativeToJsBridge_->supportsProfiling(); } void Instance::startProfiler(const std::string& title) { return nativeToJsBridge_->startProfiler(title); } void Instance::stopProfiler(const std::string& title, const std::string& filename) { return nativeToJsBridge_->stopProfiler(title, filename); } void Instance::setGlobalVariable(std::string propName, std::unique_ptr jsonValue) { nativeToJsBridge_->setGlobalVariable(std::move(propName), std::move(jsonValue)); } void *Instance::getJavaScriptContext() { return nativeToJsBridge_->getJavaScriptContext(); } void Instance::callJSFunction(ExecutorToken token, std::string&& module, std::string&& method, folly::dynamic&& params) { callback_->incrementPendingJSCalls(); nativeToJsBridge_->callFunction(token, std::move(module), std::move(method), std::move(params)); } void Instance::callJSCallback(ExecutorToken token, uint64_t callbackId, folly::dynamic&& params) { SystraceSection s(""); callback_->incrementPendingJSCalls(); nativeToJsBridge_->invokeCallback(token, (double) callbackId, std::move(params)); } ExecutorToken Instance::getMainExecutorToken() { return nativeToJsBridge_->getMainExecutorToken(); } void Instance::handleMemoryPressureUiHidden() { nativeToJsBridge_->handleMemoryPressureUiHidden(); } void Instance::handleMemoryPressureModerate() { nativeToJsBridge_->handleMemoryPressureModerate(); } void Instance::handleMemoryPressureCritical() { nativeToJsBridge_->handleMemoryPressureCritical(); } } // namespace react } // namespace facebook