Permalink
Browse files

Simplify macro usage in MethodInvoker

Reviewed By: kathryngray

Differential Revision: D5363934

fbshipit-source-id: a9cffb10b4d6244d1d66267f6c1e07ba69663b9c
  • Loading branch information...
javache authored and facebook-github-bot committed Jul 7, 2017
1 parent 9108f98 commit a8a29a92a0bf433379885b6cf58b2e111f77cb62
Showing with 34 additions and 25 deletions.
  1. +34 −25 ReactAndroid/src/main/jni/react/jni/MethodInvoker.cpp
@@ -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_OBJECT('S', JString, toStdString())
CASE_OBJECT('M', WritableNativeMap, cthis()->consume())
CASE_OBJECT('A', WritableNativeArray, cthis()->consume())
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 '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;

0 comments on commit a8a29a9

Please sign in to comment.