Skip to content

Commit

Permalink
Use c++ exceptions in react/
Browse files Browse the repository at this point in the history
Reviewed By: astreet

Differential Revision: D2905193

fb-gh-sync-id: ddb4c615ede606e99c92a09a96a15141b912ef72
shipit-source-id: ddb4c615ede606e99c92a09a96a15141b912ef72
  • Loading branch information
cjhopman authored and facebook-github-bot-5 committed Feb 9, 2016
1 parent 229c8c3 commit 7cda49d
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
3 changes: 0 additions & 3 deletions ReactAndroid/src/main/jni/react/JSCExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#include <fb/log.h>
#include <folly/json.h>
#include <folly/String.h>
#include <jni/fbjni/Exceptions.h>
#include <sys/time.h>
#include "Value.h"
#include "jni/OnLoad.h"
Expand Down Expand Up @@ -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 {

Expand Down
1 change: 0 additions & 1 deletion ReactAndroid/src/main/jni/react/JSCHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <JavaScriptCore/JSStringRef.h>
#include <glog/logging.h>
#include <jni/fbjni/Exceptions.h>

#include "Value.h"

Expand Down
20 changes: 19 additions & 1 deletion ReactAndroid/src/main/jni/react/JSCHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,29 @@
#include <JavaScriptCore/JSObjectRef.h>
#include <JavaScriptCore/JSValueRef.h>

#define throwJSExecutionException(...) jni::throwNewJavaException("com/facebook/react/bridge/JSExecutionException", __VA_ARGS__)
#include <stdexcept>
#include <algorithm>

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 <typename... Args>
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,
Expand Down
3 changes: 1 addition & 2 deletions ReactAndroid/src/main/jni/react/JSCWebWorker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

#include <glog/logging.h>
#include <folly/Memory.h>
#include <jni/fbjni/Exceptions.h>
#include <jni/LocalReference.h>

#include "JSCHelpers.h"
#include "jni/JSLoader.h"
Expand Down Expand Up @@ -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!";;
}
Expand Down
25 changes: 11 additions & 14 deletions ReactAndroid/src/main/jni/react/MethodCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

#include "MethodCall.h"

#include <jni/fbjni.h>

#include <folly/json.h>
#include <stdexcept>

namespace facebook {
namespace react {
Expand All @@ -22,13 +21,13 @@ std::vector<MethodCall> 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<std::string>("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<std::string>("Did not get valid calls back from JS: size == ", jsonData.size()));
}

auto moduleIds = jsonData[REQUEST_MODULE_IDS];
Expand All @@ -37,16 +36,14 @@ std::vector<MethodCall> 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<std::string>("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<std::string>("Did not get valid calls back from JS: %s", json.c_str()));
} else {
callId = jsonData[REQUEST_CALLID].getInt();
}
Expand All @@ -56,8 +53,8 @@ std::vector<MethodCall> 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<std::string>("Call argument isn't an array"));
}

methodCalls.emplace_back(
Expand Down

0 comments on commit 7cda49d

Please sign in to comment.