2016-05-04 02:29:58 +00:00
|
|
|
// Copyright 2004-present Facebook. All Rights Reserved.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <memory>
|
2017-03-20 20:03:04 +00:00
|
|
|
#include <mutex>
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2017-02-15 17:49:07 +00:00
|
|
|
#include <cxxreact/JSCNativeModules.h>
|
2017-06-23 23:49:55 +00:00
|
|
|
#include <cxxreact/JSExecutor.h>
|
2016-06-21 17:08:31 +00:00
|
|
|
#include <folly/Optional.h>
|
2017-02-15 17:49:07 +00:00
|
|
|
#include <folly/json.h>
|
2016-11-01 18:38:43 +00:00
|
|
|
#include <jschelpers/JSCHelpers.h>
|
2017-02-15 17:49:07 +00:00
|
|
|
#include <jschelpers/JavaScriptCore.h>
|
2016-11-01 18:38:43 +00:00
|
|
|
#include <jschelpers/Value.h>
|
2017-09-19 01:35:44 +00:00
|
|
|
#include <privatedata/PrivateDataBase.h>
|
2016-11-01 18:38:43 +00:00
|
|
|
|
2017-05-26 10:43:20 +00:00
|
|
|
#ifndef RN_EXPORT
|
|
|
|
#define RN_EXPORT __attribute__((visibility("default")))
|
|
|
|
#endif
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
|
|
|
class MessageQueueThread;
|
2017-09-21 15:34:44 +00:00
|
|
|
class RAMBundleRegistry;
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2017-01-10 15:04:49 +00:00
|
|
|
class RN_EXPORT JSCExecutorFactory : public JSExecutorFactory {
|
2016-05-04 02:29:58 +00:00
|
|
|
public:
|
2018-02-07 15:29:49 +00:00
|
|
|
JSCExecutorFactory(const folly::dynamic& jscConfig, std::function<folly::dynamic(const std::string &)> provider) :
|
|
|
|
m_jscConfig(jscConfig), m_nativeExtensionsProvider(provider) {}
|
2017-04-25 12:29:45 +00:00
|
|
|
std::unique_ptr<JSExecutor> createJSExecutor(
|
2016-05-18 19:46:01 +00:00
|
|
|
std::shared_ptr<ExecutorDelegate> delegate,
|
|
|
|
std::shared_ptr<MessageQueueThread> jsQueue) override;
|
2016-05-04 02:29:58 +00:00
|
|
|
private:
|
2016-05-18 19:46:01 +00:00
|
|
|
std::string m_cacheDir;
|
2016-05-04 02:29:58 +00:00
|
|
|
folly::dynamic m_jscConfig;
|
2018-02-07 15:29:49 +00:00
|
|
|
std::function<folly::dynamic(const std::string &)> m_nativeExtensionsProvider;
|
2016-05-04 02:29:58 +00:00
|
|
|
};
|
|
|
|
|
2017-08-07 13:41:15 +00:00
|
|
|
template<typename T>
|
|
|
|
struct JSCValueEncoder {
|
|
|
|
// If you get a build error here, it means the compiler can't see the template instantation of toJSCValue
|
|
|
|
// applicable to your type.
|
|
|
|
static const Value toJSCValue(JSGlobalContextRef ctx, T&& value);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct JSCValueEncoder<folly::dynamic> {
|
|
|
|
static const Value toJSCValue(JSGlobalContextRef ctx, const folly::dynamic &&value) {
|
|
|
|
return Value::fromDynamic(ctx, value);
|
|
|
|
}
|
|
|
|
};
|
2016-11-24 17:25:06 +00:00
|
|
|
|
2017-09-19 01:35:44 +00:00
|
|
|
class RN_EXPORT JSCExecutor : public JSExecutor, public PrivateDataBase {
|
2016-05-04 02:29:58 +00:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Must be invoked from thread this Executor will run on.
|
|
|
|
*/
|
2016-05-18 19:46:01 +00:00
|
|
|
explicit JSCExecutor(std::shared_ptr<ExecutorDelegate> delegate,
|
|
|
|
std::shared_ptr<MessageQueueThread> messageQueueThread,
|
2018-02-07 15:29:49 +00:00
|
|
|
const folly::dynamic& jscConfig,
|
|
|
|
std::function<folly::dynamic(const std::string &)> nativeExtensionsProvider) throw(JSException);
|
2016-05-04 02:29:58 +00:00
|
|
|
~JSCExecutor() override;
|
|
|
|
|
|
|
|
virtual void loadApplicationScript(
|
2016-05-14 00:15:06 +00:00
|
|
|
std::unique_ptr<const JSBigString> script,
|
2017-01-10 15:04:53 +00:00
|
|
|
std::string sourceURL) override;
|
2016-11-18 13:01:09 +00:00
|
|
|
|
2017-09-22 16:56:50 +00:00
|
|
|
virtual void setBundleRegistry(std::unique_ptr<RAMBundleRegistry> bundleRegistry) override;
|
2017-11-09 19:55:37 +00:00
|
|
|
virtual void registerBundle(uint32_t bundleId, const std::string& bundlePath) override;
|
2016-11-18 13:01:09 +00:00
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
virtual void callFunction(
|
|
|
|
const std::string& moduleId,
|
|
|
|
const std::string& methodId,
|
2016-07-22 00:32:42 +00:00
|
|
|
const folly::dynamic& arguments) override;
|
2016-11-18 13:01:09 +00:00
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
virtual void invokeCallback(
|
|
|
|
const double callbackId,
|
2016-07-22 00:32:42 +00:00
|
|
|
const folly::dynamic& arguments) override;
|
2016-11-18 13:01:09 +00:00
|
|
|
|
2016-09-26 23:01:43 +00:00
|
|
|
template <typename T>
|
|
|
|
Value callFunctionSync(
|
|
|
|
const std::string& module, const std::string& method, T&& args) {
|
2016-11-24 17:25:06 +00:00
|
|
|
return callFunctionSyncWithValue(
|
2017-08-07 13:41:15 +00:00
|
|
|
module, method, JSCValueEncoder<typename std::decay<T>::type>::toJSCValue(
|
2016-11-24 17:25:06 +00:00
|
|
|
m_context, std::forward<T>(args)));
|
2016-09-26 23:01:43 +00:00
|
|
|
}
|
2016-11-18 13:01:09 +00:00
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
virtual void setGlobalVariable(
|
2016-05-14 00:15:06 +00:00
|
|
|
std::string propName,
|
2016-07-22 00:32:42 +00:00
|
|
|
std::unique_ptr<const JSBigString> jsonValue) override;
|
2016-11-18 13:01:09 +00:00
|
|
|
|
2017-09-05 21:47:06 +00:00
|
|
|
virtual std::string getDescription() override;
|
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
virtual void* getJavaScriptContext() override;
|
2016-11-18 13:01:09 +00:00
|
|
|
|
2017-12-18 21:19:35 +00:00
|
|
|
virtual bool isInspectable() override;
|
|
|
|
|
2017-06-26 12:48:40 +00:00
|
|
|
#ifdef WITH_JSC_MEMORY_PRESSURE
|
|
|
|
virtual void handleMemoryPressure(int pressureLevel) override;
|
|
|
|
#endif
|
2016-11-18 13:01:09 +00:00
|
|
|
|
2016-05-04 02:29:58 +00:00
|
|
|
virtual void destroy() override;
|
2016-11-18 13:01:09 +00:00
|
|
|
|
2016-10-14 18:29:47 +00:00
|
|
|
void setContextName(const std::string& name);
|
2016-05-04 02:29:58 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
JSGlobalContextRef m_context;
|
2016-05-18 19:46:01 +00:00
|
|
|
std::shared_ptr<ExecutorDelegate> m_delegate;
|
2016-05-04 02:29:58 +00:00
|
|
|
std::shared_ptr<bool> m_isDestroyed = std::shared_ptr<bool>(new bool(false));
|
|
|
|
std::shared_ptr<MessageQueueThread> m_messageQueueThread;
|
2017-09-21 15:34:44 +00:00
|
|
|
std::unique_ptr<RAMBundleRegistry> m_bundleRegistry;
|
2016-10-11 14:19:31 +00:00
|
|
|
JSCNativeModules m_nativeModules;
|
2016-05-04 02:29:58 +00:00
|
|
|
folly::dynamic m_jscConfig;
|
2017-03-20 20:03:04 +00:00
|
|
|
std::once_flag m_bindFlag;
|
2018-02-07 15:29:49 +00:00
|
|
|
std::function<folly::dynamic(const std::string &)> m_nativeExtensionsProvider;
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2016-06-21 17:08:31 +00:00
|
|
|
folly::Optional<Object> m_invokeCallbackAndReturnFlushedQueueJS;
|
|
|
|
folly::Optional<Object> m_callFunctionReturnFlushedQueueJS;
|
|
|
|
folly::Optional<Object> m_flushedQueueJS;
|
2016-09-26 23:01:43 +00:00
|
|
|
folly::Optional<Object> m_callFunctionReturnResultAndFlushedQueueJS;
|
2016-06-21 17:08:31 +00:00
|
|
|
|
2016-06-16 23:00:22 +00:00
|
|
|
void initOnJSVMThread() throw(JSException);
|
2017-12-15 17:10:47 +00:00
|
|
|
static bool isNetworkInspected(const std::string &owner, const std::string &app, const std::string &device);
|
2016-09-26 23:01:43 +00:00
|
|
|
// This method is experimental, and may be modified or removed.
|
|
|
|
Value callFunctionSyncWithValue(
|
|
|
|
const std::string& module, const std::string& method, Value value);
|
2016-05-04 02:29:58 +00:00
|
|
|
void terminateOnJSVMThread();
|
2016-06-21 17:08:31 +00:00
|
|
|
void bindBridge() throw(JSException);
|
2016-09-01 21:59:25 +00:00
|
|
|
void callNativeModules(Value&&);
|
2016-07-22 00:32:42 +00:00
|
|
|
void flush();
|
2016-09-19 11:43:09 +00:00
|
|
|
void flushQueueImmediate(Value&&);
|
2017-09-21 15:34:44 +00:00
|
|
|
void loadModule(uint32_t bundleId, uint32_t moduleId);
|
2016-05-04 02:29:58 +00:00
|
|
|
|
2017-05-15 16:12:24 +00:00
|
|
|
String adoptString(std::unique_ptr<const JSBigString>);
|
|
|
|
|
2016-11-22 14:05:36 +00:00
|
|
|
template<JSValueRef (JSCExecutor::*method)(size_t, const JSValueRef[])>
|
2016-05-12 21:32:37 +00:00
|
|
|
void installNativeHook(const char* name);
|
2018-02-07 15:29:49 +00:00
|
|
|
|
2016-10-11 14:19:31 +00:00
|
|
|
JSValueRef getNativeModule(JSObjectRef object, JSStringRef propertyName);
|
2018-02-07 15:29:49 +00:00
|
|
|
JSValueRef getNativeExtension(JSObjectRef object, JSStringRef propertyName);
|
2016-05-12 21:32:37 +00:00
|
|
|
|
2016-05-12 21:32:42 +00:00
|
|
|
JSValueRef nativeRequire(
|
2016-05-04 02:29:58 +00:00
|
|
|
size_t argumentCount,
|
2016-05-12 21:32:42 +00:00
|
|
|
const JSValueRef arguments[]);
|
|
|
|
JSValueRef nativeFlushQueueImmediate(
|
2016-05-04 02:29:58 +00:00
|
|
|
size_t argumentCount,
|
2016-05-12 21:32:42 +00:00
|
|
|
const JSValueRef arguments[]);
|
|
|
|
JSValueRef nativeCallSyncHook(
|
2016-05-04 02:29:58 +00:00
|
|
|
size_t argumentCount,
|
2016-05-12 21:32:42 +00:00
|
|
|
const JSValueRef arguments[]);
|
2016-05-04 02:29:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} }
|