diff --git a/ReactAndroid/src/main/jni/xreact/jni/CxxModuleWrapper.cpp b/ReactAndroid/src/main/jni/xreact/jni/CxxModuleWrapper.cpp index 721649f0bac..800f00f4bd9 100644 --- a/ReactAndroid/src/main/jni/xreact/jni/CxxModuleWrapper.cpp +++ b/ReactAndroid/src/main/jni/xreact/jni/CxxModuleWrapper.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include @@ -98,10 +99,12 @@ void CxxMethodWrapper::invoke(jobject jCatalystInstance, ExecutorToken::jhybrido id1 = arguments->array[arguments->array.size() - 2].getInt(); int id2 = arguments->array[arguments->array.size() - 1].getInt(); second = [catalystInstance, executorToken, id2](std::vector args) mutable { + folly::dynamic argsArray(std::make_move_iterator(args.begin()), + std::make_move_iterator(args.end())); ThreadScope guard; sCatalystInstanceInvokeCallback( catalystInstance.get(), executorToken.get(), id2, - ReadableNativeArray::newObjectCxxArgs(std::move(args)).get()); + ReadableNativeArray::newObjectCxxArgs(std::move(argsArray)).get()); catalystInstance.reset(); executorToken.reset(); }; @@ -110,10 +113,12 @@ void CxxMethodWrapper::invoke(jobject jCatalystInstance, ExecutorToken::jhybrido } first = [catalystInstance, executorToken, id1](std::vector args) mutable { + folly::dynamic argsArray(std::make_move_iterator(args.begin()), + std::make_move_iterator(args.end())); ThreadScope guard; sCatalystInstanceInvokeCallback( 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, // the guard has been destroyed, and it may not be possible to // get a JNIEnv* to clean up the captured global_ref. diff --git a/ReactAndroid/src/main/jni/xreact/jni/ProxyExecutor.cpp b/ReactAndroid/src/main/jni/xreact/jni/ProxyExecutor.cpp index 9287f6c850f..e6b0b56d2b6 100644 --- a/ReactAndroid/src/main/jni/xreact/jni/ProxyExecutor.cpp +++ b/ReactAndroid/src/main/jni/xreact/jni/ProxyExecutor.cpp @@ -20,7 +20,7 @@ const auto EXECUTOR_BASECLASS = "com/facebook/react/bridge/JavaJSExecutor"; static std::string executeJSCallWithProxy( jobject executor, const std::string& methodName, - const std::vector& arguments) { + const folly::dynamic& arguments) { static auto executeJSCall = jni::findClassStatic(EXECUTOR_BASECLASS)->getMethod("executeJSCall"); @@ -88,20 +88,14 @@ void ProxyExecutor::setJSModulesUnbundle(std::unique_ptr) { } void ProxyExecutor::callFunction(const std::string& moduleId, const std::string& methodId, const folly::dynamic& arguments) { - std::vector call{ - moduleId, - methodId, - std::move(arguments), - }; + auto call = folly::dynamic::array(moduleId, methodId, std::move(arguments)); + std::string result = executeJSCallWithProxy(m_executor.get(), "callFunctionReturnFlushedQueue", std::move(call)); m_delegate->callNativeModules(*this, folly::parseJson(result), true); } void ProxyExecutor::invokeCallback(const double callbackId, const folly::dynamic& arguments) { - std::vector call{ - (double) callbackId, - std::move(arguments) - }; + auto call = folly::dynamic::array(callbackId, std::move(arguments)); std::string result = executeJSCallWithProxy(m_executor.get(), "invokeCallbackAndReturnFlushedQueue", std::move(call)); m_delegate->callNativeModules(*this, folly::parseJson(result), true); } diff --git a/ReactCommon/cxxreact/CxxNativeModule.cpp b/ReactCommon/cxxreact/CxxNativeModule.cpp index a437b00f3bf..dee9bd5e807 100644 --- a/ReactCommon/cxxreact/CxxNativeModule.cpp +++ b/ReactCommon/cxxreact/CxxNativeModule.cpp @@ -3,6 +3,8 @@ #include "CxxNativeModule.h" #include "Instance.h" +#include + #include #include @@ -26,6 +28,24 @@ std::function makeCallback( }; } +namespace { + +/** + * CxxModule::Callback accepts a vector, makeCallback returns + * a callback that accepts a dynamic, adapt the second into the first. + * TODO: Callback types should be made equal (preferably + * function) to avoid the extra copy and indirect call. + */ +CxxModule::Callback convertCallback( + std::function callback) { + return [callback = std::move(callback)](std::vector args) { + callback(folly::dynamic(std::make_move_iterator(args.begin()), + std::make_move_iterator(args.end()))); + }; +} + +} + CxxNativeModule::CxxNativeModule(std::weak_ptr instance, std::unique_ptr module) : instance_(instance) @@ -86,10 +106,13 @@ void CxxNativeModule::invoke(ExecutorToken token, unsigned int reactMethodId, fo } 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) { - first = makeCallback(instance_, token, params[params.size() - 2]); - second = makeCallback(instance_, token, params[params.size() - 1]); + first = convertCallback( + makeCallback(instance_, token, params[params.size() - 2])); + second = convertCallback( + makeCallback(instance_, token, params[params.size() - 1])); } params.resize(params.size() - method.callbacks); @@ -158,4 +181,3 @@ MethodCallResult CxxNativeModule::callSerializableNativeHook( } } -