2015-03-23 20:28:42 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
2015-02-20 04:10:52 +00:00
|
|
|
|
|
|
|
#import <Foundation/Foundation.h>
|
|
|
|
|
2015-04-21 12:26:51 +00:00
|
|
|
#import "RCTDefines.h"
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2016-06-06 14:57:55 +00:00
|
|
|
/*
|
|
|
|
* Defined in RCTUtils.m
|
|
|
|
*/
|
|
|
|
RCT_EXTERN BOOL RCTIsMainQueue(void);
|
|
|
|
|
2015-04-11 22:08:00 +00:00
|
|
|
/**
|
2015-10-08 15:13:16 +00:00
|
|
|
* This is the main assert macro that you should use. Asserts should be compiled out
|
2015-11-05 20:19:56 +00:00
|
|
|
* in production builds. You can customize the assert behaviour by setting a custom
|
|
|
|
* assert handler through `RCTSetAssertFunction`.
|
2015-04-11 22:08:00 +00:00
|
|
|
*/
|
2015-10-08 15:13:16 +00:00
|
|
|
#ifndef NS_BLOCK_ASSERTIONS
|
2015-08-07 13:06:17 +00:00
|
|
|
#define RCTAssert(condition, ...) do { \
|
|
|
|
if ((condition) == 0) { \
|
|
|
|
_RCTAssertFormat(#condition, __FILE__, __LINE__, __func__, __VA_ARGS__); \
|
|
|
|
if (RCT_NSASSERT) { \
|
|
|
|
[[NSAssertionHandler currentHandler] handleFailureInFunction:@(__func__) \
|
|
|
|
file:@(__FILE__) lineNumber:__LINE__ description:__VA_ARGS__]; \
|
|
|
|
} \
|
|
|
|
} \
|
2015-02-20 04:10:52 +00:00
|
|
|
} while (false)
|
2015-10-08 15:13:16 +00:00
|
|
|
#else
|
|
|
|
#define RCTAssert(condition, ...) do {} while (false)
|
|
|
|
#endif
|
2015-08-07 13:06:17 +00:00
|
|
|
RCT_EXTERN void _RCTAssertFormat(
|
|
|
|
const char *, const char *, int, const char *, NSString *, ...
|
|
|
|
) NS_FORMAT_FUNCTION(5,6);
|
2015-06-15 14:53:45 +00:00
|
|
|
|
2015-11-05 20:19:56 +00:00
|
|
|
/**
|
|
|
|
* Report a fatal condition when executing. These calls will _NOT_ be compiled out
|
|
|
|
* in production, and crash the app by default. You can customize the fatal behaviour
|
|
|
|
* by setting a custom fatal handler through `RCTSetFatalHandler`.
|
|
|
|
*/
|
|
|
|
RCT_EXTERN void RCTFatal(NSError *error);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The default error domain to be used for React errors.
|
|
|
|
*/
|
|
|
|
RCT_EXTERN NSString *const RCTErrorDomain;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* JS Stack trace provided as part of an NSError's userInfo
|
|
|
|
*/
|
|
|
|
RCT_EXTERN NSString *const RCTJSStackTraceKey;
|
|
|
|
|
2016-07-01 02:38:53 +00:00
|
|
|
/**
|
|
|
|
* Raw JS Stack trace string provided as part of an NSError's userInfo
|
|
|
|
*/
|
|
|
|
RCT_EXTERN NSString *const RCTJSRawStackTraceKey;
|
|
|
|
|
2015-11-10 12:29:39 +00:00
|
|
|
/**
|
|
|
|
* Name of fatal exceptions generated by RCTFatal
|
|
|
|
*/
|
|
|
|
RCT_EXTERN NSString *const RCTFatalExceptionName;
|
|
|
|
|
2015-11-05 20:19:56 +00:00
|
|
|
/**
|
|
|
|
* A block signature to be used for custom assertion handling.
|
|
|
|
*/
|
2016-06-06 14:57:55 +00:00
|
|
|
typedef void (^RCTAssertFunction)(NSString *condition,
|
2015-11-05 20:19:56 +00:00
|
|
|
NSString *fileName,
|
|
|
|
NSNumber *lineNumber,
|
|
|
|
NSString *function,
|
2016-06-06 14:57:55 +00:00
|
|
|
NSString *message);
|
2015-11-05 20:19:56 +00:00
|
|
|
|
|
|
|
typedef void (^RCTFatalHandler)(NSError *error);
|
|
|
|
|
2015-06-15 14:53:45 +00:00
|
|
|
/**
|
|
|
|
* Convenience macro for asserting that a parameter is non-nil/non-zero.
|
|
|
|
*/
|
2015-11-05 20:19:56 +00:00
|
|
|
#define RCTAssertParam(name) RCTAssert(name, @"'%s' is a required parameter", #name)
|
2015-02-20 04:10:52 +00:00
|
|
|
|
2015-04-11 22:08:00 +00:00
|
|
|
/**
|
2016-06-06 14:57:55 +00:00
|
|
|
* Convenience macro for asserting that we're running on main queue.
|
2015-04-11 22:08:00 +00:00
|
|
|
*/
|
2016-06-06 14:57:55 +00:00
|
|
|
#define RCTAssertMainQueue() RCTAssert(RCTIsMainQueue(), \
|
2015-11-05 20:19:56 +00:00
|
|
|
@"This function must be called on the main thread")
|
2015-04-11 22:08:00 +00:00
|
|
|
|
2015-11-17 15:18:55 +00:00
|
|
|
/**
|
2016-06-06 14:57:55 +00:00
|
|
|
* Convenience macro for asserting that we're running off the main queue.
|
2015-11-17 15:18:55 +00:00
|
|
|
*/
|
2016-06-06 14:57:55 +00:00
|
|
|
#define RCTAssertNotMainQueue() RCTAssert(!RCTIsMainQueue(), \
|
2015-11-17 15:18:55 +00:00
|
|
|
@"This function must not be called on the main thread")
|
|
|
|
|
2016-06-06 14:57:55 +00:00
|
|
|
/**
|
|
|
|
* Deprecated, do not use
|
|
|
|
*/
|
|
|
|
#define RCTAssertMainThread() RCTAssertMainQueue()
|
|
|
|
#define RCTAssertNotMainThread() RCTAssertNotMainQueue()
|
|
|
|
|
2015-04-11 22:08:00 +00:00
|
|
|
/**
|
|
|
|
* These methods get and set the current assert function called by the RCTAssert
|
2015-11-05 20:19:56 +00:00
|
|
|
* macros. You can use these to replace the standard behavior with custom assert
|
2015-04-11 22:08:00 +00:00
|
|
|
* functionality.
|
|
|
|
*/
|
2015-04-21 12:26:51 +00:00
|
|
|
RCT_EXTERN void RCTSetAssertFunction(RCTAssertFunction assertFunction);
|
|
|
|
RCT_EXTERN RCTAssertFunction RCTGetAssertFunction(void);
|
2015-04-11 22:08:00 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This appends additional code to the existing assert function, without
|
|
|
|
* replacing the existing functionality. Useful if you just want to forward
|
|
|
|
* assert info to an extra service without changing the default behavior.
|
|
|
|
*/
|
2015-04-21 12:26:51 +00:00
|
|
|
RCT_EXTERN void RCTAddAssertFunction(RCTAssertFunction assertFunction);
|
2015-05-25 12:19:53 +00:00
|
|
|
|
2015-08-07 13:06:17 +00:00
|
|
|
/**
|
|
|
|
* This method temporarily overrides the assert function while performing the
|
|
|
|
* specified block. This is useful for testing purposes (to detect if a given
|
|
|
|
* function asserts something) or to suppress or override assertions temporarily.
|
|
|
|
*/
|
|
|
|
RCT_EXTERN void RCTPerformBlockWithAssertFunction(void (^block)(void), RCTAssertFunction assertFunction);
|
|
|
|
|
2015-11-05 20:19:56 +00:00
|
|
|
/**
|
|
|
|
These methods get and set the current fatal handler called by the RCTFatal method.
|
|
|
|
*/
|
|
|
|
RCT_EXTERN void RCTSetFatalHandler(RCTFatalHandler fatalHandler);
|
|
|
|
RCT_EXTERN RCTFatalHandler RCTGetFatalHandler(void);
|
|
|
|
|
2015-05-25 12:19:53 +00:00
|
|
|
/**
|
|
|
|
* Get the current thread's name (or the current queue, if in debug mode)
|
|
|
|
*/
|
|
|
|
RCT_EXTERN NSString *RCTCurrentThreadName(void);
|
|
|
|
|
2015-11-05 20:20:02 +00:00
|
|
|
/**
|
|
|
|
* Helper to get generate exception message from NSError
|
|
|
|
*/
|
2015-11-14 18:25:00 +00:00
|
|
|
RCT_EXTERN NSString *RCTFormatError(NSString *message, NSArray<NSDictionary<NSString *, id> *> *stacktrace, NSUInteger maxMessageLength);
|
2015-11-05 20:20:02 +00:00
|
|
|
|
2015-05-25 12:19:53 +00:00
|
|
|
/**
|
|
|
|
* Convenience macro to assert which thread is currently running (DEBUG mode only)
|
|
|
|
*/
|
|
|
|
#if DEBUG
|
|
|
|
|
|
|
|
#define RCTAssertThread(thread, format...) \
|
|
|
|
_Pragma("clang diagnostic push") \
|
|
|
|
_Pragma("clang diagnostic ignored \"-Wdeprecated-declarations\"") \
|
|
|
|
RCTAssert( \
|
|
|
|
[(id)thread isKindOfClass:[NSString class]] ? \
|
|
|
|
[RCTCurrentThreadName() isEqualToString:(NSString *)thread] : \
|
|
|
|
[(id)thread isKindOfClass:[NSThread class]] ? \
|
|
|
|
[NSThread currentThread] == (NSThread *)thread : \
|
|
|
|
dispatch_get_current_queue() == (dispatch_queue_t)thread, \
|
|
|
|
format); \
|
|
|
|
_Pragma("clang diagnostic pop")
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2015-11-05 20:19:56 +00:00
|
|
|
#define RCTAssertThread(thread, format...) do { } while (0)
|
2015-05-25 12:19:53 +00:00
|
|
|
|
|
|
|
#endif
|