react-native-worklets 0.0.1-alpha → 0.1.0
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.
- package/Common/cpp/worklets/AnimationFrameQueue/AnimationFrameBatchinator.cpp +71 -0
- package/Common/cpp/worklets/AnimationFrameQueue/AnimationFrameBatchinator.h +38 -0
- package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.cpp +131 -0
- package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.h +82 -0
- package/Common/cpp/worklets/NativeModules/WorkletsModuleProxySpec.cpp +72 -0
- package/Common/cpp/worklets/NativeModules/WorkletsModuleProxySpec.h +44 -0
- package/Common/cpp/worklets/Registries/EventHandlerRegistry.cpp +94 -0
- package/Common/cpp/worklets/Registries/EventHandlerRegistry.h +49 -0
- package/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.cpp +8 -0
- package/Common/cpp/worklets/Registries/WorkletRuntimeRegistry.h +39 -0
- package/Common/cpp/worklets/SharedItems/Shareables.cpp +326 -0
- package/Common/cpp/worklets/SharedItems/Shareables.h +345 -0
- package/Common/cpp/worklets/Tools/AsyncQueue.cpp +52 -0
- package/Common/cpp/worklets/Tools/AsyncQueue.h +35 -0
- package/Common/cpp/worklets/Tools/Defs.h +10 -0
- package/Common/cpp/worklets/Tools/JSISerializer.cpp +342 -0
- package/Common/cpp/worklets/Tools/JSISerializer.h +47 -0
- package/Common/cpp/worklets/Tools/JSLogger.cpp +16 -0
- package/Common/cpp/worklets/Tools/JSLogger.h +20 -0
- package/Common/cpp/worklets/Tools/JSScheduler.cpp +10 -0
- package/Common/cpp/worklets/Tools/JSScheduler.h +29 -0
- package/Common/cpp/worklets/Tools/PlatformLogger.h +16 -0
- package/Common/cpp/worklets/Tools/SingleInstanceChecker.h +72 -0
- package/Common/cpp/worklets/Tools/ThreadSafeQueue.h +49 -0
- package/Common/cpp/worklets/Tools/UIScheduler.cpp +19 -0
- package/Common/cpp/worklets/Tools/UIScheduler.h +22 -0
- package/Common/cpp/worklets/Tools/WorkletEventHandler.cpp +29 -0
- package/Common/cpp/worklets/Tools/WorkletEventHandler.h +41 -0
- package/Common/cpp/worklets/Tools/WorkletsJSIUtils.cpp +26 -0
- package/Common/cpp/worklets/Tools/WorkletsJSIUtils.h +199 -0
- package/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.cpp +20 -0
- package/Common/cpp/worklets/WorkletRuntime/RNRuntimeWorkletDecorator.h +19 -0
- package/Common/cpp/worklets/WorkletRuntime/RuntimeInitialization.md +191 -0
- package/Common/cpp/worklets/WorkletRuntime/UIRuntimeDecorator.cpp +19 -0
- package/Common/cpp/worklets/WorkletRuntime/UIRuntimeDecorator.h +16 -0
- package/Common/cpp/worklets/WorkletRuntime/WorkletHermesRuntime.cpp +108 -0
- package/Common/cpp/worklets/WorkletRuntime/WorkletHermesRuntime.h +127 -0
- package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp +183 -0
- package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h +90 -0
- package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeCollector.h +36 -0
- package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.cpp +179 -0
- package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.h +22 -0
- package/LICENSE +20 -0
- package/README.md +27 -0
- package/RNWorklets.podspec +70 -0
- package/android/CMakeLists.txt +56 -0
- package/android/build.gradle +313 -0
- package/android/gradle.properties +5 -0
- package/android/proguard-rules.pro +3 -0
- package/android/spotless.gradle +9 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/cpp/worklets/CMakeLists.txt +85 -0
- package/android/src/main/cpp/worklets/android/AndroidUIScheduler.cpp +63 -0
- package/android/src/main/cpp/worklets/android/AndroidUIScheduler.h +41 -0
- package/android/src/main/cpp/worklets/android/AnimationFrameCallback.h +32 -0
- package/android/src/main/cpp/worklets/android/PlatformLogger.cpp +29 -0
- package/android/src/main/cpp/worklets/android/WorkletsModule.cpp +83 -0
- package/android/src/main/cpp/worklets/android/WorkletsModule.h +63 -0
- package/android/src/main/cpp/worklets/android/WorkletsOnLoad.cpp +13 -0
- package/android/src/main/java/com/swmansion/worklets/AndroidUIScheduler.java +60 -0
- package/android/src/main/java/com/swmansion/worklets/AnimationFrameQueue/AnimationFrameCallback.java +20 -0
- package/android/src/main/java/com/swmansion/worklets/AnimationFrameQueue/AnimationFrameQueue.java +113 -0
- package/android/src/main/java/com/swmansion/worklets/JSCallInvokerResolver.java +27 -0
- package/android/src/main/java/com/swmansion/worklets/WorkletsMessageQueueThread.java +16 -0
- package/android/src/main/java/com/swmansion/worklets/WorkletsMessageQueueThreadBase.java +72 -0
- package/android/src/main/java/com/swmansion/worklets/WorkletsModule.java +106 -0
- package/android/src/main/java/com/swmansion/worklets/WorkletsPackage.java +49 -0
- package/android/src/paper/com/swmansion/worklets/NativeWorkletsModuleSpec.java +26 -0
- package/apple/worklets/apple/AnimationFrameQueue.h +15 -0
- package/apple/worklets/apple/AnimationFrameQueue.mm +81 -0
- package/apple/worklets/apple/AssertJavaScriptQueue.h +14 -0
- package/apple/worklets/apple/AssertTurboModuleManagerQueue.h +16 -0
- package/apple/worklets/apple/IOSUIScheduler.h +14 -0
- package/apple/worklets/apple/IOSUIScheduler.mm +24 -0
- package/apple/worklets/apple/PlatformLogger.mm +31 -0
- package/apple/worklets/apple/SlowAnimations.h +8 -0
- package/apple/worklets/apple/SlowAnimations.mm +47 -0
- package/apple/worklets/apple/WorkletsDisplayLink.h +21 -0
- package/apple/worklets/apple/WorkletsMessageThread.h +16 -0
- package/apple/worklets/apple/WorkletsMessageThread.mm +32 -0
- package/apple/worklets/apple/WorkletsModule.h +10 -0
- package/apple/worklets/apple/WorkletsModule.mm +85 -0
- package/lib/module/PlatformChecker.js +35 -0
- package/lib/module/PlatformChecker.js.map +1 -0
- package/lib/module/WorkletsError.js +13 -0
- package/lib/module/WorkletsError.js.map +1 -0
- package/lib/module/WorkletsModule/JSWorklets.js +36 -0
- package/lib/module/WorkletsModule/JSWorklets.js.map +1 -0
- package/lib/module/WorkletsModule/NativeWorklets.js +39 -0
- package/lib/module/WorkletsModule/NativeWorklets.js.map +1 -0
- package/lib/module/WorkletsModule/index.js +4 -0
- package/lib/module/WorkletsModule/index.js.map +1 -0
- package/lib/module/WorkletsModule/workletsModuleInstance.js +7 -0
- package/lib/module/WorkletsModule/workletsModuleInstance.js.map +1 -0
- package/lib/module/WorkletsModule/workletsModuleInstance.web.js +5 -0
- package/lib/module/WorkletsModule/workletsModuleInstance.web.js.map +1 -0
- package/lib/module/WorkletsModule/workletsModuleProxy.js +4 -0
- package/lib/module/WorkletsModule/workletsModuleProxy.js.map +1 -0
- package/lib/module/animationFrameQueue/mockedRequestAnimationFrame.js +10 -0
- package/lib/module/animationFrameQueue/mockedRequestAnimationFrame.js.map +1 -0
- package/lib/module/animationFrameQueue/requestAnimationFrame.js +36 -0
- package/lib/module/animationFrameQueue/requestAnimationFrame.js.map +1 -0
- package/lib/module/errors.js +78 -0
- package/lib/module/errors.js.map +1 -0
- package/lib/module/index.js +17 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/initializers.js +158 -0
- package/lib/module/initializers.js.map +1 -0
- package/lib/module/logger/LogBox.js +15 -0
- package/lib/module/logger/LogBox.js.map +1 -0
- package/lib/module/logger/index.js +5 -0
- package/lib/module/logger/index.js.map +1 -0
- package/lib/module/logger/logger.js +137 -0
- package/lib/module/logger/logger.js.map +1 -0
- package/lib/module/privateGlobals.d.js +8 -0
- package/lib/module/privateGlobals.d.js.map +1 -0
- package/lib/module/runtimes.js +63 -0
- package/lib/module/runtimes.js.map +1 -0
- package/lib/module/shareableMappingCache.js +39 -0
- package/lib/module/shareableMappingCache.js.map +1 -0
- package/lib/module/shareables.js +417 -0
- package/lib/module/shareables.js.map +1 -0
- package/lib/module/specs/NativeWorkletsModule.js +5 -0
- package/lib/module/specs/NativeWorkletsModule.js.map +1 -0
- package/lib/module/specs/index.js +5 -0
- package/lib/module/specs/index.js.map +1 -0
- package/lib/module/threads.js +204 -0
- package/lib/module/threads.js.map +1 -0
- package/lib/module/valueUnpacker.js +83 -0
- package/lib/module/valueUnpacker.js.map +1 -0
- package/lib/module/workletFunction.js +37 -0
- package/lib/module/workletFunction.js.map +1 -0
- package/lib/module/workletTypes.js +12 -0
- package/lib/module/workletTypes.js.map +1 -0
- package/lib/typescript/PlatformChecker.d.ts +7 -0
- package/lib/typescript/PlatformChecker.d.ts.map +1 -0
- package/lib/typescript/WorkletsError.d.ts +3 -0
- package/lib/typescript/WorkletsError.d.ts.map +1 -0
- package/lib/typescript/WorkletsModule/JSWorklets.d.ts +3 -0
- package/lib/typescript/WorkletsModule/JSWorklets.d.ts.map +1 -0
- package/lib/typescript/WorkletsModule/NativeWorklets.d.ts +5 -0
- package/lib/typescript/WorkletsModule/NativeWorklets.d.ts.map +1 -0
- package/lib/typescript/WorkletsModule/index.d.ts +3 -0
- package/lib/typescript/WorkletsModule/index.d.ts.map +1 -0
- package/lib/typescript/WorkletsModule/workletsModuleInstance.d.ts +2 -0
- package/lib/typescript/WorkletsModule/workletsModuleInstance.d.ts.map +1 -0
- package/lib/typescript/WorkletsModule/workletsModuleInstance.web.d.ts +2 -0
- package/lib/typescript/WorkletsModule/workletsModuleInstance.web.d.ts.map +1 -0
- package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts +12 -0
- package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts.map +1 -0
- package/lib/typescript/animationFrameQueue/mockedRequestAnimationFrame.d.ts +6 -0
- package/lib/typescript/animationFrameQueue/mockedRequestAnimationFrame.d.ts.map +1 -0
- package/lib/typescript/animationFrameQueue/requestAnimationFrame.d.ts +2 -0
- package/lib/typescript/animationFrameQueue/requestAnimationFrame.d.ts.map +1 -0
- package/lib/typescript/errors.d.ts +19 -0
- package/lib/typescript/errors.d.ts.map +1 -0
- package/lib/typescript/index.d.ts +13 -0
- package/lib/typescript/index.d.ts.map +1 -0
- package/lib/typescript/initializers.d.ts +6 -0
- package/lib/typescript/initializers.d.ts.map +1 -0
- package/lib/typescript/logger/LogBox.d.ts +32 -0
- package/lib/typescript/logger/LogBox.d.ts.map +1 -0
- package/lib/typescript/logger/index.d.ts +3 -0
- package/lib/typescript/logger/index.d.ts.map +1 -0
- package/lib/typescript/logger/logger.d.ts +52 -0
- package/lib/typescript/logger/logger.d.ts.map +1 -0
- package/lib/typescript/runtimes.d.ts +16 -0
- package/lib/typescript/runtimes.d.ts.map +1 -0
- package/lib/typescript/shareableMappingCache.d.ts +16 -0
- package/lib/typescript/shareableMappingCache.d.ts.map +1 -0
- package/lib/typescript/shareables.d.ts +15 -0
- package/lib/typescript/shareables.d.ts.map +1 -0
- package/lib/typescript/specs/NativeWorkletsModule.d.ts +7 -0
- package/lib/typescript/specs/NativeWorkletsModule.d.ts.map +1 -0
- package/lib/typescript/specs/index.d.ts +3 -0
- package/lib/typescript/specs/index.d.ts.map +1 -0
- package/lib/typescript/threads.d.ts +49 -0
- package/lib/typescript/threads.d.ts.map +1 -0
- package/lib/typescript/valueUnpacker.d.ts +2 -0
- package/lib/typescript/valueUnpacker.d.ts.map +1 -0
- package/lib/typescript/workletFunction.d.ts +27 -0
- package/lib/typescript/workletFunction.d.ts.map +1 -0
- package/lib/typescript/workletTypes.d.ts +49 -0
- package/lib/typescript/workletTypes.d.ts.map +1 -0
- package/package.json +106 -8
- package/plugin/index.js +3 -0
- package/scripts/worklets_utils.rb +53 -0
- package/src/PlatformChecker.ts +43 -0
- package/src/WorkletsError.ts +13 -0
- package/src/WorkletsModule/JSWorklets.ts +57 -0
- package/src/WorkletsModule/NativeWorklets.ts +68 -0
- package/src/WorkletsModule/index.ts +7 -0
- package/src/WorkletsModule/workletsModuleInstance.ts +9 -0
- package/src/WorkletsModule/workletsModuleInstance.web.ts +5 -0
- package/src/WorkletsModule/workletsModuleProxy.ts +30 -0
- package/src/animationFrameQueue/mockedRequestAnimationFrame.ts +11 -0
- package/src/animationFrameQueue/requestAnimationFrame.ts +41 -0
- package/src/errors.ts +103 -0
- package/src/index.ts +42 -0
- package/src/initializers.ts +175 -0
- package/src/logger/LogBox.ts +55 -0
- package/src/logger/index.ts +3 -0
- package/src/logger/logger.ts +155 -0
- package/src/privateGlobals.d.ts +41 -0
- package/src/runtimes.ts +92 -0
- package/src/shareableMappingCache.ts +44 -0
- package/src/shareables.ts +577 -0
- package/src/specs/NativeWorkletsModule.ts +9 -0
- package/src/specs/index.ts +5 -0
- package/src/threads.ts +275 -0
- package/src/valueUnpacker.ts +110 -0
- package/src/workletFunction.ts +47 -0
- package/src/workletTypes.ts +76 -0
- package/Animated.js +0 -13
- package/AnimatedEvent.js +0 -167
- package/AnimatedImplementation.js +0 -666
- package/CoreAnimated.js +0 -43
- package/Easing.js +0 -236
- package/NativeAnimatedHelper.js +0 -226
- package/SpringConfig.js +0 -79
- package/animations/Animation.js +0 -36
- package/animations/DecayAnimation.js +0 -70
- package/animations/SpringAnimation.js +0 -125
- package/animations/TimingAnimation.js +0 -70
- package/bezier.js +0 -128
- package/createAnimatedComponent.js +0 -188
- package/nodes/AnimatedBlock.js +0 -19
- package/nodes/AnimatedClock.js +0 -76
- package/nodes/AnimatedCond.js +0 -23
- package/nodes/AnimatedDetach.js +0 -15
- package/nodes/AnimatedInterpolation.js +0 -338
- package/nodes/AnimatedNode.js +0 -97
- package/nodes/AnimatedOnChange.js +0 -28
- package/nodes/AnimatedOp.js +0 -17
- package/nodes/AnimatedProps.js +0 -154
- package/nodes/AnimatedSet.js +0 -19
- package/nodes/AnimatedStartClock.js +0 -21
- package/nodes/AnimatedStopClock.js +0 -21
- package/nodes/AnimatedStyle.js +0 -89
- package/nodes/AnimatedTracking.js +0 -36
- package/nodes/AnimatedTransform.js +0 -93
- package/nodes/AnimatedValue.js +0 -271
- package/nodes/AnimatedWithInput.js +0 -21
- package/nodes/SpringNode.js +0 -106
- package/nodes/TimingStep.js +0 -44
- package/utils.js +0 -28
|
@@ -0,0 +1,577 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
import { registerWorkletStackDetails } from './errors';
|
|
3
|
+
import { logger } from './logger';
|
|
4
|
+
import { shouldBeUseWeb } from './PlatformChecker';
|
|
5
|
+
import {
|
|
6
|
+
shareableMappingCache,
|
|
7
|
+
shareableMappingFlag,
|
|
8
|
+
} from './shareableMappingCache';
|
|
9
|
+
import { isWorkletFunction } from './workletFunction';
|
|
10
|
+
import { WorkletsError } from './WorkletsError';
|
|
11
|
+
import { WorkletsModule } from './WorkletsModule';
|
|
12
|
+
import type {
|
|
13
|
+
FlatShareableRef,
|
|
14
|
+
ShareableRef,
|
|
15
|
+
WorkletFunction,
|
|
16
|
+
WorkletFunctionDev,
|
|
17
|
+
} from './workletTypes';
|
|
18
|
+
|
|
19
|
+
// for web/chrome debugger/jest environments this file provides a stub implementation
|
|
20
|
+
// where no shareable references are used. Instead, the objects themselves are used
|
|
21
|
+
// instead of shareable references, because of the fact that we don't have to deal with
|
|
22
|
+
// running the code on separate VMs.
|
|
23
|
+
const SHOULD_BE_USE_WEB = shouldBeUseWeb();
|
|
24
|
+
|
|
25
|
+
const MAGIC_KEY = 'REANIMATED_MAGIC_KEY';
|
|
26
|
+
|
|
27
|
+
function isHostObject(value: NonNullable<object>) {
|
|
28
|
+
'worklet';
|
|
29
|
+
// We could use JSI to determine whether an object is a host object, however
|
|
30
|
+
// the below workaround works well and is way faster than an additional JSI call.
|
|
31
|
+
// We use the fact that host objects have broken implementation of `hasOwnProperty`
|
|
32
|
+
// and hence return true for all `in` checks regardless of the key we ask for.
|
|
33
|
+
return MAGIC_KEY in value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function isPlainJSObject(object: object): object is Record<string, unknown> {
|
|
37
|
+
return Object.getPrototypeOf(object) === Object.prototype;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function getFromCache(value: object) {
|
|
41
|
+
const cached = shareableMappingCache.get(value);
|
|
42
|
+
if (cached === shareableMappingFlag) {
|
|
43
|
+
// This means that `value` was already a clone and we should return it as is.
|
|
44
|
+
return value;
|
|
45
|
+
}
|
|
46
|
+
return cached;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// The below object is used as a replacement for objects that cannot be transferred
|
|
50
|
+
// as shareable values. In makeShareableCloneRecursive we detect if an object is of
|
|
51
|
+
// a plain Object.prototype and only allow such objects to be transferred. This lets
|
|
52
|
+
// us avoid all sorts of react internals from leaking into the UI runtime. To make it
|
|
53
|
+
// possible to catch errors when someone actually tries to access such object on the UI
|
|
54
|
+
// runtime, we use the below Proxy object which is instantiated on the UI runtime and
|
|
55
|
+
// throws whenever someone tries to access its fields.
|
|
56
|
+
const INACCESSIBLE_OBJECT = {
|
|
57
|
+
__init: () => {
|
|
58
|
+
'worklet';
|
|
59
|
+
return new Proxy(
|
|
60
|
+
{},
|
|
61
|
+
{
|
|
62
|
+
get: (_: unknown, prop: string | symbol) => {
|
|
63
|
+
if (
|
|
64
|
+
prop === '_isReanimatedSharedValue' ||
|
|
65
|
+
prop === '__remoteFunction'
|
|
66
|
+
) {
|
|
67
|
+
// not very happy about this check here, but we need to allow for
|
|
68
|
+
// "inaccessible" objects to be tested with isSharedValue check
|
|
69
|
+
// as it is being used in the mappers when extracting inputs recursively
|
|
70
|
+
// as well as with isRemoteFunction when cloning objects recursively.
|
|
71
|
+
// Apparently we can't check if a key exists there as HostObjects always
|
|
72
|
+
// return true for such tests, so the only possibility for us is to
|
|
73
|
+
// actually access that key and see if it is set to true. We therefore
|
|
74
|
+
// need to allow for this key to be accessed here.
|
|
75
|
+
return false;
|
|
76
|
+
}
|
|
77
|
+
throw new WorkletsError(
|
|
78
|
+
`Trying to access property \`${String(
|
|
79
|
+
prop
|
|
80
|
+
)}\` of an object which cannot be sent to the UI runtime.`
|
|
81
|
+
);
|
|
82
|
+
},
|
|
83
|
+
set: () => {
|
|
84
|
+
throw new WorkletsError(
|
|
85
|
+
'Trying to write to an object which cannot be sent to the UI runtime.'
|
|
86
|
+
);
|
|
87
|
+
},
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const VALID_ARRAY_VIEWS_NAMES = [
|
|
94
|
+
'Int8Array',
|
|
95
|
+
'Uint8Array',
|
|
96
|
+
'Uint8ClampedArray',
|
|
97
|
+
'Int16Array',
|
|
98
|
+
'Uint16Array',
|
|
99
|
+
'Int32Array',
|
|
100
|
+
'Uint32Array',
|
|
101
|
+
'Float32Array',
|
|
102
|
+
'Float64Array',
|
|
103
|
+
'BigInt64Array',
|
|
104
|
+
'BigUint64Array',
|
|
105
|
+
'DataView',
|
|
106
|
+
];
|
|
107
|
+
|
|
108
|
+
const DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD = 30;
|
|
109
|
+
// Below variable stores object that we process in makeShareableCloneRecursive at the specified depth.
|
|
110
|
+
// We use it to check if later on the function reenters with the same object
|
|
111
|
+
let processedObjectAtThresholdDepth: unknown;
|
|
112
|
+
|
|
113
|
+
function makeShareableCloneRecursiveWeb<T>(value: T): ShareableRef<T> {
|
|
114
|
+
return value as ShareableRef<T>;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function makeShareableCloneRecursiveNative<T>(
|
|
118
|
+
value: T,
|
|
119
|
+
shouldPersistRemote = false,
|
|
120
|
+
depth = 0
|
|
121
|
+
): ShareableRef<T> {
|
|
122
|
+
detectCyclicObject(value, depth);
|
|
123
|
+
|
|
124
|
+
const isObject = typeof value === 'object';
|
|
125
|
+
const isFunction = typeof value === 'function';
|
|
126
|
+
|
|
127
|
+
if ((!isObject && !isFunction) || value === null) {
|
|
128
|
+
return clonePrimitive(value, shouldPersistRemote);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const cached = getFromCache(value);
|
|
132
|
+
if (cached !== undefined) {
|
|
133
|
+
return cached as ShareableRef<T>;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (Array.isArray(value)) {
|
|
137
|
+
return cloneArray(value, shouldPersistRemote, depth);
|
|
138
|
+
}
|
|
139
|
+
if (isFunction && !isWorkletFunction(value)) {
|
|
140
|
+
return cloneRemoteFunction(value, shouldPersistRemote);
|
|
141
|
+
}
|
|
142
|
+
if (isHostObject(value)) {
|
|
143
|
+
return cloneHostObject(value, shouldPersistRemote);
|
|
144
|
+
}
|
|
145
|
+
if (isPlainJSObject(value) && value.__workletContextObjectFactory) {
|
|
146
|
+
return cloneContextObject(value);
|
|
147
|
+
}
|
|
148
|
+
if ((isPlainJSObject(value) || isFunction) && isWorkletFunction(value)) {
|
|
149
|
+
return cloneWorklet(value, shouldPersistRemote, depth);
|
|
150
|
+
}
|
|
151
|
+
if (isPlainJSObject(value) || isFunction) {
|
|
152
|
+
return clonePlainJSObject(value, shouldPersistRemote, depth);
|
|
153
|
+
}
|
|
154
|
+
if (value instanceof RegExp) {
|
|
155
|
+
return cloneRegExp(value);
|
|
156
|
+
}
|
|
157
|
+
if (value instanceof Error) {
|
|
158
|
+
return cloneError(value);
|
|
159
|
+
}
|
|
160
|
+
if (value instanceof ArrayBuffer) {
|
|
161
|
+
return cloneArrayBuffer(value, shouldPersistRemote);
|
|
162
|
+
}
|
|
163
|
+
if (ArrayBuffer.isView(value)) {
|
|
164
|
+
// typed array (e.g. Int32Array, Uint8ClampedArray) or DataView
|
|
165
|
+
return cloneArrayBufferView(value);
|
|
166
|
+
}
|
|
167
|
+
return inaccessibleObject(value);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
export interface MakeShareableClone {
|
|
171
|
+
<T>(value: T, shouldPersistRemote?: boolean, depth?: number): ShareableRef<T>;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
export const makeShareableCloneRecursive: MakeShareableClone = SHOULD_BE_USE_WEB
|
|
175
|
+
? makeShareableCloneRecursiveWeb
|
|
176
|
+
: makeShareableCloneRecursiveNative;
|
|
177
|
+
|
|
178
|
+
function detectCyclicObject(value: unknown, depth: number) {
|
|
179
|
+
if (depth >= DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD) {
|
|
180
|
+
// if we reach certain recursion depth we suspect that we are dealing with a cyclic object.
|
|
181
|
+
// this type of objects are not supported and cannot be transferred as shareable, so we
|
|
182
|
+
// implement a simple detection mechanism that remembers the value at a given depth and
|
|
183
|
+
// tests whether we try reenter this method later on with the same value. If that happens
|
|
184
|
+
// we throw an appropriate error.
|
|
185
|
+
if (depth === DETECT_CYCLIC_OBJECT_DEPTH_THRESHOLD) {
|
|
186
|
+
processedObjectAtThresholdDepth = value;
|
|
187
|
+
} else if (value === processedObjectAtThresholdDepth) {
|
|
188
|
+
throw new WorkletsError(
|
|
189
|
+
'Trying to convert a cyclic object to a shareable. This is not supported.'
|
|
190
|
+
);
|
|
191
|
+
}
|
|
192
|
+
} else {
|
|
193
|
+
processedObjectAtThresholdDepth = undefined;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function clonePrimitive<T>(
|
|
198
|
+
value: T,
|
|
199
|
+
shouldPersistRemote: boolean
|
|
200
|
+
): ShareableRef<T> {
|
|
201
|
+
return WorkletsModule.makeShareableClone(value, shouldPersistRemote);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function cloneArray<T extends unknown[]>(
|
|
205
|
+
value: T,
|
|
206
|
+
shouldPersistRemote: boolean,
|
|
207
|
+
depth: number
|
|
208
|
+
): ShareableRef<T> {
|
|
209
|
+
const clonedElements = value.map((element) =>
|
|
210
|
+
makeShareableCloneRecursive(element, shouldPersistRemote, depth + 1)
|
|
211
|
+
);
|
|
212
|
+
const clone = WorkletsModule.makeShareableClone(
|
|
213
|
+
clonedElements,
|
|
214
|
+
shouldPersistRemote,
|
|
215
|
+
value
|
|
216
|
+
) as ShareableRef<T>;
|
|
217
|
+
shareableMappingCache.set(value, clone);
|
|
218
|
+
shareableMappingCache.set(clone);
|
|
219
|
+
|
|
220
|
+
freezeObjectInDev(value);
|
|
221
|
+
return clone;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function cloneRemoteFunction<T extends object>(
|
|
225
|
+
value: T,
|
|
226
|
+
shouldPersistRemote: boolean
|
|
227
|
+
): ShareableRef<T> {
|
|
228
|
+
const clone = WorkletsModule.makeShareableClone(
|
|
229
|
+
value,
|
|
230
|
+
shouldPersistRemote,
|
|
231
|
+
value
|
|
232
|
+
);
|
|
233
|
+
shareableMappingCache.set(value, clone);
|
|
234
|
+
shareableMappingCache.set(clone);
|
|
235
|
+
|
|
236
|
+
freezeObjectInDev(value);
|
|
237
|
+
return clone;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function cloneHostObject<T extends object>(
|
|
241
|
+
value: T,
|
|
242
|
+
shouldPersistRemote: boolean
|
|
243
|
+
): ShareableRef<T> {
|
|
244
|
+
// for host objects we pass the reference to the object as shareable and
|
|
245
|
+
// then recreate new host object wrapping the same instance on the UI thread.
|
|
246
|
+
// there is no point of iterating over keys as we do for regular objects.
|
|
247
|
+
const clone = WorkletsModule.makeShareableClone(
|
|
248
|
+
value,
|
|
249
|
+
shouldPersistRemote,
|
|
250
|
+
value
|
|
251
|
+
);
|
|
252
|
+
shareableMappingCache.set(value, clone);
|
|
253
|
+
shareableMappingCache.set(clone);
|
|
254
|
+
|
|
255
|
+
return clone;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function cloneWorklet<T extends WorkletFunction>(
|
|
259
|
+
value: T,
|
|
260
|
+
shouldPersistRemote: boolean,
|
|
261
|
+
depth: number
|
|
262
|
+
): ShareableRef<T> {
|
|
263
|
+
if (__DEV__) {
|
|
264
|
+
// TODO: Restore this once we reimplement JS version checking
|
|
265
|
+
// const babelVersion = (value as WorkletFunctionDev).__initData.version;
|
|
266
|
+
// if (babelVersion !== undefined && babelVersion !== jsVersion) {
|
|
267
|
+
// throw new Error(`[Reanimated] Mismatch between JavaScript code version and Reanimated Babel plugin version (${jsVersion} vs. ${babelVersion}).
|
|
268
|
+
// See \`https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#mismatch-between-javascript-code-version-and-reanimated-babel-plugin-version\` for more details.
|
|
269
|
+
// Offending code was: \`${getWorkletCode(value)}\``);
|
|
270
|
+
// }
|
|
271
|
+
registerWorkletStackDetails(
|
|
272
|
+
value.__workletHash,
|
|
273
|
+
(value as WorkletFunctionDev).__stackDetails!
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
if ((value as WorkletFunctionDev).__stackDetails) {
|
|
277
|
+
// `Error` type of value cannot be copied to the UI thread, so we
|
|
278
|
+
// remove it after we handled it in dev mode or delete it to ignore it in production mode.
|
|
279
|
+
// Not removing this would cause an infinite loop in production mode and it just
|
|
280
|
+
// seems more elegant to handle it this way.
|
|
281
|
+
delete (value as WorkletFunctionDev).__stackDetails;
|
|
282
|
+
}
|
|
283
|
+
// to save on transferring static __initData field of worklet structure
|
|
284
|
+
// we request shareable value to persist its UI counterpart. This means
|
|
285
|
+
// that the __initData field that contains long strings represeting the
|
|
286
|
+
// worklet code, source map, and location, will always be
|
|
287
|
+
// serialized/deserialized once.
|
|
288
|
+
const clonedProps: Record<string, unknown> = {};
|
|
289
|
+
clonedProps.__initData = makeShareableCloneRecursive(
|
|
290
|
+
value.__initData,
|
|
291
|
+
true,
|
|
292
|
+
depth + 1
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
for (const [key, element] of Object.entries(value)) {
|
|
296
|
+
if (key === '__initData' && clonedProps.__initData !== undefined) {
|
|
297
|
+
continue;
|
|
298
|
+
}
|
|
299
|
+
clonedProps[key] = makeShareableCloneRecursive(
|
|
300
|
+
element,
|
|
301
|
+
shouldPersistRemote,
|
|
302
|
+
depth + 1
|
|
303
|
+
);
|
|
304
|
+
}
|
|
305
|
+
const clone = WorkletsModule.makeShareableClone(
|
|
306
|
+
clonedProps,
|
|
307
|
+
// retain all worklets
|
|
308
|
+
true,
|
|
309
|
+
value
|
|
310
|
+
) as ShareableRef<T>;
|
|
311
|
+
shareableMappingCache.set(value, clone);
|
|
312
|
+
shareableMappingCache.set(clone);
|
|
313
|
+
|
|
314
|
+
freezeObjectInDev(value);
|
|
315
|
+
return clone;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function cloneContextObject<T extends object>(value: T): ShareableRef<T> {
|
|
319
|
+
const workletContextObjectFactory = (value as Record<string, unknown>)
|
|
320
|
+
.__workletContextObjectFactory as () => T;
|
|
321
|
+
const handle = makeShareableCloneRecursive({
|
|
322
|
+
__init: () => {
|
|
323
|
+
'worklet';
|
|
324
|
+
return workletContextObjectFactory();
|
|
325
|
+
},
|
|
326
|
+
});
|
|
327
|
+
shareableMappingCache.set(value, handle);
|
|
328
|
+
return handle as ShareableRef<T>;
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
function clonePlainJSObject<T extends object>(
|
|
332
|
+
value: T,
|
|
333
|
+
shouldPersistRemote: boolean,
|
|
334
|
+
depth: number
|
|
335
|
+
): ShareableRef<T> {
|
|
336
|
+
const clonedProps: Record<string, unknown> = {};
|
|
337
|
+
for (const [key, element] of Object.entries(value)) {
|
|
338
|
+
if (key === '__initData' && clonedProps.__initData !== undefined) {
|
|
339
|
+
continue;
|
|
340
|
+
}
|
|
341
|
+
clonedProps[key] = makeShareableCloneRecursive(
|
|
342
|
+
element,
|
|
343
|
+
shouldPersistRemote,
|
|
344
|
+
depth + 1
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
const clone = WorkletsModule.makeShareableClone(
|
|
348
|
+
clonedProps,
|
|
349
|
+
shouldPersistRemote,
|
|
350
|
+
value
|
|
351
|
+
) as ShareableRef<T>;
|
|
352
|
+
shareableMappingCache.set(value, clone);
|
|
353
|
+
shareableMappingCache.set(clone);
|
|
354
|
+
|
|
355
|
+
freezeObjectInDev(value);
|
|
356
|
+
return clone;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
function cloneRegExp<T extends RegExp>(value: T): ShareableRef<T> {
|
|
360
|
+
const pattern = value.source;
|
|
361
|
+
const flags = value.flags;
|
|
362
|
+
const handle = makeShareableCloneRecursive({
|
|
363
|
+
__init: () => {
|
|
364
|
+
'worklet';
|
|
365
|
+
return new RegExp(pattern, flags);
|
|
366
|
+
},
|
|
367
|
+
}) as unknown as ShareableRef<T>;
|
|
368
|
+
shareableMappingCache.set(value, handle);
|
|
369
|
+
|
|
370
|
+
return handle;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
function cloneError<T extends Error>(value: T): ShareableRef<T> {
|
|
374
|
+
const { name, message, stack } = value;
|
|
375
|
+
const handle = makeShareableCloneRecursive({
|
|
376
|
+
__init: () => {
|
|
377
|
+
'worklet';
|
|
378
|
+
// eslint-disable-next-line reanimated/use-worklets-error
|
|
379
|
+
const error = new Error();
|
|
380
|
+
error.name = name;
|
|
381
|
+
error.message = message;
|
|
382
|
+
error.stack = stack;
|
|
383
|
+
return error;
|
|
384
|
+
},
|
|
385
|
+
});
|
|
386
|
+
shareableMappingCache.set(value, handle);
|
|
387
|
+
return handle as unknown as ShareableRef<T>;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
function cloneArrayBuffer<T extends ArrayBuffer>(
|
|
391
|
+
value: T,
|
|
392
|
+
shouldPersistRemote: boolean
|
|
393
|
+
): ShareableRef<T> {
|
|
394
|
+
const clone = WorkletsModule.makeShareableClone(
|
|
395
|
+
value,
|
|
396
|
+
shouldPersistRemote,
|
|
397
|
+
value
|
|
398
|
+
);
|
|
399
|
+
shareableMappingCache.set(value, clone);
|
|
400
|
+
shareableMappingCache.set(clone);
|
|
401
|
+
|
|
402
|
+
return clone;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
function cloneArrayBufferView<T extends ArrayBufferView>(
|
|
406
|
+
value: T
|
|
407
|
+
): ShareableRef<T> {
|
|
408
|
+
const buffer = value.buffer;
|
|
409
|
+
const typeName = value.constructor.name;
|
|
410
|
+
const handle = makeShareableCloneRecursive({
|
|
411
|
+
__init: () => {
|
|
412
|
+
'worklet';
|
|
413
|
+
if (!VALID_ARRAY_VIEWS_NAMES.includes(typeName)) {
|
|
414
|
+
throw new WorkletsError(`Invalid array view name \`${typeName}\`.`);
|
|
415
|
+
}
|
|
416
|
+
const constructor = global[typeName as keyof typeof global];
|
|
417
|
+
if (constructor === undefined) {
|
|
418
|
+
throw new WorkletsError(`Constructor for \`${typeName}\` not found.`);
|
|
419
|
+
}
|
|
420
|
+
return new constructor(buffer);
|
|
421
|
+
},
|
|
422
|
+
}) as unknown as ShareableRef<T>;
|
|
423
|
+
shareableMappingCache.set(value, handle);
|
|
424
|
+
|
|
425
|
+
return handle;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
function inaccessibleObject<T extends object>(value: T): ShareableRef<T> {
|
|
429
|
+
// This is reached for object types that are not of plain Object.prototype.
|
|
430
|
+
// We don't support such objects from being transferred as shareables to
|
|
431
|
+
// the UI runtime and hence we replace them with "inaccessible object"
|
|
432
|
+
// which is implemented as a Proxy object that throws on any attempt
|
|
433
|
+
// of accessing its fields. We argue that such objects can sometimes leak
|
|
434
|
+
// as attributes of objects being captured by worklets but should never
|
|
435
|
+
// be used on the UI runtime regardless. If they are being accessed, the user
|
|
436
|
+
// will get an appropriate error message.
|
|
437
|
+
const clone = makeShareableCloneRecursive<T>(INACCESSIBLE_OBJECT as T);
|
|
438
|
+
shareableMappingCache.set(value, clone);
|
|
439
|
+
return clone;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
const WORKLET_CODE_THRESHOLD = 255;
|
|
443
|
+
|
|
444
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
445
|
+
function getWorkletCode(value: WorkletFunction) {
|
|
446
|
+
const code = value?.__initData?.code;
|
|
447
|
+
if (!code) {
|
|
448
|
+
return 'unknown';
|
|
449
|
+
}
|
|
450
|
+
if (code.length > WORKLET_CODE_THRESHOLD) {
|
|
451
|
+
return `${code.substring(0, WORKLET_CODE_THRESHOLD)}...`;
|
|
452
|
+
}
|
|
453
|
+
return code;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
type RemoteFunction<T> = {
|
|
457
|
+
__remoteFunction: FlatShareableRef<T>;
|
|
458
|
+
};
|
|
459
|
+
|
|
460
|
+
function isRemoteFunction<T>(value: {
|
|
461
|
+
__remoteFunction?: unknown;
|
|
462
|
+
}): value is RemoteFunction<T> {
|
|
463
|
+
'worklet';
|
|
464
|
+
return !!value.__remoteFunction;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* We freeze
|
|
469
|
+
*
|
|
470
|
+
* - Arrays,
|
|
471
|
+
* - Remote functions,
|
|
472
|
+
* - Plain JS objects,
|
|
473
|
+
*
|
|
474
|
+
* That are transformed to a shareable with a meaningful warning. This should
|
|
475
|
+
* help detect issues when someone modifies data after it's been converted.
|
|
476
|
+
* Meaning that they may be doing a faulty assumption in their code expecting
|
|
477
|
+
* that the updates are going to automatically propagate to the object sent to
|
|
478
|
+
* the UI thread. If the user really wants some objects to be mutable they
|
|
479
|
+
* should use shared values instead.
|
|
480
|
+
*/
|
|
481
|
+
function freezeObjectInDev<T extends object>(value: T) {
|
|
482
|
+
if (!__DEV__) {
|
|
483
|
+
return;
|
|
484
|
+
}
|
|
485
|
+
Object.entries(value).forEach(([key, element]) => {
|
|
486
|
+
const descriptor = Object.getOwnPropertyDescriptor(value, key)!;
|
|
487
|
+
if (!descriptor.configurable) {
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
Object.defineProperty(value, key, {
|
|
491
|
+
get() {
|
|
492
|
+
return element;
|
|
493
|
+
},
|
|
494
|
+
set() {
|
|
495
|
+
logger.warn(
|
|
496
|
+
`Tried to modify key \`${key}\` of an object which has been already passed to a worklet. See
|
|
497
|
+
https://docs.swmansion.com/react-native-reanimated/docs/guides/troubleshooting#tried-to-modify-key-of-an-object-which-has-been-converted-to-a-shareable
|
|
498
|
+
for more details.`
|
|
499
|
+
);
|
|
500
|
+
},
|
|
501
|
+
});
|
|
502
|
+
});
|
|
503
|
+
Object.preventExtensions(value);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
export function makeShareableCloneOnUIRecursive<T>(
|
|
507
|
+
value: T
|
|
508
|
+
): FlatShareableRef<T> {
|
|
509
|
+
'worklet';
|
|
510
|
+
if (SHOULD_BE_USE_WEB) {
|
|
511
|
+
// @ts-ignore web is an interesting place where we don't run a secondary VM on the UI thread
|
|
512
|
+
// see more details in the comment where USE_STUB_IMPLEMENTATION is defined.
|
|
513
|
+
return value;
|
|
514
|
+
}
|
|
515
|
+
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
516
|
+
function cloneRecursive(value: T): FlatShareableRef<T> {
|
|
517
|
+
if (
|
|
518
|
+
(typeof value === 'object' && value !== null) ||
|
|
519
|
+
typeof value === 'function'
|
|
520
|
+
) {
|
|
521
|
+
if (isHostObject(value)) {
|
|
522
|
+
// We call `_makeShareableClone` to wrap the provided HostObject
|
|
523
|
+
// inside ShareableJSRef.
|
|
524
|
+
return global._makeShareableClone(
|
|
525
|
+
value,
|
|
526
|
+
undefined
|
|
527
|
+
) as FlatShareableRef<T>;
|
|
528
|
+
}
|
|
529
|
+
if (isRemoteFunction<T>(value)) {
|
|
530
|
+
// RemoteFunctions are created by us therefore they are
|
|
531
|
+
// a Shareable out of the box and there is no need to
|
|
532
|
+
// call `_makeShareableClone`.
|
|
533
|
+
return value.__remoteFunction;
|
|
534
|
+
}
|
|
535
|
+
if (Array.isArray(value)) {
|
|
536
|
+
return global._makeShareableClone(
|
|
537
|
+
value.map(cloneRecursive),
|
|
538
|
+
undefined
|
|
539
|
+
) as FlatShareableRef<T>;
|
|
540
|
+
}
|
|
541
|
+
const toAdapt: Record<string, FlatShareableRef<T>> = {};
|
|
542
|
+
for (const [key, element] of Object.entries(value)) {
|
|
543
|
+
toAdapt[key] = cloneRecursive(element);
|
|
544
|
+
}
|
|
545
|
+
return global._makeShareableClone(toAdapt, value) as FlatShareableRef<T>;
|
|
546
|
+
}
|
|
547
|
+
return global._makeShareableClone(value, undefined);
|
|
548
|
+
}
|
|
549
|
+
return cloneRecursive(value);
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function makeShareableJS<T extends object>(value: T): T {
|
|
553
|
+
return value;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
function makeShareableNative<T extends object>(value: T): T {
|
|
557
|
+
if (shareableMappingCache.get(value)) {
|
|
558
|
+
return value;
|
|
559
|
+
}
|
|
560
|
+
const handle = makeShareableCloneRecursive({
|
|
561
|
+
__init: () => {
|
|
562
|
+
'worklet';
|
|
563
|
+
return value;
|
|
564
|
+
},
|
|
565
|
+
});
|
|
566
|
+
shareableMappingCache.set(value, handle);
|
|
567
|
+
return value;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
/**
|
|
571
|
+
* This function creates a value on UI with persistent state - changes to it on
|
|
572
|
+
* the UI thread will be seen by all worklets. Use it when you want to create a
|
|
573
|
+
* value that is read and written only on the UI thread.
|
|
574
|
+
*/
|
|
575
|
+
export const makeShareable = SHOULD_BE_USE_WEB
|
|
576
|
+
? makeShareableJS
|
|
577
|
+
: makeShareableNative;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
import type { TurboModule } from 'react-native';
|
|
3
|
+
import { TurboModuleRegistry } from 'react-native';
|
|
4
|
+
|
|
5
|
+
interface Spec extends TurboModule {
|
|
6
|
+
installTurboModule: (valueUnpackerCode: string) => boolean;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export default TurboModuleRegistry.get<Spec>('WorkletsModule');
|