From 690eb08e63ef699b3cd74a65ba3b64b26099b461 Mon Sep 17 00:00:00 2001 From: Andy Street Date: Tue, 29 Dec 2015 19:47:22 -0800 Subject: [PATCH] WebWorkers: Extract evaluateScript logic to JSCHelpers Reviewed By: lexs Differential Revision: D2779277 fb-gh-sync-id: ab9a040193f36f40a4a34229a4a90199537253aa --- .../src/main/jni/react/JSCExecutor.cpp | 29 ++----------------- .../src/main/jni/react/JSCHelpers.cpp | 24 +++++++++++++++ ReactAndroid/src/main/jni/react/JSCHelpers.h | 3 ++ 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/ReactAndroid/src/main/jni/react/JSCExecutor.cpp b/ReactAndroid/src/main/jni/react/JSCExecutor.cpp index b678aff4c..b12021d41 100644 --- a/ReactAndroid/src/main/jni/react/JSCExecutor.cpp +++ b/ReactAndroid/src/main/jni/react/JSCExecutor.cpp @@ -70,31 +70,6 @@ static JSValueRef nativePerformanceNow( const JSValueRef arguments[], JSValueRef *exception); -static JSValueRef evaluateScriptWithJSC( - JSGlobalContextRef ctx, - JSStringRef script, - JSStringRef sourceURL) { - JSValueRef exn; - auto result = JSEvaluateScript(ctx, script, nullptr, sourceURL, 0, &exn); - if (result == nullptr) { - JSValueProtect(ctx, exn); - std::string exceptionText = Value(ctx, exn).toString().str(); - FBLOGE("Got JS Exception: %s", exceptionText.c_str()); - auto line = Value(ctx, JSObjectGetProperty(ctx, - JSValueToObject(ctx, exn, nullptr), - JSStringCreateWithUTF8CString("line"), nullptr - )); - std::ostringstream lineInfo; - if (line != nullptr && line.isNumber()) { - lineInfo << " (line " << line.asInteger() << " in the generated bundle)"; - } else { - lineInfo << " (no line info)"; - } - throwNewJavaException("com/facebook/react/bridge/JSExecutionException", (exceptionText + lineInfo.str()).c_str()); - } - return result; -} - static std::string executeJSCallWithJSC( JSGlobalContextRef ctx, const std::string& methodName, @@ -110,7 +85,7 @@ static std::string executeJSCallWithJSC( auto js = folly::to( "__fbBatchedBridge.", methodName, ".apply(null, ", folly::toJson(jsonArgs), ")"); - auto result = evaluateScriptWithJSC(ctx, String(js.c_str()), nullptr); + auto result = evaluateScript(ctx, String(js.c_str()), nullptr); JSValueProtect(ctx, result); return Value(ctx, result).toJSONString(); } @@ -167,7 +142,7 @@ void JSCExecutor::executeApplicationScript( FbSystraceSection s(TRACE_TAG_REACT_CXX_BRIDGE, "JSCExecutor::executeApplicationScript", "sourceURL", sourceURL); #endif - evaluateScriptWithJSC(m_context, jsScript, jsSourceURL); + evaluateScript(m_context, jsScript, jsSourceURL); } std::string JSCExecutor::flush() { diff --git a/ReactAndroid/src/main/jni/react/JSCHelpers.cpp b/ReactAndroid/src/main/jni/react/JSCHelpers.cpp index b1edc73dc..12702d03a 100644 --- a/ReactAndroid/src/main/jni/react/JSCHelpers.cpp +++ b/ReactAndroid/src/main/jni/react/JSCHelpers.cpp @@ -3,6 +3,10 @@ #include "JSCHelpers.h" #include +#include +#include + +#include "Value.h" namespace facebook { namespace react { @@ -28,4 +32,24 @@ JSValueRef makeJSCException( return JSValueToObject(ctx, exceptionString, NULL); } +JSValueRef evaluateScript(JSContextRef context, JSStringRef script, JSStringRef source) { + JSValueRef exn; + auto result = JSEvaluateScript(context, script, NULL, source, 0, &exn); + if (result == nullptr) { + Value exception = Value(context, exn); + std::string exceptionText = exception.toString().str(); + FBLOGE("Got JS Exception: %s", exceptionText.c_str()); + auto line = exception.asObject().getProperty("line"); + + std::ostringstream lineInfo; + if (line != nullptr && line.isNumber()) { + lineInfo << " (line " << line.asInteger() << " in the generated bundle)"; + } else { + lineInfo << " (no line info)"; + } + throwJSExecutionException("%s%s", exceptionText.c_str(), lineInfo.str().c_str()); + } + return result; +} + } } diff --git a/ReactAndroid/src/main/jni/react/JSCHelpers.h b/ReactAndroid/src/main/jni/react/JSCHelpers.h index f17374f18..fd19c48cc 100644 --- a/ReactAndroid/src/main/jni/react/JSCHelpers.h +++ b/ReactAndroid/src/main/jni/react/JSCHelpers.h @@ -4,6 +4,7 @@ #include #include +#include #define throwJSExecutionException(...) jni::throwNewJavaException("com/facebook/react/bridge/JSExecutionException", __VA_ARGS__) @@ -19,4 +20,6 @@ JSValueRef makeJSCException( JSContextRef ctx, const char* exception_text); +JSValueRef evaluateScript(JSContextRef context, JSStringRef script, JSStringRef source); + } }