Skip to content

Commit 9f120ef

Browse files
JoshuaGrossfacebook-github-bot
authored andcommitted
ReactCommon/utils: Migrate uses of NDEBUG to REACT_NATIVE_DEBUG + react_native_assert
Summary: For better cross-platform consistency, migrate usages of NDEBUG to REACT_NATIVE_DEBUG. See flags.h for explanation. Changelog: [Internal] Reviewed By: PeteTheHeat Differential Revision: D26695275 fbshipit-source-id: 85aae94105a2817d345d25f736386e545dff0a9a
1 parent da73cca commit 9f120ef

File tree

6 files changed

+33
-18
lines changed

6 files changed

+33
-18
lines changed

ReactCommon/react/utils/Android.mk

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ LOCAL_MODULE := react_utils
1111

1212
LOCAL_SRC_FILES := $(wildcard $(LOCAL_PATH)/*.cpp)
1313

14-
LOCAL_C_INCLUDES := $(LOCAL_PATH)
14+
LOCAL_C_INCLUDES := $(LOCAL_PATH) $(LOCAL_PATH)/../../
1515
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../../
1616

1717
LOCAL_CFLAGS := \
@@ -20,6 +20,8 @@ LOCAL_CFLAGS := \
2020
LOCAL_CFLAGS += -fexceptions -frtti -std=c++14 -Wall
2121

2222
LOCAL_STATIC_LIBRARIES :=
23-
LOCAL_SHARED_LIBRARIES :=
23+
LOCAL_SHARED_LIBRARIES := libreact_debug
2424

2525
include $(BUILD_SHARED_LIBRARY)
26+
27+
$(call import-module,react/debug)

ReactCommon/react/utils/BUCK

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,6 @@ rn_xplat_cxx_library(
5959
"//xplat/folly:molly",
6060
"//xplat/jsi:jsi",
6161
react_native_xplat_target("better:better"),
62+
react_native_xplat_target("react/debug:debug"),
6263
],
6364
)

ReactCommon/react/utils/CalledOnceMovableOnlyFunction.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#include <functional>
99

10+
#include <react/debug/react_native_assert.h>
11+
1012
namespace facebook {
1113
namespace react {
1214

@@ -31,7 +33,7 @@ class CalledOnceMovableOnlyFunction {
3133
}
3234

3335
~CalledOnceMovableOnlyFunction() {
34-
assert(
36+
react_native_assert(
3537
(wasCalled_ || wasMovedFrom_) &&
3638
"`CalledOnceMovableOnlyFunction` is destroyed before being called.");
3739
}
@@ -57,7 +59,7 @@ class CalledOnceMovableOnlyFunction {
5759

5860
CalledOnceMovableOnlyFunction &operator=(
5961
CalledOnceMovableOnlyFunction &&other) noexcept {
60-
assert(
62+
react_native_assert(
6163
(wasCalled_ || wasMovedFrom_) &&
6264
"`CalledOnceMovableOnlyFunction` is re-assigned before being called.");
6365
wasCalled_ = false;
@@ -71,10 +73,10 @@ class CalledOnceMovableOnlyFunction {
7173
* Callable.
7274
*/
7375
ReturnT operator()(ArgumentT... args) {
74-
assert(
76+
react_native_assert(
7577
!wasMovedFrom_ &&
7678
"`CalledOnceMovableOnlyFunction` is called after being moved from.");
77-
assert(
79+
react_native_assert(
7880
!wasCalled_ &&
7981
"`CalledOnceMovableOnlyFunction` is called more than once.");
8082

ReactCommon/react/utils/ContextContainer.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
#include <better/mutex.h>
1616
#include <better/optional.h>
1717

18+
#include <react/debug/flags.h>
19+
#include <react/debug/react_native_assert.h>
20+
1821
namespace facebook {
1922
namespace react {
2023

@@ -43,7 +46,7 @@ class ContextContainer final {
4346

4447
instances_.insert({key, std::make_shared<T>(instance)});
4548

46-
#ifndef NDEBUG
49+
#ifdef REACT_NATIVE_DEBUG
4750
typeNames_.insert({key, typeid(T).name()});
4851
#endif
4952
}
@@ -57,7 +60,7 @@ class ContextContainer final {
5760

5861
instances_.erase(key);
5962

60-
#ifndef NDEBUG
63+
#ifdef REACT_NATIVE_DEBUG
6164
typeNames_.erase(key);
6265
#endif
6366
}
@@ -73,7 +76,7 @@ class ContextContainer final {
7376
for (auto const &pair : contextContainer.instances_) {
7477
instances_.erase(pair.first);
7578
instances_.insert(pair);
76-
#ifndef NDEBUG
79+
#ifdef REACT_NATIVE_DEBUG
7780
typeNames_.erase(pair.first);
7881
typeNames_.insert(
7982
{pair.first, contextContainer.typeNames_.at(pair.first)});
@@ -90,12 +93,14 @@ class ContextContainer final {
9093
T at(std::string const &key) const {
9194
std::shared_lock<better::shared_mutex> lock(mutex_);
9295

93-
assert(
96+
react_native_assert(
9497
instances_.find(key) != instances_.end() &&
9598
"ContextContainer doesn't have an instance for given key.");
96-
assert(
99+
#ifdef REACT_NATIVE_DEBUG
100+
react_native_assert(
97101
typeNames_.at(key) == typeid(T).name() &&
98102
"ContextContainer stores an instance of different type for given key.");
103+
#endif
99104
return *std::static_pointer_cast<T>(instances_.at(key));
100105
}
101106

@@ -113,9 +118,11 @@ class ContextContainer final {
113118
return {};
114119
}
115120

116-
assert(
121+
#ifdef REACT_NATIVE_DEBUG
122+
react_native_assert(
117123
typeNames_.at(key) == typeid(T).name() &&
118124
"ContextContainer stores an instance of different type for given key.");
125+
#endif
119126

120127
return *std::static_pointer_cast<T>(iterator->second);
121128
}
@@ -124,7 +131,7 @@ class ContextContainer final {
124131
mutable better::shared_mutex mutex_;
125132
// Protected by mutex_`.
126133
mutable better::map<std::string, std::shared_ptr<void>> instances_;
127-
#ifndef NDEBUG
134+
#ifdef REACT_NATIVE_DEBUG
128135
mutable better::map<std::string, std::string> typeNames_;
129136
#endif
130137
};

ReactCommon/react/utils/ManagedObjectWrapper.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#pragma once
99

10+
#include <react/debug/react_native_assert.h>
11+
1012
#if defined(__OBJC__) && defined(__cplusplus)
1113
#if TARGET_OS_MAC && TARGET_OS_IPHONE
1214

@@ -65,7 +67,7 @@ inline std::shared_ptr<void> wrapManagedObjectWeakly(id object) noexcept
6567
inline id unwrapManagedObjectWeakly(std::shared_ptr<void> const &object) noexcept
6668
{
6769
RCTInternalGenericWeakWrapper *weakWrapper = (RCTInternalGenericWeakWrapper *)unwrapManagedObject(object);
68-
assert(weakWrapper && "`RCTInternalGenericWeakWrapper` instance must not be `nil`.");
70+
react_native_assert(weakWrapper && "`RCTInternalGenericWeakWrapper` instance must not be `nil`.");
6971
return weakWrapper.object;
7072
}
7173

ReactCommon/react/utils/RunLoopObserver.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include "RunLoopObserver.h"
99

10-
#include <cassert>
10+
#include <react/debug/react_native_assert.h>
1111

1212
namespace facebook {
1313
namespace react {
@@ -19,8 +19,9 @@ RunLoopObserver::RunLoopObserver(
1919

2020
void RunLoopObserver::setDelegate(Delegate const *delegate) const noexcept {
2121
// We need these constraints to ensure basic thread-safety.
22-
assert(delegate && "A delegate must not be `nullptr`.");
23-
assert(!delegate_ && "`RunLoopObserver::setDelegate` must be called once.");
22+
react_native_assert(delegate && "A delegate must not be `nullptr`.");
23+
react_native_assert(
24+
!delegate_ && "`RunLoopObserver::setDelegate` must be called once.");
2425
delegate_ = delegate;
2526
}
2627

@@ -47,7 +48,7 @@ void RunLoopObserver::activityDidChange(Activity activity) const noexcept {
4748
return;
4849
}
4950

50-
assert(
51+
react_native_assert(
5152
!owner_.expired() &&
5253
"`owner_` is null. The caller must `lock` the owner and check it for being not null.");
5354

0 commit comments

Comments
 (0)