react-native-worklets 0.7.2 → 0.8.0-bundle-mode-preview-1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (123) hide show
  1. package/Common/cpp/worklets/NativeModules/JSIWorkletsModuleProxy.cpp +32 -28
  2. package/Common/cpp/worklets/NativeModules/JSIWorkletsModuleProxy.h +13 -5
  3. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.cpp +7 -5
  4. package/Common/cpp/worklets/NativeModules/WorkletsModuleProxy.h +5 -4
  5. package/Common/cpp/worklets/Resources/SynchronizableUnpacker.cpp +5 -5
  6. package/Common/cpp/worklets/RunLoop/AsyncQueueImpl.cpp +42 -19
  7. package/Common/cpp/worklets/RunLoop/AsyncQueueImpl.h +2 -0
  8. package/Common/cpp/worklets/Tools/Defs.h +2 -2
  9. package/Common/cpp/worklets/Tools/ScriptBuffer.h +34 -0
  10. package/Common/cpp/worklets/WorkletRuntime/RuntimeBindings.h +24 -0
  11. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp +11 -6
  12. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.cpp +82 -0
  13. package/Common/cpp/worklets/WorkletRuntime/WorkletRuntimeDecorator.h +12 -0
  14. package/RNWorklets.podspec +15 -14
  15. package/android/CMakeLists.txt +8 -2
  16. package/android/build.gradle +92 -56
  17. package/android/src/main/cpp/worklets/android/JScriptBufferWrapper.cpp +67 -0
  18. package/android/src/main/cpp/worklets/android/JScriptBufferWrapper.h +48 -0
  19. package/android/src/main/cpp/worklets/android/JWorkletRuntimeWrapper.cpp +52 -0
  20. package/android/src/main/cpp/worklets/android/JWorkletRuntimeWrapper.h +43 -0
  21. package/android/src/main/cpp/worklets/android/WorkletsModule.cpp +115 -19
  22. package/android/src/main/cpp/worklets/android/WorkletsModule.h +11 -13
  23. package/android/src/main/cpp/worklets/android/WorkletsOnLoad.cpp +6 -0
  24. package/android/src/main/java/com/swmansion/worklets/ScriptBufferWrapper.java +88 -0
  25. package/android/src/networking/com/swmansion/worklets/WorkletRuntimeWrapper.kt +23 -0
  26. package/android/src/networking/com/swmansion/worklets/WorkletsHeaderUtil.kt +30 -0
  27. package/android/src/{legacyBundling → networking}/com/swmansion/worklets/WorkletsModule.java +52 -2
  28. package/android/src/networking/com/swmansion/worklets/WorkletsNetworkEventUtil.kt +268 -0
  29. package/android/src/networking/com/swmansion/worklets/WorkletsNetworking.kt +1084 -0
  30. package/android/src/networking/com/swmansion/worklets/WorkletsOkHttpCallUtil.kt +37 -0
  31. package/android/src/networking/com/swmansion/worklets/WorkletsProgressListener.kt +9 -0
  32. package/android/src/networking/com/swmansion/worklets/WorkletsProgressRequestBody.kt +98 -0
  33. package/android/src/networking/com/swmansion/worklets/WorkletsProgressResponseBody.kt +57 -0
  34. package/android/src/networking/com/swmansion/worklets/WorkletsProgressiveStringDecoder.kt +82 -0
  35. package/android/src/networking/com/swmansion/worklets/WorkletsRequestBodyUtil.kt +177 -0
  36. package/android/src/{experimentalBundling → no-networking}/com/swmansion/worklets/WorkletsModule.java +10 -15
  37. package/apple/worklets/apple/Networking/WorkletsNetworking.h +22 -0
  38. package/apple/worklets/apple/Networking/WorkletsNetworking.mm +706 -0
  39. package/apple/worklets/apple/WorkletsModule.mm +56 -17
  40. package/bundleMode/index.js +2 -6
  41. package/compatibility.json +4 -1
  42. package/lib/module/WorkletsModule/NativeWorklets.native.js +8 -2
  43. package/lib/module/WorkletsModule/NativeWorklets.native.js.map +1 -1
  44. package/lib/module/bundleMode/metroOverrides.native.js +115 -0
  45. package/lib/module/bundleMode/metroOverrides.native.js.map +1 -0
  46. package/lib/module/bundleMode/network.native.js +41 -0
  47. package/lib/module/bundleMode/network.native.js.map +1 -0
  48. package/lib/module/debug/jsVersion.js +1 -1
  49. package/lib/module/debug/jsVersion.js.map +1 -1
  50. package/lib/module/featureFlags/staticFlags.json +2 -0
  51. package/lib/module/featureFlags/types.js +3 -1
  52. package/lib/module/featureFlags/types.js.map +1 -1
  53. package/lib/module/index.js +4 -2
  54. package/lib/module/index.js.map +1 -1
  55. package/lib/module/initializers/initializers.native.js +24 -50
  56. package/lib/module/initializers/initializers.native.js.map +1 -1
  57. package/lib/module/initializers/workletRuntimeEntry.native.js +3 -3
  58. package/lib/module/initializers/workletRuntimeEntry.native.js.map +1 -1
  59. package/lib/module/memory/bundleUnpacker.native.js +2 -2
  60. package/lib/module/memory/bundleUnpacker.native.js.map +1 -1
  61. package/lib/module/memory/serializable.native.js +3 -3
  62. package/lib/module/memory/serializable.native.js.map +1 -1
  63. package/lib/module/memory/synchronizableUnpacker.native.js +3 -3
  64. package/lib/module/memory/synchronizableUnpacker.native.js.map +1 -1
  65. package/lib/module/platformChecker.js +2 -2
  66. package/lib/module/platformChecker.js.map +1 -1
  67. package/lib/module/runtimeKind.js +51 -0
  68. package/lib/module/runtimeKind.js.map +1 -1
  69. package/lib/module/runtimes.js +3 -0
  70. package/lib/module/runtimes.js.map +1 -1
  71. package/lib/module/runtimes.native.js +34 -3
  72. package/lib/module/runtimes.native.js.map +1 -1
  73. package/lib/module/threads.native.js +2 -2
  74. package/lib/module/threads.native.js.map +1 -1
  75. package/lib/typescript/WorkletsModule/NativeWorklets.native.d.ts.map +1 -1
  76. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts +2 -1
  77. package/lib/typescript/WorkletsModule/workletsModuleProxy.d.ts.map +1 -1
  78. package/lib/typescript/bundleMode/metroOverrides.native.d.ts +28 -0
  79. package/lib/typescript/bundleMode/metroOverrides.native.d.ts.map +1 -0
  80. package/lib/typescript/bundleMode/network.native.d.ts +7 -0
  81. package/lib/typescript/bundleMode/network.native.d.ts.map +1 -0
  82. package/lib/typescript/debug/jsVersion.d.ts +1 -1
  83. package/lib/typescript/debug/jsVersion.d.ts.map +1 -1
  84. package/lib/typescript/featureFlags/types.d.ts +3 -1
  85. package/lib/typescript/featureFlags/types.d.ts.map +1 -1
  86. package/lib/typescript/index.d.ts +2 -2
  87. package/lib/typescript/index.d.ts.map +1 -1
  88. package/lib/typescript/initializers/initializers.native.d.ts +1 -0
  89. package/lib/typescript/initializers/initializers.native.d.ts.map +1 -1
  90. package/lib/typescript/initializers/workletRuntimeEntry.native.d.ts +1 -1
  91. package/lib/typescript/memory/bundleUnpacker.native.d.ts.map +1 -1
  92. package/lib/typescript/memory/synchronizableUnpacker.native.d.ts.map +1 -1
  93. package/lib/typescript/platformChecker.d.ts.map +1 -1
  94. package/lib/typescript/runtimeKind.d.ts +31 -0
  95. package/lib/typescript/runtimeKind.d.ts.map +1 -1
  96. package/lib/typescript/runtimes.d.ts +1 -0
  97. package/lib/typescript/runtimes.d.ts.map +1 -1
  98. package/lib/typescript/runtimes.native.d.ts +20 -2
  99. package/lib/typescript/runtimes.native.d.ts.map +1 -1
  100. package/lib/typescript/threads.native.d.ts +1 -1
  101. package/package.json +8 -6
  102. package/plugin/index.d.ts +109 -0
  103. package/plugin/index.js +59 -9
  104. package/scripts/worklets_utils.rb +21 -5
  105. package/src/WorkletsModule/NativeWorklets.native.ts +14 -4
  106. package/src/WorkletsModule/workletsModuleProxy.ts +6 -3
  107. package/src/bundleMode/metroOverrides.native.ts +151 -0
  108. package/src/bundleMode/network.native.ts +59 -0
  109. package/src/debug/jsVersion.ts +1 -1
  110. package/src/featureFlags/staticFlags.json +2 -0
  111. package/src/featureFlags/types.ts +3 -1
  112. package/src/index.ts +10 -1
  113. package/src/initializers/initializers.native.ts +29 -70
  114. package/src/initializers/workletRuntimeEntry.native.ts +3 -3
  115. package/src/memory/bundleUnpacker.native.ts +2 -4
  116. package/src/memory/serializable.native.ts +3 -3
  117. package/src/memory/synchronizableUnpacker.native.ts +6 -12
  118. package/src/platformChecker.ts +3 -2
  119. package/src/privateGlobals.d.ts +7 -2
  120. package/src/runtimeKind.ts +47 -0
  121. package/src/runtimes.native.ts +43 -2
  122. package/src/runtimes.ts +10 -0
  123. package/src/threads.native.ts +2 -2
@@ -54,10 +54,8 @@ inline void scheduleOnUI(
54
54
  });
55
55
  }
56
56
 
57
- inline jsi::Value executeOnUIRuntimeSync(
58
- const std::weak_ptr<WorkletRuntime> &weakUIWorkletRuntime,
59
- jsi::Runtime &rt,
60
- const jsi::Value &worklet) {
57
+ inline jsi::Value
58
+ runOnUISync(const std::weak_ptr<WorkletRuntime> &weakUIWorkletRuntime, jsi::Runtime &rt, const jsi::Value &worklet) {
61
59
  if (auto uiWorkletRuntime = weakUIWorkletRuntime.lock()) {
62
60
  auto serializableWorklet = extractSerializableOrThrow<SerializableWorklet>(
63
61
  rt, worklet, "[Worklets] Only worklets can be executed on UI runtime.");
@@ -67,6 +65,14 @@ inline jsi::Value executeOnUIRuntimeSync(
67
65
  return jsi::Value::undefined();
68
66
  }
69
67
 
68
+ jsi::Value
69
+ runOnRuntimeSync(jsi::Runtime &rt, const jsi::Value &workletRuntimeValue, const jsi::Value &serializableWorkletValue) {
70
+ auto workletRuntime = workletRuntimeValue.getObject(rt).getHostObject<WorkletRuntime>(rt);
71
+ auto worklet = extractSerializableOrThrow<SerializableWorklet>(
72
+ rt, serializableWorkletValue, "[Worklets] Only worklets can be executed on a worklet runtime.");
73
+ return workletRuntime->runSyncSerialized(worklet)->toJSValue(rt);
74
+ }
75
+
70
76
  inline jsi::Value createWorkletRuntime(
71
77
  jsi::Runtime &originRuntime,
72
78
  const std::shared_ptr<RuntimeManager> &runtimeManager,
@@ -81,7 +87,7 @@ inline jsi::Value createWorkletRuntime(
81
87
  return jsi::Object::createFromHostObject(originRuntime, workletRuntime);
82
88
  }
83
89
 
84
- #ifdef WORKLETS_BUNDLE_MODE
90
+ #ifdef WORKLETS_BUNDLE_MODE_ENABLED
85
91
  inline jsi::Value propagateModuleUpdate(
86
92
  const std::shared_ptr<RuntimeManager> &runtimeManager,
87
93
  const std::string &code,
@@ -96,7 +102,7 @@ inline jsi::Value propagateModuleUpdate(
96
102
  }
97
103
  return jsi::Value::undefined();
98
104
  }
99
- #endif // WORKLETS_BUNDLE_MODE
105
+ #endif // WORKLETS_BUNDLE_MODE_ENABLED
100
106
 
101
107
  inline jsi::Value reportFatalErrorOnJS(
102
108
  const std::shared_ptr<JSScheduler> &jsScheduler,
@@ -146,14 +152,15 @@ inline void registerCustomSerializable(
146
152
 
147
153
  JSIWorkletsModuleProxy::JSIWorkletsModuleProxy(
148
154
  const bool isDevBundle,
149
- const std::shared_ptr<const JSBigStringBuffer> &script,
155
+ const std::shared_ptr<const ScriptBuffer> &script,
150
156
  const std::string &sourceUrl,
151
157
  const std::shared_ptr<MessageQueueThread> &jsQueue,
152
158
  const std::shared_ptr<JSScheduler> &jsScheduler,
153
159
  const std::shared_ptr<UIScheduler> &uiScheduler,
154
160
  const std::shared_ptr<MemoryManager> &memoryManager,
155
161
  const std::shared_ptr<RuntimeManager> &runtimeManager,
156
- const std::weak_ptr<WorkletRuntime> &uiWorkletRuntime)
162
+ const std::weak_ptr<WorkletRuntime> &uiWorkletRuntime,
163
+ const std::shared_ptr<RuntimeBindings> &runtimeBindings)
157
164
  : jsi::HostObject(),
158
165
  isDevBundle_(isDevBundle),
159
166
  script_(script),
@@ -163,19 +170,8 @@ JSIWorkletsModuleProxy::JSIWorkletsModuleProxy(
163
170
  uiScheduler_(uiScheduler),
164
171
  memoryManager_(memoryManager),
165
172
  runtimeManager_(runtimeManager),
166
- uiWorkletRuntime_(uiWorkletRuntime) {}
167
-
168
- JSIWorkletsModuleProxy::JSIWorkletsModuleProxy(const JSIWorkletsModuleProxy &other)
169
- : jsi::HostObject(),
170
- isDevBundle_(other.isDevBundle_),
171
- script_(other.script_),
172
- sourceUrl_(other.sourceUrl_),
173
- jsQueue_(other.jsQueue_),
174
- jsScheduler_(other.jsScheduler_),
175
- uiScheduler_(other.uiScheduler_),
176
- memoryManager_(other.memoryManager_),
177
- runtimeManager_(other.runtimeManager_),
178
- uiWorkletRuntime_(other.uiWorkletRuntime_) {}
173
+ uiWorkletRuntime_(uiWorkletRuntime),
174
+ runtimeBindings_(runtimeBindings) {}
179
175
 
180
176
  JSIWorkletsModuleProxy::~JSIWorkletsModuleProxy() = default;
181
177
 
@@ -203,7 +199,8 @@ std::vector<jsi::PropNameID> JSIWorkletsModuleProxy::getPropertyNames(jsi::Runti
203
199
  propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "registerCustomSerializable"));
204
200
 
205
201
  propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "scheduleOnUI"));
206
- propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "executeOnUIRuntimeSync"));
202
+ propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "runOnUISync"));
203
+ propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "runOnRuntimeSync"));
207
204
  propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "createWorkletRuntime"));
208
205
  propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "scheduleOnRuntime"));
209
206
  propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "reportFatalErrorOnJS"));
@@ -218,9 +215,9 @@ std::vector<jsi::PropNameID> JSIWorkletsModuleProxy::getPropertyNames(jsi::Runti
218
215
  propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "synchronizableLock"));
219
216
  propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "synchronizableUnlock"));
220
217
 
221
- #ifdef WORKLETS_BUNDLE_MODE
218
+ #ifdef WORKLETS_BUNDLE_MODE_ENABLED
222
219
  propertyNames.emplace_back(jsi::PropNameID::forAscii(rt, "propagateModuleUpdate"));
223
- #endif // WORKLETS_BUNDLE_MODE
220
+ #endif // WORKLETS_BUNDLE_MODE_ENABLED
224
221
 
225
222
  return propertyNames;
226
223
  }
@@ -385,14 +382,21 @@ jsi::Value JSIWorkletsModuleProxy::get(jsi::Runtime &rt, const jsi::PropNameID &
385
382
  });
386
383
  }
387
384
 
388
- if (name == "executeOnUIRuntimeSync") {
385
+ if (name == "runOnUISync") {
389
386
  return jsi::Function::createFromHostFunction(
390
387
  rt,
391
388
  propName,
392
389
  1,
393
390
  [uiWorkletRuntime = uiWorkletRuntime_](
394
391
  jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
395
- return executeOnUIRuntimeSync(uiWorkletRuntime, rt, args[0]);
392
+ return runOnUISync(uiWorkletRuntime, rt, args[0]);
393
+ });
394
+ }
395
+
396
+ if (name == "runOnRuntimeSync") {
397
+ return jsi::Function::createFromHostFunction(
398
+ rt, propName, 2, [](jsi::Runtime &rt, const jsi ::Value &thisValue, const jsi::Value *args, size_t count) {
399
+ return runOnRuntimeSync(rt, args[0], args[1]);
396
400
  });
397
401
  }
398
402
 
@@ -506,7 +510,7 @@ jsi::Value JSIWorkletsModuleProxy::get(jsi::Runtime &rt, const jsi::PropNameID &
506
510
  });
507
511
  }
508
512
 
509
- #ifdef WORKLETS_BUNDLE_MODE
513
+ #ifdef WORKLETS_BUNDLE_MODE_ENABLED
510
514
  if (name == "propagateModuleUpdate") {
511
515
  return jsi::Function::createFromHostFunction(
512
516
  rt,
@@ -520,7 +524,7 @@ jsi::Value JSIWorkletsModuleProxy::get(jsi::Runtime &rt, const jsi::PropNameID &
520
524
  /* sourceURL */ args[1].asString(rt).utf8(rt));
521
525
  });
522
526
  }
523
- #endif // WORKLETS_BUNDLE_MODE
527
+ #endif // WORKLETS_BUNDLE_MODE_ENABLED
524
528
 
525
529
  if (name == "getStaticFeatureFlag") {
526
530
  return jsi::Function::createFromHostFunction(
@@ -8,6 +8,8 @@
8
8
  #include <worklets/SharedItems/MemoryManager.h>
9
9
  #include <worklets/SharedItems/Serializable.h>
10
10
  #include <worklets/Tools/Defs.h>
11
+ #include <worklets/Tools/ScriptBuffer.h>
12
+ #include <worklets/WorkletRuntime/RuntimeBindings.h>
11
13
  #include <worklets/WorkletRuntime/RuntimeManager.h>
12
14
  #include <worklets/WorkletRuntime/UIRuntimeDecorator.h>
13
15
 
@@ -31,16 +33,17 @@ class JSIWorkletsModuleProxy : public jsi::HostObject {
31
33
  public:
32
34
  explicit JSIWorkletsModuleProxy(
33
35
  const bool isDevBundle,
34
- const std::shared_ptr<const JSBigStringBuffer> &script,
36
+ const std::shared_ptr<const ScriptBuffer> &script,
35
37
  const std::string &sourceUrl,
36
38
  const std::shared_ptr<MessageQueueThread> &jsQueue,
37
39
  const std::shared_ptr<JSScheduler> &jsScheduler,
38
40
  const std::shared_ptr<UIScheduler> &uiScheduler,
39
41
  const std::shared_ptr<MemoryManager> &memoryManager,
40
42
  const std::shared_ptr<RuntimeManager> &runtimeManager,
41
- const std::weak_ptr<WorkletRuntime> &uiWorkletRuntime);
43
+ const std::weak_ptr<WorkletRuntime> &uiWorkletRuntime,
44
+ const std::shared_ptr<RuntimeBindings> &runtimeBindings);
42
45
 
43
- JSIWorkletsModuleProxy(const JSIWorkletsModuleProxy &other);
46
+ JSIWorkletsModuleProxy(const JSIWorkletsModuleProxy &other) = default;
44
47
 
45
48
  ~JSIWorkletsModuleProxy() override;
46
49
 
@@ -64,7 +67,7 @@ class JSIWorkletsModuleProxy : public jsi::HostObject {
64
67
  return isDevBundle_;
65
68
  }
66
69
 
67
- [[nodiscard]] std::shared_ptr<const JSBigStringBuffer> getScript() const {
70
+ [[nodiscard]] std::shared_ptr<const ScriptBuffer> getScript() const {
68
71
  return script_;
69
72
  }
70
73
 
@@ -80,9 +83,13 @@ class JSIWorkletsModuleProxy : public jsi::HostObject {
80
83
  return runtimeManager_;
81
84
  }
82
85
 
86
+ [[nodiscard]] std::shared_ptr<RuntimeBindings> getRuntimeBindings() const {
87
+ return runtimeBindings_;
88
+ }
89
+
83
90
  private:
84
91
  const bool isDevBundle_;
85
- const std::shared_ptr<const JSBigStringBuffer> script_;
92
+ const std::shared_ptr<const ScriptBuffer> script_;
86
93
  const std::string sourceUrl_;
87
94
  const std::shared_ptr<MessageQueueThread> jsQueue_;
88
95
  const std::shared_ptr<JSScheduler> jsScheduler_;
@@ -90,6 +97,7 @@ class JSIWorkletsModuleProxy : public jsi::HostObject {
90
97
  const std::shared_ptr<MemoryManager> memoryManager_;
91
98
  const std::shared_ptr<RuntimeManager> runtimeManager_;
92
99
  const std::weak_ptr<WorkletRuntime> uiWorkletRuntime_;
100
+ const std::shared_ptr<RuntimeBindings> runtimeBindings_;
93
101
  };
94
102
 
95
103
  } // namespace worklets
@@ -5,6 +5,7 @@
5
5
  #include <worklets/RunLoop/AsyncQueueImpl.h>
6
6
  #include <worklets/SharedItems/Serializable.h>
7
7
  #include <worklets/Tools/Defs.h>
8
+ #include <worklets/Tools/ScriptBuffer.h>
8
9
  #include <worklets/WorkletRuntime/RuntimeBindings.h>
9
10
  #include <worklets/WorkletRuntime/UIRuntimeDecorator.h>
10
11
 
@@ -28,15 +29,15 @@ WorkletsModuleProxy::WorkletsModuleProxy(
28
29
  const std::shared_ptr<CallInvoker> &jsCallInvoker,
29
30
  const std::shared_ptr<UIScheduler> &uiScheduler,
30
31
  std::function<bool()> &&isJavaScriptThread,
31
- RuntimeBindings runtimeBindings,
32
- const std::shared_ptr<const JSBigStringBuffer> &script,
32
+ const std::shared_ptr<RuntimeBindings> &runtimeBindings,
33
+ const std::shared_ptr<const ScriptBuffer> &script,
33
34
  const std::string &sourceUrl)
34
35
  : isDevBundle_(isDevBundleFromRNRuntime(rnRuntime)),
35
36
  jsQueue_(jsQueue),
36
37
  jsScheduler_(std::make_shared<JSScheduler>(rnRuntime, jsCallInvoker, std::move(isJavaScriptThread))),
37
38
  uiScheduler_(uiScheduler),
38
39
  jsLogger_(std::make_shared<JSLogger>(jsScheduler_)),
39
- runtimeBindings_(std::move(runtimeBindings)),
40
+ runtimeBindings_(runtimeBindings),
40
41
  script_(script),
41
42
  sourceUrl_(sourceUrl),
42
43
  memoryManager_(std::make_shared<MemoryManager>()),
@@ -50,7 +51,7 @@ WorkletsModuleProxy::WorkletsModuleProxy(
50
51
  uiWorkletRuntime_->init(createJSIWorkletsModuleProxy());
51
52
 
52
53
  animationFrameBatchinator_ =
53
- std::make_shared<AnimationFrameBatchinator>(uiWorkletRuntime_, runtimeBindings_.requestAnimationFrame);
54
+ std::make_shared<AnimationFrameBatchinator>(uiWorkletRuntime_, runtimeBindings_->requestAnimationFrame);
54
55
 
55
56
  UIRuntimeDecorator::decorate(
56
57
  uiWorkletRuntime_->getJSIRuntime(), animationFrameBatchinator_->getJsiRequestAnimationFrame());
@@ -67,7 +68,8 @@ std::shared_ptr<JSIWorkletsModuleProxy> WorkletsModuleProxy::createJSIWorkletsMo
67
68
  uiScheduler_,
68
69
  memoryManager_,
69
70
  runtimeManager_,
70
- uiWorkletRuntime_);
71
+ uiWorkletRuntime_,
72
+ runtimeBindings_);
71
73
  }
72
74
 
73
75
  WorkletsModuleProxy::~WorkletsModuleProxy() {
@@ -9,6 +9,7 @@
9
9
  #include <worklets/Tools/Defs.h>
10
10
  #include <worklets/Tools/JSLogger.h>
11
11
  #include <worklets/Tools/JSScheduler.h>
12
+ #include <worklets/Tools/ScriptBuffer.h>
12
13
  #include <worklets/Tools/SingleInstanceChecker.h>
13
14
  #include <worklets/Tools/UIScheduler.h>
14
15
  #include <worklets/WorkletRuntime/RuntimeManager.h>
@@ -27,8 +28,8 @@ class WorkletsModuleProxy : public std::enable_shared_from_this<WorkletsModulePr
27
28
  const std::shared_ptr<CallInvoker> &jsCallInvoker,
28
29
  const std::shared_ptr<UIScheduler> &uiScheduler,
29
30
  std::function<bool()> &&isJavaScriptQueue,
30
- RuntimeBindings runtimeBindings,
31
- const std::shared_ptr<const JSBigStringBuffer> &script,
31
+ const std::shared_ptr<RuntimeBindings> &runtimeBindings,
32
+ const std::shared_ptr<const ScriptBuffer> &script,
32
33
  const std::string &sourceUrl);
33
34
 
34
35
  ~WorkletsModuleProxy();
@@ -65,8 +66,8 @@ class WorkletsModuleProxy : public std::enable_shared_from_this<WorkletsModulePr
65
66
  const std::shared_ptr<JSScheduler> jsScheduler_;
66
67
  const std::shared_ptr<UIScheduler> uiScheduler_;
67
68
  const std::shared_ptr<JSLogger> jsLogger_;
68
- const RuntimeBindings runtimeBindings_;
69
- const std::shared_ptr<const JSBigStringBuffer> script_;
69
+ const std::shared_ptr<RuntimeBindings> runtimeBindings_;
70
+ const std::shared_ptr<const ScriptBuffer> script_;
70
71
  const std::string sourceUrl_;
71
72
  const std::shared_ptr<MemoryManager> memoryManager_;
72
73
  const std::shared_ptr<RuntimeManager> runtimeManager_;
@@ -8,9 +8,9 @@ namespace worklets {
8
8
 
9
9
  const char SynchronizableUnpackerCode[] =
10
10
  R"DELIMITER__((function () {
11
- var serializer = !globalThis._WORKLET || globalThis._WORKLETS_BUNDLE_MODE ? function (value, _) {
12
- return (0, _serializable.createSerializable)(value);
13
- } : globalThis._createSerializable;
11
+ var serializer = !globalThis._WORKLET || globalThis._WORKLETS_BUNDLE_MODE_ENABLED ? _serializable.createSerializable : function (value) {
12
+ return globalThis.__serializer(value);
13
+ };
14
14
  function synchronizableUnpacker(synchronizableRef) {
15
15
  var synchronizable = synchronizableRef;
16
16
  var proxy = globalThis.__workletsModuleProxy;
@@ -28,12 +28,12 @@ const char SynchronizableUnpackerCode[] =
28
28
  synchronizable.lock();
29
29
  var prev = synchronizable.getBlocking();
30
30
  newValue = func(prev);
31
- proxy.synchronizableSetBlocking(synchronizable, serializer(newValue, undefined));
31
+ proxy.synchronizableSetBlocking(synchronizable, serializer(newValue));
32
32
  synchronizable.unlock();
33
33
  } else {
34
34
  var value = valueOrFunction;
35
35
  newValue = value;
36
- proxy.synchronizableSetBlocking(synchronizable, serializer(newValue, undefined));
36
+ proxy.synchronizableSetBlocking(synchronizable, serializer(newValue));
37
37
  }
38
38
  };
39
39
  synchronizable.lock = function () {
@@ -1,5 +1,10 @@
1
1
  #include <worklets/RunLoop/AsyncQueueImpl.h>
2
2
 
3
+ #if defined(ANDROID) && defined(WORKLETS_BUNDLE_MODE_ENABLED) && defined(WORKLETS_FETCH_PREVIEW_ENABLED)
4
+ #include <fbjni/detail/Environment.h>
5
+ #include <jni.h>
6
+ #endif // defined(ANDROID) && defined(WORKLETS_BUNDLE_MODE_ENABLED) && defined(WORKLETS_FETCH_PREVIEW_ENABLED)
7
+
3
8
  #include <memory>
4
9
  #include <string>
5
10
  #include <thread>
@@ -7,30 +12,48 @@
7
12
 
8
13
  namespace worklets {
9
14
 
15
+ using namespace facebook;
16
+
17
+ void AsyncQueueImpl::runLoop(const std::shared_ptr<AsyncQueueState> &state) {
18
+ while (state->running) {
19
+ std::unique_lock<std::mutex> lock(state->mutex);
20
+ state->cv.wait(lock, [state] { return !state->queue.empty() || !state->running; });
21
+ if (!state->running) {
22
+ return;
23
+ }
24
+ if (state->queue.empty()) {
25
+ continue;
26
+ }
27
+ auto job = std::move(state->queue.front());
28
+ state->queue.pop();
29
+ lock.unlock();
30
+ job();
31
+ }
32
+ }
33
+
10
34
  AsyncQueueImpl::AsyncQueueImpl(const std::string &name) : state_(std::make_shared<AsyncQueueState>()) {
35
+ #if defined(ANDROID) && defined(WORKLETS_BUNDLE_MODE_ENABLED) && defined(WORKLETS_FETCH_PREVIEW_ENABLED)
36
+ auto *env = jni::Environment::current();
37
+ JavaVM *jvm = nullptr;
38
+ env->GetJavaVM(&jvm);
39
+ auto thread = std::thread([name, state = state_, jvm] {
40
+ jvm->AttachCurrentThread(nullptr, nullptr);
41
+ jni::ThreadScope::WithClassLoader([state]() { AsyncQueueImpl::runLoop(state); });
42
+ jvm->DetachCurrentThread();
43
+ });
44
+ pthread_setname_np(thread.native_handle(), name.c_str());
45
+ thread.detach();
46
+ #elif defined(ANDROID)
47
+ auto thread = std::thread([name, state = state_] { AsyncQueueImpl::runLoop(state); });
48
+ pthread_setname_np(thread.native_handle(), name.c_str());
49
+ thread.detach();
50
+ #else
11
51
  auto thread = std::thread([name, state = state_] {
12
- #if __APPLE__
13
52
  pthread_setname_np(name.c_str());
14
- #endif
15
- while (state->running) {
16
- std::unique_lock<std::mutex> lock(state->mutex);
17
- state->cv.wait(lock, [state] { return !state->queue.empty() || !state->running; });
18
- if (!state->running) {
19
- return;
20
- }
21
- if (state->queue.empty()) {
22
- continue;
23
- }
24
- auto job = std::move(state->queue.front());
25
- state->queue.pop();
26
- lock.unlock();
27
- job();
28
- }
53
+ AsyncQueueImpl::runLoop(state);
29
54
  });
30
- #ifdef ANDROID
31
- pthread_setname_np(thread.native_handle(), name.c_str());
32
- #endif
33
55
  thread.detach();
56
+ #endif // defined(ANDROID) && defined(WORKLETS_BUNDLE_MODE_ENABLED) && defined(WORKLETS_FETCH_PREVIEW_ENABLED)
34
57
  }
35
58
 
36
59
  AsyncQueueImpl::~AsyncQueueImpl() {
@@ -28,6 +28,8 @@ class AsyncQueueImpl : public AsyncQueue {
28
28
  void push(std::function<void()> &&job) override;
29
29
 
30
30
  private:
31
+ static void runLoop(const std::shared_ptr<AsyncQueueState> &state);
32
+
31
33
  const std::shared_ptr<AsyncQueueState> state_;
32
34
  };
33
35
 
@@ -9,7 +9,7 @@ so we have to check if headers are available.
9
9
  #define JS_RUNTIME_HERMES 1
10
10
  #endif
11
11
 
12
- #if REACT_NATIVE_MINOR_VERSION >= 84
12
+ #if REACT_NATIVE_MINOR_VERSION >= 84 || defined(WORKLETS_BUNDLE_MODE_ENABLED)
13
13
  #include <cxxreact/JSBigString.h>
14
14
  namespace worklets {
15
15
  using JSBigStringBuffer = facebook::react::JSBigString;
@@ -19,4 +19,4 @@ using JSBigStringBuffer = facebook::react::JSBigString;
19
19
  namespace worklets {
20
20
  using JSBigStringBuffer = facebook::react::BigStringBuffer;
21
21
  }
22
- #endif // REACT_NATIVE_MINOR_VERSION >= 84
22
+ #endif // REACT_NATIVE_MINOR_VERSION >= 84 || defined(WORKLETS_BUNDLE_MODE_ENABLED)
@@ -0,0 +1,34 @@
1
+ #pragma once
2
+
3
+ #include <cxxreact/JSBigString.h>
4
+ #include <jsi/jsi.h>
5
+
6
+ #include <memory>
7
+ #include <utility>
8
+
9
+ namespace worklets {
10
+
11
+ using namespace facebook;
12
+ using namespace facebook::react;
13
+
14
+ /**
15
+ * Our custom copyable structure that is accepted by
16
+ * Hermes Runtime for evaluation.
17
+ */
18
+ class ScriptBuffer : public jsi::Buffer {
19
+ public:
20
+ explicit ScriptBuffer(std::shared_ptr<const JSBigString> script) : script_(std::move(script)) {}
21
+
22
+ size_t size() const override {
23
+ return script_->size();
24
+ }
25
+
26
+ const uint8_t *data() const override {
27
+ return reinterpret_cast<const uint8_t *>(script_->c_str());
28
+ }
29
+
30
+ private:
31
+ std::shared_ptr<const JSBigString> script_;
32
+ };
33
+
34
+ } // namespace worklets
@@ -12,6 +12,30 @@ struct RuntimeBindings {
12
12
  using RequestAnimationFrame = std::function<void(std::function<void(const double)>)>;
13
13
 
14
14
  const RequestAnimationFrame requestAnimationFrame;
15
+
16
+ #if defined(WORKLETS_BUNDLE_MODE_ENABLED) && defined(WORKLETS_FETCH_PREVIEW_ENABLED)
17
+ using AbortRequest = std::function<void(jsi::Runtime &rt, double requestId)>;
18
+ using ClearCookies = std::function<void(jsi::Runtime &rt, jsi::Function &&responseSender)>;
19
+ #ifdef ANDROID
20
+ using SendRequest = std::function<void(
21
+ jsi::Runtime &rt,
22
+ jsi::String &method,
23
+ jsi::String &url,
24
+ double requestId,
25
+ jsi::Array &headers,
26
+ jsi::Object &data,
27
+ jsi::String &responseType,
28
+ bool incrementalUpdates,
29
+ double timeout,
30
+ bool withCredentials)>;
31
+ #else
32
+ using SendRequest = std::function<void(jsi::Runtime &rt, const jsi::Value &query, jsi::Function &&responseSender)>;
33
+ #endif // ANDROID
34
+
35
+ const AbortRequest abortRequest;
36
+ const ClearCookies clearCookies;
37
+ const SendRequest sendRequest;
38
+ #endif // defined(WORKLETS_BUNDLE_MODE_ENABLED) && defined(WORKLETS_FETCH_PREVIEW_ENABLED)
15
39
  };
16
40
 
17
41
  } // namespace worklets
@@ -100,32 +100,37 @@ void WorkletRuntime::init(std::shared_ptr<JSIWorkletsModuleProxy> jsiWorkletsMod
100
100
  const auto jsScheduler = jsiWorkletsModuleProxy->getJSScheduler();
101
101
  const auto isDevBundle = jsiWorkletsModuleProxy->isDevBundle();
102
102
  const auto memoryManager_ = jsiWorkletsModuleProxy->getMemoryManager();
103
- #ifdef WORKLETS_BUNDLE_MODE
103
+ #ifdef WORKLETS_BUNDLE_MODE_ENABLED
104
104
  auto script = jsiWorkletsModuleProxy->getScript();
105
105
  const auto &sourceUrl = jsiWorkletsModuleProxy->getSourceUrl();
106
- #endif // WORKLETS_BUNDLE_MODE
106
+ auto runtimeBindings = jsiWorkletsModuleProxy->getRuntimeBindings();
107
+ #endif // WORKLETS_BUNDLE_MODE_ENABLED
107
108
 
108
109
  auto optimizedJsiWorkletsModuleProxy = jsi_utils::optimizedFromHostObject(rt, std::move(jsiWorkletsModuleProxy));
109
110
 
110
111
  WorkletRuntimeDecorator::decorate(
111
112
  rt, name_, jsScheduler, isDevBundle, std::move(optimizedJsiWorkletsModuleProxy), eventLoop_);
112
113
 
113
- #ifdef WORKLETS_BUNDLE_MODE
114
+ #ifdef WORKLETS_BUNDLE_MODE_ENABLED
114
115
  if (!script) {
115
116
  throw std::runtime_error("[Worklets] Expected to receive the bundle, but got nullptr instead.");
116
117
  }
117
118
 
118
119
  try {
119
120
  rt.evaluateJavaScript(script, sourceUrl);
120
- } catch (facebook::jsi::JSError error) {
121
+ } catch (facebook::jsi::JSError &error) {
121
122
  const auto &message = error.getMessage();
122
123
  const auto &stack = error.getStack();
123
124
  if (!message.starts_with("[Worklets] Worklets initialized successfully")) {
124
- const auto newMessage = "[Worklets] Failed to initialize runtime. Reason: " + message;
125
+ const auto newMessage = "[Worklets] Failed to initialize runtime. Reason: " + message + " " + stack;
125
126
  JSLogger::reportFatalErrorOnJS(
126
127
  jsScheduler, {.message = newMessage, .stack = stack, .name = "WorkletsError", .jsEngine = "Worklets"});
128
+ return;
127
129
  }
128
130
  }
131
+
132
+ WorkletRuntimeDecorator::postEvaluateScript(rt, runtimeBindings);
133
+
129
134
  #else
130
135
  // Legacy behavior
131
136
  auto valueUnpackerBuffer = std::make_shared<const jsi::StringBuffer>(ValueUnpackerCode);
@@ -136,7 +141,7 @@ void WorkletRuntime::init(std::shared_ptr<JSIWorkletsModuleProxy> jsiWorkletsMod
136
141
 
137
142
  auto customSerializableUnpackerBuffer = std::make_shared<const jsi::StringBuffer>(CustomSerializableUnpackerCode);
138
143
  rt.evaluateJavaScript(customSerializableUnpackerBuffer, "customSerializableUnpacker");
139
- #endif // WORKLETS_BUNDLE_MODE
144
+ #endif // WORKLETS_BUNDLE_MODE_ENABLED
140
145
  try {
141
146
  memoryManager_->loadAllCustomSerializables(shared_from_this());
142
147
  } catch (jsi::JSError &e) {
@@ -226,4 +226,86 @@ void WorkletRuntimeDecorator::decorate(
226
226
  });
227
227
  }
228
228
 
229
+ #ifdef WORKLETS_BUNDLE_MODE_ENABLED
230
+ void WorkletRuntimeDecorator::postEvaluateScript(
231
+ jsi::Runtime &rt,
232
+ const std::shared_ptr<RuntimeBindings> &runtimeBindings) {
233
+ #ifdef WORKLETS_FETCH_PREVIEW_ENABLED
234
+ installNetworking(rt, runtimeBindings);
235
+ #endif // WORKLETS_FETCH_PREVIEW_ENABLED
236
+ }
237
+
238
+ #ifdef WORKLETS_FETCH_PREVIEW_ENABLED
239
+ void WorkletRuntimeDecorator::installNetworking(
240
+ jsi::Runtime &rt,
241
+ const std::shared_ptr<RuntimeBindings> &runtimeBindings) {
242
+ auto TurboModules = rt.global().getPropertyAsObject(rt, "TurboModules");
243
+
244
+ auto Networking = TurboModules.getPropertyAsFunction(rt, "get").callWithThis(rt, TurboModules, "Networking");
245
+
246
+ #ifdef ANDROID
247
+ auto jsiSendRequest = jsi::Function::createFromHostFunction(
248
+ rt,
249
+ jsi::PropNameID::forAscii(rt, "sendRequest"),
250
+ 9,
251
+ [sendRequest = runtimeBindings->sendRequest](
252
+ jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
253
+ auto method = args[0].asString(rt);
254
+ auto url = args[1].asString(rt);
255
+ auto requestId = args[2].asNumber();
256
+ auto headers = args[3].asObject(rt).asArray(rt);
257
+ auto data = args[4].asObject(rt);
258
+ auto responseType = args[5].asString(rt);
259
+ auto incrementalUpdates = args[6].asBool();
260
+ auto timeout = args[7].asNumber();
261
+ auto withCredentials = args[8].asBool();
262
+ sendRequest(
263
+ rt, method, url, requestId, headers, data, responseType, incrementalUpdates, timeout, withCredentials);
264
+ return jsi::Value::undefined();
265
+ });
266
+ #else
267
+ auto jsiSendRequest = jsi::Function::createFromHostFunction(
268
+ rt,
269
+ jsi::PropNameID::forAscii(rt, "sendRequest"),
270
+ 2,
271
+ [sendRequest = runtimeBindings->sendRequest](
272
+ jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
273
+ auto &query = args[0];
274
+ auto responseSender = args[1].asObject(rt).asFunction(rt);
275
+ sendRequest(rt, query, std::move(responseSender));
276
+ return jsi::Value::undefined();
277
+ });
278
+ #endif // ANDROID
279
+
280
+ Networking.asObject(rt).setProperty(rt, "sendRequest", std::move(jsiSendRequest));
281
+
282
+ auto jsiAbortRequest = jsi::Function::createFromHostFunction(
283
+ rt,
284
+ jsi::PropNameID::forAscii(rt, "abortRequest"),
285
+ 1,
286
+ [abortRequest = runtimeBindings->abortRequest](
287
+ jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
288
+ auto requestID = args[0].asNumber();
289
+ abortRequest(rt, requestID);
290
+ return jsi::Value::undefined();
291
+ });
292
+
293
+ Networking.asObject(rt).setProperty(rt, "abortRequest", std::move(jsiAbortRequest));
294
+
295
+ auto jsiClearCookies = jsi::Function::createFromHostFunction(
296
+ rt,
297
+ jsi::PropNameID::forAscii(rt, "clearCookies"),
298
+ 1,
299
+ [clearCookies = runtimeBindings->clearCookies](
300
+ jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) {
301
+ auto responseSender = args[0].asObject(rt).asFunction(rt);
302
+ clearCookies(rt, std::move(responseSender));
303
+ return jsi::Value::undefined();
304
+ });
305
+
306
+ Networking.asObject(rt).setProperty(rt, "clearCookies", std::move(jsiClearCookies));
307
+ }
308
+ #endif // WORKLETS_FETCH_PREVIEW_ENABLED
309
+ #endif // WORKLETS_BUNDLE_MODE_ENABLED
310
+
229
311
  } // namespace worklets
@@ -2,6 +2,9 @@
2
2
 
3
3
  #include <worklets/RunLoop/EventLoop.h>
4
4
  #include <worklets/Tools/JSScheduler.h>
5
+ #ifdef WORKLETS_BUNDLE_MODE_ENABLED
6
+ #include <worklets/WorkletRuntime/RuntimeBindings.h>
7
+ #endif // WORKLETS_BUNDLE_MODE_ENABLED
5
8
 
6
9
  #include <jsi/jsi.h>
7
10
 
@@ -21,6 +24,15 @@ class WorkletRuntimeDecorator {
21
24
  const bool isDevBundle,
22
25
  jsi::Object &&jsiWorkletsModuleProxy,
23
26
  const std::shared_ptr<EventLoop> &eventLoop);
27
+
28
+ #ifdef WORKLETS_BUNDLE_MODE_ENABLED
29
+ static void postEvaluateScript(jsi::Runtime &rt, const std::shared_ptr<RuntimeBindings> &runtimeBindings);
30
+
31
+ private:
32
+ #ifdef WORKLETS_FETCH_PREVIEW_ENABLED
33
+ static void installNetworking(jsi::Runtime &rt, const std::shared_ptr<RuntimeBindings> &runtimeBindings);
34
+ #endif // WORKLETS_FETCH_PREVIEW_ENABLED
35
+ #endif // WORKLETS_BUNDLE_MODE_ENABLED
24
36
  };
25
37
 
26
38
  } // namespace worklets