ReactNative 源码分析4——ReactActivity之加载JSBundle

接着ReactInstanceManager.createReactContext方法继续讲

scss 复制代码
////ReactInstanceManager.createReactContext
private ReactApplicationContext createReactContext(
    JavaScriptExecutor jsExecutor, JSBundleLoader jsBundleLoader) {
    ...
    catalystInstance.runJSBundle();
    ...
}

最终会调用到如下 2 个方法进入C++层

arduino 复制代码
private native void jniLoadScriptFromAssets(
    AssetManager assetManager, String assetURL, boolean loadSynchronously);

private native void jniLoadScriptFromFile(
    String fileName, String sourceURL, boolean loadSynchronously);

jniLoadScriptFromFile是调用到instance_->loadScriptFromString,然后调用loadBundle

c 复制代码
void Instance::loadBundle(
    std::unique_ptr<RAMBundleRegistry> bundleRegistry,
    std::unique_ptr<const JSBigString> string,
    std::string sourceURL) {
  callback_->incrementPendingJSCalls();
  SystraceSection s( "Instance::loadBundle" , "sourceURL" , sourceURL);
  nativeToJsBridge_->loadBundle(
      std::move(bundleRegistry), std::move(string), std::move(sourceURL));
}
  • callback_是Java层的InstanceCallback实例,这里是通知Java层
  • nativeToJsBridge_是NativeToJsBridge对象
arduino 复制代码
void NativeToJsBridge::loadBundle(
    std::unique_ptr<RAMBundleRegistry> bundleRegistry,
    std::unique_ptr<const JSBigString> startupScript,
    std::string startupScriptSourceURL) {
  runOnExecutorQueue(
      [this,
       bundleRegistryWrap = makeMoveWrapper(std::move(bundleRegistry)),
       startupScript = makeMoveWrapper(std::move(startupScript)),
       startupScriptSourceURL =
           std::move(startupScriptSourceURL)](JSExecutor* executor) mutable {
        auto bundleRegistry = bundleRegistryWrap.move();
        if (bundleRegistry) {
          executor->setBundleRegistry(std::move(bundleRegistry));
        }
        try {
          executor->loadBundle(
              std::move(*startupScript), std::move(startupScriptSourceURL));
        } catch (...) {
          m_applicationScriptHasFailure = true;
          throw;
        }
      });
}
  • runOnExecutorQueue在JS线程中执行
  • executor是HermesExecutor对象
c 复制代码
void JSIExecutor::loadBundle(
    std::unique_ptr<const JSBigString> script,
    std::string sourceURL) {
  ...
  runtime_->evaluateJavaScript(
      std::make_unique<BigStringBuffer>(std::move(script)), sourceURL);
  ...
}
  • runtime_:DecoratedRuntime对象,它里面包装了HermesRuntime运行时

到这里其实就明确了,调用Hermes引擎去加载JSBundle

相关推荐
李斯维10 分钟前
Jetpack 可观察数据容器 LiveData 的入门与基础使用
android·android jetpack
问心无愧05131 小时前
ctf show web入门261
android·前端·笔记
alexhilton1 小时前
车载系统中的可扩展UI:从UI嵌入到系统窗口编排
android·kotlin·android jetpack
Cloud_Shy6181 小时前
解读《Effective Python 3rd Edition》:从练气到老魔(第一章 Item 4 - 6)
android·数据库·论文阅读·python
therese_100861 小时前
安卓面试题
android
码云骑士2 小时前
Android Launcher启动过程
android
Java面试题总结2 小时前
MySQL EXISTS 详解:存在性判断、NOT EXISTS 与实战示例
android·数据库·mysql
_李小白3 小时前
【android opencv学习笔记】Day 30: 滤波算法之拉普拉斯算子
android·opencv·学习
NiceCloud喜云11 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
wordbaby11 小时前
React Native + RNOH:跨页面数据回传的最佳实践与避坑指南
前端·react native