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

相关推荐
砖厂小工3 小时前
Now In Android 精讲 10 - AGENTS.md:写给 AI Agent 的项目说明书
android
Ehtan_Zheng4 小时前
Jetpack Compose 动画转换编排的艺术
android
Ehtan_Zheng4 小时前
Jetpack Compose 动画入门:轻松掌握状态驱动的动画转换
android
Ehtan_Zheng4 小时前
Jetpack Compose 布局与可见性动画
android
_李小白5 小时前
【android opencv学习笔记】Day 12: HSV 色彩空间
android·opencv·学习
千里马学框架5 小时前
手机大厂Activity嵌套模式及三分屏SplitScreen功能调研报告-独家干货
android·智能手机·分屏·aaos·安卓framework开发·车机·三分屏
Mr.QingBin5 小时前
SystemUI插件开发指南
android
芋只因5 小时前
MySQL 分库分表与 MyCat 的使用
android
Ehtan_Zheng5 小时前
Jetpack Compose 与 RecyclerView 混合布局的性能债
android