-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathJSIWorkletsModuleProxy.cpp
More file actions
637 lines (562 loc) · 27.1 KB
/
Copy pathJSIWorkletsModuleProxy.cpp
File metadata and controls
637 lines (562 loc) · 27.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
#include <jsi/jsi.h>
#include <react/renderer/uimanager/UIManagerBinding.h>
#include <react/renderer/uimanager/primitives.h>
#include <worklets/Compat/Holders.h>
#include <worklets/NativeModules/JSIWorkletsModuleProxy.h>
#include <worklets/NativeModules/WorkletsModuleProxy.h>
#include <worklets/SharedItems/Serializable.h>
#include <worklets/SharedItems/SerializableFactory.h>
#include <worklets/SharedItems/Shareable.h>
#include <worklets/SharedItems/Synchronizable.h>
#include <worklets/Tools/Defs.h>
#include <worklets/Tools/FeatureFlags.h>
#include <worklets/Tools/JSLogger.h>
#include <worklets/WorkletRuntime/BundleModeConfig.h>
#include <worklets/WorkletRuntime/UIRuntimeDecorator.h>
#include <memory>
#include <string>
#include <utility>
#include <vector>
using namespace facebook;
namespace worklets {
inline void scheduleOnUI(
const std::shared_ptr<UIScheduler> &uiScheduler,
const std::weak_ptr<WorkletRuntime> &weakUIWorkletRuntime,
jsi::Runtime &rt,
const jsi::Value &worklet) {
auto serializableWorklet = extractSerializableOrThrow<SerializableWorklet>(
rt, worklet, "[Worklets] Only worklets can be scheduled to run on UI.");
uiScheduler->scheduleOnUI([serializableWorklet, weakUIWorkletRuntime]() {
// This callback can outlive the WorkletsModuleProxy object during the
// invalidation of React Native. This happens when WorkletsModuleProxy
// destructor is called on the JS thread and the UI thread is
// executing callbacks from the `scheduleOnUI` queue. Therefore, we
// need to make sure it's still alive before we try to access it.
auto uiWorkletRuntime = weakUIWorkletRuntime.lock();
if (!uiWorkletRuntime) {
return;
}
#if JS_RUNTIME_HERMES
// JSI's scope defined here allows for JSI-objects to be cleared up
// after each runtime loop. Within these loops we typically create
// some temporary JSI objects and hence it allows for such objects to
// be garbage collected much sooner. Apparently the scope API is only
// supported on Hermes at the moment.
const auto scope = jsi::Scope(uiWorkletRuntime->getJSIRuntime());
#endif // JS_RUNTIME_HERMES
uiWorkletRuntime->runSync(serializableWorklet);
});
}
inline jsi::Value
runOnUISync(const std::weak_ptr<WorkletRuntime> &weakUIWorkletRuntime, jsi::Runtime &rt, const jsi::Value &worklet) {
if (auto uiWorkletRuntime = weakUIWorkletRuntime.lock()) {
auto serializableWorklet = extractSerializableOrThrow<SerializableWorklet>(
rt, worklet, "[Worklets] Only worklets can be executed on UI runtime.");
auto serializedResult = uiWorkletRuntime->runSyncSerialized(serializableWorklet);
return serializedResult->toJSValue(rt);
}
return jsi::Value::undefined();
}
jsi::Value
runOnRuntimeSync(jsi::Runtime &rt, const jsi::Value &workletRuntimeValue, const jsi::Value &serializableWorkletValue) {
auto workletRuntime = workletRuntimeValue.getObject(rt).getHostObject<WorkletRuntime>(rt);
auto worklet = extractSerializableOrThrow<SerializableWorklet>(
rt, serializableWorkletValue, "[Worklets] Only worklets can be executed on a worklet runtime.");
return workletRuntime->runSyncSerialized(worklet)->toJSValue(rt);
}
inline jsi::Value createWorkletRuntime(
jsi::Runtime &originRuntime,
const std::shared_ptr<RuntimeManager> &runtimeManager,
const std::shared_ptr<MessageQueueThread> &jsQueue,
std::shared_ptr<JSIWorkletsModuleProxy> jsiWorkletsModuleProxy,
const std::string &name,
std::shared_ptr<SerializableWorklet> &initializer,
const std::shared_ptr<AsyncQueue> &queue,
bool enableEventLoop) {
const auto workletRuntime =
runtimeManager->createWorkletRuntime(std::move(jsiWorkletsModuleProxy), name, initializer, queue);
return jsi::Object::createFromHostObject(originRuntime, workletRuntime);
}
inline jsi::Value propagateModuleUpdate(
const std::shared_ptr<RuntimeManager> &runtimeManager,
const std::string &code,
const std::string &sourceUrl) {
const auto runtimes = runtimeManager->getAllRuntimes();
for (const auto &runtime : runtimes) {
runtime->runSync([code, sourceUrl](jsi::Runtime &rt) -> void {
const auto buffer = std::make_shared<jsi::StringBuffer>(code);
rt.evaluateJavaScript(buffer, sourceUrl);
});
}
return jsi::Value::undefined();
}
inline jsi::Value reportFatalErrorOnJS(
const std::shared_ptr<JSScheduler> &jsScheduler,
const std::string &message,
const std::string &stack,
const std::string &name,
const std::string &jsEngine) {
JSLogger::reportFatalErrorOnJS(
jsScheduler, JSErrorData{.message = message, .stack = stack, .name = name, .jsEngine = jsEngine});
return jsi::Value::undefined();
}
inline std::shared_ptr<AsyncQueue> extractAsyncQueue(jsi::Runtime &rt, const jsi::Value &value) {
if (!value.isObject()) {
return nullptr;
}
const auto object = value.asObject(rt);
if (!object.hasNativeState(rt)) {
return nullptr;
}
const auto &nativeState = object.getNativeState(rt);
auto asyncQueue = std::dynamic_pointer_cast<AsyncQueue>(nativeState);
return asyncQueue;
}
inline void registerCustomSerializable(
const std::shared_ptr<RuntimeManager> &runtimeManager,
const std::shared_ptr<MemoryManager> &memoryManager,
const std::shared_ptr<SerializableWorklet> &determine,
const std::shared_ptr<SerializableWorklet> &pack,
const std::shared_ptr<SerializableWorklet> &unpack,
const int typeId) {
const SerializationData data{.determine = determine, .pack = pack, .unpack = unpack, .typeId = typeId};
// Prevent registering new worklet runtimes while we are updating existing ones to prevent inconsistencies.
runtimeManager->pause();
memoryManager->registerCustomSerializable(data);
for (const auto &runtime : runtimeManager->getAllRuntimes()) {
memoryManager->loadCustomSerializable(runtime, data);
}
runtimeManager->resume();
}
JSIWorkletsModuleProxy::JSIWorkletsModuleProxy(
const bool isDevBundle,
const std::shared_ptr<MessageQueueThread> &jsQueue,
const std::shared_ptr<JSScheduler> &jsScheduler,
const std::shared_ptr<UIScheduler> &uiScheduler,
const std::shared_ptr<MemoryManager> &memoryManager,
const std::shared_ptr<RuntimeManager> &runtimeManager,
const std::weak_ptr<WorkletRuntime> &uiWorkletRuntime,
const std::shared_ptr<RuntimeBindings> &runtimeBindings,
const BundleModeConfig &bundleModeConfig)
: jsi::HostObject(),
isDevBundle_(isDevBundle),
bundleModeConfig_(bundleModeConfig),
jsQueue_(jsQueue),
jsScheduler_(jsScheduler),
uiScheduler_(uiScheduler),
memoryManager_(memoryManager),
runtimeManager_(runtimeManager),
uiWorkletRuntime_(uiWorkletRuntime),
runtimeBindings_(runtimeBindings) {}
JSIWorkletsModuleProxy::~JSIWorkletsModuleProxy() = default;
std::vector<jsi::PropNameID> JSIWorkletsModuleProxy::getPropertyNames(jsi::Runtime &rt) {
std::vector<jsi::PropNameID> propertyNames;
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createSerializable"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createSerializableBigInt"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createSerializableBoolean"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createSerializableImport"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createSerializableNull"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createSerializableNumber"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createSerializableString"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createSerializableUndefined"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createSerializableHostObject"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createSerializableInitializer"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createSerializableArray"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createSerializableFunction"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createSerializableTurboModuleLike"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createSerializableObject"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createSerializableMap"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createSerializableSet"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createSerializableWorklet"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createCustomSerializable"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "registerCustomSerializable"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "scheduleOnUI"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "runOnUISync"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "runOnRuntimeSync"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "runOnRuntimeSyncWithId"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createWorkletRuntime"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "scheduleOnRuntime"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "scheduleOnRuntimeWithId"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "reportFatalErrorOnJS"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "getStaticFeatureFlag"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "setDynamicFeatureFlag"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createSynchronizable"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "synchronizableGetDirty"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "synchronizableGetBlocking"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "synchronizableSetBlocking"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "synchronizableLock"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "synchronizableUnlock"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createShareable"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "propagateModuleUpdate"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "getUIRuntimeHolder"));
propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "getUISchedulerHolder"));
return propertyNames;
}
jsi::Value JSIWorkletsModuleProxy::get(jsi::Runtime &rt, const jsi::PropNameID &propName) {
const auto name = propName.utf8(rt);
if (name == "createSerializable") {
return jsi::Function::createFromHostFunction(
rt, propName, 3, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return makeSerializableClone(rt, args[0], args[1], args[2]);
});
}
if (name == "createSerializableBigInt") {
return jsi::Function::createFromHostFunction(
rt, propName, 1, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return makeSerializableBigInt(rt, args[0].asBigInt(rt));
});
}
if (name == "createSerializableBoolean") {
return jsi::Function::createFromHostFunction(
rt, propName, 1, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return makeSerializableBoolean(rt, args[0].asBool());
});
}
if (name == "createSerializableImport") {
return jsi::Function::createFromHostFunction(
rt, propName, 2, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return makeSerializableImport(rt, args[0].asNumber(), args[1].asString(rt));
});
}
if (name == "createSerializableNumber") {
return jsi::Function::createFromHostFunction(
rt, propName, 1, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return makeSerializableNumber(rt, args[0].asNumber());
});
}
if (name == "createSerializableNull") {
return jsi::Function::createFromHostFunction(
rt, propName, 0, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return makeSerializableNull(rt);
});
}
if (name == "createSerializableString") {
return jsi::Function::createFromHostFunction(
rt, propName, 1, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return makeSerializableString(rt, args[0].asString(rt));
});
}
if (name == "createSerializableUndefined") {
return jsi::Function::createFromHostFunction(
rt, propName, 0, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return makeSerializableUndefined(rt);
});
}
if (name == "createSerializableInitializer") {
return jsi::Function::createFromHostFunction(
rt, propName, 1, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return makeSerializableInitializer(rt, args[0].asObject(rt));
});
}
if (name == "createSerializableArray") {
return jsi::Function::createFromHostFunction(
rt, propName, 2, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return makeSerializableArray(rt, args[0].asObject(rt).asArray(rt), args[1]);
});
}
if (name == "createSerializableMap") {
return jsi::Function::createFromHostFunction(
rt, propName, 2, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return makeSerializableMap(rt, args[0].asObject(rt).asArray(rt), args[1].asObject(rt).asArray(rt));
});
}
if (name == "createSerializableSet") {
return jsi::Function::createFromHostFunction(
rt, propName, 1, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return makeSerializableSet(rt, args[0].asObject(rt).asArray(rt));
});
}
if (name == "createSerializableHostObject") {
return jsi::Function::createFromHostFunction(
rt, propName, 1, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return makeSerializableHostObject(rt, args[0].asObject(rt).asHostObject(rt));
});
}
if (name == "createSerializableFunction") {
return jsi::Function::createFromHostFunction(
rt, propName, 1, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return makeSerializableFunction(rt, args[0].asObject(rt).asFunction(rt));
});
}
if (name == "createSerializableTurboModuleLike") {
return jsi::Function::createFromHostFunction(
rt, propName, 2, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return makeSerializableTurboModuleLike(rt, args[0].asObject(rt), args[1].asObject(rt).asHostObject(rt));
});
}
if (name == "createSerializableObject") {
return jsi::Function::createFromHostFunction(
rt, propName, 1, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return makeSerializableObject(rt, args[0].getObject(rt), args[1].getBool(), args[2]);
});
}
if (name == "createSerializableWorklet") {
return jsi::Function::createFromHostFunction(
rt, propName, 2, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return makeSerializableWorklet(rt, args[0].getObject(rt), args[1].getBool());
});
}
if (name == "createCustomSerializable") {
return jsi::Function::createFromHostFunction(
rt, propName, 2, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return makeCustomSerializable(rt, args[0], args[1].asNumber());
});
}
if (name == "registerCustomSerializable") {
return jsi::Function::createFromHostFunction(
rt,
propName,
4,
[memoryManager = memoryManager_, runtimeManager = runtimeManager_](
jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
const auto determine = extractSerializableOrThrow<SerializableWorklet>(
rt, args[0], "[Worklets] Determine function must be a worklet.");
const auto pack = extractSerializableOrThrow<SerializableWorklet>(
rt, args[1], "[Worklets] Pack function must be a worklet.");
const auto unpack = extractSerializableOrThrow<SerializableWorklet>(
rt, args[2], "[Worklets] Unpack function must be a worklet.");
const auto typeId = args[3].asNumber();
registerCustomSerializable(runtimeManager, memoryManager, determine, pack, unpack, typeId);
return jsi::Value::undefined();
});
}
if (name == "scheduleOnUI") {
return jsi::Function::createFromHostFunction(
rt,
propName,
1,
[uiScheduler = uiScheduler_, uiWorkletRuntime = uiWorkletRuntime_](
jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
scheduleOnUI(uiScheduler, uiWorkletRuntime, rt, args[0]);
return jsi::Value::undefined();
});
}
if (name == "runOnUISync") {
return jsi::Function::createFromHostFunction(
rt,
propName,
1,
[uiWorkletRuntime = uiWorkletRuntime_](
jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return runOnUISync(uiWorkletRuntime, rt, args[0]);
});
}
if (name == "runOnRuntimeSync") {
return jsi::Function::createFromHostFunction(
rt, propName, 2, [](jsi::Runtime &rt, const jsi ::Value &thisValue, const jsi::Value *args, size_t count) {
return runOnRuntimeSync(rt, args[0], args[1]);
});
}
if (name == "runOnRuntimeSyncWithId") {
return jsi::Function::createFromHostFunction(
rt,
propName,
2,
[runtimeManager = runtimeManager_](
jsi::Runtime &rt, const jsi ::Value &thisValue, const jsi::Value *args, size_t count) {
const int runtimeId = args[0].asNumber();
const auto workletRuntime = runtimeManager->getRuntime(runtimeId);
if (!workletRuntime) {
throw jsi::JSError(
rt, "[Worklets] runOnRuntimeSyncWithId: no worklet runtime found for id " + std::to_string(runtimeId));
}
auto serializableWorklet = extractSerializableOrThrow<SerializableWorklet>(
rt, args[1], "[Worklets] Only worklets can be executed on a worklet runtime.");
return workletRuntime->runSyncSerialized(serializableWorklet)->toJSValue(rt);
});
}
if (name == "createWorkletRuntime") {
return jsi::Function::createFromHostFunction(
rt,
propName,
5,
[clone = std::make_shared<JSIWorkletsModuleProxy>(*this)](
jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
const auto name = args[0].asString(rt).utf8(rt);
auto serializableInitializer =
extractSerializableOrThrow<SerializableWorklet>(rt, args[1], "[Worklets] Initializer must be a worklet.");
const auto useDefaultQueue = args[2].asBool();
std::shared_ptr<AsyncQueue> asyncQueue;
if (useDefaultQueue) {
asyncQueue = std::make_shared<AsyncQueueImpl>(name + "_queue");
} else {
asyncQueue = extractAsyncQueue(rt, args[3]);
}
const auto enableEventLoop = args[4].asBool();
return createWorkletRuntime(
rt,
clone->getRuntimeManager(),
clone->getJSQueue(),
clone,
name,
serializableInitializer,
asyncQueue,
enableEventLoop);
});
}
if (name == "scheduleOnRuntime") {
return jsi::Function::createFromHostFunction(
rt, propName, 2, [](jsi::Runtime &rt, const jsi ::Value &thisValue, const jsi::Value *args, size_t count) {
worklets::scheduleOnRuntime(rt, args[0], args[1]);
return jsi::Value::undefined();
});
}
if (name == "scheduleOnRuntimeWithId") {
return jsi::Function::createFromHostFunction(
rt,
propName,
2,
[runtimeManager = runtimeManager_](
jsi::Runtime &rt, const jsi ::Value &thisValue, const jsi::Value *args, size_t count) {
const int runtimeId = args[0].asNumber();
const auto workletRuntime = runtimeManager->getRuntime(runtimeId);
if (!workletRuntime) {
throw jsi::JSError(
rt, "[Worklets] scheduleOnRuntimeWithId: no worklet runtime found for id " + std::to_string(runtimeId));
}
const auto worklet = extractSerializableOrThrow<SerializableWorklet>(
rt, args[1], "[Worklets] Only worklets can be scheduled to run on a worklet runtime.");
workletRuntime->schedule(worklet);
return jsi::Value::undefined();
});
}
if (name == "reportFatalErrorOnJS") {
return jsi::Function::createFromHostFunction(
rt,
propName,
4,
[jsScheduler = jsScheduler_](
jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return reportFatalErrorOnJS(
jsScheduler,
/* message */ args[0].asString(rt).utf8(rt),
/* stack */ args[1].asString(rt).utf8(rt),
/* name */ args[2].asString(rt).utf8(rt),
/* jsEngine */ args[3].asString(rt).utf8(rt));
});
}
if (name == "createSynchronizable") {
return jsi::Function::createFromHostFunction(
rt, propName, 1, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
auto initial = extractSerializableOrThrow(rt, args[0], "[Worklets] Value must be a Serializable.");
auto synchronizable = std::make_shared<Synchronizable>(initial);
return SerializableJSRef::newNativeStateObject(rt, synchronizable);
});
}
if (name == "synchronizableGetDirty") {
return jsi::Function::createFromHostFunction(
rt, propName, 1, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
auto synchronizable = extractSynchronizableOrThrow(rt, args[0]);
return synchronizable->getDirty()->toJSValue(rt);
});
}
if (name == "synchronizableGetBlocking") {
return jsi::Function::createFromHostFunction(
rt, propName, 1, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
auto synchronizable = extractSynchronizableOrThrow(rt, args[0]);
return synchronizable->getBlocking()->toJSValue(rt);
});
}
if (name == "synchronizableSetBlocking") {
return jsi::Function::createFromHostFunction(
rt, propName, 2, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
auto synchronizable = extractSynchronizableOrThrow(rt, args[0]);
auto newValue = extractSerializableOrThrow(rt, args[1], "[Worklets] Value must be a Serializable.");
synchronizable->setBlocking(newValue);
return jsi::Value::undefined();
});
}
if (name == "synchronizableLock") {
return jsi::Function::createFromHostFunction(
rt, propName, 1, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
auto synchronizable = extractSynchronizableOrThrow(rt, args[0]);
synchronizable->lock();
return jsi::Value::undefined();
});
}
if (name == "synchronizableUnlock") {
return jsi::Function::createFromHostFunction(
rt, propName, 1, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
auto synchronizable = extractSynchronizableOrThrow(rt, args[0]);
synchronizable->unlock();
return jsi::Value::undefined();
});
}
if (name == "createShareable") {
return jsi::Function::createFromHostFunction(
rt,
propName,
5,
[runtimeManager = runtimeManager_](
jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) -> jsi::Value {
const int hostRuntimeId = args[0].asNumber();
const auto hostRuntime = runtimeManager->getRuntime(hostRuntimeId);
if (!hostRuntime) {
throw jsi::JSError(
rt, "[Worklets] createShareable: no worklet runtime found for id " + std::to_string(hostRuntimeId));
}
const auto initial = extractSerializableOrThrow(rt, args[1], "[Worklets] Value must be a Serializable.");
const auto initSynchronously = args[2].asBool();
const auto decorateHost = extractSerializableOrThrow(rt, args[3]);
const auto decorateGuest = extractSerializableOrThrow(rt, args[4]);
const auto shareable =
std::make_shared<Shareable>(hostRuntime, initial, initSynchronously, decorateHost, decorateGuest);
return SerializableJSRef::newNativeStateObject(rt, shareable);
});
}
if (name == "propagateModuleUpdate") {
return jsi::Function::createFromHostFunction(
rt,
propName,
2,
[runtimeManager = runtimeManager_](
jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return propagateModuleUpdate(
runtimeManager,
/* code */ args[0].asString(rt).utf8(rt),
/* sourceURL */ args[1].asString(rt).utf8(rt));
});
}
if (name == "getStaticFeatureFlag") {
return jsi::Function::createFromHostFunction(
rt, propName, 2, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
return worklets::StaticFeatureFlags::getFlag(
/* name */ args[0].asString(rt).utf8(rt));
});
}
if (name == "setDynamicFeatureFlag") {
return jsi::Function::createFromHostFunction(
rt, propName, 2, [](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
worklets::DynamicFeatureFlags::setFlag(
/* name */ args[0].asString(rt).utf8(rt),
/* value */ args[1].asBool());
return jsi::Value::undefined();
});
}
if (name == "getUIRuntimeHolder") {
return jsi::Function::createFromHostFunction(
rt,
propName,
0,
[uiWorkletRuntime = uiWorkletRuntime_](
jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
auto obj = jsi::Object(rt);
obj.setNativeState(rt, std::make_shared<WorkletRuntimeHolder>(uiWorkletRuntime.lock()));
return obj;
});
}
if (name == "getUISchedulerHolder") {
return jsi::Function::createFromHostFunction(
rt,
propName,
0,
[uiScheduler = uiScheduler_](
jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
auto obj = jsi::Object(rt);
obj.setNativeState(rt, std::make_shared<UISchedulerHolder>(uiScheduler));
return obj;
});
}
return jsi::Value::undefined();
}
} // namespace worklets