1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #import <QuartzCore/QuartzCore.h>
9 #import <UIKit/UIKit.h>
10 
11 #import <ABI48_0_0React/ABI48_0_0RCTAnimationType.h>
12 #import <ABI48_0_0React/ABI48_0_0RCTBorderCurve.h>
13 #import <ABI48_0_0React/ABI48_0_0RCTBorderStyle.h>
14 #import <ABI48_0_0React/ABI48_0_0RCTDefines.h>
15 #import <ABI48_0_0React/ABI48_0_0RCTLog.h>
16 #import <ABI48_0_0React/ABI48_0_0RCTPointerEvents.h>
17 #import <ABI48_0_0React/ABI48_0_0RCTTextDecorationLineType.h>
18 #import <ABI48_0_0yoga/ABI48_0_0Yoga.h>
19 #if TARGET_OS_IPHONE
20 #import <WebKit/WebKit.h>
21 #endif
22 
23 /**
24  * This class provides a collection of conversion functions for mapping
25  * JSON objects to native types and classes. These are useful when writing
26  * custom ABI48_0_0RCTViewManager setter methods.
27  */
28 @interface ABI48_0_0RCTConvert : NSObject
29 
30 + (id)id:(id)json;
31 
32 + (BOOL)BOOL:(id)json;
33 + (double)double:(id)json;
34 + (float)float:(id)json;
35 + (int)int:(id)json;
36 
37 + (int64_t)int64_t:(id)json;
38 + (uint64_t)uint64_t:(id)json;
39 
40 + (NSInteger)NSInteger:(id)json;
41 + (NSUInteger)NSUInteger:(id)json;
42 
43 + (NSArray *)NSArray:(id)json;
44 + (NSDictionary *)NSDictionary:(id)json;
45 + (NSString *)NSString:(id)json;
46 + (NSNumber *)NSNumber:(id)json;
47 
48 + (NSSet *)NSSet:(id)json;
49 + (NSData *)NSData:(id)json;
50 + (NSIndexSet *)NSIndexSet:(id)json;
51 
52 + (NSURLRequestCachePolicy)NSURLRequestCachePolicy:(id)json;
53 + (NSURL *)NSURL:(id)json;
54 + (NSURLRequest *)NSURLRequest:(id)json;
55 
56 typedef NSURL ABI48_0_0RCTFileURL;
57 + (ABI48_0_0RCTFileURL *)ABI48_0_0RCTFileURL:(id)json;
58 
59 + (NSDate *)NSDate:(id)json;
60 + (NSLocale *)NSLocale:(id)json;
61 + (NSTimeZone *)NSTimeZone:(id)json;
62 + (NSTimeInterval)NSTimeInterval:(id)json;
63 
64 + (NSLineBreakMode)NSLineBreakMode:(id)json;
65 + (NSTextAlignment)NSTextAlignment:(id)json;
66 + (NSUnderlineStyle)NSUnderlineStyle:(id)json;
67 + (NSWritingDirection)NSWritingDirection:(id)json;
68 + (NSLineBreakStrategy)NSLineBreakStrategy:(id)json;
69 + (UITextAutocapitalizationType)UITextAutocapitalizationType:(id)json;
70 + (UITextFieldViewMode)UITextFieldViewMode:(id)json;
71 + (UIKeyboardType)UIKeyboardType:(id)json;
72 + (UIKeyboardAppearance)UIKeyboardAppearance:(id)json;
73 + (UIReturnKeyType)UIReturnKeyType:(id)json;
74 #if !TARGET_OS_TV
75 + (UIDataDetectorTypes)UIDataDetectorTypes:(id)json;
76 #endif
77 
78 #if TARGET_OS_IPHONE
79 + (WKDataDetectorTypes)WKDataDetectorTypes:(id)json;
80 #endif
81 
82 + (UIViewContentMode)UIViewContentMode:(id)json;
83 #if !TARGET_OS_TV
84 + (UIBarStyle)UIBarStyle:(id)json;
85 #endif
86 
87 + (CGFloat)CGFloat:(id)json;
88 + (CGPoint)CGPoint:(id)json;
89 + (CGSize)CGSize:(id)json;
90 + (CGRect)CGRect:(id)json;
91 + (UIEdgeInsets)UIEdgeInsets:(id)json;
92 
93 + (CGLineCap)CGLineCap:(id)json;
94 + (CGLineJoin)CGLineJoin:(id)json;
95 
96 + (CGAffineTransform)CGAffineTransform:(id)json;
97 
98 + (UIColor *)UIColor:(id)json;
99 + (CGColorRef)CGColor:(id)json CF_RETURNS_NOT_RETAINED;
100 
101 + (ABI48_0_0YGValue)ABI48_0_0YGValue:(id)json;
102 
103 + (NSArray<NSArray *> *)NSArrayArray:(id)json;
104 + (NSArray<NSString *> *)NSStringArray:(id)json;
105 + (NSArray<NSArray<NSString *> *> *)NSStringArrayArray:(id)json;
106 + (NSArray<NSDictionary *> *)NSDictionaryArray:(id)json;
107 + (NSArray<NSURL *> *)NSURLArray:(id)json;
108 + (NSArray<ABI48_0_0RCTFileURL *> *)ABI48_0_0RCTFileURLArray:(id)json;
109 + (NSArray<NSNumber *> *)NSNumberArray:(id)json;
110 + (NSArray<UIColor *> *)UIColorArray:(id)json;
111 
112 typedef NSArray CGColorArray;
113 + (CGColorArray *)CGColorArray:(id)json;
114 
115 /**
116  * Convert a JSON object to a Plist-safe equivalent by stripping null values.
117  */
118 typedef id NSPropertyList;
119 + (NSPropertyList)NSPropertyList:(id)json;
120 
121 typedef BOOL css_backface_visibility_t;
122 + (ABI48_0_0YGOverflow)ABI48_0_0YGOverflow:(id)json;
123 + (ABI48_0_0YGDisplay)ABI48_0_0YGDisplay:(id)json;
124 + (css_backface_visibility_t)css_backface_visibility_t:(id)json;
125 + (ABI48_0_0YGFlexDirection)ABI48_0_0YGFlexDirection:(id)json;
126 + (ABI48_0_0YGJustify)ABI48_0_0YGJustify:(id)json;
127 + (ABI48_0_0YGAlign)ABI48_0_0YGAlign:(id)json;
128 + (ABI48_0_0YGPositionType)ABI48_0_0YGPositionType:(id)json;
129 + (ABI48_0_0YGWrap)ABI48_0_0YGWrap:(id)json;
130 + (ABI48_0_0YGDirection)ABI48_0_0YGDirection:(id)json;
131 
132 + (ABI48_0_0RCTPointerEvents)ABI48_0_0RCTPointerEvents:(id)json;
133 + (ABI48_0_0RCTAnimationType)ABI48_0_0RCTAnimationType:(id)json;
134 + (ABI48_0_0RCTBorderStyle)ABI48_0_0RCTBorderStyle:(id)json;
135 + (ABI48_0_0RCTBorderCurve)ABI48_0_0RCTBorderCurve:(id)json;
136 + (ABI48_0_0RCTTextDecorationLineType)ABI48_0_0RCTTextDecorationLineType:(id)json;
137 
138 @end
139 
140 @interface ABI48_0_0RCTConvert (Deprecated)
141 
142 /**
143  * Use lightweight generics syntax instead, e.g. NSArray<NSString *>
144  */
145 typedef NSArray NSArrayArray __deprecated_msg("Use NSArray<NSArray *>");
146 typedef NSArray NSStringArray __deprecated_msg("Use NSArray<NSString *>");
147 typedef NSArray NSStringArrayArray __deprecated_msg("Use NSArray<NSArray<NSString *> *>");
148 typedef NSArray NSDictionaryArray __deprecated_msg("Use NSArray<NSDictionary *>");
149 typedef NSArray NSURLArray __deprecated_msg("Use NSArray<NSURL *>");
150 typedef NSArray ABI48_0_0RCTFileURLArray __deprecated_msg("Use NSArray<ABI48_0_0RCTFileURL *>");
151 typedef NSArray NSNumberArray __deprecated_msg("Use NSArray<NSNumber *>");
152 typedef NSArray UIColorArray __deprecated_msg("Use NSArray<UIColor *>");
153 
154 /**
155  * Synchronous image loading is generally a bad idea for performance reasons.
156  * If you need to pass image references, try to use `ABI48_0_0RCTImageSource` and then
157  * `ABI48_0_0RCTImageLoader` instead of converting directly to a UIImage.
158  */
159 + (UIImage *)UIImage:(id)json;
160 + (CGImageRef)CGImage:(id)json CF_RETURNS_NOT_RETAINED;
161 
162 @end
163 
164 /**
165  * Underlying implementations of ABI48_0_0RCT_XXX_CONVERTER macros. Ignore these.
166  */
167 ABI48_0_0RCT_EXTERN NSNumber *ABI48_0_0RCTConvertEnumValue(const char *, NSDictionary *, NSNumber *, id);
168 ABI48_0_0RCT_EXTERN NSNumber *ABI48_0_0RCTConvertMultiEnumValue(const char *, NSDictionary *, NSNumber *, id);
169 ABI48_0_0RCT_EXTERN NSArray *ABI48_0_0RCTConvertArrayValue(SEL, id);
170 
171 /**
172  * This macro is used for logging conversion errors. This is just used to
173  * avoid repeating the same boilerplate for every error message.
174  */
175 #define ABI48_0_0RCTLogConvertError(json, typeName) \
176   ABI48_0_0RCTLogError(@"JSON value '%@' of type %@ cannot be converted to %@", json, [json classForCoder], typeName)
177 
178 /**
179  * This macro is used for creating simple converter functions that just call
180  * the specified getter method on the json value.
181  */
182 #define ABI48_0_0RCT_CONVERTER(type, name, getter) ABI48_0_0RCT_CUSTOM_CONVERTER(type, name, [json getter])
183 
184 /**
185  * This macro is used for creating converter functions with arbitrary logic.
186  */
187 #define ABI48_0_0RCT_CUSTOM_CONVERTER(type, name, code) \
188   +(type)name : (id)json ABI48_0_0RCT_DYNAMIC           \
189   {                                            \
190     if (!ABI48_0_0RCT_DEBUG) {                          \
191       return code;                             \
192     } else {                                   \
193       @try {                                   \
194         return code;                           \
195       } @catch (__unused NSException * e) {    \
196         ABI48_0_0RCTLogConvertError(json, @ #type);     \
197         json = nil;                            \
198         return code;                           \
199       }                                        \
200     }                                          \
201   }
202 
203 /**
204  * This macro is similar to ABI48_0_0RCT_CONVERTER, but specifically geared towards
205  * numeric types. It will handle string input correctly, and provides more
206  * detailed error reporting if an invalid value is passed in.
207  */
208 #define ABI48_0_0RCT_NUMBER_CONVERTER(type, getter) \
209   ABI48_0_0RCT_CUSTOM_CONVERTER(type, type, [ABI48_0_0RCT_DEBUG ? [self NSNumber:json] : json getter])
210 
211 /**
212  * When using ABI48_0_0RCT_ENUM_CONVERTER in ObjC, the compiler is OK with us returning
213  * the underlying NSInteger/NSUInteger. In ObjC++, this is a type mismatch and
214  * we need to explicitly cast the return value to expected enum return type.
215  */
216 #ifdef __cplusplus
217 #define _ABI48_0_0RCT_CAST(type, expr) static_cast<type>(expr)
218 #else
219 #define _ABI48_0_0RCT_CAST(type, expr) expr
220 #endif
221 
222 /**
223  * This macro is used for creating converters for enum types.
224  */
225 #define ABI48_0_0RCT_ENUM_CONVERTER(type, values, default, getter)                                   \
226   +(type)type : (id)json ABI48_0_0RCT_DYNAMIC                                                        \
227   {                                                                                         \
228     static NSDictionary *mapping;                                                           \
229     static dispatch_once_t onceToken;                                                       \
230     dispatch_once(&onceToken, ^{                                                            \
231       mapping = values;                                                                     \
232     });                                                                                     \
233     return _ABI48_0_0RCT_CAST(type, [ABI48_0_0RCTConvertEnumValue(#type, mapping, @(default), json) getter]); \
234   }
235 
236 /**
237  * This macro is used for creating converters for enum types for
238  * multiple enum values combined with | operator
239  */
240 #define ABI48_0_0RCT_MULTI_ENUM_CONVERTER(type, values, default, getter)                                  \
241   +(type)type : (id)json ABI48_0_0RCT_DYNAMIC                                                             \
242   {                                                                                              \
243     static NSDictionary *mapping;                                                                \
244     static dispatch_once_t onceToken;                                                            \
245     dispatch_once(&onceToken, ^{                                                                 \
246       mapping = values;                                                                          \
247     });                                                                                          \
248     return _ABI48_0_0RCT_CAST(type, [ABI48_0_0RCTConvertMultiEnumValue(#type, mapping, @(default), json) getter]); \
249   }
250 
251 /**
252  * This macro is used for creating explicitly-named converter functions
253  * for typed arrays.
254  */
255 #define ABI48_0_0RCT_ARRAY_CONVERTER_NAMED(type, name)            \
256   +(NSArray<type *> *)name##Array : (id)json ABI48_0_0RCT_DYNAMIC \
257   {                                                      \
258     return ABI48_0_0RCTConvertArrayValue(@selector(name:), json); \
259   }
260 
261 /**
262  * This macro is used for creating converter functions for typed arrays.
263  * ABI48_0_0RCT_ARRAY_CONVERTER_NAMED may be used when type contains characters
264  * which are disallowed in selector names.
265  */
266 #define ABI48_0_0RCT_ARRAY_CONVERTER(type) ABI48_0_0RCT_ARRAY_CONVERTER_NAMED(type, type)
267