跳至主内容
版本:0.80
非官方测试版翻译

本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →

高级:自定义 C++ 类型

备注

本指南假定您已熟悉纯 C++ Turbo 原生模块指南内容,本节内容将在此基础上进行扩展。

C++ Turbo 原生模块支持为大多数 std:: 标准类型提供桥接功能。您可以直接在模块中使用这些类型,无需编写额外代码。

如需在应用或库中添加对新自定义类型的支持,您需要提供相应的 bridging 头文件。

添加新自定义类型:Int64

由于 JavaScript 不支持大于 2^53 的数字,C++ Turbo 原生模块目前暂不支持 int64_t 数值类型。要表示大于 2^53 的数字,我们可以在 JS 中使用 string 类型,并在 C++ 中自动转换为 int64_t

1. 创建桥接头文件

支持新自定义类型的第一步是定义桥接头文件,负责将类型 JS 表示形式转换为 C++ 表示形式,以及 C++ 表示形式转换为 JS 表示形式。

  1. shared 文件夹中新建名为 Int64.h 的文件

  2. 在该文件中添加以下代码:

Int64.h
#pragma once

#include <react/bridging/Bridging.h>

namespace facebook::react {

template <>
struct Bridging<int64_t> {
// Converts from the JS representation to the C++ representation
static int64_t fromJs(jsi::Runtime &rt, const jsi::String &value) {
try {
size_t pos;
auto str = value.utf8(rt);
auto num = std::stoll(str, &pos);
if (pos != str.size()) {
throw std::invalid_argument("Invalid number"); // don't support alphanumeric strings
}
return num;
} catch (const std::logic_error &e) {
throw jsi::JSError(rt, e.what());
}
}

// Converts from the C++ representation to the JS representation
static jsi::String toJs(jsi::Runtime &rt, int64_t value) {
return bridging::toJs(rt, std::to_string(value));
}
};

}

自定义桥接头文件的关键组件包括:

  • 为自定义类型显式特化的 Bridging 结构体(本例中模板特化为 int64_t 类型)

  • 用于将 JS 表示形式转换为 C++ 表示形式的 fromJs 函数

  • 用于将 C++ 表示形式转换为 JS 表示形式的 toJs 函数

备注

在 iOS 平台上,请记得将 Int64.h 文件添加到 Xcode 工程中。

2. 修改 JS 规范

现在我们可以修改 JS 规范,添加使用新类型的方法。通常可以使用 Flow 或 TypeScript 编写规范。

  1. 打开 specs/NativeSampleTurbomodule

  2. 按如下方式修改规范:

NativeSampleModule.ts
import {TurboModule, TurboModuleRegistry} from 'react-native';

export interface Spec extends TurboModule {
readonly reverseString: (input: string) => string;
+ readonly cubicRoot: (input: string) => number;
}

export default TurboModuleRegistry.getEnforcing<Spec>(
'NativeSampleModule',
);

在此文件中,我们定义了需要在 C++ 中实现的函数。

3. 实现原生代码

现在我们需要实现在 JS 规范中声明的函数。

  1. 打开 specs/NativeSampleModule.h 文件并应用以下更改:
NativeSampleModule.h
#pragma once

#include <AppSpecsJSI.h>
#include <memory>
#include <string>

+ #include "Int64.h"

namespace facebook::react {

class NativeSampleModule : public NativeSampleModuleCxxSpec<NativeSampleModule> {
public:
NativeSampleModule(std::shared_ptr<CallInvoker> jsInvoker);

std::string reverseString(jsi::Runtime& rt, std::string input);
+ int32_t cubicRoot(jsi::Runtime& rt, int64_t input);
};

} // namespace facebook::react

  1. 打开 specs/NativeSampleModule.cpp 文件并实现新函数:
NativeSampleModule.cpp
#include "NativeSampleModule.h"
+ #include <cmath>

namespace facebook::react {

NativeSampleModule::NativeSampleModule(std::shared_ptr<CallInvoker> jsInvoker)
: NativeSampleModuleCxxSpec(std::move(jsInvoker)) {}

std::string NativeSampleModule::reverseString(jsi::Runtime& rt, std::string input) {
return std::string(input.rbegin(), input.rend());
}

+int32_t NativeSampleModule::cubicRoot(jsi::Runtime& rt, int64_t input) {
+ return std::cbrt(input);
+}

} // namespace facebook::react

该实现导入了 <cmath> C++ 库以执行数学运算,然后使用 <cmath> 模块中的 cbrt 原语实现了 cubicRoot 函数。

4. 在应用中测试代码

现在可以在应用中测试代码。

首先需要更新 App.tsx 文件以使用 TurboModule 的新方法,然后构建 Android 和 iOS 应用。

  1. 打开 App.tsx 文件并应用以下更改:
App.tsx
// ...
+ const [cubicSource, setCubicSource] = React.useState('')
+ const [cubicRoot, setCubicRoot] = React.useState(0)
return (
<SafeAreaView style={styles.container}>
<View>
<Text style={styles.title}>
Welcome to C++ Turbo Native Module Example
</Text>
<Text>Write down here the text you want to revert</Text>
<TextInput
style={styles.textInput}
placeholder="Write your text here"
onChangeText={setValue}
value={value}
/>
<Button title="Reverse" onPress={onPress} />
<Text>Reversed text: {reversedValue}</Text>
+ <Text>For which number do you want to compute the Cubic Root?</Text>
+ <TextInput
+ style={styles.textInput}
+ placeholder="Write your text here"
+ onChangeText={setCubicSource}
+ value={cubicSource}
+ />
+ <Button title="Get Cubic Root" onPress={() => setCubicRoot(SampleTurboModule.cubicRoot(cubicSource))} />
+ <Text>The cubic root is: {cubicRoot}</Text>
</View>
</SafeAreaView>
);
}
//...
  1. 在项目根目录运行 yarn android 测试 Android 应用

  2. 在项目根目录运行 yarn ios 测试 iOS 应用

添加新的结构化自定义类型:Address

上述方法可以推广到任何类型。对于结构化类型,React Native 提供了一些辅助函数,能更轻松地在 JavaScript 和 C++ 之间进行桥接。

假设我们需要桥接一个包含以下属性的自定义 Address 类型:

ts
interface Address {
street: string;
num: number;
isInUS: boolean;
}

1. 在规范中定义类型

首先在 JS 规范中定义新的自定义类型,这样 Codegen 就能自动生成所有支持代码,无需手动编写。

  1. 打开 specs/NativeSampleModule 文件并添加以下修改:
NativeSampleModule (Add Address type and validateAddress function)
import {TurboModule, TurboModuleRegistry} from 'react-native';

+export type Address = {
+ street: string,
+ num: number,
+ isInUS: boolean,
+};

export interface Spec extends TurboModule {
readonly reverseString: (input: string) => string;
+ readonly validateAddress: (input: Address) => boolean;
}

export default TurboModuleRegistry.getEnforcing<Spec>(
'NativeSampleModule',
);

这段代码定义了新的 Address 类型,并为 Turbo Native Module 添加了新的 validateAddress 函数。注意 validateFunction 需要接收 Address 对象作为参数。

也可以创建返回自定义类型的函数。

2. 定义桥接代码

根据规范中定义的 Address 类型,Codegen 会生成两个辅助类型:NativeSampleModuleAddressNativeSampleModuleAddressBridging

第一个类型是 Address 的定义。第二个类型包含所有在 JS 和 C++ 之间桥接自定义类型的基础设施。我们只需额外定义继承 NativeSampleModuleAddressBridgingBridging 结构体。

  1. 打开 shared/NativeSampleModule.h 文件

  2. 在文件中添加以下代码:

NativeSampleModule.h (Bridging the Address type)
#include "Int64.h"
#include <memory>
#include <string>

namespace facebook::react {
+ using Address = NativeSampleModuleAddress<std::string, int32_t, bool>;

+ template <>
+ struct Bridging<Address>
+ : NativeSampleModuleAddressBridging<Address> {};
// ...
}

这段代码为泛型 NativeSampleModuleAddress 定义了 Address 类型别名。泛型参数的顺序很重要:第一个模板参数对应结构体的第一个数据类型,第二个对应第二个,依此类推。

随后通过继承 Codegen 生成的 NativeSampleModuleAddressBridging,添加了新 Address 类型的 Bridging 特化。

备注

类型命名遵循以下约定:

  • 名称的第一部分始终是模块类型(本例中的 NativeSampleModule
  • 名称的第二部分始终是规范中定义的 JS 类型名称(本例中的 Address

3. 实现原生代码

现在需要在 C++ 中实现 validateAddress 函数。先在 .h 头文件中添加函数声明,然后在 .cpp 实现文件中完成具体逻辑。

  1. 打开 shared/NativeSampleModule.h 文件并添加函数定义
NativeSampleModule.h (validateAddress function prototype)
  std::string reverseString(jsi::Runtime& rt, std::string input);

+ bool validateAddress(jsi::Runtime &rt, jsi::Object input);
};

} // namespace facebook::react
  1. 打开 shared/NativeSampleModule.cpp 文件并添加函数实现
NativeSampleModule.cpp (validateAddress implementation)
bool NativeSampleModule::validateAddress(jsi::Runtime &rt, jsi::Object input) {
std::string street = input.getProperty(rt, "street").asString(rt).utf8(rt);
int32_t number = input.getProperty(rt, "num").asNumber();

return !street.empty() && number > 0;
}

在实现中,表示 Address 的对象是 jsi::Object。要从中提取值,需使用 JSI 提供的访问器:

  • getProperty() 通过属性名从对象中检索值

  • asString() 将属性转换为 jsi::String

  • utf8()jsi::String 转换为 std::string

  • asNumber() 将属性转换为 double

手动解析对象后,即可实现所需逻辑。

备注

要深入了解 JSI 的工作原理,请观看 App.JS 2024 的这个精彩演讲

4. 在应用中测试代码

要在应用中测试代码,需要修改 App.tsx 文件。

  1. 打开 App.tsx 文件,移除 App() 函数内的所有内容。

  2. App() 函数体替换为以下代码:

App.tsx (App function body replacement)
const [street, setStreet] = React.useState('');
const [num, setNum] = React.useState('');
const [isValidAddress, setIsValidAddress] = React.useState<
boolean | null
>(null);

const onPress = () => {
let houseNum = parseInt(num, 10);
if (isNaN(houseNum)) {
houseNum = -1;
}
const address = {
street,
num: houseNum,
isInUS: false,
};
const result = SampleTurboModule.validateAddress(address);
setIsValidAddress(result);
};

return (
<SafeAreaView style={styles.container}>
<View>
<Text style={styles.title}>
Welcome to C Turbo Native Module Example
</Text>
<Text>Address:</Text>
<TextInput
style={styles.textInput}
placeholder="Write your address here"
onChangeText={setStreet}
value={street}
/>
<Text>Number:</Text>
<TextInput
style={styles.textInput}
placeholder="Write your address here"
onChangeText={setNum}
value={num}
/>
<Button title="Validate" onPress={onPress} />
{isValidAddress != null && (
<Text>
Your address is {isValidAddress ? 'valid' : 'not valid'}
</Text>
)}
</View>
</SafeAreaView>
);

恭喜!🎉

你已成功将第一个类型从 JS 桥接到 C++。