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

相关推荐
优雅的潮叭3 小时前
c++ 学习笔记之 chrono库
c++·笔记·学习
星火开发设计4 小时前
C++ 数组:一维数组的定义、遍历与常见操作
java·开发语言·数据结构·c++·学习·数组·知识
月挽清风4 小时前
代码随想录第七天:
数据结构·c++·算法
点云SLAM5 小时前
C++内存泄漏检测之Windows 专用工具(CRT Debug、Dr.Memory)和Linux 专业工具(ASan 、heaptrack)
linux·c++·windows·asan·dr.memory·c++内存泄漏检测·c++内存管理
浅念-6 小时前
C语言小知识——指针(3)
c语言·开发语言·c++·经验分享·笔记·学习·算法
无限进步_7 小时前
【C++】大数相加算法详解:从字符串加法到内存布局的思考
开发语言·c++·windows·git·算法·github·visual studio
C+-C资深大佬8 小时前
C++ 数据类型转换是如何实现的?
开发语言·c++·算法
oioihoii9 小时前
回归测试:软件演进中的质量守护神与实践全指南
c++
十五年专注C++开发10 小时前
CMake基础: 在release模式下生成调试信息的方法
linux·c++·windows·cmake·跨平台构建
点云SLAM10 小时前
C++(C++17/20)最佳工厂写法和SLAM应用综合示例
开发语言·c++·设计模式·c++实战·注册工厂模式·c++大工程系统