Skip to content

Commit 25f8b86

Browse files
rubennortefacebook-github-bot
authored andcommitted
Replace existing usages of shadowNodeFromValue with new bridging function (#51676)
Summary: Pull Request resolved: #51676 Changelog: [internal] This replaces all existing usages of `shadowNodeFromValue` (previously defined in `primitives.h`) to use the new bridging method to automatically convert `ShadowNode::Shared` from and to JS. It also deletes the old method. Reviewed By: javache Differential Revision: D75597543 fbshipit-source-id: ab4e307dad80c7507b7ebd1cfa6621d020473d89
1 parent 9c21489 commit 25f8b86

13 files changed

Lines changed: 105 additions & 113 deletions

File tree

packages/react-native/ReactCommon/react/nativemodule/dom/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ target_link_libraries(react_nativemodule_dom
2121
rrc_root
2222
react_codegen_rncore
2323
react_cxxreact
24+
react_renderer_bridging
2425
react_renderer_dom
2526
react_renderer_uimanager
2627
)

packages/react-native/ReactCommon/react/nativemodule/dom/NativeDOM.cpp

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
*/
77

88
#include "NativeDOM.h"
9+
#include <react/renderer/bridging/bridging.h>
910
#include <react/renderer/components/root/RootShadowNode.h>
1011
#include <react/renderer/dom/DOM.h>
1112
#include <react/renderer/uimanager/PointerEventsProcessor.h>
1213
#include <react/renderer/uimanager/UIManagerBinding.h>
13-
#include <react/renderer/uimanager/primitives.h>
1414

1515
#ifdef RN_DISABLE_OSS_PLUGIN_HEADER
1616
#include "Plugins.h"
@@ -23,6 +23,14 @@ std::shared_ptr<facebook::react::TurboModule> NativeDOMModuleProvider(
2323

2424
namespace facebook::react {
2525

26+
namespace {
27+
inline ShadowNode::Shared getShadowNode(
28+
facebook::jsi::Runtime& runtime,
29+
jsi::Value& shadowNodeValue) {
30+
return Bridging<ShadowNode::Shared>::fromJs(runtime, shadowNodeValue);
31+
}
32+
} // namespace
33+
2634
#pragma mark - Private helpers
2735

2836
static UIManager& getUIManagerFromRuntime(facebook::jsi::Runtime& runtime) {
@@ -46,8 +54,7 @@ static RootShadowNode::Shared getCurrentShadowTreeRevision(
4654
}
4755

4856
return getCurrentShadowTreeRevision(
49-
runtime,
50-
shadowNodeFromValue(runtime, nativeNodeReference)->getSurfaceId());
57+
runtime, getShadowNode(runtime, nativeNodeReference)->getSurfaceId());
5158
}
5259

5360
static facebook::react::PointerEventsProcessor&
@@ -107,7 +114,7 @@ double NativeDOM::compareDocumentPosition(
107114
// Only the first is a document
108115
auto surfaceId = nativeNodeReference.asNumber();
109116
shadowNode = currentRevision;
110-
otherShadowNode = shadowNodeFromValue(rt, otherNativeNodeReference);
117+
otherShadowNode = getShadowNode(rt, otherNativeNodeReference);
111118

112119
if (isRootShadowNode(*otherShadowNode)) {
113120
// If the other is a root node, we just need to check if it is its
@@ -123,7 +130,7 @@ double NativeDOM::compareDocumentPosition(
123130
} else {
124131
// Only the second is a document
125132
auto otherSurfaceId = otherNativeNodeReference.asNumber();
126-
shadowNode = shadowNodeFromValue(rt, nativeNodeReference);
133+
shadowNode = getShadowNode(rt, nativeNodeReference);
127134
otherShadowNode = getCurrentShadowTreeRevision(rt, otherSurfaceId);
128135

129136
if (isRootShadowNode(*shadowNode)) {
@@ -138,8 +145,8 @@ double NativeDOM::compareDocumentPosition(
138145
}
139146
}
140147
} else {
141-
shadowNode = shadowNodeFromValue(rt, nativeNodeReference);
142-
otherShadowNode = shadowNodeFromValue(rt, otherNativeNodeReference);
148+
shadowNode = getShadowNode(rt, nativeNodeReference);
149+
otherShadowNode = getShadowNode(rt, otherNativeNodeReference);
143150
}
144151

145152
return dom::compareDocumentPosition(
@@ -160,7 +167,7 @@ std::vector<jsi::Value> NativeDOM::getChildNodes(
160167
}
161168

162169
auto childNodes = dom::getChildNodes(
163-
currentRevision, *shadowNodeFromValue(rt, nativeNodeReference));
170+
currentRevision, *getShadowNode(rt, nativeNodeReference));
164171
return getArrayOfInstanceHandlesFromShadowNodes(childNodes, rt);
165172
}
166173

@@ -172,7 +179,7 @@ jsi::Value NativeDOM::getParentNode(
172179
return jsi::Value::undefined();
173180
}
174181

175-
auto shadowNode = shadowNodeFromValue(rt, nativeNodeReference);
182+
auto shadowNode = getShadowNode(rt, nativeNodeReference);
176183
if (isRootShadowNode(*shadowNode)) {
177184
// The parent of the root node is the document.
178185
return jsi::Value{shadowNode->getSurfaceId()};
@@ -203,7 +210,7 @@ bool NativeDOM::isConnected(jsi::Runtime& rt, jsi::Value nativeNodeReference) {
203210
return true;
204211
}
205212

206-
auto shadowNode = shadowNodeFromValue(rt, nativeNodeReference);
213+
auto shadowNode = getShadowNode(rt, nativeNodeReference);
207214
return dom::isConnected(currentRevision, *shadowNode);
208215
}
209216

@@ -214,8 +221,7 @@ std::tuple<
214221
/* rightWidth: */ int,
215222
/* bottomWidth: */ int,
216223
/* leftWidth: */ int>
217-
NativeDOM::getBorderWidth(jsi::Runtime& rt, jsi::Value nativeElementReference) {
218-
auto shadowNode = shadowNodeFromValue(rt, nativeElementReference);
224+
NativeDOM::getBorderWidth(jsi::Runtime& rt, ShadowNode::Shared shadowNode) {
219225
auto currentRevision =
220226
getCurrentShadowTreeRevision(rt, shadowNode->getSurfaceId());
221227
if (currentRevision == nullptr) {
@@ -234,9 +240,8 @@ std::tuple<
234240
/* height: */ double>
235241
NativeDOM::getBoundingClientRect(
236242
jsi::Runtime& rt,
237-
jsi::Value nativeElementReference,
243+
ShadowNode::Shared shadowNode,
238244
bool includeTransform) {
239-
auto shadowNode = shadowNodeFromValue(rt, nativeElementReference);
240245
auto currentRevision =
241246
getCurrentShadowTreeRevision(rt, shadowNode->getSurfaceId());
242247
if (currentRevision == nullptr) {
@@ -251,8 +256,7 @@ NativeDOM::getBoundingClientRect(
251256

252257
std::tuple</* width: */ int, /* height: */ int> NativeDOM::getInnerSize(
253258
jsi::Runtime& rt,
254-
jsi::Value nativeElementReference) {
255-
auto shadowNode = shadowNodeFromValue(rt, nativeElementReference);
259+
ShadowNode::Shared shadowNode) {
256260
auto currentRevision =
257261
getCurrentShadowTreeRevision(rt, shadowNode->getSurfaceId());
258262
if (currentRevision == nullptr) {
@@ -264,10 +268,7 @@ std::tuple</* width: */ int, /* height: */ int> NativeDOM::getInnerSize(
264268
}
265269

266270
std::tuple</* scrollLeft: */ double, /* scrollTop: */ double>
267-
NativeDOM::getScrollPosition(
268-
jsi::Runtime& rt,
269-
jsi::Value nativeElementReference) {
270-
auto shadowNode = shadowNodeFromValue(rt, nativeElementReference);
271+
NativeDOM::getScrollPosition(jsi::Runtime& rt, ShadowNode::Shared shadowNode) {
271272
auto currentRevision =
272273
getCurrentShadowTreeRevision(rt, shadowNode->getSurfaceId());
273274
if (currentRevision == nullptr) {
@@ -279,8 +280,7 @@ NativeDOM::getScrollPosition(
279280
}
280281

281282
std::tuple</* scrollWidth: */ int, /* scrollHeight */ int>
282-
NativeDOM::getScrollSize(jsi::Runtime& rt, jsi::Value nativeElementReference) {
283-
auto shadowNode = shadowNodeFromValue(rt, nativeElementReference);
283+
NativeDOM::getScrollSize(jsi::Runtime& rt, ShadowNode::Shared shadowNode) {
284284
auto currentRevision =
285285
getCurrentShadowTreeRevision(rt, shadowNode->getSurfaceId());
286286
if (currentRevision == nullptr) {
@@ -293,15 +293,13 @@ NativeDOM::getScrollSize(jsi::Runtime& rt, jsi::Value nativeElementReference) {
293293

294294
std::string NativeDOM::getTagName(
295295
jsi::Runtime& rt,
296-
jsi::Value nativeElementReference) {
297-
auto shadowNode = shadowNodeFromValue(rt, nativeElementReference);
296+
ShadowNode::Shared shadowNode) {
298297
return dom::getTagName(*shadowNode);
299298
}
300299

301300
std::string NativeDOM::getTextContent(
302301
jsi::Runtime& rt,
303-
jsi::Value nativeNodeReference) {
304-
auto shadowNode = shadowNodeFromValue(rt, nativeNodeReference);
302+
ShadowNode::Shared shadowNode) {
305303
auto currentRevision =
306304
getCurrentShadowTreeRevision(rt, shadowNode->getSurfaceId());
307305
if (currentRevision == nullptr) {
@@ -313,30 +311,27 @@ std::string NativeDOM::getTextContent(
313311

314312
bool NativeDOM::hasPointerCapture(
315313
jsi::Runtime& rt,
316-
jsi::Value nativeElementReference,
314+
ShadowNode::Shared shadowNode,
317315
double pointerId) {
318316
bool isCapturing = getPointerEventsProcessorFromRuntime(rt).hasPointerCapture(
319-
static_cast<PointerIdentifier>(pointerId),
320-
shadowNodeFromValue(rt, nativeElementReference).get());
317+
static_cast<PointerIdentifier>(pointerId), shadowNode.get());
321318
return isCapturing;
322319
}
323320

324321
void NativeDOM::releasePointerCapture(
325322
jsi::Runtime& rt,
326-
jsi::Value nativeElementReference,
323+
ShadowNode::Shared shadowNode,
327324
double pointerId) {
328325
getPointerEventsProcessorFromRuntime(rt).releasePointerCapture(
329-
static_cast<PointerIdentifier>(pointerId),
330-
shadowNodeFromValue(rt, nativeElementReference).get());
326+
static_cast<PointerIdentifier>(pointerId), shadowNode.get());
331327
}
332328

333329
void NativeDOM::setPointerCapture(
334330
jsi::Runtime& rt,
335-
jsi::Value nativeElementReference,
331+
ShadowNode::Shared shadowNode,
336332
double pointerId) {
337333
getPointerEventsProcessorFromRuntime(rt).setPointerCapture(
338-
static_cast<PointerIdentifier>(pointerId),
339-
shadowNodeFromValue(rt, nativeElementReference));
334+
static_cast<PointerIdentifier>(pointerId), shadowNode);
340335
}
341336

342337
#pragma mark - Methods from the `HTMLElement` interface (for `ReactNativeElement`).
@@ -345,8 +340,7 @@ std::tuple<
345340
/* offsetParent: */ jsi::Value,
346341
/* top: */ double,
347342
/* left: */ double>
348-
NativeDOM::getOffset(jsi::Runtime& rt, jsi::Value nativeElementReference) {
349-
auto shadowNode = shadowNodeFromValue(rt, nativeElementReference);
343+
NativeDOM::getOffset(jsi::Runtime& rt, ShadowNode::Shared shadowNode) {
350344
auto currentRevision =
351345
getCurrentShadowTreeRevision(rt, shadowNode->getSurfaceId());
352346
if (currentRevision == nullptr) {
@@ -378,16 +372,15 @@ jsi::Value NativeDOM::linkRootNode(
378372
std::make_shared<InstanceHandle>(rt, instanceHandle, surfaceId);
379373
currentRevision->setInstanceHandle(instanceHandleWrapper);
380374

381-
return valueFromShadowNode(rt, currentRevision);
375+
return Bridging<ShadowNode::Shared>::toJs(rt, currentRevision);
382376
}
383377

384378
#pragma mark - Legacy layout APIs (for `ReactNativeElement`).
385379

386380
void NativeDOM::measure(
387381
jsi::Runtime& rt,
388-
jsi::Value nativeElementReference,
382+
ShadowNode::Shared shadowNode,
389383
jsi::Function callback) {
390-
auto shadowNode = shadowNodeFromValue(rt, nativeElementReference);
391384
auto currentRevision =
392385
getCurrentShadowTreeRevision(rt, shadowNode->getSurfaceId());
393386
if (currentRevision == nullptr) {
@@ -409,9 +402,8 @@ void NativeDOM::measure(
409402

410403
void NativeDOM::measureInWindow(
411404
jsi::Runtime& rt,
412-
jsi::Value nativeElementReference,
405+
ShadowNode::Shared shadowNode,
413406
jsi::Function callback) {
414-
auto shadowNode = shadowNodeFromValue(rt, nativeElementReference);
415407
auto currentRevision =
416408
getCurrentShadowTreeRevision(rt, shadowNode->getSurfaceId());
417409
if (currentRevision == nullptr) {
@@ -430,13 +422,10 @@ void NativeDOM::measureInWindow(
430422

431423
void NativeDOM::measureLayout(
432424
jsi::Runtime& rt,
433-
jsi::Value nativeElementReference,
434-
jsi::Value relativeToNativeElementReference,
425+
ShadowNode::Shared shadowNode,
426+
ShadowNode::Shared relativeToShadowNode,
435427
jsi::Function onFail,
436428
jsi::Function onSuccess) {
437-
auto shadowNode = shadowNodeFromValue(rt, nativeElementReference);
438-
auto relativeToShadowNode =
439-
shadowNodeFromValue(rt, relativeToNativeElementReference);
440429
auto currentRevision =
441430
getCurrentShadowTreeRevision(rt, shadowNode->getSurfaceId());
442431
if (currentRevision == nullptr) {
@@ -466,11 +455,10 @@ void NativeDOM::measureLayout(
466455

467456
void NativeDOM::setNativeProps(
468457
jsi::Runtime& rt,
469-
jsi::Value nativeElementReference,
458+
ShadowNode::Shared shadowNode,
470459
jsi::Value updatePayload) {
471460
getUIManagerFromRuntime(rt).setNativeProps_DEPRECATED(
472-
shadowNodeFromValue(rt, nativeElementReference),
473-
RawProps(rt, updatePayload));
461+
shadowNode, RawProps(rt, updatePayload));
474462
}
475463

476464
} // namespace facebook::react

packages/react-native/ReactCommon/react/nativemodule/dom/NativeDOM.h

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <FBReactNativeSpec/FBReactNativeSpecJSI.h>
1818
#endif
1919

20+
#include <react/renderer/bridging/bridging.h>
21+
#include <react/renderer/core/ShadowNode.h>
2022
#include <react/renderer/core/ShadowNodeFamily.h>
2123

2224
namespace facebook::react {
@@ -47,7 +49,7 @@ class NativeDOM : public NativeDOMCxxSpec<NativeDOM> {
4749
/* rightWidth: */ int,
4850
/* bottomWidth: */ int,
4951
/* leftWidth: */ int>
50-
getBorderWidth(jsi::Runtime& rt, jsi::Value nativeElementReference);
52+
getBorderWidth(jsi::Runtime& rt, ShadowNode::Shared shadowNode);
5153

5254
std::tuple<
5355
/* x: */ double,
@@ -56,37 +58,37 @@ class NativeDOM : public NativeDOMCxxSpec<NativeDOM> {
5658
/* height: */ double>
5759
getBoundingClientRect(
5860
jsi::Runtime& rt,
59-
jsi::Value nativeElementReference,
61+
ShadowNode::Shared shadowNode,
6062
bool includeTransform);
6163

6264
std::tuple</* width: */ int, /* height: */ int> getInnerSize(
6365
jsi::Runtime& rt,
64-
jsi::Value nativeElementReference);
66+
ShadowNode::Shared shadowNode);
6567

6668
std::tuple</* scrollLeft: */ double, /* scrollTop: */ double>
67-
getScrollPosition(jsi::Runtime& rt, jsi::Value nativeElementReference);
69+
getScrollPosition(jsi::Runtime& rt, ShadowNode::Shared shadowNode);
6870

6971
std::tuple</* scrollWidth: */ int, /* scrollHeight */ int> getScrollSize(
7072
jsi::Runtime& rt,
71-
jsi::Value nativeElementReference);
73+
ShadowNode::Shared shadowNode);
7274

73-
std::string getTagName(jsi::Runtime& rt, jsi::Value nativeElementReference);
75+
std::string getTagName(jsi::Runtime& rt, ShadowNode::Shared shadowNode);
7476

75-
std::string getTextContent(jsi::Runtime& rt, jsi::Value nativeNodeReference);
77+
std::string getTextContent(jsi::Runtime& rt, ShadowNode::Shared shadowNode);
7678

7779
bool hasPointerCapture(
7880
jsi::Runtime& rt,
79-
jsi::Value nativeElementReference,
81+
ShadowNode::Shared shadowNode,
8082
double pointerId);
8183

8284
void releasePointerCapture(
8385
jsi::Runtime& rt,
84-
jsi::Value nativeElementReference,
86+
ShadowNode::Shared shadowNode,
8587
double pointerId);
8688

8789
void setPointerCapture(
8890
jsi::Runtime& rt,
89-
jsi::Value nativeElementReference,
91+
ShadowNode::Shared shadowNode,
9092
double pointerId);
9193

9294
#pragma mark - Methods from the `HTMLElement` interface (for `ReactNativeElement`).
@@ -95,7 +97,7 @@ class NativeDOM : public NativeDOMCxxSpec<NativeDOM> {
9597
/* offsetParent: */ jsi::Value,
9698
/* top: */ double,
9799
/* left: */ double>
98-
getOffset(jsi::Runtime& rt, jsi::Value nativeElementReference);
100+
getOffset(jsi::Runtime& rt, ShadowNode::Shared shadowNode);
99101

100102
#pragma mark - Special methods to handle the root node.
101103

@@ -108,26 +110,26 @@ class NativeDOM : public NativeDOMCxxSpec<NativeDOM> {
108110

109111
void measure(
110112
jsi::Runtime& rt,
111-
jsi::Value nativeElementReference,
113+
ShadowNode::Shared shadowNode,
112114
jsi::Function callback);
113115

114116
void measureInWindow(
115117
jsi::Runtime& rt,
116-
jsi::Value nativeElementReference,
118+
ShadowNode::Shared shadowNode,
117119
jsi::Function callback);
118120

119121
void measureLayout(
120122
jsi::Runtime& rt,
121-
jsi::Value nativeElementReference,
122-
jsi::Value relativeToNativeElementReference,
123+
ShadowNode::Shared shadowNode,
124+
ShadowNode::Shared relativeToShadowNode,
123125
jsi::Function onFail,
124126
jsi::Function onSuccess);
125127

126128
#pragma mark - Legacy direct manipulation APIs (for `ReactNativeElement`).
127129

128130
void setNativeProps(
129131
jsi::Runtime& rt,
130-
jsi::Value nativeElementReference,
132+
ShadowNode::Shared shadowNode,
131133
jsi::Value updatePayload);
132134
};
133135

packages/react-native/ReactCommon/react/nativemodule/dom/React-domnativemodule.podspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Pod::Spec.new do |s|
5454
s.dependency "Yoga"
5555
s.dependency "ReactCommon/turbomodule/core"
5656
s.dependency "React-Fabric"
57+
s.dependency "React-Fabric/bridging"
5758
s.dependency "React-FabricComponents"
5859
add_dependency(s, "React-runtimeexecutor", :additional_framework_paths => ["platform/ios"])
5960
add_dependency(s, "React-graphics", :additional_framework_paths => ["react/renderer/graphics/platform/ios"])

0 commit comments

Comments
 (0)