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 之间的无缝转换。

相关推荐
仰泳的熊猫5 小时前
题目2570:蓝桥杯2020年第十一届省赛真题-成绩分析
数据结构·c++·算法·蓝桥杯
Thera7779 小时前
C++ 高性能时间轮定时器:从单例设计到 Linux timerfd 深度优化
linux·开发语言·c++
君义_noip10 小时前
信息学奥赛一本通 1952:【10NOIP普及组】三国游戏 | 洛谷 P1199 [NOIP 2010 普及组] 三国游戏
c++·信息学奥赛·csp-s
旖-旎11 小时前
二分查找(x的平方根)(4)
c++·算法·二分查找·力扣·双指针
顶点多余11 小时前
使用C/C++语言链接Mysql详解
数据库·c++·mysql
汉克老师11 小时前
GESP2026年3月认证C++四级( 第二部分判断题(1-10))
c++·指针·函数重载·文件操作·数组·gesp4级·gesp四级
khddvbe12 小时前
C++并发编程中的死锁避免
开发语言·c++·算法
wWYy.12 小时前
STL:list
开发语言·c++
小比特_蓝光13 小时前
vector模拟实现
c++
咱就是说不配啊13 小时前
3.19打卡day33
数据结构·c++·算法