Simplify macro usage in MethodInvoker

Reviewed By: kathryngray

Differential Revision: D5363934

fbshipit-source-id: a9cffb10b4d6244d1d66267f6c1e07ba69663b9c
This commit is contained in:
Pieter De Baets 2017-07-07 03:02:03 -07:00 committed by Facebook Github Bot
parent 9108f98ca7
commit a8a29a92a0

View File

@ -185,20 +185,18 @@ MethodCallResult MethodInvoker::invoke(std::weak_ptr<Instance>& instance, alias_
return extract(instance, type, it, end);
});
#define CASE_PRIMITIVE(KEY, TYPE, METHOD) \
case KEY: { \
auto result = env->Call ## METHOD ## MethodA(module.get(), method_, args); \
throwPendingJniExceptionAsCppException(); \
return folly::dynamic(result); \
}
#define PRIMITIVE_CASE(METHOD) { \
auto result = env->Call ## METHOD ## MethodA(module.get(), method_, args); \
throwPendingJniExceptionAsCppException(); \
return folly::dynamic(result); \
}
#define CASE_OBJECT(KEY, JNI_CLASS, ACTIONS) \
case KEY: { \
auto jobject = env->CallObjectMethodA(module.get(), method_, args); \
throwPendingJniExceptionAsCppException(); \
auto result = adopt_local(static_cast<JNI_CLASS::javaobject>(jobject)); \
return folly::dynamic(result->ACTIONS); \
}
#define OBJECT_CASE(JNI_CLASS, ACTIONS) { \
auto jobject = env->CallObjectMethodA(module.get(), method_, args); \
throwPendingJniExceptionAsCppException(); \
auto result = adopt_local(static_cast<JNI_CLASS::javaobject>(jobject)); \
return folly::dynamic(result->ACTIONS()); \
}
char returnType = signature_.at(0);
switch (returnType) {
@ -207,18 +205,29 @@ MethodCallResult MethodInvoker::invoke(std::weak_ptr<Instance>& instance, alias_
throwPendingJniExceptionAsCppException();
return folly::none;
CASE_PRIMITIVE('z', jboolean, Boolean)
CASE_OBJECT('Z', JBoolean, value())
CASE_PRIMITIVE('i', jint, Int)
CASE_OBJECT('I', JInteger, value())
CASE_PRIMITIVE('d', jdouble, Double)
CASE_OBJECT('D', JDouble, value())
CASE_PRIMITIVE('f', jfloat, Float)
CASE_OBJECT('F', JFloat, value())
case 'z':
PRIMITIVE_CASE(Boolean)
case 'Z':
OBJECT_CASE(JBoolean, value)
case 'i':
PRIMITIVE_CASE(Int)
case 'I':
OBJECT_CASE(JInteger, value)
case 'd':
PRIMITIVE_CASE(Double)
case 'D':
OBJECT_CASE(JDouble, value)
case 'f':
PRIMITIVE_CASE(Float)
case 'F':
OBJECT_CASE(JFloat, value)
CASE_OBJECT('S', JString, toStdString())
CASE_OBJECT('M', WritableNativeMap, cthis()->consume())
CASE_OBJECT('A', WritableNativeArray, cthis()->consume())
case 'S':
OBJECT_CASE(JString, toStdString)
case 'M':
OBJECT_CASE(WritableNativeMap, cthis()->consume)
case 'A':
OBJECT_CASE(WritableNativeArray, cthis()->consume)
default:
LOG(FATAL) << "Unknown return type: " << returnType;