diff --git a/ReactCommon/cxxreact/JSCExecutor.cpp b/ReactCommon/cxxreact/JSCExecutor.cpp index 536313da4..c244532ac 100644 --- a/ReactCommon/cxxreact/JSCExecutor.cpp +++ b/ReactCommon/cxxreact/JSCExecutor.cpp @@ -93,7 +93,7 @@ static JSValueRef nativeInjectHMRUpdate( static std::string executeJSCallWithJSC( JSGlobalContextRef ctx, const std::string& methodName, - const std::vector& arguments) { + const std::vector& arguments) throw(JSException) { SystraceSection s("JSCExecutor.executeJSCall", "method", methodName); @@ -115,7 +115,7 @@ std::unique_ptr JSCExecutorFactory::createJSExecutor( JSCExecutor::JSCExecutor(std::shared_ptr delegate, std::shared_ptr messageQueueThread, const std::string& cacheDir, - const folly::dynamic& jscConfig) : + const folly::dynamic& jscConfig) throw(JSException) : m_delegate(delegate), m_deviceCacheDir(cacheDir), m_messageQueueThread(messageQueueThread), @@ -199,7 +199,7 @@ void JSCExecutor::destroy() { }); } -void JSCExecutor::initOnJSVMThread() { +void JSCExecutor::initOnJSVMThread() throw(JSException) { SystraceSection s("JSCExecutor.initOnJSVMThread"); #if defined(WITH_FB_JSC_TUNING) @@ -269,7 +269,7 @@ void JSCExecutor::terminateOnJSVMThread() { m_context = nullptr; } -void JSCExecutor::loadApplicationScript(std::unique_ptr script, std::string sourceURL) { +void JSCExecutor::loadApplicationScript(std::unique_ptr script, std::string sourceURL) throw(JSException) { SystraceSection s("JSCExecutor::loadApplicationScript", "sourceURL", sourceURL); @@ -300,14 +300,15 @@ void JSCExecutor::setJSModulesUnbundle(std::unique_ptr unbund m_unbundle = std::move(unbundle); } -void JSCExecutor::flush() { +void JSCExecutor::flush() throw(JSException) { // TODO: Make this a first class function instead of evaling. #9317773 std::string calls = executeJSCallWithJSC(m_context, "flushedQueue", std::vector()); m_delegate->callNativeModules(*this, std::move(calls), true); } -void JSCExecutor::callFunction(const std::string& moduleId, const std::string& methodId, const folly::dynamic& arguments) { +void JSCExecutor::callFunction(const std::string& moduleId, const std::string& methodId, const folly::dynamic& arguments) throw(JSException) { // TODO: Make this a first class function instead of evaling. #9317773 + // TODO(cjhopman): This copies args. std::vector call{ moduleId, methodId, @@ -317,8 +318,9 @@ void JSCExecutor::callFunction(const std::string& moduleId, const std::string& m m_delegate->callNativeModules(*this, std::move(calls), true); } -void JSCExecutor::invokeCallback(const double callbackId, const folly::dynamic& arguments) { +void JSCExecutor::invokeCallback(const double callbackId, const folly::dynamic& arguments) throw(JSException) { // TODO: Make this a first class function instead of evaling. #9317773 + // TODO(cjhopman): This copies args. std::vector call{ (double) callbackId, std::move(arguments) @@ -327,7 +329,7 @@ void JSCExecutor::invokeCallback(const double callbackId, const folly::dynamic& m_delegate->callNativeModules(*this, std::move(calls), true); } -void JSCExecutor::setGlobalVariable(std::string propName, std::unique_ptr jsonValue) { +void JSCExecutor::setGlobalVariable(std::string propName, std::unique_ptr jsonValue) throw(JSException) { SystraceSection s("JSCExecutor.setGlobalVariable", "propName", propName); diff --git a/ReactCommon/cxxreact/JSCExecutor.h b/ReactCommon/cxxreact/JSCExecutor.h index 735e1c45e..816bec1a5 100644 --- a/ReactCommon/cxxreact/JSCExecutor.h +++ b/ReactCommon/cxxreact/JSCExecutor.h @@ -52,24 +52,24 @@ public: explicit JSCExecutor(std::shared_ptr delegate, std::shared_ptr messageQueueThread, const std::string& cacheDir, - const folly::dynamic& jscConfig); + const folly::dynamic& jscConfig) throw(JSException); ~JSCExecutor() override; virtual void loadApplicationScript( std::unique_ptr script, - std::string sourceURL) override; + std::string sourceURL) throw(JSException) override; virtual void setJSModulesUnbundle( std::unique_ptr unbundle) override; virtual void callFunction( const std::string& moduleId, const std::string& methodId, - const folly::dynamic& arguments) override; + const folly::dynamic& arguments) throw(JSException) override; virtual void invokeCallback( const double callbackId, - const folly::dynamic& arguments) override; + const folly::dynamic& arguments) throw(JSException) override; virtual void setGlobalVariable( std::string propName, - std::unique_ptr jsonValue) override; + std::unique_ptr jsonValue) throw(JSException) override; virtual void* getJavaScriptContext() override; virtual bool supportsProfiling() override; virtual void startProfiler(const std::string &titleString) override; @@ -103,9 +103,9 @@ private: std::unordered_map globalObjAsJSON, const folly::dynamic& jscConfig); - void initOnJSVMThread(); + void initOnJSVMThread() throw(JSException); void terminateOnJSVMThread(); - void flush(); + void flush() throw(JSException); void flushQueueImmediate(std::string queueJSON); void loadModule(uint32_t moduleId); diff --git a/ReactCommon/cxxreact/JSCHelpers.h b/ReactCommon/cxxreact/JSCHelpers.h index eb29058ef..0d2609a75 100644 --- a/ReactCommon/cxxreact/JSCHelpers.h +++ b/ReactCommon/cxxreact/JSCHelpers.h @@ -12,29 +12,9 @@ #include #include -#include "Value.h" - namespace facebook { namespace react { -class JSException : public std::runtime_error { -public: - explicit JSException(const char* msg) - : std::runtime_error(msg) - , stack_("") {} - - JSException(const char* msg, const char* stack) - : std::runtime_error(msg) - , stack_(stack) {} - - const std::string& getStack() const { - return stack_; - } - -private: - std::string stack_; -}; - inline void throwJSExecutionException(const char* msg) { throw JSException(msg); } diff --git a/ReactCommon/cxxreact/MethodCall.cpp b/ReactCommon/cxxreact/MethodCall.cpp index 2782e05b6..efb142c6c 100644 --- a/ReactCommon/cxxreact/MethodCall.cpp +++ b/ReactCommon/cxxreact/MethodCall.cpp @@ -13,7 +13,7 @@ namespace react { #define REQUEST_PARAMSS 2 #define REQUEST_CALLID 3 -std::vector parseMethodCalls(const std::string& json) { +std::vector parseMethodCalls(const std::string& json) throw(std::invalid_argument) { folly::dynamic jsonData = folly::parseJson(json); if (jsonData.isNull()) { diff --git a/ReactCommon/cxxreact/MethodCall.h b/ReactCommon/cxxreact/MethodCall.h index 02e86eb98..a28bf40e5 100644 --- a/ReactCommon/cxxreact/MethodCall.h +++ b/ReactCommon/cxxreact/MethodCall.h @@ -24,6 +24,6 @@ struct MethodCall { , callId(cid) {} }; -std::vector parseMethodCalls(const std::string& json); +std::vector parseMethodCalls(const std::string& json) throw(std::invalid_argument); } } diff --git a/ReactCommon/cxxreact/Value.cpp b/ReactCommon/cxxreact/Value.cpp index a8ccf6614..9b236b0ce 100644 --- a/ReactCommon/cxxreact/Value.cpp +++ b/ReactCommon/cxxreact/Value.cpp @@ -30,7 +30,7 @@ JSContextRef Value::context() const { return m_context; } -std::string Value::toJSONString(unsigned indent) const { +std::string Value::toJSONString(unsigned indent) const throw(JSException) { JSValueRef exn; auto stringToAdopt = JSValueCreateJSONString(m_context, m_value, indent, &exn); if (stringToAdopt == nullptr) { @@ -41,7 +41,7 @@ std::string Value::toJSONString(unsigned indent) const { } /* static */ -Value Value::fromJSON(JSContextRef ctx, const String& json) { +Value Value::fromJSON(JSContextRef ctx, const String& json) throw(JSException) { 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 7813f0839..6291aabbe 100644 --- a/ReactCommon/cxxreact/Value.h +++ b/ReactCommon/cxxreact/Value.h @@ -24,6 +24,25 @@ namespace react { class Value; class Context; +class JSException : public std::runtime_error { +public: + explicit JSException(const char* msg) + : std::runtime_error(msg) + , stack_("") {} + + JSException(const char* msg, const char* stack) + : std::runtime_error(msg) + , stack_(stack) {} + + const std::string& getStack() const { + return stack_; + } + +private: + std::string stack_; +}; + + class String : public noncopyable { public: explicit String(const char* utf8) : @@ -228,12 +247,12 @@ public: return JSValueIsString(context(), m_value); } - String toString() { + String toString() noexcept { return String::adopt(JSValueToStringCopy(context(), m_value, nullptr)); } - std::string toJSONString(unsigned indent = 0) const; - static Value fromJSON(JSContextRef ctx, const String& json); + std::string toJSONString(unsigned indent = 0) const throw(JSException); + static Value fromJSON(JSContextRef ctx, const String& json) throw(JSException); protected: JSContextRef context() const; JSContextRef m_context;