WebWorkers: Extract evaluateScript logic to JSCHelpers

Reviewed By: lexs

Differential Revision: D2779277

fb-gh-sync-id: ab9a040193f36f40a4a34229a4a90199537253aa
This commit is contained in:
Andy Street 2015-12-29 19:47:22 -08:00 committed by facebook-github-bot-5
parent a68f8f4224
commit 690eb08e63
3 changed files with 29 additions and 27 deletions

View File

@ -70,31 +70,6 @@ static JSValueRef nativePerformanceNow(
const JSValueRef arguments[], const JSValueRef arguments[],
JSValueRef *exception); 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( static std::string executeJSCallWithJSC(
JSGlobalContextRef ctx, JSGlobalContextRef ctx,
const std::string& methodName, const std::string& methodName,
@ -110,7 +85,7 @@ static std::string executeJSCallWithJSC(
auto js = folly::to<folly::fbstring>( auto js = folly::to<folly::fbstring>(
"__fbBatchedBridge.", methodName, ".apply(null, ", "__fbBatchedBridge.", methodName, ".apply(null, ",
folly::toJson(jsonArgs), ")"); folly::toJson(jsonArgs), ")");
auto result = evaluateScriptWithJSC(ctx, String(js.c_str()), nullptr); auto result = evaluateScript(ctx, String(js.c_str()), nullptr);
JSValueProtect(ctx, result); JSValueProtect(ctx, result);
return Value(ctx, result).toJSONString(); return Value(ctx, result).toJSONString();
} }
@ -167,7 +142,7 @@ void JSCExecutor::executeApplicationScript(
FbSystraceSection s(TRACE_TAG_REACT_CXX_BRIDGE, "JSCExecutor::executeApplicationScript", FbSystraceSection s(TRACE_TAG_REACT_CXX_BRIDGE, "JSCExecutor::executeApplicationScript",
"sourceURL", sourceURL); "sourceURL", sourceURL);
#endif #endif
evaluateScriptWithJSC(m_context, jsScript, jsSourceURL); evaluateScript(m_context, jsScript, jsSourceURL);
} }
std::string JSCExecutor::flush() { std::string JSCExecutor::flush() {

View File

@ -3,6 +3,10 @@
#include "JSCHelpers.h" #include "JSCHelpers.h"
#include <JavaScriptCore/JSStringRef.h> #include <JavaScriptCore/JSStringRef.h>
#include <fb/log.h>
#include <jni/fbjni/Exceptions.h>
#include "Value.h"
namespace facebook { namespace facebook {
namespace react { namespace react {
@ -28,4 +32,24 @@ JSValueRef makeJSCException(
return JSValueToObject(ctx, exceptionString, NULL); 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;
}
} } } }

View File

@ -4,6 +4,7 @@
#include <JavaScriptCore/JSContextRef.h> #include <JavaScriptCore/JSContextRef.h>
#include <JavaScriptCore/JSObjectRef.h> #include <JavaScriptCore/JSObjectRef.h>
#include <JavaScriptCore/JSValueRef.h>
#define throwJSExecutionException(...) jni::throwNewJavaException("com/facebook/react/bridge/JSExecutionException", __VA_ARGS__) #define throwJSExecutionException(...) jni::throwNewJavaException("com/facebook/react/bridge/JSExecutionException", __VA_ARGS__)
@ -19,4 +20,6 @@ JSValueRef makeJSCException(
JSContextRef ctx, JSContextRef ctx,
const char* exception_text); const char* exception_text);
JSValueRef evaluateScript(JSContextRef context, JSStringRef script, JSStringRef source);
} } } }