From 7cda49d516c7868341daf12564864cfca8c2d520 Mon Sep 17 00:00:00 2001 From: Chris Hopman Date: Tue, 9 Feb 2016 12:59:36 -0800 Subject: [PATCH] Use c++ exceptions in react/ Reviewed By: astreet Differential Revision: D2905193 fb-gh-sync-id: ddb4c615ede606e99c92a09a96a15141b912ef72 shipit-source-id: ddb4c615ede606e99c92a09a96a15141b912ef72 --- .../src/main/jni/react/JSCExecutor.cpp | 3 --- .../src/main/jni/react/JSCHelpers.cpp | 1 - ReactAndroid/src/main/jni/react/JSCHelpers.h | 20 ++++++++++++++- .../src/main/jni/react/JSCWebWorker.cpp | 3 +-- .../src/main/jni/react/MethodCall.cpp | 25 ++++++++----------- 5 files changed, 31 insertions(+), 21 deletions(-) diff --git a/ReactAndroid/src/main/jni/react/JSCExecutor.cpp b/ReactAndroid/src/main/jni/react/JSCExecutor.cpp index 8655ee5dc..c9b1c1966 100644 --- a/ReactAndroid/src/main/jni/react/JSCExecutor.cpp +++ b/ReactAndroid/src/main/jni/react/JSCExecutor.cpp @@ -9,7 +9,6 @@ #include #include #include -#include #include #include "Value.h" #include "jni/OnLoad.h" @@ -42,8 +41,6 @@ using fbsystrace::FbSystraceSection; static const int64_t NANOSECONDS_IN_SECOND = 1000000000LL; static const int64_t NANOSECONDS_IN_MILLISECOND = 1000000LL; -using namespace facebook::jni; - namespace facebook { namespace react { diff --git a/ReactAndroid/src/main/jni/react/JSCHelpers.cpp b/ReactAndroid/src/main/jni/react/JSCHelpers.cpp index b24651bd3..0711c273f 100644 --- a/ReactAndroid/src/main/jni/react/JSCHelpers.cpp +++ b/ReactAndroid/src/main/jni/react/JSCHelpers.cpp @@ -4,7 +4,6 @@ #include #include -#include #include "Value.h" diff --git a/ReactAndroid/src/main/jni/react/JSCHelpers.h b/ReactAndroid/src/main/jni/react/JSCHelpers.h index de3bfbacb..c765421c4 100644 --- a/ReactAndroid/src/main/jni/react/JSCHelpers.h +++ b/ReactAndroid/src/main/jni/react/JSCHelpers.h @@ -6,11 +6,29 @@ #include #include -#define throwJSExecutionException(...) jni::throwNewJavaException("com/facebook/react/bridge/JSExecutionException", __VA_ARGS__) +#include +#include namespace facebook { namespace react { +struct JsException : std::runtime_error { + using std::runtime_error::runtime_error; +}; + +inline void throwJSExecutionException(const char* msg) { + throw JsException(msg); +} + +template +inline void throwJSExecutionException(const char* fmt, Args... args) { + int msgSize = snprintf(nullptr, 0, fmt, args...); + msgSize = std::min(512, msgSize + 1); + char *msg = (char*) alloca(msgSize); + snprintf(msg, msgSize, fmt, args...); + throw JsException(msg); +} + void installGlobalFunction( JSGlobalContextRef ctx, const char* name, diff --git a/ReactAndroid/src/main/jni/react/JSCWebWorker.cpp b/ReactAndroid/src/main/jni/react/JSCWebWorker.cpp index 62902fcf0..2c91d17fe 100644 --- a/ReactAndroid/src/main/jni/react/JSCWebWorker.cpp +++ b/ReactAndroid/src/main/jni/react/JSCWebWorker.cpp @@ -9,8 +9,6 @@ #include #include -#include -#include #include "JSCHelpers.h" #include "jni/JSLoader.h" @@ -43,6 +41,7 @@ JSCWebWorker::JSCWebWorker(int id, JSCWebWorkerOwner *owner, std::string scriptS }); } + JSCWebWorker::~JSCWebWorker() { CHECK(isTerminated()) << "Didn't terminate the web worker before releasing it!";; } diff --git a/ReactAndroid/src/main/jni/react/MethodCall.cpp b/ReactAndroid/src/main/jni/react/MethodCall.cpp index ac9b13edd..2782e05b6 100644 --- a/ReactAndroid/src/main/jni/react/MethodCall.cpp +++ b/ReactAndroid/src/main/jni/react/MethodCall.cpp @@ -2,9 +2,8 @@ #include "MethodCall.h" -#include - #include +#include namespace facebook { namespace react { @@ -22,13 +21,13 @@ std::vector parseMethodCalls(const std::string& json) { } if (!jsonData.isArray()) { - jni::throwNewJavaException(jni::gJavaLangIllegalArgumentException, - "Did not get valid calls back from JS: %s", jsonData.typeName()); + throw std::invalid_argument( + folly::to("Did not get valid calls back from JS: ", jsonData.typeName())); } if (jsonData.size() < REQUEST_PARAMSS + 1) { - jni::throwNewJavaException(jni::gJavaLangIllegalArgumentException, - "Did not get valid calls back from JS: size == %d", jsonData.size()); + throw std::invalid_argument( + folly::to("Did not get valid calls back from JS: size == ", jsonData.size())); } auto moduleIds = jsonData[REQUEST_MODULE_IDS]; @@ -37,16 +36,14 @@ std::vector parseMethodCalls(const std::string& json) { int callId = -1; if (!moduleIds.isArray() || !methodIds.isArray() || !params.isArray()) { - jni::throwNewJavaException(jni::gJavaLangIllegalArgumentException, - "Did not get valid calls back from JS: %s", - json.c_str()); + throw std::invalid_argument( + folly::to("Did not get valid calls back from JS: ", json.c_str())); } if (jsonData.size() > REQUEST_CALLID) { if (!jsonData[REQUEST_CALLID].isInt()) { - jni::throwNewJavaException(jni::gJavaLangIllegalArgumentException, - "Did not get valid calls back from JS: %s", - json.c_str()); + throw std::invalid_argument( + folly::to("Did not get valid calls back from JS: %s", json.c_str())); } else { callId = jsonData[REQUEST_CALLID].getInt(); } @@ -56,8 +53,8 @@ std::vector parseMethodCalls(const std::string& json) { for (size_t i = 0; i < moduleIds.size(); i++) { auto paramsValue = params[i]; if (!paramsValue.isArray()) { - jni::throwNewJavaException(jni::gJavaLangIllegalArgumentException, - "Call argument isn't an array"); + throw std::invalid_argument( + folly::to("Call argument isn't an array")); } methodCalls.emplace_back(