2016-05-04 02:29:58 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
|
|
|
#include "JSCExecutor.h"
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <condition_variable>
|
|
|
|
#include <mutex>
|
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
|
|
|
#include <glog/logging.h>
|
|
|
|
#include <folly/json.h>
|
2016-05-14 00:15:06 +00:00
|
|
|
#include <folly/Memory.h>
|
2016-05-04 02:29:58 +00:00
|
|
|
#include <folly/String.h>
|
2016-05-12 21:32:42 +00:00
|
|
|
#include <folly/Conv.h>
|
2016-05-04 02:29:58 +00:00
|
|
|
#include <sys/time.h>
|
|
|
|
|
2016-05-27 23:02:50 +00:00
|
|
|
#include "FollySupport.h"
|
2016-05-04 02:29:58 +00:00
|
|
|
#include "JSCHelpers.h"
|
|
|
|
#include "Platform.h"
|
2016-05-14 00:15:06 +00:00
|
|
|
#include "SystraceSection.h"
|
2016-05-04 02:29:58 +00:00
|
|
|
#include "Value.h"
|
|
|
|
|
2016-05-14 00:15:03 +00:00
|
|
|
#if defined(WITH_JSC_EXTRA_TRACING) || DEBUG
|
2016-05-04 02:29:58 +00:00
|
|
|
#include "JSCTracing.h"
|
2016-05-14 00:15:03 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WITH_JSC_EXTRA_TRACING
|
2016-05-04 02:29:58 +00:00
|
|
|
#include "JSCLegacyProfiler.h"
|
2016-05-14 00:15:03 +00:00
|
|
|
#include "JSCLegacyTracing.h"
|
2016-05-04 02:29:58 +00:00
|
|
|
#include <JavaScriptCore/API/JSProfilerPrivate.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WITH_JSC_MEMORY_PRESSURE
|
|
|
|
#include <jsc_memory.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WITH_FB_MEMORY_PROFILING
|
|
|
|
#include "JSCMemory.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WITH_FB_JSC_TUNING
|
|
|
|
#include <jsc_config_android.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef JSC_HAS_PERF_STATS_API
|
|
|
|
#include "JSCPerfStats.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
2016-05-12 21:32:37 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
template<JSValueRef (JSCExecutor::*method)(size_t, const JSValueRef[])>
|
|
|
|
inline JSObjectCallAsFunctionCallback exceptionWrapMethod() {
|
|
|
|
struct funcWrapper {
|
|
|
|
static JSValueRef call(
|
|
|
|
JSContextRef ctx,
|
|
|
|
JSObjectRef function,
|
|
|
|
JSObjectRef thisObject,
|
|
|
|
size_t argumentCount,
|
|
|
|
const JSValueRef arguments[],
|
|
|
|
JSValueRef *exception) {
|
|
|
|
try {
|
|
|
|
auto globalObj = JSContextGetGlobalObject(ctx);
|
|
|
|
auto executor = static_cast<JSCExecutor*>(JSObjectGetPrivate(globalObj));
|
|
|
|
return (executor->*method)(argumentCount, arguments);
|
|
|
|
} catch (...) {
|
|
|
|
try {
|
|
|
|
auto functionName = Object(ctx, function).getProperty("name").toString().str();
|
|
|
|
*exception = translatePendingCppExceptionToJSError(ctx, functionName.c_str());
|
|
|
|
} catch (...) {
|
|
|
|
*exception = makeJSError(ctx, "Failed to get function name while handling exception");
|
|
|
|
}
|
|
|
|
return JSValueMakeUndefined(ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return &funcWrapper::call;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
static JSValueRef nativeInjectHMRUpdate(
|
|
|
|
JSContextRef ctx,
|
|
|
|
JSObjectRef function,
|
|
|
|
JSObjectRef thisObject,
|
|
|
|
size_t argumentCount,
|
|
|
|
const JSValueRef arguments[],
|
|
|
|
JSValueRef *exception);
|
|
|
|
|
|
|
|
static std::string executeJSCallWithJSC(
|
|
|
|
JSGlobalContextRef ctx,
|
|
|
|
const std::string& methodName,
|
2016-06-16 23:00:22 +00:00
|
|
|
const std::vector<folly::dynamic>& arguments) throw(JSException) {
|
2016-05-14 00:15:06 +00:00
|
|
|
SystraceSection s("JSCExecutor.executeJSCall",
|
|
|
|
"method", methodName);
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
// Evaluate script with JSC
|
|
|
|
folly::dynamic jsonArgs(arguments.begin(), arguments.end());
|
|
|
|
auto js = folly::to<folly::fbstring>(
|
|
|
|
"__fbBatchedBridge.", methodName, ".apply(null, ",
|
|
|
|
folly::toJson(jsonArgs), ")");
|
|
|
|
auto result = evaluateScript(ctx, String(js.c_str()), nullptr);
|
|
|
|
return Value(ctx, result).toJSONString();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<JSExecutor> JSCExecutorFactory::createJSExecutor(
|
2016-05-18 19:46:01 +00:00
|
|
|
std::shared_ptr<ExecutorDelegate> delegate, std::shared_ptr<MessageQueueThread> jsQueue) {
|
2016-05-04 02:29:58 +00:00
|
|
|
return std::unique_ptr<JSExecutor>(
|
2016-05-18 19:46:01 +00:00
|
|
|
new JSCExecutor(delegate, jsQueue, m_cacheDir, m_jscConfig));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
JSCExecutor::JSCExecutor(std::shared_ptr<ExecutorDelegate> delegate,
|
|
|
|
std::shared_ptr<MessageQueueThread> messageQueueThread,
|
|
|
|
const std::string& cacheDir,
|
2016-06-16 23:00:22 +00:00
|
|
|
const folly::dynamic& jscConfig) throw(JSException) :
|
2016-05-18 19:46:01 +00:00
|
|
|
m_delegate(delegate),
|
2016-05-04 02:29:58 +00:00
|
|
|
m_deviceCacheDir(cacheDir),
|
|
|
|
m_messageQueueThread(messageQueueThread),
|
|
|
|
m_jscConfig(jscConfig) {
|
|
|
|
initOnJSVMThread();
|
2016-05-18 19:46:01 +00:00
|
|
|
|
|
|
|
SystraceSection s("setBatchedBridgeConfig");
|
|
|
|
|
|
|
|
folly::dynamic nativeModuleConfig = folly::dynamic::array();
|
|
|
|
|
|
|
|
{
|
|
|
|
SystraceSection s("collectNativeModuleNames");
|
|
|
|
std::vector<std::string> names = delegate->moduleNames();
|
|
|
|
for (auto& name : delegate->moduleNames()) {
|
|
|
|
nativeModuleConfig.push_back(folly::dynamic::array(std::move(name)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
folly::dynamic config =
|
|
|
|
folly::dynamic::object("remoteModuleConfig", std::move(nativeModuleConfig));
|
|
|
|
|
|
|
|
SystraceSection t("setGlobalVariable");
|
|
|
|
setGlobalVariable(
|
|
|
|
"__fbBatchedBridgeConfig",
|
2016-05-27 23:02:50 +00:00
|
|
|
folly::make_unique<JSBigStdString>(detail::toStdString(folly::toJson(config))));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSCExecutor::JSCExecutor(
|
2016-05-18 19:46:01 +00:00
|
|
|
std::shared_ptr<ExecutorDelegate> delegate,
|
2016-05-04 02:29:58 +00:00
|
|
|
std::shared_ptr<MessageQueueThread> messageQueueThread,
|
|
|
|
int workerId,
|
|
|
|
JSCExecutor *owner,
|
2016-05-14 00:15:06 +00:00
|
|
|
std::string scriptURL,
|
|
|
|
std::unordered_map<std::string, std::string> globalObjAsJSON,
|
2016-05-04 02:29:58 +00:00
|
|
|
const folly::dynamic& jscConfig) :
|
2016-05-18 19:46:01 +00:00
|
|
|
m_delegate(delegate),
|
2016-05-04 02:29:58 +00:00
|
|
|
m_workerId(workerId),
|
|
|
|
m_owner(owner),
|
|
|
|
m_deviceCacheDir(owner->m_deviceCacheDir),
|
|
|
|
m_messageQueueThread(messageQueueThread),
|
|
|
|
m_jscConfig(jscConfig) {
|
|
|
|
// We post initOnJSVMThread here so that the owner doesn't have to wait for
|
|
|
|
// initialization on its own thread
|
2016-05-14 00:15:06 +00:00
|
|
|
m_messageQueueThread->runOnQueue([this, scriptURL,
|
|
|
|
globalObjAsJSON=std::move(globalObjAsJSON)] () {
|
2016-05-04 02:29:58 +00:00
|
|
|
initOnJSVMThread();
|
|
|
|
|
2016-05-12 21:32:42 +00:00
|
|
|
installNativeHook<&JSCExecutor::nativePostMessage>("postMessage");
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
for (auto& it : globalObjAsJSON) {
|
2016-05-14 00:15:06 +00:00
|
|
|
setGlobalVariable(std::move(it.first),
|
|
|
|
folly::make_unique<JSBigStdString>(std::move(it.second)));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to load the script from the network if script is a URL
|
|
|
|
// NB: For security, this will only work in debug builds
|
2016-05-14 00:15:06 +00:00
|
|
|
std::unique_ptr<const JSBigString> script;
|
|
|
|
if (scriptURL.find("http://") == 0 || scriptURL.find("https://") == 0) {
|
2016-05-04 02:29:58 +00:00
|
|
|
std::stringstream outfileBuilder;
|
|
|
|
outfileBuilder << m_deviceCacheDir << "/workerScript" << m_workerId << ".js";
|
2016-05-14 00:15:06 +00:00
|
|
|
script = folly::make_unique<JSBigStdString>(
|
|
|
|
WebWorkerUtil::loadScriptFromNetworkSync(scriptURL, outfileBuilder.str()));
|
2016-05-04 02:29:58 +00:00
|
|
|
} else {
|
|
|
|
// TODO(9604438): Protect against script does not exist
|
2016-05-14 00:15:06 +00:00
|
|
|
script = WebWorkerUtil::loadScriptFromAssets(scriptURL);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(9994180): Throw on error
|
2016-05-14 00:15:06 +00:00
|
|
|
loadApplicationScript(std::move(script), std::move(scriptURL));
|
2016-05-04 02:29:58 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
JSCExecutor::~JSCExecutor() {
|
|
|
|
CHECK(*m_isDestroyed) << "JSCExecutor::destroy() must be called before its destructor!";
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSCExecutor::destroy() {
|
|
|
|
*m_isDestroyed = true;
|
|
|
|
m_messageQueueThread->runOnQueueSync([this] () {
|
|
|
|
terminateOnJSVMThread();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-06-16 23:00:22 +00:00
|
|
|
void JSCExecutor::initOnJSVMThread() throw(JSException) {
|
2016-05-14 00:15:06 +00:00
|
|
|
SystraceSection s("JSCExecutor.initOnJSVMThread");
|
2016-05-14 00:15:00 +00:00
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
#if defined(WITH_FB_JSC_TUNING)
|
|
|
|
configureJSCForAndroid(m_jscConfig);
|
|
|
|
#endif
|
2016-05-12 21:32:37 +00:00
|
|
|
|
2016-05-17 11:21:19 +00:00
|
|
|
JSClassRef globalClass = nullptr;
|
|
|
|
{
|
|
|
|
SystraceSection s("JSClassCreate");
|
|
|
|
globalClass = JSClassCreate(&kJSClassDefinitionEmpty);
|
|
|
|
}
|
|
|
|
{
|
|
|
|
SystraceSection s("JSGlobalContextCreateInGroup");
|
|
|
|
m_context = JSGlobalContextCreateInGroup(nullptr, globalClass);
|
|
|
|
}
|
2016-05-12 21:32:37 +00:00
|
|
|
JSClassRelease(globalClass);
|
|
|
|
|
|
|
|
// Add a pointer to ourselves so we can retrieve it later in our hooks
|
|
|
|
JSObjectSetPrivate(JSContextGetGlobalObject(m_context), this);
|
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
installNativeHook<&JSCExecutor::nativeRequireModuleConfig>("nativeRequireModuleConfig");
|
2016-05-12 21:32:42 +00:00
|
|
|
installNativeHook<&JSCExecutor::nativeFlushQueueImmediate>("nativeFlushQueueImmediate");
|
|
|
|
installNativeHook<&JSCExecutor::nativeStartWorker>("nativeStartWorker");
|
|
|
|
installNativeHook<&JSCExecutor::nativePostMessageToWorker>("nativePostMessageToWorker");
|
|
|
|
installNativeHook<&JSCExecutor::nativeTerminateWorker>("nativeTerminateWorker");
|
2016-05-04 02:29:58 +00:00
|
|
|
installGlobalFunction(m_context, "nativeInjectHMRUpdate", nativeInjectHMRUpdate);
|
2016-05-12 21:32:42 +00:00
|
|
|
installNativeHook<&JSCExecutor::nativeCallSyncHook>("nativeCallSyncHook");
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
installGlobalFunction(m_context, "nativeLoggingHook", JSNativeHooks::loggingHook);
|
|
|
|
installGlobalFunction(m_context, "nativePerformanceNow", JSNativeHooks::nowHook);
|
|
|
|
|
2016-05-14 00:15:03 +00:00
|
|
|
#if defined(WITH_JSC_EXTRA_TRACING) || DEBUG
|
2016-05-04 02:29:58 +00:00
|
|
|
addNativeTracingHooks(m_context);
|
2016-05-14 00:15:03 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WITH_JSC_EXTRA_TRACING
|
2016-05-04 02:29:58 +00:00
|
|
|
addNativeProfilingHooks(m_context);
|
2016-05-14 00:15:03 +00:00
|
|
|
addNativeTracingLegacyHooks(m_context);
|
2016-05-04 02:29:58 +00:00
|
|
|
PerfLogging::installNativeHooks(m_context);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WITH_FB_MEMORY_PROFILING
|
|
|
|
addNativeMemoryHooks(m_context);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef JSC_HAS_PERF_STATS_API
|
|
|
|
addJSCPerfStatsHooks(m_context);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(WITH_FB_JSC_TUNING)
|
|
|
|
configureJSContextForAndroid(m_context, m_jscConfig, m_deviceCacheDir);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSCExecutor::terminateOnJSVMThread() {
|
|
|
|
// terminateOwnedWebWorker mutates m_ownedWorkers so collect all the workers
|
|
|
|
// to terminate first
|
|
|
|
std::vector<int> workerIds;
|
|
|
|
for (auto& it : m_ownedWorkers) {
|
|
|
|
workerIds.push_back(it.first);
|
|
|
|
}
|
|
|
|
for (int workerId : workerIds) {
|
|
|
|
terminateOwnedWebWorker(workerId);
|
|
|
|
}
|
|
|
|
|
|
|
|
JSGlobalContextRelease(m_context);
|
|
|
|
m_context = nullptr;
|
|
|
|
}
|
|
|
|
|
2016-06-16 23:00:22 +00:00
|
|
|
void JSCExecutor::loadApplicationScript(std::unique_ptr<const JSBigString> script, std::string sourceURL) throw(JSException) {
|
2016-05-14 00:15:06 +00:00
|
|
|
SystraceSection s("JSCExecutor::loadApplicationScript",
|
|
|
|
"sourceURL", sourceURL);
|
2016-05-14 00:15:00 +00:00
|
|
|
|
|
|
|
#ifdef WITH_FBSYSTRACE
|
|
|
|
fbsystrace_begin_section(
|
|
|
|
TRACE_TAG_REACT_CXX_BRIDGE,
|
|
|
|
"JSCExecutor::loadApplicationScript-createExpectingAscii");
|
|
|
|
#endif
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
ReactMarker::logMarker("loadApplicationScript_startStringConvert");
|
2016-05-14 00:15:06 +00:00
|
|
|
String jsScript = jsStringFromBigString(*script);
|
2016-05-04 02:29:58 +00:00
|
|
|
ReactMarker::logMarker("loadApplicationScript_endStringConvert");
|
|
|
|
|
|
|
|
#ifdef WITH_FBSYSTRACE
|
2016-05-14 00:15:00 +00:00
|
|
|
fbsystrace_end_section(TRACE_TAG_REACT_CXX_BRIDGE);
|
2016-05-04 02:29:58 +00:00
|
|
|
#endif
|
2016-05-14 00:15:00 +00:00
|
|
|
|
|
|
|
String jsSourceURL(sourceURL.c_str());
|
2016-05-04 02:29:58 +00:00
|
|
|
evaluateScript(m_context, jsScript, jsSourceURL);
|
|
|
|
flush();
|
|
|
|
ReactMarker::logMarker("CREATE_REACT_CONTEXT_END");
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSCExecutor::setJSModulesUnbundle(std::unique_ptr<JSModulesUnbundle> unbundle) {
|
|
|
|
if (!m_unbundle) {
|
2016-05-12 21:32:42 +00:00
|
|
|
installNativeHook<&JSCExecutor::nativeRequire>("nativeRequire");
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
m_unbundle = std::move(unbundle);
|
|
|
|
}
|
|
|
|
|
2016-06-16 23:00:22 +00:00
|
|
|
void JSCExecutor::flush() throw(JSException) {
|
2016-05-04 02:29:58 +00:00
|
|
|
// TODO: Make this a first class function instead of evaling. #9317773
|
|
|
|
std::string calls = executeJSCallWithJSC(m_context, "flushedQueue", std::vector<folly::dynamic>());
|
2016-05-18 19:46:01 +00:00
|
|
|
m_delegate->callNativeModules(*this, std::move(calls), true);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 23:00:22 +00:00
|
|
|
void JSCExecutor::callFunction(const std::string& moduleId, const std::string& methodId, const folly::dynamic& arguments) throw(JSException) {
|
2016-05-04 02:29:58 +00:00
|
|
|
// TODO: Make this a first class function instead of evaling. #9317773
|
2016-06-16 23:00:22 +00:00
|
|
|
// TODO(cjhopman): This copies args.
|
2016-05-04 02:29:58 +00:00
|
|
|
std::vector<folly::dynamic> call{
|
|
|
|
moduleId,
|
|
|
|
methodId,
|
|
|
|
std::move(arguments),
|
|
|
|
};
|
|
|
|
std::string calls = executeJSCallWithJSC(m_context, "callFunctionReturnFlushedQueue", std::move(call));
|
2016-05-18 19:46:01 +00:00
|
|
|
m_delegate->callNativeModules(*this, std::move(calls), true);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 23:00:22 +00:00
|
|
|
void JSCExecutor::invokeCallback(const double callbackId, const folly::dynamic& arguments) throw(JSException) {
|
2016-05-04 02:29:58 +00:00
|
|
|
// TODO: Make this a first class function instead of evaling. #9317773
|
2016-06-16 23:00:22 +00:00
|
|
|
// TODO(cjhopman): This copies args.
|
2016-05-04 02:29:58 +00:00
|
|
|
std::vector<folly::dynamic> call{
|
|
|
|
(double) callbackId,
|
|
|
|
std::move(arguments)
|
|
|
|
};
|
|
|
|
std::string calls = executeJSCallWithJSC(m_context, "invokeCallbackAndReturnFlushedQueue", std::move(call));
|
2016-05-18 19:46:01 +00:00
|
|
|
m_delegate->callNativeModules(*this, std::move(calls), true);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-06-16 23:00:22 +00:00
|
|
|
void JSCExecutor::setGlobalVariable(std::string propName, std::unique_ptr<const JSBigString> jsonValue) throw(JSException) {
|
2016-05-14 00:15:06 +00:00
|
|
|
SystraceSection s("JSCExecutor.setGlobalVariable",
|
|
|
|
"propName", propName);
|
2016-05-14 00:15:03 +00:00
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
auto globalObject = JSContextGetGlobalObject(m_context);
|
|
|
|
String jsPropertyName(propName.c_str());
|
|
|
|
|
2016-05-14 00:15:06 +00:00
|
|
|
String jsValueJSON = jsStringFromBigString(*jsonValue);
|
2016-05-04 02:29:58 +00:00
|
|
|
auto valueToInject = JSValueMakeFromJSONString(m_context, jsValueJSON);
|
|
|
|
|
|
|
|
JSObjectSetProperty(m_context, globalObject, jsPropertyName, valueToInject, 0, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void* JSCExecutor::getJavaScriptContext() {
|
|
|
|
return m_context;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool JSCExecutor::supportsProfiling() {
|
|
|
|
#ifdef WITH_FBSYSTRACE
|
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSCExecutor::startProfiler(const std::string &titleString) {
|
|
|
|
#ifdef WITH_JSC_EXTRA_TRACING
|
|
|
|
JSStringRef title = JSStringCreateWithUTF8CString(titleString.c_str());
|
|
|
|
#if WITH_REACT_INTERNAL_SETTINGS
|
|
|
|
JSStartProfiling(m_context, title, false);
|
|
|
|
#else
|
|
|
|
JSStartProfiling(m_context, title);
|
|
|
|
#endif
|
|
|
|
JSStringRelease(title);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSCExecutor::stopProfiler(const std::string &titleString, const std::string& filename) {
|
|
|
|
#ifdef WITH_JSC_EXTRA_TRACING
|
|
|
|
JSStringRef title = JSStringCreateWithUTF8CString(titleString.c_str());
|
|
|
|
facebook::react::stopAndOutputProfilingFile(m_context, title, filename.c_str());
|
|
|
|
JSStringRelease(title);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-05-26 18:07:41 +00:00
|
|
|
void JSCExecutor::handleMemoryPressureUiHidden() {
|
|
|
|
#ifdef WITH_JSC_MEMORY_PRESSURE
|
|
|
|
JSHandleMemoryPressure(this, m_context, JSMemoryPressure::UI_HIDDEN);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
void JSCExecutor::handleMemoryPressureModerate() {
|
|
|
|
#ifdef WITH_JSC_MEMORY_PRESSURE
|
|
|
|
JSHandleMemoryPressure(this, m_context, JSMemoryPressure::MODERATE);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSCExecutor::handleMemoryPressureCritical() {
|
|
|
|
#ifdef WITH_JSC_MEMORY_PRESSURE
|
|
|
|
JSHandleMemoryPressure(this, m_context, JSMemoryPressure::CRITICAL);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSCExecutor::flushQueueImmediate(std::string queueJSON) {
|
2016-05-18 19:46:01 +00:00
|
|
|
m_delegate->callNativeModules(*this, std::move(queueJSON), false);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void JSCExecutor::loadModule(uint32_t moduleId) {
|
|
|
|
auto module = m_unbundle->getModule(moduleId);
|
|
|
|
auto sourceUrl = String::createExpectingAscii(module.name);
|
|
|
|
auto source = String::createExpectingAscii(module.code);
|
|
|
|
evaluateScript(m_context, source, sourceUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
int JSCExecutor::addWebWorker(
|
2016-05-14 00:15:06 +00:00
|
|
|
std::string scriptURL,
|
2016-05-04 02:29:58 +00:00
|
|
|
JSValueRef workerRef,
|
|
|
|
JSValueRef globalObjRef) {
|
|
|
|
static std::atomic_int nextWorkerId(1);
|
|
|
|
int workerId = nextWorkerId++;
|
|
|
|
|
|
|
|
Object globalObj = Value(m_context, globalObjRef).asObject();
|
|
|
|
|
2016-05-12 15:12:15 +00:00
|
|
|
auto workerJscConfig = m_jscConfig;
|
|
|
|
workerJscConfig["isWebWorker"] = true;
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
std::shared_ptr<MessageQueueThread> workerMQT =
|
|
|
|
WebWorkerUtil::createWebWorkerThread(workerId, m_messageQueueThread.get());
|
|
|
|
std::unique_ptr<JSCExecutor> worker;
|
2016-05-14 00:15:06 +00:00
|
|
|
workerMQT->runOnQueueSync([this, &worker, &workerMQT, &scriptURL, &globalObj, workerId, &workerJscConfig] () {
|
2016-05-18 19:46:01 +00:00
|
|
|
worker.reset(new JSCExecutor(m_delegate, workerMQT, workerId, this, std::move(scriptURL),
|
2016-05-12 15:12:15 +00:00
|
|
|
globalObj.toJSONMap(), workerJscConfig));
|
2016-05-04 02:29:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
Object workerObj = Value(m_context, workerRef).asObject();
|
|
|
|
workerObj.makeProtected();
|
|
|
|
|
|
|
|
JSCExecutor *workerPtr = worker.get();
|
|
|
|
std::shared_ptr<MessageQueueThread> sharedMessageQueueThread = worker->m_messageQueueThread;
|
2016-05-18 19:46:01 +00:00
|
|
|
m_delegate->registerExecutor(
|
2016-05-04 02:29:58 +00:00
|
|
|
std::move(worker),
|
|
|
|
std::move(sharedMessageQueueThread));
|
|
|
|
|
|
|
|
m_ownedWorkers.emplace(
|
|
|
|
std::piecewise_construct,
|
|
|
|
std::forward_as_tuple(workerId),
|
2016-05-18 19:46:01 +00:00
|
|
|
std::forward_as_tuple(workerPtr, std::move(workerObj)));
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
return workerId;
|
|
|
|
}
|
|
|
|
|
2016-05-12 21:32:42 +00:00
|
|
|
void JSCExecutor::postMessageToOwnedWebWorker(int workerId, JSValueRef message) {
|
2016-05-04 02:29:58 +00:00
|
|
|
auto worker = m_ownedWorkers.at(workerId).executor;
|
|
|
|
std::string msgString = Value(m_context, message).toJSONString();
|
|
|
|
|
|
|
|
std::shared_ptr<bool> isWorkerDestroyed = worker->m_isDestroyed;
|
|
|
|
worker->m_messageQueueThread->runOnQueue([isWorkerDestroyed, worker, msgString] () {
|
|
|
|
if (*isWorkerDestroyed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
worker->receiveMessageFromOwner(msgString);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSCExecutor::postMessageToOwner(JSValueRef msg) {
|
|
|
|
std::string msgString = Value(m_context, msg).toJSONString();
|
|
|
|
std::shared_ptr<bool> ownerIsDestroyed = m_owner->m_isDestroyed;
|
|
|
|
m_owner->m_messageQueueThread->runOnQueue([workerId=m_workerId, owner=m_owner, ownerIsDestroyed, msgString] () {
|
|
|
|
if (*ownerIsDestroyed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
owner->receiveMessageFromOwnedWebWorker(workerId, msgString);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSCExecutor::receiveMessageFromOwnedWebWorker(int workerId, const std::string& json) {
|
|
|
|
Object* workerObj;
|
|
|
|
try {
|
|
|
|
workerObj = &m_ownedWorkers.at(workerId).jsObj;
|
|
|
|
} catch (std::out_of_range& e) {
|
|
|
|
// Worker was already terminated
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value onmessageValue = workerObj->getProperty("onmessage");
|
|
|
|
if (onmessageValue.isUndefined()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSValueRef args[] = { createMessageObject(json) };
|
|
|
|
onmessageValue.asObject().callAsFunction(1, args);
|
|
|
|
|
|
|
|
flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSCExecutor::receiveMessageFromOwner(const std::string& msgString) {
|
|
|
|
CHECK(m_owner) << "Received message in a Executor that doesn't have an owner!";
|
|
|
|
|
|
|
|
JSValueRef args[] = { createMessageObject(msgString) };
|
|
|
|
Value onmessageValue = Object::getGlobalObject(m_context).getProperty("onmessage");
|
|
|
|
onmessageValue.asObject().callAsFunction(1, args);
|
|
|
|
}
|
|
|
|
|
|
|
|
void JSCExecutor::terminateOwnedWebWorker(int workerId) {
|
|
|
|
auto& workerRegistration = m_ownedWorkers.at(workerId);
|
|
|
|
std::shared_ptr<MessageQueueThread> workerMQT = workerRegistration.executor->m_messageQueueThread;
|
|
|
|
m_ownedWorkers.erase(workerId);
|
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
workerMQT->runOnQueueSync([this, &workerMQT] {
|
2016-05-04 02:29:58 +00:00
|
|
|
workerMQT->quitSynchronous();
|
2016-05-18 19:46:01 +00:00
|
|
|
std::unique_ptr<JSExecutor> worker = m_delegate->unregisterExecutor(*this);
|
2016-05-04 02:29:58 +00:00
|
|
|
worker->destroy();
|
|
|
|
worker.reset();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Object JSCExecutor::createMessageObject(const std::string& msgJson) {
|
|
|
|
Value rebornJSMsg = Value::fromJSON(m_context, String(msgJson.c_str()));
|
|
|
|
Object messageObject = Object::create(m_context);
|
|
|
|
messageObject.setProperty("data", rebornJSMsg);
|
|
|
|
return messageObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Native JS hooks
|
2016-05-12 21:32:37 +00:00
|
|
|
template<JSValueRef (JSCExecutor::*method)(size_t, const JSValueRef[])>
|
|
|
|
void JSCExecutor::installNativeHook(const char* name) {
|
|
|
|
installGlobalFunction(m_context, name, exceptionWrapMethod<method>());
|
|
|
|
}
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
JSValueRef JSCExecutor::nativePostMessage(
|
|
|
|
size_t argumentCount,
|
2016-05-12 21:32:42 +00:00
|
|
|
const JSValueRef arguments[]) {
|
2016-05-04 02:29:58 +00:00
|
|
|
if (argumentCount != 1) {
|
2016-05-12 21:32:42 +00:00
|
|
|
throw std::invalid_argument("Got wrong number of args");
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
JSValueRef msg = arguments[0];
|
2016-05-12 21:32:42 +00:00
|
|
|
postMessageToOwner(msg);
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2016-05-12 21:32:42 +00:00
|
|
|
return JSValueMakeUndefined(m_context);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSValueRef JSCExecutor::nativeRequire(
|
|
|
|
size_t argumentCount,
|
2016-05-12 21:32:42 +00:00
|
|
|
const JSValueRef arguments[]) {
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
if (argumentCount != 1) {
|
2016-05-12 21:32:42 +00:00
|
|
|
throw std::invalid_argument("Got wrong number of args");
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 21:32:42 +00:00
|
|
|
double moduleId = Value(m_context, arguments[0]).asNumber();
|
2016-05-04 02:29:58 +00:00
|
|
|
if (moduleId <= (double) std::numeric_limits<uint32_t>::max() && moduleId >= 0.0) {
|
|
|
|
try {
|
2016-05-12 21:32:42 +00:00
|
|
|
loadModule(moduleId);
|
2016-05-14 00:14:51 +00:00
|
|
|
} catch (const std::exception&) {
|
2016-05-12 21:32:42 +00:00
|
|
|
throw std::invalid_argument(folly::to<std::string>("Received invalid module ID: ", moduleId));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
2016-05-12 21:32:42 +00:00
|
|
|
throw std::invalid_argument(folly::to<std::string>("Received invalid module ID: ", moduleId));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
2016-05-12 21:32:42 +00:00
|
|
|
return JSValueMakeUndefined(m_context);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
JSValueRef JSCExecutor::nativeRequireModuleConfig(
|
|
|
|
size_t argumentCount,
|
|
|
|
const JSValueRef arguments[]) {
|
|
|
|
if (argumentCount != 1) {
|
|
|
|
throw std::invalid_argument("Got wrong number of args");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string moduleName = Value(m_context, arguments[0]).toString().str();
|
|
|
|
folly::dynamic config = m_delegate->getModuleConfig(moduleName);
|
|
|
|
return JSValueMakeString(m_context, String(folly::toJson(config).c_str()));
|
|
|
|
}
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
JSValueRef JSCExecutor::nativeFlushQueueImmediate(
|
|
|
|
size_t argumentCount,
|
2016-05-12 21:32:42 +00:00
|
|
|
const JSValueRef arguments[]) {
|
2016-05-04 02:29:58 +00:00
|
|
|
if (argumentCount != 1) {
|
2016-05-12 21:32:42 +00:00
|
|
|
throw std::invalid_argument("Got wrong number of args");
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 21:32:42 +00:00
|
|
|
std::string resStr = Value(m_context, arguments[0]).toJSONString();
|
2016-05-18 19:46:01 +00:00
|
|
|
flushQueueImmediate(std::move(resStr));
|
2016-05-12 21:32:42 +00:00
|
|
|
return JSValueMakeUndefined(m_context);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSValueRef JSCExecutor::nativeStartWorker(
|
|
|
|
size_t argumentCount,
|
2016-05-12 21:32:42 +00:00
|
|
|
const JSValueRef arguments[]) {
|
2016-05-04 02:29:58 +00:00
|
|
|
if (argumentCount != 3) {
|
2016-05-12 21:32:42 +00:00
|
|
|
throw std::invalid_argument("Got wrong number of args");
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 21:32:42 +00:00
|
|
|
std::string scriptFile = Value(m_context, arguments[0]).toString().str();
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
JSValueRef worker = arguments[1];
|
|
|
|
JSValueRef globalObj = arguments[2];
|
|
|
|
|
2016-05-12 21:32:42 +00:00
|
|
|
int workerId = addWebWorker(scriptFile, worker, globalObj);
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2016-05-12 21:32:42 +00:00
|
|
|
return JSValueMakeNumber(m_context, workerId);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSValueRef JSCExecutor::nativePostMessageToWorker(
|
|
|
|
size_t argumentCount,
|
2016-05-12 21:32:42 +00:00
|
|
|
const JSValueRef arguments[]) {
|
2016-05-04 02:29:58 +00:00
|
|
|
if (argumentCount != 2) {
|
2016-05-12 21:32:42 +00:00
|
|
|
throw std::invalid_argument("Got wrong number of args");
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 21:32:42 +00:00
|
|
|
double workerDouble = Value(m_context, arguments[0]).asNumber();
|
2016-05-04 02:29:58 +00:00
|
|
|
if (workerDouble != workerDouble) {
|
2016-05-12 21:32:42 +00:00
|
|
|
throw std::invalid_argument("Got invalid worker id");
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 21:32:42 +00:00
|
|
|
postMessageToOwnedWebWorker((int) workerDouble, arguments[1]);
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2016-05-12 21:32:42 +00:00
|
|
|
return JSValueMakeUndefined(m_context);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSValueRef JSCExecutor::nativeTerminateWorker(
|
|
|
|
size_t argumentCount,
|
2016-05-12 21:32:42 +00:00
|
|
|
const JSValueRef arguments[]) {
|
2016-05-04 02:29:58 +00:00
|
|
|
if (argumentCount != 1) {
|
2016-05-12 21:32:42 +00:00
|
|
|
throw std::invalid_argument("Got wrong number of args");
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 21:32:42 +00:00
|
|
|
double workerDouble = Value(m_context, arguments[0]).asNumber();
|
2016-05-04 02:29:58 +00:00
|
|
|
if (workerDouble != workerDouble) {
|
2016-05-12 21:32:42 +00:00
|
|
|
std::invalid_argument("Got invalid worker id");
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 21:32:42 +00:00
|
|
|
terminateOwnedWebWorker((int) workerDouble);
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2016-05-12 21:32:42 +00:00
|
|
|
return JSValueMakeUndefined(m_context);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
JSValueRef JSCExecutor::nativeCallSyncHook(
|
|
|
|
size_t argumentCount,
|
2016-05-12 21:32:42 +00:00
|
|
|
const JSValueRef arguments[]) {
|
2016-05-04 02:29:58 +00:00
|
|
|
if (argumentCount != 3) {
|
2016-05-12 21:32:42 +00:00
|
|
|
throw std::invalid_argument("Got wrong number of args");
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 21:32:42 +00:00
|
|
|
unsigned int moduleId = Value(m_context, arguments[0]).asUnsignedInteger();
|
|
|
|
unsigned int methodId = Value(m_context, arguments[1]).asUnsignedInteger();
|
|
|
|
std::string argsJson = Value(m_context, arguments[2]).toJSONString();
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2016-05-18 19:46:01 +00:00
|
|
|
MethodCallResult result = m_delegate->callSerializableNativeHook(
|
|
|
|
*this,
|
2016-05-12 21:32:42 +00:00
|
|
|
moduleId,
|
|
|
|
methodId,
|
|
|
|
argsJson);
|
|
|
|
if (result.isUndefined) {
|
|
|
|
return JSValueMakeUndefined(m_context);
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
2016-05-12 21:32:42 +00:00
|
|
|
return Value::fromJSON(m_context, String(folly::toJson(result.result).c_str()));
|
2016-05-04 02:29:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSValueRef nativeInjectHMRUpdate(
|
|
|
|
JSContextRef ctx,
|
|
|
|
JSObjectRef function,
|
|
|
|
JSObjectRef thisObject,
|
|
|
|
size_t argumentCount,
|
|
|
|
const JSValueRef arguments[], JSValueRef *exception) {
|
|
|
|
String execJSString = Value(ctx, arguments[0]).toString();
|
|
|
|
String jsURL = Value(ctx, arguments[1]).toString();
|
|
|
|
evaluateScript(ctx, execJSString, jsURL);
|
|
|
|
return JSValueMakeUndefined(ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
} }
|