高级:自定义 C++ 类型
本指南假设您熟悉 纯 C++ Turbo 原生模块 指南。 本指南将以此为基础构建。
C++ Turbo 原生模块支持大多数 std::
标准类型的 桥接功能。您可以在模块中使用大多数这些类型,而无需任何额外的代码。
如果您想在您的应用或库中添加对新的和自定义类型的支持,您需要提供必要的 bridging
头文件。
添加新的自定义类型:Int64
C++ Turbo 原生模块尚不支持 int64_t
数字 - 因为 JavaScript 不支持大于 2^53 的数字。为了表示大于 2^53 的数字,我们可以在 JS 中使用 string
类型,并在 C++ 中自动将其转换为 int64_t
。
1. 创建桥接头文件
支持新自定义类型的第一步是定义桥接头文件,该文件负责处理从 JS 表示形式到 C++ 表示形式以及从 C++ 表示形式到 JS 表示形式的类型转换。
- 在
shared
文件夹中,添加一个名为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
类型。 - 一个
fromJs
函数,用于从 JS 表示形式转换为 C++ 表示形式 - 一个
toJs
函数,用于从 C++ 表示形式转换为 JS 表示形式
在 iOS 上,记得将 Int64.h
文件添加到 Xcode 项目。
2. 修改 JS 规范
现在,我们可以修改 JS 规范以添加使用新类型的方法。 与往常一样,我们可以为我们的规范使用 Flow 或 TypeScript。
- 打开
specs/NativeSampleTurbomodule
- 按如下方式修改规范
- TypeScript
- Flow
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',
);
// @flow
import type {TurboModule} from 'react-native';
import { TurboModuleRegistry } from "react-native";
export interface Spec extends TurboModule {
+reverseString: (input: string) => string;
+ +cubicRoot: (input: string) => number;
}
export default (TurboModuleRegistry.getEnforcing<Spec>(
"NativeSampleModule"
): Spec);
在这些文件中,我们定义了需要在 C++ 中实现的函数。
3. 实现原生代码
现在,我们需要实现我们在 JS 规范中声明的函数。
- 打开
specs/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
- 打开
specs/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 中构建我们的应用。
- 打开
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>
);
}
//...
- 要在 Android 上测试应用,请从项目的根文件夹运行
yarn android
。 - 要在 iOS 上测试应用,请从项目的根文件夹运行
yarn ios
。
添加新的结构化自定义类型:Address
上面的方法可以推广到任何类型的类型。 对于结构化类型,React Native 提供了一些辅助函数,可以更轻松地将它们从 JS 桥接到 C++,反之亦然。
假设我们要桥接具有以下属性的自定义 Address
类型
interface Address {
street: string;
num: number;
isInUS: boolean;
}
1. 在规范中定义类型
对于第一步,让我们在 JS 规范中定义新的自定义类型,以便 Codegen 可以输出所有支持代码。 这样,我们就不必手动编写代码。
- 打开
specs/NativeSampleModule
文件并添加以下更改。
- TypeScript
- Flow
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',
);
// @flow
import type {TurboModule} from 'react-native';
import { TurboModuleRegistry } from "react-native";
+export type Address = {
+ street: string,
+ num: number,
+ isInUS: boolean,
+};
export interface Spec extends TurboModule {
+reverseString: (input: string) => string;
+ +validateAddress: (input: Address) => boolean;
}
export default (TurboModuleRegistry.getEnforcing<Spec>(
"NativeSampleModule"
): Spec);
此代码定义了新的 Address
类型,并为 Turbo 原生模块定义了新的 validateAddress
函数。 请注意,validateFunction
需要一个 Address
对象作为参数。
函数也可以返回自定义类型。
2. 定义桥接代码
根据规范中定义的 Address
类型,Codegen 将生成两个辅助类型:NativeSampleModuleAddress
和 NativeSampleModuleAddressBridging
。
第一种类型是 Address
的定义。 第二种类型包含将自定义类型从 JS 桥接到 C++ 以及反之亦然的所有基础设施。 我们需要添加的唯一额外步骤是定义扩展 NativeSampleModuleAddressBridging
类型的 Bridging
结构。
- 打开
shared/NativeSampleModule.h
文件 - 在文件中添加以下代码
#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
文件中实现它。
- 打开
shared/NativeSampleModule.h
文件并添加函数定义
std::string reverseString(jsi::Runtime& rt, std::string input);
+ bool validateAddress(jsi::Runtime &rt, jsi::Object input);
};
} // namespace facebook::react
- 打开
shared/NativeSampleModule.cpp
文件并添加函数实现
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
文件。
- 打开
App.tsx
文件。 删除App()
函数的内容。 - 将
App()
函数的主体替换为以下代码
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++。