add support for registering and calling sync methods in C++ modules

Differential Revision: D3574800

fbshipit-source-id: 4c238fd96c8f83e9424c8e2bf2c8ebb1d39d397a
This commit is contained in:
Marc Horowitz
2016-08-03 18:03:42 -07:00
committed by Facebook Github Bot 8
parent a7ca90e7c7
commit e5ccdc4c2d
3 changed files with 78 additions and 5 deletions

View File

@@ -3,6 +3,8 @@
#include "CxxNativeModule.h"
#include "Instance.h"
#include <folly/json.h>
#include <cxxreact/JsArgumentHelpers.h>
using facebook::xplat::module::CxxModule;
@@ -37,10 +39,12 @@ std::string CxxNativeModule::getName() {
std::vector<MethodDescriptor> CxxNativeModule::getMethods() {
// Same as MessageQueue.MethodTypes.remote
static const auto kMethodTypeRemote = "remote";
static const auto kMethodTypeSyncHook = "syncHook";
std::vector<MethodDescriptor> descs;
for (auto& method : methods_) {
descs.emplace_back(method.name, kMethodTypeRemote);
assert(method.func || method.syncFunc);
descs.emplace_back(method.name, method.func ? kMethodTypeRemote : kMethodTypeSyncHook);
}
return descs;
}
@@ -73,6 +77,12 @@ void CxxNativeModule::invoke(ExecutorToken token, unsigned int reactMethodId, fo
const auto& method = methods_[reactMethodId];
if (!method.func) {
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 ",
@@ -119,8 +129,35 @@ void CxxNativeModule::invoke(ExecutorToken token, unsigned int reactMethodId, fo
}
}
MethodCallResult CxxNativeModule::callSerializableNativeHook(ExecutorToken token, unsigned int hookId, folly::dynamic&& args) {
throw std::runtime_error("Not supported");
MethodCallResult CxxNativeModule::callSerializableNativeHook(
ExecutorToken token, 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(), "]"));
}
const auto& method = methods_[hookId];
if (!method.syncFunc) {
throw std::runtime_error(
folly::to<std::string>("Method ", method.name,
" is asynchronous but invoked synchronously"));
}
if (!args.isString()) {
throw std::invalid_argument(
folly::to<std::string>("method parameters should be string, but are ", args.typeName()));
}
folly::dynamic params = folly::parseJson(args.stringPiece());
if (!params.isArray()) {
throw std::invalid_argument(
folly::to<std::string>("parsed method parameters should be array, but are ",
args.typeName()));
}
return { method.syncFunc(std::move(params)), false };
}
}