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

相关推荐
2401_8920709819 小时前
【Linux C++ 日志系统实战】LogFile 日志文件管理核心:滚动策略、线程安全与方法全解析
linux·c++·日志系统·日志滚动
yuzhuanhei20 小时前
Visual Studio 配置C++opencv
c++·学习·visual studio
不爱吃炸鸡柳20 小时前
C++ STL list 超详细解析:从接口使用到模拟实现
开发语言·c++·list
十五年专注C++开发20 小时前
RTTR: 一款MIT 协议开源的 C++ 运行时反射库
开发语言·c++·反射
‎ദ്ദിᵔ.˛.ᵔ₎20 小时前
STL 栈 队列
开发语言·c++
2401_8920709821 小时前
【Linux C++ 日志系统实战】高性能文件写入 AppendFile 核心方法解析
linux·c++·日志系统·文件写对象
郭涤生21 小时前
STL vector 扩容机制与自定义内存分配器设计分析
c++·算法
༾冬瓜大侠༿21 小时前
vector
c语言·开发语言·数据结构·c++·算法
cccyi721 小时前
【C++ 脚手架】etcd 的介绍与使用
c++·服务发现·etcd·服务注册
liu****21 小时前
第16届省赛蓝桥杯大赛C/C++大学B组(京津冀)
开发语言·数据结构·c++·算法·蓝桥杯