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