peachsim 0.1.1655 → 0.1.1660

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 (237) hide show
  1. package/cli/app-config.ts +4 -0
  2. package/cli/app-stubs.ts +43 -6
  3. package/cli/commands/inspect/shared.ts +5 -2
  4. package/cli/commands/inspect/url.ts +2 -1
  5. package/cli/commands/screenshot.ts +5 -5
  6. package/cli/commands/state.ts +9 -6
  7. package/cli/outbound-endpoints.ts +9 -1
  8. package/cli/output-redaction.ts +49 -0
  9. package/detox/index.ts +0 -1
  10. package/dist-lib/agent-daemon-client.cjs +1 -1
  11. package/dist-lib/agent-events.cjs +1 -1
  12. package/dist-lib/attached-projects.cjs +1 -1
  13. package/dist-lib/auth/shared-session.cjs +1 -1
  14. package/dist-lib/backend-origin.cjs +1 -1
  15. package/dist-lib/beta.cjs +1 -1
  16. package/dist-lib/beta.mjs +1 -1
  17. package/dist-lib/bridge-constants.cjs +1 -1
  18. package/dist-lib/bridge-contract-input.cjs +1 -1
  19. package/dist-lib/bridge-contract-input.mjs +1 -1
  20. package/dist-lib/bridge-contract.cjs +1 -1
  21. package/dist-lib/bridge-contract.mjs +1 -1
  22. package/dist-lib/bundle-channel.cjs +12772 -1132
  23. package/dist-lib/capture-contract.cjs +1 -1
  24. package/dist-lib/capture-contract.mjs +1 -1
  25. package/dist-lib/cloud-contract.cjs +1 -1
  26. package/dist-lib/cloud-contract.mjs +1 -1
  27. package/dist-lib/cloud-test-args.cjs +1 -1
  28. package/dist-lib/cloud-test-args.mjs +1 -1
  29. package/dist-lib/cloud.cjs +1 -1
  30. package/dist-lib/cloud.mjs +1 -1
  31. package/dist-lib/config.cjs +29 -5
  32. package/dist-lib/config.mjs +29 -5
  33. package/dist-lib/detox/index.cjs +19 -6
  34. package/dist-lib/dev-bundle-resolution.cjs +1 -1
  35. package/dist-lib/expo-swift-bundle.cjs +10411 -73
  36. package/dist-lib/expo-swift-metadata.cjs +1 -1
  37. package/dist-lib/expo-swift-metadata.mjs +1 -1
  38. package/dist-lib/home-paths.cjs +1 -1
  39. package/dist-lib/host/bridge-host.cjs +11220 -670
  40. package/dist-lib/host/fetch-proxy-handler.cjs +260 -197
  41. package/dist-lib/host/fetch-proxy-overrides.cjs +1 -1
  42. package/dist-lib/host/fetch-proxy-overrides.mjs +1 -1
  43. package/dist-lib/host/replacement-module-handler.cjs +1 -1
  44. package/dist-lib/host/websocket-proxy.cjs +1 -1
  45. package/dist-lib/index.cjs +653 -338
  46. package/dist-lib/jump-to-source-babel.cjs +1 -1
  47. package/dist-lib/jump-to-source-native.cjs +1 -1
  48. package/dist-lib/kotlin-host.cjs +443 -312
  49. package/dist-lib/menu.cjs +1 -1
  50. package/dist-lib/menu.mjs +1 -1
  51. package/dist-lib/metro-fingerprint-registry.cjs +1 -1
  52. package/dist-lib/metro-fingerprint-registry.mjs +1 -1
  53. package/dist-lib/metro-production-bundle.cjs +1 -1
  54. package/dist-lib/metro-production-bundle.mjs +1 -1
  55. package/dist-lib/metro.cjs +548 -163
  56. package/dist-lib/native-app-identity-project.cjs +179 -33
  57. package/dist-lib/native-app-identity-project.mjs +179 -33
  58. package/dist-lib/native-app-identity.cjs +1 -1
  59. package/dist-lib/native-app-identity.mjs +1 -1
  60. package/dist-lib/native-firebase-metadata.cjs +1 -1
  61. package/dist-lib/native-firebase-metadata.mjs +1 -1
  62. package/dist-lib/plist.cjs +1 -1
  63. package/dist-lib/plist.mjs +1 -1
  64. package/dist-lib/profiles.cjs +1 -1
  65. package/dist-lib/public-brand.cjs +1 -1
  66. package/dist-lib/public-origins-lib-esm.json +23 -23
  67. package/dist-lib/public-origins-lib.json +96 -63
  68. package/dist-lib/react-native-host-modules.cjs +1 -1
  69. package/dist-lib/react-native-host-modules.mjs +1 -1
  70. package/dist-lib/scripts/dev-server-scanner.cjs +2 -2
  71. package/dist-lib/sdk.cjs +1 -1
  72. package/dist-lib/sdk.mjs +1 -1
  73. package/dist-lib/skills.cjs +12193 -1448
  74. package/dist-lib/swift-compile.cjs +10348 -172
  75. package/dist-lib/swift-compile.mjs +10348 -172
  76. package/dist-lib/swift-package.cjs +1 -1
  77. package/dist-lib/swift-package.mjs +1 -1
  78. package/dist-lib/swift-submit.cjs +1 -1
  79. package/dist-lib/swift-submit.mjs +1 -1
  80. package/dist-lib/swift-toolchain.cjs +1 -1
  81. package/dist-lib/swift.cjs +1 -1
  82. package/dist-lib/vite-swift.cjs +10411 -73
  83. package/dist-lib/vite.cjs +149 -163
  84. package/dist-lib/worklet-executable.cjs +1 -1
  85. package/dist-lib/worklet-executable.mjs +1 -1
  86. package/dist-types/packages/peach-compat/src/stubs/native-seams/react-native-gesture-handler.d.ts +2 -0
  87. package/dist-types/packages/peach-engine/src/engine/app-font-registry.d.ts +14 -0
  88. package/dist-types/packages/peach-engine/src/engine/image-loader.d.ts +8 -0
  89. package/dist-types/packages/peach-engine/src/engine/node.d.ts +3 -0
  90. package/dist-types/packages/peach-engine/src/render-worker/compositor-protocol.d.ts +21 -0
  91. package/dist-types/packages/peach-engine/src/render-worker/gesture-seam-protocol.d.ts +2 -1
  92. package/dist-types/packages/peach-engine/src/render-worker/test-bridge-protocol.d.ts +1 -1
  93. package/dist-types/packages/peach-engine/src/rolldown-bundle-inventory.d.ts +1 -0
  94. package/dist-types/packages/peach-globals/src/index.d.ts +34 -0
  95. package/kotlin/README.md +38 -23
  96. package/kotlin/sdk.tar.gz +0 -0
  97. package/package.json +2 -12
  98. package/runtime-companion.json +1 -1
  99. package/src/bundleGlobalFooter.ts +0 -2
  100. package/src/config.ts +54 -2
  101. package/src/configured-bundle-producer.ts +7 -1
  102. package/src/host/fetch-proxy-handler.ts +14 -50
  103. package/src/host/rolldown-bundle-forward.ts +71 -0
  104. package/src/installed-package-versions.ts +54 -2
  105. package/src/kotlin-host.ts +138 -285
  106. package/src/kotlin-module.ts +4 -7
  107. package/src/kotlin-sdk.ts +81 -0
  108. package/src/kotlin-toolchain.ts +152 -0
  109. package/src/kotlin-view-models.ts +1 -1
  110. package/src/kotlinSdkContract.ts +113 -0
  111. package/src/nitro-adapter.lock.json +3 -3
  112. package/src/nitro-bundle.ts +18 -4
  113. package/src/nitro-compile.ts +5 -1
  114. package/src/nitro-source.ts +112 -28
  115. package/src/nitroContract.ts +16 -4
  116. package/src/stub-pattern-matcher.ts +4 -0
  117. package/src/swift-compile.ts +44 -14
  118. package/src/swift-host.ts +2 -1
  119. package/src/swift-objc-compile.ts +117 -4
  120. package/src/swift-objc-sdk.ts +10215 -59
  121. package/src/swift-objc-standin.ts +5 -2
  122. package/src/vite-plugin-one.ts +73 -134
  123. package/src/worker-system-chunks.ts +14 -4
  124. package/src/xcode-project.ts +261 -34
  125. package/kotlin/AndroidViewSources/android/content/Context.kt +0 -22
  126. package/kotlin/AndroidViewSources/android/content/res/Configuration.kt +0 -25
  127. package/kotlin/AndroidViewSources/android/content/res/Resources.kt +0 -72
  128. package/kotlin/AndroidViewSources/android/graphics/Canvas.kt +0 -22
  129. package/kotlin/AndroidViewSources/android/graphics/Color.kt +0 -13
  130. package/kotlin/AndroidViewSources/android/graphics/Paint.kt +0 -18
  131. package/kotlin/AndroidViewSources/android/util/DisplayMetrics.kt +0 -12
  132. package/kotlin/AndroidViewSources/android/view/View.kt +0 -211
  133. package/kotlin/AndroidViewSources/android/widget/FrameLayout.kt +0 -61
  134. package/kotlin/AndroidViewSources/android/widget/LinearLayout.kt +0 -49
  135. package/kotlin/AndroidViewSources/dev/peach/app/JavaFormat.kt +0 -272
  136. package/kotlin/AppSources/android/app/Activity.kt +0 -174
  137. package/kotlin/AppSources/android/app/Application.kt +0 -11
  138. package/kotlin/AppSources/android/app/UiModeManager.kt +0 -9
  139. package/kotlin/AppSources/android/content/Intent.kt +0 -16
  140. package/kotlin/AppSources/android/os/Bundle.kt +0 -11
  141. package/kotlin/AppSources/android/util/SuperNotCalledException.kt +0 -3
  142. package/kotlin/AppSources/android/view/Window.kt +0 -182
  143. package/kotlin/AppSources/android/view/WindowInsets.kt +0 -9
  144. package/kotlin/AppSources/android/view/WindowInsetsController.kt +0 -17
  145. package/kotlin/AppSources/android/view/WindowManager.kt +0 -16
  146. package/kotlin/AppSources/android/window/BackEvent.kt +0 -24
  147. package/kotlin/AppSources/android/window/OnBackAnimationCallback.kt +0 -7
  148. package/kotlin/AppSources/android/window/OnBackInvokedDispatcher.kt +0 -84
  149. package/kotlin/AppSources/androidx/activity/ActivityFlags.kt +0 -9
  150. package/kotlin/AppSources/androidx/activity/ActivityViewModelLazy.kt +0 -21
  151. package/kotlin/AppSources/androidx/activity/BackEventCompat.kt +0 -43
  152. package/kotlin/AppSources/androidx/activity/ComponentActivity.kt +0 -122
  153. package/kotlin/AppSources/androidx/activity/EdgeToEdge.kt +0 -75
  154. package/kotlin/AppSources/androidx/activity/ExperimentalActivityApi.kt +0 -6
  155. package/kotlin/AppSources/androidx/activity/OnBackPressedCallback.kt +0 -69
  156. package/kotlin/AppSources/androidx/activity/OnBackPressedDispatcher.kt +0 -144
  157. package/kotlin/AppSources/androidx/activity/OnBackPressedDispatcherOwner.kt +0 -7
  158. package/kotlin/AppSources/androidx/activity/compose/BackHandler.kt +0 -86
  159. package/kotlin/AppSources/androidx/activity/compose/ComponentActivity.kt +0 -29
  160. package/kotlin/AppSources/androidx/activity/compose/internal/BackHandlerCompat.kt +0 -38
  161. package/kotlin/AppSources/androidx/activity/compose/internal/BackHandlerDispatcherCompat.kt +0 -27
  162. package/kotlin/AppSources/androidx/compose/foundation/layout/WindowInsets.kt +0 -15
  163. package/kotlin/AppSources/androidx/compose/ui/platform/AndroidCompositionLocals.kt +0 -27
  164. package/kotlin/AppSources/androidx/compose/ui/platform/ComposeView.kt +0 -68
  165. package/kotlin/AppSources/androidx/compose/ui/res/ColorResources.kt +0 -16
  166. package/kotlin/AppSources/androidx/compose/ui/res/PainterResources.kt +0 -70
  167. package/kotlin/AppSources/androidx/compose/ui/res/StringResources.kt +0 -38
  168. package/kotlin/AppSources/androidx/core/view/WindowCompat.kt +0 -32
  169. package/kotlin/AppSources/androidx/lifecycle/AndroidViewModel.kt +0 -10
  170. package/kotlin/AppSources/dev/peach/app/PeachApp.kt +0 -388
  171. package/kotlin/AppSources/dev/peach/app/PeachDrawable.kt +0 -102
  172. package/kotlin/AppSources/dev/peach/app/PeachWindowHost.kt +0 -14
  173. package/kotlin/CompilerSources/META-INF/services/org.jetbrains.kotlin.compiler.plugin.CompilerPluginRegistrar +0 -1
  174. package/kotlin/CompilerSources/PeachJniIncoming.kt +0 -243
  175. package/kotlin/CompilerSources/PeachJniPlugin.kt +0 -116
  176. package/kotlin/ComposeSources/PeachComposeHost.kt +0 -724
  177. package/kotlin/ComposeSources/PeachMagnifier.kt +0 -185
  178. package/kotlin/ComposeSources/PeachSemantics.kt +0 -127
  179. package/kotlin/ExpoComposeSources/PeachAndroidView.kt +0 -103
  180. package/kotlin/ExpoComposeSources/PeachExpoViewMount.kt +0 -24
  181. package/kotlin/ExpoComposeSources/dev/peach/expo/PeachExpoCompose.kt +0 -28
  182. package/kotlin/ExpoComposeSources/expo/modules/kotlin/views/ExpoComposeView.kt +0 -70
  183. package/kotlin/ExpoSources/dev/peach/expo/PeachExpoRuntime.kt +0 -516
  184. package/kotlin/ExpoSources/dev/peach/expo/PeachExpoTypes.kt +0 -173
  185. package/kotlin/ExpoSources/expo/modules/kotlin/AppContext.kt +0 -20
  186. package/kotlin/ExpoSources/expo/modules/kotlin/Promise.kt +0 -19
  187. package/kotlin/ExpoSources/expo/modules/kotlin/classcomponent/ClassComponentBuilder.kt +0 -198
  188. package/kotlin/ExpoSources/expo/modules/kotlin/exception/CodedException.kt +0 -126
  189. package/kotlin/ExpoSources/expo/modules/kotlin/functions/Functions.kt +0 -132
  190. package/kotlin/ExpoSources/expo/modules/kotlin/jni/JavaScriptTypedArray.kt +0 -81
  191. package/kotlin/ExpoSources/expo/modules/kotlin/modules/Module.kt +0 -98
  192. package/kotlin/ExpoSources/expo/modules/kotlin/objects/ObjectDefinitionBuilder.kt +0 -236
  193. package/kotlin/ExpoSources/expo/modules/kotlin/records/Record.kt +0 -12
  194. package/kotlin/ExpoSources/expo/modules/kotlin/runtime/Runtime.kt +0 -9
  195. package/kotlin/ExpoSources/expo/modules/kotlin/sharedobjects/SharedObject.kt +0 -50
  196. package/kotlin/ExpoSources/expo/modules/kotlin/sharedobjects/SharedObjectRegistry.kt +0 -57
  197. package/kotlin/ExpoSources/expo/modules/kotlin/sharedobjects/SharedRef.kt +0 -19
  198. package/kotlin/ExpoSources/expo/modules/kotlin/typedarray/ConcreteTypedArrays.kt +0 -158
  199. package/kotlin/ExpoSources/expo/modules/kotlin/typedarray/GenericTypedArray.kt +0 -8
  200. package/kotlin/ExpoSources/expo/modules/kotlin/typedarray/TypedArray.kt +0 -29
  201. package/kotlin/ExpoSources/expo/modules/kotlin/typedarray/TypedArrayIterator.kt +0 -9
  202. package/kotlin/ExpoSources/expo/modules/kotlin/types/AnyType.kt +0 -155
  203. package/kotlin/ExpoSources/expo/modules/kotlin/types/Either.kt +0 -91
  204. package/kotlin/ExpoSources/expo/modules/kotlin/types/Enumerable.kt +0 -6
  205. package/kotlin/ExpoSources/expo/modules/kotlin/viewevent/ViewEvent.kt +0 -68
  206. package/kotlin/ExpoSources/expo/modules/kotlin/views/ExpoView.kt +0 -20
  207. package/kotlin/ExpoSources/expo/modules/kotlin/views/ViewDefinitionBuilder.kt +0 -72
  208. package/kotlin/ExpoSources/expo/modules/kotlin/views/ViewProps.kt +0 -36
  209. package/kotlin/JniPlatformSources/androidx/annotation/CallSuper.kt +0 -5
  210. package/kotlin/JniPlatformSources/com/facebook/proguard/annotations/DoNotStrip.kt +0 -5
  211. package/kotlin/JniPlatformSources/dalvik/annotation/optimization/FastNative.kt +0 -5
  212. package/kotlin/JniPlatformSources/kotlin/Synchronized.kt +0 -6
  213. package/kotlin/JniPlatformSources/kotlin/jvm/JvmField.kt +0 -5
  214. package/kotlin/JniPlatformSources/kotlin/jvm/JvmStatic.kt +0 -5
  215. package/kotlin/JniSources/com/facebook/jni/HybridData.kt +0 -66
  216. package/kotlin/JniSources/dev/peach/jni/IncomingBindings.kt +0 -157
  217. package/kotlin/JniSources/dev/peach/jni/NativeBindings.kt +0 -77
  218. package/kotlin/JniTransportSources/dev/peach/jni/integration/JniTransport.kt +0 -89
  219. package/kotlin/Sources/android/os/Build.kt +0 -17
  220. package/kotlin/Sources/android/os/Handler.kt +0 -35
  221. package/kotlin/Sources/android/os/Looper.kt +0 -17
  222. package/kotlin/Sources/android/os/MessageQueue.kt +0 -56
  223. package/kotlin/Sources/android/os/SystemClock.kt +0 -14
  224. package/kotlin/Sources/android/util/Log.kt +0 -64
  225. package/kotlin/Sources/androidx/annotation/DrawableRes.kt +0 -14
  226. package/kotlin/Sources/androidx/annotation/Keep.kt +0 -4
  227. package/kotlin/Sources/dev/onejs/one/OneNativeSourceDispatch.kt +0 -7
  228. package/kotlin/Sources/dev/onejs/one/OneNativeSourceViewDispatch.kt +0 -13
  229. package/kotlin/Sources/dev/onejs/one/source/OneModule.kt +0 -4
  230. package/kotlin/Sources/java/lang/Runnable.kt +0 -6
  231. package/kotlin/Sources/java/security/MessageDigest.kt +0 -114
  232. package/kotlin/Sources/java/util/IllegalFormatCodePointException.kt +0 -10
  233. package/kotlin/Sources/java/util/IllegalFormatException.kt +0 -5
  234. package/kotlin/Sources/kotlin/text/StringFormat.kt +0 -9
  235. package/kotlin/Sources/org/json/JSON.kt +0 -307
  236. package/kotlin/tools/abi-classes.main.kts +0 -41
  237. package/kotlin/tools/expo-types.main.kts +0 -127
@@ -1,516 +0,0 @@
1
- @file:OptIn(kotlin.io.encoding.ExperimentalEncodingApi::class)
2
-
3
- package dev.peach.expo
4
-
5
- import android.content.Context
6
- import android.content.res.Configuration
7
- import android.content.res.Resources
8
- import android.util.DisplayMetrics
9
- import android.view.View
10
- import expo.modules.kotlin.viewevent.ViewEventRegistry
11
- import kotlin.math.roundToInt
12
- import android.util.Log
13
- import expo.modules.kotlin.AppContext
14
- import expo.modules.kotlin.Promise
15
- import expo.modules.kotlin.classcomponent.ClassDefinitionData
16
- import expo.modules.kotlin.exception.CodedException
17
- import expo.modules.kotlin.exception.FunctionCallException
18
- import expo.modules.kotlin.exception.UnexpectedException
19
- import expo.modules.kotlin.exception.exceptionDecorator
20
- import expo.modules.kotlin.exception.toCodedException
21
- import expo.modules.kotlin.functions.AnyFunction
22
- import expo.modules.kotlin.functions.AsyncFunctionWithPromiseComponent
23
- import expo.modules.kotlin.functions.SuspendFunctionComponent
24
- import expo.modules.kotlin.functions.SyncFunctionComponent
25
- import expo.modules.kotlin.functions.UntypedAsyncFunctionComponent
26
- import expo.modules.kotlin.jni.JavaScriptTypedArray
27
- import expo.modules.kotlin.jni.TypedArrayKind
28
- import expo.modules.kotlin.modules.Module
29
- import expo.modules.kotlin.modules.ModuleDefinitionData
30
- import expo.modules.kotlin.objects.ObjectDefinitionData
31
- import expo.modules.kotlin.sharedobjects.SharedObject
32
- import expo.modules.kotlin.sharedobjects.SharedObjectClass
33
- import expo.modules.kotlin.sharedobjects.SharedObjectId
34
- import expo.modules.kotlin.sharedobjects.SharedRef
35
- import expo.modules.kotlin.typedarray.RawTypedArrayHolder
36
- import expo.modules.kotlin.views.ViewManagerDefinition
37
- import kotlin.io.encoding.Base64
38
- import kotlinx.coroutines.launch
39
- import org.json.JSONArray
40
- import org.json.JSONObject
41
-
42
- // the module registry of one Kotlin/Wasm process (ModuleRegistry and
43
- // ModuleHolder), answering the tenant's Expo requests over the Expo wire that
44
- // ExpoWire in sootsim-swift's ExpoModulesCore owns. The tenant installer is
45
- // shared with the Swift process; only this side differs.
46
- object PeachExpoRuntime {
47
- private class Holder(val name: String, val module: Module, val definition: ModuleDefinitionData) {
48
- val functions: Map<String, AnyFunction> = functionsOf(definition.objectDefinition)
49
- val classes: Map<String, ClassDefinitionData> = definition.classData.associateBy { it.name }
50
- val views: Map<String, ViewManagerDefinition> = definition.views.associateBy { it.name }
51
- }
52
-
53
- // a view React Native created, with the props it last received
54
- private class MountedView(val module: String, val definition: ViewManagerDefinition, val view: View) {
55
- var props: Map<*, *>? = null
56
- }
57
-
58
- private val viewResources = Resources(Configuration(), DisplayMetrics(), emptyMap(), emptyMap(), emptyMap(), emptyMap())
59
- private object PeachContext : Context() {
60
- override val resources: Resources get() = viewResources
61
- override val theme = Resources.Theme()
62
- }
63
-
64
- internal fun configureViewDevice(density: Float, locale: String, night: Boolean) {
65
- val metrics = viewResources.displayMetrics.apply {
66
- densityDpi = (density * DisplayMetrics.DENSITY_DEFAULT).roundToInt()
67
- this.density = densityDpi / DisplayMetrics.DENSITY_DEFAULT.toFloat()
68
- }
69
- val configuration = Configuration().apply {
70
- localeTag = locale
71
- uiMode = Configuration.UI_MODE_TYPE_NORMAL or
72
- if (night) Configuration.UI_MODE_NIGHT_YES else Configuration.UI_MODE_NIGHT_NO
73
- }
74
- viewResources.configuration.setTo(configuration)
75
- }
76
-
77
- private fun functionsOf(definition: ObjectDefinitionData): Map<String, AnyFunction> =
78
- definition.syncFunctions + definition.asyncFunctions
79
-
80
- private sealed class Settled {
81
- class Value(val value: Any?) : Settled()
82
- class Failure(val error: CodedException) : Settled()
83
- }
84
-
85
- private val appContext = AppContext()
86
- private val sharedObjects = appContext.runtime.sharedObjectRegistry
87
- // the typed arrays the current request decoded, in wire order
88
- private val requestArrays = mutableListOf<JavaScriptTypedArray>()
89
- private val holders = LinkedHashMap<String, Holder>()
90
- private val mountedViews = HashMap<String, MountedView>()
91
- private val events = mutableListOf<String>()
92
- private val settled = mutableMapOf<Int, Settled>()
93
- private val pending = mutableSetOf<Int>()
94
- private var nextCall = 1
95
- private var notify: (() -> Unit)? = null
96
-
97
- fun register(modules: List<Pair<() -> Module, String?>>) {
98
- for ((create, override) in modules) {
99
- val module = create()
100
- module.context = appContext
101
- val definition = module.definition()
102
- val name = override ?: definition.name
103
- check(name !in holders) { "duplicate Expo module: $name" }
104
- val holder = Holder(name, module, definition)
105
- module.emitter = { event, body ->
106
- events.add(wireObject(listOf("module" to name, "name" to event, "body" to body)))
107
- notify?.invoke()
108
- }
109
- holders[name] = holder
110
- for (data in definition.classData) {
111
- if (data.ownerClass != Unit::class) sharedObjects.addClass(data.ownerClass, SharedObjectClass(name, data.name))
112
- }
113
- for (body in definition.onCreate) body()
114
- }
115
- sharedObjects.emitter = { native, event, payload ->
116
- events.add(wireObject(listOf("object" to native, "name" to event, "args" to payload)))
117
- notify?.invoke()
118
- }
119
- }
120
-
121
- internal fun sharedObject(id: SharedObjectId): SharedObject = sharedObjects.toNativeObject(id)
122
-
123
- internal fun mountedView(id: String): View =
124
- mountedViews[id]?.view ?: throw CodedException("Unable to find the Expo view $id")
125
-
126
- // the tenant's wake-up: an event or a settled call is waiting
127
- fun listen(listener: () -> Unit) {
128
- notify = listener
129
- }
130
-
131
- fun dispatch(payload: String): String {
132
- requestArrays.clear()
133
- val reply = try {
134
- "\"ok\":true,\"value\":${request(decode(JSONArray("[$payload]").opt(0)))}"
135
- } catch (error: Throwable) {
136
- val coded = error.toCodedException()
137
- "\"ok\":false,\"error\":{\"message\":${quote(coded.message ?: "")},\"code\":${quote(coded.code)}}"
138
- }
139
- // JSI typed arrays share the caller's memory: what a synchronous body
140
- // wrote reaches the caller's array before the call returns
141
- val writes = requestArrays.filter { it.dirty }
142
- requestArrays.clear()
143
- if (writes.isEmpty()) return "{$reply}"
144
- return "{$reply,\"writes\":${writes.joinToString(",", "[", "]") { "[${it.ordinal},${quote(Base64.encode(it.bytes))}]" }}}"
145
- }
146
-
147
- private fun request(value: Any?): String {
148
- val fields = value as? Map<*, *> ?: throw UnexpectedException("invalid Expo request")
149
- val operation = fields["operation"] as? String
150
- when (operation) {
151
- "metadata" -> return metadata()
152
- "events" -> {
153
- val drained = events.joinToString(",", "[", "]")
154
- events.clear()
155
- return drained
156
- }
157
- "dropView" -> {
158
- val mounted = mountedViews.remove(fields["id"] as? String ?: throw UnexpectedException("invalid Expo view"))
159
- if (mounted != null) {
160
- try { mounted.definition.onViewDestroys?.invoke(mounted.view) }
161
- finally { ViewEventRegistry.unbind(mounted.view) }
162
- }
163
- return "null"
164
- }
165
- "poll" -> {
166
- val id = (fields["id"] as? Double)?.toInt() ?: throw UnexpectedException("invalid Expo poll")
167
- if (id in pending) return wireObject(listOf("pending" to id.toDouble()))
168
- return reply(settled.remove(id) ?: throw UnexpectedException("unknown Expo call $id"))
169
- }
170
- }
171
- if (operation == "release") {
172
- val id = (fields["id"] as? Double)?.toInt() ?: throw UnexpectedException("invalid Expo release")
173
- sharedObjects.delete(SharedObjectId(id))
174
- return "null"
175
- }
176
- val name = fields["module"] as? String ?: throw UnexpectedException("invalid Expo request")
177
- val holder = holders[name] ?: throw CodedException("ERR_MODULE_NOT_FOUND", "Module '$name' not found", null)
178
- if ("view" in fields) return viewRequest(holder, operation, fields)
179
- val className = fields["class"] as? String
180
- if (className != null) return classRequest(holder, className, operation, fields)
181
- val member = fields["member"] as? String ?: throw UnexpectedException("invalid Expo request")
182
- val definition = holder.definition.objectDefinition
183
- return when (operation) {
184
- "get" -> {
185
- definition.constants[member]?.let { constant ->
186
- if (!constant.evaluated) {
187
- constant.value = constant.getter?.body?.invoke(emptyArray())
188
- constant.evaluated = true
189
- }
190
- return wireObject(listOf("value" to constant.value))
191
- }
192
- val getter = definition.properties[member]?.getter ?: return wireObject(listOf("value" to Undefined))
193
- wireObject(listOf("value" to getter.body(emptyArray())))
194
- }
195
- "set" -> {
196
- val setter = definition.properties[member]?.setter ?: return wireObject(listOf("value" to Undefined))
197
- setter.body(setter.convertArgs(listOf(fields["value"])))
198
- wireObject(listOf("value" to Undefined))
199
- }
200
- "call" -> {
201
- val function = holder.functions[member]
202
- ?: throw CodedException("ERR_FUNCTION_NOT_FOUND", "Function '$member' not found in module '$name'", null)
203
- val args = fields["args"] as? List<*> ?: throw UnexpectedException("invalid Expo arguments")
204
- call(name, function, args)
205
- }
206
- else -> throw UnexpectedException("invalid Expo operation $operation")
207
- }
208
- }
209
-
210
- // a view's props, which create the view the first time, or one of its
211
- // async functions, whose first argument is the view when it takes the owner
212
- private fun viewRequest(holder: Holder, operation: String?, fields: Map<*, *>): String {
213
- val id = fields["id"] as? String ?: throw UnexpectedException("invalid Expo view")
214
- val viewName = fields["view"] as? String ?: throw UnexpectedException("invalid Expo view")
215
- if (operation == "viewProps") {
216
- configureViewDevice(
217
- (fields["density"] as? Double)?.toFloat() ?: throw UnexpectedException("missing Expo view device density"),
218
- fields["locale"] as? String ?: throw UnexpectedException("missing Expo view device locale"),
219
- fields["night"] as? Boolean ?: throw UnexpectedException("missing Expo view device theme"),
220
- )
221
- val props = fields["props"] as? Map<*, *> ?: throw UnexpectedException("invalid Expo view props")
222
- val mounted = mountedViews.getOrPut(id) {
223
- val definition = holder.views[viewName] ?: throw UnexpectedException("View '$viewName' not found in module '${holder.name}'")
224
- val view = definition.createView(PeachContext, appContext)
225
- ViewEventRegistry.bind(view, definition.callbacks) { event, body ->
226
- events.add(wireObject(listOf("view" to id, "module" to holder.name, "name" to event, "body" to body)))
227
- notify?.invoke()
228
- }
229
- MountedView(holder.name, definition, view)
230
- }
231
- updateView(mounted, props)
232
- return "null"
233
- }
234
- val mounted = mountedViews[id]?.takeIf { it.module == holder.name && it.definition.name == viewName }
235
- ?: throw CodedException("Unable to find the $viewName view $id")
236
- val member = fields["member"] as? String ?: throw UnexpectedException("invalid Expo request")
237
- if (operation != "call") throw UnexpectedException("invalid Expo operation $operation")
238
- val function = mounted.definition.asyncFunctions[member]
239
- ?: throw CodedException("ERR_FUNCTION_NOT_FOUND", "Function '$member' not found in view '$viewName'", null)
240
- val args = fields["args"] as? List<*> ?: throw UnexpectedException("invalid Expo arguments")
241
- return call("${holder.name}.$viewName", function, if (function.takesOwner) listOf(mounted.view) + args else args)
242
- }
243
-
244
- // Fabric's first props hold only the props React set; later ones each
245
- // prop that changed, a removed one as null. a failed prop is logged and
246
- // the rest still apply, then the view hears that its props updated
247
- private fun updateView(mounted: MountedView, props: Map<*, *>) {
248
- val last = mounted.props
249
- val changed = mounted.definition.props.values.filter { prop ->
250
- if (last == null) prop.name in props else last[prop.name] != props[prop.name]
251
- }
252
- mounted.props = props
253
- if (last != null && changed.isEmpty()) return
254
- for (prop in changed) {
255
- try {
256
- prop.set(props[prop.name], mounted.view)
257
- } catch (error: Throwable) {
258
- Log.e("ExpoModulesCore", error.message ?: error.toString())
259
- }
260
- }
261
- mounted.definition.onViewDidUpdateProps?.invoke(mounted.view)
262
- }
263
-
264
- // a Class member: the constructor, a static function, or an instance's
265
- // function or property, whose first argument is `this` when it takes the owner
266
- private fun classRequest(holder: Holder, className: String, operation: String?, fields: Map<*, *>): String {
267
- val data = holder.classes[className] ?: throw UnexpectedException("Class '$className' not found in module '${holder.name}'")
268
- val args = fields["args"] as? List<*> ?: emptyList<Any?>()
269
- val owner = fields["this"]
270
- if (operation == "construct") {
271
- val constructor = data.constructor ?: return "[0,null]"
272
- val native = exceptionDecorator({ FunctionCallException("constructor", "${holder.name}.$className", it) }) {
273
- constructor.body(constructor.convertArgs(args))
274
- }
275
- if (native !is SharedObject) return "[0,null]"
276
- if (native.runtime == null) native.runtime = appContext.runtime
277
- val id = sharedObjects.add(native).value
278
- return "[$id,${encode((native as? SharedRef<*>)?.nativeRefType)}]"
279
- }
280
- val member = fields["member"] as? String ?: throw UnexpectedException("invalid Expo request")
281
- val withOwner = { function: AnyFunction, rest: List<Any?> -> if (function.takesOwner) listOf(owner) + rest else rest }
282
- return when (operation) {
283
- "get" -> {
284
- val getter = data.objectDefinition.properties[member]?.getter ?: return wireObject(listOf("value" to Undefined))
285
- wireObject(listOf("value" to getter.body(getter.convertArgs(withOwner(getter, emptyList())))))
286
- }
287
- "set" -> {
288
- val setter = data.objectDefinition.properties[member]?.setter ?: return wireObject(listOf("value" to Undefined))
289
- setter.body(setter.convertArgs(withOwner(setter, listOf(fields["value"]))))
290
- wireObject(listOf("value" to Undefined))
291
- }
292
- "call" -> {
293
- val function = if ("this" in fields) functionsOf(data.objectDefinition)[member]
294
- else data.staticSyncFunctions[member] ?: data.staticAsyncFunctions[member]
295
- function ?: throw CodedException("ERR_FUNCTION_NOT_FOUND", "Function '$member' not found in class '$className'", null)
296
- call("${holder.name}.$className", function, withOwner(function, args))
297
- }
298
- else -> throw UnexpectedException("invalid Expo operation $operation")
299
- }
300
- }
301
-
302
- private fun call(module: String, function: AnyFunction, args: List<Any?>): String {
303
- if (function is SyncFunctionComponent) {
304
- val value = exceptionDecorator({ FunctionCallException(function.name, module, it) }) {
305
- function.body(function.convertArgs(args))
306
- }
307
- return wireObject(listOf("value" to value))
308
- }
309
- val id = nextCall++
310
- pending.add(id)
311
- val settle = { result: Settled ->
312
- if (pending.remove(id)) {
313
- settled[id] = result
314
- notify?.invoke()
315
- }
316
- }
317
- val promise = object : Promise {
318
- override fun resolve(value: Any?) = settle(Settled.Value(value))
319
- override fun reject(code: String?, message: String?, cause: Throwable?) =
320
- settle(Settled.Failure(CodedException(code ?: "UnknownCode", message, cause)))
321
- }
322
- val fail = { error: Throwable ->
323
- settle(Settled.Failure(FunctionCallException(function.name, module, error.toCodedException())))
324
- }
325
- // android runs async bodies on its modules queue, after the call returns
326
- appContext.modulesQueue.launch {
327
- try {
328
- val converted = function.convertArgs(args)
329
- when (function) {
330
- is UntypedAsyncFunctionComponent -> promise.resolve(function.body(converted))
331
- is AsyncFunctionWithPromiseComponent -> function.body(converted, promise)
332
- is SuspendFunctionComponent -> promise.resolve(function.body(this, converted))
333
- }
334
- } catch (error: Throwable) {
335
- fail(error)
336
- }
337
- }
338
- return wireObject(listOf("pending" to id.toDouble()))
339
- }
340
-
341
- private fun reply(result: Settled): String = when (result) {
342
- is Settled.Value -> wireObject(listOf("value" to result.value))
343
- is Settled.Failure -> throw result.error
344
- }
345
-
346
- private fun functionMetadata(functions: Map<String, AnyFunction>): List<Any?> =
347
- functions.keys.sorted().map { name ->
348
- val function = functions.getValue(name)
349
- Wire(wireObject(listOf("name" to name, "async" to (function !is SyncFunctionComponent), "enumerable" to function.isEnumerable)))
350
- }
351
-
352
- private fun metadata(): String = holders.keys.sorted().map { holders.getValue(it) }.map { holder ->
353
- val definition = holder.definition.objectDefinition
354
- val properties = definition.constants.keys.sorted().map { Wire(wireObject(listOf("name" to it, "get" to true, "set" to false))) } +
355
- definition.properties.keys.sorted().map { name ->
356
- val property = definition.properties.getValue(name)
357
- Wire(wireObject(listOf("name" to name, "get" to (property.getter != null), "set" to (property.setter != null))))
358
- }
359
- Wire(wireObject(listOf(
360
- "name" to holder.name,
361
- "definitionName" to holder.definition.name,
362
- "constants" to Wire(wireObject(definition.legacyConstantsProvider().entries.sortedBy { it.key }.map { it.key to it.value })),
363
- "functions" to functionMetadata(holder.functions),
364
- "properties" to properties,
365
- "events" to definition.eventNames,
366
- "views" to holder.definition.views.map { view ->
367
- Wire(wireObject(listOf(
368
- "name" to view.name,
369
- "props" to view.props.keys.sorted(),
370
- "events" to view.callbacks,
371
- "functions" to functionMetadata(view.asyncFunctions),
372
- )))
373
- },
374
- "classes" to holder.definition.classData.sortedBy { it.name }.map { data ->
375
- val instance = data.objectDefinition
376
- Wire(wireObject(listOf(
377
- "name" to data.name,
378
- "functions" to functionMetadata(functionsOf(instance)),
379
- "staticFunctions" to functionMetadata(data.staticSyncFunctions + data.staticAsyncFunctions),
380
- "properties" to instance.properties.keys.sorted().map { name ->
381
- val property = instance.properties.getValue(name)
382
- Wire(wireObject(listOf("name" to name, "get" to (property.getter != null), "set" to (property.setter != null))))
383
- },
384
- )))
385
- },
386
- )))
387
- }.let { encode(it) }
388
-
389
- // javascript undefined, and an already encoded wire value
390
- private object Undefined
391
- private class Wire(val json: String)
392
-
393
- private fun decode(value: Any?): Any? = when (value) {
394
- null, JSONObject.NULL -> null
395
- is Boolean, is String -> value
396
- is Number -> value.toDouble()
397
- is JSONArray -> List(value.length()) { decode(value.opt(it)) }
398
- is JSONObject -> {
399
- val keys = value.keys().asSequence().toList()
400
- if (keys.size != 1) throw UnexpectedException("invalid Expo wire value")
401
- val raw = value.opt(keys[0])
402
- when (keys[0]) {
403
- "k" -> {
404
- val pairs = raw as? JSONArray ?: throw UnexpectedException("invalid Expo wire object")
405
- val fields = LinkedHashMap<String, Any?>()
406
- for (index in 0 until pairs.length()) {
407
- val pair = pairs.opt(index) as? JSONArray ?: throw UnexpectedException("invalid Expo wire object")
408
- fields[pair.opt(0) as? String ?: throw UnexpectedException("invalid Expo wire object")] = decode(pair.opt(1))
409
- }
410
- fields
411
- }
412
- // a missing argument reaches the converters as null
413
- "u" -> null
414
- "n" -> when (raw) {
415
- "-0" -> -0.0
416
- "NaN" -> Double.NaN
417
- "Infinity" -> Double.POSITIVE_INFINITY
418
- "-Infinity" -> Double.NEGATIVE_INFINITY
419
- else -> throw UnexpectedException("invalid Expo wire number")
420
- }
421
- "ab" -> Base64.decode(raw as? String ?: throw UnexpectedException("invalid Expo wire bytes"))
422
- "t" -> {
423
- val pair = raw as? JSONArray ?: throw UnexpectedException("invalid Expo wire typed array")
424
- val kind = TypedArrayKind.entries.firstOrNull { it.name == pair.opt(0) }
425
- ?: throw UnexpectedException("invalid Expo wire typed array")
426
- val bytes = Base64.decode(pair.opt(1) as? String ?: throw UnexpectedException("invalid Expo wire bytes"))
427
- val offset = pair.opt(2) as? Number ?: throw UnexpectedException("invalid Expo wire typed array")
428
- val byteOffset = offset.toInt()
429
- val width = JavaScriptTypedArray.elementSize(kind)
430
- if (pair.length() != 3 || offset.toDouble() != byteOffset.toDouble() || byteOffset < 0 || byteOffset % width != 0 || bytes.size % width != 0)
431
- throw UnexpectedException("invalid Expo wire typed array")
432
- JavaScriptTypedArray(kind, bytes, requestArrays.size, byteOffset).also { requestArrays.add(it) }
433
- }
434
- "so" -> SharedObjectId((raw as? Number)?.toInt() ?: throw UnexpectedException("invalid Expo shared object"))
435
- else -> throw UnexpectedException("Expo wire value '${keys[0]}' is not supported in Kotlin yet")
436
- }
437
- }
438
- else -> throw UnexpectedException("invalid Expo wire value")
439
- }
440
-
441
- private fun wireObject(fields: List<Pair<String, Any?>>): String =
442
- fields.joinToString(",", "{\"k\":[", "]}") { (key, value) -> "[${quote(key)},${encode(value)}]" }
443
-
444
- // JSTypeConverterProvider's conversions to javascript
445
- private fun encode(value: Any?): String = when (value) {
446
- null, Unit -> "null"
447
- Undefined -> "{\"u\":0}"
448
- is Wire -> value.json
449
- is String -> quote(value)
450
- is Boolean -> value.toString()
451
- is Char -> quote(value.toString())
452
- is Number -> number(value.toDouble())
453
- is Map<*, *> -> wireObject(value.entries.map { (key, item) -> key.toString() to item })
454
- is Collection<*> -> value.joinToString(",", "[", "]") { encode(it) }
455
- is Array<*> -> value.joinToString(",", "[", "]") { encode(it) }
456
- is Pair<*, *> -> "[${encode(value.first)},${encode(value.second)}]"
457
- is IntArray -> value.joinToString(",", "[", "]") { number(it.toDouble()) }
458
- is LongArray -> value.joinToString(",", "[", "]") { number(it.toDouble()) }
459
- is DoubleArray -> value.joinToString(",", "[", "]") { number(it) }
460
- is FloatArray -> value.joinToString(",", "[", "]") { number(it.toDouble()) }
461
- is BooleanArray -> value.joinToString(",", "[", "]") { it.toString() }
462
- // a ByteArray reaches JavaScript as a Uint8Array, as on Android
463
- is ByteArray -> typedArray(TypedArrayKind.Uint8Array, value, 0)
464
- is JavaScriptTypedArray -> typedArray(value.kind, value.bytes, value.byteOffset)
465
- is RawTypedArrayHolder -> typedArray(value.rawArray.kind, value.rawArray.bytes, value.rawArray.byteOffset)
466
- is SharedObject -> sharedObjectWire(value)
467
- else -> {
468
- val converted = PeachExpoTypes.toJS(value)
469
- if (converted === PeachExpoTypes.Unconverted)
470
- throw UnexpectedException("Cannot convert '${value::class.simpleName}' to a JavaScript value")
471
- encode(converted)
472
- }
473
- }
474
-
475
- private fun typedArray(kind: TypedArrayKind, bytes: ByteArray, byteOffset: Int): String =
476
- "{\"t\":[${quote(kind.name)},${quote(Base64.encode(bytes))},$byteOffset]}"
477
-
478
- // a native object JavaScript already holds is that object; a new one gets an
479
- // instance of its exported class, or a plain object for an unexported
480
- // SharedRef, as convertSharedObject does
481
- private fun sharedObjectWire(native: SharedObject): String {
482
- val exported = sharedObjects.classOf(native)
483
- if (native.sharedObjectId.value == 0) {
484
- if (exported == null && native !is SharedRef<*>)
485
- throw UnexpectedException("Could not find JavaScript class for shared object: $native")
486
- if (native.runtime == null) native.runtime = appContext.runtime
487
- sharedObjects.add(native)
488
- }
489
- val refType = (native as? SharedRef<*>)?.nativeRefType
490
- return "{\"so\":[${native.sharedObjectId.value},${encode(exported?.module)},${encode(exported?.name)},${encode(refType)}]}"
491
- }
492
-
493
- private fun number(value: Double): String = when {
494
- value.isNaN() -> "{\"n\":\"NaN\"}"
495
- value == Double.POSITIVE_INFINITY -> "{\"n\":\"Infinity\"}"
496
- value == Double.NEGATIVE_INFINITY -> "{\"n\":\"-Infinity\"}"
497
- value == 0.0 && 1.0 / value < 0 -> "{\"n\":\"-0\"}"
498
- value == value.toLong().toDouble() && kotlin.math.abs(value) < 1e15 -> value.toLong().toString()
499
- else -> value.toString()
500
- }
501
-
502
- private fun quote(value: String): String {
503
- val out = StringBuilder("\"")
504
- for (c in value) {
505
- when (c) {
506
- '"' -> out.append("\\\"")
507
- '\\' -> out.append("\\\\")
508
- '\n' -> out.append("\\n")
509
- '\r' -> out.append("\\r")
510
- '\t' -> out.append("\\t")
511
- else -> if (c.code < 0x20) out.append("\\u").append(c.code.toString(16).padStart(4, '0')) else out.append(c)
512
- }
513
- }
514
- return out.append('"').toString()
515
- }
516
- }