mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Replace folly::make_unique with std::make_unique (#26730)
Summary: There is a mixed usage of `folly::make_unique` and `std::make_unique`. Soon, `folly::make_unique` may be removed (see [this PR](https://github.com/facebook/folly/pull/1150)). Since `react-native` only supports C++14-compilers and later, switch to always using `std::make_unique`. ## Changelog [Internal] [Removed] - Replace folly::make_unique with std::make_unique Pull Request resolved: https://github.com/facebook/react-native/pull/26730 Test Plan: Running the existing test suite. No change in behavior is expected. Joshua Gross: buck install -r fb4a, make sure MP Home and forced teardown works okay on android Reviewed By: shergin Differential Revision: D18062400 Pulled By: JoshuaGross fbshipit-source-id: 978ca794c7e972db872a8dcc57c31bdec7451481
This commit is contained in:
committed by
Facebook Github Bot
parent
468d1a2d2e
commit
ba18ee9b87
@@ -7,18 +7,19 @@
|
||||
|
||||
#include "NativeToJsBridge.h"
|
||||
|
||||
#include <folly/json.h>
|
||||
#include <folly/Memory.h>
|
||||
#include <folly/MoveWrapper.h>
|
||||
#include <folly/json.h>
|
||||
#include <glog/logging.h>
|
||||
|
||||
#include "Instance.h"
|
||||
#include "JSBigString.h"
|
||||
#include "SystraceSection.h"
|
||||
#include "MethodCall.h"
|
||||
#include "MessageQueueThread.h"
|
||||
#include "MethodCall.h"
|
||||
#include "ModuleRegistry.h"
|
||||
#include "RAMBundleRegistry.h"
|
||||
#include "SystraceSection.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#ifdef WITH_FBSYSTRACE
|
||||
#include <fbsystrace.h>
|
||||
@@ -30,37 +31,40 @@ namespace react {
|
||||
|
||||
// This class manages calls from JS to native code.
|
||||
class JsToNativeBridge : public react::ExecutorDelegate {
|
||||
public:
|
||||
JsToNativeBridge(std::shared_ptr<ModuleRegistry> registry,
|
||||
std::shared_ptr<InstanceCallback> callback)
|
||||
: m_registry(registry)
|
||||
, m_callback(callback) {}
|
||||
public:
|
||||
JsToNativeBridge(
|
||||
std::shared_ptr<ModuleRegistry> registry,
|
||||
std::shared_ptr<InstanceCallback> callback)
|
||||
: m_registry(registry), m_callback(callback) {}
|
||||
|
||||
std::shared_ptr<ModuleRegistry> getModuleRegistry() override {
|
||||
return m_registry;
|
||||
}
|
||||
|
||||
|
||||
bool isBatchActive() {
|
||||
return m_batchHadNativeModuleCalls;
|
||||
}
|
||||
|
||||
void callNativeModules(
|
||||
__unused JSExecutor& executor, folly::dynamic&& calls, bool isEndOfBatch) override {
|
||||
|
||||
CHECK(m_registry || calls.empty()) <<
|
||||
"native module calls cannot be completed with no native modules";
|
||||
__unused JSExecutor &executor,
|
||||
folly::dynamic &&calls,
|
||||
bool isEndOfBatch) override {
|
||||
CHECK(m_registry || calls.empty())
|
||||
<< "native module calls cannot be completed with no native modules";
|
||||
m_batchHadNativeModuleCalls = m_batchHadNativeModuleCalls || !calls.empty();
|
||||
|
||||
// An exception anywhere in here stops processing of the batch. This
|
||||
// was the behavior of the Android bridge, and since exception handling
|
||||
// terminates the whole bridge, there's not much point in continuing.
|
||||
for (auto& call : parseMethodCalls(std::move(calls))) {
|
||||
m_registry->callNativeMethod(call.moduleId, call.methodId, std::move(call.arguments), call.callId);
|
||||
for (auto &call : parseMethodCalls(std::move(calls))) {
|
||||
m_registry->callNativeMethod(
|
||||
call.moduleId, call.methodId, std::move(call.arguments), call.callId);
|
||||
}
|
||||
if (isEndOfBatch) {
|
||||
// onBatchComplete will be called on the native (module) queue, but
|
||||
// decrementPendingJSCalls will be called sync. Be aware that the bridge may still
|
||||
// be processing native calls when the bridge idle signaler fires.
|
||||
// decrementPendingJSCalls will be called sync. Be aware that the bridge
|
||||
// may still be processing native calls when the bridge idle signaler
|
||||
// fires.
|
||||
if (m_batchHadNativeModuleCalls) {
|
||||
m_callback->onBatchComplete();
|
||||
m_batchHadNativeModuleCalls = false;
|
||||
@@ -70,13 +74,15 @@ public:
|
||||
}
|
||||
|
||||
MethodCallResult callSerializableNativeHook(
|
||||
__unused JSExecutor& executor, unsigned int moduleId, unsigned int methodId,
|
||||
folly::dynamic&& args) override {
|
||||
return m_registry->callSerializableNativeHook(moduleId, methodId, std::move(args));
|
||||
__unused JSExecutor &executor,
|
||||
unsigned int moduleId,
|
||||
unsigned int methodId,
|
||||
folly::dynamic &&args) override {
|
||||
return m_registry->callSerializableNativeHook(
|
||||
moduleId, methodId, std::move(args));
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
private:
|
||||
// These methods are always invoked from an Executor. The NativeToJsBridge
|
||||
// keeps a reference to the executor, and when destroy() is called, the
|
||||
// executor is destroyed synchronously on its queue.
|
||||
@@ -98,33 +104,32 @@ NativeToJsBridge::NativeToJsBridge(
|
||||
|
||||
// This must be called on the same thread on which the constructor was called.
|
||||
NativeToJsBridge::~NativeToJsBridge() {
|
||||
CHECK(*m_destroyed) <<
|
||||
"NativeToJsBridge::destroy() must be called before deallocating the NativeToJsBridge!";
|
||||
CHECK(*m_destroyed)
|
||||
<< "NativeToJsBridge::destroy() must be called before deallocating the NativeToJsBridge!";
|
||||
}
|
||||
|
||||
void NativeToJsBridge::loadApplication(
|
||||
std::unique_ptr<RAMBundleRegistry> bundleRegistry,
|
||||
std::unique_ptr<const JSBigString> startupScript,
|
||||
std::string startupScriptSourceURL) {
|
||||
|
||||
runOnExecutorQueue(
|
||||
[this,
|
||||
bundleRegistryWrap=folly::makeMoveWrapper(std::move(bundleRegistry)),
|
||||
startupScript=folly::makeMoveWrapper(std::move(startupScript)),
|
||||
startupScriptSourceURL=std::move(startupScriptSourceURL)]
|
||||
(JSExecutor* executor) mutable {
|
||||
auto bundleRegistry = bundleRegistryWrap.move();
|
||||
if (bundleRegistry) {
|
||||
executor->setBundleRegistry(std::move(bundleRegistry));
|
||||
}
|
||||
try {
|
||||
executor->loadApplicationScript(std::move(*startupScript),
|
||||
std::move(startupScriptSourceURL));
|
||||
} catch (...) {
|
||||
m_applicationScriptHasFailure = true;
|
||||
throw;
|
||||
}
|
||||
});
|
||||
bundleRegistryWrap = folly::makeMoveWrapper(std::move(bundleRegistry)),
|
||||
startupScript = folly::makeMoveWrapper(std::move(startupScript)),
|
||||
startupScriptSourceURL =
|
||||
std::move(startupScriptSourceURL)](JSExecutor *executor) mutable {
|
||||
auto bundleRegistry = bundleRegistryWrap.move();
|
||||
if (bundleRegistry) {
|
||||
executor->setBundleRegistry(std::move(bundleRegistry));
|
||||
}
|
||||
try {
|
||||
executor->loadApplicationScript(
|
||||
std::move(*startupScript), std::move(startupScriptSourceURL));
|
||||
} catch (...) {
|
||||
m_applicationScriptHasFailure = true;
|
||||
throw;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void NativeToJsBridge::loadApplicationSync(
|
||||
@@ -135,8 +140,8 @@ void NativeToJsBridge::loadApplicationSync(
|
||||
m_executor->setBundleRegistry(std::move(bundleRegistry));
|
||||
}
|
||||
try {
|
||||
m_executor->loadApplicationScript(std::move(startupScript),
|
||||
std::move(startupScriptSourceURL));
|
||||
m_executor->loadApplicationScript(
|
||||
std::move(startupScript), std::move(startupScriptSourceURL));
|
||||
} catch (...) {
|
||||
m_applicationScriptHasFailure = true;
|
||||
throw;
|
||||
@@ -144,99 +149,110 @@ void NativeToJsBridge::loadApplicationSync(
|
||||
}
|
||||
|
||||
void NativeToJsBridge::callFunction(
|
||||
std::string&& module,
|
||||
std::string&& method,
|
||||
folly::dynamic&& arguments) {
|
||||
std::string &&module,
|
||||
std::string &&method,
|
||||
folly::dynamic &&arguments) {
|
||||
int systraceCookie = -1;
|
||||
#ifdef WITH_FBSYSTRACE
|
||||
#ifdef WITH_FBSYSTRACE
|
||||
systraceCookie = m_systraceCookie++;
|
||||
FbSystraceAsyncFlow::begin(
|
||||
TRACE_TAG_REACT_CXX_BRIDGE,
|
||||
"JSCall",
|
||||
systraceCookie);
|
||||
#endif
|
||||
TRACE_TAG_REACT_CXX_BRIDGE, "JSCall", systraceCookie);
|
||||
#endif
|
||||
|
||||
runOnExecutorQueue([this, module = std::move(module), method = std::move(method), arguments = std::move(arguments), systraceCookie]
|
||||
(JSExecutor* executor) {
|
||||
if (m_applicationScriptHasFailure) {
|
||||
LOG(ERROR) << "Attempting to call JS function on a bad application bundle: " << module.c_str() << "." << method.c_str() << "()";
|
||||
throw std::runtime_error("Attempting to call JS function on a bad application bundle: " + module + "." + method + "()");
|
||||
}
|
||||
runOnExecutorQueue([this,
|
||||
module = std::move(module),
|
||||
method = std::move(method),
|
||||
arguments = std::move(arguments),
|
||||
systraceCookie](JSExecutor *executor) {
|
||||
if (m_applicationScriptHasFailure) {
|
||||
LOG(ERROR)
|
||||
<< "Attempting to call JS function on a bad application bundle: "
|
||||
<< module.c_str() << "." << method.c_str() << "()";
|
||||
throw std::runtime_error(
|
||||
"Attempting to call JS function on a bad application bundle: " +
|
||||
module + "." + method + "()");
|
||||
}
|
||||
|
||||
#ifdef WITH_FBSYSTRACE
|
||||
FbSystraceAsyncFlow::end(
|
||||
TRACE_TAG_REACT_CXX_BRIDGE,
|
||||
"JSCall",
|
||||
systraceCookie);
|
||||
SystraceSection s("NativeToJsBridge::callFunction", "module", module, "method", method);
|
||||
#else
|
||||
(void)(systraceCookie);
|
||||
#endif
|
||||
// This is safe because we are running on the executor's thread: it won't
|
||||
// destruct until after it's been unregistered (which we check above) and
|
||||
// that will happen on this thread
|
||||
executor->callFunction(module, method, arguments);
|
||||
});
|
||||
#ifdef WITH_FBSYSTRACE
|
||||
FbSystraceAsyncFlow::end(
|
||||
TRACE_TAG_REACT_CXX_BRIDGE, "JSCall", systraceCookie);
|
||||
SystraceSection s(
|
||||
"NativeToJsBridge::callFunction", "module", module, "method", method);
|
||||
#else
|
||||
(void)(systraceCookie);
|
||||
#endif
|
||||
// This is safe because we are running on the executor's thread: it won't
|
||||
// destruct until after it's been unregistered (which we check above) and
|
||||
// that will happen on this thread
|
||||
executor->callFunction(module, method, arguments);
|
||||
});
|
||||
}
|
||||
|
||||
void NativeToJsBridge::invokeCallback(double callbackId, folly::dynamic&& arguments) {
|
||||
void NativeToJsBridge::invokeCallback(
|
||||
double callbackId,
|
||||
folly::dynamic &&arguments) {
|
||||
int systraceCookie = -1;
|
||||
#ifdef WITH_FBSYSTRACE
|
||||
#ifdef WITH_FBSYSTRACE
|
||||
systraceCookie = m_systraceCookie++;
|
||||
FbSystraceAsyncFlow::begin(
|
||||
TRACE_TAG_REACT_CXX_BRIDGE,
|
||||
"<callback>",
|
||||
systraceCookie);
|
||||
#endif
|
||||
TRACE_TAG_REACT_CXX_BRIDGE, "<callback>", systraceCookie);
|
||||
#endif
|
||||
|
||||
runOnExecutorQueue([this, callbackId, arguments = std::move(arguments), systraceCookie]
|
||||
(JSExecutor* executor) {
|
||||
if (m_applicationScriptHasFailure) {
|
||||
LOG(ERROR) << "Attempting to call JS callback on a bad application bundle: " << callbackId;
|
||||
throw std::runtime_error("Attempting to invoke JS callback on a bad application bundle.");
|
||||
}
|
||||
#ifdef WITH_FBSYSTRACE
|
||||
FbSystraceAsyncFlow::end(
|
||||
TRACE_TAG_REACT_CXX_BRIDGE,
|
||||
"<callback>",
|
||||
systraceCookie);
|
||||
SystraceSection s("NativeToJsBridge::invokeCallback");
|
||||
#else
|
||||
(void)(systraceCookie);
|
||||
#endif
|
||||
executor->invokeCallback(callbackId, arguments);
|
||||
});
|
||||
runOnExecutorQueue(
|
||||
[this, callbackId, arguments = std::move(arguments), systraceCookie](
|
||||
JSExecutor *executor) {
|
||||
if (m_applicationScriptHasFailure) {
|
||||
LOG(ERROR)
|
||||
<< "Attempting to call JS callback on a bad application bundle: "
|
||||
<< callbackId;
|
||||
throw std::runtime_error(
|
||||
"Attempting to invoke JS callback on a bad application bundle.");
|
||||
}
|
||||
#ifdef WITH_FBSYSTRACE
|
||||
FbSystraceAsyncFlow::end(
|
||||
TRACE_TAG_REACT_CXX_BRIDGE, "<callback>", systraceCookie);
|
||||
SystraceSection s("NativeToJsBridge::invokeCallback");
|
||||
#else
|
||||
(void)(systraceCookie);
|
||||
#endif
|
||||
executor->invokeCallback(callbackId, arguments);
|
||||
});
|
||||
}
|
||||
|
||||
void NativeToJsBridge::registerBundle(uint32_t bundleId, const std::string& bundlePath) {
|
||||
runOnExecutorQueue([bundleId, bundlePath] (JSExecutor* executor) {
|
||||
void NativeToJsBridge::registerBundle(
|
||||
uint32_t bundleId,
|
||||
const std::string &bundlePath) {
|
||||
runOnExecutorQueue([bundleId, bundlePath](JSExecutor *executor) {
|
||||
executor->registerBundle(bundleId, bundlePath);
|
||||
});
|
||||
}
|
||||
|
||||
void NativeToJsBridge::setGlobalVariable(std::string propName,
|
||||
std::unique_ptr<const JSBigString> jsonValue) {
|
||||
runOnExecutorQueue([propName=std::move(propName), jsonValue=folly::makeMoveWrapper(std::move(jsonValue))]
|
||||
(JSExecutor* executor) mutable {
|
||||
executor->setGlobalVariable(propName, jsonValue.move());
|
||||
});
|
||||
void NativeToJsBridge::setGlobalVariable(
|
||||
std::string propName,
|
||||
std::unique_ptr<const JSBigString> jsonValue) {
|
||||
runOnExecutorQueue([propName = std::move(propName),
|
||||
jsonValue = folly::makeMoveWrapper(std::move(jsonValue))](
|
||||
JSExecutor *executor) mutable {
|
||||
executor->setGlobalVariable(propName, jsonValue.move());
|
||||
});
|
||||
}
|
||||
|
||||
void* NativeToJsBridge::getJavaScriptContext() {
|
||||
// TODO(cjhopman): this seems unsafe unless we require that it is only called on the main js queue.
|
||||
void *NativeToJsBridge::getJavaScriptContext() {
|
||||
// TODO(cjhopman): this seems unsafe unless we require that it is only called
|
||||
// on the main js queue.
|
||||
return m_executor->getJavaScriptContext();
|
||||
}
|
||||
|
||||
bool NativeToJsBridge::isInspectable() {
|
||||
return m_inspectable;
|
||||
}
|
||||
|
||||
|
||||
bool NativeToJsBridge::isBatchActive() {
|
||||
return m_delegate->isBatchActive();
|
||||
}
|
||||
|
||||
void NativeToJsBridge::handleMemoryPressure(int pressureLevel) {
|
||||
runOnExecutorQueue([=] (JSExecutor* executor) {
|
||||
runOnExecutorQueue([=](JSExecutor *executor) {
|
||||
executor->handleMemoryPressure(pressureLevel);
|
||||
});
|
||||
}
|
||||
@@ -253,23 +269,27 @@ void NativeToJsBridge::destroy() {
|
||||
});
|
||||
}
|
||||
|
||||
void NativeToJsBridge::runOnExecutorQueue(std::function<void(JSExecutor*)> task) {
|
||||
void NativeToJsBridge::runOnExecutorQueue(
|
||||
std::function<void(JSExecutor *)> task) {
|
||||
if (*m_destroyed) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::shared_ptr<bool> isDestroyed = m_destroyed;
|
||||
m_executorMessageQueueThread->runOnQueue([this, isDestroyed, task=std::move(task)] {
|
||||
if (*isDestroyed) {
|
||||
return;
|
||||
}
|
||||
m_executorMessageQueueThread->runOnQueue(
|
||||
[this, isDestroyed, task = std::move(task)] {
|
||||
if (*isDestroyed) {
|
||||
return;
|
||||
}
|
||||
|
||||
// The executor is guaranteed to be valid for the duration of the task because:
|
||||
// 1. the executor is only destroyed after it is unregistered
|
||||
// 2. the executor is unregistered on this queue
|
||||
// 3. we just confirmed that the executor hasn't been unregistered above
|
||||
task(m_executor.get());
|
||||
});
|
||||
// The executor is guaranteed to be valid for the duration of the task
|
||||
// because:
|
||||
// 1. the executor is only destroyed after it is unregistered
|
||||
// 2. the executor is unregistered on this queue
|
||||
// 3. we just confirmed that the executor hasn't been unregistered above
|
||||
task(m_executor.get());
|
||||
});
|
||||
}
|
||||
|
||||
} }
|
||||
} // namespace react
|
||||
} // namespace facebook
|
||||
|
||||
Reference in New Issue
Block a user