/* * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ #pragma once #include #include #include #include #include #include #include #include #include #include #include namespace facebook::react { class ReactInstance final : private jsinspector_modern::InstanceTargetDelegate { public: using BindingsInstallFunc = std::function; ReactInstance( std::unique_ptr runtime, std::shared_ptr jsMessageQueueThread, std::shared_ptr timerManager, JsErrorHandler::OnJsError onJsError, jsinspector_modern::HostTarget *parentInspectorTarget = nullptr); RuntimeExecutor getUnbufferedRuntimeExecutor() noexcept; RuntimeExecutor getBufferedRuntimeExecutor() noexcept; std::shared_ptr getRuntimeScheduler() noexcept; struct JSRuntimeFlags { bool isProfiling = false; const std::string runtimeDiagnosticFlags = {}; }; void initializeRuntime(JSRuntimeFlags options, BindingsInstallFunc bindingsInstallFunc) noexcept; void loadScript( std::unique_ptr script, const std::string &sourceURL, std::function &&beforeLoad = nullptr, std::function &&afterLoad = nullptr); void registerSegment(uint32_t segmentId, const std::string &segmentPath); void callFunctionOnModule(const std::string &moduleName, const std::string &methodName, folly::dynamic &&args); void handleMemoryPressureJs(int pressureLevel); /** * Unregisters the instance from the inspector. This method must be called * on the main (non-JS) thread. */ void unregisterFromInspector(); void *getJavaScriptContext(); private: std::shared_ptr runtime_; std::shared_ptr jsMessageQueueThread_; std::shared_ptr bufferedRuntimeExecutor_; std::shared_ptr timerManager_; std::unordered_map> callableModules_; std::shared_ptr runtimeScheduler_; std::shared_ptr jsErrorHandler_; jsinspector_modern::InstanceTarget *inspectorTarget_{nullptr}; jsinspector_modern::RuntimeTarget *runtimeInspectorTarget_{nullptr}; jsinspector_modern::HostTarget *parentInspectorTarget_{nullptr}; }; } // namespace facebook::react