2016-08-03 22:58:45 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
|
|
|
#include "CxxNativeModule.h"
|
|
|
|
#include "Instance.h"
|
|
|
|
|
2016-10-26 16:36:39 +00:00
|
|
|
#include <iterator>
|
2017-10-12 18:49:42 +00:00
|
|
|
#include <glog/logging.h>
|
2016-08-04 01:03:42 +00:00
|
|
|
#include <folly/json.h>
|
|
|
|
|
2017-05-13 00:57:12 +00:00
|
|
|
#include "JsArgumentHelpers.h"
|
|
|
|
#include "SystraceSection.h"
|
2017-06-23 23:49:55 +00:00
|
|
|
#include "MessageQueueThread.h"
|
2016-08-03 22:58:45 +00:00
|
|
|
|
|
|
|
using facebook::xplat::module::CxxModule;
|
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
|
|
|
std::function<void(folly::dynamic)> makeCallback(
|
2017-04-25 12:29:45 +00:00
|
|
|
std::weak_ptr<Instance> instance, const folly::dynamic& callbackId) {
|
2017-08-18 21:43:28 +00:00
|
|
|
if (!callbackId.isNumber()) {
|
2016-08-03 22:58:45 +00:00
|
|
|
throw std::invalid_argument("Expected callback(s) as final argument");
|
|
|
|
}
|
|
|
|
|
2017-08-18 21:43:28 +00:00
|
|
|
auto id = callbackId.asInt();
|
2017-04-25 12:29:45 +00:00
|
|
|
return [winstance = std::move(instance), id](folly::dynamic args) {
|
2016-08-03 22:58:45 +00:00
|
|
|
if (auto instance = winstance.lock()) {
|
2017-04-25 12:29:45 +00:00
|
|
|
instance->callJSCallback(id, std::move(args));
|
2016-08-03 22:58:45 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2016-10-26 16:36:39 +00:00
|
|
|
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())));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-08-03 22:58:45 +00:00
|
|
|
std::string CxxNativeModule::getName() {
|
2017-03-14 22:28:53 +00:00
|
|
|
return name_;
|
2016-08-03 22:58:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<MethodDescriptor> CxxNativeModule::getMethods() {
|
2017-03-14 22:28:53 +00:00
|
|
|
lazyInit();
|
|
|
|
|
2016-08-03 22:58:45 +00:00
|
|
|
std::vector<MethodDescriptor> descs;
|
|
|
|
for (auto& method : methods_) {
|
2017-07-24 13:46:01 +00:00
|
|
|
descs.emplace_back(method.name, method.getType());
|
2016-08-03 22:58:45 +00:00
|
|
|
}
|
|
|
|
return descs;
|
|
|
|
}
|
|
|
|
|
|
|
|
folly::dynamic CxxNativeModule::getConstants() {
|
2017-03-14 22:28:53 +00:00
|
|
|
lazyInit();
|
|
|
|
|
2017-04-08 22:14:32 +00:00
|
|
|
if (!module_) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-08-03 22:58:45 +00:00
|
|
|
folly::dynamic constants = folly::dynamic::object();
|
|
|
|
for (auto& pair : module_->getConstants()) {
|
|
|
|
constants.insert(std::move(pair.first), std::move(pair.second));
|
|
|
|
}
|
|
|
|
return constants;
|
|
|
|
}
|
|
|
|
|
2017-05-13 00:57:12 +00:00
|
|
|
void CxxNativeModule::invoke(unsigned int reactMethodId, folly::dynamic&& params, int callId) {
|
2016-08-03 22:58:45 +00:00
|
|
|
if (reactMethodId >= methods_.size()) {
|
2017-03-17 13:55:37 +00:00
|
|
|
throw std::invalid_argument(folly::to<std::string>("methodId ", reactMethodId,
|
|
|
|
" out of range [0..", methods_.size(), "]"));
|
2016-08-03 22:58:45 +00:00
|
|
|
}
|
|
|
|
if (!params.isArray()) {
|
|
|
|
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];
|
|
|
|
|
2016-08-04 01:03:42 +00:00
|
|
|
if (!method.func) {
|
2017-03-17 13:55:37 +00:00
|
|
|
throw std::runtime_error(folly::to<std::string>("Method ", method.name,
|
|
|
|
" is synchronous but invoked asynchronously"));
|
2016-08-04 01:03:42 +00:00
|
|
|
}
|
|
|
|
|
2016-08-03 22:58:45 +00:00
|
|
|
if (params.size() < method.callbacks) {
|
2017-03-17 13:55:37 +00:00
|
|
|
throw std::invalid_argument(folly::to<std::string>("Expected ", method.callbacks,
|
|
|
|
" callbacks, but only ", params.size(), " parameters provided"));
|
2016-08-03 22:58:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (method.callbacks == 1) {
|
2017-04-25 12:29:45 +00:00
|
|
|
first = convertCallback(makeCallback(instance_, params[params.size() - 1]));
|
2016-08-03 22:58:45 +00:00
|
|
|
} else if (method.callbacks == 2) {
|
2017-04-25 12:29:45 +00:00
|
|
|
first = convertCallback(makeCallback(instance_, params[params.size() - 2]));
|
|
|
|
second = convertCallback(makeCallback(instance_, params[params.size() - 1]));
|
2016-08-03 22:58:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
params.resize(params.size() - method.callbacks);
|
|
|
|
|
|
|
|
// I've got a few flawed options here. I can let the C++ exception
|
2018-01-13 06:03:51 +00:00
|
|
|
// propagate, and the registry will log/convert them to java exceptions.
|
2016-08-03 22:58:45 +00:00
|
|
|
// This lets all the java and red box handling work ok, but the only info I
|
|
|
|
// can capture about the C++ exception is the what() string, not the stack.
|
|
|
|
// I can std::terminate() the app. This causes the full, accurate C++
|
|
|
|
// stack trace to be added to logcat by debuggerd. The java state is lost,
|
|
|
|
// but in practice, the java stack is always the same in this case since
|
|
|
|
// the javascript stack is not visible, and the crash is unfriendly to js
|
|
|
|
// developers, but crucial to C++ developers. The what() value is also
|
|
|
|
// lost. Finally, I can catch, log the java stack, then rethrow the C++
|
|
|
|
// exception. In this case I get java and C++ stack data, but the C++
|
|
|
|
// stack is as of the rethrow, not the original throw, both the C++ and
|
|
|
|
// java stacks always look the same.
|
|
|
|
//
|
|
|
|
// I am going with option 2, since that seems like the most useful
|
|
|
|
// choice. It would be nice to be able to get what() and the C++
|
|
|
|
// stack. I'm told that will be possible in the future. TODO
|
|
|
|
// mhorowitz #7128529: convert C++ exceptions to Java
|
|
|
|
|
2017-05-13 00:57:12 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
SystraceSection s(method.name.c_str());
|
2017-03-17 13:55:37 +00:00
|
|
|
try {
|
|
|
|
method.func(std::move(params), first, second);
|
|
|
|
} catch (const facebook::xplat::JsArgumentException& ex) {
|
|
|
|
throw;
|
2017-10-12 18:49:42 +00:00
|
|
|
} 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();
|
2017-03-17 13:55:37 +00:00
|
|
|
} catch (...) {
|
2017-10-12 18:49:42 +00:00
|
|
|
LOG(ERROR) << "Method call " << method.name.c_str() << " failed. unknown error";
|
2017-03-17 13:55:37 +00:00
|
|
|
std::terminate();
|
|
|
|
}
|
|
|
|
});
|
2016-08-03 22:58:45 +00:00
|
|
|
}
|
|
|
|
|
2017-04-25 12:29:45 +00:00
|
|
|
MethodCallResult CxxNativeModule::callSerializableNativeHook(unsigned int hookId, folly::dynamic&& args) {
|
2016-08-04 01:03:42 +00:00
|
|
|
if (hookId >= methods_.size()) {
|
|
|
|
throw std::invalid_argument(
|
|
|
|
folly::to<std::string>("methodId ", hookId, " out of range [0..", methods_.size(), "]"));
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto& method = methods_[hookId];
|
|
|
|
|
|
|
|
if (!method.syncFunc) {
|
|
|
|
throw std::runtime_error(
|
|
|
|
folly::to<std::string>("Method ", method.name,
|
|
|
|
" is asynchronous but invoked synchronously"));
|
|
|
|
}
|
|
|
|
|
2017-02-02 13:07:28 +00:00
|
|
|
return method.syncFunc(std::move(args));
|
2016-08-03 22:58:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 22:28:53 +00:00
|
|
|
void CxxNativeModule::lazyInit() {
|
2017-04-08 22:14:32 +00:00
|
|
|
if (module_ || !provider_) {
|
2017-03-14 22:28:53 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-08 22:14:32 +00:00
|
|
|
// TODO 17216751: providers should never return null modules
|
2017-03-14 22:28:53 +00:00
|
|
|
module_ = provider_();
|
2017-04-08 22:14:32 +00:00
|
|
|
provider_ = nullptr;
|
|
|
|
if (module_) {
|
|
|
|
methods_ = module_->getMethods();
|
|
|
|
module_->setInstance(instance_);
|
|
|
|
}
|
2017-03-14 22:28:53 +00:00
|
|
|
}
|
|
|
|
|
2016-08-03 22:58:45 +00:00
|
|
|
}
|
|
|
|
}
|