react-native-worklets 0.7.1 → 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 +16 -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 +17 -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
@@ -10,8 +10,20 @@ worklets_assert_new_architecture_enabled($new_arch_enabled)
10
10
 
11
11
  ios_min_version = '13.4'
12
12
 
13
- feature_flags = "-DWORKLETS_FEATURE_FLAGS=\"#{worklets_get_static_feature_flags()}\""
13
+ feature_flags = $worklets_config[:feature_flags_flag]
14
14
  version_flags = "-DWORKLETS_VERSION=#{package['version']} -DREACT_NATIVE_MINOR_VERSION=#{$worklets_config[:react_native_minor_version]}"
15
+ worklets_profiling_flag = ENV['IS_WORKLETS_PROFILING'] ? '-DWORKLETS_PROFILING' : ''
16
+ bundle_mode_flag = $worklets_config[:bundle_mode_flag]
17
+ fetch_preview_flag = $worklets_config[:fetch_preview_flag]
18
+ hermes_v1_flag = ENV['RCT_HERMES_V1_ENABLED'] == '1' ? '-DHERMES_V1_ENABLED' : ''
19
+
20
+ # React Native doesn't expose these flags, but not having them
21
+ # can lead to runtime errors due to ABI mismatches.
22
+ # There's also
23
+ # HERMESVM_PROFILER_OPCODE
24
+ # HERMESVM_PROFILER_BB
25
+ # which shouldn't be defined in standard setups.
26
+ hermes_debug_hidden_flags = 'HERMES_ENABLE_DEBUGGER=1'
15
27
 
16
28
  Pod::Spec.new do |s|
17
29
  s.name = "RNWorklets"
@@ -47,16 +59,6 @@ Pod::Spec.new do |s|
47
59
  if using_hermes && !$worklets_config[:is_tvos_target]
48
60
  s.dependency 'React-hermes'
49
61
  end
50
-
51
- # React Native doesn't expose these flags, but not having them
52
- # can lead to runtime errors due to ABI mismatches.
53
- # There's also
54
- # HERMESVM_PROFILER_OPCODE
55
- # HERMESVM_PROFILER_BB
56
- # which shouldn't be defined in standard setups.
57
- hermes_debug_hidden_flags = 'HERMES_ENABLE_DEBUGGER=1'
58
-
59
- bundle_mode_flag = $worklets_config[:bundle_mode] ? 'WORKLETS_BUNDLE_MODE=1' : ''
60
62
 
61
63
  s.pod_target_xcconfig = {
62
64
  "USE_HEADERMAP" => "YES",
@@ -73,9 +75,9 @@ Pod::Spec.new do |s|
73
75
  ].join(' '),
74
76
  "FRAMEWORK_SEARCH_PATHS" => '"${PODS_CONFIGURATION_BUILD_DIR}/React-hermes"',
75
77
  "CLANG_CXX_LANGUAGE_STANDARD" => "c++20",
76
- "GCC_PREPROCESSOR_DEFINITIONS[config=*Debug*]" => "$(inherited) #{hermes_debug_hidden_flags} #{bundle_mode_flag}",
77
- "GCC_PREPROCESSOR_DEFINITIONS[config=*Release*]" => "$(inherited) #{bundle_mode_flag}",
78
- "OTHER_CFLAGS" => "$(inherited) #{feature_flags} #{version_flags}",
78
+ "GCC_PREPROCESSOR_DEFINITIONS[config=*Debug*]" => "$(inherited) #{hermes_debug_hidden_flags}",
79
+ "GCC_PREPROCESSOR_DEFINITIONS[config=*Release*]" => "$(inherited)",
80
+ "OTHER_CFLAGS" => "$(inherited) #{feature_flags} #{version_flags} #{worklets_profiling_flag} #{bundle_mode_flag} #{fetch_preview_flag} #{hermes_v1_flag}",
79
81
  }
80
82
  s.xcconfig = {
81
83
  "HEADER_SEARCH_PATHS" => [
@@ -21,11 +21,17 @@ string(
21
21
 
22
22
  string(APPEND CMAKE_CXX_FLAGS " -fno-omit-frame-pointer -fstack-protector-all")
23
23
 
24
+ if(${WORKLETS_PROFILING})
25
+ string(APPEND CMAKE_CXX_FLAGS " -DWORKLETS_PROFILING")
26
+ endif()
24
27
  if(${IS_REANIMATED_EXAMPLE_APP})
25
28
  string(APPEND CMAKE_CXX_FLAGS " -DIS_REANIMATED_EXAMPLE_APP -Wpedantic")
26
29
  endif()
27
- if(${WORKLETS_BUNDLE_MODE})
28
- string(APPEND CMAKE_CXX_FLAGS " -DWORKLETS_BUNDLE_MODE")
30
+ if(${WORKLETS_BUNDLE_MODE_ENABLED})
31
+ string(APPEND CMAKE_CXX_FLAGS " -DWORKLETS_BUNDLE_MODE_ENABLED")
32
+ endif()
33
+ if(${WORKLETS_FETCH_PREVIEW_ENABLED})
34
+ string(APPEND CMAKE_CXX_FLAGS " -DWORKLETS_FETCH_PREVIEW_ENABLED")
29
35
  endif()
30
36
 
31
37
  if(NOT ${CMAKE_BUILD_TYPE} MATCHES "Debug")
@@ -1,7 +1,7 @@
1
1
  import com.android.build.gradle.tasks.ExternalNativeBuildJsonTask
2
2
  import groovy.json.JsonSlurper
3
- import java.nio.file.Paths
4
3
  import org.apache.tools.ant.taskdefs.condition.Os
4
+ import javax.inject.Inject
5
5
 
6
6
  def safeExtGet(prop, fallback) {
7
7
  rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
@@ -13,7 +13,12 @@ def safeAppExtGet(prop, fallback) {
13
13
  }
14
14
 
15
15
  def isNewArchitectureEnabled() {
16
- // To opt-in for the New Architecture, you can either:
16
+ // In React Native 0.82+, users can no longer opt-out of the New Architecture.
17
+ if(getReactNativeMinorVersion() >= 82){
18
+ return true
19
+ }
20
+
21
+ // In older versions, to opt-in for the New Architecture, you can either:
17
22
  // - Set `newArchEnabled` to true inside the `gradle.properties` file
18
23
  // - Invoke gradle with `-newArchEnabled=true`
19
24
  // - Set an environment variable `ORG_GRADLE_PROJECT_newArchEnabled=true`
@@ -27,19 +32,15 @@ def resolveReactNativeDirectory() {
27
32
  }
28
33
 
29
34
  // Fallback to node resolver for custom directory structures like monorepos.
30
- def reactNativePackage = file(
31
- providers.exec {
32
- workingDir(rootDir)
33
- commandLine("node", "--print", "require.resolve('react-native/package.json')")
34
- }.standardOutput.asText.get().trim()
35
- )
35
+ def reactNativePackage = file(providers.exec {
36
+ workingDir(rootDir)
37
+ commandLine("node", "--print", "require.resolve('react-native/package.json')")
38
+ }.standardOutput.asText.get().trim())
36
39
  if (reactNativePackage.exists()) {
37
40
  return reactNativePackage.parentFile
38
41
  }
39
42
 
40
- throw new GradleException(
41
- "[Worklets] Unable to resolve react-native location in node_modules. You should set project extension property (in `app/build.gradle`) named `REACT_NATIVE_NODE_MODULES_DIR` with the path to react-native in node_modules."
42
- )
43
+ throw new GradleException("[Worklets] Unable to resolve react-native location in node_modules. You should set project extension property (in `app/build.gradle`) named `REACT_NATIVE_NODE_MODULES_DIR` with the path to react-native in node_modules.")
43
44
  }
44
45
 
45
46
  def getReactNativeVersion() {
@@ -60,7 +61,7 @@ def getWorkletsVersion() {
60
61
  return json.version
61
62
  }
62
63
 
63
- def toPlatformFileString(String path) {
64
+ static def toPlatformFileString(String path) {
64
65
  if (Os.isFamily(Os.FAMILY_WINDOWS)) {
65
66
  path = path.replace(File.separatorChar, '/' as char)
66
67
  }
@@ -74,25 +75,33 @@ def getStaticFeatureFlags() {
74
75
  if (!staticFeatureFlagsFile.exists()) {
75
76
  throw new GradleException("[Worklets] Feature flags file not found at ${staticFeatureFlagsFile.absolutePath}.")
76
77
  }
77
- new JsonSlurper().parseText(staticFeatureFlagsFile.text).each { key, value ->
78
- featureFlags[key] = value.toString()
78
+ new JsonSlurper().parseText(staticFeatureFlagsFile.text).each { key, value -> featureFlags[key] = value.toString()
79
79
  }
80
80
 
81
81
  def packageJsonFile = file(rootDir.path + "/../package.json")
82
82
  if (packageJsonFile.exists()) {
83
83
  def packageJson = new JsonSlurper().parseText(packageJsonFile.text)
84
- packageJson.worklets?.staticFeatureFlags?.each { key, value ->
85
- featureFlags[key] = value.toString()
84
+ packageJson.worklets?.staticFeatureFlags?.each { key, value -> featureFlags[key] = value.toString()
86
85
  }
87
86
  }
88
87
 
88
+ return featureFlags;
89
+ }
90
+
91
+ def getStaticFeatureFlagsString(featureFlags) {
89
92
  return featureFlags.collect { key, value -> "[${key}:${value}]" }.join("")
90
93
  }
91
94
 
92
- if (isNewArchitectureEnabled()) {
95
+ def isFlagEnabled(featureFlags, flagName) {
96
+ return (featureFlags.containsKey(flagName) && featureFlags[flagName] == "true");
97
+ }
98
+
99
+ if (isNewArchitectureEnabled() && project != rootProject) {
93
100
  apply plugin: "com.facebook.react"
94
101
  }
95
102
 
103
+ def featureFlags = getStaticFeatureFlags()
104
+
96
105
  def packageDir = project.projectDir.parentFile
97
106
  def reactNativeRootDir = resolveReactNativeDirectory()
98
107
  def REACT_NATIVE_MINOR_VERSION = getReactNativeMinorVersion()
@@ -100,14 +109,16 @@ def REACT_NATIVE_VERSION = getReactNativeVersion()
100
109
  def WORKLETS_VERSION = getWorkletsVersion()
101
110
  def IS_NEW_ARCHITECTURE_ENABLED = isNewArchitectureEnabled()
102
111
  def IS_REANIMATED_EXAMPLE_APP = safeAppExtGet("isReanimatedExampleApp", false)
103
- def BUNDLE_MODE = safeAppExtGet("workletsBundleMode", false)
104
- def WORKLETS_FEATURE_FLAGS = getStaticFeatureFlags()
112
+ def BUNDLE_MODE_ENABLED = isFlagEnabled(featureFlags, "BUNDLE_MODE_ENABLED");
113
+ def FETCH_PREVIEW_ENABLED = isFlagEnabled(featureFlags, "FETCH_PREVIEW_ENABLED");
114
+ def WORKLETS_FEATURE_FLAGS = getStaticFeatureFlagsString(featureFlags)
105
115
  def HERMES_V1_ENABLED = safeAppExtGet("hermesV1Enabled", false)
116
+ def WORKLETS_PROFILING = safeAppExtGet("enableWorkletsProfiling", false)
106
117
 
107
118
  // Set version for prefab
108
119
  version WORKLETS_VERSION
109
120
 
110
- def workletsPrefabHeadersDir = project.file("$buildDir/prefab-headers/worklets")
121
+ def workletsPrefabHeadersDir = project.file("${getLayout().getBuildDirectory().getAsFile().get().absolutePath}/prefab-headers/worklets")
111
122
 
112
123
  def JS_RUNTIME = {
113
124
  // Override JS runtime with environment variable
@@ -136,20 +147,22 @@ buildscript {
136
147
  mavenCentral()
137
148
  }
138
149
  dependencies {
139
- classpath "com.android.tools.build:gradle:8.2.1"
150
+ classpath "com.android.tools.build:gradle:8.13.1"
140
151
  classpath "de.undercouch:gradle-download-task:5.6.0"
141
- classpath "com.diffplug.spotless:spotless-plugin-gradle:6.25.0"
152
+ classpath "com.diffplug.spotless:spotless-plugin-gradle:8.1.0"
142
153
  }
143
154
  }
144
155
 
145
- if (project == rootProject) {
146
- apply from: "spotless.gradle"
147
- }
148
-
149
156
  apply plugin: "com.android.library"
150
157
  apply plugin: "maven-publish"
151
158
  apply plugin: "de.undercouch.download"
152
159
 
160
+ if (project == rootProject) {
161
+ apply from: "spotless.gradle"
162
+ } else {
163
+ apply plugin: "org.jetbrains.kotlin.android"
164
+ }
165
+
153
166
  apply from: "./fix-prefab.gradle"
154
167
 
155
168
 
@@ -183,10 +196,11 @@ android {
183
196
  versionCode 1
184
197
  versionName WORKLETS_VERSION
185
198
 
199
+ buildConfigField("boolean", "WORKLETS_PROFILING", WORKLETS_PROFILING.toString())
186
200
  buildConfigField("boolean", "IS_INTERNAL_BUILD", "false")
187
201
  buildConfigField("int", "EXOPACKAGE_FLAGS", "0")
188
202
  buildConfigField("int", "REACT_NATIVE_MINOR_VERSION", REACT_NATIVE_MINOR_VERSION.toString())
189
- buildConfigField("boolean", "BUNDLE_MODE", BUNDLE_MODE.toString())
203
+ buildConfigField("boolean", "BUNDLE_MODE_ENABLED", BUNDLE_MODE_ENABLED.toString())
190
204
 
191
205
  externalNativeBuild {
192
206
  cmake {
@@ -196,12 +210,14 @@ android {
196
210
  "-DREACT_NATIVE_DIR=${toPlatformFileString(reactNativeRootDir.path)}",
197
211
  "-DJS_RUNTIME=${JS_RUNTIME}",
198
212
  "-DIS_REANIMATED_EXAMPLE_APP=${IS_REANIMATED_EXAMPLE_APP}",
199
- "-DWORKLETS_BUNDLE_MODE=${BUNDLE_MODE}",
213
+ "-DWORKLETS_BUNDLE_MODE_ENABLED=${BUNDLE_MODE_ENABLED}",
214
+ "-DWORKLETS_FETCH_PREVIEW_ENABLED=${FETCH_PREVIEW_ENABLED}",
215
+ "-DWORKLETS_PROFILING=${WORKLETS_PROFILING}",
200
216
  "-DWORKLETS_VERSION=${WORKLETS_VERSION}",
201
217
  "-DANDROID_SUPPORT_FLEXIBLE_PAGE_SIZES=ON",
202
218
  "-DWORKLETS_FEATURE_FLAGS=${WORKLETS_FEATURE_FLAGS}",
203
219
  "-DHERMES_V1_ENABLED=${HERMES_V1_ENABLED}"
204
- abiFilters (*reactNativeArchitectures())
220
+ abiFilters(*reactNativeArchitectures())
205
221
  targets("worklets")
206
222
  }
207
223
  }
@@ -240,20 +256,18 @@ android {
240
256
  packagingOptions {
241
257
  // For some reason gradle only complains about the duplicated version of librrc_root and libreact_render libraries
242
258
  // while there are more libraries copied in intermediates folder of the lib build directory, we exclude
243
- // only the ones that make the build fail (ideally we should only include libreanimated but we
259
+ // only the ones that make the build fail (ideally we should only include libworklets but we
244
260
  // are only allowed to specify exclude patterns)
245
- excludes = [
246
- "META-INF",
247
- "META-INF/**",
248
- "**/libc++_shared.so",
249
- "**/libfbjni.so",
250
- "**/libjsi.so",
251
- "**/libhermes.so",
252
- "**/libhermesvm.so",
253
- "**/libhermestooling.so",
254
- "**/libreactnative.so",
255
- "**/libjscexecutor.so",
256
- ]
261
+ excludes = ["META-INF",
262
+ "META-INF/**",
263
+ "**/libc++_shared.so",
264
+ "**/libfbjni.so",
265
+ "**/libjsi.so",
266
+ "**/libhermes.so",
267
+ "**/libhermesvm.so",
268
+ "**/libhermestooling.so",
269
+ "**/libreactnative.so",
270
+ "**/libjscexecutor.so",]
257
271
  }
258
272
  compileOptions {
259
273
  sourceCompatibility JavaVersion.VERSION_17
@@ -262,16 +276,21 @@ android {
262
276
  sourceSets {
263
277
  main {
264
278
  java {
265
- if (BUNDLE_MODE) {
266
- srcDirs += "src/experimentalBundling"
279
+ if (FETCH_PREVIEW_ENABLED) {
280
+ srcDirs += "src/networking"
267
281
  } else {
268
- srcDirs += "src/legacyBundling"
282
+ srcDirs += "src/no-networking"
269
283
  }
270
284
  }
271
285
  }
272
286
  }
273
- tasks.withType(ExternalNativeBuildJsonTask) {
274
- compileTask ->
287
+ if(project != rootProject) {
288
+ kotlinOptions {
289
+ jvmTarget = '17'
290
+ }
291
+ }
292
+ tasks.withType(ExternalNativeBuildJsonTask).tap {
293
+ configureEach { compileTask ->
275
294
  compileTask.doLast {
276
295
  if (!IS_REANIMATED_EXAMPLE_APP) {
277
296
  return
@@ -284,6 +303,7 @@ android {
284
303
 
285
304
  println("Generated clangd metadata.")
286
305
  }
306
+ }
287
307
  }
288
308
  }
289
309
 
@@ -293,7 +313,7 @@ def validateReactNativeVersionResult = providers.exec {
293
313
  ignoreExitValue = true
294
314
  }
295
315
 
296
- task assertMinimalReactNativeVersionTask {
316
+ tasks.register('assertMinimalReactNativeVersionTask') {
297
317
  doFirst {
298
318
  if (validateReactNativeVersionResult.getResult().get().exitValue != 0) {
299
319
  throw new GradleException(validateReactNativeVersionResult.getStandardError().getAsText().get().trim())
@@ -303,7 +323,7 @@ task assertMinimalReactNativeVersionTask {
303
323
 
304
324
  preBuild.dependsOn(assertMinimalReactNativeVersionTask)
305
325
 
306
- task assertNewArchitectureEnabledTask {
326
+ tasks.register('assertNewArchitectureEnabledTask') {
307
327
  onlyIf { !IS_NEW_ARCHITECTURE_ENABLED }
308
328
  doFirst {
309
329
  throw new GradleException("[Worklets] Worklets require new architecture to be enabled. Please enable it by setting `newArchEnabled` to `true` in `gradle.properties`.")
@@ -312,17 +332,25 @@ task assertNewArchitectureEnabledTask {
312
332
 
313
333
  preBuild.dependsOn(assertNewArchitectureEnabledTask)
314
334
 
315
- task prepareWorkletsHeadersForPrefabs(type: Copy) {
335
+ tasks.register('prepareWorkletsHeadersForPrefabs', Copy) {
316
336
  from("$projectDir/src/main/cpp")
317
337
  from("$projectDir/../Common/cpp")
318
338
  include("worklets/**/*.h")
319
- into(workletsPrefabHeadersDir)
339
+ into(workletsPrefabHeadersDir.absolutePath)
340
+ }
341
+
342
+ interface FSService {
343
+ @Inject
344
+ FileSystemOperations getFs()
320
345
  }
321
346
 
322
- task cleanCmakeCache() {
323
- tasks.getByName("clean").dependsOn(cleanCmakeCache)
347
+ tasks.register('cleanCMakeCache') {
348
+ def fsProvider = project.objects.newInstance(FSService)
349
+ def cxxDir = file("${projectDir}/.cxx")
324
350
  doFirst {
325
- delete "${projectDir}/.cxx"
351
+ fsProvider.fs.delete {
352
+ delete cxxDir
353
+ }
326
354
  }
327
355
  }
328
356
 
@@ -333,13 +361,21 @@ repositories {
333
361
 
334
362
  dependencies {
335
363
  implementation "com.facebook.yoga:proguard-annotations:1.19.0"
336
- implementation "androidx.transition:transition:1.1.0"
337
- implementation "androidx.core:core:1.6.0"
364
+ implementation "androidx.transition:transition:1.6.0"
365
+ implementation "androidx.core:core:1.15.0"
338
366
 
339
367
  implementation "com.facebook.react:react-android" // version substituted by RNGP
368
+ implementation "androidx.core:core-ktx:1.17.0"
340
369
  if (JS_RUNTIME == "hermes") {
341
370
  implementation "com.facebook.react:hermes-android" // version substituted by RNGP
342
371
  }
343
372
  }
344
373
 
345
374
  preBuild.dependsOn(prepareWorkletsHeadersForPrefabs)
375
+
376
+ afterEvaluate {
377
+ tasks.named("clean") {
378
+ it.finalizedBy(cleanCMakeCache)
379
+ }
380
+ }
381
+
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Based on
3
+ * https://github.com/facebook/react-native/blob/main/packages/react-native/ReactAndroid/src/main/jni/react/jni/JSLoader.cpp
4
+ */
5
+
6
+ #include <cxxreact/JSBigString.h>
7
+ #include <cxxreact/RecoverableError.h>
8
+ #include <worklets/Tools/ScriptBuffer.h>
9
+ #include <worklets/android/JScriptBufferWrapper.h>
10
+
11
+ #include <utility>
12
+
13
+ namespace worklets {
14
+
15
+ using namespace facebook::jni;
16
+
17
+ jni::local_ref<JScriptBufferWrapper::jhybriddata> JScriptBufferWrapper::initHybridFromAssets(
18
+ jni::alias_ref<jhybridobject> jThis, // NOLINT //(performance-unnecessary-value-param)
19
+ jni::alias_ref<JAssetManager::javaobject> assetManager, // NOLINT //(performance-unnecessary-value-param)
20
+ const std::string &sourceURL) {
21
+ auto manager = extractAssetManager(assetManager);
22
+ auto bigString = loadScriptFromAssets(manager, sourceURL);
23
+ auto script = std::make_shared<ScriptBuffer>(std::move(bigString));
24
+ return makeCxxInstance(std::move(script), sourceURL);
25
+ }
26
+
27
+ jni::local_ref<JScriptBufferWrapper::jhybriddata> JScriptBufferWrapper::initHybridFromFile(
28
+ jni::alias_ref<jhybridobject> jThis, // NOLINT //(performance-unnecessary-value-param)
29
+ const std::string &fileName) {
30
+ std::shared_ptr<const ScriptBuffer> script;
31
+ RecoverableError::runRethrowingAsRecoverable<std::system_error>([&fileName, &script]() {
32
+ auto bigString = JSBigFileString::fromPath(fileName);
33
+ script = std::make_shared<ScriptBuffer>(std::move(bigString));
34
+ });
35
+ return makeCxxInstance(std::move(script), fileName);
36
+ }
37
+
38
+ jni::local_ref<JScriptBufferWrapper::jhybriddata> JScriptBufferWrapper::initHybridFromString(
39
+ jni::alias_ref<jhybridobject> jThis, // NOLINT //(performance-unnecessary-value-param)
40
+ const std::string &scriptStr,
41
+ const std::string &sourceURL) {
42
+ auto bigString = std::make_shared<JSBigStdString>(scriptStr);
43
+ auto script = std::make_shared<ScriptBuffer>(std::move(bigString));
44
+ return makeCxxInstance(std::move(script), sourceURL);
45
+ }
46
+
47
+ JScriptBufferWrapper::JScriptBufferWrapper(
48
+ const std::shared_ptr<const ScriptBuffer> &script,
49
+ const std::string &sourceUrl)
50
+ : script_(script), sourceUrl_(sourceUrl) {}
51
+
52
+ std::shared_ptr<const ScriptBuffer> JScriptBufferWrapper::getScript() const {
53
+ return script_;
54
+ }
55
+
56
+ std::string JScriptBufferWrapper::getSourceUrl() const {
57
+ return sourceUrl_;
58
+ }
59
+
60
+ void JScriptBufferWrapper::registerNatives() {
61
+ registerHybrid({
62
+ makeNativeMethod("initHybridFromAssets", JScriptBufferWrapper::initHybridFromAssets),
63
+ makeNativeMethod("initHybridFromFile", JScriptBufferWrapper::initHybridFromFile),
64
+ makeNativeMethod("initHybridFromString", JScriptBufferWrapper::initHybridFromString),
65
+ });
66
+ }
67
+ } // namespace worklets
@@ -0,0 +1,48 @@
1
+ #pragma once
2
+
3
+ #include <fbjni/fbjni.h>
4
+ #include <jsireact/JSIExecutor.h>
5
+ #include <react/jni/JSLoader.h>
6
+ #include <worklets/Tools/ScriptBuffer.h>
7
+
8
+ #include <memory>
9
+ #include <string>
10
+
11
+ namespace worklets {
12
+
13
+ using namespace facebook;
14
+ using namespace facebook::react;
15
+
16
+ /**
17
+ * JScriptBufferWrapper is a JNI wrapper class that holds a ScriptBuffer containing
18
+ * JavaScript code and its source URL, to be evaluated on a WorkletRuntime.
19
+ */
20
+ class JScriptBufferWrapper : public jni::HybridClass<JScriptBufferWrapper> {
21
+ public:
22
+ constexpr static const char *const kJavaDescriptor = "Lcom/swmansion/worklets/ScriptBufferWrapper;";
23
+
24
+ static void registerNatives();
25
+
26
+ [[nodiscard]] std::shared_ptr<const ScriptBuffer> getScript() const;
27
+ [[nodiscard]] std::string getSourceUrl() const;
28
+
29
+ private:
30
+ static jni::local_ref<JScriptBufferWrapper::jhybriddata> initHybridFromAssets(
31
+ jni::alias_ref<jhybridobject> jThis,
32
+ jni::alias_ref<JAssetManager::javaobject> assetManager,
33
+ const std::string &assetURL);
34
+ static jni::local_ref<JScriptBufferWrapper::jhybriddata> initHybridFromFile(
35
+ jni::alias_ref<jhybridobject> jThis,
36
+ const std::string &fileName);
37
+ static jni::local_ref<JScriptBufferWrapper::jhybriddata>
38
+ initHybridFromString(jni::alias_ref<jhybridobject> jThis, const std::string &script, const std::string &sourceURL);
39
+
40
+ friend HybridBase;
41
+
42
+ explicit JScriptBufferWrapper(const std::shared_ptr<const ScriptBuffer> &script, const std::string &sourceUrl);
43
+
44
+ const std::shared_ptr<const ScriptBuffer> script_;
45
+ const std::string sourceUrl_;
46
+ };
47
+
48
+ } // namespace worklets
@@ -0,0 +1,52 @@
1
+ #if defined(WORKLETS_BUNDLE_MODE_ENABLED) && defined(WORKLETS_FETCH_PREVIEW_ENABLED)
2
+
3
+ #include <jsi/JSIDynamic.h>
4
+ #include <jsi/jsi.h>
5
+ #include <worklets/android/JWorkletRuntimeWrapper.h>
6
+
7
+ #include <utility>
8
+ #include <vector>
9
+
10
+ namespace worklets {
11
+
12
+ using namespace facebook::jni;
13
+
14
+ local_ref<JWorkletRuntimeWrapper::JavaPart> JWorkletRuntimeWrapper::makeJWorkletRuntimeWrapper(
15
+ std::shared_ptr<WorkletRuntime> workletRuntime) {
16
+ return JWorkletRuntimeWrapper::newObjectCxxArgs(std::move(workletRuntime), workletRuntime->getRuntimeId());
17
+ }
18
+
19
+ JWorkletRuntimeWrapper::JWorkletRuntimeWrapper(std::shared_ptr<WorkletRuntime> workletRuntime, uint64_t runtimeId)
20
+ : workletRuntime_(std::move(workletRuntime)) {}
21
+
22
+ void JWorkletRuntimeWrapper::cxxEmitDeviceEvent(
23
+ jni::alias_ref<WritableNativeArray::javaobject> params // NOLINT //(performance-unnecessary-value-param
24
+ ) {
25
+ workletRuntime_->schedule([params = params->cthis()->consume()](jsi::Runtime &rt) {
26
+ facebook::jsi::Value emitter = rt.global().getProperty(rt, "__rctDeviceEventEmitter");
27
+ if (!emitter.isUndefined()) {
28
+ facebook::jsi::Object emitterObject = emitter.asObject(rt);
29
+ facebook::jsi::Function emitFunction = emitterObject.getPropertyAsFunction(rt, "emit");
30
+ std::vector<jsi::Value> jsArgs;
31
+ for (auto &param : params) {
32
+ jsArgs.push_back(jsi::valueFromDynamic(rt, param));
33
+ }
34
+
35
+ emitFunction.callWithThis(rt, emitterObject, static_cast<const jsi::Value *>(jsArgs.data()), jsArgs.size());
36
+ }
37
+ });
38
+ }
39
+
40
+ int JWorkletRuntimeWrapper::cxxGetRuntimeId() {
41
+ return static_cast<int>(workletRuntime_->getRuntimeId());
42
+ }
43
+
44
+ void JWorkletRuntimeWrapper::registerNatives() {
45
+ javaClassStatic()->registerNatives({});
46
+ registerHybrid(
47
+ {makeNativeMethod("cxxEmitDeviceEvent", JWorkletRuntimeWrapper::cxxEmitDeviceEvent),
48
+ makeNativeMethod("cxxGetRuntimeId", JWorkletRuntimeWrapper::cxxGetRuntimeId)});
49
+ }
50
+ } // namespace worklets
51
+
52
+ #endif // defined(WORKLETS_BUNDLE_MODE_ENABLED) && defined(WORKLETS_FETCH_PREVIEW_ENABLED)
@@ -0,0 +1,43 @@
1
+ #pragma once
2
+
3
+ #if defined(WORKLETS_BUNDLE_MODE_ENABLED) && defined(WORKLETS_FETCH_PREVIEW_ENABLED)
4
+
5
+ #include <fbjni/fbjni.h>
6
+ #include <react/jni/ReadableNativeArray.h>
7
+ #include <react/jni/WritableNativeArray.h>
8
+ #include <worklets/WorkletRuntime/WorkletRuntime.h>
9
+
10
+ #include <memory>
11
+
12
+ namespace worklets {
13
+
14
+ using namespace facebook;
15
+ using namespace facebook::react;
16
+ using namespace facebook::jni;
17
+
18
+ /**
19
+ * JNI wrapper around WorkletRuntime to expose its methods to Java.
20
+ */
21
+ class JWorkletRuntimeWrapper : public jni::HybridClass<JWorkletRuntimeWrapper> {
22
+ public:
23
+ constexpr static const char *const kJavaDescriptor = "Lcom/swmansion/worklets/WorkletRuntimeWrapper;";
24
+
25
+ static local_ref<JWorkletRuntimeWrapper::JavaPart> makeJWorkletRuntimeWrapper(
26
+ std::shared_ptr<WorkletRuntime> workletRuntime);
27
+
28
+ static void registerNatives();
29
+
30
+ void cxxEmitDeviceEvent(jni::alias_ref<WritableNativeArray::javaobject> params);
31
+ int cxxGetRuntimeId();
32
+
33
+ private:
34
+ friend HybridBase;
35
+
36
+ JWorkletRuntimeWrapper(std::shared_ptr<WorkletRuntime> workletRuntime, uint64_t runtimeId);
37
+
38
+ const std::shared_ptr<WorkletRuntime> workletRuntime_;
39
+ };
40
+
41
+ } // namespace worklets
42
+
43
+ #endif // defined(WORKLETS_BUNDLE_MODE_ENABLED) && defined(WORKLETS_FETCH_PREVIEW_ENABLED)