Don't use the same throw for calling into JSC, and calling into native modules

Differential Revision: D3779181

fbshipit-source-id: 7fb17c9c90abd9302137dad3a326c6ce30e7ca86
This commit is contained in:
Marc Horowitz 2016-09-01 14:59:25 -07:00 committed by Facebook Github Bot 6
parent 46e47aaecd
commit 1d571c5ed5
2 changed files with 38 additions and 25 deletions

View File

@ -344,15 +344,14 @@ 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
}
@ -360,31 +359,44 @@ void JSCExecutor::flush() {
}
}
void JSCExecutor::flush() {
callNativeModules(m_flushedQueueJS->callAsFunction({}));
}
void JSCExecutor::callFunction(const std::string& moduleId, const std::string& methodId, const folly::dynamic& arguments) {
// This weird pattern is because Value is not default constructible.
// The lambda is inlined, so there's no overhead.
auto result = [&] {
try {
auto result = m_callFunctionReturnFlushedQueueJS->callAsFunction({
return 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));
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) {
auto result = [&] {
try {
auto result = m_invokeCallbackAndReturnFlushedQueueJS->callAsFunction({
return 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)));
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) {

View File

@ -117,6 +117,7 @@ private:
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);