@solidnative-dev/fabric-host 0.0.0-stage → 0.1.0-beta.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/LICENSE +21 -0
- package/README.md +87 -2
- package/dist/fatal-error-handler.d.ts +27 -0
- package/dist/fatal-error-handler.d.ts.map +1 -0
- package/dist/fatal-error-handler.js +106 -0
- package/dist/fatal-error-handler.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/native-binding.d.ts +183 -0
- package/dist/native-binding.d.ts.map +1 -0
- package/dist/native-binding.js +554 -0
- package/dist/native-binding.js.map +1 -0
- package/dist/native-fabric-host.d.ts +32 -0
- package/dist/native-fabric-host.d.ts.map +1 -0
- package/dist/native-fabric-host.js +275 -0
- package/dist/native-fabric-host.js.map +1 -0
- package/dist/native-transaction.d.ts +17 -0
- package/dist/native-transaction.d.ts.map +1 -0
- package/dist/native-transaction.js +124 -0
- package/dist/native-transaction.js.map +1 -0
- package/dist/react-native-0.88.d.ts +77 -0
- package/dist/react-native-0.88.d.ts.map +1 -0
- package/dist/react-native-0.88.js +79 -0
- package/dist/react-native-0.88.js.map +1 -0
- package/dist/surface-readiness.d.ts +18 -0
- package/dist/surface-readiness.d.ts.map +1 -0
- package/dist/surface-readiness.js +43 -0
- package/dist/surface-readiness.js.map +1 -0
- package/native/README.md +502 -0
- package/native/SolidNativeFabric.podspec +84 -0
- package/native/android/SolidNativeBindingsInstaller.cpp +4218 -0
- package/native/android/SolidNativeBindingsInstaller.h +8 -0
- package/native/android/SolidNativeRuntime.cmake +61 -0
- package/native/android/java/dev/solidnative/runtime/SolidNativeBindingsInstaller.kt +239 -0
- package/native/android/java/dev/solidnative/runtime/SolidNativeDebugModule.kt +191 -0
- package/native/android/java/dev/solidnative/runtime/SolidNativePackage.kt +43 -0
- package/native/android/java/dev/solidnative/runtime/SolidNativePlatformModule.kt +81 -0
- package/native/android/java/dev/solidnative/runtime/SolidNativeStartupFailureView.kt +93 -0
- package/native/android/java/dev/solidnative/runtime/SolidNativeSurface.kt +423 -0
- package/native/android/solid-native.gradle +90 -0
- package/native/apple/SolidNativeDebugRequest.mm +421 -0
- package/native/fabric/SolidNativeDebugRequest.h +12 -0
- package/native/fabric/SolidNativeDirtyNodes.h +26 -0
- package/native/fabric/SolidNativeFabricApi.cpp +224 -0
- package/native/fabric/SolidNativeFabricApi.h +107 -0
- package/native/fabric/SolidNativeFabricApplication.h +32 -0
- package/native/fabric/SolidNativeFabricApplication.mm +373 -0
- package/native/fabric/SolidNativeFabricJSIBinding.h +43 -0
- package/native/fabric/SolidNativeFabricJSIBinding.mm +4491 -0
- package/native/fabric/SolidNativeFabricSmokeApplication.h +26 -0
- package/native/fabric/SolidNativeFabricSmokeApplication.mm +331 -0
- package/native/fabric/SolidNativeFabricSurface.h +63 -0
- package/native/fabric/SolidNativeFabricSurface.mm +549 -0
- package/native/fabric/SolidNativeFabricTransactionCoordinator.cpp +1275 -0
- package/native/fabric/SolidNativeFabricTransactionCoordinator.h +90 -0
- package/native/fabric/SolidNativeFabricTransactionCoordinatorApple.mm +97 -0
- package/native/fabric/SolidNativeRootViewController.h +20 -0
- package/native/fabric/SolidNativeRootViewController.mm +83 -0
- package/native/fabric/SolidNativeStagedNodeMap.h +104 -0
- package/native/fabric/SolidNativeStartupFailureView.h +17 -0
- package/native/fabric/SolidNativeStartupFailureView.mm +77 -0
- package/native/react-native-boundary.json +106 -0
- package/native/worklets/SolidNativeUIWorklet.cpp +1080 -0
- package/native/worklets/SolidNativeUIWorklet.h +245 -0
- package/package.json +41 -3
- package/scripts/verify-react-native-boundary.mjs +282 -0
|
@@ -0,0 +1,4491 @@
|
|
|
1
|
+
#import "SolidNativeFabricJSIBinding.h"
|
|
2
|
+
|
|
3
|
+
#import "SolidNativeFabricSurface.h"
|
|
4
|
+
|
|
5
|
+
#import <QuartzCore/QuartzCore.h>
|
|
6
|
+
|
|
7
|
+
#include <algorithm>
|
|
8
|
+
#include <array>
|
|
9
|
+
#include <atomic>
|
|
10
|
+
#include <cmath>
|
|
11
|
+
#include <cstddef>
|
|
12
|
+
#include <cstdint>
|
|
13
|
+
#include <functional>
|
|
14
|
+
#include <limits>
|
|
15
|
+
#include <mutex>
|
|
16
|
+
#include <optional>
|
|
17
|
+
#include <stdexcept>
|
|
18
|
+
#include <string>
|
|
19
|
+
#include <unordered_map>
|
|
20
|
+
#include <unordered_set>
|
|
21
|
+
#include <utility>
|
|
22
|
+
#include <vector>
|
|
23
|
+
|
|
24
|
+
#include <folly/dynamic.h>
|
|
25
|
+
#include <jsi/jsi.h>
|
|
26
|
+
#include <react/renderer/core/InstanceHandle.h>
|
|
27
|
+
#include <react/renderer/core/EventListener.h>
|
|
28
|
+
#include <react/renderer/core/RawEvent.h>
|
|
29
|
+
#include <react/renderer/core/ShadowNodeFamily.h>
|
|
30
|
+
#include <react/renderer/runtimescheduler/RuntimeScheduler.h>
|
|
31
|
+
#include <react/renderer/runtimescheduler/RuntimeSchedulerBinding.h>
|
|
32
|
+
|
|
33
|
+
#include "SolidNativeFabricTransactionCoordinator.h"
|
|
34
|
+
#include "../worklets/SolidNativeUIWorklet.h"
|
|
35
|
+
|
|
36
|
+
@interface SolidNativeDisplayLinkObserver : NSObject
|
|
37
|
+
- (instancetype)initWithCallback:(BOOL (^)(CADisplayLink *))callback;
|
|
38
|
+
- (void)start;
|
|
39
|
+
- (void)invalidate;
|
|
40
|
+
@end
|
|
41
|
+
|
|
42
|
+
@implementation SolidNativeDisplayLinkObserver {
|
|
43
|
+
CADisplayLink *_displayLink;
|
|
44
|
+
BOOL (^_callback)(CADisplayLink *);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
- (instancetype)initWithCallback:(BOOL (^)(CADisplayLink *))callback {
|
|
48
|
+
if (self = [super init]) {
|
|
49
|
+
_callback = [callback copy];
|
|
50
|
+
}
|
|
51
|
+
return self;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
- (void)start {
|
|
55
|
+
if (_callback == nil || _displayLink != nil) return;
|
|
56
|
+
_displayLink = [CADisplayLink displayLinkWithTarget:self
|
|
57
|
+
selector:@selector(onDisplayLink:)];
|
|
58
|
+
const auto maximumFramesPerSecond =
|
|
59
|
+
static_cast<float>(UIScreen.mainScreen.maximumFramesPerSecond);
|
|
60
|
+
_displayLink.preferredFrameRateRange = CAFrameRateRangeMake(
|
|
61
|
+
std::min(30.0F, maximumFramesPerSecond),
|
|
62
|
+
maximumFramesPerSecond,
|
|
63
|
+
maximumFramesPerSecond);
|
|
64
|
+
[_displayLink addToRunLoop:NSRunLoop.mainRunLoop
|
|
65
|
+
forMode:NSRunLoopCommonModes];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
- (void)onDisplayLink:(CADisplayLink *)displayLink {
|
|
69
|
+
auto callback = _callback;
|
|
70
|
+
if (callback != nil && callback(displayLink)) return;
|
|
71
|
+
[self invalidate];
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
- (void)invalidate {
|
|
75
|
+
_callback = nil;
|
|
76
|
+
[_displayLink invalidate];
|
|
77
|
+
_displayLink = nil;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
@end
|
|
81
|
+
|
|
82
|
+
@interface SolidNativePanGestureObserver : NSObject <UIGestureRecognizerDelegate>
|
|
83
|
+
- (instancetype)initWithView:(UIView *)view
|
|
84
|
+
callback:(void (^)(NSInteger,
|
|
85
|
+
CGFloat,
|
|
86
|
+
CGFloat,
|
|
87
|
+
NSTimeInterval,
|
|
88
|
+
CGFloat,
|
|
89
|
+
CGFloat))callback;
|
|
90
|
+
- (void)invalidate;
|
|
91
|
+
@end
|
|
92
|
+
|
|
93
|
+
@implementation SolidNativePanGestureObserver {
|
|
94
|
+
__weak UIView *_view;
|
|
95
|
+
UIPanGestureRecognizer *_recognizer;
|
|
96
|
+
void (^_callback)(NSInteger, CGFloat, CGFloat, NSTimeInterval, CGFloat, CGFloat);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
- (instancetype)initWithView:(UIView *)view
|
|
100
|
+
callback:(void (^)(NSInteger,
|
|
101
|
+
CGFloat,
|
|
102
|
+
CGFloat,
|
|
103
|
+
NSTimeInterval,
|
|
104
|
+
CGFloat,
|
|
105
|
+
CGFloat))callback {
|
|
106
|
+
if (self = [super init]) {
|
|
107
|
+
_view = view;
|
|
108
|
+
_callback = [callback copy];
|
|
109
|
+
_recognizer = [[UIPanGestureRecognizer alloc]
|
|
110
|
+
initWithTarget:self
|
|
111
|
+
action:@selector(onPan:)];
|
|
112
|
+
_recognizer.minimumNumberOfTouches = 1;
|
|
113
|
+
_recognizer.maximumNumberOfTouches = 1;
|
|
114
|
+
_recognizer.cancelsTouchesInView = NO;
|
|
115
|
+
_recognizer.delegate = self;
|
|
116
|
+
[view addGestureRecognizer:_recognizer];
|
|
117
|
+
}
|
|
118
|
+
return self;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
- (void)onPan:(UIPanGestureRecognizer *)recognizer {
|
|
122
|
+
auto callback = _callback;
|
|
123
|
+
if (callback == nil) return;
|
|
124
|
+
NSInteger phase;
|
|
125
|
+
switch (recognizer.state) {
|
|
126
|
+
case UIGestureRecognizerStateBegan:
|
|
127
|
+
phase = 0;
|
|
128
|
+
break;
|
|
129
|
+
case UIGestureRecognizerStateChanged:
|
|
130
|
+
phase = 1;
|
|
131
|
+
break;
|
|
132
|
+
case UIGestureRecognizerStateEnded:
|
|
133
|
+
phase = 2;
|
|
134
|
+
break;
|
|
135
|
+
case UIGestureRecognizerStateCancelled:
|
|
136
|
+
case UIGestureRecognizerStateFailed:
|
|
137
|
+
phase = 3;
|
|
138
|
+
break;
|
|
139
|
+
default:
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
UIView *coordinateView = recognizer.view.superview ?: recognizer.view;
|
|
143
|
+
const auto translation = [recognizer translationInView:coordinateView];
|
|
144
|
+
const auto velocity = [recognizer velocityInView:coordinateView];
|
|
145
|
+
callback(
|
|
146
|
+
phase,
|
|
147
|
+
translation.x,
|
|
148
|
+
translation.y,
|
|
149
|
+
CACurrentMediaTime() * 1000.0,
|
|
150
|
+
velocity.x,
|
|
151
|
+
velocity.y);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
|
|
155
|
+
shouldRecognizeSimultaneouslyWithGestureRecognizer:
|
|
156
|
+
(UIGestureRecognizer *)otherGestureRecognizer {
|
|
157
|
+
return YES;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
- (void)invalidate {
|
|
161
|
+
_callback = nil;
|
|
162
|
+
auto view = _view;
|
|
163
|
+
if (view != nil && _recognizer != nil) {
|
|
164
|
+
[view removeGestureRecognizer:_recognizer];
|
|
165
|
+
}
|
|
166
|
+
_recognizer.delegate = nil;
|
|
167
|
+
_recognizer = nil;
|
|
168
|
+
_view = nil;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
@end
|
|
172
|
+
|
|
173
|
+
namespace {
|
|
174
|
+
|
|
175
|
+
using facebook::jsi::Array;
|
|
176
|
+
using facebook::jsi::Function;
|
|
177
|
+
using facebook::jsi::JSError;
|
|
178
|
+
using facebook::jsi::Object;
|
|
179
|
+
using facebook::jsi::PropNameID;
|
|
180
|
+
using facebook::jsi::Runtime;
|
|
181
|
+
using facebook::jsi::String;
|
|
182
|
+
using facebook::jsi::Value;
|
|
183
|
+
|
|
184
|
+
using solid_native::fabric::react_native::NodeIdentity;
|
|
185
|
+
using solid_native::fabric::react_native::NodeIdentityMap;
|
|
186
|
+
using solid_native::fabric::react_native::NativeResourceResolver;
|
|
187
|
+
using solid_native::fabric::react_native::ReactNativeVersion;
|
|
188
|
+
using solid_native::fabric::react_native::TransactionCoordinator;
|
|
189
|
+
|
|
190
|
+
constexpr char GlobalBindingName[] = "__solidNativeHost";
|
|
191
|
+
constexpr char ReactNativeStopSurfaceGlobal[] = "RN$stopSurface";
|
|
192
|
+
constexpr int HostContractVersion = 1;
|
|
193
|
+
constexpr size_t MaximumTransportDepth = 64;
|
|
194
|
+
constexpr size_t MaximumTransportValues = 100000;
|
|
195
|
+
constexpr size_t MaximumRetainedResources = 64;
|
|
196
|
+
constexpr size_t MaximumResourceKindLength = 128;
|
|
197
|
+
constexpr size_t MaximumActiveUIWorklets = 256;
|
|
198
|
+
constexpr size_t MaximumFatalErrorNameBytes = 512;
|
|
199
|
+
constexpr size_t MaximumFatalErrorMessageBytes = 8192;
|
|
200
|
+
constexpr NSUInteger MaximumUIWorkletMountAttempts = 300;
|
|
201
|
+
constexpr NSUInteger MaximumMountObservationAttempts = 2000;
|
|
202
|
+
constexpr int64_t MountObservationRetryNanoseconds = 5 * NSEC_PER_MSEC;
|
|
203
|
+
|
|
204
|
+
std::string utf8(NSString *value);
|
|
205
|
+
folly::dynamic dynamicFromJSI(
|
|
206
|
+
Runtime &runtime,
|
|
207
|
+
const Value &value,
|
|
208
|
+
const std::string &path,
|
|
209
|
+
size_t depth,
|
|
210
|
+
size_t &valueCount,
|
|
211
|
+
std::vector<Value> &ancestors);
|
|
212
|
+
|
|
213
|
+
void installEmptySurfaceStopCompatibility(Runtime &runtime) {
|
|
214
|
+
auto global = runtime.global();
|
|
215
|
+
if (!global.getProperty(runtime, ReactNativeStopSurfaceGlobal)
|
|
216
|
+
.isUndefined()) {
|
|
217
|
+
return;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// React Native exposes startEmptySurface, but UIManager::stopSurface still
|
|
221
|
+
// unconditionally calls the React renderer's RN$stopSurface global. Solid
|
|
222
|
+
// Native has no React root to unmount. Supply the missing empty-surface stop
|
|
223
|
+
// half without replacing a renderer-owned function when one exists.
|
|
224
|
+
global.setProperty(
|
|
225
|
+
runtime,
|
|
226
|
+
ReactNativeStopSurfaceGlobal,
|
|
227
|
+
Function::createFromHostFunction(
|
|
228
|
+
runtime,
|
|
229
|
+
PropNameID::forAscii(runtime, ReactNativeStopSurfaceGlobal),
|
|
230
|
+
1,
|
|
231
|
+
[](Runtime &,
|
|
232
|
+
const Value &,
|
|
233
|
+
const Value *,
|
|
234
|
+
size_t) { return Value::undefined(); }));
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
struct RoutedEvent final {
|
|
238
|
+
int64_t node;
|
|
239
|
+
std::string name;
|
|
240
|
+
};
|
|
241
|
+
|
|
242
|
+
std::vector<std::string> semanticEventNames(const std::string &nativeName) {
|
|
243
|
+
auto name = nativeName;
|
|
244
|
+
// EventEmitter::dispatchEvent normalizes native names such as touchEnd to
|
|
245
|
+
// topTouchEnd before Scheduler EventListeners observe them. Keep routing
|
|
246
|
+
// independent of that legacy React prefix so platform adapters can use the
|
|
247
|
+
// native event names they actually emit.
|
|
248
|
+
if (name.size() > 3 && name.compare(0, 3, "top") == 0) {
|
|
249
|
+
name = name.substr(3);
|
|
250
|
+
if (!name.empty() && name[0] >= 'A' && name[0] <= 'Z') {
|
|
251
|
+
name[0] = static_cast<char>(name[0] - 'A' + 'a');
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
if (name == "click") return {"press"};
|
|
255
|
+
// React Native's JavaScript Pressability layer normally synthesizes click
|
|
256
|
+
// semantics from these iOS touch events. Solid owns that layer here, so one
|
|
257
|
+
// native release can fan out to both pressOut and press subscribers.
|
|
258
|
+
if (name == "touchStart" || name == "pointerDown") {
|
|
259
|
+
return {"pressIn"};
|
|
260
|
+
}
|
|
261
|
+
if (name == "touchMove" || name == "pointerMove") {
|
|
262
|
+
return {"pressMove"};
|
|
263
|
+
}
|
|
264
|
+
if (name == "pointerEnter") return {"hoverIn"};
|
|
265
|
+
if (name == "pointerLeave") return {"hoverOut"};
|
|
266
|
+
if (name == "touchEnd") return {"pressOut", "press"};
|
|
267
|
+
if (name == "touchCancel" || name == "pointerCancel") {
|
|
268
|
+
return {"pressCancel"};
|
|
269
|
+
}
|
|
270
|
+
if (name == "pointerUp") return {"pressOut"};
|
|
271
|
+
// TextInput and Switch both inherit React Native's generic `change` event.
|
|
272
|
+
// Route both portable candidates; the node's declared subscription selects
|
|
273
|
+
// the one that is semantically valid for that component.
|
|
274
|
+
if (name == "change") return {"changeText", "valueChange"};
|
|
275
|
+
if (name == "appear") return {"focus"};
|
|
276
|
+
if (name == "disappear") return {"blur"};
|
|
277
|
+
if (name == "dismissed") return {"dismiss"};
|
|
278
|
+
if (name == "nativeDismissCancelled") return {"nativeDismissCancel"};
|
|
279
|
+
if (name == "sheetDetentChanged") return {"sheetDetentChange"};
|
|
280
|
+
if (name == "finishTransitioning") return {"transitionEnd"};
|
|
281
|
+
return {name};
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
bool eventBubbles(const std::string &name) {
|
|
285
|
+
return name == "endEditing" || name == "keyPress" ||
|
|
286
|
+
name == "longPress" || name == "press" || name == "pressCancel" ||
|
|
287
|
+
name == "pressIn" || name == "pressMove" || name == "pressOut" ||
|
|
288
|
+
name == "scroll" ||
|
|
289
|
+
name == "scrollBeginDrag" || name == "scrollEndDrag" ||
|
|
290
|
+
name == "valueChange";
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
const char *eventPriority(facebook::react::RawEvent::Category category) {
|
|
294
|
+
using Category = facebook::react::RawEvent::Category;
|
|
295
|
+
switch (category) {
|
|
296
|
+
case Category::Discrete:
|
|
297
|
+
case Category::ContinuousStart:
|
|
298
|
+
case Category::ContinuousEnd:
|
|
299
|
+
return "discrete";
|
|
300
|
+
case Category::Continuous:
|
|
301
|
+
return "continuous";
|
|
302
|
+
case Category::Unspecified:
|
|
303
|
+
case Category::Idle:
|
|
304
|
+
return "default";
|
|
305
|
+
}
|
|
306
|
+
return "default";
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const char *semanticEventPriority(
|
|
310
|
+
const std::string &name,
|
|
311
|
+
facebook::react::RawEvent::Category category) {
|
|
312
|
+
if (name == "changeText" || name == "keyPress" || name == "valueChange" ||
|
|
313
|
+
name == "submitEditing" || name == "scrollBeginDrag" ||
|
|
314
|
+
name == "scrollEndDrag" || name == "accessibilityAction" ||
|
|
315
|
+
name == "accessibilityEscape" || name == "accessibilityTap" ||
|
|
316
|
+
name == "magicTap") {
|
|
317
|
+
return "discrete";
|
|
318
|
+
}
|
|
319
|
+
if (name == "contentSizeChange" || name == "insetsChange" ||
|
|
320
|
+
name == "layout" || name == "selectionChange") {
|
|
321
|
+
return "default";
|
|
322
|
+
}
|
|
323
|
+
if (name == "scroll") return "continuous";
|
|
324
|
+
if (name == "tabSelected" || name == "tabSelectionRejected" ||
|
|
325
|
+
name == "tabSelectionPrevented" ||
|
|
326
|
+
name == "pressHeaderBarButtonItem" ||
|
|
327
|
+
name == "pressHeaderBarButtonMenuItem") {
|
|
328
|
+
return "discrete";
|
|
329
|
+
}
|
|
330
|
+
// Android's corresponding legacy path categorizes coalescible Image
|
|
331
|
+
// completion events as Continuous. Normalize resource completion at this
|
|
332
|
+
// framework-neutral boundary so both platform adapters expose one contract.
|
|
333
|
+
// Screen visibility notifications likewise should not inherit user-input
|
|
334
|
+
// scheduling semantics from the backend emitter.
|
|
335
|
+
if (name == "load" || name == "error" || name == "focus" ||
|
|
336
|
+
name == "blur" || name == "transitionEnd" || name == "show" ||
|
|
337
|
+
name == "dismiss" || name == "orientationChange" ||
|
|
338
|
+
name == "requestClose" || name == "willAppear" ||
|
|
339
|
+
name == "didAppear" || name == "willDisappear" ||
|
|
340
|
+
name == "didDisappear" || name == "moreTabSelected" ||
|
|
341
|
+
name == "sheetDetentChange") {
|
|
342
|
+
return "default";
|
|
343
|
+
}
|
|
344
|
+
return eventPriority(category);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
void installEmptySurfaceStopHandler(Runtime &runtime) {
|
|
348
|
+
auto global = runtime.global();
|
|
349
|
+
auto existing = global.getProperty(runtime, "RN$stopSurface");
|
|
350
|
+
if (existing.isObject() &&
|
|
351
|
+
existing.getObject(runtime).isFunction(runtime)) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// React Native 0.87 starts an empty-module Surface through
|
|
356
|
+
// UIManager::startEmptySurface, so ReactFabric never installs its global
|
|
357
|
+
// stop function. SurfaceHandler::stop nevertheless calls the same global.
|
|
358
|
+
// Solid already committed the empty tree; this handler completes that
|
|
359
|
+
// React-only half of the stop contract without loading ReactFabric.
|
|
360
|
+
global.setProperty(
|
|
361
|
+
runtime,
|
|
362
|
+
"RN$stopSurface",
|
|
363
|
+
Function::createFromHostFunction(
|
|
364
|
+
runtime,
|
|
365
|
+
PropNameID::forAscii(runtime, "RN$stopSurface"),
|
|
366
|
+
1,
|
|
367
|
+
[](Runtime &,
|
|
368
|
+
const Value &,
|
|
369
|
+
const Value *,
|
|
370
|
+
size_t) -> Value { return Value(); }));
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
enum class IOSViewOutput : size_t {
|
|
374
|
+
Opacity = 0,
|
|
375
|
+
TranslateX = 1,
|
|
376
|
+
TranslateY = 2,
|
|
377
|
+
ScaleX = 3,
|
|
378
|
+
ScaleY = 4,
|
|
379
|
+
Rotation = 5,
|
|
380
|
+
};
|
|
381
|
+
|
|
382
|
+
constexpr size_t IOSViewOutputCount = 6;
|
|
383
|
+
|
|
384
|
+
IOSViewOutput requireIOSViewOutput(const std::string &name) {
|
|
385
|
+
if (name == "opacity") return IOSViewOutput::Opacity;
|
|
386
|
+
if (name == "translateX") return IOSViewOutput::TranslateX;
|
|
387
|
+
if (name == "translateY") return IOSViewOutput::TranslateY;
|
|
388
|
+
if (name == "scaleX") return IOSViewOutput::ScaleX;
|
|
389
|
+
if (name == "scaleY") return IOSViewOutput::ScaleY;
|
|
390
|
+
if (name == "rotation") return IOSViewOutput::Rotation;
|
|
391
|
+
throw std::invalid_argument(
|
|
392
|
+
"iOS native view worklets do not support output channel " + name +
|
|
393
|
+
".");
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
struct IOSUIWorkletTimingState final {
|
|
397
|
+
std::vector<double> from;
|
|
398
|
+
solid_native::worklets::UIWorkletTimingSequence definition;
|
|
399
|
+
std::optional<int64_t> startedAtNanoseconds;
|
|
400
|
+
};
|
|
401
|
+
|
|
402
|
+
struct IOSUIWorkletSpringState final {
|
|
403
|
+
std::vector<double> from;
|
|
404
|
+
std::vector<double> to;
|
|
405
|
+
solid_native::worklets::UIWorkletSpring definition;
|
|
406
|
+
std::optional<int64_t> startedAtNanoseconds;
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
struct IOSUIWorkletDecayState final {
|
|
410
|
+
std::vector<double> from;
|
|
411
|
+
std::vector<double> velocities;
|
|
412
|
+
double initialSpeed;
|
|
413
|
+
solid_native::worklets::UIWorkletDecay definition;
|
|
414
|
+
std::optional<int64_t> startedAtNanoseconds;
|
|
415
|
+
};
|
|
416
|
+
|
|
417
|
+
struct IOSUIWorkletPanState final {
|
|
418
|
+
solid_native::worklets::UIWorkletPanGesture definition;
|
|
419
|
+
bool active{false};
|
|
420
|
+
double originX{0};
|
|
421
|
+
double originY{0};
|
|
422
|
+
int64_t sequence{0};
|
|
423
|
+
std::optional<double> timestamp;
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
struct IOSUIWorkletState final {
|
|
427
|
+
IOSUIWorkletState(
|
|
428
|
+
int64_t handleValue,
|
|
429
|
+
int64_t targetNodeValue,
|
|
430
|
+
facebook::react::Tag targetTagValue,
|
|
431
|
+
solid_native::worklets::UIWorkletGraph graphValue,
|
|
432
|
+
std::vector<IOSViewOutput> channelValues)
|
|
433
|
+
: handle(handleValue),
|
|
434
|
+
targetNode(targetNodeValue),
|
|
435
|
+
targetTag(targetTagValue),
|
|
436
|
+
graph(std::move(graphValue)),
|
|
437
|
+
channels(std::move(channelValues)),
|
|
438
|
+
inputs(graph.initialInputs()),
|
|
439
|
+
outputs(graph.evaluate(inputs)) {}
|
|
440
|
+
|
|
441
|
+
mutable std::mutex mutex;
|
|
442
|
+
const int64_t handle;
|
|
443
|
+
const int64_t targetNode;
|
|
444
|
+
const facebook::react::Tag targetTag;
|
|
445
|
+
const solid_native::worklets::UIWorkletGraph graph;
|
|
446
|
+
const std::vector<IOSViewOutput> channels;
|
|
447
|
+
std::vector<double> inputs;
|
|
448
|
+
std::vector<solid_native::worklets::UIWorkletOutputValue> outputs;
|
|
449
|
+
bool active{true};
|
|
450
|
+
bool framePending{false};
|
|
451
|
+
uint64_t frameGeneration{0};
|
|
452
|
+
bool hasTimestamp{false};
|
|
453
|
+
double timestamp{0};
|
|
454
|
+
int64_t sequence{0};
|
|
455
|
+
int64_t appliedSequence{0};
|
|
456
|
+
int64_t frameTimeNanoseconds{0};
|
|
457
|
+
NSUInteger mountAttempts{0};
|
|
458
|
+
std::optional<IOSUIWorkletTimingState> timing;
|
|
459
|
+
double timingProgress{0};
|
|
460
|
+
solid_native::worklets::UIWorkletFrameStatistics timingFrameStatistics;
|
|
461
|
+
std::optional<IOSUIWorkletSpringState> spring;
|
|
462
|
+
std::optional<double> springPosition;
|
|
463
|
+
std::optional<double> springVelocity;
|
|
464
|
+
solid_native::worklets::UIWorkletFrameStatistics springFrameStatistics;
|
|
465
|
+
std::optional<IOSUIWorkletDecayState> decay;
|
|
466
|
+
std::optional<double> decayElapsedMilliseconds;
|
|
467
|
+
std::optional<double> decaySpeed;
|
|
468
|
+
solid_native::worklets::UIWorkletFrameStatistics decayFrameStatistics;
|
|
469
|
+
std::optional<IOSUIWorkletPanState> pan;
|
|
470
|
+
__strong SolidNativeDisplayLinkObserver *frameObserver{nil};
|
|
471
|
+
__strong SolidNativePanGestureObserver *panObserver{nil};
|
|
472
|
+
bool applied{false};
|
|
473
|
+
};
|
|
474
|
+
|
|
475
|
+
Object makeUIWorkletFrameStatistics(
|
|
476
|
+
Runtime &runtime,
|
|
477
|
+
const solid_native::worklets::UIWorkletFrameStatisticsSnapshot &value) {
|
|
478
|
+
Object statistics(runtime);
|
|
479
|
+
statistics.setProperty(
|
|
480
|
+
runtime, "frameCount", static_cast<double>(value.frameCount));
|
|
481
|
+
statistics.setProperty(
|
|
482
|
+
runtime, "intervalCount", static_cast<double>(value.intervalCount));
|
|
483
|
+
statistics.setProperty(
|
|
484
|
+
runtime,
|
|
485
|
+
"sampledIntervalCount",
|
|
486
|
+
static_cast<double>(value.sampledIntervalCount));
|
|
487
|
+
statistics.setProperty(
|
|
488
|
+
runtime,
|
|
489
|
+
"droppedIntervalSampleCount",
|
|
490
|
+
static_cast<double>(value.droppedIntervalSampleCount));
|
|
491
|
+
statistics.setProperty(
|
|
492
|
+
runtime,
|
|
493
|
+
"firstFrameTimeMilliseconds",
|
|
494
|
+
value.firstFrameTimeMilliseconds);
|
|
495
|
+
statistics.setProperty(
|
|
496
|
+
runtime,
|
|
497
|
+
"lastFrameTimeMilliseconds",
|
|
498
|
+
value.lastFrameTimeMilliseconds);
|
|
499
|
+
statistics.setProperty(
|
|
500
|
+
runtime,
|
|
501
|
+
"minimumFrameIntervalMilliseconds",
|
|
502
|
+
value.minimumFrameIntervalMilliseconds);
|
|
503
|
+
statistics.setProperty(
|
|
504
|
+
runtime,
|
|
505
|
+
"maximumFrameIntervalMilliseconds",
|
|
506
|
+
value.maximumFrameIntervalMilliseconds);
|
|
507
|
+
statistics.setProperty(
|
|
508
|
+
runtime,
|
|
509
|
+
"meanFrameIntervalMilliseconds",
|
|
510
|
+
value.meanFrameIntervalMilliseconds);
|
|
511
|
+
statistics.setProperty(
|
|
512
|
+
runtime,
|
|
513
|
+
"p50FrameIntervalMilliseconds",
|
|
514
|
+
value.p50FrameIntervalMilliseconds);
|
|
515
|
+
statistics.setProperty(
|
|
516
|
+
runtime,
|
|
517
|
+
"p95FrameIntervalMilliseconds",
|
|
518
|
+
value.p95FrameIntervalMilliseconds);
|
|
519
|
+
statistics.setProperty(
|
|
520
|
+
runtime,
|
|
521
|
+
"p99FrameIntervalMilliseconds",
|
|
522
|
+
value.p99FrameIntervalMilliseconds);
|
|
523
|
+
return statistics;
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
void invalidateIOSUIWorkletPanObserver(
|
|
527
|
+
SolidNativePanGestureObserver *observer) {
|
|
528
|
+
if (observer == nil) return;
|
|
529
|
+
if (NSThread.isMainThread) {
|
|
530
|
+
[observer invalidate];
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
534
|
+
[observer invalidate];
|
|
535
|
+
});
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
void invalidateIOSUIWorkletFrameObserver(
|
|
539
|
+
SolidNativeDisplayLinkObserver *observer) {
|
|
540
|
+
if (observer == nil) return;
|
|
541
|
+
if (NSThread.isMainThread) {
|
|
542
|
+
[observer invalidate];
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
546
|
+
[observer invalidate];
|
|
547
|
+
});
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
void deactivateIOSUIWorklet(
|
|
551
|
+
const std::shared_ptr<IOSUIWorkletState> &state) {
|
|
552
|
+
SolidNativeDisplayLinkObserver *frameObserver = nil;
|
|
553
|
+
SolidNativePanGestureObserver *observer = nil;
|
|
554
|
+
{
|
|
555
|
+
std::lock_guard lock(state->mutex);
|
|
556
|
+
state->active = false;
|
|
557
|
+
state->framePending = false;
|
|
558
|
+
state->frameGeneration++;
|
|
559
|
+
if (state->pan.has_value()) state->pan->active = false;
|
|
560
|
+
frameObserver = state->frameObserver;
|
|
561
|
+
state->frameObserver = nil;
|
|
562
|
+
observer = state->panObserver;
|
|
563
|
+
state->panObserver = nil;
|
|
564
|
+
}
|
|
565
|
+
invalidateIOSUIWorkletFrameObserver(frameObserver);
|
|
566
|
+
invalidateIOSUIWorkletPanObserver(observer);
|
|
567
|
+
}
|
|
568
|
+
|
|
569
|
+
std::atomic<int64_t> nextIOSUIWorkletHandle{1};
|
|
570
|
+
|
|
571
|
+
class IOSUIWorkletRegistry final {
|
|
572
|
+
public:
|
|
573
|
+
std::shared_ptr<IOSUIWorkletState> install(
|
|
574
|
+
int64_t targetNode,
|
|
575
|
+
facebook::react::Tag targetTag,
|
|
576
|
+
solid_native::worklets::UIWorkletGraph graph) {
|
|
577
|
+
std::vector<IOSViewOutput> channels;
|
|
578
|
+
channels.reserve(graph.outputNames().size());
|
|
579
|
+
for (const auto &name : graph.outputNames()) {
|
|
580
|
+
channels.push_back(requireIOSViewOutput(name));
|
|
581
|
+
}
|
|
582
|
+
graph.evaluate(graph.initialInputs());
|
|
583
|
+
const auto handle =
|
|
584
|
+
nextIOSUIWorkletHandle.fetch_add(1, std::memory_order_relaxed);
|
|
585
|
+
if (handle <= 0 || handle > 9007199254740991LL) {
|
|
586
|
+
throw std::runtime_error(
|
|
587
|
+
"The iOS UI worklet handle space is exhausted.");
|
|
588
|
+
}
|
|
589
|
+
auto state = std::make_shared<IOSUIWorkletState>(
|
|
590
|
+
handle,
|
|
591
|
+
targetNode,
|
|
592
|
+
targetTag,
|
|
593
|
+
std::move(graph),
|
|
594
|
+
std::move(channels));
|
|
595
|
+
{
|
|
596
|
+
std::lock_guard lock(mutex_);
|
|
597
|
+
if (active_.size() >= MaximumActiveUIWorklets) {
|
|
598
|
+
throw std::runtime_error("The iOS UI worklet registry is full.");
|
|
599
|
+
}
|
|
600
|
+
active_.emplace(handle, state);
|
|
601
|
+
issued_.insert(handle);
|
|
602
|
+
}
|
|
603
|
+
return state;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
std::shared_ptr<IOSUIWorkletState> update(
|
|
607
|
+
int64_t handle,
|
|
608
|
+
std::vector<double> inputs,
|
|
609
|
+
double timestamp) {
|
|
610
|
+
auto state = requireActive(handle);
|
|
611
|
+
{
|
|
612
|
+
std::lock_guard lock(state->mutex);
|
|
613
|
+
if (!state->active) {
|
|
614
|
+
throw std::invalid_argument(
|
|
615
|
+
"The iOS UI worklet graph is no longer active.");
|
|
616
|
+
}
|
|
617
|
+
if (state->pan.has_value()) {
|
|
618
|
+
throw std::invalid_argument(
|
|
619
|
+
"The iOS UI worklet input vector is owned by its native pan gesture.");
|
|
620
|
+
}
|
|
621
|
+
if (!std::isfinite(timestamp) || timestamp < 0) {
|
|
622
|
+
throw std::invalid_argument(
|
|
623
|
+
"UI worklet timestamps must be finite and non-negative.");
|
|
624
|
+
}
|
|
625
|
+
if (state->hasTimestamp && timestamp < state->timestamp) {
|
|
626
|
+
throw std::invalid_argument(
|
|
627
|
+
"UI worklet input timestamps must be monotonic.");
|
|
628
|
+
}
|
|
629
|
+
// Evaluate the complete candidate before publishing any input.
|
|
630
|
+
state->graph.evaluate(inputs);
|
|
631
|
+
state->inputs = std::move(inputs);
|
|
632
|
+
state->timestamp = timestamp;
|
|
633
|
+
state->hasTimestamp = true;
|
|
634
|
+
state->mountAttempts = 0;
|
|
635
|
+
state->timing.reset();
|
|
636
|
+
state->timingProgress = 0;
|
|
637
|
+
state->timingFrameStatistics.reset();
|
|
638
|
+
state->spring.reset();
|
|
639
|
+
state->springPosition.reset();
|
|
640
|
+
state->springVelocity.reset();
|
|
641
|
+
state->springFrameStatistics.reset();
|
|
642
|
+
state->decay.reset();
|
|
643
|
+
state->decayElapsedMilliseconds.reset();
|
|
644
|
+
state->decaySpeed.reset();
|
|
645
|
+
state->decayFrameStatistics.reset();
|
|
646
|
+
}
|
|
647
|
+
return state;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
std::shared_ptr<IOSUIWorkletState> animate(
|
|
651
|
+
int64_t handle,
|
|
652
|
+
std::vector<double> inputs,
|
|
653
|
+
double timestamp,
|
|
654
|
+
solid_native::worklets::UIWorkletTiming timing) {
|
|
655
|
+
auto state = requireActive(handle);
|
|
656
|
+
{
|
|
657
|
+
std::lock_guard lock(state->mutex);
|
|
658
|
+
if (!state->active) {
|
|
659
|
+
throw std::invalid_argument(
|
|
660
|
+
"The iOS UI worklet graph is no longer active.");
|
|
661
|
+
}
|
|
662
|
+
if (state->pan.has_value()) {
|
|
663
|
+
throw std::invalid_argument(
|
|
664
|
+
"The iOS UI worklet input vector is owned by its native pan gesture.");
|
|
665
|
+
}
|
|
666
|
+
if (!std::isfinite(timestamp) || timestamp < 0) {
|
|
667
|
+
throw std::invalid_argument(
|
|
668
|
+
"UI worklet timestamps must be finite and non-negative.");
|
|
669
|
+
}
|
|
670
|
+
if (state->hasTimestamp && timestamp < state->timestamp) {
|
|
671
|
+
throw std::invalid_argument(
|
|
672
|
+
"UI worklet input timestamps must be monotonic.");
|
|
673
|
+
}
|
|
674
|
+
// Validate the complete target before replacing an active timing.
|
|
675
|
+
state->graph.evaluate(inputs);
|
|
676
|
+
std::vector<solid_native::worklets::UIWorkletTimingKeyframe> keyframes;
|
|
677
|
+
keyframes.push_back({.inputs = std::move(inputs), .timing = timing});
|
|
678
|
+
state->timing = IOSUIWorkletTimingState{
|
|
679
|
+
.from = state->inputs,
|
|
680
|
+
.definition = {
|
|
681
|
+
.keyframes = std::move(keyframes),
|
|
682
|
+
.totalDurationMilliseconds = timing.durationMilliseconds,
|
|
683
|
+
},
|
|
684
|
+
};
|
|
685
|
+
state->timestamp = timestamp;
|
|
686
|
+
state->hasTimestamp = true;
|
|
687
|
+
state->mountAttempts = 0;
|
|
688
|
+
state->timingProgress = 0;
|
|
689
|
+
state->timingFrameStatistics.reset();
|
|
690
|
+
state->spring.reset();
|
|
691
|
+
state->springPosition.reset();
|
|
692
|
+
state->springVelocity.reset();
|
|
693
|
+
state->springFrameStatistics.reset();
|
|
694
|
+
state->decay.reset();
|
|
695
|
+
state->decayElapsedMilliseconds.reset();
|
|
696
|
+
state->decaySpeed.reset();
|
|
697
|
+
state->decayFrameStatistics.reset();
|
|
698
|
+
}
|
|
699
|
+
return state;
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
std::shared_ptr<IOSUIWorkletState> animateKeyframes(
|
|
703
|
+
int64_t handle,
|
|
704
|
+
solid_native::worklets::UIWorkletTimingSequence keyframes,
|
|
705
|
+
double timestamp) {
|
|
706
|
+
auto state = requireActive(handle);
|
|
707
|
+
{
|
|
708
|
+
std::lock_guard lock(state->mutex);
|
|
709
|
+
if (!state->active) {
|
|
710
|
+
throw std::invalid_argument(
|
|
711
|
+
"The iOS UI worklet graph is no longer active.");
|
|
712
|
+
}
|
|
713
|
+
if (state->pan.has_value()) {
|
|
714
|
+
throw std::invalid_argument(
|
|
715
|
+
"The iOS UI worklet input vector is owned by its native pan gesture.");
|
|
716
|
+
}
|
|
717
|
+
if (!std::isfinite(timestamp) || timestamp < 0) {
|
|
718
|
+
throw std::invalid_argument(
|
|
719
|
+
"UI worklet timestamps must be finite and non-negative.");
|
|
720
|
+
}
|
|
721
|
+
if (state->hasTimestamp && timestamp < state->timestamp) {
|
|
722
|
+
throw std::invalid_argument(
|
|
723
|
+
"UI worklet input timestamps must be monotonic.");
|
|
724
|
+
}
|
|
725
|
+
// Reject any invalid terminal before replacing the active driver.
|
|
726
|
+
for (const auto &keyframe : keyframes.keyframes) {
|
|
727
|
+
state->graph.evaluate(keyframe.inputs);
|
|
728
|
+
}
|
|
729
|
+
state->timing = IOSUIWorkletTimingState{
|
|
730
|
+
.from = state->inputs,
|
|
731
|
+
.definition = std::move(keyframes),
|
|
732
|
+
};
|
|
733
|
+
state->timestamp = timestamp;
|
|
734
|
+
state->hasTimestamp = true;
|
|
735
|
+
state->mountAttempts = 0;
|
|
736
|
+
state->timingProgress = 0;
|
|
737
|
+
state->timingFrameStatistics.reset();
|
|
738
|
+
state->spring.reset();
|
|
739
|
+
state->springPosition.reset();
|
|
740
|
+
state->springVelocity.reset();
|
|
741
|
+
state->springFrameStatistics.reset();
|
|
742
|
+
state->decay.reset();
|
|
743
|
+
state->decayElapsedMilliseconds.reset();
|
|
744
|
+
state->decaySpeed.reset();
|
|
745
|
+
state->decayFrameStatistics.reset();
|
|
746
|
+
}
|
|
747
|
+
return state;
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
std::shared_ptr<IOSUIWorkletState> spring(
|
|
751
|
+
int64_t handle,
|
|
752
|
+
std::vector<double> inputs,
|
|
753
|
+
double timestamp,
|
|
754
|
+
solid_native::worklets::UIWorkletSpring spring) {
|
|
755
|
+
auto state = requireActive(handle);
|
|
756
|
+
{
|
|
757
|
+
std::lock_guard lock(state->mutex);
|
|
758
|
+
if (!state->active) {
|
|
759
|
+
throw std::invalid_argument(
|
|
760
|
+
"The iOS UI worklet graph is no longer active.");
|
|
761
|
+
}
|
|
762
|
+
if (state->pan.has_value()) {
|
|
763
|
+
throw std::invalid_argument(
|
|
764
|
+
"The iOS UI worklet input vector is owned by its native pan gesture.");
|
|
765
|
+
}
|
|
766
|
+
if (!std::isfinite(timestamp) || timestamp < 0) {
|
|
767
|
+
throw std::invalid_argument(
|
|
768
|
+
"UI worklet timestamps must be finite and non-negative.");
|
|
769
|
+
}
|
|
770
|
+
if (state->hasTimestamp && timestamp < state->timestamp) {
|
|
771
|
+
throw std::invalid_argument(
|
|
772
|
+
"UI worklet input timestamps must be monotonic.");
|
|
773
|
+
}
|
|
774
|
+
state->graph.evaluate(inputs);
|
|
775
|
+
state->timing.reset();
|
|
776
|
+
state->timingProgress = 0;
|
|
777
|
+
state->timingFrameStatistics.reset();
|
|
778
|
+
state->spring = IOSUIWorkletSpringState{
|
|
779
|
+
.from = state->inputs,
|
|
780
|
+
.to = std::move(inputs),
|
|
781
|
+
.definition = spring,
|
|
782
|
+
};
|
|
783
|
+
state->springPosition = 0;
|
|
784
|
+
state->springVelocity = spring.initialVelocity;
|
|
785
|
+
state->springFrameStatistics.reset();
|
|
786
|
+
state->decay.reset();
|
|
787
|
+
state->decayElapsedMilliseconds.reset();
|
|
788
|
+
state->decaySpeed.reset();
|
|
789
|
+
state->decayFrameStatistics.reset();
|
|
790
|
+
state->timestamp = timestamp;
|
|
791
|
+
state->hasTimestamp = true;
|
|
792
|
+
state->mountAttempts = 0;
|
|
793
|
+
}
|
|
794
|
+
return state;
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
std::shared_ptr<IOSUIWorkletState> decay(
|
|
798
|
+
int64_t handle,
|
|
799
|
+
std::vector<double> velocities,
|
|
800
|
+
double timestamp,
|
|
801
|
+
solid_native::worklets::UIWorkletDecay decay) {
|
|
802
|
+
auto state = requireActive(handle);
|
|
803
|
+
{
|
|
804
|
+
std::lock_guard lock(state->mutex);
|
|
805
|
+
if (!state->active) {
|
|
806
|
+
throw std::invalid_argument(
|
|
807
|
+
"The iOS UI worklet graph is no longer active.");
|
|
808
|
+
}
|
|
809
|
+
if (state->pan.has_value()) {
|
|
810
|
+
throw std::invalid_argument(
|
|
811
|
+
"The iOS UI worklet input vector is owned by its native pan gesture.");
|
|
812
|
+
}
|
|
813
|
+
if (!std::isfinite(timestamp) || timestamp < 0) {
|
|
814
|
+
throw std::invalid_argument(
|
|
815
|
+
"UI worklet timestamps must be finite and non-negative.");
|
|
816
|
+
}
|
|
817
|
+
if (state->hasTimestamp && timestamp < state->timestamp) {
|
|
818
|
+
throw std::invalid_argument(
|
|
819
|
+
"UI worklet input timestamps must be monotonic.");
|
|
820
|
+
}
|
|
821
|
+
if (velocities.size() != state->inputs.size()) {
|
|
822
|
+
throw std::invalid_argument(
|
|
823
|
+
"UI worklet decay velocities must match the input vector.");
|
|
824
|
+
}
|
|
825
|
+
double initialSpeed = 0;
|
|
826
|
+
for (const auto velocity : velocities) {
|
|
827
|
+
if (!std::isfinite(velocity) ||
|
|
828
|
+
std::abs(velocity) >
|
|
829
|
+
solid_native::worklets::MaximumUIWorkletDecayVelocity) {
|
|
830
|
+
throw std::invalid_argument(
|
|
831
|
+
"UI worklet decay velocity is outside its bounded range.");
|
|
832
|
+
}
|
|
833
|
+
initialSpeed = std::max(initialSpeed, std::abs(velocity));
|
|
834
|
+
}
|
|
835
|
+
const auto terminal = decay.evaluate(
|
|
836
|
+
initialSpeed, decay.maximumDurationMilliseconds);
|
|
837
|
+
auto terminalInputs = state->inputs;
|
|
838
|
+
for (size_t index = 0; index < terminalInputs.size(); ++index) {
|
|
839
|
+
terminalInputs.at(index) +=
|
|
840
|
+
velocities.at(index) * terminal.displacementFactorSeconds;
|
|
841
|
+
}
|
|
842
|
+
// Reject a terminal graph failure before replacing another driver.
|
|
843
|
+
state->graph.evaluate(terminalInputs);
|
|
844
|
+
state->timing.reset();
|
|
845
|
+
state->timingProgress = 0;
|
|
846
|
+
state->timingFrameStatistics.reset();
|
|
847
|
+
state->spring.reset();
|
|
848
|
+
state->springPosition.reset();
|
|
849
|
+
state->springVelocity.reset();
|
|
850
|
+
state->springFrameStatistics.reset();
|
|
851
|
+
state->decay = IOSUIWorkletDecayState{
|
|
852
|
+
.from = state->inputs,
|
|
853
|
+
.velocities = std::move(velocities),
|
|
854
|
+
.initialSpeed = initialSpeed,
|
|
855
|
+
.definition = decay,
|
|
856
|
+
};
|
|
857
|
+
state->decayElapsedMilliseconds = 0;
|
|
858
|
+
state->decaySpeed = initialSpeed;
|
|
859
|
+
state->decayFrameStatistics.reset();
|
|
860
|
+
state->timestamp = timestamp;
|
|
861
|
+
state->hasTimestamp = true;
|
|
862
|
+
state->mountAttempts = 0;
|
|
863
|
+
}
|
|
864
|
+
return state;
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
std::vector<double> cancelAnimation(int64_t handle, double timestamp) {
|
|
868
|
+
auto state = requireActive(handle);
|
|
869
|
+
SolidNativeDisplayLinkObserver *frameObserver = nil;
|
|
870
|
+
std::vector<double> inputs;
|
|
871
|
+
{
|
|
872
|
+
std::lock_guard lock(state->mutex);
|
|
873
|
+
if (!state->active) {
|
|
874
|
+
throw std::invalid_argument(
|
|
875
|
+
"The iOS UI worklet graph is no longer active.");
|
|
876
|
+
}
|
|
877
|
+
if (!std::isfinite(timestamp) || timestamp < 0) {
|
|
878
|
+
throw std::invalid_argument(
|
|
879
|
+
"UI worklet timestamps must be finite and non-negative.");
|
|
880
|
+
}
|
|
881
|
+
if (state->hasTimestamp && timestamp < state->timestamp) {
|
|
882
|
+
throw std::invalid_argument(
|
|
883
|
+
"UI worklet input timestamps must be monotonic.");
|
|
884
|
+
}
|
|
885
|
+
const auto hadDriver = state->timing.has_value() ||
|
|
886
|
+
state->spring.has_value() || state->decay.has_value();
|
|
887
|
+
state->timing.reset();
|
|
888
|
+
state->spring.reset();
|
|
889
|
+
if (state->springVelocity.has_value()) state->springVelocity = 0;
|
|
890
|
+
state->decay.reset();
|
|
891
|
+
if (state->decaySpeed.has_value()) state->decaySpeed = 0;
|
|
892
|
+
state->timestamp = timestamp;
|
|
893
|
+
state->hasTimestamp = true;
|
|
894
|
+
if (hadDriver) {
|
|
895
|
+
state->framePending = false;
|
|
896
|
+
state->frameGeneration++;
|
|
897
|
+
frameObserver = state->frameObserver;
|
|
898
|
+
state->frameObserver = nil;
|
|
899
|
+
}
|
|
900
|
+
inputs = state->inputs;
|
|
901
|
+
}
|
|
902
|
+
invalidateIOSUIWorkletFrameObserver(frameObserver);
|
|
903
|
+
return inputs;
|
|
904
|
+
}
|
|
905
|
+
|
|
906
|
+
std::shared_ptr<IOSUIWorkletState> attachPan(
|
|
907
|
+
int64_t handle,
|
|
908
|
+
solid_native::worklets::UIWorkletPanGesture pan) {
|
|
909
|
+
auto state = requireActive(handle);
|
|
910
|
+
{
|
|
911
|
+
std::lock_guard lock(state->mutex);
|
|
912
|
+
if (!state->active) {
|
|
913
|
+
throw std::invalid_argument(
|
|
914
|
+
"The iOS UI worklet graph is no longer active.");
|
|
915
|
+
}
|
|
916
|
+
if (state->pan.has_value()) {
|
|
917
|
+
throw std::invalid_argument(
|
|
918
|
+
"The iOS UI worklet graph already owns a native pan gesture.");
|
|
919
|
+
}
|
|
920
|
+
state->pan = IOSUIWorkletPanState{.definition = std::move(pan)};
|
|
921
|
+
}
|
|
922
|
+
return state;
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
void detachPan(int64_t handle) {
|
|
926
|
+
auto state = requireActive(handle);
|
|
927
|
+
SolidNativePanGestureObserver *observer = nil;
|
|
928
|
+
{
|
|
929
|
+
std::lock_guard lock(state->mutex);
|
|
930
|
+
if (state->pan.has_value()) state->pan->active = false;
|
|
931
|
+
state->pan.reset();
|
|
932
|
+
observer = state->panObserver;
|
|
933
|
+
state->panObserver = nil;
|
|
934
|
+
}
|
|
935
|
+
invalidateIOSUIWorkletPanObserver(observer);
|
|
936
|
+
}
|
|
937
|
+
|
|
938
|
+
std::vector<double> releasePan(int64_t handle, double timestamp) {
|
|
939
|
+
auto state = requireActive(handle);
|
|
940
|
+
SolidNativeDisplayLinkObserver *frameObserver = nil;
|
|
941
|
+
SolidNativePanGestureObserver *panObserver = nil;
|
|
942
|
+
std::vector<double> inputs;
|
|
943
|
+
{
|
|
944
|
+
std::lock_guard lock(state->mutex);
|
|
945
|
+
if (!state->active) {
|
|
946
|
+
throw std::invalid_argument(
|
|
947
|
+
"The iOS UI worklet graph is no longer active.");
|
|
948
|
+
}
|
|
949
|
+
if (!std::isfinite(timestamp) || timestamp < 0) {
|
|
950
|
+
throw std::invalid_argument(
|
|
951
|
+
"UI worklet timestamps must be finite and non-negative.");
|
|
952
|
+
}
|
|
953
|
+
if (state->hasTimestamp && timestamp < state->timestamp) {
|
|
954
|
+
throw std::invalid_argument(
|
|
955
|
+
"UI worklet input timestamps must be monotonic.");
|
|
956
|
+
}
|
|
957
|
+
if (!state->pan.has_value()) {
|
|
958
|
+
throw std::invalid_argument(
|
|
959
|
+
"The iOS UI worklet graph does not own a native pan gesture.");
|
|
960
|
+
}
|
|
961
|
+
const auto hadDriver = state->timing.has_value() ||
|
|
962
|
+
state->spring.has_value() || state->decay.has_value();
|
|
963
|
+
state->pan->active = false;
|
|
964
|
+
state->pan.reset();
|
|
965
|
+
panObserver = state->panObserver;
|
|
966
|
+
state->panObserver = nil;
|
|
967
|
+
state->timing.reset();
|
|
968
|
+
state->spring.reset();
|
|
969
|
+
if (state->springVelocity.has_value()) state->springVelocity = 0;
|
|
970
|
+
state->decay.reset();
|
|
971
|
+
if (state->decaySpeed.has_value()) state->decaySpeed = 0;
|
|
972
|
+
state->timestamp = timestamp;
|
|
973
|
+
state->hasTimestamp = true;
|
|
974
|
+
if (hadDriver) {
|
|
975
|
+
state->framePending = false;
|
|
976
|
+
state->frameGeneration++;
|
|
977
|
+
frameObserver = state->frameObserver;
|
|
978
|
+
state->frameObserver = nil;
|
|
979
|
+
}
|
|
980
|
+
inputs = state->inputs;
|
|
981
|
+
}
|
|
982
|
+
invalidateIOSUIWorkletFrameObserver(frameObserver);
|
|
983
|
+
invalidateIOSUIWorkletPanObserver(panObserver);
|
|
984
|
+
return inputs;
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
void destroy(int64_t handle) {
|
|
988
|
+
std::shared_ptr<IOSUIWorkletState> state;
|
|
989
|
+
{
|
|
990
|
+
std::lock_guard lock(mutex_);
|
|
991
|
+
if (issued_.erase(handle) == 0) {
|
|
992
|
+
throw std::invalid_argument(
|
|
993
|
+
"Unknown iOS UI worklet graph handle " +
|
|
994
|
+
std::to_string(handle) + ".");
|
|
995
|
+
}
|
|
996
|
+
const auto entry = active_.find(handle);
|
|
997
|
+
if (entry != active_.end()) {
|
|
998
|
+
state = std::move(entry->second);
|
|
999
|
+
active_.erase(entry);
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
if (state) deactivateIOSUIWorklet(state);
|
|
1003
|
+
}
|
|
1004
|
+
|
|
1005
|
+
void cancelNode(int64_t targetNode) {
|
|
1006
|
+
std::vector<std::shared_ptr<IOSUIWorkletState>> cancelled;
|
|
1007
|
+
{
|
|
1008
|
+
std::lock_guard lock(mutex_);
|
|
1009
|
+
for (auto iterator = active_.begin(); iterator != active_.end();) {
|
|
1010
|
+
if (iterator->second->targetNode != targetNode) {
|
|
1011
|
+
++iterator;
|
|
1012
|
+
continue;
|
|
1013
|
+
}
|
|
1014
|
+
cancelled.push_back(iterator->second);
|
|
1015
|
+
iterator = active_.erase(iterator);
|
|
1016
|
+
}
|
|
1017
|
+
}
|
|
1018
|
+
for (const auto &state : cancelled) {
|
|
1019
|
+
deactivateIOSUIWorklet(state);
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
|
|
1023
|
+
void cancelAll() {
|
|
1024
|
+
std::vector<std::shared_ptr<IOSUIWorkletState>> cancelled;
|
|
1025
|
+
{
|
|
1026
|
+
std::lock_guard lock(mutex_);
|
|
1027
|
+
for (const auto &[_, state] : active_) cancelled.push_back(state);
|
|
1028
|
+
active_.clear();
|
|
1029
|
+
issued_.clear();
|
|
1030
|
+
}
|
|
1031
|
+
for (const auto &state : cancelled) {
|
|
1032
|
+
deactivateIOSUIWorklet(state);
|
|
1033
|
+
}
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
size_t activeCount() const {
|
|
1037
|
+
std::lock_guard lock(mutex_);
|
|
1038
|
+
return active_.size();
|
|
1039
|
+
}
|
|
1040
|
+
|
|
1041
|
+
std::pair<size_t, size_t> activityCounts() const {
|
|
1042
|
+
std::lock_guard lock(mutex_);
|
|
1043
|
+
size_t pendingFrameCount = 0;
|
|
1044
|
+
for (const auto &[_, state] : active_) {
|
|
1045
|
+
std::lock_guard lock(state->mutex);
|
|
1046
|
+
if (state->active && state->framePending) pendingFrameCount++;
|
|
1047
|
+
}
|
|
1048
|
+
return {active_.size(), pendingFrameCount};
|
|
1049
|
+
}
|
|
1050
|
+
|
|
1051
|
+
std::shared_ptr<IOSUIWorkletState> requireActive(int64_t handle) const {
|
|
1052
|
+
std::lock_guard lock(mutex_);
|
|
1053
|
+
const auto entry = active_.find(handle);
|
|
1054
|
+
if (entry == active_.end()) {
|
|
1055
|
+
throw std::invalid_argument(
|
|
1056
|
+
"Unknown or cancelled iOS UI worklet graph handle " +
|
|
1057
|
+
std::to_string(handle) + ".");
|
|
1058
|
+
}
|
|
1059
|
+
return entry->second;
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
private:
|
|
1063
|
+
mutable std::mutex mutex_;
|
|
1064
|
+
std::unordered_map<int64_t, std::shared_ptr<IOSUIWorkletState>> active_;
|
|
1065
|
+
std::unordered_set<int64_t> issued_;
|
|
1066
|
+
};
|
|
1067
|
+
|
|
1068
|
+
class EventRoutingState final {
|
|
1069
|
+
public:
|
|
1070
|
+
struct LayoutRouteStageNode final {
|
|
1071
|
+
int64_t node;
|
|
1072
|
+
facebook::react::Tag tag;
|
|
1073
|
+
std::optional<int64_t> previousNodeForTag;
|
|
1074
|
+
std::optional<facebook::react::Tag> previousTagForNode;
|
|
1075
|
+
std::optional<std::unordered_set<std::string>> previousEvents;
|
|
1076
|
+
};
|
|
1077
|
+
|
|
1078
|
+
struct LayoutRouteStage final {
|
|
1079
|
+
bool active{false};
|
|
1080
|
+
int64_t previousSurface{0};
|
|
1081
|
+
int64_t previousSequence{0};
|
|
1082
|
+
std::vector<LayoutRouteStageNode> nodes;
|
|
1083
|
+
};
|
|
1084
|
+
|
|
1085
|
+
std::atomic<bool> active{true};
|
|
1086
|
+
std::atomic<bool> handlerInstalled{false};
|
|
1087
|
+
std::atomic<int64_t> surface{0};
|
|
1088
|
+
std::atomic<int64_t> sequence{0};
|
|
1089
|
+
|
|
1090
|
+
std::optional<std::vector<RoutedEvent>> route(
|
|
1091
|
+
facebook::react::Tag tag,
|
|
1092
|
+
const std::string &nativeName) const {
|
|
1093
|
+
std::lock_guard lock(mutex_);
|
|
1094
|
+
const auto nodeEntry = nodesByTag_.find(tag);
|
|
1095
|
+
if (nodeEntry == nodesByTag_.end()) return std::nullopt;
|
|
1096
|
+
|
|
1097
|
+
std::vector<RoutedEvent> events;
|
|
1098
|
+
for (const auto &name : semanticEventNames(nativeName)) {
|
|
1099
|
+
auto node = nodeEntry->second;
|
|
1100
|
+
while (true) {
|
|
1101
|
+
const auto eventsEntry = eventsByNode_.find(node);
|
|
1102
|
+
if (eventsEntry != eventsByNode_.end() &&
|
|
1103
|
+
eventsEntry->second.contains(name)) {
|
|
1104
|
+
events.push_back(RoutedEvent{.node = node, .name = name});
|
|
1105
|
+
break;
|
|
1106
|
+
}
|
|
1107
|
+
if (!eventBubbles(name)) break;
|
|
1108
|
+
const auto parentEntry = parentsByNode_.find(node);
|
|
1109
|
+
if (parentEntry == parentsByNode_.end()) break;
|
|
1110
|
+
node = parentEntry->second;
|
|
1111
|
+
}
|
|
1112
|
+
}
|
|
1113
|
+
return events;
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
bool isSubscribed(int64_t node, const std::string &name) const {
|
|
1117
|
+
std::lock_guard lock(mutex_);
|
|
1118
|
+
const auto eventsEntry = eventsByNode_.find(node);
|
|
1119
|
+
return eventsEntry != eventsByNode_.end() &&
|
|
1120
|
+
eventsEntry->second.contains(name);
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
std::optional<facebook::react::Tag> tagForNode(int64_t node) const {
|
|
1124
|
+
std::lock_guard lock(mutex_);
|
|
1125
|
+
const auto entry = tagsByNode_.find(node);
|
|
1126
|
+
if (entry == tagsByNode_.end()) return std::nullopt;
|
|
1127
|
+
return entry->second;
|
|
1128
|
+
}
|
|
1129
|
+
|
|
1130
|
+
void reclaimNode(int64_t node) {
|
|
1131
|
+
std::lock_guard lock(mutex_);
|
|
1132
|
+
eventsByNode_.erase(node);
|
|
1133
|
+
parentsByNode_.erase(node);
|
|
1134
|
+
const auto tagEntry = tagsByNode_.find(node);
|
|
1135
|
+
if (tagEntry == tagsByNode_.end()) return;
|
|
1136
|
+
nodesByTag_.erase(tagEntry->second);
|
|
1137
|
+
tagsByNode_.erase(tagEntry);
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
LayoutRouteStage stageLayoutRoutes(
|
|
1141
|
+
NSDictionary<NSString *, id> *transaction,
|
|
1142
|
+
const NodeIdentityMap &identities) {
|
|
1143
|
+
LayoutRouteStage stage;
|
|
1144
|
+
std::unordered_map<int64_t, std::unordered_set<std::string>> layoutEvents;
|
|
1145
|
+
id mutationsValue = transaction[@"mutations"];
|
|
1146
|
+
if ([mutationsValue isKindOfClass:NSArray.class]) {
|
|
1147
|
+
for (id value in (NSArray *)mutationsValue) {
|
|
1148
|
+
if (![value isKindOfClass:NSDictionary.class]) continue;
|
|
1149
|
+
auto mutation = (NSDictionary<NSString *, id> *)value;
|
|
1150
|
+
if (![mutation[@"type"] isEqual:@"update-event-listeners"] ||
|
|
1151
|
+
![mutation[@"node"] isKindOfClass:NSNumber.class] ||
|
|
1152
|
+
![mutation[@"events"] isKindOfClass:NSArray.class]) {
|
|
1153
|
+
continue;
|
|
1154
|
+
}
|
|
1155
|
+
std::unordered_set<std::string> events;
|
|
1156
|
+
for (id eventValue in (NSArray *)mutation[@"events"]) {
|
|
1157
|
+
if ([eventValue isKindOfClass:NSString.class]) {
|
|
1158
|
+
events.insert(utf8((NSString *)eventValue));
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
const auto node = [(NSNumber *)mutation[@"node"] longLongValue];
|
|
1162
|
+
if (events.contains("layout")) {
|
|
1163
|
+
layoutEvents[node] = std::move(events);
|
|
1164
|
+
} else {
|
|
1165
|
+
layoutEvents.erase(node);
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
}
|
|
1169
|
+
if (layoutEvents.empty()) return stage;
|
|
1170
|
+
|
|
1171
|
+
std::lock_guard lock(mutex_);
|
|
1172
|
+
stage.active = true;
|
|
1173
|
+
stage.previousSurface = surface.load(std::memory_order_acquire);
|
|
1174
|
+
stage.previousSequence = sequence.load(std::memory_order_acquire);
|
|
1175
|
+
for (auto &[node, events] : layoutEvents) {
|
|
1176
|
+
std::optional<facebook::react::Tag> tag;
|
|
1177
|
+
const auto identity = identities.find(node);
|
|
1178
|
+
if (identity != identities.end()) {
|
|
1179
|
+
tag = identity->second.tag;
|
|
1180
|
+
} else {
|
|
1181
|
+
const auto existingTag = tagsByNode_.find(node);
|
|
1182
|
+
if (existingTag != tagsByNode_.end()) tag = existingTag->second;
|
|
1183
|
+
}
|
|
1184
|
+
if (!tag.has_value()) continue;
|
|
1185
|
+
|
|
1186
|
+
LayoutRouteStageNode stagedNode{
|
|
1187
|
+
.node = node,
|
|
1188
|
+
.tag = *tag,
|
|
1189
|
+
.previousNodeForTag = std::nullopt,
|
|
1190
|
+
.previousTagForNode = std::nullopt,
|
|
1191
|
+
.previousEvents = std::nullopt};
|
|
1192
|
+
const auto previousNode = nodesByTag_.find(*tag);
|
|
1193
|
+
if (previousNode != nodesByTag_.end()) {
|
|
1194
|
+
stagedNode.previousNodeForTag = previousNode->second;
|
|
1195
|
+
}
|
|
1196
|
+
const auto previousTag = tagsByNode_.find(node);
|
|
1197
|
+
if (previousTag != tagsByNode_.end()) {
|
|
1198
|
+
stagedNode.previousTagForNode = previousTag->second;
|
|
1199
|
+
}
|
|
1200
|
+
const auto previousEvents = eventsByNode_.find(node);
|
|
1201
|
+
if (previousEvents != eventsByNode_.end()) {
|
|
1202
|
+
stagedNode.previousEvents = previousEvents->second;
|
|
1203
|
+
}
|
|
1204
|
+
nodesByTag_[*tag] = node;
|
|
1205
|
+
tagsByNode_[node] = *tag;
|
|
1206
|
+
eventsByNode_[node] = std::move(events);
|
|
1207
|
+
stage.nodes.push_back(std::move(stagedNode));
|
|
1208
|
+
}
|
|
1209
|
+
if (stage.nodes.empty()) {
|
|
1210
|
+
stage.active = false;
|
|
1211
|
+
return stage;
|
|
1212
|
+
}
|
|
1213
|
+
id surfaceValue = transaction[@"surface"];
|
|
1214
|
+
id sequenceValue = transaction[@"sequence"];
|
|
1215
|
+
if ([surfaceValue isKindOfClass:NSNumber.class]) {
|
|
1216
|
+
surface.store(
|
|
1217
|
+
[(NSNumber *)surfaceValue longLongValue], std::memory_order_release);
|
|
1218
|
+
}
|
|
1219
|
+
if ([sequenceValue isKindOfClass:NSNumber.class]) {
|
|
1220
|
+
sequence.store(
|
|
1221
|
+
[(NSNumber *)sequenceValue longLongValue],
|
|
1222
|
+
std::memory_order_release);
|
|
1223
|
+
}
|
|
1224
|
+
return stage;
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
void finishLayoutRoutes(LayoutRouteStage &stage) {
|
|
1228
|
+
stage.active = false;
|
|
1229
|
+
}
|
|
1230
|
+
|
|
1231
|
+
void rollbackLayoutRoutes(LayoutRouteStage &stage) {
|
|
1232
|
+
if (!stage.active) return;
|
|
1233
|
+
std::lock_guard lock(mutex_);
|
|
1234
|
+
for (auto iterator = stage.nodes.rbegin();
|
|
1235
|
+
iterator != stage.nodes.rend();
|
|
1236
|
+
++iterator) {
|
|
1237
|
+
const auto &node = *iterator;
|
|
1238
|
+
if (node.previousNodeForTag.has_value()) {
|
|
1239
|
+
nodesByTag_[node.tag] = *node.previousNodeForTag;
|
|
1240
|
+
} else {
|
|
1241
|
+
nodesByTag_.erase(node.tag);
|
|
1242
|
+
}
|
|
1243
|
+
if (node.previousTagForNode.has_value()) {
|
|
1244
|
+
tagsByNode_[node.node] = *node.previousTagForNode;
|
|
1245
|
+
} else {
|
|
1246
|
+
tagsByNode_.erase(node.node);
|
|
1247
|
+
}
|
|
1248
|
+
if (node.previousEvents.has_value()) {
|
|
1249
|
+
eventsByNode_[node.node] = *node.previousEvents;
|
|
1250
|
+
} else {
|
|
1251
|
+
eventsByNode_.erase(node.node);
|
|
1252
|
+
}
|
|
1253
|
+
}
|
|
1254
|
+
surface.store(stage.previousSurface, std::memory_order_release);
|
|
1255
|
+
sequence.store(stage.previousSequence, std::memory_order_release);
|
|
1256
|
+
stage.active = false;
|
|
1257
|
+
}
|
|
1258
|
+
|
|
1259
|
+
void publishCommit(
|
|
1260
|
+
NSDictionary<NSString *, id> *transaction,
|
|
1261
|
+
const NodeIdentityMap &identities,
|
|
1262
|
+
int64_t committedSequence) {
|
|
1263
|
+
std::lock_guard lock(mutex_);
|
|
1264
|
+
for (const auto &[node, identity] : identities) {
|
|
1265
|
+
nodesByTag_[identity.tag] = node;
|
|
1266
|
+
tagsByNode_[node] = identity.tag;
|
|
1267
|
+
}
|
|
1268
|
+
|
|
1269
|
+
id mutationsValue = transaction[@"mutations"];
|
|
1270
|
+
if ([mutationsValue isKindOfClass:NSArray.class]) {
|
|
1271
|
+
for (id value in (NSArray *)mutationsValue) {
|
|
1272
|
+
if (![value isKindOfClass:NSDictionary.class]) continue;
|
|
1273
|
+
auto mutation = (NSDictionary<NSString *, id> *)value;
|
|
1274
|
+
id typeValue = mutation[@"type"];
|
|
1275
|
+
if (![typeValue isKindOfClass:NSString.class]) continue;
|
|
1276
|
+
if ([(NSString *)typeValue isEqualToString:@"insert-child"] ||
|
|
1277
|
+
[(NSString *)typeValue isEqualToString:@"remove-child"]) {
|
|
1278
|
+
id parentValue = mutation[@"parent"];
|
|
1279
|
+
id childValue = mutation[@"child"];
|
|
1280
|
+
if (![parentValue isKindOfClass:NSNumber.class] ||
|
|
1281
|
+
![childValue isKindOfClass:NSNumber.class]) {
|
|
1282
|
+
continue;
|
|
1283
|
+
}
|
|
1284
|
+
const auto parent = [(NSNumber *)parentValue longLongValue];
|
|
1285
|
+
const auto child = [(NSNumber *)childValue longLongValue];
|
|
1286
|
+
if ([(NSString *)typeValue isEqualToString:@"insert-child"]) {
|
|
1287
|
+
parentsByNode_[child] = parent;
|
|
1288
|
+
} else {
|
|
1289
|
+
const auto parentEntry = parentsByNode_.find(child);
|
|
1290
|
+
if (parentEntry != parentsByNode_.end() &&
|
|
1291
|
+
parentEntry->second == parent) {
|
|
1292
|
+
parentsByNode_.erase(parentEntry);
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
continue;
|
|
1296
|
+
}
|
|
1297
|
+
id nodeValue = mutation[@"node"];
|
|
1298
|
+
if (![nodeValue isKindOfClass:NSNumber.class]) continue;
|
|
1299
|
+
const auto node = [(NSNumber *)nodeValue longLongValue];
|
|
1300
|
+
if ([(NSString *)typeValue isEqualToString:@"delete-node"]) {
|
|
1301
|
+
eventsByNode_.erase(node);
|
|
1302
|
+
parentsByNode_.erase(node);
|
|
1303
|
+
continue;
|
|
1304
|
+
}
|
|
1305
|
+
if (![(NSString *)typeValue
|
|
1306
|
+
isEqualToString:@"update-event-listeners"]) {
|
|
1307
|
+
continue;
|
|
1308
|
+
}
|
|
1309
|
+
std::unordered_set<std::string> events;
|
|
1310
|
+
id eventsValue = mutation[@"events"];
|
|
1311
|
+
if ([eventsValue isKindOfClass:NSArray.class]) {
|
|
1312
|
+
for (id eventValue in (NSArray *)eventsValue) {
|
|
1313
|
+
if ([eventValue isKindOfClass:NSString.class]) {
|
|
1314
|
+
events.insert(utf8((NSString *)eventValue));
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
}
|
|
1318
|
+
eventsByNode_[node] = std::move(events);
|
|
1319
|
+
}
|
|
1320
|
+
}
|
|
1321
|
+
sequence.store(committedSequence, std::memory_order_release);
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
private:
|
|
1325
|
+
mutable std::mutex mutex_;
|
|
1326
|
+
std::unordered_map<facebook::react::Tag, int64_t> nodesByTag_;
|
|
1327
|
+
std::unordered_map<int64_t, facebook::react::Tag> tagsByNode_;
|
|
1328
|
+
std::unordered_map<int64_t, int64_t> parentsByNode_;
|
|
1329
|
+
std::unordered_map<int64_t, std::unordered_set<std::string>> eventsByNode_;
|
|
1330
|
+
};
|
|
1331
|
+
|
|
1332
|
+
struct RetiredInstances final {
|
|
1333
|
+
int64_t fabricRevision;
|
|
1334
|
+
std::vector<int64_t> handles;
|
|
1335
|
+
};
|
|
1336
|
+
|
|
1337
|
+
struct CommitMountObservation final {
|
|
1338
|
+
int64_t surface;
|
|
1339
|
+
int64_t sequence;
|
|
1340
|
+
int64_t fabricRevision;
|
|
1341
|
+
double commitStartedAt;
|
|
1342
|
+
double commitStartedMonotonic;
|
|
1343
|
+
bool notifyLifecycle;
|
|
1344
|
+
std::optional<std::string> causalOperationId;
|
|
1345
|
+
};
|
|
1346
|
+
|
|
1347
|
+
class RuntimeState final : public facebook::jsi::NativeState {
|
|
1348
|
+
public:
|
|
1349
|
+
RuntimeState(
|
|
1350
|
+
Runtime &runtime,
|
|
1351
|
+
const Object &storage,
|
|
1352
|
+
std::shared_ptr<EventRoutingState> routing)
|
|
1353
|
+
: storage_(runtime, storage), routing_(std::move(routing)) {}
|
|
1354
|
+
|
|
1355
|
+
~RuntimeState() override {
|
|
1356
|
+
uiWorklets_.cancelAll();
|
|
1357
|
+
routing_->active.store(false, std::memory_order_release);
|
|
1358
|
+
routing_->handlerInstalled.store(false, std::memory_order_release);
|
|
1359
|
+
commitLifecycleHandlerInstalled_.store(false, std::memory_order_release);
|
|
1360
|
+
if (runtimeRetirementHandler_) runtimeRetirementHandler_();
|
|
1361
|
+
}
|
|
1362
|
+
|
|
1363
|
+
void setRuntimeRetirementHandler(std::function<void()> handler) {
|
|
1364
|
+
runtimeRetirementHandler_ = std::move(handler);
|
|
1365
|
+
}
|
|
1366
|
+
|
|
1367
|
+
Object storage(Runtime &runtime) const {
|
|
1368
|
+
auto value = storage_.lock(runtime);
|
|
1369
|
+
if (!value.isObject()) {
|
|
1370
|
+
throw std::runtime_error(
|
|
1371
|
+
"The Solid Native runtime storage was collected unexpectedly.");
|
|
1372
|
+
}
|
|
1373
|
+
return value.getObject(runtime);
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
Object instances(Runtime &runtime) const {
|
|
1377
|
+
return storage(runtime).getPropertyAsObject(runtime, "instances");
|
|
1378
|
+
}
|
|
1379
|
+
|
|
1380
|
+
Object resources(Runtime &runtime) const {
|
|
1381
|
+
return storage(runtime).getPropertyAsObject(runtime, "resources");
|
|
1382
|
+
}
|
|
1383
|
+
|
|
1384
|
+
int64_t retainResource(
|
|
1385
|
+
Runtime &runtime,
|
|
1386
|
+
const std::string &kind,
|
|
1387
|
+
const Value &value) {
|
|
1388
|
+
if (resourceKinds_.size() >= MaximumRetainedResources) {
|
|
1389
|
+
throw std::runtime_error(
|
|
1390
|
+
"The Solid Native runtime resource registry is full.");
|
|
1391
|
+
}
|
|
1392
|
+
if (nextResourceHandle_ > 9007199254740991LL) {
|
|
1393
|
+
throw std::runtime_error(
|
|
1394
|
+
"The Solid Native runtime exhausted native resource handles.");
|
|
1395
|
+
}
|
|
1396
|
+
const auto handle = nextResourceHandle_++;
|
|
1397
|
+
const auto key = std::to_string(handle);
|
|
1398
|
+
resources(runtime).setProperty(
|
|
1399
|
+
runtime, key.c_str(), Value(runtime, value));
|
|
1400
|
+
resourceKinds_.emplace(handle, kind);
|
|
1401
|
+
return handle;
|
|
1402
|
+
}
|
|
1403
|
+
|
|
1404
|
+
Value resolveResource(
|
|
1405
|
+
Runtime &runtime,
|
|
1406
|
+
int64_t handle,
|
|
1407
|
+
const std::string &kind) const {
|
|
1408
|
+
const auto resource = resourceKinds_.find(handle);
|
|
1409
|
+
if (resource == resourceKinds_.end()) {
|
|
1410
|
+
throw std::invalid_argument(
|
|
1411
|
+
"Native resource " + std::to_string(handle) +
|
|
1412
|
+
" is not retained.");
|
|
1413
|
+
}
|
|
1414
|
+
if (resource->second != kind) {
|
|
1415
|
+
throw std::invalid_argument(
|
|
1416
|
+
"Native resource " + std::to_string(handle) +
|
|
1417
|
+
" has kind " + resource->second + ", not " + kind + ".");
|
|
1418
|
+
}
|
|
1419
|
+
return resources(runtime).getProperty(runtime, keyFor(handle).c_str());
|
|
1420
|
+
}
|
|
1421
|
+
|
|
1422
|
+
void releaseResource(Runtime &runtime, int64_t handle) {
|
|
1423
|
+
if (resourceKinds_.erase(handle) == 0) {
|
|
1424
|
+
throw std::invalid_argument(
|
|
1425
|
+
"Native resource " + std::to_string(handle) +
|
|
1426
|
+
" is not retained.");
|
|
1427
|
+
}
|
|
1428
|
+
resources(runtime).deleteProperty(runtime, keyFor(handle).c_str());
|
|
1429
|
+
}
|
|
1430
|
+
|
|
1431
|
+
void reclaimAllResources(Runtime &runtime) {
|
|
1432
|
+
const auto handles = resourceKinds_;
|
|
1433
|
+
for (const auto &[handle, _] : handles) {
|
|
1434
|
+
resources(runtime).deleteProperty(runtime, keyFor(handle).c_str());
|
|
1435
|
+
}
|
|
1436
|
+
resourceKinds_.clear();
|
|
1437
|
+
}
|
|
1438
|
+
|
|
1439
|
+
size_t retainedResourceCount() const {
|
|
1440
|
+
return resourceKinds_.size();
|
|
1441
|
+
}
|
|
1442
|
+
|
|
1443
|
+
bool hasInstance(Runtime &runtime, int64_t handle) const {
|
|
1444
|
+
const auto key = std::to_string(handle);
|
|
1445
|
+
return instances(runtime).hasProperty(runtime, key.c_str());
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
void retainInstance(Runtime &runtime, int64_t handle, Value &&value) {
|
|
1449
|
+
const auto key = std::to_string(handle);
|
|
1450
|
+
instances(runtime).setProperty(runtime, key.c_str(), std::move(value));
|
|
1451
|
+
retainedHandles_.insert(handle);
|
|
1452
|
+
retainedNodeCount_++;
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
void releaseInstance(Runtime &runtime, int64_t handle) {
|
|
1456
|
+
if (retainedHandles_.erase(handle) == 0) return;
|
|
1457
|
+
uiWorklets_.cancelNode(handle);
|
|
1458
|
+
const auto key = std::to_string(handle);
|
|
1459
|
+
instances(runtime).deleteProperty(runtime, key.c_str());
|
|
1460
|
+
retainedNodeCount_--;
|
|
1461
|
+
routing_->reclaimNode(handle);
|
|
1462
|
+
}
|
|
1463
|
+
|
|
1464
|
+
size_t retainedNodeCount() const {
|
|
1465
|
+
return retainedNodeCount_;
|
|
1466
|
+
}
|
|
1467
|
+
|
|
1468
|
+
std::shared_ptr<IOSUIWorkletState> installUIWorklet(
|
|
1469
|
+
Runtime &runtime,
|
|
1470
|
+
int64_t targetNode,
|
|
1471
|
+
const Value &graphValue) {
|
|
1472
|
+
if (!hasInstance(runtime, targetNode)) {
|
|
1473
|
+
throw std::invalid_argument(
|
|
1474
|
+
"A native UI worklet target must be a live Solid Native node.");
|
|
1475
|
+
}
|
|
1476
|
+
const auto targetTag = routing_->tagForNode(targetNode);
|
|
1477
|
+
if (!targetTag.has_value()) {
|
|
1478
|
+
throw std::invalid_argument(
|
|
1479
|
+
"A native UI worklet target must have a committed Fabric identity.");
|
|
1480
|
+
}
|
|
1481
|
+
size_t valueCount = 0;
|
|
1482
|
+
std::vector<Value> ancestors;
|
|
1483
|
+
auto graph = dynamicFromJSI(
|
|
1484
|
+
runtime,
|
|
1485
|
+
graphValue,
|
|
1486
|
+
"UI worklet graph",
|
|
1487
|
+
0,
|
|
1488
|
+
valueCount,
|
|
1489
|
+
ancestors);
|
|
1490
|
+
return uiWorklets_.install(
|
|
1491
|
+
targetNode,
|
|
1492
|
+
*targetTag,
|
|
1493
|
+
solid_native::worklets::UIWorkletGraph::parse(graph));
|
|
1494
|
+
}
|
|
1495
|
+
|
|
1496
|
+
std::shared_ptr<IOSUIWorkletState> updateUIWorklet(
|
|
1497
|
+
int64_t handle,
|
|
1498
|
+
std::vector<double> inputs,
|
|
1499
|
+
double timestamp) {
|
|
1500
|
+
return uiWorklets_.update(handle, std::move(inputs), timestamp);
|
|
1501
|
+
}
|
|
1502
|
+
|
|
1503
|
+
std::shared_ptr<IOSUIWorkletState> animateUIWorklet(
|
|
1504
|
+
Runtime &runtime,
|
|
1505
|
+
int64_t handle,
|
|
1506
|
+
std::vector<double> inputs,
|
|
1507
|
+
double timestamp,
|
|
1508
|
+
const Value &timingValue) {
|
|
1509
|
+
size_t valueCount = 0;
|
|
1510
|
+
std::vector<Value> ancestors;
|
|
1511
|
+
auto timing = dynamicFromJSI(
|
|
1512
|
+
runtime,
|
|
1513
|
+
timingValue,
|
|
1514
|
+
"UI worklet timing",
|
|
1515
|
+
0,
|
|
1516
|
+
valueCount,
|
|
1517
|
+
ancestors);
|
|
1518
|
+
return uiWorklets_.animate(
|
|
1519
|
+
handle,
|
|
1520
|
+
std::move(inputs),
|
|
1521
|
+
timestamp,
|
|
1522
|
+
solid_native::worklets::UIWorkletTiming::parse(timing));
|
|
1523
|
+
}
|
|
1524
|
+
|
|
1525
|
+
std::shared_ptr<IOSUIWorkletState> animateUIWorkletKeyframes(
|
|
1526
|
+
Runtime &runtime,
|
|
1527
|
+
int64_t handle,
|
|
1528
|
+
const Value &keyframesValue,
|
|
1529
|
+
double timestamp) {
|
|
1530
|
+
auto state = uiWorklets_.requireActive(handle);
|
|
1531
|
+
size_t valueCount = 0;
|
|
1532
|
+
std::vector<Value> ancestors;
|
|
1533
|
+
auto keyframes = dynamicFromJSI(
|
|
1534
|
+
runtime,
|
|
1535
|
+
keyframesValue,
|
|
1536
|
+
"UI worklet timing keyframes",
|
|
1537
|
+
0,
|
|
1538
|
+
valueCount,
|
|
1539
|
+
ancestors);
|
|
1540
|
+
return uiWorklets_.animateKeyframes(
|
|
1541
|
+
handle,
|
|
1542
|
+
solid_native::worklets::UIWorkletTimingSequence::parse(
|
|
1543
|
+
keyframes, state->graph.inputCount()),
|
|
1544
|
+
timestamp);
|
|
1545
|
+
}
|
|
1546
|
+
|
|
1547
|
+
std::shared_ptr<IOSUIWorkletState> springUIWorklet(
|
|
1548
|
+
Runtime &runtime,
|
|
1549
|
+
int64_t handle,
|
|
1550
|
+
std::vector<double> inputs,
|
|
1551
|
+
double timestamp,
|
|
1552
|
+
const Value &springValue) {
|
|
1553
|
+
size_t valueCount = 0;
|
|
1554
|
+
std::vector<Value> ancestors;
|
|
1555
|
+
auto spring = dynamicFromJSI(
|
|
1556
|
+
runtime,
|
|
1557
|
+
springValue,
|
|
1558
|
+
"UI worklet spring",
|
|
1559
|
+
0,
|
|
1560
|
+
valueCount,
|
|
1561
|
+
ancestors);
|
|
1562
|
+
return uiWorklets_.spring(
|
|
1563
|
+
handle,
|
|
1564
|
+
std::move(inputs),
|
|
1565
|
+
timestamp,
|
|
1566
|
+
solid_native::worklets::UIWorkletSpring::parse(spring));
|
|
1567
|
+
}
|
|
1568
|
+
|
|
1569
|
+
std::shared_ptr<IOSUIWorkletState> decayUIWorklet(
|
|
1570
|
+
Runtime &runtime,
|
|
1571
|
+
int64_t handle,
|
|
1572
|
+
std::vector<double> velocities,
|
|
1573
|
+
double timestamp,
|
|
1574
|
+
const Value &decayValue) {
|
|
1575
|
+
size_t valueCount = 0;
|
|
1576
|
+
std::vector<Value> ancestors;
|
|
1577
|
+
auto decay = dynamicFromJSI(
|
|
1578
|
+
runtime,
|
|
1579
|
+
decayValue,
|
|
1580
|
+
"UI worklet decay",
|
|
1581
|
+
0,
|
|
1582
|
+
valueCount,
|
|
1583
|
+
ancestors);
|
|
1584
|
+
return uiWorklets_.decay(
|
|
1585
|
+
handle,
|
|
1586
|
+
std::move(velocities),
|
|
1587
|
+
timestamp,
|
|
1588
|
+
solid_native::worklets::UIWorkletDecay::parse(decay));
|
|
1589
|
+
}
|
|
1590
|
+
|
|
1591
|
+
std::vector<double> cancelUIWorkletAnimation(
|
|
1592
|
+
int64_t handle,
|
|
1593
|
+
double timestamp) {
|
|
1594
|
+
return uiWorklets_.cancelAnimation(handle, timestamp);
|
|
1595
|
+
}
|
|
1596
|
+
|
|
1597
|
+
std::shared_ptr<IOSUIWorkletState> attachUIWorkletPan(
|
|
1598
|
+
Runtime &runtime,
|
|
1599
|
+
int64_t handle,
|
|
1600
|
+
const Value &panValue) {
|
|
1601
|
+
auto state = uiWorklets_.requireActive(handle);
|
|
1602
|
+
size_t valueCount = 0;
|
|
1603
|
+
std::vector<Value> ancestors;
|
|
1604
|
+
auto pan = dynamicFromJSI(
|
|
1605
|
+
runtime,
|
|
1606
|
+
panValue,
|
|
1607
|
+
"UI worklet pan gesture",
|
|
1608
|
+
0,
|
|
1609
|
+
valueCount,
|
|
1610
|
+
ancestors);
|
|
1611
|
+
return uiWorklets_.attachPan(
|
|
1612
|
+
handle,
|
|
1613
|
+
solid_native::worklets::UIWorkletPanGesture::parse(
|
|
1614
|
+
pan, state->graph));
|
|
1615
|
+
}
|
|
1616
|
+
|
|
1617
|
+
void detachUIWorkletPan(int64_t handle) {
|
|
1618
|
+
uiWorklets_.detachPan(handle);
|
|
1619
|
+
}
|
|
1620
|
+
|
|
1621
|
+
std::vector<double> releaseUIWorkletPan(
|
|
1622
|
+
int64_t handle,
|
|
1623
|
+
double timestamp) {
|
|
1624
|
+
return uiWorklets_.releasePan(handle, timestamp);
|
|
1625
|
+
}
|
|
1626
|
+
|
|
1627
|
+
void destroyUIWorklet(int64_t handle) {
|
|
1628
|
+
uiWorklets_.destroy(handle);
|
|
1629
|
+
}
|
|
1630
|
+
|
|
1631
|
+
std::shared_ptr<IOSUIWorkletState> requireUIWorklet(
|
|
1632
|
+
int64_t handle) const {
|
|
1633
|
+
return uiWorklets_.requireActive(handle);
|
|
1634
|
+
}
|
|
1635
|
+
|
|
1636
|
+
size_t activeUIWorkletCount() const {
|
|
1637
|
+
return uiWorklets_.activeCount();
|
|
1638
|
+
}
|
|
1639
|
+
|
|
1640
|
+
std::pair<size_t, size_t> uiWorkletActivityCounts() const {
|
|
1641
|
+
return uiWorklets_.activityCounts();
|
|
1642
|
+
}
|
|
1643
|
+
|
|
1644
|
+
void cancelAllUIWorklets() {
|
|
1645
|
+
uiWorklets_.cancelAll();
|
|
1646
|
+
}
|
|
1647
|
+
|
|
1648
|
+
void cancelDeletedUIWorklets(const std::vector<int64_t> &handles) {
|
|
1649
|
+
for (const auto handle : handles) uiWorklets_.cancelNode(handle);
|
|
1650
|
+
}
|
|
1651
|
+
|
|
1652
|
+
void retireInstances(
|
|
1653
|
+
int64_t fabricRevision,
|
|
1654
|
+
const std::vector<int64_t> &handles) {
|
|
1655
|
+
std::vector<int64_t> retainedHandles;
|
|
1656
|
+
retainedHandles.reserve(handles.size());
|
|
1657
|
+
for (const auto handle : handles) {
|
|
1658
|
+
if (retainedHandles_.contains(handle) &&
|
|
1659
|
+
!isPendingRetirement(handle)) {
|
|
1660
|
+
retainedHandles.push_back(handle);
|
|
1661
|
+
}
|
|
1662
|
+
}
|
|
1663
|
+
if (!retainedHandles.empty()) {
|
|
1664
|
+
retiredInstances_.push_back({
|
|
1665
|
+
.fabricRevision = fabricRevision,
|
|
1666
|
+
.handles = std::move(retainedHandles),
|
|
1667
|
+
});
|
|
1668
|
+
}
|
|
1669
|
+
}
|
|
1670
|
+
|
|
1671
|
+
void reclaimMountedInstances(
|
|
1672
|
+
Runtime &runtime,
|
|
1673
|
+
int64_t mountedFabricRevision,
|
|
1674
|
+
const std::shared_ptr<EventRoutingState> &routing) {
|
|
1675
|
+
auto iterator = retiredInstances_.begin();
|
|
1676
|
+
while (iterator != retiredInstances_.end()) {
|
|
1677
|
+
if (iterator->fabricRevision > mountedFabricRevision) {
|
|
1678
|
+
iterator++;
|
|
1679
|
+
continue;
|
|
1680
|
+
}
|
|
1681
|
+
for (const auto handle : iterator->handles) {
|
|
1682
|
+
releaseInstance(runtime, handle);
|
|
1683
|
+
}
|
|
1684
|
+
iterator = retiredInstances_.erase(iterator);
|
|
1685
|
+
}
|
|
1686
|
+
}
|
|
1687
|
+
|
|
1688
|
+
void reclaimAllInstances(
|
|
1689
|
+
Runtime &runtime,
|
|
1690
|
+
const std::shared_ptr<EventRoutingState> &routing) {
|
|
1691
|
+
const std::vector<int64_t> handles(
|
|
1692
|
+
retainedHandles_.begin(), retainedHandles_.end());
|
|
1693
|
+
for (const auto handle : handles) {
|
|
1694
|
+
releaseInstance(runtime, handle);
|
|
1695
|
+
}
|
|
1696
|
+
retiredInstances_.clear();
|
|
1697
|
+
}
|
|
1698
|
+
|
|
1699
|
+
void setEventHandler(Runtime &runtime, const Value &handler) {
|
|
1700
|
+
storage(runtime).setProperty(
|
|
1701
|
+
runtime, "eventHandler", Value(runtime, handler));
|
|
1702
|
+
routing_->handlerInstalled.store(
|
|
1703
|
+
handler.isObject() && handler.getObject(runtime).isFunction(runtime),
|
|
1704
|
+
std::memory_order_release);
|
|
1705
|
+
}
|
|
1706
|
+
|
|
1707
|
+
Value eventHandler(Runtime &runtime) const {
|
|
1708
|
+
return storage(runtime).getProperty(runtime, "eventHandler");
|
|
1709
|
+
}
|
|
1710
|
+
|
|
1711
|
+
void setCommitLifecycleHandler(Runtime &runtime, const Value &handler) {
|
|
1712
|
+
storage(runtime).setProperty(
|
|
1713
|
+
runtime, "commitLifecycleHandler", Value(runtime, handler));
|
|
1714
|
+
commitLifecycleHandlerInstalled_.store(
|
|
1715
|
+
handler.isObject() && handler.getObject(runtime).isFunction(runtime),
|
|
1716
|
+
std::memory_order_release);
|
|
1717
|
+
}
|
|
1718
|
+
|
|
1719
|
+
Value commitLifecycleHandler(Runtime &runtime) const {
|
|
1720
|
+
return storage(runtime).getProperty(runtime, "commitLifecycleHandler");
|
|
1721
|
+
}
|
|
1722
|
+
|
|
1723
|
+
bool hasCommitLifecycleHandler() const {
|
|
1724
|
+
return commitLifecycleHandlerInstalled_.load(std::memory_order_acquire);
|
|
1725
|
+
}
|
|
1726
|
+
|
|
1727
|
+
private:
|
|
1728
|
+
static std::string keyFor(int64_t handle) {
|
|
1729
|
+
return std::to_string(handle);
|
|
1730
|
+
}
|
|
1731
|
+
|
|
1732
|
+
bool isPendingRetirement(int64_t handle) const {
|
|
1733
|
+
return std::any_of(
|
|
1734
|
+
retiredInstances_.begin(),
|
|
1735
|
+
retiredInstances_.end(),
|
|
1736
|
+
[handle](const RetiredInstances &retired) {
|
|
1737
|
+
return std::find(
|
|
1738
|
+
retired.handles.begin(),
|
|
1739
|
+
retired.handles.end(),
|
|
1740
|
+
handle) != retired.handles.end();
|
|
1741
|
+
});
|
|
1742
|
+
}
|
|
1743
|
+
|
|
1744
|
+
facebook::jsi::WeakObject storage_;
|
|
1745
|
+
std::shared_ptr<EventRoutingState> routing_;
|
|
1746
|
+
size_t retainedNodeCount_{0};
|
|
1747
|
+
std::unordered_set<int64_t> retainedHandles_;
|
|
1748
|
+
std::vector<RetiredInstances> retiredInstances_;
|
|
1749
|
+
int64_t nextResourceHandle_{1};
|
|
1750
|
+
std::unordered_map<int64_t, std::string> resourceKinds_;
|
|
1751
|
+
std::atomic<bool> commitLifecycleHandlerInstalled_{false};
|
|
1752
|
+
IOSUIWorkletRegistry uiWorklets_;
|
|
1753
|
+
std::function<void()> runtimeRetirementHandler_;
|
|
1754
|
+
};
|
|
1755
|
+
|
|
1756
|
+
struct PreparedNodeIdentities final {
|
|
1757
|
+
NodeIdentityMap identities;
|
|
1758
|
+
std::vector<std::pair<int64_t, Value>> retainedValues;
|
|
1759
|
+
};
|
|
1760
|
+
|
|
1761
|
+
class IdentityRetention final {
|
|
1762
|
+
public:
|
|
1763
|
+
IdentityRetention(
|
|
1764
|
+
Runtime &runtime,
|
|
1765
|
+
const std::shared_ptr<RuntimeState> &state,
|
|
1766
|
+
PreparedNodeIdentities &prepared)
|
|
1767
|
+
: runtime_(runtime), state_(state) {
|
|
1768
|
+
handles_.reserve(prepared.retainedValues.size());
|
|
1769
|
+
try {
|
|
1770
|
+
for (auto &[handle, value] : prepared.retainedValues) {
|
|
1771
|
+
if (state_->hasInstance(runtime_, handle)) {
|
|
1772
|
+
throw std::invalid_argument(
|
|
1773
|
+
"Node " + std::to_string(handle) +
|
|
1774
|
+
" already owns a JavaScript instance identity.");
|
|
1775
|
+
}
|
|
1776
|
+
state_->retainInstance(runtime_, handle, std::move(value));
|
|
1777
|
+
handles_.push_back(handle);
|
|
1778
|
+
}
|
|
1779
|
+
} catch (...) {
|
|
1780
|
+
rollback();
|
|
1781
|
+
throw;
|
|
1782
|
+
}
|
|
1783
|
+
}
|
|
1784
|
+
|
|
1785
|
+
~IdentityRetention() {
|
|
1786
|
+
if (committed_) return;
|
|
1787
|
+
rollback();
|
|
1788
|
+
}
|
|
1789
|
+
|
|
1790
|
+
void commit() {
|
|
1791
|
+
committed_ = true;
|
|
1792
|
+
}
|
|
1793
|
+
|
|
1794
|
+
private:
|
|
1795
|
+
void rollback() {
|
|
1796
|
+
for (const auto handle : handles_) {
|
|
1797
|
+
state_->releaseInstance(runtime_, handle);
|
|
1798
|
+
}
|
|
1799
|
+
handles_.clear();
|
|
1800
|
+
}
|
|
1801
|
+
|
|
1802
|
+
Runtime &runtime_;
|
|
1803
|
+
std::shared_ptr<RuntimeState> state_;
|
|
1804
|
+
std::vector<int64_t> handles_;
|
|
1805
|
+
bool committed_{false};
|
|
1806
|
+
};
|
|
1807
|
+
|
|
1808
|
+
std::string utf8(NSString *value) {
|
|
1809
|
+
const char *characters = value.UTF8String;
|
|
1810
|
+
if (characters == nullptr) {
|
|
1811
|
+
throw std::invalid_argument("A native string is not valid UTF-8.");
|
|
1812
|
+
}
|
|
1813
|
+
return characters;
|
|
1814
|
+
}
|
|
1815
|
+
|
|
1816
|
+
std::string childPath(const std::string &path, const std::string &name) {
|
|
1817
|
+
return path + "." + name;
|
|
1818
|
+
}
|
|
1819
|
+
|
|
1820
|
+
std::string indexPath(const std::string &path, size_t index) {
|
|
1821
|
+
return path + "[" + std::to_string(index) + "]";
|
|
1822
|
+
}
|
|
1823
|
+
|
|
1824
|
+
std::string requireTransportString(
|
|
1825
|
+
std::string value,
|
|
1826
|
+
const std::string &path) {
|
|
1827
|
+
if (value.find('\0') != std::string::npos) {
|
|
1828
|
+
throw std::invalid_argument(
|
|
1829
|
+
path + ": strings containing a null byte are not supported.");
|
|
1830
|
+
}
|
|
1831
|
+
return value;
|
|
1832
|
+
}
|
|
1833
|
+
|
|
1834
|
+
int64_t requireSafeInteger(
|
|
1835
|
+
Runtime &runtime,
|
|
1836
|
+
const Value &value,
|
|
1837
|
+
const char *name,
|
|
1838
|
+
bool positive) {
|
|
1839
|
+
if (!value.isNumber()) {
|
|
1840
|
+
throw JSError(runtime, std::string(name) + " must be an integer.");
|
|
1841
|
+
}
|
|
1842
|
+
const auto number = value.getNumber();
|
|
1843
|
+
if (!std::isfinite(number) || std::trunc(number) != number ||
|
|
1844
|
+
std::abs(number) > 9007199254740991.0 || (positive && number <= 0)) {
|
|
1845
|
+
throw JSError(
|
|
1846
|
+
runtime,
|
|
1847
|
+
std::string(name) +
|
|
1848
|
+
(positive ? " must be a positive safe integer."
|
|
1849
|
+
: " must be a safe integer."));
|
|
1850
|
+
}
|
|
1851
|
+
return static_cast<int64_t>(number);
|
|
1852
|
+
}
|
|
1853
|
+
|
|
1854
|
+
std::string requireResourceKind(Runtime &runtime, const Value &value) {
|
|
1855
|
+
if (!value.isString()) {
|
|
1856
|
+
throw JSError(runtime, "The native resource kind must be a string.");
|
|
1857
|
+
}
|
|
1858
|
+
const auto kind = value.getString(runtime).utf8(runtime);
|
|
1859
|
+
const auto validStart = [](char character) {
|
|
1860
|
+
return character >= 'a' && character <= 'z';
|
|
1861
|
+
};
|
|
1862
|
+
const auto validPart = [&](char character) {
|
|
1863
|
+
return validStart(character) ||
|
|
1864
|
+
(character >= '0' && character <= '9') || character == '.' ||
|
|
1865
|
+
character == '-';
|
|
1866
|
+
};
|
|
1867
|
+
if (kind.empty() || kind.size() > MaximumResourceKindLength ||
|
|
1868
|
+
!validStart(kind.front()) ||
|
|
1869
|
+
!std::all_of(kind.begin() + 1, kind.end(), validPart)) {
|
|
1870
|
+
throw JSError(
|
|
1871
|
+
runtime,
|
|
1872
|
+
"The native resource kind must contain 1-128 lowercase identifier "
|
|
1873
|
+
"characters.");
|
|
1874
|
+
}
|
|
1875
|
+
return kind;
|
|
1876
|
+
}
|
|
1877
|
+
|
|
1878
|
+
std::string requireFatalErrorText(
|
|
1879
|
+
Runtime &runtime,
|
|
1880
|
+
const Value &value,
|
|
1881
|
+
const char *name,
|
|
1882
|
+
size_t maximumBytes) {
|
|
1883
|
+
if (!value.isString()) {
|
|
1884
|
+
throw JSError(runtime, std::string{name} + " must be a string.");
|
|
1885
|
+
}
|
|
1886
|
+
const auto text = value.getString(runtime).utf8(runtime);
|
|
1887
|
+
if (text.empty() || text.size() > maximumBytes ||
|
|
1888
|
+
text.find('\0') != std::string::npos) {
|
|
1889
|
+
throw JSError(
|
|
1890
|
+
runtime,
|
|
1891
|
+
std::string{name} +
|
|
1892
|
+
" must be a bounded non-empty string without null bytes.");
|
|
1893
|
+
}
|
|
1894
|
+
return text;
|
|
1895
|
+
}
|
|
1896
|
+
|
|
1897
|
+
bool containsObject(
|
|
1898
|
+
Runtime &runtime,
|
|
1899
|
+
const std::vector<Value> &ancestors,
|
|
1900
|
+
const Object &candidate) {
|
|
1901
|
+
for (const auto &value : ancestors) {
|
|
1902
|
+
auto ancestor = value.getObject(runtime);
|
|
1903
|
+
if (Object::strictEquals(runtime, ancestor, candidate)) return true;
|
|
1904
|
+
}
|
|
1905
|
+
return false;
|
|
1906
|
+
}
|
|
1907
|
+
|
|
1908
|
+
folly::dynamic dynamicFromJSI(
|
|
1909
|
+
Runtime &runtime,
|
|
1910
|
+
const Value &value,
|
|
1911
|
+
const std::string &path,
|
|
1912
|
+
size_t depth,
|
|
1913
|
+
size_t &valueCount,
|
|
1914
|
+
std::vector<Value> &ancestors) {
|
|
1915
|
+
if (++valueCount > MaximumTransportValues) {
|
|
1916
|
+
throw std::invalid_argument(
|
|
1917
|
+
"The host transaction exceeds the transport value limit.");
|
|
1918
|
+
}
|
|
1919
|
+
if (depth > MaximumTransportDepth) {
|
|
1920
|
+
throw std::invalid_argument(
|
|
1921
|
+
path + ": the host transaction exceeds the nesting limit.");
|
|
1922
|
+
}
|
|
1923
|
+
if (value.isUndefined()) {
|
|
1924
|
+
throw std::invalid_argument(
|
|
1925
|
+
path + ": undefined is not a transport-safe value.");
|
|
1926
|
+
}
|
|
1927
|
+
if (value.isNull()) return nullptr;
|
|
1928
|
+
if (value.isBool()) return value.getBool();
|
|
1929
|
+
if (value.isNumber()) {
|
|
1930
|
+
const auto number = value.getNumber();
|
|
1931
|
+
if (!std::isfinite(number)) {
|
|
1932
|
+
throw std::invalid_argument(path + ": numbers must be finite.");
|
|
1933
|
+
}
|
|
1934
|
+
return number;
|
|
1935
|
+
}
|
|
1936
|
+
if (value.isString()) {
|
|
1937
|
+
return requireTransportString(
|
|
1938
|
+
value.getString(runtime).utf8(runtime), path);
|
|
1939
|
+
}
|
|
1940
|
+
if (value.isBigInt()) {
|
|
1941
|
+
throw std::invalid_argument(
|
|
1942
|
+
path + ": bigint is not a transport-safe value.");
|
|
1943
|
+
}
|
|
1944
|
+
if (value.isSymbol()) {
|
|
1945
|
+
throw std::invalid_argument(
|
|
1946
|
+
path + ": symbol is not a transport-safe value.");
|
|
1947
|
+
}
|
|
1948
|
+
if (!value.isObject()) {
|
|
1949
|
+
throw std::invalid_argument(path + ": unsupported JavaScript value.");
|
|
1950
|
+
}
|
|
1951
|
+
|
|
1952
|
+
auto object = value.getObject(runtime);
|
|
1953
|
+
if (object.isFunction(runtime)) {
|
|
1954
|
+
throw std::invalid_argument(
|
|
1955
|
+
path + ": functions are not transport-safe values.");
|
|
1956
|
+
}
|
|
1957
|
+
if (containsObject(runtime, ancestors, object)) {
|
|
1958
|
+
throw std::invalid_argument(
|
|
1959
|
+
path + ": cyclic objects are not transport-safe values.");
|
|
1960
|
+
}
|
|
1961
|
+
ancestors.emplace_back(runtime, object);
|
|
1962
|
+
|
|
1963
|
+
if (object.isArray(runtime)) {
|
|
1964
|
+
auto array = object.getArray(runtime);
|
|
1965
|
+
folly::dynamic result = folly::dynamic::array;
|
|
1966
|
+
const auto size = array.size(runtime);
|
|
1967
|
+
result.reserve(size);
|
|
1968
|
+
for (size_t index = 0; index < size; ++index) {
|
|
1969
|
+
const auto child = array.getValueAtIndex(runtime, index);
|
|
1970
|
+
result.push_back(dynamicFromJSI(
|
|
1971
|
+
runtime,
|
|
1972
|
+
child,
|
|
1973
|
+
indexPath(path, index),
|
|
1974
|
+
depth + 1,
|
|
1975
|
+
valueCount,
|
|
1976
|
+
ancestors));
|
|
1977
|
+
}
|
|
1978
|
+
ancestors.pop_back();
|
|
1979
|
+
return result;
|
|
1980
|
+
}
|
|
1981
|
+
|
|
1982
|
+
folly::dynamic result = folly::dynamic::object;
|
|
1983
|
+
auto names = object.getPropertyNames(runtime);
|
|
1984
|
+
const auto size = names.size(runtime);
|
|
1985
|
+
for (size_t index = 0; index < size; ++index) {
|
|
1986
|
+
auto nameString =
|
|
1987
|
+
names.getValueAtIndex(runtime, index).getString(runtime);
|
|
1988
|
+
auto name = requireTransportString(nameString.utf8(runtime), path);
|
|
1989
|
+
const auto child = object.getProperty(runtime, nameString);
|
|
1990
|
+
result[name] = dynamicFromJSI(
|
|
1991
|
+
runtime,
|
|
1992
|
+
child,
|
|
1993
|
+
childPath(path, name),
|
|
1994
|
+
depth + 1,
|
|
1995
|
+
valueCount,
|
|
1996
|
+
ancestors);
|
|
1997
|
+
}
|
|
1998
|
+
ancestors.pop_back();
|
|
1999
|
+
return result;
|
|
2000
|
+
}
|
|
2001
|
+
|
|
2002
|
+
id foundationFromDynamic(const folly::dynamic &value) {
|
|
2003
|
+
switch (value.type()) {
|
|
2004
|
+
case folly::dynamic::NULLT:
|
|
2005
|
+
return NSNull.null;
|
|
2006
|
+
case folly::dynamic::BOOL:
|
|
2007
|
+
return @(value.getBool());
|
|
2008
|
+
case folly::dynamic::DOUBLE:
|
|
2009
|
+
return @(value.getDouble());
|
|
2010
|
+
case folly::dynamic::INT64:
|
|
2011
|
+
return @(value.getInt());
|
|
2012
|
+
case folly::dynamic::STRING:
|
|
2013
|
+
return [NSString stringWithUTF8String:value.getString().c_str()];
|
|
2014
|
+
case folly::dynamic::ARRAY: {
|
|
2015
|
+
NSMutableArray *array =
|
|
2016
|
+
[NSMutableArray arrayWithCapacity:value.size()];
|
|
2017
|
+
for (const auto &child : value) {
|
|
2018
|
+
[array addObject:foundationFromDynamic(child)];
|
|
2019
|
+
}
|
|
2020
|
+
return array;
|
|
2021
|
+
}
|
|
2022
|
+
case folly::dynamic::OBJECT: {
|
|
2023
|
+
NSMutableDictionary<NSString *, id> *dictionary =
|
|
2024
|
+
[NSMutableDictionary dictionaryWithCapacity:value.size()];
|
|
2025
|
+
for (const auto &item : value.items()) {
|
|
2026
|
+
if (!item.first.isString()) {
|
|
2027
|
+
throw std::invalid_argument(
|
|
2028
|
+
"The host transaction contains a non-string object key.");
|
|
2029
|
+
}
|
|
2030
|
+
auto key =
|
|
2031
|
+
[NSString stringWithUTF8String:item.first.getString().c_str()];
|
|
2032
|
+
dictionary[key] = foundationFromDynamic(item.second);
|
|
2033
|
+
}
|
|
2034
|
+
return dictionary;
|
|
2035
|
+
}
|
|
2036
|
+
}
|
|
2037
|
+
throw std::invalid_argument("Unsupported host transaction value.");
|
|
2038
|
+
}
|
|
2039
|
+
|
|
2040
|
+
Object surfaceInfoObject(
|
|
2041
|
+
Runtime &runtime,
|
|
2042
|
+
NSDictionary<NSString *, id> *snapshot,
|
|
2043
|
+
size_t retainedNodeCount,
|
|
2044
|
+
size_t retainedResourceCount,
|
|
2045
|
+
size_t activeUIWorkletCount,
|
|
2046
|
+
size_t pendingUIWorkletFrameCount) {
|
|
2047
|
+
Object result(runtime);
|
|
2048
|
+
result.setProperty(
|
|
2049
|
+
runtime, "ready", [snapshot[@"ready"] boolValue] == YES);
|
|
2050
|
+
result.setProperty(
|
|
2051
|
+
runtime, "surface", [snapshot[@"surface"] doubleValue]);
|
|
2052
|
+
result.setProperty(
|
|
2053
|
+
runtime, "sequence", [snapshot[@"sequence"] doubleValue]);
|
|
2054
|
+
result.setProperty(
|
|
2055
|
+
runtime, "retainedNodeCount", static_cast<double>(retainedNodeCount));
|
|
2056
|
+
result.setProperty(
|
|
2057
|
+
runtime,
|
|
2058
|
+
"retainedResourceCount",
|
|
2059
|
+
static_cast<double>(retainedResourceCount));
|
|
2060
|
+
result.setProperty(
|
|
2061
|
+
runtime,
|
|
2062
|
+
"activeUIWorkletCount",
|
|
2063
|
+
static_cast<double>(activeUIWorkletCount));
|
|
2064
|
+
result.setProperty(
|
|
2065
|
+
runtime,
|
|
2066
|
+
"pendingUIWorkletFrameCount",
|
|
2067
|
+
static_cast<double>(pendingUIWorkletFrameCount));
|
|
2068
|
+
return result;
|
|
2069
|
+
}
|
|
2070
|
+
|
|
2071
|
+
Object commitResultObject(
|
|
2072
|
+
Runtime &runtime,
|
|
2073
|
+
NSDictionary<NSString *, id> *result) {
|
|
2074
|
+
Object object(runtime);
|
|
2075
|
+
object.setProperty(runtime, "surface", [result[@"surface"] doubleValue]);
|
|
2076
|
+
object.setProperty(runtime, "sequence", [result[@"sequence"] doubleValue]);
|
|
2077
|
+
object.setProperty(
|
|
2078
|
+
runtime, "mounted", [result[@"mounted"] boolValue] == YES);
|
|
2079
|
+
id fabricRevision = result[@"fabricRevision"];
|
|
2080
|
+
if ([fabricRevision isKindOfClass:NSNumber.class]) {
|
|
2081
|
+
object.setProperty(
|
|
2082
|
+
runtime,
|
|
2083
|
+
"hostRevision",
|
|
2084
|
+
[(NSNumber *)fabricRevision doubleValue]);
|
|
2085
|
+
}
|
|
2086
|
+
return object;
|
|
2087
|
+
}
|
|
2088
|
+
|
|
2089
|
+
std::optional<std::string> commitCausalOperationId(
|
|
2090
|
+
NSDictionary<NSString *, id> *transaction) {
|
|
2091
|
+
id contextValue = transaction[@"causalContext"];
|
|
2092
|
+
if (![contextValue isKindOfClass:NSDictionary.class]) return std::nullopt;
|
|
2093
|
+
id operationIdValue = ((NSDictionary *)contextValue)[@"operationId"];
|
|
2094
|
+
if (![operationIdValue isKindOfClass:NSString.class]) return std::nullopt;
|
|
2095
|
+
return utf8((NSString *)operationIdValue);
|
|
2096
|
+
}
|
|
2097
|
+
|
|
2098
|
+
Object measurementObject(
|
|
2099
|
+
Runtime &runtime,
|
|
2100
|
+
NSDictionary<NSString *, id> *measurement) {
|
|
2101
|
+
Object object(runtime);
|
|
2102
|
+
object.setProperty(runtime, "x", [measurement[@"x"] doubleValue]);
|
|
2103
|
+
object.setProperty(runtime, "y", [measurement[@"y"] doubleValue]);
|
|
2104
|
+
object.setProperty(
|
|
2105
|
+
runtime, "width", [measurement[@"width"] doubleValue]);
|
|
2106
|
+
object.setProperty(
|
|
2107
|
+
runtime, "height", [measurement[@"height"] doubleValue]);
|
|
2108
|
+
object.setProperty(runtime, "pageX", [measurement[@"pageX"] doubleValue]);
|
|
2109
|
+
object.setProperty(runtime, "pageY", [measurement[@"pageY"] doubleValue]);
|
|
2110
|
+
object.setProperty(
|
|
2111
|
+
runtime,
|
|
2112
|
+
"observedSequence",
|
|
2113
|
+
[measurement[@"observedSequence"] doubleValue]);
|
|
2114
|
+
return object;
|
|
2115
|
+
}
|
|
2116
|
+
|
|
2117
|
+
std::vector<int64_t> deletedNodeHandles(
|
|
2118
|
+
NSDictionary<NSString *, id> *transaction) {
|
|
2119
|
+
std::vector<int64_t> handles;
|
|
2120
|
+
id mutationsValue = transaction[@"mutations"];
|
|
2121
|
+
if (![mutationsValue isKindOfClass:NSArray.class]) return handles;
|
|
2122
|
+
for (id value in (NSArray *)mutationsValue) {
|
|
2123
|
+
if (![value isKindOfClass:NSDictionary.class]) continue;
|
|
2124
|
+
auto mutation = (NSDictionary<NSString *, id> *)value;
|
|
2125
|
+
if (![mutation[@"type"] isEqual:@"delete-node"] ||
|
|
2126
|
+
![mutation[@"node"] isKindOfClass:NSNumber.class]) {
|
|
2127
|
+
continue;
|
|
2128
|
+
}
|
|
2129
|
+
handles.push_back([(NSNumber *)mutation[@"node"] longLongValue]);
|
|
2130
|
+
}
|
|
2131
|
+
return handles;
|
|
2132
|
+
}
|
|
2133
|
+
|
|
2134
|
+
PreparedNodeIdentities prepareNodeIdentities(
|
|
2135
|
+
Runtime &runtime,
|
|
2136
|
+
NSDictionary<NSString *, id> *transaction,
|
|
2137
|
+
int64_t surface,
|
|
2138
|
+
const std::shared_ptr<RuntimeState> &state) {
|
|
2139
|
+
PreparedNodeIdentities prepared;
|
|
2140
|
+
id mutationsValue = transaction[@"mutations"];
|
|
2141
|
+
if (![mutationsValue isKindOfClass:NSArray.class]) return prepared;
|
|
2142
|
+
|
|
2143
|
+
for (id value in (NSArray *)mutationsValue) {
|
|
2144
|
+
if (![value isKindOfClass:NSDictionary.class]) continue;
|
|
2145
|
+
auto mutation = (NSDictionary<NSString *, id> *)value;
|
|
2146
|
+
id typeValue = mutation[@"type"];
|
|
2147
|
+
if (![typeValue isKindOfClass:NSString.class]) continue;
|
|
2148
|
+
NSString *type = (NSString *)typeValue;
|
|
2149
|
+
if (![type isEqualToString:@"create-element"] &&
|
|
2150
|
+
![type isEqualToString:@"create-text"]) {
|
|
2151
|
+
continue;
|
|
2152
|
+
}
|
|
2153
|
+
|
|
2154
|
+
id nodeValue = mutation[@"node"];
|
|
2155
|
+
if (![nodeValue isKindOfClass:NSNumber.class] ||
|
|
2156
|
+
CFGetTypeID((__bridge CFTypeRef)nodeValue) == CFBooleanGetTypeID()) {
|
|
2157
|
+
continue;
|
|
2158
|
+
}
|
|
2159
|
+
const double nodeNumber = [(NSNumber *)nodeValue doubleValue];
|
|
2160
|
+
if (!std::isfinite(nodeNumber) || std::trunc(nodeNumber) != nodeNumber ||
|
|
2161
|
+
nodeNumber <= 0 || nodeNumber > 9007199254740991.0) {
|
|
2162
|
+
continue;
|
|
2163
|
+
}
|
|
2164
|
+
const auto node = static_cast<int64_t>(nodeNumber);
|
|
2165
|
+
if (prepared.identities.contains(node)) continue;
|
|
2166
|
+
if (state->hasInstance(runtime, node)) {
|
|
2167
|
+
throw std::invalid_argument(
|
|
2168
|
+
"Node " + std::to_string(node) +
|
|
2169
|
+
" cannot reuse a retained JavaScript instance identity.");
|
|
2170
|
+
}
|
|
2171
|
+
|
|
2172
|
+
Object instance(runtime);
|
|
2173
|
+
instance.setProperty(runtime, "surface", static_cast<double>(surface));
|
|
2174
|
+
instance.setProperty(runtime, "node", static_cast<double>(node));
|
|
2175
|
+
auto objectConstructor =
|
|
2176
|
+
runtime.global().getPropertyAsObject(runtime, "Object");
|
|
2177
|
+
auto freeze = objectConstructor.getPropertyAsFunction(runtime, "freeze");
|
|
2178
|
+
freeze.call(runtime, instance);
|
|
2179
|
+
|
|
2180
|
+
const auto tag = TransactionCoordinator::allocateFabricTag();
|
|
2181
|
+
Value retainedValue(runtime, instance);
|
|
2182
|
+
auto instanceHandle = std::make_shared<facebook::react::InstanceHandle>(
|
|
2183
|
+
runtime, retainedValue, tag);
|
|
2184
|
+
prepared.identities.emplace(
|
|
2185
|
+
node, NodeIdentity{.tag = tag, .instanceHandle = instanceHandle});
|
|
2186
|
+
prepared.retainedValues.emplace_back(
|
|
2187
|
+
node, Value(runtime, retainedValue));
|
|
2188
|
+
}
|
|
2189
|
+
return prepared;
|
|
2190
|
+
}
|
|
2191
|
+
|
|
2192
|
+
std::shared_ptr<facebook::react::EventListener> createEventListener(
|
|
2193
|
+
const std::shared_ptr<EventRoutingState> &routing,
|
|
2194
|
+
const std::shared_ptr<facebook::react::RuntimeScheduler> &runtimeScheduler,
|
|
2195
|
+
const std::weak_ptr<RuntimeState> &runtimeState) {
|
|
2196
|
+
std::weak_ptr<facebook::react::RuntimeScheduler> weakScheduler =
|
|
2197
|
+
runtimeScheduler;
|
|
2198
|
+
return std::make_shared<facebook::react::EventListener>(
|
|
2199
|
+
[routing, weakScheduler, runtimeState](
|
|
2200
|
+
const facebook::react::RawEvent &event) -> bool {
|
|
2201
|
+
auto family = event.shadowNodeFamily.lock();
|
|
2202
|
+
if (!family || family->getSurfaceId() !=
|
|
2203
|
+
routing->surface.load(std::memory_order_acquire)) {
|
|
2204
|
+
return false;
|
|
2205
|
+
}
|
|
2206
|
+
auto route = routing->route(family->getTag(), event.type);
|
|
2207
|
+
if (!route.has_value()) return false;
|
|
2208
|
+
if (!routing->active.load(std::memory_order_acquire) ||
|
|
2209
|
+
!routing->handlerInstalled.load(std::memory_order_acquire) ||
|
|
2210
|
+
route->empty() || !event.eventPayload) {
|
|
2211
|
+
return true;
|
|
2212
|
+
}
|
|
2213
|
+
auto scheduler = weakScheduler.lock();
|
|
2214
|
+
if (!scheduler) return true;
|
|
2215
|
+
|
|
2216
|
+
const auto surface = family->getSurfaceId();
|
|
2217
|
+
const auto observedSequence =
|
|
2218
|
+
routing->sequence.load(std::memory_order_acquire);
|
|
2219
|
+
const auto timestamp = event.eventStartTimeStamp.toDOMHighResTimeStamp();
|
|
2220
|
+
const auto coalescible = event.isUnique;
|
|
2221
|
+
const auto payload = event.eventPayload;
|
|
2222
|
+
for (const auto &routedEvent : *route) {
|
|
2223
|
+
const auto node = routedEvent.node;
|
|
2224
|
+
const auto name = routedEvent.name;
|
|
2225
|
+
const auto priority =
|
|
2226
|
+
std::string(semanticEventPriority(name, event.category));
|
|
2227
|
+
const auto bubbles = eventBubbles(name);
|
|
2228
|
+
scheduler->scheduleWork(
|
|
2229
|
+
[routing,
|
|
2230
|
+
runtimeState,
|
|
2231
|
+
payload,
|
|
2232
|
+
surface,
|
|
2233
|
+
node,
|
|
2234
|
+
observedSequence,
|
|
2235
|
+
name,
|
|
2236
|
+
timestamp,
|
|
2237
|
+
priority,
|
|
2238
|
+
bubbles,
|
|
2239
|
+
coalescible](Runtime &runtime) {
|
|
2240
|
+
if (!routing->active.load(std::memory_order_acquire)) return;
|
|
2241
|
+
if (!routing->isSubscribed(node, name)) return;
|
|
2242
|
+
auto state = runtimeState.lock();
|
|
2243
|
+
if (!state) return;
|
|
2244
|
+
auto handlerValue = state->eventHandler(runtime);
|
|
2245
|
+
if (!handlerValue.isObject()) return;
|
|
2246
|
+
auto handlerObject = handlerValue.getObject(runtime);
|
|
2247
|
+
if (!handlerObject.isFunction(runtime)) return;
|
|
2248
|
+
|
|
2249
|
+
Object nativeEvent(runtime);
|
|
2250
|
+
nativeEvent.setProperty(
|
|
2251
|
+
runtime, "surface", static_cast<double>(surface));
|
|
2252
|
+
nativeEvent.setProperty(
|
|
2253
|
+
runtime, "target", static_cast<double>(node));
|
|
2254
|
+
nativeEvent.setProperty(
|
|
2255
|
+
runtime,
|
|
2256
|
+
"observedSequence",
|
|
2257
|
+
static_cast<double>(observedSequence));
|
|
2258
|
+
nativeEvent.setProperty(
|
|
2259
|
+
runtime,
|
|
2260
|
+
"name",
|
|
2261
|
+
String::createFromUtf8(runtime, name));
|
|
2262
|
+
nativeEvent.setProperty(runtime, "timestamp", timestamp);
|
|
2263
|
+
nativeEvent.setProperty(
|
|
2264
|
+
runtime,
|
|
2265
|
+
"priority",
|
|
2266
|
+
String::createFromUtf8(runtime, priority));
|
|
2267
|
+
nativeEvent.setProperty(runtime, "bubbles", bubbles);
|
|
2268
|
+
nativeEvent.setProperty(
|
|
2269
|
+
runtime, "coalescible", coalescible);
|
|
2270
|
+
nativeEvent.setProperty(
|
|
2271
|
+
runtime, "payload", payload->asJSIValue(runtime));
|
|
2272
|
+
auto handler = handlerObject.getFunction(runtime);
|
|
2273
|
+
handler.call(runtime, nativeEvent);
|
|
2274
|
+
});
|
|
2275
|
+
}
|
|
2276
|
+
return true;
|
|
2277
|
+
});
|
|
2278
|
+
}
|
|
2279
|
+
|
|
2280
|
+
} // namespace
|
|
2281
|
+
|
|
2282
|
+
@interface SolidNativeFabricSurface (SolidNativeJSIIdentities)
|
|
2283
|
+
- (nullable NSDictionary<NSString *, id> *)
|
|
2284
|
+
applyTransactionSynchronously:(NSDictionary<NSString *, id> *)transaction
|
|
2285
|
+
nodeIdentities:
|
|
2286
|
+
(const solid_native::fabric::react_native::NodeIdentityMap *)
|
|
2287
|
+
nodeIdentities
|
|
2288
|
+
runtime:(facebook::jsi::Runtime *)runtime
|
|
2289
|
+
resourceResolver:
|
|
2290
|
+
(const NativeResourceResolver *)resourceResolver
|
|
2291
|
+
error:(NSError *_Nullable *_Nullable)error;
|
|
2292
|
+
- (void)addSolidNativeEventListener:
|
|
2293
|
+
(const std::shared_ptr<facebook::react::EventListener> &)listener;
|
|
2294
|
+
- (void)removeSolidNativeEventListener:
|
|
2295
|
+
(const std::shared_ptr<facebook::react::EventListener> &)listener;
|
|
2296
|
+
- (nullable NSDictionary<NSString *, id> *)
|
|
2297
|
+
measureNodeSynchronously:(int64_t)node
|
|
2298
|
+
afterSequence:(int64_t)afterSequence
|
|
2299
|
+
error:(NSError *_Nullable *_Nullable)error;
|
|
2300
|
+
- (BOOL)hasNoLogicalNodes;
|
|
2301
|
+
- (int64_t)mountedFabricRevision;
|
|
2302
|
+
- (nullable UIView *)findComponentViewWithTag:(facebook::react::Tag)tag;
|
|
2303
|
+
@end
|
|
2304
|
+
|
|
2305
|
+
@interface SolidNativeFabricJSIBinding ()
|
|
2306
|
+
- (BOOL)reportFatalErrorWithName:(NSString *)name
|
|
2307
|
+
message:(NSString *)message;
|
|
2308
|
+
- (void)updateStatusBarStyle:(NSString *)style animated:(BOOL)animated;
|
|
2309
|
+
- (void)updateStatusBarHidden:(BOOL)hidden animation:(NSString *)animation;
|
|
2310
|
+
- (NSDictionary<NSString *, id> *)surfaceSnapshot;
|
|
2311
|
+
- (nullable NSDictionary<NSString *, id> *)
|
|
2312
|
+
applyTransaction:(NSDictionary<NSString *, id> *)transaction
|
|
2313
|
+
nodeIdentities:
|
|
2314
|
+
(const solid_native::fabric::react_native::NodeIdentityMap *)nodeIdentities
|
|
2315
|
+
runtime:(facebook::jsi::Runtime *)runtime
|
|
2316
|
+
resourceResolver:(const NativeResourceResolver *)resourceResolver
|
|
2317
|
+
error:(NSError *_Nullable *_Nullable)error;
|
|
2318
|
+
- (nullable NSDictionary<NSString *, id> *)
|
|
2319
|
+
measureNode:(int64_t)node
|
|
2320
|
+
afterSequence:(int64_t)afterSequence
|
|
2321
|
+
error:(NSError *_Nullable *_Nullable)error;
|
|
2322
|
+
- (BOOL)destroySurface:(NSError *_Nullable *_Nullable)error;
|
|
2323
|
+
- (void)scheduleUIWorkletFrame:
|
|
2324
|
+
(std::shared_ptr<IOSUIWorkletState>)worklet;
|
|
2325
|
+
- (void)attachUIWorkletPanGesture:
|
|
2326
|
+
(std::shared_ptr<IOSUIWorkletState>)worklet;
|
|
2327
|
+
- (BOOL)applyUIWorkletFrame:(const IOSUIWorkletState &)worklet
|
|
2328
|
+
values:
|
|
2329
|
+
(const std::array<double, IOSViewOutputCount> &)values;
|
|
2330
|
+
- (void)scheduleMountObservation:
|
|
2331
|
+
(std::shared_ptr<const CommitMountObservation>)observation;
|
|
2332
|
+
- (void)pollMountObservation:
|
|
2333
|
+
(std::shared_ptr<const CommitMountObservation>)observation
|
|
2334
|
+
surface:(SolidNativeFabricSurface *)surface
|
|
2335
|
+
runtimeScheduler:
|
|
2336
|
+
(std::shared_ptr<facebook::react::RuntimeScheduler>)
|
|
2337
|
+
runtimeScheduler
|
|
2338
|
+
runtimeState:(std::weak_ptr<RuntimeState>)runtimeState
|
|
2339
|
+
routing:(std::shared_ptr<EventRoutingState>)routing
|
|
2340
|
+
attempt:(NSUInteger)attempt;
|
|
2341
|
+
- (void)retireRuntimeGeneration:(uint64_t)generation;
|
|
2342
|
+
@end
|
|
2343
|
+
|
|
2344
|
+
@implementation SolidNativeFabricJSIBinding {
|
|
2345
|
+
NSLock *_lock;
|
|
2346
|
+
SolidNativeFabricSurface *_surface;
|
|
2347
|
+
SolidNativeFabricSurface *_eventSurface;
|
|
2348
|
+
BOOL _surfaceReady;
|
|
2349
|
+
BOOL _invalidated;
|
|
2350
|
+
BOOL _runtimeInstalled;
|
|
2351
|
+
uint64_t _runtimeGeneration;
|
|
2352
|
+
std::shared_ptr<EventRoutingState> _routing;
|
|
2353
|
+
std::weak_ptr<RuntimeState> _runtimeState;
|
|
2354
|
+
std::shared_ptr<facebook::react::RuntimeScheduler> _runtimeScheduler;
|
|
2355
|
+
std::shared_ptr<facebook::react::EventListener> _eventListener;
|
|
2356
|
+
SolidNativeFabricFatalErrorHandler _fatalErrorHandler;
|
|
2357
|
+
SolidNativeFabricStatusBarStyleHandler _statusBarStyleHandler;
|
|
2358
|
+
SolidNativeFabricStatusBarHiddenHandler _statusBarHiddenHandler;
|
|
2359
|
+
}
|
|
2360
|
+
|
|
2361
|
+
- (instancetype)init {
|
|
2362
|
+
if (self = [super init]) {
|
|
2363
|
+
_lock = [[NSLock alloc] init];
|
|
2364
|
+
_routing = std::make_shared<EventRoutingState>();
|
|
2365
|
+
}
|
|
2366
|
+
return self;
|
|
2367
|
+
}
|
|
2368
|
+
|
|
2369
|
+
- (void)installIntoRuntime:(Runtime &)runtime {
|
|
2370
|
+
const auto existing = runtime.global().getProperty(runtime, GlobalBindingName);
|
|
2371
|
+
if (!existing.isUndefined()) {
|
|
2372
|
+
throw JSError(
|
|
2373
|
+
runtime,
|
|
2374
|
+
"Solid Native cannot replace an existing __solidNativeHost binding.");
|
|
2375
|
+
}
|
|
2376
|
+
|
|
2377
|
+
installEmptySurfaceStopCompatibility(runtime);
|
|
2378
|
+
|
|
2379
|
+
std::shared_ptr<EventRoutingState> previousRouting;
|
|
2380
|
+
std::shared_ptr<RuntimeState> previousRuntimeState;
|
|
2381
|
+
std::shared_ptr<facebook::react::EventListener> previousEventListener;
|
|
2382
|
+
SolidNativeFabricSurface *previousEventSurface = nil;
|
|
2383
|
+
SolidNativeFabricSurface *surface = nil;
|
|
2384
|
+
BOOL replacingRuntime = NO;
|
|
2385
|
+
BOOL hasStatusBarControl = NO;
|
|
2386
|
+
uint64_t runtimeGeneration = 0;
|
|
2387
|
+
[_lock lock];
|
|
2388
|
+
replacingRuntime = _runtimeInstalled;
|
|
2389
|
+
_runtimeInstalled = YES;
|
|
2390
|
+
runtimeGeneration = ++_runtimeGeneration;
|
|
2391
|
+
hasStatusBarControl =
|
|
2392
|
+
_statusBarStyleHandler != nil && _statusBarHiddenHandler != nil;
|
|
2393
|
+
previousRouting = _routing;
|
|
2394
|
+
previousRuntimeState = _runtimeState.lock();
|
|
2395
|
+
previousEventListener = _eventListener;
|
|
2396
|
+
previousEventSurface = _eventSurface;
|
|
2397
|
+
surface = _surface;
|
|
2398
|
+
_routing = std::make_shared<EventRoutingState>();
|
|
2399
|
+
auto routing = _routing;
|
|
2400
|
+
_eventSurface = nil;
|
|
2401
|
+
_eventListener.reset();
|
|
2402
|
+
_runtimeScheduler.reset();
|
|
2403
|
+
_runtimeState.reset();
|
|
2404
|
+
[_lock unlock];
|
|
2405
|
+
previousRouting->active.store(false, std::memory_order_release);
|
|
2406
|
+
previousRouting->handlerInstalled.store(false, std::memory_order_release);
|
|
2407
|
+
if (previousRuntimeState) previousRuntimeState->cancelAllUIWorklets();
|
|
2408
|
+
if (previousEventSurface != nil && previousEventListener) {
|
|
2409
|
+
[previousEventSurface removeSolidNativeEventListener:previousEventListener];
|
|
2410
|
+
}
|
|
2411
|
+
if (replacingRuntime && previousRuntimeState && surface != nil) {
|
|
2412
|
+
[surface resetForJSRuntimeReload];
|
|
2413
|
+
}
|
|
2414
|
+
|
|
2415
|
+
SolidNativeFabricJSIBinding *binding = self;
|
|
2416
|
+
Object host(runtime);
|
|
2417
|
+
Object runtimeStorage(runtime);
|
|
2418
|
+
runtimeStorage.setProperty(runtime, "instances", Object(runtime));
|
|
2419
|
+
runtimeStorage.setProperty(runtime, "resources", Object(runtime));
|
|
2420
|
+
auto runtimeState =
|
|
2421
|
+
std::make_shared<RuntimeState>(runtime, runtimeStorage, routing);
|
|
2422
|
+
__weak SolidNativeFabricJSIBinding *weakBinding = self;
|
|
2423
|
+
runtimeState->setRuntimeRetirementHandler(
|
|
2424
|
+
[weakBinding, runtimeGeneration] {
|
|
2425
|
+
SolidNativeFabricJSIBinding *binding = weakBinding;
|
|
2426
|
+
if (binding != nil) {
|
|
2427
|
+
[binding retireRuntimeGeneration:runtimeGeneration];
|
|
2428
|
+
}
|
|
2429
|
+
});
|
|
2430
|
+
std::weak_ptr<RuntimeState> weakRuntimeState = runtimeState;
|
|
2431
|
+
host.setNativeState(runtime, runtimeState);
|
|
2432
|
+
Object runtimeStorageDescriptor(runtime);
|
|
2433
|
+
runtimeStorageDescriptor.setProperty(runtime, "value", runtimeStorage);
|
|
2434
|
+
runtimeStorageDescriptor.setProperty(runtime, "writable", false);
|
|
2435
|
+
runtimeStorageDescriptor.setProperty(runtime, "configurable", false);
|
|
2436
|
+
runtimeStorageDescriptor.setProperty(runtime, "enumerable", false);
|
|
2437
|
+
host.setProperty(runtime, "contractVersion", HostContractVersion);
|
|
2438
|
+
host.setProperty(runtime, "backend", "react-native-fabric");
|
|
2439
|
+
host.setProperty(runtime, "backendVersion", ReactNativeVersion);
|
|
2440
|
+
host.setProperty(runtime, "platform", "ios");
|
|
2441
|
+
|
|
2442
|
+
auto schedulerBinding =
|
|
2443
|
+
facebook::react::RuntimeSchedulerBinding::getBinding(runtime);
|
|
2444
|
+
if (!schedulerBinding) {
|
|
2445
|
+
throw JSError(
|
|
2446
|
+
runtime, "React Native did not install its RuntimeScheduler binding.");
|
|
2447
|
+
}
|
|
2448
|
+
auto runtimeScheduler = schedulerBinding->getRuntimeScheduler();
|
|
2449
|
+
if (!runtimeScheduler) {
|
|
2450
|
+
throw JSError(
|
|
2451
|
+
runtime, "React Native did not expose its RuntimeScheduler.");
|
|
2452
|
+
}
|
|
2453
|
+
auto eventListener = createEventListener(
|
|
2454
|
+
routing, runtimeScheduler, weakRuntimeState);
|
|
2455
|
+
[_lock lock];
|
|
2456
|
+
_runtimeState = weakRuntimeState;
|
|
2457
|
+
_runtimeScheduler = runtimeScheduler;
|
|
2458
|
+
_eventListener = eventListener;
|
|
2459
|
+
auto surfaceForEvents = _surface;
|
|
2460
|
+
if (surfaceForEvents != nil) _eventSurface = surfaceForEvents;
|
|
2461
|
+
[_lock unlock];
|
|
2462
|
+
if (surfaceForEvents != nil) {
|
|
2463
|
+
[surfaceForEvents addSolidNativeEventListener:eventListener];
|
|
2464
|
+
}
|
|
2465
|
+
|
|
2466
|
+
auto objectConstructor =
|
|
2467
|
+
runtime.global().getPropertyAsObject(runtime, "Object");
|
|
2468
|
+
auto defineProperty =
|
|
2469
|
+
objectConstructor.getPropertyAsFunction(runtime, "defineProperty");
|
|
2470
|
+
defineProperty.call(
|
|
2471
|
+
runtime,
|
|
2472
|
+
host,
|
|
2473
|
+
String::createFromAscii(runtime, "__runtimeStorage"),
|
|
2474
|
+
runtimeStorageDescriptor);
|
|
2475
|
+
|
|
2476
|
+
host.setProperty(
|
|
2477
|
+
runtime,
|
|
2478
|
+
"getSurfaceInfo",
|
|
2479
|
+
Function::createFromHostFunction(
|
|
2480
|
+
runtime,
|
|
2481
|
+
PropNameID::forAscii(runtime, "getSurfaceInfo"),
|
|
2482
|
+
0,
|
|
2483
|
+
[binding, weakRuntimeState, routing](Runtime &runtime,
|
|
2484
|
+
const Value &,
|
|
2485
|
+
const Value *,
|
|
2486
|
+
size_t count) -> Value {
|
|
2487
|
+
if (count != 0) {
|
|
2488
|
+
throw JSError(
|
|
2489
|
+
runtime, "getSurfaceInfo does not accept arguments.");
|
|
2490
|
+
}
|
|
2491
|
+
auto runtimeState = weakRuntimeState.lock();
|
|
2492
|
+
if (!runtimeState) {
|
|
2493
|
+
throw JSError(runtime, "The Solid Native runtime has stopped.");
|
|
2494
|
+
}
|
|
2495
|
+
auto snapshot = [binding surfaceSnapshot];
|
|
2496
|
+
routing->surface.store(
|
|
2497
|
+
[snapshot[@"surface"] longLongValue],
|
|
2498
|
+
std::memory_order_release);
|
|
2499
|
+
routing->sequence.store(
|
|
2500
|
+
[snapshot[@"sequence"] longLongValue],
|
|
2501
|
+
std::memory_order_release);
|
|
2502
|
+
const auto uiWorkletActivity =
|
|
2503
|
+
runtimeState->uiWorkletActivityCounts();
|
|
2504
|
+
return surfaceInfoObject(
|
|
2505
|
+
runtime,
|
|
2506
|
+
snapshot,
|
|
2507
|
+
runtimeState->retainedNodeCount(),
|
|
2508
|
+
runtimeState->retainedResourceCount(),
|
|
2509
|
+
uiWorkletActivity.first,
|
|
2510
|
+
uiWorkletActivity.second);
|
|
2511
|
+
}));
|
|
2512
|
+
host.setProperty(
|
|
2513
|
+
runtime,
|
|
2514
|
+
"setEventHandler",
|
|
2515
|
+
Function::createFromHostFunction(
|
|
2516
|
+
runtime,
|
|
2517
|
+
PropNameID::forAscii(runtime, "setEventHandler"),
|
|
2518
|
+
1,
|
|
2519
|
+
[weakRuntimeState](Runtime &runtime,
|
|
2520
|
+
const Value &,
|
|
2521
|
+
const Value *arguments,
|
|
2522
|
+
size_t count) -> Value {
|
|
2523
|
+
if (count != 1 ||
|
|
2524
|
+
(!arguments[0].isNull() &&
|
|
2525
|
+
(!arguments[0].isObject() ||
|
|
2526
|
+
!arguments[0].getObject(runtime).isFunction(runtime)))) {
|
|
2527
|
+
throw JSError(
|
|
2528
|
+
runtime,
|
|
2529
|
+
"setEventHandler requires exactly one function or null.");
|
|
2530
|
+
}
|
|
2531
|
+
auto runtimeState = weakRuntimeState.lock();
|
|
2532
|
+
if (!runtimeState) {
|
|
2533
|
+
throw JSError(runtime, "The Solid Native runtime has stopped.");
|
|
2534
|
+
}
|
|
2535
|
+
if (arguments[0].isNull()) {
|
|
2536
|
+
const Value empty;
|
|
2537
|
+
runtimeState->setEventHandler(runtime, empty);
|
|
2538
|
+
} else {
|
|
2539
|
+
runtimeState->setEventHandler(runtime, arguments[0]);
|
|
2540
|
+
}
|
|
2541
|
+
return Value();
|
|
2542
|
+
}));
|
|
2543
|
+
host.setProperty(
|
|
2544
|
+
runtime,
|
|
2545
|
+
"setCommitLifecycleHandler",
|
|
2546
|
+
Function::createFromHostFunction(
|
|
2547
|
+
runtime,
|
|
2548
|
+
PropNameID::forAscii(runtime, "setCommitLifecycleHandler"),
|
|
2549
|
+
1,
|
|
2550
|
+
[weakRuntimeState](Runtime &runtime,
|
|
2551
|
+
const Value &,
|
|
2552
|
+
const Value *arguments,
|
|
2553
|
+
size_t count) -> Value {
|
|
2554
|
+
if (count != 1 ||
|
|
2555
|
+
(!arguments[0].isNull() &&
|
|
2556
|
+
(!arguments[0].isObject() ||
|
|
2557
|
+
!arguments[0].getObject(runtime).isFunction(runtime)))) {
|
|
2558
|
+
throw JSError(
|
|
2559
|
+
runtime,
|
|
2560
|
+
"setCommitLifecycleHandler requires exactly one function "
|
|
2561
|
+
"or null.");
|
|
2562
|
+
}
|
|
2563
|
+
auto runtimeState = weakRuntimeState.lock();
|
|
2564
|
+
if (!runtimeState) {
|
|
2565
|
+
throw JSError(runtime, "The Solid Native runtime has stopped.");
|
|
2566
|
+
}
|
|
2567
|
+
if (arguments[0].isNull()) {
|
|
2568
|
+
const Value empty;
|
|
2569
|
+
runtimeState->setCommitLifecycleHandler(runtime, empty);
|
|
2570
|
+
} else {
|
|
2571
|
+
runtimeState->setCommitLifecycleHandler(runtime, arguments[0]);
|
|
2572
|
+
}
|
|
2573
|
+
return Value();
|
|
2574
|
+
}));
|
|
2575
|
+
host.setProperty(
|
|
2576
|
+
runtime,
|
|
2577
|
+
"measure",
|
|
2578
|
+
Function::createFromHostFunction(
|
|
2579
|
+
runtime,
|
|
2580
|
+
PropNameID::forAscii(runtime, "measure"),
|
|
2581
|
+
2,
|
|
2582
|
+
[binding](Runtime &runtime,
|
|
2583
|
+
const Value &,
|
|
2584
|
+
const Value *arguments,
|
|
2585
|
+
size_t count) -> Value {
|
|
2586
|
+
if (count < 1 || count > 2) {
|
|
2587
|
+
throw JSError(
|
|
2588
|
+
runtime,
|
|
2589
|
+
"measure requires a node and an optional commit sequence.");
|
|
2590
|
+
}
|
|
2591
|
+
const auto node = requireSafeInteger(
|
|
2592
|
+
runtime, arguments[0], "The measured node", true);
|
|
2593
|
+
const auto afterSequence = count == 2
|
|
2594
|
+
? requireSafeInteger(
|
|
2595
|
+
runtime,
|
|
2596
|
+
arguments[1],
|
|
2597
|
+
"The measurement sequence",
|
|
2598
|
+
false)
|
|
2599
|
+
: 0;
|
|
2600
|
+
if (afterSequence < 0) {
|
|
2601
|
+
throw JSError(
|
|
2602
|
+
runtime,
|
|
2603
|
+
"The measurement sequence must be non-negative.");
|
|
2604
|
+
}
|
|
2605
|
+
NSError *error = nil;
|
|
2606
|
+
auto result = [binding measureNode:node
|
|
2607
|
+
afterSequence:afterSequence
|
|
2608
|
+
error:&error];
|
|
2609
|
+
if (result == nil) {
|
|
2610
|
+
throw JSError(runtime, utf8(error.localizedDescription));
|
|
2611
|
+
}
|
|
2612
|
+
return measurementObject(runtime, result);
|
|
2613
|
+
}));
|
|
2614
|
+
host.setProperty(
|
|
2615
|
+
runtime,
|
|
2616
|
+
"retainNativeResource",
|
|
2617
|
+
Function::createFromHostFunction(
|
|
2618
|
+
runtime,
|
|
2619
|
+
PropNameID::forAscii(runtime, "retainNativeResource"),
|
|
2620
|
+
2,
|
|
2621
|
+
[weakRuntimeState](Runtime &runtime,
|
|
2622
|
+
const Value &,
|
|
2623
|
+
const Value *arguments,
|
|
2624
|
+
size_t count) -> Value {
|
|
2625
|
+
if (count != 2 || !arguments[1].isObject() ||
|
|
2626
|
+
arguments[1].getObject(runtime).isFunction(runtime) ||
|
|
2627
|
+
arguments[1].getObject(runtime).isArray(runtime)) {
|
|
2628
|
+
throw JSError(
|
|
2629
|
+
runtime,
|
|
2630
|
+
"retainNativeResource requires a kind and one non-array "
|
|
2631
|
+
"object.");
|
|
2632
|
+
}
|
|
2633
|
+
auto runtimeState = weakRuntimeState.lock();
|
|
2634
|
+
if (!runtimeState) {
|
|
2635
|
+
throw JSError(runtime, "The Solid Native runtime has stopped.");
|
|
2636
|
+
}
|
|
2637
|
+
const auto kind = requireResourceKind(runtime, arguments[0]);
|
|
2638
|
+
return Value(static_cast<double>(runtimeState->retainResource(
|
|
2639
|
+
runtime, kind, arguments[1])));
|
|
2640
|
+
}));
|
|
2641
|
+
host.setProperty(
|
|
2642
|
+
runtime,
|
|
2643
|
+
"releaseNativeResource",
|
|
2644
|
+
Function::createFromHostFunction(
|
|
2645
|
+
runtime,
|
|
2646
|
+
PropNameID::forAscii(runtime, "releaseNativeResource"),
|
|
2647
|
+
1,
|
|
2648
|
+
[weakRuntimeState](Runtime &runtime,
|
|
2649
|
+
const Value &,
|
|
2650
|
+
const Value *arguments,
|
|
2651
|
+
size_t count) -> Value {
|
|
2652
|
+
if (count != 1) {
|
|
2653
|
+
throw JSError(
|
|
2654
|
+
runtime,
|
|
2655
|
+
"releaseNativeResource requires exactly one handle.");
|
|
2656
|
+
}
|
|
2657
|
+
auto runtimeState = weakRuntimeState.lock();
|
|
2658
|
+
if (!runtimeState) {
|
|
2659
|
+
throw JSError(runtime, "The Solid Native runtime has stopped.");
|
|
2660
|
+
}
|
|
2661
|
+
const auto handle = requireSafeInteger(
|
|
2662
|
+
runtime, arguments[0], "The native resource handle", true);
|
|
2663
|
+
runtimeState->releaseResource(runtime, handle);
|
|
2664
|
+
return Value();
|
|
2665
|
+
}));
|
|
2666
|
+
host.setProperty(
|
|
2667
|
+
runtime,
|
|
2668
|
+
"installUIWorklet",
|
|
2669
|
+
Function::createFromHostFunction(
|
|
2670
|
+
runtime,
|
|
2671
|
+
PropNameID::forAscii(runtime, "installUIWorklet"),
|
|
2672
|
+
2,
|
|
2673
|
+
[binding, weakRuntimeState](Runtime &runtime,
|
|
2674
|
+
const Value &,
|
|
2675
|
+
const Value *arguments,
|
|
2676
|
+
size_t count) -> Value {
|
|
2677
|
+
if (count != 2 || !arguments[1].isObject()) {
|
|
2678
|
+
throw JSError(
|
|
2679
|
+
runtime,
|
|
2680
|
+
"installUIWorklet requires a target node and one graph "
|
|
2681
|
+
"object.");
|
|
2682
|
+
}
|
|
2683
|
+
auto state = weakRuntimeState.lock();
|
|
2684
|
+
if (!state) {
|
|
2685
|
+
throw JSError(runtime, "The Solid Native runtime has stopped.");
|
|
2686
|
+
}
|
|
2687
|
+
const auto target = requireSafeInteger(
|
|
2688
|
+
runtime,
|
|
2689
|
+
arguments[0],
|
|
2690
|
+
"The native UI worklet target",
|
|
2691
|
+
true);
|
|
2692
|
+
try {
|
|
2693
|
+
auto worklet =
|
|
2694
|
+
state->installUIWorklet(runtime, target, arguments[1]);
|
|
2695
|
+
[binding scheduleUIWorkletFrame:worklet];
|
|
2696
|
+
return Value(static_cast<double>(worklet->handle));
|
|
2697
|
+
} catch (const JSError &) {
|
|
2698
|
+
throw;
|
|
2699
|
+
} catch (const std::exception &error) {
|
|
2700
|
+
throw JSError(runtime, error.what());
|
|
2701
|
+
}
|
|
2702
|
+
}));
|
|
2703
|
+
host.setProperty(
|
|
2704
|
+
runtime,
|
|
2705
|
+
"updateUIWorkletInputs",
|
|
2706
|
+
Function::createFromHostFunction(
|
|
2707
|
+
runtime,
|
|
2708
|
+
PropNameID::forAscii(runtime, "updateUIWorkletInputs"),
|
|
2709
|
+
3,
|
|
2710
|
+
[binding, weakRuntimeState](Runtime &runtime,
|
|
2711
|
+
const Value &,
|
|
2712
|
+
const Value *arguments,
|
|
2713
|
+
size_t count) -> Value {
|
|
2714
|
+
if (count != 3 || !arguments[1].isObject() ||
|
|
2715
|
+
!arguments[1].getObject(runtime).isArray(runtime) ||
|
|
2716
|
+
!arguments[2].isNumber()) {
|
|
2717
|
+
throw JSError(
|
|
2718
|
+
runtime,
|
|
2719
|
+
"updateUIWorkletInputs requires a handle, input array, "
|
|
2720
|
+
"and timestamp.");
|
|
2721
|
+
}
|
|
2722
|
+
auto state = weakRuntimeState.lock();
|
|
2723
|
+
if (!state) {
|
|
2724
|
+
throw JSError(runtime, "The Solid Native runtime has stopped.");
|
|
2725
|
+
}
|
|
2726
|
+
const auto handle = requireSafeInteger(
|
|
2727
|
+
runtime,
|
|
2728
|
+
arguments[0],
|
|
2729
|
+
"The native UI worklet handle",
|
|
2730
|
+
true);
|
|
2731
|
+
auto array = arguments[1].getObject(runtime).getArray(runtime);
|
|
2732
|
+
if (array.size(runtime) >
|
|
2733
|
+
solid_native::worklets::MaximumUIWorkletInputs) {
|
|
2734
|
+
throw JSError(
|
|
2735
|
+
runtime,
|
|
2736
|
+
"The native UI worklet input vector is too large.");
|
|
2737
|
+
}
|
|
2738
|
+
std::vector<double> inputs;
|
|
2739
|
+
inputs.reserve(array.size(runtime));
|
|
2740
|
+
for (size_t index = 0; index < array.size(runtime); ++index) {
|
|
2741
|
+
auto value = array.getValueAtIndex(runtime, index);
|
|
2742
|
+
if (!value.isNumber() || !std::isfinite(value.getNumber())) {
|
|
2743
|
+
throw JSError(
|
|
2744
|
+
runtime,
|
|
2745
|
+
"Native UI worklet inputs must be finite numbers.");
|
|
2746
|
+
}
|
|
2747
|
+
inputs.push_back(value.getNumber());
|
|
2748
|
+
}
|
|
2749
|
+
try {
|
|
2750
|
+
auto worklet = state->updateUIWorklet(
|
|
2751
|
+
handle,
|
|
2752
|
+
std::move(inputs),
|
|
2753
|
+
arguments[2].getNumber());
|
|
2754
|
+
[binding scheduleUIWorkletFrame:worklet];
|
|
2755
|
+
return Value();
|
|
2756
|
+
} catch (const std::exception &error) {
|
|
2757
|
+
throw JSError(runtime, error.what());
|
|
2758
|
+
}
|
|
2759
|
+
}));
|
|
2760
|
+
host.setProperty(
|
|
2761
|
+
runtime,
|
|
2762
|
+
"animateUIWorkletInputs",
|
|
2763
|
+
Function::createFromHostFunction(
|
|
2764
|
+
runtime,
|
|
2765
|
+
PropNameID::forAscii(runtime, "animateUIWorkletInputs"),
|
|
2766
|
+
4,
|
|
2767
|
+
[binding, weakRuntimeState](Runtime &runtime,
|
|
2768
|
+
const Value &,
|
|
2769
|
+
const Value *arguments,
|
|
2770
|
+
size_t count) -> Value {
|
|
2771
|
+
if (count != 4 || !arguments[1].isObject() ||
|
|
2772
|
+
!arguments[1].getObject(runtime).isArray(runtime) ||
|
|
2773
|
+
!arguments[2].isNumber() || !arguments[3].isObject()) {
|
|
2774
|
+
throw JSError(
|
|
2775
|
+
runtime,
|
|
2776
|
+
"animateUIWorkletInputs requires a handle, input array, "
|
|
2777
|
+
"timestamp, and timing object.");
|
|
2778
|
+
}
|
|
2779
|
+
auto state = weakRuntimeState.lock();
|
|
2780
|
+
if (!state) {
|
|
2781
|
+
throw JSError(runtime, "The Solid Native runtime has stopped.");
|
|
2782
|
+
}
|
|
2783
|
+
const auto handle = requireSafeInteger(
|
|
2784
|
+
runtime,
|
|
2785
|
+
arguments[0],
|
|
2786
|
+
"The native UI worklet handle",
|
|
2787
|
+
true);
|
|
2788
|
+
auto array = arguments[1].getObject(runtime).getArray(runtime);
|
|
2789
|
+
if (array.size(runtime) >
|
|
2790
|
+
solid_native::worklets::MaximumUIWorkletInputs) {
|
|
2791
|
+
throw JSError(
|
|
2792
|
+
runtime,
|
|
2793
|
+
"The native UI worklet input vector is too large.");
|
|
2794
|
+
}
|
|
2795
|
+
std::vector<double> inputs;
|
|
2796
|
+
inputs.reserve(array.size(runtime));
|
|
2797
|
+
for (size_t index = 0; index < array.size(runtime); ++index) {
|
|
2798
|
+
auto value = array.getValueAtIndex(runtime, index);
|
|
2799
|
+
if (!value.isNumber() || !std::isfinite(value.getNumber())) {
|
|
2800
|
+
throw JSError(
|
|
2801
|
+
runtime,
|
|
2802
|
+
"Native UI worklet inputs must be finite numbers.");
|
|
2803
|
+
}
|
|
2804
|
+
inputs.push_back(value.getNumber());
|
|
2805
|
+
}
|
|
2806
|
+
try {
|
|
2807
|
+
auto worklet = state->animateUIWorklet(
|
|
2808
|
+
runtime,
|
|
2809
|
+
handle,
|
|
2810
|
+
std::move(inputs),
|
|
2811
|
+
arguments[2].getNumber(),
|
|
2812
|
+
arguments[3]);
|
|
2813
|
+
[binding scheduleUIWorkletFrame:worklet];
|
|
2814
|
+
return Value();
|
|
2815
|
+
} catch (const JSError &) {
|
|
2816
|
+
throw;
|
|
2817
|
+
} catch (const std::exception &error) {
|
|
2818
|
+
throw JSError(runtime, error.what());
|
|
2819
|
+
}
|
|
2820
|
+
}));
|
|
2821
|
+
host.setProperty(
|
|
2822
|
+
runtime,
|
|
2823
|
+
"animateUIWorkletKeyframes",
|
|
2824
|
+
Function::createFromHostFunction(
|
|
2825
|
+
runtime,
|
|
2826
|
+
PropNameID::forAscii(runtime, "animateUIWorkletKeyframes"),
|
|
2827
|
+
3,
|
|
2828
|
+
[binding, weakRuntimeState](Runtime &runtime,
|
|
2829
|
+
const Value &,
|
|
2830
|
+
const Value *arguments,
|
|
2831
|
+
size_t count) -> Value {
|
|
2832
|
+
if (count != 3 || !arguments[1].isObject() ||
|
|
2833
|
+
!arguments[1].getObject(runtime).isArray(runtime) ||
|
|
2834
|
+
!arguments[2].isNumber()) {
|
|
2835
|
+
throw JSError(
|
|
2836
|
+
runtime,
|
|
2837
|
+
"animateUIWorkletKeyframes requires a handle, keyframe "
|
|
2838
|
+
"array, and timestamp.");
|
|
2839
|
+
}
|
|
2840
|
+
auto state = weakRuntimeState.lock();
|
|
2841
|
+
if (!state) {
|
|
2842
|
+
throw JSError(runtime, "The Solid Native runtime has stopped.");
|
|
2843
|
+
}
|
|
2844
|
+
const auto handle = requireSafeInteger(
|
|
2845
|
+
runtime,
|
|
2846
|
+
arguments[0],
|
|
2847
|
+
"The native UI worklet handle",
|
|
2848
|
+
true);
|
|
2849
|
+
try {
|
|
2850
|
+
auto worklet = state->animateUIWorkletKeyframes(
|
|
2851
|
+
runtime,
|
|
2852
|
+
handle,
|
|
2853
|
+
arguments[1],
|
|
2854
|
+
arguments[2].getNumber());
|
|
2855
|
+
[binding scheduleUIWorkletFrame:worklet];
|
|
2856
|
+
return Value();
|
|
2857
|
+
} catch (const JSError &) {
|
|
2858
|
+
throw;
|
|
2859
|
+
} catch (const std::exception &error) {
|
|
2860
|
+
throw JSError(runtime, error.what());
|
|
2861
|
+
}
|
|
2862
|
+
}));
|
|
2863
|
+
host.setProperty(
|
|
2864
|
+
runtime,
|
|
2865
|
+
"springUIWorkletInputs",
|
|
2866
|
+
Function::createFromHostFunction(
|
|
2867
|
+
runtime,
|
|
2868
|
+
PropNameID::forAscii(runtime, "springUIWorkletInputs"),
|
|
2869
|
+
4,
|
|
2870
|
+
[binding, weakRuntimeState](Runtime &runtime,
|
|
2871
|
+
const Value &,
|
|
2872
|
+
const Value *arguments,
|
|
2873
|
+
size_t count) -> Value {
|
|
2874
|
+
if (count != 4 || !arguments[1].isObject() ||
|
|
2875
|
+
!arguments[1].getObject(runtime).isArray(runtime) ||
|
|
2876
|
+
!arguments[2].isNumber() || !arguments[3].isObject()) {
|
|
2877
|
+
throw JSError(
|
|
2878
|
+
runtime,
|
|
2879
|
+
"springUIWorkletInputs requires a handle, input array, "
|
|
2880
|
+
"timestamp, and spring object.");
|
|
2881
|
+
}
|
|
2882
|
+
auto state = weakRuntimeState.lock();
|
|
2883
|
+
if (!state) {
|
|
2884
|
+
throw JSError(runtime, "The Solid Native runtime has stopped.");
|
|
2885
|
+
}
|
|
2886
|
+
const auto handle = requireSafeInteger(
|
|
2887
|
+
runtime,
|
|
2888
|
+
arguments[0],
|
|
2889
|
+
"The native UI worklet handle",
|
|
2890
|
+
true);
|
|
2891
|
+
auto array = arguments[1].getObject(runtime).getArray(runtime);
|
|
2892
|
+
if (array.size(runtime) >
|
|
2893
|
+
solid_native::worklets::MaximumUIWorkletInputs) {
|
|
2894
|
+
throw JSError(
|
|
2895
|
+
runtime,
|
|
2896
|
+
"The native UI worklet input vector is too large.");
|
|
2897
|
+
}
|
|
2898
|
+
std::vector<double> inputs;
|
|
2899
|
+
inputs.reserve(array.size(runtime));
|
|
2900
|
+
for (size_t index = 0; index < array.size(runtime); ++index) {
|
|
2901
|
+
auto value = array.getValueAtIndex(runtime, index);
|
|
2902
|
+
if (!value.isNumber() || !std::isfinite(value.getNumber())) {
|
|
2903
|
+
throw JSError(
|
|
2904
|
+
runtime,
|
|
2905
|
+
"Native UI worklet inputs must be finite numbers.");
|
|
2906
|
+
}
|
|
2907
|
+
inputs.push_back(value.getNumber());
|
|
2908
|
+
}
|
|
2909
|
+
try {
|
|
2910
|
+
auto worklet = state->springUIWorklet(
|
|
2911
|
+
runtime,
|
|
2912
|
+
handle,
|
|
2913
|
+
std::move(inputs),
|
|
2914
|
+
arguments[2].getNumber(),
|
|
2915
|
+
arguments[3]);
|
|
2916
|
+
[binding scheduleUIWorkletFrame:worklet];
|
|
2917
|
+
return Value();
|
|
2918
|
+
} catch (const JSError &) {
|
|
2919
|
+
throw;
|
|
2920
|
+
} catch (const std::exception &error) {
|
|
2921
|
+
throw JSError(runtime, error.what());
|
|
2922
|
+
}
|
|
2923
|
+
}));
|
|
2924
|
+
host.setProperty(
|
|
2925
|
+
runtime,
|
|
2926
|
+
"decayUIWorkletInputs",
|
|
2927
|
+
Function::createFromHostFunction(
|
|
2928
|
+
runtime,
|
|
2929
|
+
PropNameID::forAscii(runtime, "decayUIWorkletInputs"),
|
|
2930
|
+
4,
|
|
2931
|
+
[binding, weakRuntimeState](Runtime &runtime,
|
|
2932
|
+
const Value &,
|
|
2933
|
+
const Value *arguments,
|
|
2934
|
+
size_t count) -> Value {
|
|
2935
|
+
if (count != 4 || !arguments[1].isObject() ||
|
|
2936
|
+
!arguments[1].getObject(runtime).isArray(runtime) ||
|
|
2937
|
+
!arguments[2].isNumber() || !arguments[3].isObject()) {
|
|
2938
|
+
throw JSError(
|
|
2939
|
+
runtime,
|
|
2940
|
+
"decayUIWorkletInputs requires a handle, velocity array, "
|
|
2941
|
+
"timestamp, and decay object.");
|
|
2942
|
+
}
|
|
2943
|
+
auto state = weakRuntimeState.lock();
|
|
2944
|
+
if (!state) {
|
|
2945
|
+
throw JSError(runtime, "The Solid Native runtime has stopped.");
|
|
2946
|
+
}
|
|
2947
|
+
const auto handle = requireSafeInteger(
|
|
2948
|
+
runtime,
|
|
2949
|
+
arguments[0],
|
|
2950
|
+
"The native UI worklet handle",
|
|
2951
|
+
true);
|
|
2952
|
+
auto array = arguments[1].getObject(runtime).getArray(runtime);
|
|
2953
|
+
if (array.size(runtime) >
|
|
2954
|
+
solid_native::worklets::MaximumUIWorkletInputs) {
|
|
2955
|
+
throw JSError(
|
|
2956
|
+
runtime,
|
|
2957
|
+
"The native UI worklet velocity vector is too large.");
|
|
2958
|
+
}
|
|
2959
|
+
std::vector<double> velocities;
|
|
2960
|
+
velocities.reserve(array.size(runtime));
|
|
2961
|
+
for (size_t index = 0; index < array.size(runtime); ++index) {
|
|
2962
|
+
auto value = array.getValueAtIndex(runtime, index);
|
|
2963
|
+
if (!value.isNumber() || !std::isfinite(value.getNumber())) {
|
|
2964
|
+
throw JSError(
|
|
2965
|
+
runtime,
|
|
2966
|
+
"Native UI worklet velocities must be finite numbers.");
|
|
2967
|
+
}
|
|
2968
|
+
velocities.push_back(value.getNumber());
|
|
2969
|
+
}
|
|
2970
|
+
try {
|
|
2971
|
+
auto worklet = state->decayUIWorklet(
|
|
2972
|
+
runtime,
|
|
2973
|
+
handle,
|
|
2974
|
+
std::move(velocities),
|
|
2975
|
+
arguments[2].getNumber(),
|
|
2976
|
+
arguments[3]);
|
|
2977
|
+
[binding scheduleUIWorkletFrame:worklet];
|
|
2978
|
+
return Value();
|
|
2979
|
+
} catch (const JSError &) {
|
|
2980
|
+
throw;
|
|
2981
|
+
} catch (const std::exception &error) {
|
|
2982
|
+
throw JSError(runtime, error.what());
|
|
2983
|
+
}
|
|
2984
|
+
}));
|
|
2985
|
+
host.setProperty(
|
|
2986
|
+
runtime,
|
|
2987
|
+
"cancelUIWorkletAnimation",
|
|
2988
|
+
Function::createFromHostFunction(
|
|
2989
|
+
runtime,
|
|
2990
|
+
PropNameID::forAscii(runtime, "cancelUIWorkletAnimation"),
|
|
2991
|
+
2,
|
|
2992
|
+
[weakRuntimeState](Runtime &runtime,
|
|
2993
|
+
const Value &,
|
|
2994
|
+
const Value *arguments,
|
|
2995
|
+
size_t count) -> Value {
|
|
2996
|
+
if (count != 2 || !arguments[1].isNumber()) {
|
|
2997
|
+
throw JSError(
|
|
2998
|
+
runtime,
|
|
2999
|
+
"cancelUIWorkletAnimation requires a handle and timestamp.");
|
|
3000
|
+
}
|
|
3001
|
+
auto state = weakRuntimeState.lock();
|
|
3002
|
+
if (!state) {
|
|
3003
|
+
throw JSError(runtime, "The Solid Native runtime has stopped.");
|
|
3004
|
+
}
|
|
3005
|
+
const auto handle = requireSafeInteger(
|
|
3006
|
+
runtime,
|
|
3007
|
+
arguments[0],
|
|
3008
|
+
"The native UI worklet handle",
|
|
3009
|
+
true);
|
|
3010
|
+
try {
|
|
3011
|
+
const auto inputs = state->cancelUIWorkletAnimation(
|
|
3012
|
+
handle, arguments[1].getNumber());
|
|
3013
|
+
Array result(runtime, inputs.size());
|
|
3014
|
+
for (size_t index = 0; index < inputs.size(); ++index) {
|
|
3015
|
+
result.setValueAtIndex(runtime, index, inputs.at(index));
|
|
3016
|
+
}
|
|
3017
|
+
return result;
|
|
3018
|
+
} catch (const std::exception &error) {
|
|
3019
|
+
throw JSError(runtime, error.what());
|
|
3020
|
+
}
|
|
3021
|
+
}));
|
|
3022
|
+
host.setProperty(
|
|
3023
|
+
runtime,
|
|
3024
|
+
"attachUIWorkletPanGesture",
|
|
3025
|
+
Function::createFromHostFunction(
|
|
3026
|
+
runtime,
|
|
3027
|
+
PropNameID::forAscii(runtime, "attachUIWorkletPanGesture"),
|
|
3028
|
+
2,
|
|
3029
|
+
[binding, weakRuntimeState](Runtime &runtime,
|
|
3030
|
+
const Value &,
|
|
3031
|
+
const Value *arguments,
|
|
3032
|
+
size_t count) -> Value {
|
|
3033
|
+
if (count != 2 || !arguments[1].isObject()) {
|
|
3034
|
+
throw JSError(
|
|
3035
|
+
runtime,
|
|
3036
|
+
"attachUIWorkletPanGesture requires a handle and one pan object.");
|
|
3037
|
+
}
|
|
3038
|
+
auto state = weakRuntimeState.lock();
|
|
3039
|
+
if (!state) {
|
|
3040
|
+
throw JSError(runtime, "The Solid Native runtime has stopped.");
|
|
3041
|
+
}
|
|
3042
|
+
const auto handle = requireSafeInteger(
|
|
3043
|
+
runtime,
|
|
3044
|
+
arguments[0],
|
|
3045
|
+
"The native UI worklet handle",
|
|
3046
|
+
true);
|
|
3047
|
+
bool attached = false;
|
|
3048
|
+
try {
|
|
3049
|
+
auto worklet =
|
|
3050
|
+
state->attachUIWorkletPan(runtime, handle, arguments[1]);
|
|
3051
|
+
[binding attachUIWorkletPanGesture:worklet];
|
|
3052
|
+
attached = true;
|
|
3053
|
+
return Value();
|
|
3054
|
+
} catch (const JSError &) {
|
|
3055
|
+
if (!attached) {
|
|
3056
|
+
try {
|
|
3057
|
+
state->detachUIWorkletPan(handle);
|
|
3058
|
+
} catch (...) {
|
|
3059
|
+
}
|
|
3060
|
+
}
|
|
3061
|
+
throw;
|
|
3062
|
+
} catch (const std::exception &error) {
|
|
3063
|
+
if (!attached) {
|
|
3064
|
+
try {
|
|
3065
|
+
state->detachUIWorkletPan(handle);
|
|
3066
|
+
} catch (...) {
|
|
3067
|
+
}
|
|
3068
|
+
}
|
|
3069
|
+
throw JSError(runtime, error.what());
|
|
3070
|
+
}
|
|
3071
|
+
}));
|
|
3072
|
+
host.setProperty(
|
|
3073
|
+
runtime,
|
|
3074
|
+
"detachUIWorkletPanGesture",
|
|
3075
|
+
Function::createFromHostFunction(
|
|
3076
|
+
runtime,
|
|
3077
|
+
PropNameID::forAscii(runtime, "detachUIWorkletPanGesture"),
|
|
3078
|
+
2,
|
|
3079
|
+
[weakRuntimeState](Runtime &runtime,
|
|
3080
|
+
const Value &,
|
|
3081
|
+
const Value *arguments,
|
|
3082
|
+
size_t count) -> Value {
|
|
3083
|
+
if (count != 2 || !arguments[1].isNumber()) {
|
|
3084
|
+
throw JSError(
|
|
3085
|
+
runtime,
|
|
3086
|
+
"detachUIWorkletPanGesture requires a handle and timestamp.");
|
|
3087
|
+
}
|
|
3088
|
+
auto state = weakRuntimeState.lock();
|
|
3089
|
+
if (!state) {
|
|
3090
|
+
throw JSError(runtime, "The Solid Native runtime has stopped.");
|
|
3091
|
+
}
|
|
3092
|
+
const auto handle = requireSafeInteger(
|
|
3093
|
+
runtime,
|
|
3094
|
+
arguments[0],
|
|
3095
|
+
"The native UI worklet handle",
|
|
3096
|
+
true);
|
|
3097
|
+
try {
|
|
3098
|
+
const auto inputs = state->releaseUIWorkletPan(
|
|
3099
|
+
handle, arguments[1].getNumber());
|
|
3100
|
+
Array result(runtime, inputs.size());
|
|
3101
|
+
for (size_t index = 0; index < inputs.size(); ++index) {
|
|
3102
|
+
result.setValueAtIndex(runtime, index, inputs.at(index));
|
|
3103
|
+
}
|
|
3104
|
+
return result;
|
|
3105
|
+
} catch (const std::exception &error) {
|
|
3106
|
+
throw JSError(runtime, error.what());
|
|
3107
|
+
}
|
|
3108
|
+
}));
|
|
3109
|
+
host.setProperty(
|
|
3110
|
+
runtime,
|
|
3111
|
+
"destroyUIWorklet",
|
|
3112
|
+
Function::createFromHostFunction(
|
|
3113
|
+
runtime,
|
|
3114
|
+
PropNameID::forAscii(runtime, "destroyUIWorklet"),
|
|
3115
|
+
1,
|
|
3116
|
+
[weakRuntimeState](Runtime &runtime,
|
|
3117
|
+
const Value &,
|
|
3118
|
+
const Value *arguments,
|
|
3119
|
+
size_t count) -> Value {
|
|
3120
|
+
if (count != 1) {
|
|
3121
|
+
throw JSError(
|
|
3122
|
+
runtime,
|
|
3123
|
+
"destroyUIWorklet requires exactly one handle.");
|
|
3124
|
+
}
|
|
3125
|
+
auto state = weakRuntimeState.lock();
|
|
3126
|
+
if (!state) {
|
|
3127
|
+
throw JSError(runtime, "The Solid Native runtime has stopped.");
|
|
3128
|
+
}
|
|
3129
|
+
const auto handle = requireSafeInteger(
|
|
3130
|
+
runtime,
|
|
3131
|
+
arguments[0],
|
|
3132
|
+
"The native UI worklet handle",
|
|
3133
|
+
true);
|
|
3134
|
+
try {
|
|
3135
|
+
state->destroyUIWorklet(handle);
|
|
3136
|
+
return Value();
|
|
3137
|
+
} catch (const std::exception &error) {
|
|
3138
|
+
throw JSError(runtime, error.what());
|
|
3139
|
+
}
|
|
3140
|
+
}));
|
|
3141
|
+
host.setProperty(
|
|
3142
|
+
runtime,
|
|
3143
|
+
"getUIWorkletInfo",
|
|
3144
|
+
Function::createFromHostFunction(
|
|
3145
|
+
runtime,
|
|
3146
|
+
PropNameID::forAscii(runtime, "getUIWorkletInfo"),
|
|
3147
|
+
1,
|
|
3148
|
+
[weakRuntimeState](Runtime &runtime,
|
|
3149
|
+
const Value &,
|
|
3150
|
+
const Value *arguments,
|
|
3151
|
+
size_t count) -> Value {
|
|
3152
|
+
if (count != 1) {
|
|
3153
|
+
throw JSError(
|
|
3154
|
+
runtime,
|
|
3155
|
+
"getUIWorkletInfo requires exactly one handle.");
|
|
3156
|
+
}
|
|
3157
|
+
auto runtimeState = weakRuntimeState.lock();
|
|
3158
|
+
if (!runtimeState) {
|
|
3159
|
+
throw JSError(runtime, "The Solid Native runtime has stopped.");
|
|
3160
|
+
}
|
|
3161
|
+
const auto handle = requireSafeInteger(
|
|
3162
|
+
runtime,
|
|
3163
|
+
arguments[0],
|
|
3164
|
+
"The native UI worklet handle",
|
|
3165
|
+
true);
|
|
3166
|
+
try {
|
|
3167
|
+
auto worklet = runtimeState->requireUIWorklet(handle);
|
|
3168
|
+
std::lock_guard lock(worklet->mutex);
|
|
3169
|
+
Object info(runtime);
|
|
3170
|
+
info.setProperty(
|
|
3171
|
+
runtime, "handle", static_cast<double>(worklet->handle));
|
|
3172
|
+
info.setProperty(
|
|
3173
|
+
runtime, "target", static_cast<double>(worklet->targetNode));
|
|
3174
|
+
info.setProperty(
|
|
3175
|
+
runtime, "sequence", static_cast<double>(worklet->sequence));
|
|
3176
|
+
info.setProperty(
|
|
3177
|
+
runtime,
|
|
3178
|
+
"appliedSequence",
|
|
3179
|
+
static_cast<double>(worklet->appliedSequence));
|
|
3180
|
+
info.setProperty(runtime, "applied", worklet->applied);
|
|
3181
|
+
info.setProperty(
|
|
3182
|
+
runtime,
|
|
3183
|
+
"frameTimeNanoseconds",
|
|
3184
|
+
static_cast<double>(worklet->frameTimeNanoseconds));
|
|
3185
|
+
if (worklet->hasTimestamp) {
|
|
3186
|
+
info.setProperty(runtime, "timestamp", worklet->timestamp);
|
|
3187
|
+
}
|
|
3188
|
+
info.setProperty(
|
|
3189
|
+
runtime, "timingActive", worklet->timing.has_value());
|
|
3190
|
+
info.setProperty(
|
|
3191
|
+
runtime, "timingProgress", worklet->timingProgress);
|
|
3192
|
+
if (!worklet->timingFrameStatistics.empty()) {
|
|
3193
|
+
info.setProperty(
|
|
3194
|
+
runtime,
|
|
3195
|
+
"timingFrameStatistics",
|
|
3196
|
+
makeUIWorkletFrameStatistics(
|
|
3197
|
+
runtime,
|
|
3198
|
+
worklet->timingFrameStatistics.snapshot()));
|
|
3199
|
+
}
|
|
3200
|
+
if (worklet->springPosition.has_value() &&
|
|
3201
|
+
worklet->springVelocity.has_value()) {
|
|
3202
|
+
info.setProperty(
|
|
3203
|
+
runtime,
|
|
3204
|
+
"springActive",
|
|
3205
|
+
worklet->spring.has_value());
|
|
3206
|
+
info.setProperty(
|
|
3207
|
+
runtime,
|
|
3208
|
+
"springPosition",
|
|
3209
|
+
*worklet->springPosition);
|
|
3210
|
+
info.setProperty(
|
|
3211
|
+
runtime,
|
|
3212
|
+
"springVelocity",
|
|
3213
|
+
*worklet->springVelocity);
|
|
3214
|
+
if (!worklet->springFrameStatistics.empty()) {
|
|
3215
|
+
info.setProperty(
|
|
3216
|
+
runtime,
|
|
3217
|
+
"springFrameStatistics",
|
|
3218
|
+
makeUIWorkletFrameStatistics(
|
|
3219
|
+
runtime,
|
|
3220
|
+
worklet->springFrameStatistics.snapshot()));
|
|
3221
|
+
}
|
|
3222
|
+
}
|
|
3223
|
+
if (worklet->decayElapsedMilliseconds.has_value() &&
|
|
3224
|
+
worklet->decaySpeed.has_value()) {
|
|
3225
|
+
info.setProperty(
|
|
3226
|
+
runtime,
|
|
3227
|
+
"decayActive",
|
|
3228
|
+
worklet->decay.has_value());
|
|
3229
|
+
info.setProperty(
|
|
3230
|
+
runtime,
|
|
3231
|
+
"decayElapsedMilliseconds",
|
|
3232
|
+
*worklet->decayElapsedMilliseconds);
|
|
3233
|
+
info.setProperty(
|
|
3234
|
+
runtime,
|
|
3235
|
+
"decaySpeed",
|
|
3236
|
+
*worklet->decaySpeed);
|
|
3237
|
+
if (!worklet->decayFrameStatistics.empty()) {
|
|
3238
|
+
info.setProperty(
|
|
3239
|
+
runtime,
|
|
3240
|
+
"decayFrameStatistics",
|
|
3241
|
+
makeUIWorkletFrameStatistics(
|
|
3242
|
+
runtime,
|
|
3243
|
+
worklet->decayFrameStatistics.snapshot()));
|
|
3244
|
+
}
|
|
3245
|
+
}
|
|
3246
|
+
if (worklet->pan.has_value()) {
|
|
3247
|
+
const auto &pan = *worklet->pan;
|
|
3248
|
+
info.setProperty(runtime, "gestureAttached", true);
|
|
3249
|
+
info.setProperty(runtime, "gestureActive", pan.active);
|
|
3250
|
+
info.setProperty(
|
|
3251
|
+
runtime,
|
|
3252
|
+
"gestureSequence",
|
|
3253
|
+
static_cast<double>(pan.sequence));
|
|
3254
|
+
if (pan.timestamp.has_value()) {
|
|
3255
|
+
info.setProperty(
|
|
3256
|
+
runtime, "gestureTimestamp", *pan.timestamp);
|
|
3257
|
+
}
|
|
3258
|
+
}
|
|
3259
|
+
Object outputs(runtime);
|
|
3260
|
+
for (const auto &output : worklet->outputs) {
|
|
3261
|
+
outputs.setProperty(runtime, output.name.c_str(), output.value);
|
|
3262
|
+
}
|
|
3263
|
+
info.setProperty(runtime, "outputs", outputs);
|
|
3264
|
+
return info;
|
|
3265
|
+
} catch (const std::exception &error) {
|
|
3266
|
+
throw JSError(runtime, error.what());
|
|
3267
|
+
}
|
|
3268
|
+
}));
|
|
3269
|
+
host.setProperty(
|
|
3270
|
+
runtime,
|
|
3271
|
+
"reportFatalError",
|
|
3272
|
+
Function::createFromHostFunction(
|
|
3273
|
+
runtime,
|
|
3274
|
+
PropNameID::forAscii(runtime, "reportFatalError"),
|
|
3275
|
+
2,
|
|
3276
|
+
[binding, weakRuntimeState](Runtime &runtime,
|
|
3277
|
+
const Value &,
|
|
3278
|
+
const Value *arguments,
|
|
3279
|
+
size_t count) -> Value {
|
|
3280
|
+
if (count != 2) {
|
|
3281
|
+
throw JSError(
|
|
3282
|
+
runtime,
|
|
3283
|
+
"reportFatalError requires exactly two arguments: name and message.");
|
|
3284
|
+
}
|
|
3285
|
+
auto runtimeState = weakRuntimeState.lock();
|
|
3286
|
+
if (!runtimeState) {
|
|
3287
|
+
throw JSError(runtime, "The Solid Native runtime has stopped.");
|
|
3288
|
+
}
|
|
3289
|
+
const auto name = requireFatalErrorText(
|
|
3290
|
+
runtime,
|
|
3291
|
+
arguments[0],
|
|
3292
|
+
"The fatal error name",
|
|
3293
|
+
MaximumFatalErrorNameBytes);
|
|
3294
|
+
const auto message = requireFatalErrorText(
|
|
3295
|
+
runtime,
|
|
3296
|
+
arguments[1],
|
|
3297
|
+
"The fatal error message",
|
|
3298
|
+
MaximumFatalErrorMessageBytes);
|
|
3299
|
+
auto nativeName = [NSString stringWithUTF8String:name.c_str()];
|
|
3300
|
+
auto nativeMessage =
|
|
3301
|
+
[NSString stringWithUTF8String:message.c_str()];
|
|
3302
|
+
if (nativeName == nil || nativeMessage == nil ||
|
|
3303
|
+
![binding reportFatalErrorWithName:nativeName
|
|
3304
|
+
message:nativeMessage]) {
|
|
3305
|
+
throw JSError(
|
|
3306
|
+
runtime,
|
|
3307
|
+
"The native shell did not accept the terminal error.");
|
|
3308
|
+
}
|
|
3309
|
+
return Value();
|
|
3310
|
+
}));
|
|
3311
|
+
if (hasStatusBarControl) {
|
|
3312
|
+
host.setProperty(
|
|
3313
|
+
runtime,
|
|
3314
|
+
"setStatusBarStyle",
|
|
3315
|
+
Function::createFromHostFunction(
|
|
3316
|
+
runtime,
|
|
3317
|
+
PropNameID::forAscii(runtime, "setStatusBarStyle"),
|
|
3318
|
+
2,
|
|
3319
|
+
[binding, weakRuntimeState](Runtime &runtime,
|
|
3320
|
+
const Value &,
|
|
3321
|
+
const Value *arguments,
|
|
3322
|
+
size_t count) -> Value {
|
|
3323
|
+
if (count != 2) {
|
|
3324
|
+
throw JSError(
|
|
3325
|
+
runtime,
|
|
3326
|
+
"setStatusBarStyle requires exactly two arguments: style and animated.");
|
|
3327
|
+
}
|
|
3328
|
+
auto runtimeState = weakRuntimeState.lock();
|
|
3329
|
+
if (!runtimeState) {
|
|
3330
|
+
throw JSError(runtime, "The Solid Native runtime has stopped.");
|
|
3331
|
+
}
|
|
3332
|
+
if (!arguments[0].isString()) {
|
|
3333
|
+
throw JSError(
|
|
3334
|
+
runtime,
|
|
3335
|
+
"setStatusBarStyle requires a string style.");
|
|
3336
|
+
}
|
|
3337
|
+
if (!arguments[1].isBool()) {
|
|
3338
|
+
throw JSError(
|
|
3339
|
+
runtime,
|
|
3340
|
+
"setStatusBarStyle requires a boolean animated flag.");
|
|
3341
|
+
}
|
|
3342
|
+
const auto style = arguments[0].asString(runtime).utf8(runtime);
|
|
3343
|
+
if (style != "default" && style != "light-content" &&
|
|
3344
|
+
style != "dark-content") {
|
|
3345
|
+
throw JSError(
|
|
3346
|
+
runtime,
|
|
3347
|
+
"setStatusBarStyle style must be default, light-content, or dark-content.");
|
|
3348
|
+
}
|
|
3349
|
+
auto nativeStyle = [NSString stringWithUTF8String:style.c_str()];
|
|
3350
|
+
if (nativeStyle != nil) {
|
|
3351
|
+
[binding updateStatusBarStyle:nativeStyle
|
|
3352
|
+
animated:arguments[1].getBool()];
|
|
3353
|
+
}
|
|
3354
|
+
return Value();
|
|
3355
|
+
}));
|
|
3356
|
+
host.setProperty(
|
|
3357
|
+
runtime,
|
|
3358
|
+
"setStatusBarHidden",
|
|
3359
|
+
Function::createFromHostFunction(
|
|
3360
|
+
runtime,
|
|
3361
|
+
PropNameID::forAscii(runtime, "setStatusBarHidden"),
|
|
3362
|
+
2,
|
|
3363
|
+
[binding, weakRuntimeState](Runtime &runtime,
|
|
3364
|
+
const Value &,
|
|
3365
|
+
const Value *arguments,
|
|
3366
|
+
size_t count) -> Value {
|
|
3367
|
+
if (count != 2) {
|
|
3368
|
+
throw JSError(
|
|
3369
|
+
runtime,
|
|
3370
|
+
"setStatusBarHidden requires exactly two arguments: hidden and animation.");
|
|
3371
|
+
}
|
|
3372
|
+
auto runtimeState = weakRuntimeState.lock();
|
|
3373
|
+
if (!runtimeState) {
|
|
3374
|
+
throw JSError(runtime, "The Solid Native runtime has stopped.");
|
|
3375
|
+
}
|
|
3376
|
+
if (!arguments[0].isBool()) {
|
|
3377
|
+
throw JSError(
|
|
3378
|
+
runtime,
|
|
3379
|
+
"setStatusBarHidden requires a boolean hidden flag.");
|
|
3380
|
+
}
|
|
3381
|
+
if (!arguments[1].isString()) {
|
|
3382
|
+
throw JSError(
|
|
3383
|
+
runtime,
|
|
3384
|
+
"setStatusBarHidden requires a string animation.");
|
|
3385
|
+
}
|
|
3386
|
+
const auto animation =
|
|
3387
|
+
arguments[1].asString(runtime).utf8(runtime);
|
|
3388
|
+
if (animation != "none" && animation != "fade" &&
|
|
3389
|
+
animation != "slide") {
|
|
3390
|
+
throw JSError(
|
|
3391
|
+
runtime,
|
|
3392
|
+
"setStatusBarHidden animation must be none, fade, or slide.");
|
|
3393
|
+
}
|
|
3394
|
+
auto nativeAnimation =
|
|
3395
|
+
[NSString stringWithUTF8String:animation.c_str()];
|
|
3396
|
+
if (nativeAnimation != nil) {
|
|
3397
|
+
[binding updateStatusBarHidden:arguments[0].getBool()
|
|
3398
|
+
animation:nativeAnimation];
|
|
3399
|
+
}
|
|
3400
|
+
return Value();
|
|
3401
|
+
}));
|
|
3402
|
+
}
|
|
3403
|
+
host.setProperty(
|
|
3404
|
+
runtime,
|
|
3405
|
+
"destroySurface",
|
|
3406
|
+
Function::createFromHostFunction(
|
|
3407
|
+
runtime,
|
|
3408
|
+
PropNameID::forAscii(runtime, "destroySurface"),
|
|
3409
|
+
0,
|
|
3410
|
+
[binding, weakRuntimeState, routing](
|
|
3411
|
+
Runtime &runtime,
|
|
3412
|
+
const Value &,
|
|
3413
|
+
const Value *,
|
|
3414
|
+
size_t count) -> Value {
|
|
3415
|
+
if (count != 0) {
|
|
3416
|
+
throw JSError(
|
|
3417
|
+
runtime, "destroySurface does not accept arguments.");
|
|
3418
|
+
}
|
|
3419
|
+
auto runtimeState = weakRuntimeState.lock();
|
|
3420
|
+
if (!runtimeState) {
|
|
3421
|
+
throw JSError(runtime, "The Solid Native runtime has stopped.");
|
|
3422
|
+
}
|
|
3423
|
+
if (runtimeState->activeUIWorkletCount() != 0) {
|
|
3424
|
+
throw JSError(
|
|
3425
|
+
runtime,
|
|
3426
|
+
"Cannot destroy a Fabric surface with active native UI "
|
|
3427
|
+
"worklets.");
|
|
3428
|
+
}
|
|
3429
|
+
installEmptySurfaceStopHandler(runtime);
|
|
3430
|
+
NSError *error = nil;
|
|
3431
|
+
if (![binding destroySurface:&error]) {
|
|
3432
|
+
throw JSError(runtime, utf8(error.localizedDescription));
|
|
3433
|
+
}
|
|
3434
|
+
runtimeState->reclaimAllInstances(runtime, routing);
|
|
3435
|
+
runtimeState->reclaimAllResources(runtime);
|
|
3436
|
+
return Value();
|
|
3437
|
+
}));
|
|
3438
|
+
host.setProperty(
|
|
3439
|
+
runtime,
|
|
3440
|
+
"commit",
|
|
3441
|
+
Function::createFromHostFunction(
|
|
3442
|
+
runtime,
|
|
3443
|
+
PropNameID::forAscii(runtime, "commit"),
|
|
3444
|
+
1,
|
|
3445
|
+
[binding, weakRuntimeState, routing](Runtime &runtime,
|
|
3446
|
+
const Value &,
|
|
3447
|
+
const Value *arguments,
|
|
3448
|
+
size_t count) -> Value {
|
|
3449
|
+
if (count != 1 || !arguments[0].isObject()) {
|
|
3450
|
+
throw JSError(
|
|
3451
|
+
runtime, "commit requires exactly one transaction object.");
|
|
3452
|
+
}
|
|
3453
|
+
const auto commitStartedAt =
|
|
3454
|
+
[NSDate date].timeIntervalSince1970 * 1000.0;
|
|
3455
|
+
const auto commitStartedMonotonic = CACurrentMediaTime() * 1000.0;
|
|
3456
|
+
|
|
3457
|
+
try {
|
|
3458
|
+
auto runtimeState = weakRuntimeState.lock();
|
|
3459
|
+
if (!runtimeState) {
|
|
3460
|
+
throw JSError(runtime, "The Solid Native runtime has stopped.");
|
|
3461
|
+
}
|
|
3462
|
+
size_t valueCount = 0;
|
|
3463
|
+
std::vector<Value> ancestors;
|
|
3464
|
+
auto dynamic = dynamicFromJSI(
|
|
3465
|
+
runtime,
|
|
3466
|
+
arguments[0],
|
|
3467
|
+
"transaction",
|
|
3468
|
+
0,
|
|
3469
|
+
valueCount,
|
|
3470
|
+
ancestors);
|
|
3471
|
+
id transaction = foundationFromDynamic(dynamic);
|
|
3472
|
+
if (![transaction isKindOfClass:NSDictionary.class]) {
|
|
3473
|
+
throw std::invalid_argument(
|
|
3474
|
+
"The host transaction must be an object.");
|
|
3475
|
+
}
|
|
3476
|
+
auto transactionDictionary =
|
|
3477
|
+
(NSDictionary<NSString *, id> *)transaction;
|
|
3478
|
+
id surfaceValue = transactionDictionary[@"surface"];
|
|
3479
|
+
const auto surface =
|
|
3480
|
+
[surfaceValue isKindOfClass:NSNumber.class]
|
|
3481
|
+
? [(NSNumber *)surfaceValue longLongValue]
|
|
3482
|
+
: 0;
|
|
3483
|
+
auto prepared = prepareNodeIdentities(
|
|
3484
|
+
runtime,
|
|
3485
|
+
transactionDictionary,
|
|
3486
|
+
surface,
|
|
3487
|
+
runtimeState);
|
|
3488
|
+
const auto deletedHandles =
|
|
3489
|
+
deletedNodeHandles(transactionDictionary);
|
|
3490
|
+
IdentityRetention retention(runtime, runtimeState, prepared);
|
|
3491
|
+
const NativeResourceResolver resourceResolver =
|
|
3492
|
+
[runtimeState](Runtime &runtime,
|
|
3493
|
+
int64_t handle,
|
|
3494
|
+
const std::string &kind) -> Value {
|
|
3495
|
+
return runtimeState->resolveResource(runtime, handle, kind);
|
|
3496
|
+
};
|
|
3497
|
+
auto layoutRouteStage = routing->stageLayoutRoutes(
|
|
3498
|
+
transactionDictionary, prepared.identities);
|
|
3499
|
+
NSError *error = nil;
|
|
3500
|
+
auto result = [binding
|
|
3501
|
+
applyTransaction:
|
|
3502
|
+
transactionDictionary
|
|
3503
|
+
nodeIdentities:&prepared.identities
|
|
3504
|
+
runtime:&runtime
|
|
3505
|
+
resourceResolver:&resourceResolver
|
|
3506
|
+
error:&error];
|
|
3507
|
+
if (result == nil) {
|
|
3508
|
+
routing->rollbackLayoutRoutes(layoutRouteStage);
|
|
3509
|
+
throw JSError(runtime, utf8(error.localizedDescription));
|
|
3510
|
+
}
|
|
3511
|
+
retention.commit();
|
|
3512
|
+
runtimeState->cancelDeletedUIWorklets(deletedHandles);
|
|
3513
|
+
routing->publishCommit(
|
|
3514
|
+
transactionDictionary,
|
|
3515
|
+
prepared.identities,
|
|
3516
|
+
[result[@"sequence"] longLongValue]);
|
|
3517
|
+
routing->finishLayoutRoutes(layoutRouteStage);
|
|
3518
|
+
id fabricRevisionValue = result[@"fabricRevision"];
|
|
3519
|
+
if ([fabricRevisionValue isKindOfClass:NSNumber.class]) {
|
|
3520
|
+
const auto fabricRevision =
|
|
3521
|
+
[(NSNumber *)fabricRevisionValue longLongValue];
|
|
3522
|
+
runtimeState->retireInstances(
|
|
3523
|
+
fabricRevision, deletedHandles);
|
|
3524
|
+
const auto notifyLifecycle =
|
|
3525
|
+
runtimeState->hasCommitLifecycleHandler();
|
|
3526
|
+
if (notifyLifecycle || !deletedHandles.empty()) {
|
|
3527
|
+
auto observation =
|
|
3528
|
+
std::make_shared<const CommitMountObservation>(
|
|
3529
|
+
CommitMountObservation{
|
|
3530
|
+
.surface = surface,
|
|
3531
|
+
.sequence =
|
|
3532
|
+
[result[@"sequence"] longLongValue],
|
|
3533
|
+
.fabricRevision = fabricRevision,
|
|
3534
|
+
.commitStartedAt = commitStartedAt,
|
|
3535
|
+
.commitStartedMonotonic =
|
|
3536
|
+
commitStartedMonotonic,
|
|
3537
|
+
.notifyLifecycle = notifyLifecycle,
|
|
3538
|
+
.causalOperationId = commitCausalOperationId(
|
|
3539
|
+
transactionDictionary),
|
|
3540
|
+
});
|
|
3541
|
+
[binding scheduleMountObservation:std::move(observation)];
|
|
3542
|
+
}
|
|
3543
|
+
}
|
|
3544
|
+
return commitResultObject(runtime, result);
|
|
3545
|
+
} catch (const JSError &) {
|
|
3546
|
+
throw;
|
|
3547
|
+
} catch (const std::exception &error) {
|
|
3548
|
+
throw JSError(runtime, error.what());
|
|
3549
|
+
}
|
|
3550
|
+
}));
|
|
3551
|
+
|
|
3552
|
+
auto freeze = objectConstructor.getPropertyAsFunction(runtime, "freeze");
|
|
3553
|
+
freeze.call(runtime, host);
|
|
3554
|
+
|
|
3555
|
+
Object descriptor(runtime);
|
|
3556
|
+
descriptor.setProperty(runtime, "value", host);
|
|
3557
|
+
descriptor.setProperty(runtime, "writable", false);
|
|
3558
|
+
descriptor.setProperty(runtime, "configurable", false);
|
|
3559
|
+
descriptor.setProperty(runtime, "enumerable", false);
|
|
3560
|
+
defineProperty.call(
|
|
3561
|
+
runtime,
|
|
3562
|
+
runtime.global(),
|
|
3563
|
+
String::createFromAscii(runtime, GlobalBindingName),
|
|
3564
|
+
descriptor);
|
|
3565
|
+
}
|
|
3566
|
+
|
|
3567
|
+
- (void)retireRuntimeGeneration:(uint64_t)generation {
|
|
3568
|
+
std::shared_ptr<EventRoutingState> routing;
|
|
3569
|
+
std::shared_ptr<facebook::react::EventListener> eventListener;
|
|
3570
|
+
SolidNativeFabricSurface *eventSurface = nil;
|
|
3571
|
+
SolidNativeFabricSurface *surface = nil;
|
|
3572
|
+
[_lock lock];
|
|
3573
|
+
if (generation != _runtimeGeneration) {
|
|
3574
|
+
[_lock unlock];
|
|
3575
|
+
return;
|
|
3576
|
+
}
|
|
3577
|
+
routing = _routing;
|
|
3578
|
+
eventListener = _eventListener;
|
|
3579
|
+
eventSurface = _eventSurface;
|
|
3580
|
+
surface = _surface;
|
|
3581
|
+
_eventSurface = nil;
|
|
3582
|
+
_eventListener.reset();
|
|
3583
|
+
_runtimeScheduler.reset();
|
|
3584
|
+
_runtimeState.reset();
|
|
3585
|
+
[_lock unlock];
|
|
3586
|
+
|
|
3587
|
+
routing->active.store(false, std::memory_order_release);
|
|
3588
|
+
routing->handlerInstalled.store(false, std::memory_order_release);
|
|
3589
|
+
if (eventSurface != nil && eventListener) {
|
|
3590
|
+
[eventSurface removeSolidNativeEventListener:eventListener];
|
|
3591
|
+
}
|
|
3592
|
+
// Fabric InstanceHandles retain JSI WeakObjects. Release the logical tree
|
|
3593
|
+
// from the retiring NativeState destructor, before Hermes tears them down.
|
|
3594
|
+
if (surface != nil) [surface resetForJSRuntimeReload];
|
|
3595
|
+
}
|
|
3596
|
+
|
|
3597
|
+
- (void)publishSurface:(SolidNativeFabricSurface *)surface {
|
|
3598
|
+
[_lock lock];
|
|
3599
|
+
auto previousEventSurface = _eventSurface;
|
|
3600
|
+
auto eventListener = _eventListener;
|
|
3601
|
+
_surface = surface;
|
|
3602
|
+
_eventSurface = eventListener ? surface : nil;
|
|
3603
|
+
_surfaceReady = NO;
|
|
3604
|
+
_invalidated = NO;
|
|
3605
|
+
_routing->active.store(true, std::memory_order_release);
|
|
3606
|
+
_routing->surface.store(surface.surfaceId, std::memory_order_release);
|
|
3607
|
+
[_lock unlock];
|
|
3608
|
+
if (previousEventSurface != nil && eventListener) {
|
|
3609
|
+
[previousEventSurface removeSolidNativeEventListener:eventListener];
|
|
3610
|
+
}
|
|
3611
|
+
if (eventListener) {
|
|
3612
|
+
[surface addSolidNativeEventListener:eventListener];
|
|
3613
|
+
}
|
|
3614
|
+
}
|
|
3615
|
+
|
|
3616
|
+
- (void)markSurfaceReloading {
|
|
3617
|
+
[_lock lock];
|
|
3618
|
+
if (!_invalidated && _surface != nil) _surfaceReady = NO;
|
|
3619
|
+
[_lock unlock];
|
|
3620
|
+
}
|
|
3621
|
+
|
|
3622
|
+
- (void)markSurfaceReady {
|
|
3623
|
+
[_lock lock];
|
|
3624
|
+
if (!_invalidated && _surface != nil) _surfaceReady = YES;
|
|
3625
|
+
[_lock unlock];
|
|
3626
|
+
}
|
|
3627
|
+
|
|
3628
|
+
- (void)setFatalErrorHandler:(SolidNativeFabricFatalErrorHandler)handler {
|
|
3629
|
+
[_lock lock];
|
|
3630
|
+
_fatalErrorHandler = [handler copy];
|
|
3631
|
+
[_lock unlock];
|
|
3632
|
+
}
|
|
3633
|
+
|
|
3634
|
+
- (void)setStatusBarStyleHandler:
|
|
3635
|
+
(SolidNativeFabricStatusBarStyleHandler)handler {
|
|
3636
|
+
[_lock lock];
|
|
3637
|
+
_statusBarStyleHandler = [handler copy];
|
|
3638
|
+
[_lock unlock];
|
|
3639
|
+
}
|
|
3640
|
+
|
|
3641
|
+
- (void)setStatusBarHiddenHandler:
|
|
3642
|
+
(SolidNativeFabricStatusBarHiddenHandler)handler {
|
|
3643
|
+
[_lock lock];
|
|
3644
|
+
_statusBarHiddenHandler = [handler copy];
|
|
3645
|
+
[_lock unlock];
|
|
3646
|
+
}
|
|
3647
|
+
|
|
3648
|
+
- (BOOL)reportFatalErrorWithName:(NSString *)name
|
|
3649
|
+
message:(NSString *)message {
|
|
3650
|
+
SolidNativeFabricFatalErrorHandler handler = nil;
|
|
3651
|
+
[_lock lock];
|
|
3652
|
+
if (!_invalidated) handler = [_fatalErrorHandler copy];
|
|
3653
|
+
[_lock unlock];
|
|
3654
|
+
if (handler == nil) return NO;
|
|
3655
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
3656
|
+
handler(name, message);
|
|
3657
|
+
});
|
|
3658
|
+
return YES;
|
|
3659
|
+
}
|
|
3660
|
+
|
|
3661
|
+
- (void)updateStatusBarStyle:(NSString *)style animated:(BOOL)animated {
|
|
3662
|
+
SolidNativeFabricStatusBarStyleHandler handler = nil;
|
|
3663
|
+
[_lock lock];
|
|
3664
|
+
if (!_invalidated) handler = [_statusBarStyleHandler copy];
|
|
3665
|
+
[_lock unlock];
|
|
3666
|
+
if (handler == nil) return;
|
|
3667
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
3668
|
+
handler(style, animated);
|
|
3669
|
+
});
|
|
3670
|
+
}
|
|
3671
|
+
|
|
3672
|
+
- (void)updateStatusBarHidden:(BOOL)hidden animation:(NSString *)animation {
|
|
3673
|
+
SolidNativeFabricStatusBarHiddenHandler handler = nil;
|
|
3674
|
+
[_lock lock];
|
|
3675
|
+
if (!_invalidated) handler = [_statusBarHiddenHandler copy];
|
|
3676
|
+
[_lock unlock];
|
|
3677
|
+
if (handler == nil) return;
|
|
3678
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
3679
|
+
handler(hidden, animation);
|
|
3680
|
+
});
|
|
3681
|
+
}
|
|
3682
|
+
|
|
3683
|
+
- (NSDictionary<NSString *, id> *)surfaceSnapshot {
|
|
3684
|
+
[_lock lock];
|
|
3685
|
+
const BOOL ready = !_invalidated && _surfaceReady && _surface != nil;
|
|
3686
|
+
const NSInteger surface = ready ? _surface.surfaceId : 0;
|
|
3687
|
+
const int64_t sequence = ready ? _surface.lastSequence : 0;
|
|
3688
|
+
auto snapshot = @{
|
|
3689
|
+
@"ready" : @(ready),
|
|
3690
|
+
@"surface" : @(surface),
|
|
3691
|
+
@"sequence" : @(sequence)
|
|
3692
|
+
};
|
|
3693
|
+
[_lock unlock];
|
|
3694
|
+
return snapshot;
|
|
3695
|
+
}
|
|
3696
|
+
|
|
3697
|
+
- (nullable NSDictionary<NSString *, id> *)
|
|
3698
|
+
applyTransaction:(NSDictionary<NSString *, id> *)transaction
|
|
3699
|
+
nodeIdentities:(const NodeIdentityMap *)nodeIdentities
|
|
3700
|
+
runtime:(facebook::jsi::Runtime *)runtime
|
|
3701
|
+
resourceResolver:(const NativeResourceResolver *)resourceResolver
|
|
3702
|
+
error:(NSError **)error {
|
|
3703
|
+
[_lock lock];
|
|
3704
|
+
if (_invalidated || !_surfaceReady || _surface == nil) {
|
|
3705
|
+
if (error != nullptr) {
|
|
3706
|
+
*error = [NSError
|
|
3707
|
+
errorWithDomain:@"SolidNativeFabricJSIBinding"
|
|
3708
|
+
code:1
|
|
3709
|
+
userInfo:@{
|
|
3710
|
+
NSLocalizedDescriptionKey :
|
|
3711
|
+
@"The Solid Native Fabric surface is not ready."
|
|
3712
|
+
}];
|
|
3713
|
+
}
|
|
3714
|
+
[_lock unlock];
|
|
3715
|
+
return nil;
|
|
3716
|
+
}
|
|
3717
|
+
auto result = [_surface applyTransactionSynchronously:transaction
|
|
3718
|
+
nodeIdentities:nodeIdentities
|
|
3719
|
+
runtime:runtime
|
|
3720
|
+
resourceResolver:resourceResolver
|
|
3721
|
+
error:error];
|
|
3722
|
+
[_lock unlock];
|
|
3723
|
+
return result;
|
|
3724
|
+
}
|
|
3725
|
+
|
|
3726
|
+
- (nullable NSDictionary<NSString *, id> *)
|
|
3727
|
+
measureNode:(int64_t)node
|
|
3728
|
+
afterSequence:(int64_t)afterSequence
|
|
3729
|
+
error:(NSError **)error {
|
|
3730
|
+
[_lock lock];
|
|
3731
|
+
if (_invalidated || !_surfaceReady || _surface == nil) {
|
|
3732
|
+
if (error != nullptr) {
|
|
3733
|
+
*error = [NSError
|
|
3734
|
+
errorWithDomain:@"SolidNativeFabricJSIBinding"
|
|
3735
|
+
code:1
|
|
3736
|
+
userInfo:@{
|
|
3737
|
+
NSLocalizedDescriptionKey :
|
|
3738
|
+
@"The Solid Native Fabric surface is not ready."
|
|
3739
|
+
}];
|
|
3740
|
+
}
|
|
3741
|
+
[_lock unlock];
|
|
3742
|
+
return nil;
|
|
3743
|
+
}
|
|
3744
|
+
auto result = [_surface measureNodeSynchronously:node
|
|
3745
|
+
afterSequence:afterSequence
|
|
3746
|
+
error:error];
|
|
3747
|
+
[_lock unlock];
|
|
3748
|
+
return result;
|
|
3749
|
+
}
|
|
3750
|
+
|
|
3751
|
+
- (void)scheduleUIWorkletFrame:
|
|
3752
|
+
(std::shared_ptr<IOSUIWorkletState>)worklet {
|
|
3753
|
+
uint64_t frameGeneration;
|
|
3754
|
+
{
|
|
3755
|
+
std::lock_guard lock(worklet->mutex);
|
|
3756
|
+
if (!worklet->active || worklet->framePending) return;
|
|
3757
|
+
worklet->framePending = true;
|
|
3758
|
+
frameGeneration = ++worklet->frameGeneration;
|
|
3759
|
+
}
|
|
3760
|
+
|
|
3761
|
+
__weak SolidNativeFabricJSIBinding *weakBinding = self;
|
|
3762
|
+
std::weak_ptr<IOSUIWorkletState> weakWorklet = worklet;
|
|
3763
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
3764
|
+
SolidNativeFabricJSIBinding *binding = weakBinding;
|
|
3765
|
+
if (binding == nil) {
|
|
3766
|
+
std::lock_guard lock(worklet->mutex);
|
|
3767
|
+
if (worklet->frameGeneration == frameGeneration) {
|
|
3768
|
+
worklet->framePending = false;
|
|
3769
|
+
}
|
|
3770
|
+
return;
|
|
3771
|
+
}
|
|
3772
|
+
auto observer = [[SolidNativeDisplayLinkObserver alloc]
|
|
3773
|
+
initWithCallback:^(CADisplayLink *displayLink) {
|
|
3774
|
+
auto frameWorklet = weakWorklet.lock();
|
|
3775
|
+
SolidNativeFabricJSIBinding *frameBinding = weakBinding;
|
|
3776
|
+
if (frameWorklet == nullptr || frameBinding == nil) {
|
|
3777
|
+
if (frameWorklet != nullptr) {
|
|
3778
|
+
std::lock_guard lock(frameWorklet->mutex);
|
|
3779
|
+
if (frameWorklet->frameGeneration == frameGeneration) {
|
|
3780
|
+
frameWorklet->framePending = false;
|
|
3781
|
+
frameWorklet->frameObserver = nil;
|
|
3782
|
+
}
|
|
3783
|
+
}
|
|
3784
|
+
return NO;
|
|
3785
|
+
}
|
|
3786
|
+
BOOL scheduleNextFrame = NO;
|
|
3787
|
+
{
|
|
3788
|
+
std::lock_guard lock(frameWorklet->mutex);
|
|
3789
|
+
if (frameWorklet->frameGeneration != frameGeneration) return NO;
|
|
3790
|
+
if (!frameWorklet->active) {
|
|
3791
|
+
frameWorklet->framePending = false;
|
|
3792
|
+
frameWorklet->frameObserver = nil;
|
|
3793
|
+
return NO;
|
|
3794
|
+
}
|
|
3795
|
+
try {
|
|
3796
|
+
auto nextInputs = frameWorklet->inputs;
|
|
3797
|
+
auto nextTimingProgress = frameWorklet->timingProgress;
|
|
3798
|
+
const auto recordsTimingFrame = frameWorklet->timing.has_value();
|
|
3799
|
+
const auto recordsSpringFrame = frameWorklet->spring.has_value();
|
|
3800
|
+
const auto recordsDecayFrame = frameWorklet->decay.has_value();
|
|
3801
|
+
const auto nextFrameTimeNanoseconds = static_cast<int64_t>(
|
|
3802
|
+
std::llround(displayLink.timestamp * 1000000000.0));
|
|
3803
|
+
if (frameWorklet->timing.has_value()) {
|
|
3804
|
+
auto &timing = *frameWorklet->timing;
|
|
3805
|
+
if (!timing.startedAtNanoseconds.has_value()) {
|
|
3806
|
+
timing.startedAtNanoseconds = nextFrameTimeNanoseconds;
|
|
3807
|
+
}
|
|
3808
|
+
const auto elapsedMilliseconds = static_cast<double>(
|
|
3809
|
+
nextFrameTimeNanoseconds -
|
|
3810
|
+
*timing.startedAtNanoseconds) /
|
|
3811
|
+
1000000.0;
|
|
3812
|
+
const auto sample =
|
|
3813
|
+
timing.definition.evaluate(timing.from, elapsedMilliseconds);
|
|
3814
|
+
nextTimingProgress = sample.progress;
|
|
3815
|
+
nextInputs = sample.inputs;
|
|
3816
|
+
if (sample.settled) frameWorklet->timing.reset();
|
|
3817
|
+
} else if (frameWorklet->spring.has_value()) {
|
|
3818
|
+
auto &spring = *frameWorklet->spring;
|
|
3819
|
+
if (!spring.startedAtNanoseconds.has_value()) {
|
|
3820
|
+
spring.startedAtNanoseconds = nextFrameTimeNanoseconds;
|
|
3821
|
+
}
|
|
3822
|
+
const auto elapsedMilliseconds = static_cast<double>(
|
|
3823
|
+
nextFrameTimeNanoseconds -
|
|
3824
|
+
*spring.startedAtNanoseconds) /
|
|
3825
|
+
1000000.0;
|
|
3826
|
+
const auto sample =
|
|
3827
|
+
spring.definition.evaluate(elapsedMilliseconds);
|
|
3828
|
+
nextInputs =
|
|
3829
|
+
solid_native::worklets::interpolateUIWorkletInputs(
|
|
3830
|
+
spring.from, spring.to, sample.position);
|
|
3831
|
+
frameWorklet->springPosition = sample.position;
|
|
3832
|
+
frameWorklet->springVelocity = sample.velocity;
|
|
3833
|
+
if (sample.settled) frameWorklet->spring.reset();
|
|
3834
|
+
} else if (frameWorklet->decay.has_value()) {
|
|
3835
|
+
auto &decay = *frameWorklet->decay;
|
|
3836
|
+
if (!decay.startedAtNanoseconds.has_value()) {
|
|
3837
|
+
decay.startedAtNanoseconds = nextFrameTimeNanoseconds;
|
|
3838
|
+
}
|
|
3839
|
+
const auto elapsedMilliseconds = static_cast<double>(
|
|
3840
|
+
nextFrameTimeNanoseconds -
|
|
3841
|
+
*decay.startedAtNanoseconds) /
|
|
3842
|
+
1000000.0;
|
|
3843
|
+
const auto sample = decay.definition.evaluate(
|
|
3844
|
+
decay.initialSpeed, elapsedMilliseconds);
|
|
3845
|
+
nextInputs = decay.from;
|
|
3846
|
+
for (size_t index = 0; index < nextInputs.size(); ++index) {
|
|
3847
|
+
nextInputs.at(index) += decay.velocities.at(index) *
|
|
3848
|
+
sample.displacementFactorSeconds;
|
|
3849
|
+
}
|
|
3850
|
+
frameWorklet->decayElapsedMilliseconds = elapsedMilliseconds;
|
|
3851
|
+
frameWorklet->decaySpeed = sample.speed;
|
|
3852
|
+
if (sample.settled) frameWorklet->decay.reset();
|
|
3853
|
+
}
|
|
3854
|
+
auto nextOutputs = frameWorklet->graph.evaluate(nextInputs);
|
|
3855
|
+
if (recordsTimingFrame) {
|
|
3856
|
+
frameWorklet->timingFrameStatistics.recordFrame(
|
|
3857
|
+
nextFrameTimeNanoseconds);
|
|
3858
|
+
}
|
|
3859
|
+
if (recordsSpringFrame) {
|
|
3860
|
+
frameWorklet->springFrameStatistics.recordFrame(
|
|
3861
|
+
nextFrameTimeNanoseconds);
|
|
3862
|
+
}
|
|
3863
|
+
if (recordsDecayFrame) {
|
|
3864
|
+
frameWorklet->decayFrameStatistics.recordFrame(
|
|
3865
|
+
nextFrameTimeNanoseconds);
|
|
3866
|
+
}
|
|
3867
|
+
frameWorklet->inputs = std::move(nextInputs);
|
|
3868
|
+
frameWorklet->outputs = std::move(nextOutputs);
|
|
3869
|
+
frameWorklet->timingProgress = nextTimingProgress;
|
|
3870
|
+
frameWorklet->frameTimeNanoseconds = nextFrameTimeNanoseconds;
|
|
3871
|
+
std::array<double, IOSViewOutputCount> values;
|
|
3872
|
+
values.fill(std::numeric_limits<double>::quiet_NaN());
|
|
3873
|
+
for (size_t index = 0;
|
|
3874
|
+
index < frameWorklet->outputs.size();
|
|
3875
|
+
++index) {
|
|
3876
|
+
values.at(
|
|
3877
|
+
static_cast<size_t>(frameWorklet->channels.at(index))) =
|
|
3878
|
+
frameWorklet->outputs.at(index).value;
|
|
3879
|
+
}
|
|
3880
|
+
frameWorklet->sequence++;
|
|
3881
|
+
frameWorklet->applied = [frameBinding
|
|
3882
|
+
applyUIWorkletFrame:*frameWorklet
|
|
3883
|
+
values:values];
|
|
3884
|
+
if (frameWorklet->applied) {
|
|
3885
|
+
frameWorklet->appliedSequence = frameWorklet->sequence;
|
|
3886
|
+
frameWorklet->mountAttempts = 0;
|
|
3887
|
+
scheduleNextFrame =
|
|
3888
|
+
frameWorklet->timing.has_value() ||
|
|
3889
|
+
frameWorklet->spring.has_value() ||
|
|
3890
|
+
frameWorklet->decay.has_value();
|
|
3891
|
+
} else {
|
|
3892
|
+
frameWorklet->mountAttempts++;
|
|
3893
|
+
scheduleNextFrame =
|
|
3894
|
+
frameWorklet->mountAttempts <
|
|
3895
|
+
MaximumUIWorkletMountAttempts;
|
|
3896
|
+
}
|
|
3897
|
+
} catch (...) {
|
|
3898
|
+
frameWorklet->applied = false;
|
|
3899
|
+
frameWorklet->timing.reset();
|
|
3900
|
+
frameWorklet->spring.reset();
|
|
3901
|
+
frameWorklet->decay.reset();
|
|
3902
|
+
}
|
|
3903
|
+
frameWorklet->framePending = scheduleNextFrame;
|
|
3904
|
+
if (!scheduleNextFrame) frameWorklet->frameObserver = nil;
|
|
3905
|
+
}
|
|
3906
|
+
return scheduleNextFrame;
|
|
3907
|
+
}];
|
|
3908
|
+
{
|
|
3909
|
+
std::lock_guard lock(worklet->mutex);
|
|
3910
|
+
if (!worklet->active || !worklet->framePending ||
|
|
3911
|
+
worklet->frameGeneration != frameGeneration) {
|
|
3912
|
+
return;
|
|
3913
|
+
}
|
|
3914
|
+
worklet->frameObserver = observer;
|
|
3915
|
+
}
|
|
3916
|
+
[observer start];
|
|
3917
|
+
});
|
|
3918
|
+
}
|
|
3919
|
+
|
|
3920
|
+
- (void)attachUIWorkletPanGesture:
|
|
3921
|
+
(std::shared_ptr<IOSUIWorkletState>)worklet {
|
|
3922
|
+
__block std::string error;
|
|
3923
|
+
__weak SolidNativeFabricJSIBinding *weakBinding = self;
|
|
3924
|
+
void (^attach)(void) = ^{
|
|
3925
|
+
SolidNativeFabricJSIBinding *binding = weakBinding;
|
|
3926
|
+
if (binding == nil) {
|
|
3927
|
+
error = "The iOS native UI worklet binding has stopped.";
|
|
3928
|
+
return;
|
|
3929
|
+
}
|
|
3930
|
+
[binding->_lock lock];
|
|
3931
|
+
auto surface = (!binding->_invalidated && binding->_surfaceReady)
|
|
3932
|
+
? binding->_surface
|
|
3933
|
+
: nil;
|
|
3934
|
+
[binding->_lock unlock];
|
|
3935
|
+
if (surface == nil) {
|
|
3936
|
+
error = "The iOS native UI worklet surface is unavailable.";
|
|
3937
|
+
return;
|
|
3938
|
+
}
|
|
3939
|
+
auto view = [surface findComponentViewWithTag:worklet->targetTag];
|
|
3940
|
+
if (view == nil || view.window == nil) {
|
|
3941
|
+
error = "The iOS native UI worklet pan target is not mounted.";
|
|
3942
|
+
return;
|
|
3943
|
+
}
|
|
3944
|
+
|
|
3945
|
+
std::weak_ptr<IOSUIWorkletState> weakWorklet = worklet;
|
|
3946
|
+
auto observer = [[SolidNativePanGestureObserver alloc]
|
|
3947
|
+
initWithView:view
|
|
3948
|
+
callback:^(NSInteger phaseValue, CGFloat translationX,
|
|
3949
|
+
CGFloat translationY, NSTimeInterval timestamp,
|
|
3950
|
+
CGFloat velocityX, CGFloat velocityY) {
|
|
3951
|
+
auto state = weakWorklet.lock();
|
|
3952
|
+
SolidNativeFabricJSIBinding *strongBinding = weakBinding;
|
|
3953
|
+
if (!state || strongBinding == nil)
|
|
3954
|
+
return;
|
|
3955
|
+
BOOL scheduleReleaseDecay = NO;
|
|
3956
|
+
{
|
|
3957
|
+
std::lock_guard lock(state->mutex);
|
|
3958
|
+
if (!state->active || !state->pan.has_value())
|
|
3959
|
+
return;
|
|
3960
|
+
auto &pan = *state->pan;
|
|
3961
|
+
try {
|
|
3962
|
+
if (phaseValue < 0 || phaseValue > 3 ||
|
|
3963
|
+
!std::isfinite(static_cast<double>(translationX)) ||
|
|
3964
|
+
!std::isfinite(static_cast<double>(translationY)) ||
|
|
3965
|
+
!std::isfinite(static_cast<double>(velocityX)) ||
|
|
3966
|
+
!std::isfinite(static_cast<double>(velocityY)) ||
|
|
3967
|
+
!std::isfinite(timestamp) || timestamp < 0 ||
|
|
3968
|
+
(pan.timestamp.has_value() &&
|
|
3969
|
+
timestamp < *pan.timestamp)) {
|
|
3970
|
+
throw std::invalid_argument(
|
|
3971
|
+
"The iOS native UI worklet pan sample is invalid.");
|
|
3972
|
+
}
|
|
3973
|
+
const auto phase = static_cast<
|
|
3974
|
+
solid_native::worklets::UIWorkletPanGesturePhase>(
|
|
3975
|
+
phaseValue);
|
|
3976
|
+
if (phase ==
|
|
3977
|
+
solid_native::worklets::UIWorkletPanGesturePhase::Begin) {
|
|
3978
|
+
if (pan.active) {
|
|
3979
|
+
throw std::invalid_argument(
|
|
3980
|
+
"The iOS native UI worklet pan is already active.");
|
|
3981
|
+
}
|
|
3982
|
+
pan.active = true;
|
|
3983
|
+
pan.originX = state->inputs.at(pan.definition.xInput);
|
|
3984
|
+
pan.originY = state->inputs.at(pan.definition.yInput);
|
|
3985
|
+
pan.timestamp = timestamp;
|
|
3986
|
+
state->timing.reset();
|
|
3987
|
+
state->timingProgress = 0;
|
|
3988
|
+
state->spring.reset();
|
|
3989
|
+
if (state->springPosition.has_value()) {
|
|
3990
|
+
state->springPosition = 0;
|
|
3991
|
+
}
|
|
3992
|
+
if (state->springVelocity.has_value()) {
|
|
3993
|
+
state->springVelocity = 0;
|
|
3994
|
+
}
|
|
3995
|
+
state->decay.reset();
|
|
3996
|
+
if (state->decayElapsedMilliseconds.has_value()) {
|
|
3997
|
+
state->decayElapsedMilliseconds = 0;
|
|
3998
|
+
}
|
|
3999
|
+
if (state->decaySpeed.has_value())
|
|
4000
|
+
state->decaySpeed = 0;
|
|
4001
|
+
return;
|
|
4002
|
+
}
|
|
4003
|
+
if (!pan.active) {
|
|
4004
|
+
throw std::invalid_argument(
|
|
4005
|
+
"The iOS native UI worklet pan is not active.");
|
|
4006
|
+
}
|
|
4007
|
+
if (phase == solid_native::worklets::
|
|
4008
|
+
UIWorkletPanGesturePhase::Cancel) {
|
|
4009
|
+
pan.active = false;
|
|
4010
|
+
pan.timestamp = timestamp;
|
|
4011
|
+
return;
|
|
4012
|
+
}
|
|
4013
|
+
auto nextInputs = pan.definition.evaluateInputs(
|
|
4014
|
+
state->inputs, pan.originX, pan.originY,
|
|
4015
|
+
static_cast<double>(translationX),
|
|
4016
|
+
static_cast<double>(translationY));
|
|
4017
|
+
auto nextOutputs = state->graph.evaluate(nextInputs);
|
|
4018
|
+
std::optional<IOSUIWorkletDecayState> releaseDecay;
|
|
4019
|
+
double releaseSpeed = 0;
|
|
4020
|
+
if (phase == solid_native::worklets::
|
|
4021
|
+
UIWorkletPanGesturePhase::End &&
|
|
4022
|
+
pan.definition.releaseDecay.has_value()) {
|
|
4023
|
+
const auto xVelocity = static_cast<double>(velocityX);
|
|
4024
|
+
const auto yVelocity = static_cast<double>(velocityY);
|
|
4025
|
+
if (std::abs(xVelocity) >
|
|
4026
|
+
solid_native::worklets::
|
|
4027
|
+
MaximumUIWorkletDecayVelocity ||
|
|
4028
|
+
std::abs(yVelocity) >
|
|
4029
|
+
solid_native::worklets::
|
|
4030
|
+
MaximumUIWorkletDecayVelocity) {
|
|
4031
|
+
throw std::invalid_argument(
|
|
4032
|
+
"The iOS native UI worklet pan velocity is outside "
|
|
4033
|
+
"its bounded range.");
|
|
4034
|
+
}
|
|
4035
|
+
std::vector<double> velocities(state->inputs.size(), 0);
|
|
4036
|
+
velocities.at(pan.definition.xInput) = xVelocity;
|
|
4037
|
+
velocities.at(pan.definition.yInput) = yVelocity;
|
|
4038
|
+
releaseSpeed =
|
|
4039
|
+
std::max(std::abs(xVelocity), std::abs(yVelocity));
|
|
4040
|
+
const auto &definition = *pan.definition.releaseDecay;
|
|
4041
|
+
const auto terminal = definition.evaluate(
|
|
4042
|
+
releaseSpeed, definition.maximumDurationMilliseconds);
|
|
4043
|
+
auto terminalInputs = nextInputs;
|
|
4044
|
+
for (size_t index = 0; index < terminalInputs.size();
|
|
4045
|
+
++index) {
|
|
4046
|
+
terminalInputs.at(index) +=
|
|
4047
|
+
velocities.at(index) *
|
|
4048
|
+
terminal.displacementFactorSeconds;
|
|
4049
|
+
}
|
|
4050
|
+
// Reject a terminal graph failure before publishing the
|
|
4051
|
+
// pan.
|
|
4052
|
+
state->graph.evaluate(terminalInputs);
|
|
4053
|
+
releaseDecay = IOSUIWorkletDecayState{
|
|
4054
|
+
.from = nextInputs,
|
|
4055
|
+
.velocities = std::move(velocities),
|
|
4056
|
+
.initialSpeed = releaseSpeed,
|
|
4057
|
+
.definition = definition,
|
|
4058
|
+
};
|
|
4059
|
+
}
|
|
4060
|
+
std::array<double, IOSViewOutputCount> values;
|
|
4061
|
+
values.fill(std::numeric_limits<double>::quiet_NaN());
|
|
4062
|
+
for (size_t index = 0; index < nextOutputs.size(); ++index) {
|
|
4063
|
+
values.at(static_cast<size_t>(state->channels.at(index))) =
|
|
4064
|
+
nextOutputs.at(index).value;
|
|
4065
|
+
}
|
|
4066
|
+
state->inputs = std::move(nextInputs);
|
|
4067
|
+
state->outputs = std::move(nextOutputs);
|
|
4068
|
+
state->timing.reset();
|
|
4069
|
+
state->timingProgress = 0;
|
|
4070
|
+
state->spring.reset();
|
|
4071
|
+
if (state->springPosition.has_value()) {
|
|
4072
|
+
state->springPosition = 0;
|
|
4073
|
+
}
|
|
4074
|
+
if (state->springVelocity.has_value()) {
|
|
4075
|
+
state->springVelocity = 0;
|
|
4076
|
+
}
|
|
4077
|
+
state->decay = std::move(releaseDecay);
|
|
4078
|
+
if (state->decay.has_value()) {
|
|
4079
|
+
state->decayElapsedMilliseconds = 0;
|
|
4080
|
+
state->decaySpeed = releaseSpeed;
|
|
4081
|
+
state->decayFrameStatistics.reset();
|
|
4082
|
+
scheduleReleaseDecay = YES;
|
|
4083
|
+
} else {
|
|
4084
|
+
if (state->decayElapsedMilliseconds.has_value()) {
|
|
4085
|
+
state->decayElapsedMilliseconds = 0;
|
|
4086
|
+
}
|
|
4087
|
+
if (state->decaySpeed.has_value())
|
|
4088
|
+
state->decaySpeed = 0;
|
|
4089
|
+
}
|
|
4090
|
+
state->sequence++;
|
|
4091
|
+
state->frameTimeNanoseconds =
|
|
4092
|
+
static_cast<int64_t>(std::llround(timestamp * 1000000.0));
|
|
4093
|
+
state->applied = [strongBinding applyUIWorkletFrame:*state
|
|
4094
|
+
values:values];
|
|
4095
|
+
if (state->applied) {
|
|
4096
|
+
state->appliedSequence = state->sequence;
|
|
4097
|
+
}
|
|
4098
|
+
pan.sequence++;
|
|
4099
|
+
pan.timestamp = timestamp;
|
|
4100
|
+
if (phase ==
|
|
4101
|
+
solid_native::worklets::UIWorkletPanGesturePhase::End) {
|
|
4102
|
+
pan.active = false;
|
|
4103
|
+
}
|
|
4104
|
+
} catch (...) {
|
|
4105
|
+
pan.active = false;
|
|
4106
|
+
state->timing.reset();
|
|
4107
|
+
state->timingProgress = 0;
|
|
4108
|
+
state->spring.reset();
|
|
4109
|
+
if (state->springPosition.has_value()) {
|
|
4110
|
+
state->springPosition = 0;
|
|
4111
|
+
}
|
|
4112
|
+
if (state->springVelocity.has_value()) {
|
|
4113
|
+
state->springVelocity = 0;
|
|
4114
|
+
}
|
|
4115
|
+
state->decay.reset();
|
|
4116
|
+
if (state->decayElapsedMilliseconds.has_value()) {
|
|
4117
|
+
state->decayElapsedMilliseconds = 0;
|
|
4118
|
+
}
|
|
4119
|
+
if (state->decaySpeed.has_value())
|
|
4120
|
+
state->decaySpeed = 0;
|
|
4121
|
+
state->applied = false;
|
|
4122
|
+
}
|
|
4123
|
+
}
|
|
4124
|
+
if (scheduleReleaseDecay) {
|
|
4125
|
+
[strongBinding scheduleUIWorkletFrame:state];
|
|
4126
|
+
}
|
|
4127
|
+
}];
|
|
4128
|
+
{
|
|
4129
|
+
std::lock_guard lock(worklet->mutex);
|
|
4130
|
+
if (!worklet->active || !worklet->pan.has_value() ||
|
|
4131
|
+
worklet->panObserver != nil) {
|
|
4132
|
+
[observer invalidate];
|
|
4133
|
+
error = "The iOS native UI worklet pan attachment was cancelled.";
|
|
4134
|
+
return;
|
|
4135
|
+
}
|
|
4136
|
+
worklet->panObserver = observer;
|
|
4137
|
+
}
|
|
4138
|
+
};
|
|
4139
|
+
if (NSThread.isMainThread) {
|
|
4140
|
+
attach();
|
|
4141
|
+
} else {
|
|
4142
|
+
dispatch_sync(dispatch_get_main_queue(), attach);
|
|
4143
|
+
}
|
|
4144
|
+
if (!error.empty()) throw std::runtime_error(error);
|
|
4145
|
+
}
|
|
4146
|
+
|
|
4147
|
+
- (BOOL)applyUIWorkletFrame:(const IOSUIWorkletState &)worklet
|
|
4148
|
+
values:
|
|
4149
|
+
(const std::array<double, IOSViewOutputCount> &)values {
|
|
4150
|
+
NSAssert(
|
|
4151
|
+
NSThread.isMainThread,
|
|
4152
|
+
@"Native UI worklet frames must run on the main thread.");
|
|
4153
|
+
for (const auto value : values) {
|
|
4154
|
+
if (!std::isnan(value) && !std::isfinite(value)) return NO;
|
|
4155
|
+
}
|
|
4156
|
+
const auto opacity = values.at(static_cast<size_t>(IOSViewOutput::Opacity));
|
|
4157
|
+
if (!std::isnan(opacity) && (opacity < 0 || opacity > 1)) return NO;
|
|
4158
|
+
|
|
4159
|
+
[_lock lock];
|
|
4160
|
+
auto surface = (!_invalidated && _surfaceReady) ? _surface : nil;
|
|
4161
|
+
[_lock unlock];
|
|
4162
|
+
if (surface == nil) return NO;
|
|
4163
|
+
auto view = [surface findComponentViewWithTag:worklet.targetTag];
|
|
4164
|
+
if (view == nil || view.window == nil) return NO;
|
|
4165
|
+
|
|
4166
|
+
if (!std::isnan(opacity)) view.alpha = static_cast<CGFloat>(opacity);
|
|
4167
|
+
const auto translateX =
|
|
4168
|
+
values.at(static_cast<size_t>(IOSViewOutput::TranslateX));
|
|
4169
|
+
const auto translateY =
|
|
4170
|
+
values.at(static_cast<size_t>(IOSViewOutput::TranslateY));
|
|
4171
|
+
const auto scaleX = values.at(static_cast<size_t>(IOSViewOutput::ScaleX));
|
|
4172
|
+
const auto scaleY = values.at(static_cast<size_t>(IOSViewOutput::ScaleY));
|
|
4173
|
+
const auto rotation =
|
|
4174
|
+
values.at(static_cast<size_t>(IOSViewOutput::Rotation));
|
|
4175
|
+
const bool ownsTransform = !std::isnan(translateX) ||
|
|
4176
|
+
!std::isnan(translateY) || !std::isnan(scaleX) ||
|
|
4177
|
+
!std::isnan(scaleY) || !std::isnan(rotation);
|
|
4178
|
+
if (ownsTransform) {
|
|
4179
|
+
auto transform = CGAffineTransformIdentity;
|
|
4180
|
+
transform = CGAffineTransformTranslate(
|
|
4181
|
+
transform,
|
|
4182
|
+
static_cast<CGFloat>(std::isnan(translateX) ? 0 : translateX),
|
|
4183
|
+
static_cast<CGFloat>(std::isnan(translateY) ? 0 : translateY));
|
|
4184
|
+
transform = CGAffineTransformScale(
|
|
4185
|
+
transform,
|
|
4186
|
+
static_cast<CGFloat>(std::isnan(scaleX) ? 1 : scaleX),
|
|
4187
|
+
static_cast<CGFloat>(std::isnan(scaleY) ? 1 : scaleY));
|
|
4188
|
+
transform = CGAffineTransformRotate(
|
|
4189
|
+
transform,
|
|
4190
|
+
static_cast<CGFloat>(
|
|
4191
|
+
(std::isnan(rotation) ? 0 : rotation) * M_PI / 180.0));
|
|
4192
|
+
view.transform = transform;
|
|
4193
|
+
if (!CGAffineTransformEqualToTransform(view.transform, transform)) {
|
|
4194
|
+
return NO;
|
|
4195
|
+
}
|
|
4196
|
+
}
|
|
4197
|
+
if (!std::isnan(opacity) &&
|
|
4198
|
+
std::abs(static_cast<double>(view.alpha) - opacity) > 0.000001) {
|
|
4199
|
+
return NO;
|
|
4200
|
+
}
|
|
4201
|
+
return YES;
|
|
4202
|
+
}
|
|
4203
|
+
|
|
4204
|
+
- (void)scheduleMountObservation:
|
|
4205
|
+
(std::shared_ptr<const CommitMountObservation>)observation {
|
|
4206
|
+
[_lock lock];
|
|
4207
|
+
if (_invalidated || _surface == nil || !_runtimeScheduler) {
|
|
4208
|
+
[_lock unlock];
|
|
4209
|
+
return;
|
|
4210
|
+
}
|
|
4211
|
+
auto surface = _surface;
|
|
4212
|
+
auto runtimeScheduler = _runtimeScheduler;
|
|
4213
|
+
auto runtimeState = _runtimeState;
|
|
4214
|
+
auto routing = _routing;
|
|
4215
|
+
[_lock unlock];
|
|
4216
|
+
|
|
4217
|
+
__weak SolidNativeFabricJSIBinding *weakBinding = self;
|
|
4218
|
+
dispatch_async(dispatch_get_main_queue(), ^{
|
|
4219
|
+
[weakBinding pollMountObservation:observation
|
|
4220
|
+
surface:surface
|
|
4221
|
+
runtimeScheduler:runtimeScheduler
|
|
4222
|
+
runtimeState:runtimeState
|
|
4223
|
+
routing:routing
|
|
4224
|
+
attempt:0];
|
|
4225
|
+
});
|
|
4226
|
+
}
|
|
4227
|
+
|
|
4228
|
+
- (void)pollMountObservation:
|
|
4229
|
+
(std::shared_ptr<const CommitMountObservation>)observation
|
|
4230
|
+
surface:(SolidNativeFabricSurface *)surface
|
|
4231
|
+
runtimeScheduler:
|
|
4232
|
+
(std::shared_ptr<facebook::react::RuntimeScheduler>)
|
|
4233
|
+
runtimeScheduler
|
|
4234
|
+
runtimeState:(std::weak_ptr<RuntimeState>)runtimeState
|
|
4235
|
+
routing:(std::shared_ptr<EventRoutingState>)routing
|
|
4236
|
+
attempt:(NSUInteger)attempt {
|
|
4237
|
+
[_lock lock];
|
|
4238
|
+
const BOOL active = !_invalidated && _surface == surface;
|
|
4239
|
+
[_lock unlock];
|
|
4240
|
+
if (!active) return;
|
|
4241
|
+
|
|
4242
|
+
const auto mountedFabricRevision = [surface mountedFabricRevision];
|
|
4243
|
+
if (mountedFabricRevision >= observation->fabricRevision) {
|
|
4244
|
+
const auto mountedAt = [NSDate date].timeIntervalSince1970 * 1000.0;
|
|
4245
|
+
const auto mountedMonotonic = CACurrentMediaTime() * 1000.0;
|
|
4246
|
+
const auto mountLatency = std::max(
|
|
4247
|
+
0.0,
|
|
4248
|
+
mountedMonotonic - observation->commitStartedMonotonic);
|
|
4249
|
+
runtimeScheduler->scheduleWork(
|
|
4250
|
+
[runtimeState,
|
|
4251
|
+
routing,
|
|
4252
|
+
observation,
|
|
4253
|
+
mountedFabricRevision,
|
|
4254
|
+
mountedAt,
|
|
4255
|
+
mountLatency](Runtime &runtime) {
|
|
4256
|
+
auto state = runtimeState.lock();
|
|
4257
|
+
if (!state) return;
|
|
4258
|
+
state->reclaimMountedInstances(
|
|
4259
|
+
runtime, mountedFabricRevision, routing);
|
|
4260
|
+
if (!observation->notifyLifecycle ||
|
|
4261
|
+
!routing->active.load(std::memory_order_acquire) ||
|
|
4262
|
+
!state->hasCommitLifecycleHandler()) {
|
|
4263
|
+
return;
|
|
4264
|
+
}
|
|
4265
|
+
auto handlerValue = state->commitLifecycleHandler(runtime);
|
|
4266
|
+
if (!handlerValue.isObject()) return;
|
|
4267
|
+
auto handlerObject = handlerValue.getObject(runtime);
|
|
4268
|
+
if (!handlerObject.isFunction(runtime)) return;
|
|
4269
|
+
|
|
4270
|
+
Object event(runtime);
|
|
4271
|
+
event.setProperty(
|
|
4272
|
+
runtime,
|
|
4273
|
+
"type",
|
|
4274
|
+
String::createFromAscii(runtime, "commit-mounted"));
|
|
4275
|
+
event.setProperty(
|
|
4276
|
+
runtime,
|
|
4277
|
+
"surface",
|
|
4278
|
+
static_cast<double>(observation->surface));
|
|
4279
|
+
event.setProperty(
|
|
4280
|
+
runtime,
|
|
4281
|
+
"sequence",
|
|
4282
|
+
static_cast<double>(observation->sequence));
|
|
4283
|
+
event.setProperty(
|
|
4284
|
+
runtime,
|
|
4285
|
+
"hostRevision",
|
|
4286
|
+
static_cast<double>(observation->fabricRevision));
|
|
4287
|
+
event.setProperty(
|
|
4288
|
+
runtime, "commitStartedAt", observation->commitStartedAt);
|
|
4289
|
+
event.setProperty(runtime, "mountedAt", mountedAt);
|
|
4290
|
+
event.setProperty(runtime, "mountLatency", mountLatency);
|
|
4291
|
+
if (observation->causalOperationId.has_value()) {
|
|
4292
|
+
Object causalContext(runtime);
|
|
4293
|
+
causalContext.setProperty(
|
|
4294
|
+
runtime,
|
|
4295
|
+
"operationId",
|
|
4296
|
+
String::createFromUtf8(
|
|
4297
|
+
runtime, *observation->causalOperationId));
|
|
4298
|
+
event.setProperty(
|
|
4299
|
+
runtime, "causalContext", std::move(causalContext));
|
|
4300
|
+
}
|
|
4301
|
+
auto handler = handlerObject.getFunction(runtime);
|
|
4302
|
+
handler.call(runtime, event);
|
|
4303
|
+
});
|
|
4304
|
+
if (observation->notifyLifecycle) {
|
|
4305
|
+
auto frameObserver = [[SolidNativeDisplayLinkObserver alloc]
|
|
4306
|
+
initWithCallback:^(CADisplayLink *) {
|
|
4307
|
+
const auto frameStartedAt =
|
|
4308
|
+
[NSDate date].timeIntervalSince1970 * 1000.0;
|
|
4309
|
+
const auto frameStartedMonotonic =
|
|
4310
|
+
CACurrentMediaTime() * 1000.0;
|
|
4311
|
+
const auto frameLatency = std::max(
|
|
4312
|
+
0.0,
|
|
4313
|
+
frameStartedMonotonic -
|
|
4314
|
+
observation->commitStartedMonotonic);
|
|
4315
|
+
const auto mountToFrameLatency = std::max(
|
|
4316
|
+
0.0, frameStartedMonotonic - mountedMonotonic);
|
|
4317
|
+
runtimeScheduler->scheduleWork(
|
|
4318
|
+
[runtimeState,
|
|
4319
|
+
routing,
|
|
4320
|
+
observation,
|
|
4321
|
+
mountedAt,
|
|
4322
|
+
frameStartedAt,
|
|
4323
|
+
frameLatency,
|
|
4324
|
+
mountToFrameLatency](Runtime &runtime) {
|
|
4325
|
+
auto state = runtimeState.lock();
|
|
4326
|
+
if (!state ||
|
|
4327
|
+
!routing->active.load(std::memory_order_acquire) ||
|
|
4328
|
+
!state->hasCommitLifecycleHandler()) {
|
|
4329
|
+
return;
|
|
4330
|
+
}
|
|
4331
|
+
auto handlerValue =
|
|
4332
|
+
state->commitLifecycleHandler(runtime);
|
|
4333
|
+
if (!handlerValue.isObject()) return;
|
|
4334
|
+
auto handlerObject = handlerValue.getObject(runtime);
|
|
4335
|
+
if (!handlerObject.isFunction(runtime)) return;
|
|
4336
|
+
|
|
4337
|
+
Object event(runtime);
|
|
4338
|
+
event.setProperty(
|
|
4339
|
+
runtime,
|
|
4340
|
+
"type",
|
|
4341
|
+
String::createFromAscii(runtime, "commit-frame"));
|
|
4342
|
+
event.setProperty(
|
|
4343
|
+
runtime,
|
|
4344
|
+
"surface",
|
|
4345
|
+
static_cast<double>(observation->surface));
|
|
4346
|
+
event.setProperty(
|
|
4347
|
+
runtime,
|
|
4348
|
+
"sequence",
|
|
4349
|
+
static_cast<double>(observation->sequence));
|
|
4350
|
+
event.setProperty(
|
|
4351
|
+
runtime,
|
|
4352
|
+
"hostRevision",
|
|
4353
|
+
static_cast<double>(observation->fabricRevision));
|
|
4354
|
+
event.setProperty(runtime, "mountedAt", mountedAt);
|
|
4355
|
+
event.setProperty(
|
|
4356
|
+
runtime, "frameStartedAt", frameStartedAt);
|
|
4357
|
+
event.setProperty(
|
|
4358
|
+
runtime, "frameLatency", frameLatency);
|
|
4359
|
+
event.setProperty(
|
|
4360
|
+
runtime,
|
|
4361
|
+
"mountToFrameLatency",
|
|
4362
|
+
mountToFrameLatency);
|
|
4363
|
+
if (observation->causalOperationId.has_value()) {
|
|
4364
|
+
Object causalContext(runtime);
|
|
4365
|
+
causalContext.setProperty(
|
|
4366
|
+
runtime,
|
|
4367
|
+
"operationId",
|
|
4368
|
+
String::createFromUtf8(
|
|
4369
|
+
runtime,
|
|
4370
|
+
*observation->causalOperationId));
|
|
4371
|
+
event.setProperty(
|
|
4372
|
+
runtime,
|
|
4373
|
+
"causalContext",
|
|
4374
|
+
std::move(causalContext));
|
|
4375
|
+
}
|
|
4376
|
+
handlerObject.getFunction(runtime).call(runtime, event);
|
|
4377
|
+
});
|
|
4378
|
+
return NO;
|
|
4379
|
+
}];
|
|
4380
|
+
[frameObserver start];
|
|
4381
|
+
}
|
|
4382
|
+
return;
|
|
4383
|
+
}
|
|
4384
|
+
|
|
4385
|
+
if (attempt + 1 >= MaximumMountObservationAttempts) {
|
|
4386
|
+
NSLog(
|
|
4387
|
+
@"Solid Native timed out waiting for Fabric revision %lld on surface "
|
|
4388
|
+
"%lld.",
|
|
4389
|
+
static_cast<long long>(observation->fabricRevision),
|
|
4390
|
+
static_cast<long long>(observation->surface));
|
|
4391
|
+
runtimeScheduler->scheduleWork(
|
|
4392
|
+
[runtimeState, routing, mountedFabricRevision](Runtime &runtime) {
|
|
4393
|
+
auto state = runtimeState.lock();
|
|
4394
|
+
if (!state) return;
|
|
4395
|
+
state->reclaimMountedInstances(
|
|
4396
|
+
runtime, mountedFabricRevision, routing);
|
|
4397
|
+
});
|
|
4398
|
+
return;
|
|
4399
|
+
}
|
|
4400
|
+
|
|
4401
|
+
__weak SolidNativeFabricJSIBinding *weakBinding = self;
|
|
4402
|
+
dispatch_after(
|
|
4403
|
+
dispatch_time(
|
|
4404
|
+
DISPATCH_TIME_NOW, MountObservationRetryNanoseconds),
|
|
4405
|
+
dispatch_get_main_queue(),
|
|
4406
|
+
^{
|
|
4407
|
+
[weakBinding pollMountObservation:observation
|
|
4408
|
+
surface:surface
|
|
4409
|
+
runtimeScheduler:runtimeScheduler
|
|
4410
|
+
runtimeState:runtimeState
|
|
4411
|
+
routing:routing
|
|
4412
|
+
attempt:attempt + 1];
|
|
4413
|
+
});
|
|
4414
|
+
}
|
|
4415
|
+
|
|
4416
|
+
- (BOOL)destroySurface:(NSError **)error {
|
|
4417
|
+
[_lock lock];
|
|
4418
|
+
if (_invalidated || !_surfaceReady || _surface == nil) {
|
|
4419
|
+
if (error != nullptr) {
|
|
4420
|
+
*error = [NSError
|
|
4421
|
+
errorWithDomain:@"SolidNativeFabricJSIBinding"
|
|
4422
|
+
code:1
|
|
4423
|
+
userInfo:@{
|
|
4424
|
+
NSLocalizedDescriptionKey :
|
|
4425
|
+
@"The Solid Native Fabric surface is not ready."
|
|
4426
|
+
}];
|
|
4427
|
+
}
|
|
4428
|
+
[_lock unlock];
|
|
4429
|
+
return NO;
|
|
4430
|
+
}
|
|
4431
|
+
if (![_surface hasNoLogicalNodes]) {
|
|
4432
|
+
if (error != nullptr) {
|
|
4433
|
+
*error = [NSError
|
|
4434
|
+
errorWithDomain:@"SolidNativeFabricJSIBinding"
|
|
4435
|
+
code:2
|
|
4436
|
+
userInfo:@{
|
|
4437
|
+
NSLocalizedDescriptionKey :
|
|
4438
|
+
@"Cannot destroy a Fabric surface with live nodes."
|
|
4439
|
+
}];
|
|
4440
|
+
}
|
|
4441
|
+
[_lock unlock];
|
|
4442
|
+
return NO;
|
|
4443
|
+
}
|
|
4444
|
+
|
|
4445
|
+
auto surface = _surface;
|
|
4446
|
+
auto eventSurface = _eventSurface;
|
|
4447
|
+
auto eventListener = _eventListener;
|
|
4448
|
+
_invalidated = YES;
|
|
4449
|
+
_surfaceReady = NO;
|
|
4450
|
+
_surface = nil;
|
|
4451
|
+
_eventSurface = nil;
|
|
4452
|
+
_statusBarStyleHandler = nil;
|
|
4453
|
+
_statusBarHiddenHandler = nil;
|
|
4454
|
+
_routing->active.store(false, std::memory_order_release);
|
|
4455
|
+
_routing->handlerInstalled.store(false, std::memory_order_release);
|
|
4456
|
+
_runtimeState.reset();
|
|
4457
|
+
_runtimeScheduler.reset();
|
|
4458
|
+
_eventListener.reset();
|
|
4459
|
+
[_lock unlock];
|
|
4460
|
+
|
|
4461
|
+
if (eventSurface != nil && eventListener) {
|
|
4462
|
+
[eventSurface removeSolidNativeEventListener:eventListener];
|
|
4463
|
+
}
|
|
4464
|
+
[surface stop];
|
|
4465
|
+
return YES;
|
|
4466
|
+
}
|
|
4467
|
+
|
|
4468
|
+
- (void)invalidate {
|
|
4469
|
+
[_lock lock];
|
|
4470
|
+
auto eventSurface = _eventSurface;
|
|
4471
|
+
auto eventListener = _eventListener;
|
|
4472
|
+
auto runtimeState = _runtimeState.lock();
|
|
4473
|
+
_invalidated = YES;
|
|
4474
|
+
_surfaceReady = NO;
|
|
4475
|
+
_surface = nil;
|
|
4476
|
+
_eventSurface = nil;
|
|
4477
|
+
_statusBarStyleHandler = nil;
|
|
4478
|
+
_statusBarHiddenHandler = nil;
|
|
4479
|
+
_routing->active.store(false, std::memory_order_release);
|
|
4480
|
+
_routing->handlerInstalled.store(false, std::memory_order_release);
|
|
4481
|
+
_runtimeState.reset();
|
|
4482
|
+
_runtimeScheduler.reset();
|
|
4483
|
+
_eventListener.reset();
|
|
4484
|
+
[_lock unlock];
|
|
4485
|
+
if (runtimeState) runtimeState->cancelAllUIWorklets();
|
|
4486
|
+
if (eventSurface != nil && eventListener) {
|
|
4487
|
+
[eventSurface removeSolidNativeEventListener:eventListener];
|
|
4488
|
+
}
|
|
4489
|
+
}
|
|
4490
|
+
|
|
4491
|
+
@end
|