Commit 7ffab16
react-native code-gen > C++ TurboModules struct support (#35265)
Summary:
Pull Request resolved: #35265
This adds a templating layer for C++ TurboModules to automatically generate struct templates from TurboModule specs.
You have to define concrete types for those templates to use them in your C++ TurboModule.
E.g. for the JS flow type:
```
export type ObjectStruct = {|
a: number,
b: string,
c?: ?string,
|};
```
code-gen will now generate the following template code:
```
#pragma mark - NativeCxxModuleExampleCxxBaseObjectStruct
template <typename P0, typename P1, typename P2>
struct NativeCxxModuleExampleCxxBaseObjectStruct {
P0 a;
P1 b;
P2 c;
bool operator==(const NativeCxxModuleExampleCxxBaseObjectStruct &other) const {
return a == other.a && b == other.b && c == other.c;
}
};
template <typename P0, typename P1, typename P2>
struct NativeCxxModuleExampleCxxBaseObjectStructBridging {
static NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> fromJs(
jsi::Runtime &rt,
const jsi::Object &value,
const std::shared_ptr<CallInvoker> &jsInvoker) {
NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> result{
bridging::fromJs<P0>(rt, value.getProperty(rt, "a"), jsInvoker),
bridging::fromJs<P1>(rt, value.getProperty(rt, "b"), jsInvoker),
bridging::fromJs<P2>(rt, value.getProperty(rt, "c"), jsInvoker)};
return result;
}
static jsi::Object toJs(
jsi::Runtime &rt,
const NativeCxxModuleExampleCxxBaseObjectStruct<P0, P1, P2> &value) {
auto result = facebook::jsi::Object(rt);
result.setProperty(rt, "a", bridging::toJs(rt, value.a));
result.setProperty(rt, "b", bridging::toJs(rt, value.b));
if (value.c) {
result.setProperty(rt, "c", bridging::toJs(rt, value.c.value()));
}
return result;
}
};
```
and you can use it in our C++ TurboModule for example as:
```
using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct<
int32_t,
std::string,
std::optional<std::string>>;
template <>
struct Bridging<ObjectStruct>
: NativeCxxModuleExampleCxxBaseObjectStructBridging<
int32_t,
std::string,
std::optional<std::string>> {};
```
or as
```
using ObjectStruct = NativeCxxModuleExampleCxxBaseObjectStruct<
float,
folly::StringPiece,
std::optional<std::string>>;
template <>
struct Bridging<ObjectStruct>
: NativeCxxModuleExampleCxxBaseObjectStructBridging<
float,
folly::StringPiece,
std::optional<std::string>> {};
```
Or as
...
Changelog: [Internal]
Differential Revision: D41133761
fbshipit-source-id: 9ed5a118834e067951b5b45d2551188dff38219f1 parent 603cbbf commit 7ffab16
10 files changed
Lines changed: 505 additions & 233 deletions
File tree
- Libraries/WebPerformance
- packages
- react-native-codegen/src/generators/modules
- __tests__/__snapshots__
- rn-tester/NativeCxxModuleExample
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | 16 | | |
18 | 17 | | |
19 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
20 | 44 | | |
21 | 45 | | |
22 | 46 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
18 | 18 | | |
19 | 19 | | |
20 | 20 | | |
21 | | - | |
| 21 | + | |
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
| |||
27 | 27 | | |
28 | 28 | | |
29 | 29 | | |
30 | | - | |
| 30 | + | |
31 | 31 | | |
32 | 32 | | |
33 | 33 | | |
| |||
Lines changed: 0 additions & 76 deletions
This file was deleted.
Lines changed: 80 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
28 | 29 | | |
29 | 30 | | |
30 | 31 | | |
31 | | - | |
32 | | - | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
33 | 39 | | |
34 | 40 | | |
35 | 41 | | |
| |||
186 | 192 | | |
187 | 193 | | |
188 | 194 | | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
| 262 | + | |
| 263 | + | |
| 264 | + | |
189 | 265 | | |
190 | 266 | | |
191 | 267 | | |
| |||
251 | 327 | | |
252 | 328 | | |
253 | 329 | | |
| 330 | + | |
254 | 331 | | |
255 | 332 | | |
256 | 333 | | |
257 | 334 | | |
258 | 335 | | |
259 | 336 | | |
260 | 337 | | |
| 338 | + | |
261 | 339 | | |
262 | 340 | | |
263 | 341 | | |
| |||
0 commit comments