Skip to content

Commit d25af38

Browse files
javachemeta-codesync[bot]
authored andcommitted
Fix backwards-compat for ModuleConstants<::Builder> (#54919)
Summary: Pull Request resolved: #54919 Add a new `ResultT` to codegen and some C++ trickery so that `ModuleConstants<Type::Builder>` is equal to ModuleConstants<Type> and existing overrides of the module spec remain compatible. Changelog: [Internal] Reviewed By: cipolleschi Differential Revision: D89373357 fbshipit-source-id: ac192e99e424a829668722722cf0c374172de48b
1 parent 4de9242 commit d25af38

5 files changed

Lines changed: 128 additions & 1 deletion

File tree

packages/react-native-codegen/e2e/deep_imports/__tests__/modules/__snapshots__/GenerateModuleObjCpp-test.js.snap

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,9 @@ namespace JS {
275275
struct ConstantsEEE {
276276
277277
struct Builder {
278+
// Backwards compat for RCTTypedModuleConstants
279+
using ResultT = ConstantsEEE;
280+
278281
struct Input {
279282
RCTRequired<bool> D;
280283
RCTRequired<double> E;
@@ -304,6 +307,9 @@ namespace JS {
304307
struct ConstantsEE {
305308
306309
struct Builder {
310+
// Backwards compat for RCTTypedModuleConstants
311+
using ResultT = ConstantsEE;
312+
307313
struct Input {
308314
RCTRequired<bool> D;
309315
RCTRequired<JS::NativeObjectTurboModule::ConstantsEEE::Builder> E;
@@ -333,6 +339,9 @@ namespace JS {
333339
struct ConstantsE {
334340
335341
struct Builder {
342+
// Backwards compat for RCTTypedModuleConstants
343+
using ResultT = ConstantsE;
344+
336345
struct Input {
337346
RCTRequired<bool> D;
338347
RCTRequired<JS::NativeObjectTurboModule::ConstantsEE::Builder> E;
@@ -362,6 +371,9 @@ namespace JS {
362371
struct Constants {
363372
364373
struct Builder {
374+
// Backwards compat for RCTTypedModuleConstants
375+
using ResultT = Constants;
376+
365377
struct Input {
366378
RCTRequired<bool> D;
367379
RCTRequired<JS::NativeObjectTurboModule::ConstantsE::Builder> E;
@@ -420,6 +432,9 @@ namespace JS {
420432
struct ConstantsEEE {
421433
422434
struct Builder {
435+
// Backwards compat for RCTTypedModuleConstants
436+
using ResultT = ConstantsEEE;
437+
423438
struct Input {
424439
std::optional<bool> D;
425440
std::optional<double> E;
@@ -449,6 +464,9 @@ namespace JS {
449464
struct ConstantsEE {
450465
451466
struct Builder {
467+
// Backwards compat for RCTTypedModuleConstants
468+
using ResultT = ConstantsEE;
469+
452470
struct Input {
453471
std::optional<bool> D;
454472
std::optional<JS::NativeOptionalObjectTurboModule::ConstantsEEE::Builder> E;
@@ -478,6 +496,9 @@ namespace JS {
478496
struct ConstantsE {
479497
480498
struct Builder {
499+
// Backwards compat for RCTTypedModuleConstants
500+
using ResultT = ConstantsE;
501+
481502
struct Input {
482503
std::optional<bool> D;
483504
std::optional<JS::NativeOptionalObjectTurboModule::ConstantsEE::Builder> E;
@@ -507,6 +528,9 @@ namespace JS {
507528
struct Constants {
508529
509530
struct Builder {
531+
// Backwards compat for RCTTypedModuleConstants
532+
using ResultT = Constants;
533+
510534
struct Input {
511535
std::optional<bool> D;
512536
id<NSObject> _Nullable A;
@@ -698,6 +722,9 @@ namespace JS {
698722
struct Constants {
699723
700724
struct Builder {
725+
// Backwards compat for RCTTypedModuleConstants
726+
using ResultT = Constants;
727+
701728
struct Input {
702729
RCTRequired<bool> const1;
703730
RCTRequired<double> const2;
@@ -768,6 +795,9 @@ namespace JS {
768795
struct ConstantsIdElement {
769796
770797
struct Builder {
798+
// Backwards compat for RCTTypedModuleConstants
799+
using ResultT = ConstantsIdElement;
800+
771801
struct Input {
772802
RCTRequired<double> prop;
773803
};
@@ -795,6 +825,9 @@ namespace JS {
795825
struct Constants {
796826
797827
struct Builder {
828+
// Backwards compat for RCTTypedModuleConstants
829+
using ResultT = Constants;
830+
798831
struct Input {
799832
RCTRequired<std::vector<bool>> const1;
800833
RCTRequired<std::vector<double>> const2;
@@ -896,6 +929,9 @@ namespace JS {
896929
struct Constants {
897930
898931
struct Builder {
932+
// Backwards compat for RCTTypedModuleConstants
933+
using ResultT = Constants;
934+
899935
struct Input {
900936
RCTRequired<std::optional<bool>> const1;
901937
RCTRequired<std::optional<double>> const2;
@@ -996,6 +1032,9 @@ namespace JS {
9961032
struct Constants {
9971033
9981034
struct Builder {
1035+
// Backwards compat for RCTTypedModuleConstants
1036+
using ResultT = Constants;
1037+
9991038
struct Input {
10001039
std::optional<bool> const1;
10011040
std::optional<double> const2;
@@ -1096,6 +1135,9 @@ namespace JS {
10961135
struct Constants {
10971136
10981137
struct Builder {
1138+
// Backwards compat for RCTTypedModuleConstants
1139+
using ResultT = Constants;
1140+
10991141
struct Input {
11001142
std::optional<bool> const1;
11011143
std::optional<double> const2;
@@ -1787,6 +1829,9 @@ namespace JS {
17871829
struct ConstantsEEE {
17881830
17891831
struct Builder {
1832+
// Backwards compat for RCTTypedModuleConstants
1833+
using ResultT = ConstantsEEE;
1834+
17901835
struct Input {
17911836
RCTRequired<bool> D;
17921837
RCTRequired<double> E;
@@ -1816,6 +1861,9 @@ namespace JS {
18161861
struct ConstantsEE {
18171862
18181863
struct Builder {
1864+
// Backwards compat for RCTTypedModuleConstants
1865+
using ResultT = ConstantsEE;
1866+
18191867
struct Input {
18201868
RCTRequired<bool> D;
18211869
RCTRequired<JS::NativeObjectTurboModule::ConstantsEEE::Builder> E;
@@ -1845,6 +1893,9 @@ namespace JS {
18451893
struct ConstantsE {
18461894
18471895
struct Builder {
1896+
// Backwards compat for RCTTypedModuleConstants
1897+
using ResultT = ConstantsE;
1898+
18481899
struct Input {
18491900
RCTRequired<bool> D;
18501901
RCTRequired<JS::NativeObjectTurboModule::ConstantsEE::Builder> E;
@@ -1874,6 +1925,9 @@ namespace JS {
18741925
struct Constants {
18751926
18761927
struct Builder {
1928+
// Backwards compat for RCTTypedModuleConstants
1929+
using ResultT = Constants;
1930+
18771931
struct Input {
18781932
RCTRequired<bool> D;
18791933
RCTRequired<JS::NativeObjectTurboModule::ConstantsE::Builder> E;
@@ -1932,6 +1986,9 @@ namespace JS {
19321986
struct ConstantsEEE {
19331987
19341988
struct Builder {
1989+
// Backwards compat for RCTTypedModuleConstants
1990+
using ResultT = ConstantsEEE;
1991+
19351992
struct Input {
19361993
std::optional<bool> D;
19371994
std::optional<double> E;
@@ -1961,6 +2018,9 @@ namespace JS {
19612018
struct ConstantsEE {
19622019
19632020
struct Builder {
2021+
// Backwards compat for RCTTypedModuleConstants
2022+
using ResultT = ConstantsEE;
2023+
19642024
struct Input {
19652025
std::optional<bool> D;
19662026
std::optional<JS::NativeOptionalObjectTurboModule::ConstantsEEE::Builder> E;
@@ -1990,6 +2050,9 @@ namespace JS {
19902050
struct ConstantsE {
19912051
19922052
struct Builder {
2053+
// Backwards compat for RCTTypedModuleConstants
2054+
using ResultT = ConstantsE;
2055+
19932056
struct Input {
19942057
std::optional<bool> D;
19952058
std::optional<JS::NativeOptionalObjectTurboModule::ConstantsEE::Builder> E;
@@ -2019,6 +2082,9 @@ namespace JS {
20192082
struct Constants {
20202083
20212084
struct Builder {
2085+
// Backwards compat for RCTTypedModuleConstants
2086+
using ResultT = Constants;
2087+
20222088
struct Input {
20232089
std::optional<bool> D;
20242090
id<NSObject> _Nullable A;
@@ -2210,6 +2276,9 @@ namespace JS {
22102276
struct Constants {
22112277
22122278
struct Builder {
2279+
// Backwards compat for RCTTypedModuleConstants
2280+
using ResultT = Constants;
2281+
22132282
struct Input {
22142283
RCTRequired<bool> const1;
22152284
RCTRequired<double> const2;
@@ -2280,6 +2349,9 @@ namespace JS {
22802349
struct ConstantsIdElement {
22812350
22822351
struct Builder {
2352+
// Backwards compat for RCTTypedModuleConstants
2353+
using ResultT = ConstantsIdElement;
2354+
22832355
struct Input {
22842356
RCTRequired<double> prop;
22852357
};
@@ -2307,6 +2379,9 @@ namespace JS {
23072379
struct Constants {
23082380
23092381
struct Builder {
2382+
// Backwards compat for RCTTypedModuleConstants
2383+
using ResultT = Constants;
2384+
23102385
struct Input {
23112386
RCTRequired<std::vector<bool>> const1;
23122387
RCTRequired<std::vector<double>> const2;
@@ -2408,6 +2483,9 @@ namespace JS {
24082483
struct Constants {
24092484
24102485
struct Builder {
2486+
// Backwards compat for RCTTypedModuleConstants
2487+
using ResultT = Constants;
2488+
24112489
struct Input {
24122490
RCTRequired<std::optional<bool>> const1;
24132491
RCTRequired<std::optional<double>> const2;
@@ -2508,6 +2586,9 @@ namespace JS {
25082586
struct Constants {
25092587
25102588
struct Builder {
2589+
// Backwards compat for RCTTypedModuleConstants
2590+
using ResultT = Constants;
2591+
25112592
struct Input {
25122593
std::optional<bool> const1;
25132594
std::optional<double> const2;
@@ -2608,6 +2689,9 @@ namespace JS {
26082689
struct Constants {
26092690
26102691
struct Builder {
2692+
// Backwards compat for RCTTypedModuleConstants
2693+
using ResultT = Constants;
2694+
26112695
struct Input {
26122696
std::optional<bool> const1;
26132697
std::optional<double> const2;

packages/react-native-codegen/src/generators/modules/GenerateModuleObjCpp/header/serializeConstantsStruct.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ const StructTemplate = ({
3535
struct ${structName} {
3636
3737
struct Builder {
38+
// Backwards compat for RCTTypedModuleConstants
39+
using ResultT = ${structName};
40+
3841
struct Input {
3942
${builderInputProps}
4043
};

packages/react-native-codegen/src/generators/modules/__tests__/__snapshots__/GenerateModuleHObjCpp-test.js.snap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,9 @@ namespace JS {
10081008
struct Constants {
10091009
10101010
struct Builder {
1011+
// Backwards compat for RCTTypedModuleConstants
1012+
using ResultT = Constants;
1013+
10111014
struct Input {
10121015
RCTRequired<bool> const1;
10131016
RCTRequired<double> const2;

packages/react-native/Libraries/TypeSafety/RCTTypedModuleConstants.h

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
#include <type_traits>
89
#include <utility>
910

1011
#import <Foundation/Foundation.h>
@@ -32,10 +33,44 @@
3233

3334
namespace facebook::react {
3435

36+
namespace detail {
37+
38+
// Helper to detect if T is a Builder type.
39+
// Builder types have: nested Input struct and buildUnsafeRawValue() method.
40+
template <typename T, typename = void>
41+
struct IsBuilder : std::false_type {};
42+
43+
template <typename T>
44+
struct IsBuilder<T, std::void_t<typename T::Input, decltype(std::declval<T>().buildUnsafeRawValue())>>
45+
: std::true_type {};
46+
47+
// Resolve Builder to its parent Constants type.
48+
// For backwards compatibility: if T is a Builder type (e.g., JS::X::Constants::Builder),
49+
// we resolve to the parent Constants type (JS::X::Constants).
50+
//
51+
// This requires Builder to have a type alias `using ResultT = Constants;` defined by codegen.
52+
template <typename T, bool = IsBuilder<T>::value>
53+
struct ResolveConstantsType {
54+
// Not a Builder, use T as-is
55+
using type = T;
56+
};
57+
58+
// Specialization for Builder types that have ResultT defined
59+
template <typename T>
60+
struct ResolveConstantsType<T, true> {
61+
// Use T::ResultT which points to the parent Constants type (added by codegen)
62+
using type = typename T::ResultT;
63+
};
64+
65+
} // namespace detail
66+
3567
// Objective-C doesn't allow arbitrary types in its lightweight generics, only object and block types. We can work
3668
// around that by having the struct type we care about be a block-argument. The block never exists at runtime.
69+
//
70+
// For backwards compatibility: if T is a Builder type (e.g., JS::Module::Constants::Builder),
71+
// ModuleConstants<Builder> resolves to the same type as ModuleConstants<Constants>.
3772
template <typename T>
38-
using ModuleConstants = _RCTTypedModuleConstants<void (^)(T)> *;
73+
using ModuleConstants = _RCTTypedModuleConstants<void (^)(typename detail::ResolveConstantsType<T>::type)> *;
3974

4075
template <typename T>
4176
ModuleConstants<T> typedConstants(typename T::Builder::Input &&value)

packages/react-native/ReactCommon/react/nativemodule/samples/platform/ios/ReactCommon/RCTNativeSampleTurboModuleSpec.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ NS_ASSUME_NONNULL_BEGIN
2525
namespace JS::NativeSampleTurboModule {
2626
struct Constants {
2727
struct Builder {
28+
using ResultT = Constants;
29+
2830
struct Input {
2931
RCTRequired<bool> const1;
3032
RCTRequired<double> const2;

0 commit comments

Comments
 (0)