React Native 源码分析—— TurboModules JSI通信机制
    		       		warning:
    		            这篇文章距离上次修改已过445天,其中的内容可能已经有所变动。
    		        
        		                
                
#include <jsi/jsi.h>
#include <react/jsi/JSIStoreValueUser.h>
#include <react/jsi/JSIExecutor.h>
#include <react/jsi/JSINativeModules.h>
#include <react/jni/JMessageQueueThread.h>
#include <react/jni/JavaScriptExecutorHolder.h>
#include <react/jni/JsiApi.h>
#include <react/jni/NativeModuleRegistry.h>
#include <react/jni/JReactMarker.h>
#include <folly/json.h>
#include <memory>
 
using namespace facebook;
using namespace react;
 
// 假设这是一个已经初始化的JSIExecutor
std::shared_ptr<jsi::JSIRuntime> runtime;
 
// 假设这是一个已经初始化的NativeModuleRegistry
std::shared_ptr<NativeModuleRegistry> nativeModuleRegistry;
 
// 假设这是一个已经初始化的JavaScriptExecutorHolder
std::shared_ptr<JavaScriptExecutorHolder> jseh;
 
// 假设这是一个已经初始化的JMessageQueueThread
JMessageQueueThread* jmt;
 
// 创建TurboModule
void installTurboModules(jni::JNIEnv& env, jni::local_ref<jhybriddata> jniBatchConfig) {
  // 从JNI获取JavaScriptExecutorHolder和NativeModuleRegistry
  jni::local_ref<jni::JObject> executorFactory =
    jni::findClassLocal("com/facebook/react/turbomodule/core/TurboModuleManager")
    ->getMethod<jni::JObject(JNIEnv&, jni::local_ref<jhybriddata>)>("getExecutorFactory")(env, jniBatchConfig);
  jni::local_ref<JExecutorToken> executorToken =
    jni::findClassLocal("com/facebook/react/turbomodule/core/TurboModuleManager")
    ->getStaticMethod<jni::local_ref<JExecutorToken>(JNIEnv&, jni::local_ref<jhybriddata>)>("getExecutorToken")(env, jniBatchConfig);
 
  // 获取JavaScriptExecutorHolder
  jni::local_ref<JExecutorToken::javaobject> executorTokenObj = executorToken->cthis();
  jni::local_ref<JavaScriptExecutorHolder::javaobject> executorHolderObj =
    jni::dynamic_cast_local_ref<JavaScriptExecutorHolder::javaobject>(executorFactory->call(JExecutorToken::javaClassLocal()->getMethod<jni::local_ref<jni::JObject>()>("getExecutorFactory")));
  jseh = std::make_shared<JavaScriptExecutorHolder>(executorHolderObj);
 
  // 获取NativeModuleRegistry
  nativeModuleRegistry = std::make_shared<NativeModuleRegistry>(jniBatchConfig, executorTokenObj);
 
  // 创建JSIExecutor
  runtime = jsi::JSIExecutor::create(std::make_shared<JSIExecutor           
评论已关闭