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

相关推荐
rainbow688915 小时前
EffectiveC++入门:四大习惯提升代码质量
c++
秋邱15 小时前
用 Python 写出 C++ 的性能?用CANN中PyPTO 算子开发硬核上手指南
开发语言·c++·python
我在人间贩卖青春16 小时前
C++之析构函数
c++·析构函数
我在人间贩卖青春16 小时前
C++之数据类型的扩展
c++·字符串·数据类型
wangjialelele17 小时前
平衡二叉搜索树:AVL树和红黑树
java·c语言·开发语言·数据结构·c++·算法·深度优先
苏宸啊17 小时前
C++栈和队列
c++
森G17 小时前
七、04ledc-sdk--------makefile有变化
linux·c语言·arm开发·c++·ubuntu
橘颂TA17 小时前
【测试】高效浏览器操作:基础功能与优化设置大全
c++·功能测试·职场和发展·测试·web测试
一只小小的芙厨18 小时前
寒假集训笔记·以点为对象的树形DP
c++·算法
艾莉丝努力练剑18 小时前
hixl vs NCCL:昇腾生态通信库的独特优势分析
运维·c++·人工智能·cann