Rect Native bridging 源码分析--AString.h

此代码来自:packages/react-native/ReactCommon/react/bridging/AString.h

cpp 复制代码
/*
 * 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 <react/bridging/Base.h>

#include <string>
#include <string_view>

namespace facebook::react {

template <>
struct Bridging<std::string> {
  static std::string fromJs(jsi::Runtime &rt, const jsi::String &value)
  {
    return value.utf8(rt);
  }

  static jsi::String toJs(jsi::Runtime &rt, const std::string &value)
  {
    return jsi::String::createFromUtf8(rt, value);
  }
};

template <>
struct Bridging<std::string_view> {
  static jsi::String toJs(jsi::Runtime &rt, std::string_view value)
  {
    return jsi::String::createFromUtf8(rt, reinterpret_cast<const uint8_t *>(value.data()), value.length());
  }
};

template <>
struct Bridging<const char *> : Bridging<std::string_view> {};

template <size_t N>
struct Bridging<char[N]> : Bridging<std::string_view> {};

} // namespace facebook::react

核心功能分析

1 std::string 的桥接

cpp 复制代码
template <>
struct Bridging<std::string> {
  static std::string fromJs(jsi::Runtime &rt, const jsi::String &value)
  {
    return valueutf8(rt);
  }

  static jsi::String toJs(jsi::Runtime &rt, const std::string &value)
  {
    return jsi::String::createFromUtf8(rt, value);
  }
};

功能

  • fromJs(): 将 JavaScript 字符串转换为 C++ 的std::string
  • toJs(): 将 C++ 的std::string转换为 JavaScript 字符串

2 std::string_view 的桥接

cpp 复制代码
template <>
struct Bridging<std::string_view> {
  static jsi::String toJs(jsi::Runtime &rt, std::string_view value)
  {
    return jsi::String::createFromUtf8(rt, reinterpret_cast<const uint8_t *>(value.data()), value.length());
  }
};

功能

  • 只提供了toJs()方法,用于将std::string_view转换为 JavaScript 字符串
  • 使用了reinterpret_cast进行类型转换

3 其他字符串类型的桥接

cpp 复制代码
template <>
struct Bridging<const char *> : Bridging<std::string_view> {};

template <size_t N>
struct Bridging<char[N]> : Bridging<std::string_view> {};

功能

  • 这两个是继承关系,让const char *和固定大小的字符数组都复用std::string_view的桥接逻辑

技术要点

1 模板特化:使用 C++ 模板特化来为不同的字符串类型提供专门的转换逻辑

2 运行时引用 :所有方法都接收jsi::Runtime &rt参数,这是 JavaScript 运行时的引用

3 UTF-8 编码:所有字符串转换都使用 UTF-8 编码

4 类型安全:通过模板特化保证类型安全的转换

5 性能优化 :对于std::string_view只提供单向转换,避免不必要的内存分配

使用场景

这段代码主要用于:

  • React Native 原生模块开发
  • C++ 和 JavaScript 之间的数据传递
  • 保持字符串数据在不同语言环境下的一致性

这是 React Native 桥接系统的基础组件,确保了字符串类型在 C++ 和 JavaScript 之间的无缝转换。

相关推荐
闻缺陷则喜何志丹2 小时前
【二分查找】P10091 [ROIR 2022 Day 2] 分数排序|普及+
c++·算法·二分查找
阿豪只会阿巴3 小时前
【多喝热水系列】从零开始的ROS2之旅——Day4
c++·笔记·python·ros2
郭涤生4 小时前
fmtlib/fmt仓库熟悉
c++
Stanford_11065 小时前
【2026新年启程】学习之路,探索之路,技术之路,成长之路……都与你同行!!!
前端·c++·学习·微信小程序·排序算法·微信开放平台
郝学胜-神的一滴5 小时前
Linux线程属性设置分离技术详解
linux·服务器·数据结构·c++·程序人生·算法
w-w0w-w5 小时前
C++构造函数初始化列表全解析
c++
梵尔纳多5 小时前
初识 OpenGL
c++·图形渲染
王老师青少年编程6 小时前
2025年12月GESP(C++二级): 黄金格
c++·算法·gesp·csp·信奥赛·二级·黄金格
一起搞IT吧6 小时前
相机Camera日志实例分析之十二:相机Camx【萌拍后置zoom拍照】单帧流程日志详解
android·c++·数码相机·智能手机