yoga::bit_cast
Summary: X-link: https://github.com/facebook/react-native/pull/39358 This adds a function polyfilling C++ 20's `std::bit_cast`, using `memcpy()` to be safe with strict aliasing rules. This replaces the conditional code in CompactValue for type punning, an unsafe place in YGJNI where we do it unsafely, and is used in ValuePool. The polyfill can be switched to `std::bit_cast` whenever we adopt C++ 20. Note that this doesn't actually call into `memcpy()`, as verified by Godbolt. Compilers are aware of the memcpy type punning pattern and optimize it, but it's ugly and confusing to folks who haven't seen it before. Reviewed By: javache Differential Revision: D49082997 fbshipit-source-id: b848775a68286bdb11b2a3a95bef8069364ac9b5
This commit is contained in:
committed by
Facebook GitHub Bot
parent
f8e2bc0875
commit
26d2a2682f
@@ -16,6 +16,7 @@
|
||||
#include "YogaJniException.h"
|
||||
|
||||
#include <yoga/Yoga-internal.h>
|
||||
#include <yoga/bits/BitCast.h>
|
||||
|
||||
// TODO: Reconcile missing layoutContext functionality from callbacks in the C
|
||||
// API and use that
|
||||
@@ -677,11 +678,10 @@ static YGSize YGJNIMeasureFunc(
|
||||
uint32_t wBits = 0xFFFFFFFF & (measureResult >> 32);
|
||||
uint32_t hBits = 0xFFFFFFFF & measureResult;
|
||||
|
||||
// TODO: this is unsafe under strict aliasing and should use bit_cast
|
||||
const float* measuredWidth = reinterpret_cast<float*>(&wBits);
|
||||
const float* measuredHeight = reinterpret_cast<float*>(&hBits);
|
||||
const float measuredWidth = yoga::bit_cast<float>(wBits);
|
||||
const float measuredHeight = yoga::bit_cast<float>(hBits);
|
||||
|
||||
return YGSize{*measuredWidth, *measuredHeight};
|
||||
return YGSize{measuredWidth, measuredHeight};
|
||||
} else {
|
||||
return YGSize{
|
||||
widthMode == YGMeasureModeUndefined ? 0 : width,
|
||||
|
Reference in New Issue
Block a user