Fix a folly::dynamic deprecated pattern

Reviewed By: javache

Differential Revision: D4075622

fbshipit-source-id: 4a6b6c4068d762dce2b1535bfd9630e185f5489f
This commit is contained in:
Giuseppe Ottaviano 2016-10-26 09:36:39 -07:00 коммит произвёл Facebook Github Bot
Родитель d8e77f0c76
Коммит dc02907039
3 изменённых файлов: 37 добавлений и 16 удалений

Просмотреть файл

@ -12,6 +12,7 @@
#include <folly/json.h> #include <folly/json.h>
#include <folly/ScopeGuard.h> #include <folly/ScopeGuard.h>
#include <iterator>
#include <unordered_set> #include <unordered_set>
#include <dlfcn.h> #include <dlfcn.h>
@ -98,10 +99,12 @@ void CxxMethodWrapper::invoke(jobject jCatalystInstance, ExecutorToken::jhybrido
id1 = arguments->array[arguments->array.size() - 2].getInt(); id1 = arguments->array[arguments->array.size() - 2].getInt();
int id2 = arguments->array[arguments->array.size() - 1].getInt(); int id2 = arguments->array[arguments->array.size() - 1].getInt();
second = [catalystInstance, executorToken, id2](std::vector<folly::dynamic> args) mutable { second = [catalystInstance, executorToken, id2](std::vector<folly::dynamic> args) mutable {
folly::dynamic argsArray(std::make_move_iterator(args.begin()),
std::make_move_iterator(args.end()));
ThreadScope guard; ThreadScope guard;
sCatalystInstanceInvokeCallback( sCatalystInstanceInvokeCallback(
catalystInstance.get(), executorToken.get(), id2, catalystInstance.get(), executorToken.get(), id2,
ReadableNativeArray::newObjectCxxArgs(std::move(args)).get()); ReadableNativeArray::newObjectCxxArgs(std::move(argsArray)).get());
catalystInstance.reset(); catalystInstance.reset();
executorToken.reset(); executorToken.reset();
}; };
@ -110,10 +113,12 @@ void CxxMethodWrapper::invoke(jobject jCatalystInstance, ExecutorToken::jhybrido
} }
first = [catalystInstance, executorToken, id1](std::vector<folly::dynamic> args) mutable { first = [catalystInstance, executorToken, id1](std::vector<folly::dynamic> args) mutable {
folly::dynamic argsArray(std::make_move_iterator(args.begin()),
std::make_move_iterator(args.end()));
ThreadScope guard; ThreadScope guard;
sCatalystInstanceInvokeCallback( sCatalystInstanceInvokeCallback(
catalystInstance.get(), executorToken.get(), id1, catalystInstance.get(), executorToken.get(), id1,
ReadableNativeArray::newObjectCxxArgs(std::move(args)).get()); ReadableNativeArray::newObjectCxxArgs(std::move(argsArray)).get());
// This is necessary because by the time the lambda's dtor runs, // This is necessary because by the time the lambda's dtor runs,
// the guard has been destroyed, and it may not be possible to // the guard has been destroyed, and it may not be possible to
// get a JNIEnv* to clean up the captured global_ref. // get a JNIEnv* to clean up the captured global_ref.

Просмотреть файл

@ -20,7 +20,7 @@ const auto EXECUTOR_BASECLASS = "com/facebook/react/bridge/JavaJSExecutor";
static std::string executeJSCallWithProxy( static std::string executeJSCallWithProxy(
jobject executor, jobject executor,
const std::string& methodName, const std::string& methodName,
const std::vector<folly::dynamic>& arguments) { const folly::dynamic& arguments) {
static auto executeJSCall = static auto executeJSCall =
jni::findClassStatic(EXECUTOR_BASECLASS)->getMethod<jstring(jstring, jstring)>("executeJSCall"); jni::findClassStatic(EXECUTOR_BASECLASS)->getMethod<jstring(jstring, jstring)>("executeJSCall");
@ -88,20 +88,14 @@ void ProxyExecutor::setJSModulesUnbundle(std::unique_ptr<JSModulesUnbundle>) {
} }
void ProxyExecutor::callFunction(const std::string& moduleId, const std::string& methodId, const folly::dynamic& arguments) { void ProxyExecutor::callFunction(const std::string& moduleId, const std::string& methodId, const folly::dynamic& arguments) {
std::vector<folly::dynamic> call{ auto call = folly::dynamic::array(moduleId, methodId, std::move(arguments));
moduleId,
methodId,
std::move(arguments),
};
std::string result = executeJSCallWithProxy(m_executor.get(), "callFunctionReturnFlushedQueue", std::move(call)); std::string result = executeJSCallWithProxy(m_executor.get(), "callFunctionReturnFlushedQueue", std::move(call));
m_delegate->callNativeModules(*this, folly::parseJson(result), true); m_delegate->callNativeModules(*this, folly::parseJson(result), true);
} }
void ProxyExecutor::invokeCallback(const double callbackId, const folly::dynamic& arguments) { void ProxyExecutor::invokeCallback(const double callbackId, const folly::dynamic& arguments) {
std::vector<folly::dynamic> call{ auto call = folly::dynamic::array(callbackId, std::move(arguments));
(double) callbackId,
std::move(arguments)
};
std::string result = executeJSCallWithProxy(m_executor.get(), "invokeCallbackAndReturnFlushedQueue", std::move(call)); std::string result = executeJSCallWithProxy(m_executor.get(), "invokeCallbackAndReturnFlushedQueue", std::move(call));
m_delegate->callNativeModules(*this, folly::parseJson(result), true); m_delegate->callNativeModules(*this, folly::parseJson(result), true);
} }

Просмотреть файл

@ -3,6 +3,8 @@
#include "CxxNativeModule.h" #include "CxxNativeModule.h"
#include "Instance.h" #include "Instance.h"
#include <iterator>
#include <folly/json.h> #include <folly/json.h>
#include <cxxreact/JsArgumentHelpers.h> #include <cxxreact/JsArgumentHelpers.h>
@ -26,6 +28,24 @@ std::function<void(folly::dynamic)> makeCallback(
}; };
} }
namespace {
/**
* CxxModule::Callback accepts a vector<dynamic>, makeCallback returns
* a callback that accepts a dynamic, adapt the second into the first.
* TODO: Callback types should be made equal (preferably
* function<void(dynamic)>) to avoid the extra copy and indirect call.
*/
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())));
};
}
}
CxxNativeModule::CxxNativeModule(std::weak_ptr<Instance> instance, CxxNativeModule::CxxNativeModule(std::weak_ptr<Instance> instance,
std::unique_ptr<CxxModule> module) std::unique_ptr<CxxModule> module)
: instance_(instance) : instance_(instance)
@ -86,10 +106,13 @@ void CxxNativeModule::invoke(ExecutorToken token, unsigned int reactMethodId, fo
} }
if (method.callbacks == 1) { if (method.callbacks == 1) {
first = makeCallback(instance_, token, params[params.size() - 1]); first = convertCallback(
makeCallback(instance_, token, params[params.size() - 1]));
} else if (method.callbacks == 2) { } else if (method.callbacks == 2) {
first = makeCallback(instance_, token, params[params.size() - 2]); first = convertCallback(
second = makeCallback(instance_, token, params[params.size() - 1]); makeCallback(instance_, token, params[params.size() - 2]));
second = convertCallback(
makeCallback(instance_, token, params[params.size() - 1]));
} }
params.resize(params.size() - method.callbacks); params.resize(params.size() - method.callbacks);
@ -158,4 +181,3 @@ MethodCallResult CxxNativeModule::callSerializableNativeHook(
} }
} }