// Copyright 2004-present Facebook. All Rights Reserved. #pragma once #include #include #include #include #ifndef RN_EXPORT #define RN_EXPORT __attribute__((visibility("default"))) #endif namespace folly { struct dynamic; } namespace facebook { namespace react { class JSBigString; class JSExecutorFactory; class JSModulesUnbundle; class MessageQueueThread; class ModuleRegistry; struct InstanceCallback { virtual ~InstanceCallback() {} virtual void onBatchComplete() = 0; virtual void incrementPendingJSCalls() = 0; virtual void decrementPendingJSCalls() = 0; }; class RN_EXPORT Instance { public: ~Instance(); void initializeBridge(std::unique_ptr callback, std::shared_ptr jsef, std::shared_ptr jsQueue, std::shared_ptr moduleRegistry); void setSourceURL(std::string sourceURL); void loadScriptFromString(std::unique_ptr string, std::string sourceURL, bool loadSynchronously); void loadUnbundle(std::unique_ptr unbundle, std::unique_ptr startupScript, std::string startupScriptSourceURL, bool loadSynchronously); bool supportsProfiling(); void setGlobalVariable(std::string propName, std::unique_ptr jsonValue); void *getJavaScriptContext(); void callJSFunction(std::string &&module, std::string &&method, folly::dynamic &¶ms); void callJSCallback(uint64_t callbackId, folly::dynamic &¶ms); // This method is experimental, and may be modified or removed. template Value callFunctionSync(const std::string &module, const std::string &method, T &&args) { CHECK(nativeToJsBridge_); return nativeToJsBridge_->callFunctionSync(module, method, std::forward(args)); } const ModuleRegistry &getModuleRegistry() const; ModuleRegistry &getModuleRegistry(); #ifdef WITH_JSC_MEMORY_PRESSURE void handleMemoryPressure(int pressureLevel); #endif private: void callNativeModules(folly::dynamic &&calls, bool isEndOfBatch); void loadApplication(std::unique_ptr unbundle, std::unique_ptr startupScript, std::string startupScriptSourceURL); void loadApplicationSync(std::unique_ptr unbundle, std::unique_ptr startupScript, std::string startupScriptSourceURL); std::shared_ptr callback_; std::unique_ptr nativeToJsBridge_; std::shared_ptr moduleRegistry_; std::mutex m_syncMutex; std::condition_variable m_syncCV; bool m_syncReady = false; }; } // namespace react } // namespace facebook