@solidnative-dev/fabric-host 0.0.0-stage → 0.1.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +87 -2
  3. package/dist/fatal-error-handler.d.ts +27 -0
  4. package/dist/fatal-error-handler.d.ts.map +1 -0
  5. package/dist/fatal-error-handler.js +106 -0
  6. package/dist/fatal-error-handler.js.map +1 -0
  7. package/dist/index.d.ts +7 -0
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +7 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/native-binding.d.ts +183 -0
  12. package/dist/native-binding.d.ts.map +1 -0
  13. package/dist/native-binding.js +554 -0
  14. package/dist/native-binding.js.map +1 -0
  15. package/dist/native-fabric-host.d.ts +32 -0
  16. package/dist/native-fabric-host.d.ts.map +1 -0
  17. package/dist/native-fabric-host.js +275 -0
  18. package/dist/native-fabric-host.js.map +1 -0
  19. package/dist/native-transaction.d.ts +17 -0
  20. package/dist/native-transaction.d.ts.map +1 -0
  21. package/dist/native-transaction.js +124 -0
  22. package/dist/native-transaction.js.map +1 -0
  23. package/dist/react-native-0.87.d.ts +61 -0
  24. package/dist/react-native-0.87.d.ts.map +1 -0
  25. package/dist/react-native-0.87.js +77 -0
  26. package/dist/react-native-0.87.js.map +1 -0
  27. package/dist/react-native-0.88.d.ts +77 -0
  28. package/dist/react-native-0.88.d.ts.map +1 -0
  29. package/dist/react-native-0.88.js +79 -0
  30. package/dist/react-native-0.88.js.map +1 -0
  31. package/dist/surface-readiness.d.ts +18 -0
  32. package/dist/surface-readiness.d.ts.map +1 -0
  33. package/dist/surface-readiness.js +43 -0
  34. package/dist/surface-readiness.js.map +1 -0
  35. package/native/README.md +502 -0
  36. package/native/SolidNativeFabric.podspec +84 -0
  37. package/native/android/SolidNativeBindingsInstaller.cpp +4218 -0
  38. package/native/android/SolidNativeBindingsInstaller.h +8 -0
  39. package/native/android/SolidNativeRuntime.cmake +61 -0
  40. package/native/android/java/dev/solidnative/runtime/SolidNativeBindingsInstaller.kt +239 -0
  41. package/native/android/java/dev/solidnative/runtime/SolidNativeDebugModule.kt +191 -0
  42. package/native/android/java/dev/solidnative/runtime/SolidNativePackage.kt +43 -0
  43. package/native/android/java/dev/solidnative/runtime/SolidNativePlatformModule.kt +81 -0
  44. package/native/android/java/dev/solidnative/runtime/SolidNativeStartupFailureView.kt +93 -0
  45. package/native/android/java/dev/solidnative/runtime/SolidNativeSurface.kt +423 -0
  46. package/native/android/solid-native.gradle +90 -0
  47. package/native/apple/SolidNativeDebugRequest.mm +421 -0
  48. package/native/fabric/SolidNativeDebugRequest.h +12 -0
  49. package/native/fabric/SolidNativeDirtyNodes.h +26 -0
  50. package/native/fabric/SolidNativeFabricApi.cpp +224 -0
  51. package/native/fabric/SolidNativeFabricApi.h +107 -0
  52. package/native/fabric/SolidNativeFabricApplication.h +32 -0
  53. package/native/fabric/SolidNativeFabricApplication.mm +373 -0
  54. package/native/fabric/SolidNativeFabricJSIBinding.h +43 -0
  55. package/native/fabric/SolidNativeFabricJSIBinding.mm +4491 -0
  56. package/native/fabric/SolidNativeFabricSmokeApplication.h +26 -0
  57. package/native/fabric/SolidNativeFabricSmokeApplication.mm +331 -0
  58. package/native/fabric/SolidNativeFabricSurface.h +63 -0
  59. package/native/fabric/SolidNativeFabricSurface.mm +549 -0
  60. package/native/fabric/SolidNativeFabricTransactionCoordinator.cpp +1275 -0
  61. package/native/fabric/SolidNativeFabricTransactionCoordinator.h +90 -0
  62. package/native/fabric/SolidNativeFabricTransactionCoordinatorApple.mm +97 -0
  63. package/native/fabric/SolidNativeRootViewController.h +20 -0
  64. package/native/fabric/SolidNativeRootViewController.mm +83 -0
  65. package/native/fabric/SolidNativeStagedNodeMap.h +104 -0
  66. package/native/fabric/SolidNativeStartupFailureView.h +17 -0
  67. package/native/fabric/SolidNativeStartupFailureView.mm +77 -0
  68. package/native/react-native-boundary.json +106 -0
  69. package/native/worklets/SolidNativeUIWorklet.cpp +1080 -0
  70. package/native/worklets/SolidNativeUIWorklet.h +245 -0
  71. package/package.json +41 -3
  72. package/scripts/verify-react-native-boundary.mjs +282 -0
@@ -0,0 +1,8 @@
1
+ #pragma once
2
+
3
+ namespace solid_native::android {
4
+
5
+ /** Registers the package-owned React Native Fabric Android JSI installer. */
6
+ void registerSolidNativeBindingsInstaller();
7
+
8
+ } // namespace solid_native::android
@@ -0,0 +1,61 @@
1
+ include_guard(GLOBAL)
2
+
3
+ if(NOT DEFINED REACT_ANDROID_DIR)
4
+ message(FATAL_ERROR "Solid Native requires REACT_ANDROID_DIR from React Native's application CMake integration.")
5
+ endif()
6
+
7
+ foreach(_SOLID_NATIVE_VERSION_PART MAJOR MINOR PATCH)
8
+ if(NOT DEFINED SOLID_NATIVE_REACT_NATIVE_VERSION_${_SOLID_NATIVE_VERSION_PART})
9
+ message(FATAL_ERROR "Solid Native requires SOLID_NATIVE_REACT_NATIVE_VERSION_${_SOLID_NATIVE_VERSION_PART} from its reviewed boundary manifest.")
10
+ endif()
11
+ endforeach()
12
+
13
+ set(_SOLID_NATIVE_ANDROID_DIR "${CMAKE_CURRENT_LIST_DIR}")
14
+ get_filename_component(
15
+ _SOLID_NATIVE_NATIVE_DIR
16
+ "${_SOLID_NATIVE_ANDROID_DIR}/.."
17
+ ABSOLUTE
18
+ )
19
+ set(_SOLID_NATIVE_FABRIC_DIR "${_SOLID_NATIVE_NATIVE_DIR}/fabric")
20
+ set(_SOLID_NATIVE_WORKLETS_DIR "${_SOLID_NATIVE_NATIVE_DIR}/worklets")
21
+
22
+ function(solid_native_configure_android_target target)
23
+ if(NOT TARGET "${target}")
24
+ message(FATAL_ERROR "Solid Native cannot configure missing CMake target '${target}'.")
25
+ endif()
26
+ get_target_property(_solid_native_configured "${target}" SOLID_NATIVE_RUNTIME_CONFIGURED)
27
+ if(_solid_native_configured)
28
+ return()
29
+ endif()
30
+
31
+ target_sources(
32
+ "${target}"
33
+ PRIVATE
34
+ "${_SOLID_NATIVE_ANDROID_DIR}/SolidNativeBindingsInstaller.cpp"
35
+ "${_SOLID_NATIVE_FABRIC_DIR}/SolidNativeFabricApi.cpp"
36
+ "${_SOLID_NATIVE_FABRIC_DIR}/SolidNativeFabricTransactionCoordinator.cpp"
37
+ "${_SOLID_NATIVE_WORKLETS_DIR}/SolidNativeUIWorklet.cpp"
38
+ )
39
+
40
+ # React Native's Android Folly binary encodes its feature mode in an ABI
41
+ # link check. Compile the shared coordinator with that exact mode.
42
+ target_compile_options("${target}" PRIVATE ${folly_FLAGS})
43
+ target_compile_definitions(
44
+ "${target}"
45
+ PRIVATE
46
+ "SOLID_NATIVE_REACT_NATIVE_VERSION_MAJOR=${SOLID_NATIVE_REACT_NATIVE_VERSION_MAJOR}"
47
+ "SOLID_NATIVE_REACT_NATIVE_VERSION_MINOR=${SOLID_NATIVE_REACT_NATIVE_VERSION_MINOR}"
48
+ "SOLID_NATIVE_REACT_NATIVE_VERSION_PATCH=${SOLID_NATIVE_REACT_NATIVE_VERSION_PATCH}"
49
+ )
50
+
51
+ target_include_directories(
52
+ "${target}"
53
+ PRIVATE
54
+ "${REACT_ANDROID_DIR}/src/main/jni"
55
+ "${REACT_ANDROID_DIR}/../ReactCommon"
56
+ "${_SOLID_NATIVE_ANDROID_DIR}"
57
+ "${_SOLID_NATIVE_FABRIC_DIR}"
58
+ "${_SOLID_NATIVE_WORKLETS_DIR}"
59
+ )
60
+ set_property(TARGET "${target}" PROPERTY SOLID_NATIVE_RUNTIME_CONFIGURED TRUE)
61
+ endfunction()
@@ -0,0 +1,239 @@
1
+ package dev.solidnative.runtime
2
+
3
+ import android.os.Handler
4
+ import android.os.Looper
5
+ import android.util.Log
6
+ import android.view.Choreographer
7
+ import java.util.concurrent.CountDownLatch
8
+ import java.util.concurrent.TimeUnit
9
+ import com.facebook.jni.HybridData
10
+ import com.facebook.jni.annotations.DoNotStrip
11
+ import com.facebook.react.runtime.BindingsInstaller
12
+ import com.facebook.soloader.SoLoader
13
+
14
+ /** Installs the Solid Native host object before the application bundle runs. */
15
+ @DoNotStrip
16
+ class SolidNativeBindingsInstaller private constructor(hybridData: HybridData) :
17
+ BindingsInstaller(hybridData) {
18
+
19
+ constructor() : this(initHybrid())
20
+
21
+ external fun publishSurface(surfaceId: Int)
22
+
23
+ external fun clearSurface(surfaceId: Int)
24
+
25
+ external fun deactivateSurface(surfaceId: Int)
26
+
27
+ fun setSurfaceStopHandler(handler: ((Int) -> Unit)?) {
28
+ surfaceStopHandler = handler
29
+ }
30
+
31
+ fun setFatalErrorHandler(handler: ((Int, String, String) -> Unit)?) {
32
+ fatalErrorHandler = handler
33
+ }
34
+
35
+ fun setUIWorkletOutputHandler(handler: ((UIWorkletFrame) -> Boolean)?) {
36
+ uiWorkletOutputHandler = handler
37
+ }
38
+
39
+ fun setUIWorkletPanGestureHandlers(
40
+ attach: ((Long, Int) -> Boolean)?,
41
+ detach: ((Long) -> Unit)?,
42
+ ) {
43
+ uiWorkletPanAttachHandler = attach
44
+ uiWorkletPanDetachHandler = detach
45
+ }
46
+
47
+ companion object {
48
+ private val mainHandler = Handler(Looper.getMainLooper())
49
+
50
+ @Volatile private var surfaceStopHandler: ((Int) -> Unit)? = null
51
+
52
+ @Volatile private var fatalErrorHandler: ((Int, String, String) -> Unit)? = null
53
+
54
+ @Volatile private var uiWorkletOutputHandler: ((UIWorkletFrame) -> Boolean)? = null
55
+
56
+ @Volatile private var uiWorkletPanAttachHandler: ((Long, Int) -> Boolean)? = null
57
+
58
+ @Volatile private var uiWorkletPanDetachHandler: ((Long) -> Unit)? = null
59
+
60
+ @Volatile private var lastUIWorkletFrame: UIWorkletFrame? = null
61
+
62
+ init {
63
+ SoLoader.loadLibrary("appmodules")
64
+ }
65
+
66
+ @JvmStatic @DoNotStrip private external fun initHybrid(): HybridData
67
+
68
+ @JvmStatic
69
+ @DoNotStrip
70
+ fun requestSurfaceStop(surfaceId: Int) {
71
+ mainHandler.post { surfaceStopHandler?.invoke(surfaceId) }
72
+ }
73
+
74
+ @JvmStatic
75
+ @DoNotStrip
76
+ fun requestFatalError(surfaceId: Int, name: String, message: String) {
77
+ mainHandler.post { fatalErrorHandler?.invoke(surfaceId, name, message) }
78
+ }
79
+
80
+ @JvmStatic
81
+ @DoNotStrip
82
+ fun requestCommitFrame(token: Long) {
83
+ mainHandler.post {
84
+ Choreographer.getInstance().postFrameCallback {
85
+ onCommitFrame(token)
86
+ }
87
+ }
88
+ }
89
+
90
+ @JvmStatic
91
+ @DoNotStrip
92
+ fun requestUIWorkletFrame(token: Long) {
93
+ mainHandler.post {
94
+ Choreographer.getInstance().postFrameCallback { frameTimeNanoseconds ->
95
+ onUIWorkletFrame(token, frameTimeNanoseconds)
96
+ }
97
+ }
98
+ }
99
+
100
+ @JvmStatic
101
+ @DoNotStrip
102
+ private fun attachUIWorkletPanGesture(token: Long, targetTag: Int): Boolean {
103
+ val handler = uiWorkletPanAttachHandler ?: return false
104
+ if (Looper.myLooper() === Looper.getMainLooper()) {
105
+ return handler(token, targetTag)
106
+ }
107
+ val completed = CountDownLatch(1)
108
+ var attached = false
109
+ mainHandler.post {
110
+ try {
111
+ attached = uiWorkletPanAttachHandler?.invoke(token, targetTag) == true
112
+ } catch (error: Throwable) {
113
+ Log.e(TAG, "Failed to attach a native UI worklet pan.", error)
114
+ } finally {
115
+ completed.countDown()
116
+ }
117
+ }
118
+ return try {
119
+ completed.await(PAN_ATTACHMENT_TIMEOUT_SECONDS, TimeUnit.SECONDS) && attached
120
+ } catch (_: InterruptedException) {
121
+ Thread.currentThread().interrupt()
122
+ false
123
+ }
124
+ }
125
+
126
+ @JvmStatic
127
+ @DoNotStrip
128
+ private fun detachUIWorkletPanGesture(token: Long) {
129
+ if (Looper.myLooper() === Looper.getMainLooper()) {
130
+ uiWorkletPanDetachHandler?.invoke(token)
131
+ } else {
132
+ mainHandler.post { uiWorkletPanDetachHandler?.invoke(token) }
133
+ }
134
+ }
135
+
136
+ @JvmStatic
137
+ fun dispatchUIWorkletPanGesture(
138
+ token: Long,
139
+ phase: Int,
140
+ translationX: Double,
141
+ translationY: Double,
142
+ timestamp: Double,
143
+ velocityX: Double,
144
+ velocityY: Double,
145
+ ) {
146
+ onUIWorkletPan(
147
+ token,
148
+ phase,
149
+ translationX,
150
+ translationY,
151
+ timestamp,
152
+ velocityX,
153
+ velocityY,
154
+ )
155
+ }
156
+
157
+ @JvmStatic
158
+ @DoNotStrip
159
+ private fun applyUIWorkletFrame(
160
+ handle: Long,
161
+ targetTag: Int,
162
+ sequence: Long,
163
+ frameTimeNanoseconds: Long,
164
+ opacity: Double,
165
+ translateX: Double,
166
+ translateY: Double,
167
+ scaleX: Double,
168
+ scaleY: Double,
169
+ rotation: Double,
170
+ ): Boolean {
171
+ val onMainThread = Looper.myLooper() === Looper.getMainLooper()
172
+ val frame =
173
+ UIWorkletFrame(
174
+ handle = handle,
175
+ targetTag = targetTag,
176
+ sequence = sequence,
177
+ frameTimeNanoseconds = frameTimeNanoseconds,
178
+ opacity = opacity,
179
+ translateX = translateX,
180
+ translateY = translateY,
181
+ scaleX = scaleX,
182
+ scaleY = scaleY,
183
+ rotation = rotation,
184
+ onMainThread = onMainThread,
185
+ )
186
+ if (!onMainThread) {
187
+ lastUIWorkletFrame = frame.copy(applied = false)
188
+ return false
189
+ }
190
+ val applied =
191
+ try {
192
+ uiWorkletOutputHandler?.invoke(frame) == true
193
+ } catch (error: Throwable) {
194
+ Log.e(TAG, "Failed to apply a native UI worklet frame.", error)
195
+ false
196
+ }
197
+ lastUIWorkletFrame = frame.copy(applied = applied)
198
+ return applied
199
+ }
200
+
201
+ @JvmStatic fun getLastUIWorkletFrame(): UIWorkletFrame? = lastUIWorkletFrame
202
+
203
+ @JvmStatic @DoNotStrip private external fun onCommitFrame(token: Long)
204
+
205
+ @JvmStatic
206
+ @DoNotStrip
207
+ private external fun onUIWorkletFrame(token: Long, frameTimeNanoseconds: Long)
208
+
209
+ @JvmStatic
210
+ @DoNotStrip
211
+ private external fun onUIWorkletPan(
212
+ token: Long,
213
+ phase: Int,
214
+ translationX: Double,
215
+ translationY: Double,
216
+ timestamp: Double,
217
+ velocityX: Double,
218
+ velocityY: Double,
219
+ )
220
+
221
+ private const val TAG = "SOLID_NATIVE"
222
+ private const val PAN_ATTACHMENT_TIMEOUT_SECONDS = 5L
223
+ }
224
+ }
225
+
226
+ data class UIWorkletFrame(
227
+ val handle: Long,
228
+ val targetTag: Int,
229
+ val sequence: Long,
230
+ val frameTimeNanoseconds: Long,
231
+ val opacity: Double,
232
+ val translateX: Double,
233
+ val translateY: Double,
234
+ val scaleX: Double,
235
+ val scaleY: Double,
236
+ val rotation: Double,
237
+ val onMainThread: Boolean,
238
+ val applied: Boolean = false,
239
+ )
@@ -0,0 +1,191 @@
1
+ package dev.solidnative.runtime
2
+
3
+ import android.content.BroadcastReceiver
4
+ import android.content.Context
5
+ import android.content.Intent
6
+ import android.content.IntentFilter
7
+ import android.content.pm.ApplicationInfo
8
+ import android.os.Build
9
+ import com.facebook.react.bridge.Arguments
10
+ import com.facebook.react.bridge.Promise
11
+ import com.facebook.react.bridge.ReactApplicationContext
12
+ import com.facebook.react.bridge.ReactMethod
13
+ import com.facebook.react.module.annotations.ReactModule
14
+ import java.io.File
15
+ import java.io.FileOutputStream
16
+
17
+ /**
18
+ * Debuggable-build-only, app-private mailbox for bounded one-shot debug operations.
19
+ * The request receiver is not exported and responses never leave app storage.
20
+ */
21
+ @ReactModule(name = SolidNativeDebugModule.NAME)
22
+ class SolidNativeDebugModule(
23
+ private val context: ReactApplicationContext,
24
+ ) : NativeSolidNativeDebugSpec(context) {
25
+ private val lock = Any()
26
+ private val pendingRequestIds = linkedSetOf<String>()
27
+ private var listenerReady = false
28
+ private var receiverRegistered = false
29
+
30
+ private val requestReceiver =
31
+ object : BroadcastReceiver() {
32
+ override fun onReceive(context: Context?, intent: Intent?) {
33
+ if (intent?.action != requestAction()) return
34
+ val requestId = intent.getStringExtra(REQUEST_ID_EXTRA) ?: return
35
+ if (!REQUEST_ID_PATTERN.matches(requestId)) return
36
+ val operation = intent.getStringExtra(OPERATION_EXTRA) ?: OPERATION_CAUSAL_SNAPSHOT
37
+ if (operation !in ALLOWED_OPERATIONS) return
38
+ val sessionId = intent.getStringExtra(SESSION_ID_EXTRA)
39
+ if (operation == OPERATION_SOLID_DIAGNOSTICS_END) {
40
+ if (sessionId == null || !REQUEST_ID_PATTERN.matches(sessionId)) return
41
+ } else if (sessionId != null) {
42
+ return
43
+ }
44
+ val accepted = synchronized(lock) {
45
+ if (!listenerReady) return
46
+ while (pendingRequestIds.size >= MAX_PENDING_REQUESTS) {
47
+ pendingRequestIds.remove(pendingRequestIds.first())
48
+ }
49
+ pendingRequestIds.add(requestId)
50
+ }
51
+ if (!accepted) return
52
+ responseFile(requestId).delete()
53
+ val event =
54
+ Arguments.createMap().apply {
55
+ putString("requestId", requestId)
56
+ putString("operation", operation)
57
+ if (sessionId != null) putString("sessionId", sessionId)
58
+ }
59
+ try {
60
+ if (!this@SolidNativeDebugModule.context.hasActiveReactInstance()) {
61
+ synchronized(lock) { pendingRequestIds.remove(requestId) }
62
+ return
63
+ }
64
+ emitOnSnapshotRequest(event)
65
+ } catch (error: Throwable) {
66
+ synchronized(lock) { pendingRequestIds.remove(requestId) }
67
+ }
68
+ }
69
+ }
70
+
71
+ override fun getName(): String = NAME
72
+
73
+ override fun initialize() {
74
+ super.initialize()
75
+ if (!isDebuggable() || receiverRegistered) return
76
+ val filter = IntentFilter(requestAction())
77
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
78
+ context.registerReceiver(requestReceiver, filter, Context.RECEIVER_NOT_EXPORTED)
79
+ } else {
80
+ @Suppress("DEPRECATION") context.registerReceiver(requestReceiver, filter)
81
+ }
82
+ receiverRegistered = true
83
+ }
84
+
85
+ override fun invalidate() {
86
+ synchronized(lock) {
87
+ listenerReady = false
88
+ pendingRequestIds.clear()
89
+ }
90
+ if (receiverRegistered) {
91
+ try {
92
+ context.unregisterReceiver(requestReceiver)
93
+ } catch (_: IllegalArgumentException) {
94
+ }
95
+ receiverRegistered = false
96
+ }
97
+ super.invalidate()
98
+ }
99
+
100
+ @ReactMethod
101
+ override fun setSnapshotListenerReady(ready: Boolean) {
102
+ synchronized(lock) {
103
+ listenerReady = ready
104
+ if (!ready) pendingRequestIds.clear()
105
+ }
106
+ }
107
+
108
+ @ReactMethod
109
+ override fun publishSnapshot(requestId: String, payload: String, promise: Promise) {
110
+ if (!isDebuggable()) {
111
+ promise.reject(ERROR_UNAVAILABLE, "Native debug transport requires a debuggable build.")
112
+ return
113
+ }
114
+ if (!REQUEST_ID_PATTERN.matches(requestId)) {
115
+ promise.reject(ERROR_REQUEST, "Native debug request ID is invalid.")
116
+ return
117
+ }
118
+ val accepted = synchronized(lock) { pendingRequestIds.remove(requestId) }
119
+ if (!accepted) {
120
+ promise.reject(ERROR_REQUEST, "Native debug request is unknown or already settled.")
121
+ return
122
+ }
123
+ val bytes = payload.toByteArray(Charsets.UTF_8)
124
+ if (bytes.isEmpty() || bytes.size > MAX_SNAPSHOT_BYTES) {
125
+ promise.reject(
126
+ ERROR_PAYLOAD,
127
+ "Native debug response must contain 1-$MAX_SNAPSHOT_BYTES UTF-8 bytes.",
128
+ )
129
+ return
130
+ }
131
+ try {
132
+ val directory = responseDirectory()
133
+ check(directory.isDirectory || directory.mkdirs()) {
134
+ "Could not create the native debug response directory."
135
+ }
136
+ val temporary = File(directory, ".$requestId.tmp")
137
+ temporary.delete()
138
+ FileOutputStream(temporary).use { output ->
139
+ output.write(bytes)
140
+ output.fd.sync()
141
+ }
142
+ val response = responseFile(requestId)
143
+ response.delete()
144
+ check(temporary.renameTo(response)) {
145
+ "Could not publish the native debug response atomically."
146
+ }
147
+ promise.resolve(null)
148
+ } catch (error: Throwable) {
149
+ File(responseDirectory(), ".$requestId.tmp").delete()
150
+ promise.reject(ERROR_WRITE, "Could not publish the native debug response.", error)
151
+ }
152
+ }
153
+
154
+ private fun isDebuggable(): Boolean =
155
+ context.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0
156
+
157
+ private fun requestAction(): String =
158
+ "${context.packageName}.$REQUEST_ACTION_SUFFIX"
159
+
160
+ private fun responseDirectory(): File = File(context.cacheDir, RESPONSE_DIRECTORY)
161
+
162
+ private fun responseFile(requestId: String): File =
163
+ File(responseDirectory(), "$requestId.json")
164
+
165
+ companion object {
166
+ const val NAME = "SolidNativeDebug"
167
+ const val REQUEST_ACTION_SUFFIX = "solidnative.DEBUG_SNAPSHOT_REQUEST"
168
+ const val REQUEST_ID_EXTRA = "requestId"
169
+ const val OPERATION_EXTRA = "operation"
170
+ const val SESSION_ID_EXTRA = "sessionId"
171
+ const val OPERATION_CAUSAL_SNAPSHOT = "causal-snapshot"
172
+ const val OPERATION_SOLID_DIAGNOSTICS_BEGIN = "solid-diagnostics-begin"
173
+ const val OPERATION_SOLID_DIAGNOSTICS_END = "solid-diagnostics-end"
174
+ const val OPERATION_TREE_SNAPSHOT = "tree-snapshot"
175
+ const val RESPONSE_DIRECTORY = "solid-native-debug"
176
+ const val MAX_SNAPSHOT_BYTES = 16_777_216
177
+ private const val MAX_PENDING_REQUESTS = 8
178
+ private const val ERROR_UNAVAILABLE = "E_SOLID_NATIVE_DEBUG_UNAVAILABLE"
179
+ private const val ERROR_REQUEST = "E_SOLID_NATIVE_DEBUG_REQUEST"
180
+ private const val ERROR_PAYLOAD = "E_SOLID_NATIVE_DEBUG_PAYLOAD"
181
+ private const val ERROR_WRITE = "E_SOLID_NATIVE_DEBUG_WRITE"
182
+ private val REQUEST_ID_PATTERN = Regex("^[a-f0-9]{32}$")
183
+ private val ALLOWED_OPERATIONS =
184
+ setOf(
185
+ OPERATION_CAUSAL_SNAPSHOT,
186
+ OPERATION_SOLID_DIAGNOSTICS_BEGIN,
187
+ OPERATION_SOLID_DIAGNOSTICS_END,
188
+ OPERATION_TREE_SNAPSHOT,
189
+ )
190
+ }
191
+ }
@@ -0,0 +1,43 @@
1
+ package dev.solidnative.runtime
2
+
3
+ import com.facebook.react.BaseReactPackage
4
+ import com.facebook.react.bridge.NativeModule
5
+ import com.facebook.react.bridge.ReactApplicationContext
6
+ import com.facebook.react.module.model.ReactModuleInfo
7
+ import com.facebook.react.module.model.ReactModuleInfoProvider
8
+
9
+ /** Registers the package-owned runtime modules with the selected React Native Fabric backend. */
10
+ class SolidNativePackage : BaseReactPackage() {
11
+ override fun getModule(
12
+ name: String,
13
+ reactContext: ReactApplicationContext,
14
+ ): NativeModule? =
15
+ when (name) {
16
+ SolidNativeDebugModule.NAME -> SolidNativeDebugModule(reactContext)
17
+ SolidNativePlatformModule.NAME -> SolidNativePlatformModule(reactContext)
18
+ else -> null
19
+ }
20
+
21
+ override fun getReactModuleInfoProvider(): ReactModuleInfoProvider = ReactModuleInfoProvider {
22
+ mapOf(
23
+ SolidNativeDebugModule.NAME to
24
+ ReactModuleInfo(
25
+ SolidNativeDebugModule.NAME,
26
+ SolidNativeDebugModule::class.java.name,
27
+ false,
28
+ false,
29
+ false,
30
+ true,
31
+ ),
32
+ SolidNativePlatformModule.NAME to
33
+ ReactModuleInfo(
34
+ SolidNativePlatformModule.NAME,
35
+ SolidNativePlatformModule::class.java.name,
36
+ false,
37
+ false,
38
+ false,
39
+ true,
40
+ ),
41
+ )
42
+ }
43
+ }
@@ -0,0 +1,81 @@
1
+ package dev.solidnative.runtime
2
+
3
+ import com.facebook.react.bridge.Arguments
4
+ import com.facebook.react.bridge.ReactApplicationContext
5
+ import com.facebook.react.module.annotations.ReactModule
6
+ import java.lang.ref.WeakReference
7
+
8
+ /** Package-owned Android lifecycle events that React Native does not expose. */
9
+ @ReactModule(name = SolidNativePlatformModule.NAME)
10
+ class SolidNativePlatformModule(
11
+ private val context: ReactApplicationContext,
12
+ ) : NativeSolidNativePlatformAndroidSpec(context) {
13
+ override fun getName(): String = NAME
14
+
15
+ override fun initialize() {
16
+ super.initialize()
17
+ SolidNativeMemoryWarning.attach(this)
18
+ }
19
+
20
+ override fun invalidate() {
21
+ SolidNativeMemoryWarning.detach(this)
22
+ super.invalidate()
23
+ }
24
+
25
+ internal fun emitMemoryWarning(level: Int) {
26
+ if (!context.hasActiveReactInstance()) return
27
+ val event = Arguments.createMap().apply { putInt("level", level) }
28
+ try {
29
+ emitOnMemoryWarning(event)
30
+ } catch (_: RuntimeException) {
31
+ // A lifecycle callback must not crash Application while memory is scarce.
32
+ // A future warning can retry after the React instance becomes healthy.
33
+ }
34
+ }
35
+
36
+ companion object {
37
+ const val NAME = "SolidNativePlatformAndroid"
38
+ }
39
+ }
40
+
41
+ /**
42
+ * Application lifecycle seam for Android memory pressure.
43
+ *
44
+ * `TRIM_MEMORY_UI_HIDDEN` is deliberately not a warning: backgrounding is an
45
+ * application-visibility transition, not evidence that the OS needs memory.
46
+ * Running-low/critical and background-or-worse levels request cache reduction.
47
+ */
48
+ object SolidNativeMemoryWarning {
49
+ private const val RUNNING_LOW = 10
50
+ private const val RUNNING_CRITICAL = 15
51
+ private const val BACKGROUND = 40
52
+ private const val LOW_MEMORY_LEVEL = 80
53
+ private var activeModule: WeakReference<SolidNativePlatformModule>? = null
54
+
55
+ @Synchronized
56
+ internal fun attach(module: SolidNativePlatformModule) {
57
+ activeModule = WeakReference(module)
58
+ }
59
+
60
+ @Synchronized
61
+ internal fun detach(module: SolidNativePlatformModule) {
62
+ if (activeModule?.get() === module) activeModule = null
63
+ }
64
+
65
+ @JvmStatic
66
+ fun handleTrimMemory(level: Int) {
67
+ if (!isMemoryPressure(level)) return
68
+ currentModule()?.emitMemoryWarning(level)
69
+ }
70
+
71
+ @JvmStatic
72
+ fun handleLowMemory() {
73
+ currentModule()?.emitMemoryWarning(LOW_MEMORY_LEVEL)
74
+ }
75
+
76
+ @Synchronized
77
+ private fun currentModule(): SolidNativePlatformModule? = activeModule?.get()
78
+
79
+ private fun isMemoryPressure(level: Int): Boolean =
80
+ level in RUNNING_LOW..RUNNING_CRITICAL || level >= BACKGROUND
81
+ }