react-native-reanimated 3.0.0-rc.7 → 3.0.0-rc.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (177) hide show
  1. package/Common/cpp/AnimatedSensor/AnimatedSensorModule.cpp +32 -37
  2. package/Common/cpp/AnimatedSensor/AnimatedSensorModule.h +4 -3
  3. package/Common/cpp/LayoutAnimations/LayoutAnimationsManager.cpp +76 -0
  4. package/Common/cpp/LayoutAnimations/LayoutAnimationsManager.h +41 -0
  5. package/Common/cpp/NativeModules/NativeReanimatedModule.cpp +161 -165
  6. package/Common/cpp/NativeModules/NativeReanimatedModule.h +36 -34
  7. package/Common/cpp/NativeModules/NativeReanimatedModuleSpec.cpp +22 -34
  8. package/Common/cpp/NativeModules/NativeReanimatedModuleSpec.h +13 -14
  9. package/Common/cpp/SharedItems/RuntimeManager.h +1 -34
  10. package/Common/cpp/SharedItems/Shareables.cpp +142 -0
  11. package/Common/cpp/SharedItems/Shareables.h +455 -0
  12. package/Common/cpp/Tools/PlatformDepMethodsHolder.h +6 -0
  13. package/Common/cpp/Tools/RuntimeDecorator.cpp +54 -26
  14. package/Common/cpp/Tools/RuntimeDecorator.h +11 -2
  15. package/Common/cpp/Tools/Scheduler.cpp +10 -0
  16. package/Common/cpp/Tools/SingleInstanceChecker.h +1 -1
  17. package/android/src/main/cpp/LayoutAnimations.cpp +6 -16
  18. package/android/src/main/cpp/LayoutAnimations.h +4 -4
  19. package/android/src/main/cpp/NativeProxy.cpp +50 -63
  20. package/ios/native/NativeProxy.mm +26 -42
  21. package/lib/commonjs/createAnimatedComponent.js +7 -11
  22. package/lib/commonjs/createAnimatedComponent.js.map +1 -1
  23. package/lib/commonjs/reanimated2/NativeReanimated/NativeReanimated.js +24 -25
  24. package/lib/commonjs/reanimated2/NativeReanimated/NativeReanimated.js.map +1 -1
  25. package/lib/commonjs/reanimated2/ViewDescriptorsSet.js +27 -55
  26. package/lib/commonjs/reanimated2/ViewDescriptorsSet.js.map +1 -1
  27. package/lib/commonjs/reanimated2/WorkletEventHandler.js +5 -3
  28. package/lib/commonjs/reanimated2/WorkletEventHandler.js.map +1 -1
  29. package/lib/commonjs/reanimated2/animation/util.js.map +1 -1
  30. package/lib/commonjs/reanimated2/commonTypes.js +7 -0
  31. package/lib/commonjs/reanimated2/commonTypes.js.map +1 -1
  32. package/lib/commonjs/reanimated2/core.js +111 -209
  33. package/lib/commonjs/reanimated2/core.js.map +1 -1
  34. package/lib/commonjs/reanimated2/globals.d.js.map +1 -1
  35. package/lib/commonjs/reanimated2/hook/useAnimatedKeyboard.js +15 -9
  36. package/lib/commonjs/reanimated2/hook/useAnimatedKeyboard.js.map +1 -1
  37. package/lib/commonjs/reanimated2/hook/useAnimatedRef.js +18 -8
  38. package/lib/commonjs/reanimated2/hook/useAnimatedRef.js.map +1 -1
  39. package/lib/commonjs/reanimated2/hook/useAnimatedSensor.js +27 -36
  40. package/lib/commonjs/reanimated2/hook/useAnimatedSensor.js.map +1 -1
  41. package/lib/commonjs/reanimated2/hook/useAnimatedStyle.js +24 -50
  42. package/lib/commonjs/reanimated2/hook/useAnimatedStyle.js.map +1 -1
  43. package/lib/commonjs/reanimated2/hook/useSharedValue.js +3 -2
  44. package/lib/commonjs/reanimated2/hook/useSharedValue.js.map +1 -1
  45. package/lib/commonjs/reanimated2/hook/utils.js.map +1 -1
  46. package/lib/commonjs/reanimated2/interpolateColor.js.map +1 -1
  47. package/lib/commonjs/reanimated2/jestUtils.js +8 -33
  48. package/lib/commonjs/reanimated2/jestUtils.js.map +1 -1
  49. package/lib/commonjs/reanimated2/js-reanimated/JSReanimated.js +16 -100
  50. package/lib/commonjs/reanimated2/js-reanimated/JSReanimated.js.map +1 -1
  51. package/lib/commonjs/reanimated2/js-reanimated/index.js +5 -0
  52. package/lib/commonjs/reanimated2/js-reanimated/index.js.map +1 -1
  53. package/lib/commonjs/reanimated2/layoutReanimation/animationBuilder/commonTypes.js.map +1 -1
  54. package/lib/commonjs/reanimated2/layoutReanimation/animationBuilder/index.js.map +1 -1
  55. package/lib/commonjs/reanimated2/layoutReanimation/animationsManager.js +91 -0
  56. package/lib/commonjs/reanimated2/layoutReanimation/animationsManager.js.map +1 -0
  57. package/lib/commonjs/reanimated2/layoutReanimation/index.js +1 -1
  58. package/lib/commonjs/reanimated2/layoutReanimation/index.js.map +1 -1
  59. package/lib/commonjs/reanimated2/mappers.js +186 -0
  60. package/lib/commonjs/reanimated2/mappers.js.map +1 -0
  61. package/lib/commonjs/reanimated2/mutables.js +173 -0
  62. package/lib/commonjs/reanimated2/mutables.js.map +1 -0
  63. package/lib/commonjs/reanimated2/shareables.js +153 -0
  64. package/lib/commonjs/reanimated2/shareables.js.map +1 -0
  65. package/lib/commonjs/reanimated2/threads.js +58 -0
  66. package/lib/commonjs/reanimated2/threads.js.map +1 -0
  67. package/lib/commonjs/reanimated2/time.js +55 -0
  68. package/lib/commonjs/reanimated2/time.js.map +1 -0
  69. package/lib/commonjs/reanimated2/valueSetter.js +81 -0
  70. package/lib/commonjs/reanimated2/valueSetter.js.map +1 -0
  71. package/lib/module/createAnimatedComponent.js +7 -12
  72. package/lib/module/createAnimatedComponent.js.map +1 -1
  73. package/lib/module/reanimated2/NativeReanimated/NativeReanimated.js +24 -25
  74. package/lib/module/reanimated2/NativeReanimated/NativeReanimated.js.map +1 -1
  75. package/lib/module/reanimated2/ViewDescriptorsSet.js +27 -53
  76. package/lib/module/reanimated2/ViewDescriptorsSet.js.map +1 -1
  77. package/lib/module/reanimated2/WorkletEventHandler.js +4 -3
  78. package/lib/module/reanimated2/WorkletEventHandler.js.map +1 -1
  79. package/lib/module/reanimated2/animation/util.js.map +1 -1
  80. package/lib/module/reanimated2/commonTypes.js +7 -0
  81. package/lib/module/reanimated2/commonTypes.js.map +1 -1
  82. package/lib/module/reanimated2/core.js +66 -188
  83. package/lib/module/reanimated2/core.js.map +1 -1
  84. package/lib/module/reanimated2/globals.d.js.map +1 -1
  85. package/lib/module/reanimated2/hook/useAnimatedKeyboard.js +16 -6
  86. package/lib/module/reanimated2/hook/useAnimatedKeyboard.js.map +1 -1
  87. package/lib/module/reanimated2/hook/useAnimatedRef.js +17 -8
  88. package/lib/module/reanimated2/hook/useAnimatedRef.js.map +1 -1
  89. package/lib/module/reanimated2/hook/useAnimatedSensor.js +29 -34
  90. package/lib/module/reanimated2/hook/useAnimatedSensor.js.map +1 -1
  91. package/lib/module/reanimated2/hook/useAnimatedStyle.js +26 -52
  92. package/lib/module/reanimated2/hook/useAnimatedStyle.js.map +1 -1
  93. package/lib/module/reanimated2/hook/useSharedValue.js +3 -2
  94. package/lib/module/reanimated2/hook/useSharedValue.js.map +1 -1
  95. package/lib/module/reanimated2/hook/utils.js.map +1 -1
  96. package/lib/module/reanimated2/interpolateColor.js.map +1 -1
  97. package/lib/module/reanimated2/jestUtils.js +8 -31
  98. package/lib/module/reanimated2/jestUtils.js.map +1 -1
  99. package/lib/module/reanimated2/js-reanimated/JSReanimated.js +16 -95
  100. package/lib/module/reanimated2/js-reanimated/JSReanimated.js.map +1 -1
  101. package/lib/module/reanimated2/js-reanimated/index.js +5 -0
  102. package/lib/module/reanimated2/js-reanimated/index.js.map +1 -1
  103. package/lib/module/reanimated2/layoutReanimation/animationBuilder/commonTypes.js.map +1 -1
  104. package/lib/module/reanimated2/layoutReanimation/animationBuilder/index.js.map +1 -1
  105. package/lib/module/reanimated2/layoutReanimation/animationsManager.js +84 -0
  106. package/lib/module/reanimated2/layoutReanimation/animationsManager.js.map +1 -0
  107. package/lib/module/reanimated2/layoutReanimation/index.js +1 -1
  108. package/lib/module/reanimated2/layoutReanimation/index.js.map +1 -1
  109. package/lib/module/reanimated2/mappers.js +173 -0
  110. package/lib/module/reanimated2/mappers.js.map +1 -0
  111. package/lib/module/reanimated2/mutables.js +149 -0
  112. package/lib/module/reanimated2/mutables.js.map +1 -0
  113. package/lib/module/reanimated2/shareables.js +133 -0
  114. package/lib/module/reanimated2/shareables.js.map +1 -0
  115. package/lib/module/reanimated2/threads.js +45 -0
  116. package/lib/module/reanimated2/threads.js.map +1 -0
  117. package/lib/module/reanimated2/time.js +37 -0
  118. package/lib/module/reanimated2/time.js.map +1 -0
  119. package/lib/module/reanimated2/valueSetter.js +66 -0
  120. package/lib/module/reanimated2/valueSetter.js.map +1 -0
  121. package/package.json +1 -1
  122. package/plugin.js +31 -15
  123. package/src/createAnimatedComponent.tsx +8 -24
  124. package/src/reanimated2/NativeReanimated/NativeReanimated.ts +34 -53
  125. package/src/reanimated2/ViewDescriptorsSet.ts +30 -69
  126. package/src/reanimated2/WorkletEventHandler.ts +4 -11
  127. package/src/reanimated2/animation/util.ts +1 -0
  128. package/src/reanimated2/commonTypes.ts +29 -4
  129. package/src/reanimated2/core.ts +103 -230
  130. package/src/reanimated2/globals.d.ts +26 -26
  131. package/src/reanimated2/hook/useAnimatedKeyboard.ts +18 -9
  132. package/src/reanimated2/hook/useAnimatedRef.ts +27 -9
  133. package/src/reanimated2/hook/useAnimatedSensor.ts +33 -31
  134. package/src/reanimated2/hook/useAnimatedStyle.ts +26 -64
  135. package/src/reanimated2/hook/useSharedValue.ts +6 -3
  136. package/src/reanimated2/hook/utils.ts +1 -1
  137. package/src/reanimated2/interpolateColor.ts +2 -2
  138. package/src/reanimated2/jestUtils.ts +14 -29
  139. package/src/reanimated2/js-reanimated/JSReanimated.ts +19 -93
  140. package/src/reanimated2/js-reanimated/index.ts +4 -0
  141. package/src/reanimated2/layoutReanimation/animationBuilder/commonTypes.ts +7 -0
  142. package/src/reanimated2/layoutReanimation/animationBuilder/index.ts +1 -0
  143. package/src/reanimated2/layoutReanimation/animationsManager.ts +94 -0
  144. package/src/reanimated2/layoutReanimation/index.ts +1 -1
  145. package/src/reanimated2/mappers.ts +179 -0
  146. package/src/reanimated2/mutables.ts +151 -0
  147. package/src/reanimated2/shareables.ts +121 -0
  148. package/src/reanimated2/threads.ts +46 -0
  149. package/src/reanimated2/time.ts +30 -0
  150. package/src/reanimated2/valueSetter.ts +64 -0
  151. package/Common/cpp/LayoutAnimations/LayoutAnimationsProxy.cpp +0 -127
  152. package/Common/cpp/LayoutAnimations/LayoutAnimationsProxy.h +0 -56
  153. package/Common/cpp/Registries/MapperRegistry.cpp +0 -147
  154. package/Common/cpp/Registries/MapperRegistry.h +0 -29
  155. package/Common/cpp/Registries/WorkletsCache.cpp +0 -45
  156. package/Common/cpp/Registries/WorkletsCache.h +0 -24
  157. package/Common/cpp/SharedItems/FrozenObject.cpp +0 -35
  158. package/Common/cpp/SharedItems/FrozenObject.h +0 -36
  159. package/Common/cpp/SharedItems/HostFunctionHandler.h +0 -28
  160. package/Common/cpp/SharedItems/MutableValue.cpp +0 -134
  161. package/Common/cpp/SharedItems/MutableValue.h +0 -54
  162. package/Common/cpp/SharedItems/MutableValueSetterProxy.cpp +0 -47
  163. package/Common/cpp/SharedItems/MutableValueSetterProxy.h +0 -25
  164. package/Common/cpp/SharedItems/RemoteObject.cpp +0 -45
  165. package/Common/cpp/SharedItems/RemoteObject.h +0 -35
  166. package/Common/cpp/SharedItems/ShareableValue.cpp +0 -529
  167. package/Common/cpp/SharedItems/ShareableValue.h +0 -63
  168. package/Common/cpp/SharedItems/ValueWrapper.h +0 -183
  169. package/Common/cpp/Tools/JSIStoreValueUser.cpp +0 -55
  170. package/Common/cpp/Tools/JSIStoreValueUser.h +0 -37
  171. package/Common/cpp/Tools/Mapper.cpp +0 -86
  172. package/Common/cpp/Tools/Mapper.h +0 -51
  173. package/lib/commonjs/reanimated2/layoutReanimation/LayoutAnimationRepository.js +0 -58
  174. package/lib/commonjs/reanimated2/layoutReanimation/LayoutAnimationRepository.js.map +0 -1
  175. package/lib/module/reanimated2/layoutReanimation/LayoutAnimationRepository.js +0 -52
  176. package/lib/module/reanimated2/layoutReanimation/LayoutAnimationRepository.js.map +0 -1
  177. package/src/reanimated2/layoutReanimation/LayoutAnimationRepository.ts +0 -49
@@ -1,529 +0,0 @@
1
- #include <cxxabi.h>
2
- #include <utility>
3
-
4
- #ifdef RCT_NEW_ARCH_ENABLED
5
- #include <react/renderer/uimanager/primitives.h>
6
- #endif
7
-
8
- #include "FrozenObject.h"
9
- #include "MutableValue.h"
10
- #include "MutableValueSetterProxy.h"
11
- #include "RemoteObject.h"
12
- #include "RuntimeDecorator.h"
13
- #include "RuntimeManager.h"
14
- #include "ShareableValue.h"
15
- #include "SharedParent.h"
16
-
17
- namespace reanimated {
18
- class ShareableValue;
19
- const char *HIDDEN_HOST_OBJECT_PROP = "__reanimatedHostObjectRef";
20
- const char *ALREADY_CONVERTED = "__alreadyConverted";
21
- const char *CALL_ASYNC = "__callAsync";
22
- const char *PRIMAL_FUNCTION = "__primalFunction";
23
- const char *CALLBACK_ERROR_SUFFIX =
24
- "\n\nPossible solutions are:\n"
25
- "a) If you want to synchronously execute this method, mark it as a Worklet\n"
26
- "b) If you want to execute this method on the JS thread, wrap it using runOnJS";
27
-
28
- void addHiddenProperty(
29
- jsi::Runtime &rt,
30
- jsi::Value &&value,
31
- const jsi::Object &obj,
32
- const char *name) {
33
- jsi::Object globalObject = rt.global().getPropertyAsObject(rt, "Object");
34
- jsi::Function defineProperty =
35
- globalObject.getPropertyAsFunction(rt, "defineProperty");
36
- jsi::String internalPropName = jsi::String::createFromUtf8(rt, name);
37
- jsi::Object paramForDefineProperty(rt);
38
- paramForDefineProperty.setProperty(rt, "enumerable", false);
39
- paramForDefineProperty.setProperty(rt, "value", value);
40
- defineProperty.call(rt, obj, internalPropName, paramForDefineProperty);
41
- }
42
-
43
- void freeze(jsi::Runtime &rt, const jsi::Object &obj) {
44
- jsi::Object globalObject = rt.global().getPropertyAsObject(rt, "Object");
45
- jsi::Function freeze = globalObject.getPropertyAsFunction(rt, "freeze");
46
- freeze.call(rt, obj);
47
- }
48
-
49
- void ShareableValue::adaptCache(jsi::Runtime &rt, const jsi::Value &value) {
50
- // when adapting from host object we can assign cached value immediately such
51
- // that we avoid running `toJSValue` in the future when given object is
52
- // accessed
53
- if (RuntimeDecorator::isWorkletRuntime(rt)) {
54
- if (remoteValue.expired()) {
55
- remoteValue = getWeakRef(rt);
56
- }
57
- (*remoteValue.lock()) = jsi::Value(rt, value);
58
- } else {
59
- hostValue = std::make_unique<jsi::Value>(rt, value);
60
- }
61
- }
62
-
63
- void ShareableValue::adapt(
64
- jsi::Runtime &rt,
65
- const jsi::Value &value,
66
- ValueType objectType) {
67
- if (value.isObject()) {
68
- jsi::Object object = value.asObject(rt);
69
- jsi::Value hiddenValue = object.getProperty(rt, HIDDEN_HOST_OBJECT_PROP);
70
- if (!(hiddenValue.isUndefined())) {
71
- jsi::Object hiddenProperty = hiddenValue.asObject(rt);
72
- if (hiddenProperty.isHostObject<FrozenObject>(rt)) {
73
- type = ValueType::FrozenObjectType;
74
- if (object.hasProperty(rt, "__workletHash") && object.isFunction(rt)) {
75
- type = ValueType::WorkletFunctionType;
76
- }
77
- valueContainer = std::make_unique<FrozenObjectWrapper>(
78
- hiddenProperty.getHostObject<FrozenObject>(rt));
79
- if (object.hasProperty(rt, ALREADY_CONVERTED)) {
80
- adaptCache(rt, value);
81
- }
82
- return;
83
- }
84
- }
85
- }
86
-
87
- if (objectType == ValueType::MutableValueType) {
88
- type = ValueType::MutableValueType;
89
- valueContainer =
90
- std::make_unique<MutableValueWrapper>(std::make_shared<MutableValue>(
91
- rt, value, runtimeManager, runtimeManager->scheduler));
92
- } else if (value.isUndefined()) {
93
- type = ValueType::UndefinedType;
94
- } else if (value.isNull()) {
95
- type = ValueType::NullType;
96
- } else if (value.isBool()) {
97
- type = ValueType::BoolType;
98
- valueContainer = std::make_unique<BooleanValueWrapper>(value.getBool());
99
- } else if (value.isNumber()) {
100
- type = ValueType::NumberType;
101
- valueContainer = std::make_unique<NumberValueWrapper>(value.asNumber());
102
- } else if (value.isString()) {
103
- type = ValueType::StringType;
104
- valueContainer =
105
- std::make_unique<StringValueWrapper>(value.asString(rt).utf8(rt));
106
- } else if (value.isObject()) {
107
- auto object = value.asObject(rt);
108
- if (object.isFunction(rt)) {
109
- if (object.getProperty(rt, "__workletHash").isUndefined()) {
110
- // not a worklet, we treat this as a host function
111
- type = ValueType::HostFunctionType;
112
- containsHostFunction = true;
113
-
114
- // Check if it's a hostFunction wrapper
115
- jsi::Value primalFunction = object.getProperty(rt, PRIMAL_FUNCTION);
116
- if (!primalFunction.isUndefined()) {
117
- jsi::Object handlerAsObject = primalFunction.asObject(rt);
118
- std::shared_ptr<HostFunctionHandler> handler =
119
- handlerAsObject.getHostObject<HostFunctionHandler>(rt);
120
- valueContainer = std::make_unique<HostFunctionWrapper>(handler);
121
- } else {
122
- valueContainer = std::make_unique<HostFunctionWrapper>(
123
- std::make_shared<HostFunctionHandler>(
124
- std::make_shared<jsi::Function>(object.asFunction(rt)), rt));
125
- }
126
-
127
- } else {
128
- // a worklet
129
- type = ValueType::WorkletFunctionType;
130
- valueContainer = std::make_unique<FrozenObjectWrapper>(
131
- std::make_shared<FrozenObject>(rt, object, runtimeManager));
132
- auto &frozenObject = ValueWrapper::asFrozenObject(valueContainer);
133
- containsHostFunction |= frozenObject->containsHostFunction;
134
- if (RuntimeDecorator::isReactRuntime(rt) && !containsHostFunction) {
135
- addHiddenProperty(
136
- rt,
137
- createHost(rt, frozenObject),
138
- object,
139
- HIDDEN_HOST_OBJECT_PROP);
140
- }
141
- }
142
- } else if (object.isArray(rt)) {
143
- type = ValueType::FrozenArrayType;
144
- auto array = object.asArray(rt);
145
- valueContainer = std::make_unique<FrozenArrayWrapper>();
146
- auto &frozenArray = ValueWrapper::asFrozenArray(valueContainer);
147
- for (size_t i = 0, size = array.size(rt); i < size; i++) {
148
- auto sv = adapt(rt, array.getValueAtIndex(rt, i), runtimeManager);
149
- containsHostFunction |= sv->containsHostFunction;
150
- frozenArray.push_back(sv);
151
- }
152
- } else if (object.isHostObject<MutableValue>(rt)) {
153
- type = ValueType::MutableValueType;
154
- valueContainer = std::make_unique<MutableValueWrapper>(
155
- object.getHostObject<MutableValue>(rt));
156
- adaptCache(rt, value);
157
- } else if (object.isHostObject<RemoteObject>(rt)) {
158
- type = ValueType::RemoteObjectType;
159
- valueContainer = std::make_unique<RemoteObjectWrapper>(
160
- object.getHostObject<RemoteObject>(rt));
161
- adaptCache(rt, value);
162
- } else if (objectType == ValueType::RemoteObjectType) {
163
- type = ValueType::RemoteObjectType;
164
- valueContainer =
165
- std::make_unique<RemoteObjectWrapper>(std::make_shared<RemoteObject>(
166
- rt, object, runtimeManager, runtimeManager->scheduler));
167
- #ifdef RCT_NEW_ARCH_ENABLED
168
- } else if (object.isHostObject<ShadowNodeWrapper>(rt)) {
169
- type = ValueType::ShadowNodeType;
170
- auto shadowNode = object.getHostObject<ShadowNodeWrapper>(rt)->shadowNode;
171
- valueContainer = std::make_unique<ShadowNodeValueWrapper>(shadowNode);
172
- adaptCache(rt, value);
173
- #endif
174
- } else {
175
- // create frozen object based on a copy of a given object
176
- type = ValueType::FrozenObjectType;
177
- valueContainer = std::make_unique<FrozenObjectWrapper>(
178
- std::make_shared<FrozenObject>(rt, object, runtimeManager));
179
- auto &frozenObject = ValueWrapper::asFrozenObject(valueContainer);
180
- containsHostFunction |= frozenObject->containsHostFunction;
181
- if (RuntimeDecorator::isReactRuntime(rt)) {
182
- if (!containsHostFunction) {
183
- addHiddenProperty(
184
- rt,
185
- createHost(rt, frozenObject),
186
- object,
187
- HIDDEN_HOST_OBJECT_PROP);
188
- }
189
- freeze(rt, object);
190
- }
191
- }
192
- } else if (value.isSymbol()) {
193
- type = ValueType::StringType;
194
- valueContainer =
195
- std::make_unique<StringValueWrapper>(value.asSymbol(rt).toString(rt));
196
- } else {
197
- throw "Invalid value type";
198
- }
199
- }
200
-
201
- std::shared_ptr<ShareableValue> ShareableValue::adapt(
202
- jsi::Runtime &rt,
203
- const jsi::Value &value,
204
- RuntimeManager *runtimeManager,
205
- ValueType valueType) {
206
- auto sv = std::shared_ptr<ShareableValue>(
207
- new ShareableValue(runtimeManager, runtimeManager->scheduler));
208
- sv->adapt(rt, value, valueType);
209
- return sv;
210
- }
211
-
212
- jsi::Value ShareableValue::getValue(jsi::Runtime &rt) {
213
- // TODO: maybe we can cache toJSValue results on a per-runtime basis, need to
214
- // avoid ref loops
215
- if (&rt == runtimeManager->runtime.get()) {
216
- // Getting value on the same runtime where it was created, prepare
217
- // remoteValue
218
- if (remoteValue.expired()) {
219
- remoteValue = getWeakRef(rt);
220
- }
221
-
222
- if (remoteValue.lock()->isUndefined()) {
223
- (*remoteValue.lock()) = toJSValue(rt);
224
- }
225
- return jsi::Value(rt, *remoteValue.lock());
226
- } else {
227
- // Getting value on a different runtime than where it was created from,
228
- // prepare hostValue
229
- if (hostValue.get() == nullptr) {
230
- hostValue = std::make_unique<jsi::Value>(toJSValue(rt));
231
- }
232
- return jsi::Value(rt, *hostValue);
233
- }
234
- }
235
-
236
- jsi::Object ShareableValue::createHost(
237
- jsi::Runtime &rt,
238
- std::shared_ptr<jsi::HostObject> host) {
239
- return jsi::Object::createFromHostObject(rt, host);
240
- }
241
-
242
- jsi::Value createFrozenWrapper(
243
- jsi::Runtime &rt,
244
- std::shared_ptr<FrozenObject> frozenObject) {
245
- jsi::Object __reanimatedHiddenHost =
246
- jsi::Object::createFromHostObject(rt, frozenObject);
247
- jsi::Object obj = frozenObject->shallowClone(rt);
248
- jsi::Object globalObject = rt.global().getPropertyAsObject(rt, "Object");
249
- jsi::Function freeze = globalObject.getPropertyAsFunction(rt, "freeze");
250
- if (!frozenObject->containsHostFunction) {
251
- addHiddenProperty(
252
- rt, std::move(__reanimatedHiddenHost), obj, HIDDEN_HOST_OBJECT_PROP);
253
- addHiddenProperty(rt, true, obj, ALREADY_CONVERTED);
254
- }
255
- return freeze.call(rt, obj);
256
- }
257
-
258
- jsi::Value ShareableValue::toJSValue(jsi::Runtime &rt) {
259
- switch (type) {
260
- case ValueType::UndefinedType:
261
- return jsi::Value::undefined();
262
- case ValueType::NullType:
263
- return jsi::Value::null();
264
- case ValueType::BoolType:
265
- return jsi::Value(ValueWrapper::asBoolean(valueContainer));
266
- case ValueType::NumberType:
267
- return jsi::Value(ValueWrapper::asNumber(valueContainer));
268
- case ValueType::StringType: {
269
- auto &stringValue = ValueWrapper::asString(valueContainer);
270
- return jsi::Value(rt, jsi::String::createFromUtf8(rt, stringValue));
271
- }
272
- case ValueType::FrozenObjectType: {
273
- auto &frozenObject = ValueWrapper::asFrozenObject(valueContainer);
274
- return createFrozenWrapper(rt, frozenObject);
275
- }
276
- case ValueType::FrozenArrayType: {
277
- auto &frozenArray = ValueWrapper::asFrozenArray(valueContainer);
278
- jsi::Array array(rt, frozenArray.size());
279
- for (size_t i = 0; i < frozenArray.size(); i++) {
280
- array.setValueAtIndex(rt, i, frozenArray[i]->toJSValue(rt));
281
- }
282
- return array;
283
- }
284
- case ValueType::RemoteObjectType: {
285
- auto &remoteObject = ValueWrapper::asRemoteObject(valueContainer);
286
- if (RuntimeDecorator::isWorkletRuntime(rt)) {
287
- remoteObject->maybeInitializeOnWorkletRuntime(rt);
288
- }
289
- return createHost(rt, remoteObject);
290
- }
291
- case ValueType::MutableValueType: {
292
- auto &mutableObject = ValueWrapper::asMutableValue(valueContainer);
293
- return createHost(rt, mutableObject);
294
- }
295
- #ifdef RCT_NEW_ARCH_ENABLED
296
- case ValueType::ShadowNodeType: {
297
- auto &shadowNode = ValueWrapper::asShadowNode(valueContainer);
298
- return createHost(rt, std::make_shared<ShadowNodeWrapper>(shadowNode));
299
- }
300
- #endif
301
- case ValueType::HostFunctionType: {
302
- auto hostFunctionWrapper =
303
- ValueWrapper::asHostFunctionWrapper(valueContainer);
304
- auto &hostRuntime = hostFunctionWrapper->value->hostRuntime;
305
- if (hostRuntime == &rt) {
306
- // function is accessed from the same runtime it was crated, we just
307
- // return same function obj
308
- return jsi::Value(
309
- rt, *hostFunctionWrapper->value->getPureFunction().get());
310
- } else {
311
- // function is accessed from a different runtime, we wrap function in
312
- // host func that'd enqueue call on an appropriate thread
313
-
314
- auto runtimeManager = this->runtimeManager;
315
- auto hostFunction = hostFunctionWrapper->value;
316
-
317
- auto warnFunction = [runtimeManager, hostFunction](
318
- jsi::Runtime &rt,
319
- const jsi::Value &thisValue,
320
- const jsi::Value *args,
321
- size_t count) -> jsi::Value {
322
- jsi::Value jsThis = rt.global().getProperty(rt, "jsThis");
323
- std::string workletLocation = jsThis.asObject(rt)
324
- .getProperty(rt, "__location")
325
- .toString(rt)
326
- .utf8(rt);
327
- std::string exceptionMessage = "Tried to synchronously call ";
328
- if (hostFunction->functionName.empty()) {
329
- exceptionMessage += "anonymous function";
330
- } else {
331
- exceptionMessage += "function {" + hostFunction->functionName + "}";
332
- }
333
- exceptionMessage +=
334
- " from a different thread.\n\nOccurred in worklet location: ";
335
- exceptionMessage += workletLocation;
336
- exceptionMessage += CALLBACK_ERROR_SUFFIX;
337
- runtimeManager->errorHandler->setError(exceptionMessage);
338
- runtimeManager->errorHandler->raise();
339
-
340
- return jsi::Value::undefined();
341
- };
342
-
343
- auto clb = [runtimeManager, hostFunction, hostRuntime](
344
- jsi::Runtime &rt,
345
- const jsi::Value &thisValue,
346
- const jsi::Value *args,
347
- size_t count) -> jsi::Value {
348
- // TODO: we should find thread based on runtime such that we could
349
- // also call UI methods from RN and not only RN methods from UI
350
-
351
- std::vector<std::shared_ptr<ShareableValue>> params;
352
- for (int i = 0; i < count; ++i) {
353
- params.push_back(
354
- ShareableValue::adapt(rt, args[i], runtimeManager));
355
- }
356
-
357
- std::function<void()> job = [hostFunction, hostRuntime, params] {
358
- jsi::Value *args = new jsi::Value[params.size()];
359
- for (int i = 0; i < params.size(); ++i) {
360
- args[i] = params[i]->getValue(*hostRuntime);
361
- }
362
- jsi::Value returnedValue =
363
- hostFunction->getPureFunction().get()->call(
364
- *hostRuntime,
365
- static_cast<const jsi::Value *>(args),
366
- static_cast<size_t>(params.size()));
367
-
368
- delete[] args;
369
- // ToDo use returned value to return promise
370
- };
371
-
372
- runtimeManager->scheduler->scheduleOnJS(job);
373
- return jsi::Value::undefined();
374
- };
375
- jsi::Function wrapperFunction = jsi::Function::createFromHostFunction(
376
- rt, jsi::PropNameID::forAscii(rt, "hostFunction"), 0, warnFunction);
377
- jsi::Function res = jsi::Function::createFromHostFunction(
378
- rt, jsi::PropNameID::forAscii(rt, "hostFunction"), 0, clb);
379
- addHiddenProperty(rt, std::move(res), wrapperFunction, CALL_ASYNC);
380
- jsi::Object functionHandler =
381
- createHost(rt, hostFunctionWrapper->value);
382
- addHiddenProperty(
383
- rt, std::move(functionHandler), wrapperFunction, PRIMAL_FUNCTION);
384
- return wrapperFunction;
385
- }
386
- }
387
- case ValueType::WorkletFunctionType: {
388
- auto runtimeManager = this->runtimeManager;
389
- auto &frozenObject = ValueWrapper::asFrozenObject(this->valueContainer);
390
- if (RuntimeDecorator::isWorkletRuntime(rt)) {
391
- // when running on worklet thread we prep a function
392
-
393
- auto jsThis = std::make_shared<jsi::Object>(
394
- frozenObject->shallowClone(*runtimeManager->runtime));
395
- std::shared_ptr<jsi::Function> funPtr(
396
- runtimeManager->workletsCache->getFunction(rt, frozenObject));
397
- auto name = funPtr->getProperty(rt, "name").asString(rt).utf8(rt);
398
-
399
- auto clb = [=](jsi::Runtime &rt,
400
- const jsi::Value &thisValue,
401
- const jsi::Value *args,
402
- size_t count) mutable -> jsi::Value {
403
- const jsi::String jsThisName =
404
- jsi::String::createFromAscii(rt, "jsThis");
405
- jsi::Object global = rt.global();
406
- jsi::Value oldJSThis = global.getProperty(rt, jsThisName);
407
- global.setProperty(rt, jsThisName, *jsThis); // set jsThis
408
-
409
- jsi::Value res = jsi::Value::undefined();
410
- try {
411
- if (thisValue.isObject()) {
412
- res =
413
- funPtr->callWithThis(rt, thisValue.asObject(rt), args, count);
414
- } else {
415
- res = funPtr->call(rt, args, count);
416
- }
417
- } catch (std::exception &e) {
418
- std::string str = e.what();
419
- runtimeManager->errorHandler->setError(str);
420
- runtimeManager->errorHandler->raise();
421
- } catch (...) {
422
- if (demangleExceptionName(
423
- abi::__cxa_current_exception_type()->name()) ==
424
- "facebook::jsi::JSError") {
425
- throw jsi::JSError(rt, "Javascript worklet error");
426
- }
427
- // TODO find out a way to get the error's message on hermes
428
- jsi::Value location = jsThis->getProperty(rt, "__location");
429
- std::string str = "Javascript worklet error";
430
- if (location.isString()) {
431
- str += "\nIn file: " + location.asString(rt).utf8(rt);
432
- }
433
- runtimeManager->errorHandler->setError(str);
434
- runtimeManager->errorHandler->raise();
435
- }
436
- global.setProperty(rt, jsThisName, oldJSThis); // clean jsThis
437
- return res;
438
- };
439
- return jsi::Function::createFromHostFunction(
440
- rt, jsi::PropNameID::forAscii(rt, name.c_str()), 0, clb);
441
- } else {
442
- // when run outside of UI thread we enqueue a call on the UI thread
443
- auto clb = [=](jsi::Runtime &rt,
444
- const jsi::Value &thisValue,
445
- const jsi::Value *args,
446
- size_t count) -> jsi::Value {
447
- // TODO: we should find thread based on runtime such that we could
448
- // also call UI methods from RN and not only RN methods from UI
449
-
450
- std::vector<std::shared_ptr<ShareableValue>> params;
451
- for (int i = 0; i < count; ++i) {
452
- params.push_back(
453
- ShareableValue::adapt(rt, args[i], runtimeManager));
454
- }
455
-
456
- runtimeManager->scheduler->scheduleOnUI([=] {
457
- jsi::Runtime &rt = *runtimeManager->runtime.get();
458
- auto jsThis = createFrozenWrapper(rt, frozenObject).getObject(rt);
459
- auto code =
460
- jsThis.getProperty(rt, "asString").asString(rt).utf8(rt);
461
- std::shared_ptr<jsi::Function> funPtr(
462
- runtimeManager->workletsCache->getFunction(rt, frozenObject));
463
-
464
- jsi::Value *args = new jsi::Value[params.size()];
465
- for (int i = 0; i < params.size(); ++i) {
466
- args[i] = params[i]->getValue(rt);
467
- }
468
-
469
- jsi::Value returnedValue;
470
- const jsi::String jsThisName =
471
- jsi::String::createFromAscii(rt, "jsThis");
472
- jsi::Object global = rt.global();
473
- jsi::Value oldJSThis = global.getProperty(rt, jsThisName);
474
- global.setProperty(rt, jsThisName, jsThis); // set jsThis
475
- try {
476
- returnedValue = funPtr->call(
477
- rt,
478
- static_cast<const jsi::Value *>(args),
479
- static_cast<size_t>(params.size()));
480
- } catch (std::exception &e) {
481
- std::string str = e.what();
482
- runtimeManager->errorHandler->setError(str);
483
- runtimeManager->errorHandler->raise();
484
- } catch (...) {
485
- if (demangleExceptionName(
486
- abi::__cxa_current_exception_type()->name()) ==
487
- "facebook::jsi::JSError") {
488
- throw jsi::JSError(rt, "Javascript worklet error");
489
- }
490
- // TODO find out a way to get the error's message on hermes
491
- jsi::Value location = jsThis.getProperty(rt, "__location");
492
- std::string str = "Javascript worklet error";
493
- if (location.isString()) {
494
- str += "\nIn file: " + location.asString(rt).utf8(rt);
495
- }
496
- runtimeManager->errorHandler->setError(str);
497
- runtimeManager->errorHandler->raise();
498
- }
499
- global.setProperty(rt, jsThisName, oldJSThis); // clean jsThis
500
-
501
- delete[] args;
502
- // ToDo use returned value to return promise
503
- });
504
- return jsi::Value::undefined();
505
- };
506
- return jsi::Function::createFromHostFunction(
507
- rt, jsi::PropNameID::forAscii(rt, "_workletFunction"), 0, clb);
508
- }
509
- }
510
- default: {
511
- throw "Unable to find conversion method for this type";
512
- }
513
- }
514
- throw "convert error";
515
- }
516
-
517
- std::string ShareableValue::demangleExceptionName(std::string toDemangle) {
518
- int status = 0;
519
- char *buff =
520
- __cxxabiv1::__cxa_demangle(toDemangle.c_str(), nullptr, nullptr, &status);
521
- if (!buff) {
522
- return toDemangle;
523
- }
524
- std::string demangled = buff;
525
- std::free(buff);
526
- return demangled;
527
- }
528
-
529
- } // namespace reanimated
@@ -1,63 +0,0 @@
1
- #pragma once
2
-
3
- #include <jsi/jsi.h>
4
- #include <memory>
5
- #include <mutex>
6
- #include <string>
7
- #include <unordered_map>
8
- #include <vector>
9
- #include "AnimatedSensorModule.h"
10
- #include "HostFunctionHandler.h"
11
- #include "JSIStoreValueUser.h"
12
- #include "LayoutAnimationsProxy.h"
13
- #include "RuntimeManager.h"
14
- #include "Scheduler.h"
15
- #include "SharedParent.h"
16
- #include "ValueWrapper.h"
17
- #include "WorkletsCache.h"
18
-
19
- using namespace facebook;
20
-
21
- namespace reanimated {
22
-
23
- class ShareableValue : public std::enable_shared_from_this<ShareableValue>,
24
- public StoreUser {
25
- friend WorkletsCache;
26
- friend FrozenObject;
27
- friend LayoutAnimationsProxy;
28
- friend NativeReanimatedModule;
29
- friend AnimatedSensorModule;
30
- friend void extractMutables(
31
- jsi::Runtime &rt,
32
- std::shared_ptr<ShareableValue> sv,
33
- std::vector<std::shared_ptr<MutableValue>> &res);
34
-
35
- private:
36
- RuntimeManager *runtimeManager;
37
- std::unique_ptr<ValueWrapper> valueContainer;
38
- std::unique_ptr<jsi::Value> hostValue;
39
- std::weak_ptr<jsi::Value> remoteValue;
40
- bool containsHostFunction = false;
41
-
42
- ShareableValue(RuntimeManager *runtimeManager, std::shared_ptr<Scheduler> s)
43
- : StoreUser(s, *runtimeManager), runtimeManager(runtimeManager) {}
44
-
45
- jsi::Value toJSValue(jsi::Runtime &rt);
46
- jsi::Object createHost(
47
- jsi::Runtime &rt,
48
- std::shared_ptr<jsi::HostObject> host);
49
- void adapt(jsi::Runtime &rt, const jsi::Value &value, ValueType objectType);
50
- void adaptCache(jsi::Runtime &rt, const jsi::Value &value);
51
- std::string demangleExceptionName(std::string toDemangle);
52
-
53
- public:
54
- ValueType type = ValueType::UndefinedType;
55
- static std::shared_ptr<ShareableValue> adapt(
56
- jsi::Runtime &rt,
57
- const jsi::Value &value,
58
- RuntimeManager *runtimeManager,
59
- ValueType objectType = ValueType::UndefinedType);
60
- jsi::Value getValue(jsi::Runtime &rt);
61
- };
62
-
63
- } // namespace reanimated