diff --git a/ReactCommon/cxxreact/JSCExecutor.cpp b/ReactCommon/cxxreact/JSCExecutor.cpp index 42967be0c..b2b50fdc6 100644 --- a/ReactCommon/cxxreact/JSCExecutor.cpp +++ b/ReactCommon/cxxreact/JSCExecutor.cpp @@ -340,42 +340,64 @@ void JSCExecutor::bindBridge() throw(JSException) { m_flushedQueueJS = batchedBridge.getProperty("flushedQueue").asObject(); } -void JSCExecutor::flush() throw(JSException) { +void JSCExecutor::flush() { auto result = m_flushedQueueJS->callAsFunction({}); - auto calls = Value(m_context, result).toJSONString(); - m_delegate->callNativeModules(*this, std::move(calls), true); + try { + auto calls = Value(m_context, result).toJSONString(); + m_delegate->callNativeModules(*this, std::move(calls), true); + } catch (...) { + std::string message = "Error in flush()"; + try { + message += ":" + Value(m_context, result).toString().str(); + } catch (...) { + // ignored + } + std::throw_with_nested(std::runtime_error(message)); + } } -void JSCExecutor::callFunction(const std::string& moduleId, const std::string& methodId, const folly::dynamic& arguments) throw(JSException) { - 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); +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)); + } } -void JSCExecutor::invokeCallback(const double callbackId, const folly::dynamic& arguments) throw(JSException) { - 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); +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("Error invoking callback.", callbackId))); + } } -void JSCExecutor::setGlobalVariable(std::string propName, std::unique_ptr jsonValue) throw(JSException) { - SystraceSection s("JSCExecutor.setGlobalVariable", - "propName", propName); +void JSCExecutor::setGlobalVariable(std::string propName, std::unique_ptr jsonValue) { + try { + SystraceSection s("JSCExecutor.setGlobalVariable", + "propName", propName); - auto globalObject = JSContextGetGlobalObject(m_context); - String jsPropertyName(propName.c_str()); + auto globalObject = JSContextGetGlobalObject(m_context); + String jsPropertyName(propName.c_str()); - String jsValueJSON = jsStringFromBigString(*jsonValue); - auto valueToInject = JSValueMakeFromJSONString(m_context, jsValueJSON); + String jsValueJSON = jsStringFromBigString(*jsonValue); + auto valueToInject = JSValueMakeFromJSONString(m_context, jsValueJSON); - JSObjectSetProperty(m_context, globalObject, jsPropertyName, valueToInject, 0, NULL); + JSObjectSetProperty(m_context, globalObject, jsPropertyName, valueToInject, 0, NULL); + } catch (...) { + std::throw_with_nested(std::runtime_error("Error setting global variable: " + propName)); + } } void* JSCExecutor::getJavaScriptContext() { diff --git a/ReactCommon/cxxreact/JSCExecutor.h b/ReactCommon/cxxreact/JSCExecutor.h index b02c7e5b6..455fa5b9d 100644 --- a/ReactCommon/cxxreact/JSCExecutor.h +++ b/ReactCommon/cxxreact/JSCExecutor.h @@ -70,13 +70,13 @@ public: virtual void callFunction( const std::string& moduleId, const std::string& methodId, - const folly::dynamic& arguments) throw(JSException) override; + const folly::dynamic& arguments) override; virtual void invokeCallback( const double callbackId, - const folly::dynamic& arguments) throw(JSException) override; + const folly::dynamic& arguments) override; virtual void setGlobalVariable( std::string propName, - std::unique_ptr jsonValue) throw(JSException) override; + std::unique_ptr jsonValue) override; virtual void* getJavaScriptContext() override; virtual bool supportsProfiling() override; virtual void startProfiler(const std::string &titleString) override; @@ -117,7 +117,7 @@ private: void initOnJSVMThread() throw(JSException); void terminateOnJSVMThread(); void bindBridge() throw(JSException); - void flush() throw(JSException); + void flush(); void flushQueueImmediate(std::string queueJSON); void loadModule(uint32_t moduleId); diff --git a/ReactCommon/cxxreact/Value.cpp b/ReactCommon/cxxreact/Value.cpp index 7bc40b20f..43b6e4413 100644 --- a/ReactCommon/cxxreact/Value.cpp +++ b/ReactCommon/cxxreact/Value.cpp @@ -35,7 +35,7 @@ JSContextRef Value::context() const { return m_context; } -std::string Value::toJSONString(unsigned indent) const throw(JSException) { +std::string Value::toJSONString(unsigned indent) const { JSValueRef exn; auto stringToAdopt = JSValueCreateJSONString(m_context, m_value, indent, &exn); if (stringToAdopt == nullptr) { @@ -46,7 +46,7 @@ std::string Value::toJSONString(unsigned indent) const throw(JSException) { } /* static */ -Value Value::fromJSON(JSContextRef ctx, const String& json) throw(JSException) { +Value Value::fromJSON(JSContextRef ctx, const String& json) { auto result = JSValueMakeFromJSONString(ctx, json); if (!result) { throwJSExecutionException("Failed to create String from JSON"); diff --git a/ReactCommon/cxxreact/Value.h b/ReactCommon/cxxreact/Value.h index 044f3a522..8d924df15 100644 --- a/ReactCommon/cxxreact/Value.h +++ b/ReactCommon/cxxreact/Value.h @@ -282,8 +282,8 @@ public: return String::adopt(JSValueToStringCopy(context(), m_value, nullptr)); } - std::string toJSONString(unsigned indent = 0) const throw(JSException); - static Value fromJSON(JSContextRef ctx, const String& json) throw(JSException); + std::string toJSONString(unsigned indent = 0) const; + static Value fromJSON(JSContextRef ctx, const String& json); static JSValueRef fromDynamic(JSContextRef ctx, const folly::dynamic& value); JSContextRef context() const; protected: