From ba18ee9b87a35fccec4385a67d393ab4a09f5b29 Mon Sep 17 00:00:00 2001 From: Joe Loser Date: Tue, 22 Oct 2019 12:19:37 -0700 Subject: [PATCH] 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 --- React/CxxBridge/JSCExecutorFactory.mm | 4 +- React/CxxModule/RCTCxxMethod.mm | 5 +- .../facebook/hermes/reactexecutor/OnLoad.cpp | 7 +- .../com/facebook/react/jscexecutor/OnLoad.cpp | 7 +- .../jni/react/jni/CatalystInstanceImpl.cpp | 8 +- .../main/jni/react/jni/CatalystInstanceImpl.h | 2 +- .../src/main/jni/react/jni/JInspector.cpp | 4 +- .../src/main/jni/react/jni/JInspector.h | 3 +- .../jni/react/jni/JMessageQueueThread.cpp | 1 - .../src/main/jni/react/jni/JSLoader.cpp | 4 +- .../jni/react/jni/JniJSModulesUnbundle.cpp | 4 +- .../jni/react/jni/ModuleRegistryBuilder.cpp | 6 +- .../src/main/jni/react/jni/ProxyExecutor.cpp | 7 +- ReactCommon/cxxreact/Instance.cpp | 138 +++++----- ReactCommon/cxxreact/JSBigString.cpp | 4 +- ReactCommon/cxxreact/JSDeltaBundleClient.cpp | 27 +- ReactCommon/cxxreact/JSIndexedRAMBundle.cpp | 65 +++-- ReactCommon/cxxreact/NativeToJsBridge.cpp | 258 ++++++++++-------- ReactCommon/cxxreact/RAMBundleRegistry.cpp | 37 +-- ReactCommon/cxxreact/SampleCxxModule.cpp | 162 ++++++----- 20 files changed, 412 insertions(+), 341 deletions(-) diff --git a/React/CxxBridge/JSCExecutorFactory.mm b/React/CxxBridge/JSCExecutorFactory.mm index 75d19536300..12d96eb5c16 100644 --- a/React/CxxBridge/JSCExecutorFactory.mm +++ b/React/CxxBridge/JSCExecutorFactory.mm @@ -10,6 +10,8 @@ #import #import +#import + namespace facebook { namespace react { @@ -28,7 +30,7 @@ std::unique_ptr JSCExecutorFactory::createJSExecutor( runtimeInstaller(runtime); } }; - return folly::make_unique( + return std::make_unique( facebook::jsc::makeJSCRuntime(), delegate, JSIExecutor::defaultTimeoutInvoker, diff --git a/React/CxxModule/RCTCxxMethod.mm b/React/CxxModule/RCTCxxMethod.mm index 351e6801f3b..40d6be72c4a 100644 --- a/React/CxxModule/RCTCxxMethod.mm +++ b/React/CxxModule/RCTCxxMethod.mm @@ -12,10 +12,11 @@ #import #import #import -#import #import "RCTCxxUtils.h" +#import + using facebook::xplat::module::CxxModule; using namespace facebook::react; @@ -27,7 +28,7 @@ using namespace facebook::react; - (instancetype)initWithCxxMethod:(const CxxModule::Method &)method { if ((self = [super init])) { - _method = folly::make_unique(method); + _method = std::make_unique(method); } return self; } diff --git a/ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/OnLoad.cpp b/ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/OnLoad.cpp index f3a6a719ebb..9dd04e882f0 100644 --- a/ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/OnLoad.cpp +++ b/ReactAndroid/src/main/java/com/facebook/hermes/reactexecutor/OnLoad.cpp @@ -8,7 +8,6 @@ #include <../instrumentation/HermesMemoryDumper.h> #include #include -#include #include #include #include @@ -16,6 +15,8 @@ #include #include +#include + namespace facebook { namespace react { @@ -103,7 +104,7 @@ class HermesExecutorHolder JReactMarker::setLogPerfMarkerIfNeeded(); return makeCxxInstance( - folly::make_unique(installBindings)); + std::make_unique(installBindings)); } static jni::local_ref initHybrid( @@ -124,7 +125,7 @@ class HermesExecutorHolder heapDumper, tripWireCooldownMS, tripWireLimitBytes); - return makeCxxInstance(folly::make_unique( + return makeCxxInstance(std::make_unique( installBindings, JSIExecutor::defaultTimeoutInvoker, runtimeConfig)); } diff --git a/ReactAndroid/src/main/java/com/facebook/react/jscexecutor/OnLoad.cpp b/ReactAndroid/src/main/java/com/facebook/react/jscexecutor/OnLoad.cpp index 5160cc75a4b..af724e282d6 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/jscexecutor/OnLoad.cpp +++ b/ReactAndroid/src/main/java/com/facebook/react/jscexecutor/OnLoad.cpp @@ -6,7 +6,6 @@ */ #include -#include #include #include #include @@ -14,6 +13,8 @@ #include #include +#include + namespace facebook { namespace react { @@ -30,7 +31,7 @@ class JSCExecutorFactory : public JSExecutorFactory { &reactAndroidLoggingHook); react::bindNativeLogger(runtime, androidLogger); }; - return folly::make_unique( + return std::make_unique( jsc::makeJSCRuntime(), delegate, JSIExecutor::defaultTimeoutInvoker, @@ -56,7 +57,7 @@ class JSCExecutorHolder // Android. JReactMarker::setLogPerfMarkerIfNeeded(); // TODO mhorowitz T28461666 fill in some missing nice to have glue - return makeCxxInstance(folly::make_unique()); + return makeCxxInstance(std::make_unique()); } static void registerNatives() { diff --git a/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp b/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp index 3a276ce0560..07ea9e43a92 100644 --- a/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp +++ b/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.cpp @@ -8,6 +8,7 @@ #include "CatalystInstanceImpl.h" #include +#include #include #include #include @@ -25,7 +26,6 @@ #include #include #include -#include #include #include #include @@ -93,7 +93,7 @@ CatalystInstanceImpl::initHybrid(jni::alias_ref) { } CatalystInstanceImpl::CatalystInstanceImpl() - : instance_(folly::make_unique()) {} + : instance_(std::make_unique()) {} CatalystInstanceImpl::~CatalystInstanceImpl() { if (moduleMessageQueue_ != NULL) { @@ -181,7 +181,7 @@ void CatalystInstanceImpl::initializeBridge( instance_->initializeBridge( std::make_unique(callback, moduleMessageQueue_), jseh->getExecutorFactory(), - folly::make_unique(jsQueue), + std::make_unique(jsQueue), moduleRegistry_); } @@ -276,7 +276,7 @@ void CatalystInstanceImpl::setGlobalVariable( instance_->setGlobalVariable( std::move(propName), - folly::make_unique(std::move(jsonValue))); + std::make_unique(std::move(jsonValue))); } jlong CatalystInstanceImpl::getJavaScriptContext() { diff --git a/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.h b/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.h index f875965149c..6eca067a4b1 100644 --- a/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.h +++ b/ReactAndroid/src/main/jni/react/jni/CatalystInstanceImpl.h @@ -5,12 +5,12 @@ * LICENSE file in the root directory of this source tree. */ +#include #include #include #include #include -#include #include "CxxModuleWrapper.h" #include "JMessageQueueThread.h" diff --git a/ReactAndroid/src/main/jni/react/jni/JInspector.cpp b/ReactAndroid/src/main/jni/react/jni/JInspector.cpp index 711e84db7f3..329a686e078 100644 --- a/ReactAndroid/src/main/jni/react/jni/JInspector.cpp +++ b/ReactAndroid/src/main/jni/react/jni/JInspector.cpp @@ -7,6 +7,8 @@ #include "JInspector.h" +#include + #ifdef WITH_INSPECTOR namespace facebook { @@ -80,7 +82,7 @@ jni::local_ref> JInspector::getPages() { } jni::local_ref JInspector::connect(int pageId, jni::alias_ref remote) { - auto localConnection = inspector_->connect(pageId, folly::make_unique(std::move(remote))); + auto localConnection = inspector_->connect(pageId, std::make_unique(std::move(remote))); return JLocalConnection::newObjectCxxArgs(std::move(localConnection)); } diff --git a/ReactAndroid/src/main/jni/react/jni/JInspector.h b/ReactAndroid/src/main/jni/react/jni/JInspector.h index 0bd1dfcf456..6dca06401ae 100644 --- a/ReactAndroid/src/main/jni/react/jni/JInspector.h +++ b/ReactAndroid/src/main/jni/react/jni/JInspector.h @@ -12,7 +12,8 @@ #include #include -#include + +#include namespace facebook { namespace react { diff --git a/ReactAndroid/src/main/jni/react/jni/JMessageQueueThread.cpp b/ReactAndroid/src/main/jni/react/jni/JMessageQueueThread.cpp index 35a19b1a393..c54b325257b 100644 --- a/ReactAndroid/src/main/jni/react/jni/JMessageQueueThread.cpp +++ b/ReactAndroid/src/main/jni/react/jni/JMessageQueueThread.cpp @@ -12,7 +12,6 @@ #include #include -#include #include #include "JNativeRunnable.h" diff --git a/ReactAndroid/src/main/jni/react/jni/JSLoader.cpp b/ReactAndroid/src/main/jni/react/jni/JSLoader.cpp index e9388cbe521..b5e1eccab74 100644 --- a/ReactAndroid/src/main/jni/react/jni/JSLoader.cpp +++ b/ReactAndroid/src/main/jni/react/jni/JSLoader.cpp @@ -12,8 +12,8 @@ #include #include #include -#include #include +#include #include #include #include @@ -48,7 +48,7 @@ std::unique_ptr loadScriptFromAssets( assetName.c_str(), AASSET_MODE_STREAMING); // Optimized for sequential read: see AssetManager.java for docs if (asset) { - auto buf = folly::make_unique(AAsset_getLength(asset)); + auto buf = std::make_unique(AAsset_getLength(asset)); size_t offset = 0; int readbytes; while ((readbytes = AAsset_read(asset, buf->data() + offset, buf->size() - offset)) > 0) { diff --git a/ReactAndroid/src/main/jni/react/jni/JniJSModulesUnbundle.cpp b/ReactAndroid/src/main/jni/react/jni/JniJSModulesUnbundle.cpp index c842524e8fb..5c632d44f7a 100644 --- a/ReactAndroid/src/main/jni/react/jni/JniJSModulesUnbundle.cpp +++ b/ReactAndroid/src/main/jni/react/jni/JniJSModulesUnbundle.cpp @@ -15,8 +15,6 @@ #include #include -#include - using magic_number_t = uint32_t; const magic_number_t MAGIC_FILE_HEADER = 0xFB0BD1E5; const char* MAGIC_FILE_NAME = "UNBUNDLE"; @@ -46,7 +44,7 @@ static asset_ptr openAsset( std::unique_ptr JniJSModulesUnbundle::fromEntryFile( AAssetManager *assetManager, const std::string& entryFile) { - return folly::make_unique(assetManager, jsModulesDir(entryFile)); + return std::make_unique(assetManager, jsModulesDir(entryFile)); } JniJSModulesUnbundle::JniJSModulesUnbundle(AAssetManager *assetManager, const std::string& moduleDirectory) : diff --git a/ReactAndroid/src/main/jni/react/jni/ModuleRegistryBuilder.cpp b/ReactAndroid/src/main/jni/react/jni/ModuleRegistryBuilder.cpp index f4e3a30ff2e..09b2a078c25 100644 --- a/ReactAndroid/src/main/jni/react/jni/ModuleRegistryBuilder.cpp +++ b/ReactAndroid/src/main/jni/react/jni/ModuleRegistryBuilder.cpp @@ -7,11 +7,11 @@ #include "ModuleRegistryBuilder.h" +#include #include #include #include -#include namespace facebook { namespace react { @@ -47,14 +47,14 @@ std::vector> buildNativeModuleList( std::vector> modules; if (javaModules) { for (const auto& jm : *javaModules) { - modules.emplace_back(folly::make_unique( + modules.emplace_back(std::make_unique( winstance, jm, moduleMessageQueue)); } } if (cxxModules) { for (const auto& cm : *cxxModules) { std::string moduleName = cm->getName(); - modules.emplace_back(folly::make_unique( + modules.emplace_back(std::make_unique( winstance, moduleName, cm->getProvider(moduleName), moduleMessageQueue)); } } diff --git a/ReactAndroid/src/main/jni/react/jni/ProxyExecutor.cpp b/ReactAndroid/src/main/jni/react/jni/ProxyExecutor.cpp index 5e43001aaa1..4c4a369a6bc 100644 --- a/ReactAndroid/src/main/jni/react/jni/ProxyExecutor.cpp +++ b/ReactAndroid/src/main/jni/react/jni/ProxyExecutor.cpp @@ -13,10 +13,11 @@ #include #include #include -#include #include #include +#include + namespace facebook { namespace react { @@ -38,7 +39,7 @@ static std::string executeJSCallWithProxy( std::unique_ptr ProxyExecutorOneTimeFactory::createJSExecutor( std::shared_ptr delegate, std::shared_ptr) { - return folly::make_unique(std::move(m_executor), delegate); + return std::make_unique(std::move(m_executor), delegate); } ProxyExecutor::ProxyExecutor(jni::global_ref&& executorInstance, @@ -74,7 +75,7 @@ void ProxyExecutor::loadApplicationScript( SystraceSection t("setGlobalVariable"); setGlobalVariable( "__fbBatchedBridgeConfig", - folly::make_unique(folly::toJson(config))); + std::make_unique(folly::toJson(config))); } static auto loadApplicationScript = diff --git a/ReactCommon/cxxreact/Instance.cpp b/ReactCommon/cxxreact/Instance.cpp index b8e7b5aac2d..65f1ce171d5 100644 --- a/ReactCommon/cxxreact/Instance.cpp +++ b/ReactCommon/cxxreact/Instance.cpp @@ -18,7 +18,6 @@ #include "SystraceSection.h" #include -#include #include #include @@ -26,6 +25,7 @@ #include #include +#include #include #include @@ -46,7 +46,7 @@ void Instance::initializeBridge( callback_ = std::move(callback); moduleRegistry_ = std::move(moduleRegistry); jsQueue->runOnQueueSync([this, &jsef, jsQueue]() mutable { - nativeToJsBridge_ = folly::make_unique( + nativeToJsBridge_ = std::make_unique( jsef.get(), moduleRegistry_, jsQueue, callback_); std::lock_guard lock(m_syncMutex); @@ -57,26 +57,26 @@ void Instance::initializeBridge( CHECK(nativeToJsBridge_); } -void Instance::loadApplication(std::unique_ptr bundleRegistry, - std::unique_ptr string, - std::string sourceURL) { +void Instance::loadApplication( + std::unique_ptr bundleRegistry, + std::unique_ptr string, + std::string sourceURL) { callback_->incrementPendingJSCalls(); - SystraceSection s("Instance::loadApplication", "sourceURL", - sourceURL); - nativeToJsBridge_->loadApplication(std::move(bundleRegistry), std::move(string), - std::move(sourceURL)); + SystraceSection s("Instance::loadApplication", "sourceURL", sourceURL); + nativeToJsBridge_->loadApplication( + std::move(bundleRegistry), std::move(string), std::move(sourceURL)); } -void Instance::loadApplicationSync(std::unique_ptr bundleRegistry, - std::unique_ptr string, - std::string sourceURL) { +void Instance::loadApplicationSync( + std::unique_ptr bundleRegistry, + std::unique_ptr string, + std::string sourceURL) { std::unique_lock lock(m_syncMutex); m_syncCV.wait(lock, [this] { return m_syncReady; }); - SystraceSection s("Instance::loadApplicationSync", "sourceURL", - sourceURL); - nativeToJsBridge_->loadApplicationSync(std::move(bundleRegistry), std::move(string), - std::move(sourceURL)); + SystraceSection s("Instance::loadApplicationSync", "sourceURL", sourceURL); + nativeToJsBridge_->loadApplicationSync( + std::move(bundleRegistry), std::move(string), std::move(sourceURL)); } void Instance::setSourceURL(std::string sourceURL) { @@ -86,11 +86,11 @@ void Instance::setSourceURL(std::string sourceURL) { nativeToJsBridge_->loadApplication(nullptr, nullptr, std::move(sourceURL)); } -void Instance::loadScriptFromString(std::unique_ptr string, - std::string sourceURL, - bool loadSynchronously) { - SystraceSection s("Instance::loadScriptFromString", "sourceURL", - sourceURL); +void Instance::loadScriptFromString( + std::unique_ptr string, + std::string sourceURL, + bool loadSynchronously) { + SystraceSection s("Instance::loadScriptFromString", "sourceURL", sourceURL); if (loadSynchronously) { loadApplicationSync(nullptr, std::move(string), std::move(sourceURL)); } else { @@ -110,54 +110,63 @@ bool Instance::isIndexedRAMBundle(const char *sourcePath) { return parseTypeFromHeader(header) == ScriptTag::RAMBundle; } -bool Instance::isIndexedRAMBundle(std::unique_ptr* script) { +bool Instance::isIndexedRAMBundle(std::unique_ptr *script) { BundleHeader header; - strncpy(reinterpret_cast(&header), script->get()->c_str(), sizeof(header)); + strncpy( + reinterpret_cast(&header), + script->get()->c_str(), + sizeof(header)); return parseTypeFromHeader(header) == ScriptTag::RAMBundle; } -void Instance::loadRAMBundleFromString(std::unique_ptr script, const std::string& sourceURL) { - auto bundle = folly::make_unique(std::move(script)); +void Instance::loadRAMBundleFromString( + std::unique_ptr script, + const std::string &sourceURL) { + auto bundle = std::make_unique(std::move(script)); auto startupScript = bundle->getStartupCode(); auto registry = RAMBundleRegistry::singleBundleRegistry(std::move(bundle)); - loadRAMBundle( - std::move(registry), - std::move(startupScript), - sourceURL, - true); + loadRAMBundle(std::move(registry), std::move(startupScript), sourceURL, true); } -void Instance::loadRAMBundleFromFile(const std::string& sourcePath, - const std::string& sourceURL, - bool loadSynchronously) { - auto bundle = folly::make_unique(sourcePath.c_str()); - auto startupScript = bundle->getStartupCode(); - auto registry = RAMBundleRegistry::multipleBundlesRegistry(std::move(bundle), JSIndexedRAMBundle::buildFactory()); - loadRAMBundle( +void Instance::loadRAMBundleFromFile( + const std::string &sourcePath, + const std::string &sourceURL, + bool loadSynchronously) { + auto bundle = std::make_unique(sourcePath.c_str()); + auto startupScript = bundle->getStartupCode(); + auto registry = RAMBundleRegistry::multipleBundlesRegistry( + std::move(bundle), JSIndexedRAMBundle::buildFactory()); + loadRAMBundle( std::move(registry), std::move(startupScript), sourceURL, loadSynchronously); } -void Instance::loadRAMBundle(std::unique_ptr bundleRegistry, - std::unique_ptr startupScript, - std::string startupScriptSourceURL, - bool loadSynchronously) { +void Instance::loadRAMBundle( + std::unique_ptr bundleRegistry, + std::unique_ptr startupScript, + std::string startupScriptSourceURL, + bool loadSynchronously) { if (loadSynchronously) { - loadApplicationSync(std::move(bundleRegistry), std::move(startupScript), - std::move(startupScriptSourceURL)); + loadApplicationSync( + std::move(bundleRegistry), + std::move(startupScript), + std::move(startupScriptSourceURL)); } else { - loadApplication(std::move(bundleRegistry), std::move(startupScript), - std::move(startupScriptSourceURL)); + loadApplication( + std::move(bundleRegistry), + std::move(startupScript), + std::move(startupScriptSourceURL)); } } -void Instance::setGlobalVariable(std::string propName, - std::unique_ptr jsonValue) { - nativeToJsBridge_->setGlobalVariable(std::move(propName), - std::move(jsonValue)); +void Instance::setGlobalVariable( + std::string propName, + std::unique_ptr jsonValue) { + nativeToJsBridge_->setGlobalVariable( + std::move(propName), std::move(jsonValue)); } void *Instance::getJavaScriptContext() { @@ -168,16 +177,18 @@ void *Instance::getJavaScriptContext() { bool Instance::isInspectable() { return nativeToJsBridge_ ? nativeToJsBridge_->isInspectable() : false; } - + bool Instance::isBatchActive() { return nativeToJsBridge_ ? nativeToJsBridge_->isBatchActive() : false; } -void Instance::callJSFunction(std::string &&module, std::string &&method, - folly::dynamic &¶ms) { +void Instance::callJSFunction( + std::string &&module, + std::string &&method, + folly::dynamic &¶ms) { 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 &¶ms) { @@ -186,7 +197,9 @@ void Instance::callJSCallback(uint64_t callbackId, folly::dynamic &¶ms) { nativeToJsBridge_->invokeCallback((double)callbackId, std::move(params)); } -void Instance::registerBundle(uint32_t bundleId, const std::string& bundlePath) { +void Instance::registerBundle( + uint32_t bundleId, + const std::string &bundlePath) { nativeToJsBridge_->registerBundle(bundleId, bundlePath); } @@ -194,17 +207,20 @@ const ModuleRegistry &Instance::getModuleRegistry() const { return *moduleRegistry_; } -ModuleRegistry &Instance::getModuleRegistry() { return *moduleRegistry_; } +ModuleRegistry &Instance::getModuleRegistry() { + return *moduleRegistry_; +} void Instance::handleMemoryPressure(int pressureLevel) { nativeToJsBridge_->handleMemoryPressure(pressureLevel); } -void Instance::invokeAsync(std::function&& func) { - nativeToJsBridge_->runOnExecutorQueue([func=std::move(func)](JSExecutor *executor) { - func(); - executor->flush(); - }); +void Instance::invokeAsync(std::function &&func) { + nativeToJsBridge_->runOnExecutorQueue( + [func = std::move(func)](JSExecutor *executor) { + func(); + executor->flush(); + }); } } // namespace react diff --git a/ReactCommon/cxxreact/JSBigString.cpp b/ReactCommon/cxxreact/JSBigString.cpp index 222076eacf8..834a7ca6900 100644 --- a/ReactCommon/cxxreact/JSBigString.cpp +++ b/ReactCommon/cxxreact/JSBigString.cpp @@ -16,6 +16,8 @@ #include #include +#include + namespace facebook { namespace react { @@ -148,7 +150,7 @@ std::unique_ptr JSBigFileString::fromPath( struct stat fileInfo; folly::checkUnixError(::fstat(fd, &fileInfo), "fstat on bundle failed."); - return folly::make_unique(fd, fileInfo.st_size); + return std::make_unique(fd, fileInfo.st_size); } } // namespace react diff --git a/ReactCommon/cxxreact/JSDeltaBundleClient.cpp b/ReactCommon/cxxreact/JSDeltaBundleClient.cpp index e90f54b6242..71e894fa65b 100644 --- a/ReactCommon/cxxreact/JSDeltaBundleClient.cpp +++ b/ReactCommon/cxxreact/JSDeltaBundleClient.cpp @@ -7,25 +7,24 @@ #include "JSDeltaBundleClient.h" +#include #include -#include - namespace facebook { namespace react { namespace { - std::string startupCode(const folly::dynamic *pre, const folly::dynamic *post) { - std::ostringstream startupCode; +std::string startupCode(const folly::dynamic *pre, const folly::dynamic *post) { + std::ostringstream startupCode; - for (auto section : {pre, post}) { - if (section != nullptr) { - startupCode << section->getString() << '\n'; - } + for (auto section : {pre, post}) { + if (section != nullptr) { + startupCode << section->getString() << '\n'; } - - return startupCode.str(); } + + return startupCode.str(); +} } // namespace void JSDeltaBundleClient::patchModules(const folly::dynamic *modules) { @@ -36,7 +35,7 @@ void JSDeltaBundleClient::patchModules(const folly::dynamic *modules) { } } -void JSDeltaBundleClient::patch(const folly::dynamic& delta) { +void JSDeltaBundleClient::patch(const folly::dynamic &delta) { auto const base = delta.get_ptr("base"); if (base != nullptr && base->asBool()) { @@ -76,10 +75,10 @@ void JSDeltaBundleClient::patch(const folly::dynamic& delta) { patchModules(modified); } } - } -JSModulesUnbundle::Module JSDeltaBundleClient::getModule(uint32_t moduleId) const { +JSModulesUnbundle::Module JSDeltaBundleClient::getModule( + uint32_t moduleId) const { auto search = modules_.find(moduleId); if (search != modules_.end()) { return {folly::to(search->first, ".js"), search->second}; @@ -89,7 +88,7 @@ JSModulesUnbundle::Module JSDeltaBundleClient::getModule(uint32_t moduleId) cons } std::unique_ptr JSDeltaBundleClient::getStartupCode() const { - return folly::make_unique(startupCode_); + return std::make_unique(startupCode_); } void JSDeltaBundleClient::clear() { diff --git a/ReactCommon/cxxreact/JSIndexedRAMBundle.cpp b/ReactCommon/cxxreact/JSIndexedRAMBundle.cpp index 8247cf66996..9c775d06a17 100644 --- a/ReactCommon/cxxreact/JSIndexedRAMBundle.cpp +++ b/ReactCommon/cxxreact/JSIndexedRAMBundle.cpp @@ -9,37 +9,39 @@ #include #include +#include #include -#include namespace facebook { namespace react { -std::function(std::string)> JSIndexedRAMBundle::buildFactory() { - return [](const std::string& bundlePath){ - return folly::make_unique(bundlePath.c_str()); +std::function(std::string)> +JSIndexedRAMBundle::buildFactory() { + return [](const std::string &bundlePath) { + return std::make_unique(bundlePath.c_str()); }; } JSIndexedRAMBundle::JSIndexedRAMBundle(const char *sourcePath) { m_bundle = std::make_unique(sourcePath, std::ifstream::binary); if (!m_bundle) { - throw std::ios_base::failure( - folly::to("Bundle ", sourcePath, - "cannot be opened: ", m_bundle->rdstate())); + throw std::ios_base::failure(folly::to( + "Bundle ", sourcePath, "cannot be opened: ", m_bundle->rdstate())); } init(); } -JSIndexedRAMBundle::JSIndexedRAMBundle(std::unique_ptr script) { +JSIndexedRAMBundle::JSIndexedRAMBundle( + std::unique_ptr script) { // tmpStream is needed because m_bundle is std::istream type // which has no member 'write' - std::unique_ptr tmpStream = std::make_unique(); + std::unique_ptr tmpStream = + std::make_unique(); tmpStream->write(script->c_str(), script->size()); m_bundle = std::move(tmpStream); if (!m_bundle) { - throw std::ios_base::failure( - folly::to("Bundle from string cannot be opened: ", m_bundle->rdstate())); + throw std::ios_base::failure(folly::to( + "Bundle from string cannot be opened: ", m_bundle->rdstate())); } init(); } @@ -48,8 +50,8 @@ void JSIndexedRAMBundle::init() { // read in magic header, number of entries, and length of the startup section uint32_t header[3]; static_assert( - sizeof(header) == 12, - "header size must exactly match the input file format"); + sizeof(header) == 12, + "header size must exactly match the input file format"); readBundle(reinterpret_cast(header), sizeof(header)); const size_t numTableEntries = folly::Endian::little(header[1]); @@ -61,15 +63,17 @@ void JSIndexedRAMBundle::init() { // read the lookup table from the file readBundle( - reinterpret_cast(m_table.data.get()), m_table.byteLength()); + reinterpret_cast(m_table.data.get()), m_table.byteLength()); // read the startup code - m_startupCode = std::unique_ptr(new JSBigBufferString{startupCodeSize - 1}); + m_startupCode = std::unique_ptr( + new JSBigBufferString{startupCodeSize - 1}); readBundle(m_startupCode->data(), startupCodeSize - 1); } -JSIndexedRAMBundle::Module JSIndexedRAMBundle::getModule(uint32_t moduleId) const { +JSIndexedRAMBundle::Module JSIndexedRAMBundle::getModule( + uint32_t moduleId) const { Module ret; ret.name = folly::to(moduleId, ".js"); ret.code = getModuleCode(moduleId); @@ -77,7 +81,8 @@ JSIndexedRAMBundle::Module JSIndexedRAMBundle::getModule(uint32_t moduleId) cons } std::unique_ptr JSIndexedRAMBundle::getStartupCode() { - CHECK(m_startupCode) << "startup code for a RAM Bundle can only be retrieved once"; + CHECK(m_startupCode) + << "startup code for a RAM Bundle can only be retrieved once"; return std::move(m_startupCode); } @@ -85,24 +90,29 @@ std::string JSIndexedRAMBundle::getModuleCode(const uint32_t id) const { const auto moduleData = id < m_table.numEntries ? &m_table.data[id] : nullptr; // entries without associated code have offset = 0 and length = 0 - const uint32_t length = moduleData ? folly::Endian::little(moduleData->length) : 0; + const uint32_t length = + moduleData ? folly::Endian::little(moduleData->length) : 0; if (length == 0) { throw std::ios_base::failure( - folly::to("Error loading module", id, "from RAM Bundle")); + folly::to("Error loading module", id, "from RAM Bundle")); } std::string ret(length - 1, '\0'); - readBundle(&ret.front(), length - 1, m_baseOffset + folly::Endian::little(moduleData->offset)); + readBundle( + &ret.front(), + length - 1, + m_baseOffset + folly::Endian::little(moduleData->offset)); return ret; } -void JSIndexedRAMBundle::readBundle(char *buffer, const std::streamsize bytes) const { +void JSIndexedRAMBundle::readBundle(char *buffer, const std::streamsize bytes) + const { if (!m_bundle->read(buffer, bytes)) { if (m_bundle->rdstate() & std::ios::eofbit) { throw std::ios_base::failure("Unexpected end of RAM Bundle file"); } - throw std::ios_base::failure( - folly::to("Error reading RAM Bundle: ", m_bundle->rdstate())); + throw std::ios_base::failure(folly::to( + "Error reading RAM Bundle: ", m_bundle->rdstate())); } } @@ -110,13 +120,12 @@ void JSIndexedRAMBundle::readBundle( char *buffer, const std::streamsize bytes, const std::ifstream::pos_type position) const { - if (!m_bundle->seekg(position)) { - throw std::ios_base::failure( - folly::to("Error reading RAM Bundle: ", m_bundle->rdstate())); + throw std::ios_base::failure(folly::to( + "Error reading RAM Bundle: ", m_bundle->rdstate())); } readBundle(buffer, bytes); } -} // namespace react -} // namespace facebook +} // namespace react +} // namespace facebook diff --git a/ReactCommon/cxxreact/NativeToJsBridge.cpp b/ReactCommon/cxxreact/NativeToJsBridge.cpp index 55ea94e1cdb..0e34f942a85 100644 --- a/ReactCommon/cxxreact/NativeToJsBridge.cpp +++ b/ReactCommon/cxxreact/NativeToJsBridge.cpp @@ -7,18 +7,19 @@ #include "NativeToJsBridge.h" -#include -#include #include +#include #include #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 #ifdef WITH_FBSYSTRACE #include @@ -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 registry, - std::shared_ptr callback) - : m_registry(registry) - , m_callback(callback) {} + public: + JsToNativeBridge( + std::shared_ptr registry, + std::shared_ptr callback) + : m_registry(registry), m_callback(callback) {} std::shared_ptr 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 bundleRegistry, std::unique_ptr 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, - "", - systraceCookie); - #endif + TRACE_TAG_REACT_CXX_BRIDGE, "", 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, - "", - 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, "", 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 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 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 task) { +void NativeToJsBridge::runOnExecutorQueue( + std::function task) { if (*m_destroyed) { return; } std::shared_ptr 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 diff --git a/ReactCommon/cxxreact/RAMBundleRegistry.cpp b/ReactCommon/cxxreact/RAMBundleRegistry.cpp index 049ba0e0c0d..c19110c9ce1 100644 --- a/ReactCommon/cxxreact/RAMBundleRegistry.cpp +++ b/ReactCommon/cxxreact/RAMBundleRegistry.cpp @@ -7,9 +7,10 @@ #include "RAMBundleRegistry.h" -#include #include +#include + namespace facebook { namespace react { @@ -17,44 +18,44 @@ constexpr uint32_t RAMBundleRegistry::MAIN_BUNDLE_ID; std::unique_ptr RAMBundleRegistry::singleBundleRegistry( std::unique_ptr mainBundle) { - return folly::make_unique(std::move(mainBundle)); + return std::make_unique(std::move(mainBundle)); } std::unique_ptr RAMBundleRegistry::multipleBundlesRegistry( std::unique_ptr mainBundle, std::function(std::string)> factory) { - return folly::make_unique( + return std::make_unique( std::move(mainBundle), std::move(factory)); } RAMBundleRegistry::RAMBundleRegistry( std::unique_ptr mainBundle, - std::function(std::string)> factory): - m_factory(std::move(factory)) { + std::function(std::string)> factory) + : m_factory(std::move(factory)) { m_bundles.emplace(MAIN_BUNDLE_ID, std::move(mainBundle)); } void RAMBundleRegistry::registerBundle( - uint32_t bundleId, std::string bundlePath) { + uint32_t bundleId, + std::string bundlePath) { m_bundlePaths.emplace(bundleId, std::move(bundlePath)); } JSModulesUnbundle::Module RAMBundleRegistry::getModule( - uint32_t bundleId, uint32_t moduleId) { + uint32_t bundleId, + uint32_t moduleId) { if (m_bundles.find(bundleId) == m_bundles.end()) { if (!m_factory) { throw std::runtime_error( - "You need to register factory function in order to " - "support multiple RAM bundles." - ); + "You need to register factory function in order to " + "support multiple RAM bundles."); } auto bundlePath = m_bundlePaths.find(bundleId); if (bundlePath == m_bundlePaths.end()) { throw std::runtime_error( - "In order to fetch RAM bundle from the registry, its file " - "path needs to be registered first." - ); + "In order to fetch RAM bundle from the registry, its file " + "path needs to be registered first."); } m_bundles.emplace(bundleId, m_factory(bundlePath->second)); } @@ -64,14 +65,14 @@ JSModulesUnbundle::Module RAMBundleRegistry::getModule( return module; } return { - folly::to("seg-", bundleId, '_', std::move(module.name)), - std::move(module.code), + folly::to("seg-", bundleId, '_', std::move(module.name)), + std::move(module.code), }; } -JSModulesUnbundle* RAMBundleRegistry::getBundle(uint32_t bundleId) const { +JSModulesUnbundle *RAMBundleRegistry::getBundle(uint32_t bundleId) const { return m_bundles.at(bundleId).get(); } -} // namespace react -} // namespace facebook +} // namespace react +} // namespace facebook diff --git a/ReactCommon/cxxreact/SampleCxxModule.cpp b/ReactCommon/cxxreact/SampleCxxModule.cpp index f29b6e232b7..31acd0beac7 100644 --- a/ReactCommon/cxxreact/SampleCxxModule.cpp +++ b/ReactCommon/cxxreact/SampleCxxModule.cpp @@ -8,14 +8,16 @@ #include "SampleCxxModule.h" #include -#include #include +#include #include using namespace folly; -namespace facebook { namespace xplat { namespace samples { +namespace facebook { +namespace xplat { +namespace samples { std::string Sample::hello() { LOG(WARNING) << "glog: hello, world"; @@ -26,11 +28,11 @@ double Sample::add(double a, double b) { return a + b; } -std::string Sample::concat(const std::string& a, const std::string& b) { +std::string Sample::concat(const std::string &a, const std::string &b) { return a + b; } -std::string Sample::repeat(int count, const std::string& str) { +std::string Sample::repeat(int count, const std::string &str) { std::string ret; for (int i = 0; i < count; i++) { ret += str; @@ -39,8 +41,7 @@ std::string Sample::repeat(int count, const std::string& str) { return ret; } -void Sample::save(std::map dict) -{ +void Sample::save(std::map dict) { state_ = std::move(dict); } @@ -49,16 +50,16 @@ std::map Sample::load() { } void Sample::except() { -// TODO mhorowitz #7128529: There's no way to automatically test this -// right now. + // TODO mhorowitz #7128529: There's no way to automatically test this + // right now. // throw std::runtime_error("oops"); } void Sample::call_later(int msec, std::function f) { std::thread t([=] { - std::this_thread::sleep_for(std::chrono::milliseconds(msec)); - f(); - }); + std::this_thread::sleep_for(std::chrono::milliseconds(msec)); + f(); + }); t.detach(); } @@ -67,7 +68,7 @@ double Sample::twice(double n) { } SampleCxxModule::SampleCxxModule(std::unique_ptr sample) - : sample_(std::move(sample)) {} + : sample_(std::move(sample)) {} std::string SampleCxxModule::getName() { return "Sample"; @@ -75,90 +76,107 @@ std::string SampleCxxModule::getName() { auto SampleCxxModule::getConstants() -> std::map { return { - { "one", 1 }, - { "two", 2 }, - { "animal", "fox" }, + {"one", 1}, + {"two", 2}, + {"animal", "fox"}, }; } auto SampleCxxModule::getMethods() -> std::vector { return { - Method("hello", [this] { - sample_->hello(); - }), - Method("add", [this](dynamic args, Callback cb) { - LOG(WARNING) << "Sample: add => " - << sample_->add(jsArgAsDouble(args, 0), jsArgAsDouble(args, 1)); - cb({sample_->add(jsArgAsDouble(args, 0), jsArgAsDouble(args, 1))}); - }), - Method("concat", [this](dynamic args, Callback cb) { - cb({sample_->concat(jsArgAsString(args, 0), - jsArgAsString(args, 1))}); - }), - Method("repeat", [this](dynamic args, Callback cb) { - cb({sample_->repeat((int)jsArgAsInt(args, 0), - jsArgAsString(args, 1))}); - }), - Method("save", this, &SampleCxxModule::save), - Method("load", this, &SampleCxxModule::load), - Method("call_later", [this](dynamic args, Callback cb) { - sample_->call_later((int)jsArgAsInt(args, 0), [cb] { - cb({}); - }); - }), - Method("except", [this] { - sample_->except(); - }), - Method("twice", [this](dynamic args) -> dynamic { - return sample_->twice(jsArgAsDouble(args, 0)); - }, SyncTag), - Method("syncHello", [this]() -> dynamic { - sample_->hello(); - return nullptr; - }, SyncTag), - Method("addIfPositiveAsPromise", [](dynamic args, Callback cb, Callback cbError) { - auto a = jsArgAsDouble(args, 0); - auto b = jsArgAsDouble(args, 1); - if (a < 0 || b < 0) { - cbError({"Negative number!"}); - } else { - cb({a + b}); - } - }), - Method("addIfPositiveAsAsync", [](dynamic args, Callback cb, Callback cbError) { - auto a = jsArgAsDouble(args, 0); - auto b = jsArgAsDouble(args, 1); - if (a < 0 || b < 0) { - cbError({"Negative number!"}); - } else { - cb({a + b}); - } - }, AsyncTag), + Method("hello", [this] { sample_->hello(); }), + Method( + "add", + [this](dynamic args, Callback cb) { + LOG(WARNING) << "Sample: add => " + << sample_->add( + jsArgAsDouble(args, 0), jsArgAsDouble(args, 1)); + cb({sample_->add(jsArgAsDouble(args, 0), jsArgAsDouble(args, 1))}); + }), + Method( + "concat", + [this](dynamic args, Callback cb) { + cb({sample_->concat( + jsArgAsString(args, 0), jsArgAsString(args, 1))}); + }), + Method( + "repeat", + [this](dynamic args, Callback cb) { + cb({sample_->repeat( + (int)jsArgAsInt(args, 0), jsArgAsString(args, 1))}); + }), + Method("save", this, &SampleCxxModule::save), + Method("load", this, &SampleCxxModule::load), + Method( + "call_later", + [this](dynamic args, Callback cb) { + sample_->call_later((int)jsArgAsInt(args, 0), [cb] { cb({}); }); + }), + Method("except", [this] { sample_->except(); }), + Method( + "twice", + [this](dynamic args) -> dynamic { + return sample_->twice(jsArgAsDouble(args, 0)); + }, + SyncTag), + Method( + "syncHello", + [this]() -> dynamic { + sample_->hello(); + return nullptr; + }, + SyncTag), + Method( + "addIfPositiveAsPromise", + [](dynamic args, Callback cb, Callback cbError) { + auto a = jsArgAsDouble(args, 0); + auto b = jsArgAsDouble(args, 1); + if (a < 0 || b < 0) { + cbError({"Negative number!"}); + } else { + cb({a + b}); + } + }), + Method( + "addIfPositiveAsAsync", + [](dynamic args, Callback cb, Callback cbError) { + auto a = jsArgAsDouble(args, 0); + auto b = jsArgAsDouble(args, 1); + if (a < 0 || b < 0) { + cbError({"Negative number!"}); + } else { + cb({a + b}); + } + }, + AsyncTag), }; } void SampleCxxModule::save(folly::dynamic args) { std::map m; - for (const auto& p : jsArgN(args, 0, &dynamic::items)) { - m.emplace(jsArg(p.first, &dynamic::asString, "map key"), - jsArg(p.second, &dynamic::asString, "map value")); + for (const auto &p : jsArgN(args, 0, &dynamic::items)) { + m.emplace( + jsArg(p.first, &dynamic::asString, "map key"), + jsArg(p.second, &dynamic::asString, "map value")); } sample_->save(std::move(m)); } void SampleCxxModule::load(__unused folly::dynamic args, Callback cb) { dynamic d = dynamic::object; - for (const auto& p : sample_->load()) { + for (const auto &p : sample_->load()) { d.insert(p.first, p.second); } cb({d}); } -}}} +} // namespace samples +} // namespace xplat +} // namespace facebook // By convention, the function name should be the same as the class name. facebook::xplat::module::CxxModule *SampleCxxModule() { return new facebook::xplat::samples::SampleCxxModule( - folly::make_unique()); + std::make_unique()); }