Skip to content

Commit

Permalink
Don't use the same throw for calling into JSC, and calling into nativ…
Browse files Browse the repository at this point in the history
…e modules

Differential Revision: D3779181

fbshipit-source-id: 7fb17c9c90abd9302137dad3a326c6ce30e7ca86
  • Loading branch information
mhorowitz authored and Facebook Github Bot 6 committed Sep 1, 2016
1 parent 46e47aa commit 1d571c5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
62 changes: 37 additions & 25 deletions ReactCommon/cxxreact/JSCExecutor.cpp
Expand Up @@ -344,47 +344,59 @@ void JSCExecutor::bindBridge() throw(JSException) {
m_flushedQueueJS = batchedBridge.getProperty("flushedQueue").asObject();
}

void JSCExecutor::flush() {
auto result = m_flushedQueueJS->callAsFunction({});
void JSCExecutor::callNativeModules(Value&& value) {
try {
auto calls = Value(m_context, result).toJSONString();
auto calls = value.toJSONString();
m_delegate->callNativeModules(*this, std::move(calls), true);
} catch (...) {
std::string message = "Error in flush()";
try {
message += ":" + Value(m_context, result).toString().str();
message += ":" + value.toString().str();
} catch (...) {
// ignored
}
std::throw_with_nested(std::runtime_error(message));
}
}

void JSCExecutor::flush() {
callNativeModules(m_flushedQueueJS->callAsFunction({}));
}

void JSCExecutor::callFunction(const std::string& moduleId, const std::string& methodId, const folly::dynamic& arguments) {
try {
auto result = m_callFunctionReturnFlushedQueueJS->callAsFunction({
Value(m_context, String::createExpectingAscii(moduleId)),
Value(m_context, String::createExpectingAscii(methodId)),
Value::fromDynamic(m_context, std::move(arguments))
});
auto calls = Value(m_context, result).toJSONString();
m_delegate->callNativeModules(*this, std::move(calls), true);
} catch (...) {
std::throw_with_nested(std::runtime_error("Error calling function: " + moduleId + ":" + methodId));
}
// This weird pattern is because Value is not default constructible.
// The lambda is inlined, so there's no overhead.

auto result = [&] {
try {
return m_callFunctionReturnFlushedQueueJS->callAsFunction({
Value(m_context, String::createExpectingAscii(moduleId)),
Value(m_context, String::createExpectingAscii(methodId)),
Value::fromDynamic(m_context, std::move(arguments))
});
} catch (...) {
std::throw_with_nested(
std::runtime_error("Error calling function: " + moduleId + ":" + methodId));
}
}();

callNativeModules(std::move(result));
}

void JSCExecutor::invokeCallback(const double callbackId, const folly::dynamic& arguments) {
try {
auto result = m_invokeCallbackAndReturnFlushedQueueJS->callAsFunction({
JSValueMakeNumber(m_context, callbackId),
Value::fromDynamic(m_context, std::move(arguments))
});
auto calls = Value(m_context, result).toJSONString();
m_delegate->callNativeModules(*this, std::move(calls), true);
} catch (...) {
std::throw_with_nested(std::runtime_error(folly::to<std::string>("Error invoking callback.", callbackId)));
}
auto result = [&] {
try {
return m_invokeCallbackAndReturnFlushedQueueJS->callAsFunction({
JSValueMakeNumber(m_context, callbackId),
Value::fromDynamic(m_context, std::move(arguments))
});
} catch (...) {
std::throw_with_nested(
std::runtime_error(folly::to<std::string>("Error invoking callback.", callbackId)));
}
}();

callNativeModules(std::move(result));
}

void JSCExecutor::setGlobalVariable(std::string propName, std::unique_ptr<const JSBigString> jsonValue) {
Expand Down
1 change: 1 addition & 0 deletions ReactCommon/cxxreact/JSCExecutor.h
Expand Up @@ -117,6 +117,7 @@ class JSCExecutor : public JSExecutor {
void initOnJSVMThread() throw(JSException);
void terminateOnJSVMThread();
void bindBridge() throw(JSException);
void callNativeModules(Value&&);
void flush();
void flushQueueImmediate(std::string queueJSON);
void loadModule(uint32_t moduleId);
Expand Down

0 comments on commit 1d571c5

Please sign in to comment.