2018-05-31 15:31:03 -07:00
|
|
|
// Copyright (c) 2004-present, Facebook, Inc.
|
|
|
|
|
|
|
|
// This source code is licensed under the MIT license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree.
|
2016-05-03 19:29:58 -07:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <memory>
|
2017-03-20 13:03:04 -07:00
|
|
|
#include <mutex>
|
2016-05-03 19:29:58 -07:00
|
|
|
|
2017-02-15 09:49:07 -08:00
|
|
|
#include <cxxreact/JSCNativeModules.h>
|
2017-06-23 16:49:55 -07:00
|
|
|
#include <cxxreact/JSExecutor.h>
|
2016-06-21 10:08:31 -07:00
|
|
|
#include <folly/Optional.h>
|
2017-02-15 09:49:07 -08:00
|
|
|
#include <folly/json.h>
|
2016-11-01 11:38:43 -07:00
|
|
|
#include <jschelpers/JSCHelpers.h>
|
2017-02-15 09:49:07 -08:00
|
|
|
#include <jschelpers/JavaScriptCore.h>
|
2016-11-01 11:38:43 -07:00
|
|
|
#include <jschelpers/Value.h>
|
2017-09-18 18:35:44 -07:00
|
|
|
#include <privatedata/PrivateDataBase.h>
|
2016-11-01 11:38:43 -07:00
|
|
|
|
2017-05-26 03:43:20 -07:00
|
|
|
#ifndef RN_EXPORT
|
|
|
|
#define RN_EXPORT __attribute__((visibility("default")))
|
|
|
|
#endif
|
|
|
|
|
2016-05-03 19:29:58 -07:00
|
|
|
namespace facebook {
|
|
|
|
namespace react {
|
|
|
|
|
|
|
|
class MessageQueueThread;
|
2017-09-21 08:34:44 -07:00
|
|
|
class RAMBundleRegistry;
|
2016-05-03 19:29:58 -07:00
|
|
|
|
2017-01-10 07:04:49 -08:00
|
|
|
class RN_EXPORT JSCExecutorFactory : public JSExecutorFactory {
|
2016-05-03 19:29:58 -07:00
|
|
|
public:
|
2018-05-21 11:42:09 -07:00
|
|
|
JSCExecutorFactory(const folly::dynamic& jscConfig) :
|
|
|
|
m_jscConfig(jscConfig) {}
|
2017-04-25 05:29:45 -07:00
|
|
|
std::unique_ptr<JSExecutor> createJSExecutor(
|
2016-05-18 12:46:01 -07:00
|
|
|
std::shared_ptr<ExecutorDelegate> delegate,
|
|
|
|
std::shared_ptr<MessageQueueThread> jsQueue) override;
|
2016-05-03 19:29:58 -07:00
|
|
|
private:
|
2016-05-18 12:46:01 -07:00
|
|
|
std::string m_cacheDir;
|
2016-05-03 19:29:58 -07:00
|
|
|
folly::dynamic m_jscConfig;
|
|
|
|
};
|
|
|
|
|
2017-08-07 06:41:15 -07: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 09:25:06 -08:00
|
|
|
|
2017-09-18 18:35:44 -07:00
|
|
|
class RN_EXPORT JSCExecutor : public JSExecutor, public PrivateDataBase {
|
2016-05-03 19:29:58 -07:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Must be invoked from thread this Executor will run on.
|
|
|
|
*/
|
2016-05-18 12:46:01 -07:00
|
|
|
explicit JSCExecutor(std::shared_ptr<ExecutorDelegate> delegate,
|
|
|
|
std::shared_ptr<MessageQueueThread> messageQueueThread,
|
2018-05-21 11:42:09 -07:00
|
|
|
const folly::dynamic& jscConfig) throw(JSException);
|
2016-05-03 19:29:58 -07:00
|
|
|
~JSCExecutor() override;
|
|
|
|
|
|
|
|
virtual void loadApplicationScript(
|
2016-05-13 17:15:06 -07:00
|
|
|
std::unique_ptr<const JSBigString> script,
|
2017-01-10 07:04:53 -08:00
|
|
|
std::string sourceURL) override;
|
2016-11-18 05:01:09 -08:00
|
|
|
|
2017-09-22 09:56:50 -07:00
|
|
|
virtual void setBundleRegistry(std::unique_ptr<RAMBundleRegistry> bundleRegistry) override;
|
2017-11-09 11:55:37 -08:00
|
|
|
virtual void registerBundle(uint32_t bundleId, const std::string& bundlePath) override;
|
2016-11-18 05:01:09 -08:00
|
|
|
|
2016-05-03 19:29:58 -07:00
|
|
|
virtual void callFunction(
|
|
|
|
const std::string& moduleId,
|
|
|
|
const std::string& methodId,
|
2016-07-21 17:32:42 -07:00
|
|
|
const folly::dynamic& arguments) override;
|
2016-11-18 05:01:09 -08:00
|
|
|
|
2016-05-03 19:29:58 -07:00
|
|
|
virtual void invokeCallback(
|
|
|
|
const double callbackId,
|
2016-07-21 17:32:42 -07:00
|
|
|
const folly::dynamic& arguments) override;
|
2016-11-18 05:01:09 -08:00
|
|
|
|
2016-05-03 19:29:58 -07:00
|
|
|
virtual void setGlobalVariable(
|
2016-05-13 17:15:06 -07:00
|
|
|
std::string propName,
|
2016-07-21 17:32:42 -07:00
|
|
|
std::unique_ptr<const JSBigString> jsonValue) override;
|
2016-11-18 05:01:09 -08:00
|
|
|
|
2017-09-05 14:47:06 -07:00
|
|
|
virtual std::string getDescription() override;
|
|
|
|
|
2016-05-03 19:29:58 -07:00
|
|
|
virtual void* getJavaScriptContext() override;
|
2016-11-18 05:01:09 -08:00
|
|
|
|
2017-12-18 13:19:35 -08:00
|
|
|
virtual bool isInspectable() override;
|
|
|
|
|
2017-06-26 05:48:40 -07:00
|
|
|
virtual void handleMemoryPressure(int pressureLevel) override;
|
2016-11-18 05:01:09 -08:00
|
|
|
|
2016-05-03 19:29:58 -07:00
|
|
|
virtual void destroy() override;
|
2016-11-18 05:01:09 -08:00
|
|
|
|
2016-10-14 11:29:47 -07:00
|
|
|
void setContextName(const std::string& name);
|
2016-05-03 19:29:58 -07:00
|
|
|
|
|
|
|
private:
|
|
|
|
JSGlobalContextRef m_context;
|
2016-05-18 12:46:01 -07:00
|
|
|
std::shared_ptr<ExecutorDelegate> m_delegate;
|
2016-05-03 19:29:58 -07:00
|
|
|
std::shared_ptr<bool> m_isDestroyed = std::shared_ptr<bool>(new bool(false));
|
|
|
|
std::shared_ptr<MessageQueueThread> m_messageQueueThread;
|
2017-09-21 08:34:44 -07:00
|
|
|
std::unique_ptr<RAMBundleRegistry> m_bundleRegistry;
|
2016-10-11 07:19:31 -07:00
|
|
|
JSCNativeModules m_nativeModules;
|
2016-05-03 19:29:58 -07:00
|
|
|
folly::dynamic m_jscConfig;
|
2017-03-20 13:03:04 -07:00
|
|
|
std::once_flag m_bindFlag;
|
2016-05-03 19:29:58 -07:00
|
|
|
|
2016-06-21 10:08:31 -07:00
|
|
|
folly::Optional<Object> m_invokeCallbackAndReturnFlushedQueueJS;
|
|
|
|
folly::Optional<Object> m_callFunctionReturnFlushedQueueJS;
|
|
|
|
folly::Optional<Object> m_flushedQueueJS;
|
2016-09-26 16:01:43 -07:00
|
|
|
folly::Optional<Object> m_callFunctionReturnResultAndFlushedQueueJS;
|
2016-06-21 10:08:31 -07:00
|
|
|
|
2016-06-16 16:00:22 -07:00
|
|
|
void initOnJSVMThread() throw(JSException);
|
2017-12-15 09:10:47 -08:00
|
|
|
static bool isNetworkInspected(const std::string &owner, const std::string &app, const std::string &device);
|
2016-05-03 19:29:58 -07:00
|
|
|
void terminateOnJSVMThread();
|
2016-06-21 10:08:31 -07:00
|
|
|
void bindBridge() throw(JSException);
|
2016-09-01 14:59:25 -07:00
|
|
|
void callNativeModules(Value&&);
|
2016-07-21 17:32:42 -07:00
|
|
|
void flush();
|
2016-09-19 04:43:09 -07:00
|
|
|
void flushQueueImmediate(Value&&);
|
2017-09-21 08:34:44 -07:00
|
|
|
void loadModule(uint32_t bundleId, uint32_t moduleId);
|
2016-05-03 19:29:58 -07:00
|
|
|
|
2017-05-15 09:12:24 -07:00
|
|
|
String adoptString(std::unique_ptr<const JSBigString>);
|
|
|
|
|
2016-11-22 06:05:36 -08:00
|
|
|
template<JSValueRef (JSCExecutor::*method)(size_t, const JSValueRef[])>
|
2016-05-12 14:32:37 -07:00
|
|
|
void installNativeHook(const char* name);
|
2018-02-07 07:29:49 -08:00
|
|
|
|
2016-10-11 07:19:31 -07:00
|
|
|
JSValueRef getNativeModule(JSObjectRef object, JSStringRef propertyName);
|
2016-05-12 14:32:37 -07:00
|
|
|
|
2016-05-12 14:32:42 -07:00
|
|
|
JSValueRef nativeRequire(
|
2016-05-03 19:29:58 -07:00
|
|
|
size_t argumentCount,
|
2016-05-12 14:32:42 -07:00
|
|
|
const JSValueRef arguments[]);
|
|
|
|
JSValueRef nativeFlushQueueImmediate(
|
2016-05-03 19:29:58 -07:00
|
|
|
size_t argumentCount,
|
2016-05-12 14:32:42 -07:00
|
|
|
const JSValueRef arguments[]);
|
|
|
|
JSValueRef nativeCallSyncHook(
|
2016-05-03 19:29:58 -07:00
|
|
|
size_t argumentCount,
|
2016-05-12 14:32:42 -07:00
|
|
|
const JSValueRef arguments[]);
|
2016-05-03 19:29:58 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
} }
|