-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathRNGHRuntimeDecorator.cpp
More file actions
133 lines (110 loc) · 4.27 KB
/
Copy pathRNGHRuntimeDecorator.cpp
File metadata and controls
133 lines (110 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#ifndef ANDROID
#include <react/renderer/components/text/ParagraphShadowNode.h>
#include <react/renderer/components/text/TextShadowNode.h>
#endif
#include <react/renderer/uimanager/primitives.h>
#include "RNGHRuntimeDecorator.h"
namespace gesturehandler {
using namespace facebook;
using namespace facebook::react;
void RNGHRuntimeDecorator::installRNRuntimeBindings(
jsi::Runtime &rnRuntime,
int moduleId,
std::function<void(int, int)> &&setGestureState) {
const auto isViewFlatteningDisabled = jsi::Function::createFromHostFunction(
rnRuntime,
jsi::PropNameID::forAscii(rnRuntime, "_isViewFlatteningDisabled"),
1,
[](jsi::Runtime &runtime,
const jsi::Value &,
const jsi::Value *args,
size_t argumentCount) -> jsi::Value {
if (!args[0].isObject()) {
return jsi::Value::null();
}
#if REACT_NATIVE_MINOR_VERSION >= 81
auto shadowNode = Bridging<std::shared_ptr<const ShadowNode>>::fromJs(
runtime, args[0]);
#else
auto shadowNode = shadowNodeFromValue(runtime, args[0]);
#endif
#ifndef ANDROID
if (dynamic_pointer_cast<const ParagraphShadowNode>(shadowNode)) {
return jsi::Value(true);
}
if (dynamic_pointer_cast<const TextShadowNode>(shadowNode)) {
return jsi::Value(true);
}
#endif
const auto isFormsStackingContext = shadowNode->getTraits().check(
ShadowNodeTraits::FormsStackingContext);
// This is done using component names instead of type checking because
// of duplicate symbols for RN types, which prevent RTTI from working.
const auto &componentName = shadowNode->getComponentName();
const auto isTextOrParagraphComponent =
strcmp(componentName, "Paragraph") == 0 ||
strcmp(componentName, "Text") == 0;
return jsi::Value(isFormsStackingContext || isTextOrParagraphComponent);
});
rnRuntime.global().setProperty(
rnRuntime,
"_isViewFlatteningDisabled",
std::move(isViewFlatteningDisabled));
auto setGestureStateAsync = jsi::Function::createFromHostFunction(
rnRuntime,
jsi::PropNameID::forAscii(rnRuntime, "_setGestureStateAsync"),
2,
[setGestureState](
jsi::Runtime &rt,
const jsi::Value &,
const jsi::Value *args,
size_t argumentCount) -> jsi::Value {
if (argumentCount == 2) {
const auto handlerTag = static_cast<int>(args[0].asNumber());
const auto state = static_cast<int>(args[1].asNumber());
setGestureState(handlerTag, state);
}
return jsi::Value::undefined();
});
rnRuntime.global().setProperty(
rnRuntime, "_setGestureStateAsync", std::move(setGestureStateAsync));
auto moduleIdValue = jsi::Value(moduleId);
rnRuntime.global().setProperty(
rnRuntime, "_RNGH_MODULE_ID", std::move(moduleIdValue));
}
void RNGHRuntimeDecorator::installUIRuntimeBindings(
jsi::Runtime &uiRuntime,
std::function<void(int, int)> &&setGestureState) {
auto setGestureStateSync = jsi::Function::createFromHostFunction(
uiRuntime,
jsi::PropNameID::forAscii(uiRuntime, "_setGestureStateSync"),
2,
[setGestureState](
jsi::Runtime &rt,
const jsi::Value &,
const jsi::Value *args,
size_t argumentCount) -> jsi::Value {
if (argumentCount == 2) {
const auto handlerTag = static_cast<int>(args[0].asNumber());
const auto state = static_cast<int>(args[1].asNumber());
setGestureState(handlerTag, state);
}
return jsi::Value::undefined();
});
uiRuntime.global().setProperty(
uiRuntime, "_setGestureStateSync", std::move(setGestureStateSync));
}
jsi::Runtime *RNGHRuntimeDecorator::tryFindUIRuntime(jsi::Runtime &rnRuntime) {
const auto runtimeHolder =
rnRuntime.global().getProperty(rnRuntime, "_WORKLET_RUNTIME");
if (runtimeHolder.isUndefined()) {
return nullptr;
}
const auto arrayBufferValue =
runtimeHolder.getObject(rnRuntime).getArrayBuffer(rnRuntime).data(
rnRuntime);
const auto uiRuntimeAddress =
reinterpret_cast<uintptr_t *>(&arrayBufferValue[0]);
return reinterpret_cast<jsi::Runtime *>(*uiRuntimeAddress);
}
} // namespace gesturehandler