Skip to content

Commit f94e434

Browse files
Emil SjolanderFacebook Github Bot
authored andcommitted
Implement custom assert macro
Reviewed By: javache Differential Revision: D3648805 fbshipit-source-id: a6bf1bb55e1e0ee37284647ab76d66f3956a66c0
1 parent 2d168fc commit f94e434

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

React/CSSLayout/CSSLayout.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ __forceinline const float fmaxf(const float a, const float b) {
2727

2828
CSSNodeRef CSSNodeNew() {
2929
CSSNodeRef node = calloc(1, sizeof(CSSNode));
30-
assert(node != NULL);
30+
CSS_ASSERT(node != NULL, "Could not allocate memory for node");
3131

3232
CSSNodeInit(node);
3333
return node;
@@ -119,8 +119,7 @@ uint32_t CSSNodeChildCount(CSSNodeRef node) {
119119
}
120120

121121
void CSSNodeMarkDirty(CSSNodeRef node) {
122-
// Nodes without custom measure functions should not manually mark themselves as dirty
123-
assert(node->measure != NULL);
122+
CSS_ASSERT(node->measure != NULL, "Nodes without custom measure functions should not manually mark themselves as dirty");
124123
_CSSNodeMarkDirty(node);
125124
}
126125

@@ -794,8 +793,8 @@ static void setPosition(CSSNode* node, CSSDirection direction) {
794793
static void layoutNodeImpl(CSSNode* node, float availableWidth, float availableHeight,
795794
CSSDirection parentDirection, CSSMeasureMode widthMeasureMode, CSSMeasureMode heightMeasureMode, bool performLayout) {
796795

797-
assert(isUndefined(availableWidth) ? widthMeasureMode == CSSMeasureModeUndefined : true); // availableWidth is indefinite so widthMeasureMode must be CSSMeasureModeUndefined
798-
assert(isUndefined(availableHeight) ? heightMeasureMode == CSSMeasureModeUndefined : true); // availableHeight is indefinite so heightMeasureMode must be CSSMeasureModeUndefined
796+
CSS_ASSERT(isUndefined(availableWidth) ? widthMeasureMode == CSSMeasureModeUndefined : true, "availableWidth is indefinite so widthMeasureMode must be CSSMeasureModeUndefined");
797+
CSS_ASSERT(isUndefined(availableHeight) ? heightMeasureMode == CSSMeasureModeUndefined : true, "availableHeight is indefinite so heightMeasureMode must be CSSMeasureModeUndefined");
799798

800799
float paddingAndBorderAxisRow = getPaddingAndBorderAxis(node, CSSFlexDirectionRow);
801800
float paddingAndBorderAxisColumn = getPaddingAndBorderAxis(node, CSSFlexDirectionColumn);

React/CSSLayout/CSSMacros.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,17 @@
1616
# define CSS_EXTERN_C_BEGIN
1717
# define CSS_EXTERN_C_END
1818
#endif
19+
20+
#ifndef FB_ASSERTIONS_ENABLED
21+
#define FB_ASSERTIONS_ENABLED 1
22+
#endif
23+
24+
#if !(FB_ASSERTIONS_ENABLED)
25+
#define abort()
26+
#endif
27+
28+
#define CSS_ASSERT(X, message) \
29+
if (!(X)) { \
30+
fprintf(stderr, "%s\n", message); \
31+
abort(); \
32+
}

React/CSSLayout/CSSNodeList.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ struct CSSNodeList {
1717

1818
CSSNodeListRef CSSNodeListNew(uint32_t initialCapacity) {
1919
CSSNodeListRef list = malloc(sizeof(struct CSSNodeList));
20-
assert(list != NULL);
20+
CSS_ASSERT(list != NULL, "Could not allocate memory for list");
2121

2222
list->capacity = initialCapacity;
2323
list->count = 0;
2424
list->items = malloc(sizeof(void*) * list->capacity);
25-
assert(list->items != NULL);
25+
CSS_ASSERT(list->items != NULL, "Could not allocate memory for items");
2626

2727
return list;
2828
}
@@ -43,7 +43,7 @@ void CSSNodeListInsert(CSSNodeListRef list, CSSNodeRef node, uint32_t index) {
4343
if (list->count == list->capacity) {
4444
list->capacity *= 2;
4545
list->items = realloc(list->items, sizeof(void*) * list->capacity);
46-
assert(list->items != NULL);
46+
CSS_ASSERT(list->items != NULL, "Could not extend allocation for items");
4747
}
4848

4949
for (uint32_t i = list->count; i > index; i--) {

React/CSSLayout/CSSNodeList.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <stdint.h>
1616

1717
#include <CSSLayout/CSSLayout.h>
18+
#include <CSSLayout/CSSMacros.h>
1819

1920
CSS_EXTERN_C_BEGIN
2021

0 commit comments

Comments
 (0)