Skip to content

Commit f422ddc

Browse files
authored
[RN] Sync Android touch payload and TurboModule source updates (#59622)
Synchronise Android touch payload field constants and their JNI and test-shadow implementations. Use designated initialisers for cached TurboModule method metadata. Restore missing source updates while preserving recently merged pull requests. Changelog: [Internal] fbshipit-source-id: f652b5906bf2419f0cedca9711c225a6f53cf054
1 parent 7c609bd commit f422ddc

4 files changed

Lines changed: 69 additions & 48 deletions

File tree

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/events/TouchesHelper.kt‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,16 @@ internal object TouchesHelper {
2323
ReactNativeJNISoLoader.staticInit()
2424
}
2525

26-
// Values per pointer in the data passed to nativeCreateTouchPayload.
27-
private const val POINTER_FIELDS = 8
26+
// Keep in sync with the field indices in TouchesHelper.cpp.
27+
const val PAGE_X_INDEX = 0
28+
const val PAGE_Y_INDEX = 1
29+
const val LOCATION_X_INDEX = 2
30+
const val LOCATION_Y_INDEX = 3
31+
const val TARGET_SURFACE_INDEX = 4
32+
const val TARGET_INDEX = 5
33+
const val TIMESTAMP_INDEX = 6
34+
const val IDENTIFIER_INDEX = 7
35+
const val POINTER_FIELDS = 8
2836

2937
/**
3038
* Fills the pointer data of [nativeCreateTouchPayload]: for each pointer of the
@@ -46,24 +54,24 @@ internal object TouchesHelper {
4654
val targetViewCoordinateY = motionEvent.y - event.viewY
4755

4856
for (index in 0 until motionEvent.pointerCount) {
49-
var field = index * POINTER_FIELDS
57+
val field = index * POINTER_FIELDS
5058

5159
// pageX,Y values are relative to the RootReactView
5260
// the motionEvent already contains coordinates in that view
53-
pointerData[field++] = motionEvent.getX(index).pxToDp().toDouble()
54-
pointerData[field++] = motionEvent.getY(index).pxToDp().toDouble()
61+
pointerData[field + PAGE_X_INDEX] = motionEvent.getX(index).pxToDp().toDouble()
62+
pointerData[field + PAGE_Y_INDEX] = motionEvent.getY(index).pxToDp().toDouble()
5563

5664
// locationX,Y values are relative to the target view
5765
// To compute the values for the view, we subtract that views location from the event X,Y
5866
val locationX = motionEvent.getX(index) - targetViewCoordinateX
5967
val locationY = motionEvent.getY(index) - targetViewCoordinateY
60-
pointerData[field++] = locationX.pxToDp().toDouble()
61-
pointerData[field++] = locationY.pxToDp().toDouble()
68+
pointerData[field + LOCATION_X_INDEX] = locationX.pxToDp().toDouble()
69+
pointerData[field + LOCATION_Y_INDEX] = locationY.pxToDp().toDouble()
6270

63-
pointerData[field++] = event.surfaceId.toDouble()
64-
pointerData[field++] = event.viewTag.toDouble()
65-
pointerData[field++] = event.timestampMs.toDouble()
66-
pointerData[field] = motionEvent.getPointerId(index).toDouble()
71+
pointerData[field + TARGET_SURFACE_INDEX] = event.surfaceId.toDouble()
72+
pointerData[field + TARGET_INDEX] = event.viewTag.toDouble()
73+
pointerData[field + TIMESTAMP_INDEX] = event.timestampMs.toDouble()
74+
pointerData[field + IDENTIFIER_INDEX] = motionEvent.getPointerId(index).toDouble()
6775
}
6876

6977
return pointerData

‎packages/react-native/ReactAndroid/src/main/jni/react/jni/TouchesHelper.cpp‎

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,29 @@ namespace facebook::react {
1818

1919
namespace {
2020

21-
// Must match TouchesHelper.POINTER_FIELDS and the order documented on
22-
// TouchesHelper.nativeCreateTouchPayload.
21+
// Keep in sync with the field indices in TouchesHelper.kt.
22+
constexpr jint kPageXIndex = 0;
23+
constexpr jint kPageYIndex = 1;
24+
constexpr jint kLocationXIndex = 2;
25+
constexpr jint kLocationYIndex = 3;
26+
constexpr jint kTargetSurfaceIndex = 4;
27+
constexpr jint kTargetIndex = 5;
28+
constexpr jint kTimestampIndex = 6;
29+
constexpr jint kIdentifierIndex = 7;
2330
constexpr jint kPointerFields = 8;
2431

2532
folly::dynamic createTouch(const double* fields) {
2633
folly::dynamic touch = folly::dynamic::object();
27-
touch.insert("pageX", fields[0]);
28-
touch.insert("pageY", fields[1]);
29-
touch.insert("locationX", fields[2]);
30-
touch.insert("locationY", fields[3]);
31-
touch.insert("targetSurface", static_cast<int>(fields[4]));
32-
touch.insert("target", static_cast<int>(fields[5]));
33-
touch.insert("timestamp", fields[6]);
34-
touch.insert("identifier", fields[7]);
34+
// NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic)
35+
touch.insert("pageX", fields[kPageXIndex]);
36+
touch.insert("pageY", fields[kPageYIndex]);
37+
touch.insert("locationX", fields[kLocationXIndex]);
38+
touch.insert("locationY", fields[kLocationYIndex]);
39+
touch.insert("targetSurface", static_cast<int>(fields[kTargetSurfaceIndex]));
40+
touch.insert("target", static_cast<int>(fields[kTargetIndex]));
41+
touch.insert("timestamp", fields[kTimestampIndex]);
42+
touch.insert("identifier", fields[kIdentifierIndex]);
43+
// NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
3544
return touch;
3645
}
3746

@@ -53,6 +62,7 @@ local_ref<WritableNativeMap::jhybridobject> createTouchPayload(
5362
folly::dynamic changedTouches = folly::dynamic::array();
5463
folly::dynamic touches = folly::dynamic::array();
5564
for (jint index = 0; index < pointerCount; index++) {
65+
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
5666
const double* fields = values.get() + index * kPointerFields;
5767
if ((changedPointers & (1 << index)) != 0) {
5868
changedTouches.push_back(createTouch(fields));
@@ -64,15 +74,18 @@ local_ref<WritableNativeMap::jhybridobject> createTouchPayload(
6474

6575
auto jPayload = WritableNativeMap::newObjectJavaArgs();
6676
auto* payload = jPayload->cthis();
77+
// NOLINTBEGIN(cppcoreguidelines-pro-bounds-pointer-arithmetic)
6778
const double* fields = values.get() + eventPointer * kPointerFields;
68-
payload->putDouble("pageX", fields[0]);
69-
payload->putDouble("pageY", fields[1]);
70-
payload->putDouble("locationX", fields[2]);
71-
payload->putDouble("locationY", fields[3]);
72-
payload->putInt("targetSurface", static_cast<int>(fields[4]));
73-
payload->putInt("target", static_cast<int>(fields[5]));
74-
payload->putDouble("timestamp", fields[6]);
75-
payload->putDouble("identifier", fields[7]);
79+
payload->putDouble("pageX", fields[kPageXIndex]);
80+
payload->putDouble("pageY", fields[kPageYIndex]);
81+
payload->putDouble("locationX", fields[kLocationXIndex]);
82+
payload->putDouble("locationY", fields[kLocationYIndex]);
83+
payload->putInt(
84+
"targetSurface", static_cast<int>(fields[kTargetSurfaceIndex]));
85+
payload->putInt("target", static_cast<int>(fields[kTargetIndex]));
86+
payload->putDouble("timestamp", fields[kTimestampIndex]);
87+
payload->putDouble("identifier", fields[kIdentifierIndex]);
88+
// NOLINTEND(cppcoreguidelines-pro-bounds-pointer-arithmetic)
7689
WritableNativeArray changedTouchesArray(std::move(changedTouches));
7790
payload->putNativeArray("changedTouches", &changedTouchesArray);
7891
WritableNativeArray touchesArray(std::move(touches));

‎packages/react-native/ReactAndroid/src/test/java/com/facebook/testutils/shadows/ShadowTouchesHelper.kt‎

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import org.robolectric.annotation.Implements
1919
class ShadowTouchesHelper {
2020

2121
companion object {
22-
private const val POINTER_FIELDS = 8
2322

2423
/** Builds the same payload as the native TouchesHelper::createTouchPayload. */
2524
@JvmStatic
@@ -30,7 +29,7 @@ class ShadowTouchesHelper {
3029
changedPointers: Int,
3130
touchPointers: Int,
3231
): WritableNativeMap {
33-
val pointerCount = pointerData.size / POINTER_FIELDS
32+
val pointerCount = pointerData.size / TouchesHelper.POINTER_FIELDS
3433
val changedTouches = JavaOnlyArray()
3534
val touches = JavaOnlyArray()
3635
for (index in 0 until pointerCount) {
@@ -49,15 +48,15 @@ class ShadowTouchesHelper {
4948
}
5049

5150
private fun putTouch(touch: WritableMap, pointerData: DoubleArray, index: Int) {
52-
val field = index * POINTER_FIELDS
53-
touch.putDouble("pageX", pointerData[field])
54-
touch.putDouble("pageY", pointerData[field + 1])
55-
touch.putDouble("locationX", pointerData[field + 2])
56-
touch.putDouble("locationY", pointerData[field + 3])
57-
touch.putInt("targetSurface", pointerData[field + 4].toInt())
58-
touch.putInt("target", pointerData[field + 5].toInt())
59-
touch.putDouble("timestamp", pointerData[field + 6])
60-
touch.putDouble("identifier", pointerData[field + 7])
51+
val field = index * TouchesHelper.POINTER_FIELDS
52+
touch.putDouble("pageX", pointerData[field + TouchesHelper.PAGE_X_INDEX])
53+
touch.putDouble("pageY", pointerData[field + TouchesHelper.PAGE_Y_INDEX])
54+
touch.putDouble("locationX", pointerData[field + TouchesHelper.LOCATION_X_INDEX])
55+
touch.putDouble("locationY", pointerData[field + TouchesHelper.LOCATION_Y_INDEX])
56+
touch.putInt("targetSurface", pointerData[field + TouchesHelper.TARGET_SURFACE_INDEX].toInt())
57+
touch.putInt("target", pointerData[field + TouchesHelper.TARGET_INDEX].toInt())
58+
touch.putDouble("timestamp", pointerData[field + TouchesHelper.TIMESTAMP_INDEX])
59+
touch.putDouble("identifier", pointerData[field + TouchesHelper.IDENTIFIER_INDEX])
6160
}
6261
}
6362
}

‎packages/react-native/ReactCommon/react/nativemodule/core/platform/android/ReactCommon/JavaTurboModule.cpp‎

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -594,14 +594,15 @@ const JavaTurboModule::MethodInfo& JavaTurboModule::getMethodInfo(
594594
std::lock_guard<std::mutex> lock(methodInfosMutex_);
595595
auto entry = methodInfos_.find(methodID);
596596
if (entry == methodInfos_.end()) {
597-
entry = methodInfos_
598-
.emplace(
599-
methodID,
600-
MethodInfo{
601-
getMethodArgTypesFromSignature(methodSignature),
602-
methodSignature.substr(
603-
methodSignature.find_last_of(')') + 1)})
604-
.first;
597+
entry =
598+
methodInfos_
599+
.emplace(
600+
methodID,
601+
MethodInfo{
602+
.argTypes = getMethodArgTypesFromSignature(methodSignature),
603+
.returnType = methodSignature.substr(
604+
methodSignature.find_last_of(')') + 1)})
605+
.first;
605606
}
606607
return entry->second;
607608
}

0 commit comments

Comments
 (0)