Codemod: Clang-format for all files in ReactCommon directory

Summary:
We are moving towards 100%-prettified files. That's the first step when we apply Clang Format for `ReactCommon`.

Changelog: [Internal]

Reviewed By: mdvacca

Differential Revision: D20110895

fbshipit-source-id: 0a0ce4997cf1c3721b0b07ef78c1a57ce87d20f9
This commit is contained in:
Valentin Shergin
2020-02-25 19:52:27 -08:00
committed by Facebook Github Bot
parent a3ade9e167
commit ede2c5031f
54 changed files with 895 additions and 696 deletions
+72 -53
View File
@@ -8,20 +8,21 @@
#include "CxxNativeModule.h"
#include "Instance.h"
#include <iterator>
#include <glog/logging.h>
#include <folly/json.h>
#include <glog/logging.h>
#include <iterator>
#include "JsArgumentHelpers.h"
#include "SystraceSection.h"
#include "MessageQueueThread.h"
#include "SystraceSection.h"
using facebook::xplat::module::CxxModule;
namespace facebook {
namespace react {
std::function<void(folly::dynamic)> makeCallback(
std::weak_ptr<Instance> instance, const folly::dynamic& callbackId) {
std::weak_ptr<Instance> instance,
const folly::dynamic &callbackId) {
if (!callbackId.isNumber()) {
throw std::invalid_argument("Expected callback(s) as final argument");
}
@@ -45,12 +46,13 @@ namespace {
CxxModule::Callback convertCallback(
std::function<void(folly::dynamic)> callback) {
return [callback = std::move(callback)](std::vector<folly::dynamic> args) {
callback(folly::dynamic(std::make_move_iterator(args.begin()),
std::make_move_iterator(args.end())));
callback(folly::dynamic(
std::make_move_iterator(args.begin()),
std::make_move_iterator(args.end())));
};
}
}
} // namespace
std::string CxxNativeModule::getName() {
return name_;
@@ -60,7 +62,7 @@ std::vector<MethodDescriptor> CxxNativeModule::getMethods() {
lazyInit();
std::vector<MethodDescriptor> descs;
for (auto& method : methods_) {
for (auto &method : methods_) {
descs.emplace_back(method.name, method.getType());
}
return descs;
@@ -74,42 +76,54 @@ folly::dynamic CxxNativeModule::getConstants() {
}
folly::dynamic constants = folly::dynamic::object();
for (auto& pair : module_->getConstants()) {
for (auto &pair : module_->getConstants()) {
constants.insert(std::move(pair.first), std::move(pair.second));
}
return constants;
}
void CxxNativeModule::invoke(unsigned int reactMethodId, folly::dynamic&& params, int callId) {
void CxxNativeModule::invoke(
unsigned int reactMethodId,
folly::dynamic &&params,
int callId) {
if (reactMethodId >= methods_.size()) {
throw std::invalid_argument(folly::to<std::string>("methodId ", reactMethodId,
" out of range [0..", methods_.size(), "]"));
throw std::invalid_argument(folly::to<std::string>(
"methodId ",
reactMethodId,
" out of range [0..",
methods_.size(),
"]"));
}
if (!params.isArray()) {
throw std::invalid_argument(
folly::to<std::string>("method parameters should be array, but are ", params.typeName()));
throw std::invalid_argument(folly::to<std::string>(
"method parameters should be array, but are ", params.typeName()));
}
CxxModule::Callback first;
CxxModule::Callback second;
const auto& method = methods_[reactMethodId];
const auto &method = methods_[reactMethodId];
if (!method.func) {
throw std::runtime_error(folly::to<std::string>("Method ", method.name,
" is synchronous but invoked asynchronously"));
throw std::runtime_error(folly::to<std::string>(
"Method ", method.name, " is synchronous but invoked asynchronously"));
}
if (params.size() < method.callbacks) {
throw std::invalid_argument(folly::to<std::string>("Expected ", method.callbacks,
" callbacks, but only ", params.size(), " parameters provided"));
throw std::invalid_argument(folly::to<std::string>(
"Expected ",
method.callbacks,
" callbacks, but only ",
params.size(),
" parameters provided"));
}
if (method.callbacks == 1) {
first = convertCallback(makeCallback(instance_, params[params.size() - 1]));
} else if (method.callbacks == 2) {
first = convertCallback(makeCallback(instance_, params[params.size() - 2]));
second = convertCallback(makeCallback(instance_, params[params.size() - 1]));
second =
convertCallback(makeCallback(instance_, params[params.size() - 1]));
}
params.resize(params.size() - method.callbacks);
@@ -133,44 +147,49 @@ void CxxNativeModule::invoke(unsigned int reactMethodId, folly::dynamic&& params
// stack. I'm told that will be possible in the future. TODO
// mhorowitz #7128529: convert C++ exceptions to Java
messageQueueThread_->runOnQueue([method, params=std::move(params), first, second, callId] () {
#ifdef WITH_FBSYSTRACE
if (callId != -1) {
fbsystrace_end_async_flow(TRACE_TAG_REACT_APPS, "native", callId);
}
#else
(void)(callId);
#endif
SystraceSection s(method.name.c_str());
try {
method.func(std::move(params), first, second);
} catch (const facebook::xplat::JsArgumentException& ex) {
throw;
} catch (std::exception& e) {
LOG(ERROR) << "std::exception. Method call " << method.name.c_str() << " failed: " << e.what();
std::terminate();
} catch (std::string& error) {
LOG(ERROR) << "std::string. Method call " << method.name.c_str() << " failed: " << error.c_str();
std::terminate();
} catch (...) {
LOG(ERROR) << "Method call " << method.name.c_str() << " failed. unknown error";
std::terminate();
}
});
messageQueueThread_->runOnQueue(
[method, params = std::move(params), first, second, callId]() {
#ifdef WITH_FBSYSTRACE
if (callId != -1) {
fbsystrace_end_async_flow(TRACE_TAG_REACT_APPS, "native", callId);
}
#else
(void)(callId);
#endif
SystraceSection s(method.name.c_str());
try {
method.func(std::move(params), first, second);
} catch (const facebook::xplat::JsArgumentException &ex) {
throw;
} catch (std::exception &e) {
LOG(ERROR) << "std::exception. Method call " << method.name.c_str()
<< " failed: " << e.what();
std::terminate();
} catch (std::string &error) {
LOG(ERROR) << "std::string. Method call " << method.name.c_str()
<< " failed: " << error.c_str();
std::terminate();
} catch (...) {
LOG(ERROR) << "Method call " << method.name.c_str()
<< " failed. unknown error";
std::terminate();
}
});
}
MethodCallResult CxxNativeModule::callSerializableNativeHook(unsigned int hookId, folly::dynamic&& args) {
MethodCallResult CxxNativeModule::callSerializableNativeHook(
unsigned int hookId,
folly::dynamic &&args) {
if (hookId >= methods_.size()) {
throw std::invalid_argument(
folly::to<std::string>("methodId ", hookId, " out of range [0..", methods_.size(), "]"));
throw std::invalid_argument(folly::to<std::string>(
"methodId ", hookId, " out of range [0..", methods_.size(), "]"));
}
const auto& method = methods_[hookId];
const auto &method = methods_[hookId];
if (!method.syncFunc) {
throw std::runtime_error(
folly::to<std::string>("Method ", method.name,
" is asynchronous but invoked synchronously"));
throw std::runtime_error(folly::to<std::string>(
"Method ", method.name, " is asynchronous but invoked synchronously"));
}
return method.syncFunc(std::move(args));
@@ -190,5 +209,5 @@ void CxxNativeModule::lazyInit() {
}
}
}
}
} // namespace react
} // namespace facebook