peachsim 0.1.1716 → 0.1.1738

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 (282) hide show
  1. package/README.md +21 -3
  2. package/cli/app-bundle-resources.ts +2 -1
  3. package/cli/app-fonts.ts +4 -4
  4. package/cli/app-splash.ts +4 -4
  5. package/cli/app-stubs.ts +17 -0
  6. package/cli/command-registry.ts +1 -0
  7. package/cli/commands/control.ts +83 -822
  8. package/cli/commands/coverage-swift.ts +112 -4
  9. package/cli/commands/daemon.ts +8 -0
  10. package/cli/commands/detox.ts +3 -0
  11. package/cli/commands/doctor.ts +19 -2
  12. package/cli/commands/inspect/actions.ts +74 -14
  13. package/cli/commands/inspect/core.ts +5 -1
  14. package/cli/commands/inspect/settling.ts +5 -2
  15. package/cli/commands/inspect/wait-ready.ts +6 -0
  16. package/cli/commands/inspect.ts +48 -45
  17. package/cli/commands/kotlin.ts +12 -2
  18. package/cli/commands/package.ts +760 -0
  19. package/cli/commands/serve.ts +6 -0
  20. package/cli/commands/shell-url.ts +838 -0
  21. package/cli/commands/swift.ts +42 -102
  22. package/cli/commands/test.ts +20 -0
  23. package/cli/commands/upload.ts +1 -1
  24. package/cli/device-option.ts +1 -1
  25. package/cli/main.ts +13 -1
  26. package/cli/native-input.ts +199 -0
  27. package/cli/outbound-endpoints.ts +26 -1
  28. package/cli/packaged-app-launcher.ts +95 -0
  29. package/cli/scene-video.ts +36 -12
  30. package/detox/index.ts +196 -24
  31. package/dist-lib/agent-daemon-client.cjs +1 -1
  32. package/dist-lib/agent-events.cjs +1 -1
  33. package/dist-lib/attached-projects.cjs +5 -2
  34. package/dist-lib/auth/shared-session.cjs +5 -2
  35. package/dist-lib/backend-origin.cjs +1 -1
  36. package/dist-lib/beta.cjs +1 -1
  37. package/dist-lib/beta.mjs +1 -1
  38. package/dist-lib/bridge-constants.cjs +1 -1
  39. package/dist-lib/bridge-contract-input.cjs +1 -1
  40. package/dist-lib/bridge-contract-input.mjs +1 -1
  41. package/dist-lib/bridge-contract.cjs +1 -1
  42. package/dist-lib/bridge-contract.mjs +1 -1
  43. package/dist-lib/bundle-channel.cjs +39889 -31873
  44. package/dist-lib/capture-contract.cjs +1 -1
  45. package/dist-lib/capture-contract.mjs +1 -1
  46. package/dist-lib/cloud-contract.cjs +1 -1
  47. package/dist-lib/cloud-contract.mjs +1 -1
  48. package/dist-lib/cloud-test-args.cjs +1 -1
  49. package/dist-lib/cloud-test-args.mjs +1 -1
  50. package/dist-lib/cloud.cjs +1 -1
  51. package/dist-lib/cloud.mjs +1 -1
  52. package/dist-lib/config.cjs +1 -1
  53. package/dist-lib/config.mjs +1 -1
  54. package/dist-lib/detox/index.cjs +195 -63
  55. package/dist-lib/dev-bundle-resolution.cjs +14 -13
  56. package/dist-lib/expo-swift-bundle.cjs +18507 -11835
  57. package/dist-lib/expo-swift-metadata.cjs +4 -1
  58. package/dist-lib/expo-swift-metadata.mjs +3 -1
  59. package/dist-lib/home-paths.cjs +27 -3
  60. package/dist-lib/host/bridge-host.cjs +28373 -24090
  61. package/dist-lib/host/fetch-proxy-handler.cjs +235 -225
  62. package/dist-lib/host/fetch-proxy-overrides.cjs +1 -1
  63. package/dist-lib/host/fetch-proxy-overrides.mjs +1 -1
  64. package/dist-lib/host/replacement-module-handler.cjs +1 -1
  65. package/dist-lib/host/websocket-proxy.cjs +1 -1
  66. package/dist-lib/index.cjs +915 -399
  67. package/dist-lib/jump-to-source-babel.cjs +1 -1
  68. package/dist-lib/jump-to-source-native.cjs +1 -1
  69. package/dist-lib/kotlin-host.cjs +335 -47
  70. package/dist-lib/menu.cjs +13 -1
  71. package/dist-lib/menu.mjs +13 -1
  72. package/dist-lib/metro-fingerprint-registry.cjs +1 -1
  73. package/dist-lib/metro-fingerprint-registry.mjs +1 -1
  74. package/dist-lib/metro-production-bundle.cjs +1 -1
  75. package/dist-lib/metro-production-bundle.mjs +1 -1
  76. package/dist-lib/metro.cjs +918 -411
  77. package/dist-lib/native-app-identity-project.cjs +703 -283
  78. package/dist-lib/native-app-identity-project.mjs +500 -87
  79. package/dist-lib/native-app-identity.cjs +13 -2
  80. package/dist-lib/native-app-identity.mjs +13 -2
  81. package/dist-lib/native-firebase-metadata.cjs +1 -1
  82. package/dist-lib/native-firebase-metadata.mjs +1 -1
  83. package/dist-lib/plist.cjs +1 -1
  84. package/dist-lib/plist.mjs +1 -1
  85. package/dist-lib/profiles.cjs +5 -2
  86. package/dist-lib/public-brand.cjs +1 -1
  87. package/dist-lib/public-origins-lib-esm.json +28 -23
  88. package/dist-lib/public-origins-lib.json +418 -224
  89. package/dist-lib/react-native-host-modules.cjs +35 -2
  90. package/dist-lib/react-native-host-modules.mjs +35 -2
  91. package/dist-lib/reap-browser.cjs +12 -4
  92. package/dist-lib/scripts/dev-server-scanner.cjs +15 -8
  93. package/dist-lib/sdk.cjs +17 -7
  94. package/dist-lib/sdk.mjs +16 -7
  95. package/dist-lib/skills.cjs +44825 -29141
  96. package/dist-lib/swift-compile.cjs +616 -400
  97. package/dist-lib/swift-compile.mjs +528 -315
  98. package/dist-lib/swift-package.cjs +1 -1
  99. package/dist-lib/swift-package.mjs +1 -1
  100. package/dist-lib/swift-submit.cjs +2 -2
  101. package/dist-lib/swift-submit.mjs +2 -2
  102. package/dist-lib/swift-toolchain.cjs +105 -17
  103. package/dist-lib/swift.cjs +1 -1
  104. package/dist-lib/vite-swift.cjs +4660 -2252
  105. package/dist-lib/vite.cjs +248 -234
  106. package/dist-lib/worklet-executable.cjs +1 -1
  107. package/dist-lib/worklet-executable.mjs +1 -1
  108. package/dist-types/packages/peach-compat/src/stubs/native-seams/react-native-cookies.d.ts +17 -8
  109. package/dist-types/packages/peach-engine/src/engine/canvaskit-renderer.d.ts +1 -0
  110. package/dist-types/packages/peach-engine/src/engine/font-config.d.ts +7 -4
  111. package/dist-types/packages/peach-engine/src/engine/image-loader.d.ts +9 -6
  112. package/dist-types/packages/peach-engine/src/engine/liquid-glass.d.ts +0 -5
  113. package/dist-types/packages/peach-engine/src/engine/memory-counts.d.ts +11 -1
  114. package/dist-types/packages/peach-engine/src/engine/node.d.ts +5 -12
  115. package/dist-types/packages/peach-engine/src/engine/render-damage.d.ts +2 -2
  116. package/dist-types/packages/peach-engine/src/engine/text-first-baseline.d.ts +5 -0
  117. package/dist-types/packages/peach-engine/src/engine/text-paragraph.d.ts +1 -0
  118. package/dist-types/packages/peach-engine/src/engine/types.d.ts +3 -0
  119. package/dist-types/packages/peach-engine/src/native-preview-bundle.d.ts +2 -0
  120. package/dist-types/packages/peach-engine/src/render-worker/compositor-protocol.d.ts +9 -0
  121. package/dist-types/packages/peach-engine/src/render-worker/host-service-protocol.d.ts +3 -0
  122. package/dist-types/packages/peach-engine/src/render-worker/native-ui-commandContract.d.ts +1 -1
  123. package/dist-types/packages/peach-engine/src/render-worker/protocol.d.ts +7 -2
  124. package/dist-types/packages/peach-engine/src/render-worker/shell-protocol.d.ts +5 -0
  125. package/dist-types/packages/peach-engine/src/render-worker/tree-sync-port.d.ts +1 -1
  126. package/dist-types/packages/peach-engine/src/render-worker/worklet-runtime.d.ts +36 -2
  127. package/dist-types/packages/peach-engine/src/shell-events.d.ts +1 -0
  128. package/dist-types/packages/peach-globals/src/audioBridgeContract.d.ts +5 -0
  129. package/dist-types/packages/peach-globals/src/index.d.ts +1 -1
  130. package/dist-types/packages/peach-globals/src/keyboardBridgeContract.d.ts +11 -0
  131. package/dist-types/packages/peach-globals/src/shellBridgeContract.d.ts +1 -0
  132. package/dist-types/packages/peach-globals/src/testBridgeContract.d.ts +9 -2
  133. package/dist-types/packages/peach-kit/src/capabilities.d.ts +1 -0
  134. package/kotlin/README.md +3 -0
  135. package/kotlin/sdk.tar.gz +0 -0
  136. package/package.json +1 -1
  137. package/runtime-companion.json +1 -1
  138. package/scripts/dev-server-scanner.ts +12 -25
  139. package/sdk/ios/System/Library/Frameworks/Accounts.framework/Headers/ACAccount.h +55 -0
  140. package/sdk/ios/System/Library/Frameworks/Accounts.framework/Headers/ACAccountCredential.h +35 -0
  141. package/sdk/ios/System/Library/Frameworks/Accounts.framework/Headers/ACAccountStore.h +94 -0
  142. package/sdk/ios/System/Library/Frameworks/Accounts.framework/Headers/ACAccountType.h +53 -0
  143. package/sdk/ios/System/Library/Frameworks/Accounts.framework/Headers/ACError.h +37 -0
  144. package/sdk/ios/System/Library/Frameworks/Accounts.framework/Headers/Accounts.h +13 -0
  145. package/sdk/ios/System/Library/Frameworks/Accounts.framework/Headers/AccountsDefines.h +13 -0
  146. package/sdk/ios/System/Library/Frameworks/Accounts.framework/Modules/module.modulemap +5 -0
  147. package/sdk/ios/System/Library/Frameworks/AuthenticationServices.framework/Headers/ASFoundation.h +52 -0
  148. package/sdk/ios/System/Library/Frameworks/AuthenticationServices.framework/Headers/ASWebAuthenticationSession.h +128 -0
  149. package/sdk/ios/System/Library/Frameworks/AuthenticationServices.framework/Headers/ASWebAuthenticationSessionCallback.h +35 -0
  150. package/sdk/ios/System/Library/Frameworks/AuthenticationServices.framework/Headers/AuthenticationServices.h +3 -0
  151. package/sdk/ios/System/Library/Frameworks/AuthenticationServices.framework/Modules/module.modulemap +5 -0
  152. package/sdk/ios/System/Library/Frameworks/CFNetwork.framework/Headers/CFHTTPMessage.h +724 -0
  153. package/sdk/ios/System/Library/Frameworks/CFNetwork.framework/Headers/CFNetwork.h +4 -0
  154. package/sdk/ios/System/Library/Frameworks/CFNetwork.framework/Headers/CFNetworkDefs.h +27 -0
  155. package/sdk/ios/System/Library/Frameworks/CFNetwork.framework/Modules/module.modulemap +5 -0
  156. package/sdk/ios/System/Library/Frameworks/CoreFoundation.framework/Headers/CFURL.h +1301 -0
  157. package/sdk/ios/System/Library/Frameworks/CoreGraphics.framework/Headers/CoreGraphics.h +3 -1
  158. package/sdk/ios/System/Library/Frameworks/CoreLocation.framework/Headers/CLLocationManager.h +21 -50
  159. package/sdk/ios/System/Library/Frameworks/CoreLocation.framework/Headers/CLRegion.h +6 -6
  160. package/sdk/ios/System/Library/Frameworks/Foundation.framework/Headers/Foundation.apinotes +58 -3
  161. package/sdk/ios/System/Library/Frameworks/Foundation.framework/Headers/Foundation.h +711 -46
  162. package/sdk/ios/System/Library/Frameworks/UIKit.framework/Headers/UIApplication.h +12 -0
  163. package/sdk/ios/System/Library/Frameworks/UIKit.framework/Headers/UIInterface.h +23 -0
  164. package/sdk/ios/System/Library/Frameworks/UIKit.framework/Headers/UIKit.h +7 -9
  165. package/sdk/ios/System/Library/Frameworks/UIKit.framework/Headers/UIOrientation.h +14 -0
  166. package/sdk/ios/System/Library/Frameworks/UIKit.framework/Headers/UIStringDrawing.h +12 -0
  167. package/sdk/ios/System/Library/Frameworks/UIKit.framework/Headers/UIView.h +14 -0
  168. package/sdk/ios/System/Library/Frameworks/UIKit.framework/Headers/UIViewAnimating.h +12 -0
  169. package/sdk/ios/System/Library/Frameworks/UIKit.framework/Headers/UIViewController.h +16 -0
  170. package/sdk/ios/System/Library/Frameworks/UIKit.framework/Headers/UIWindow.h +2 -0
  171. package/sdk/ios/usr/include/ConditionalMacros.h +619 -0
  172. package/sdk/ios/usr/include/module.modulemap +1 -0
  173. package/sdk/ios/usr/include/objc/objc-sync.h +59 -0
  174. package/sdk/ios/usr/include/os/signpost.h +414 -0
  175. package/sdk/ios/usr/include/os.modulemap +10 -0
  176. package/sdk/ios/usr/include/sys/cdefs.h +5 -0
  177. package/sdk/ios/usr/include/sys/ioccom.h +99 -0
  178. package/sdk/ios/usr/include/sys/syslog.h +236 -0
  179. package/sdk/ios/usr/include/syslog.h +24 -0
  180. package/sdk/ios/wasm/include/errno.h +4 -20
  181. package/sdk/ios/wasm/include/fcntl.h +4 -2
  182. package/sdk/ios/wasm/include/pthread.h +3 -0
  183. package/sdk/ios/wasm/include/setjmp.h +57 -0
  184. package/sdk/ios/wasm/include/stdio.h +24 -0
  185. package/sdk/ios/wasm/include/stdlib.h +32 -7
  186. package/sdk/ios/wasm/include/sys/file.h +7 -0
  187. package/sdk/ios/wasm/include/sys/ioctl.h +6 -0
  188. package/sdk/ios/wasm/include/sys/resource.h +2 -2
  189. package/sdk/ios/wasm/include/sys/syscall.h +9 -0
  190. package/sdk/ios/wasm/include/sys/time.h +10 -0
  191. package/sdk/ios/wasm/include/unistd.h +12 -3
  192. package/sdk/native-hashes.json +4 -3
  193. package/skills/contrast/SKILL.md +2 -1
  194. package/skills/peach-debug/SKILL.md +0 -1
  195. package/skills/peach-perf/SKILL.md +0 -1
  196. package/skills/peach-setup/SKILL.md +0 -1
  197. package/skills/peach-test/SKILL.md +7 -4
  198. package/skills/peach-visual/SKILL.md +0 -1
  199. package/src/admitted-mac-build.ts +3 -0
  200. package/src/android-app-identity.ts +18 -0
  201. package/src/android-autolinking.ts +1 -0
  202. package/src/android-bridge-packages.ts +279 -0
  203. package/src/android-root-module.ts +9 -9
  204. package/src/appPackageContract.ts +83 -0
  205. package/src/bridge-contract.ts +50 -0
  206. package/src/bundle-channel.mjs +41 -6
  207. package/src/cli-runtime.ts +9 -5
  208. package/src/dev-bundle-resolution.ts +7 -27
  209. package/src/dom-source-reference.ts +15 -0
  210. package/src/expo-discovery-cache.ts +134 -26
  211. package/src/expo-kotlin-host.ts +97 -26
  212. package/src/expo-manifest-body.ts +33 -0
  213. package/src/expo-native-workspace.ts +158 -38
  214. package/src/expo-swift-host.ts +350 -75
  215. package/src/expo-swift-ready.ts +5 -1
  216. package/src/expoKotlinContract.ts +12 -2
  217. package/src/expoSwiftContract.ts +10 -0
  218. package/src/generated/swift-c-predefines.json +1 -1
  219. package/src/home-paths.ts +41 -2
  220. package/src/host/app-package-handler.ts +47 -0
  221. package/src/host/bridge-host.ts +8 -0
  222. package/src/host/fetch-proxy-handler.ts +10 -0
  223. package/src/host/staged-host-files.ts +18 -0
  224. package/src/index.ts +3 -0
  225. package/src/kotlin-host.ts +225 -48
  226. package/src/kotlin-instance-source.ts +126 -0
  227. package/src/kotlinSdkContract.ts +2 -1
  228. package/src/menu.ts +14 -0
  229. package/src/native-app-identity-project.ts +7 -2
  230. package/src/native-app-identity.ts +11 -0
  231. package/src/native-firebase-config.ts +24 -1
  232. package/src/native-prebuilt.ts +154 -0
  233. package/src/nitro-adapter.lock.json +270 -270
  234. package/src/nitro-adapter.ts +56 -50
  235. package/src/nitro-bundle.ts +92 -74
  236. package/src/nitro-compile.ts +45 -14
  237. package/src/nitro-dependencies.ts +63 -46
  238. package/src/nitro-source.ts +64 -26
  239. package/src/nitroAdapterContract.ts +146 -98
  240. package/src/nitroContract.ts +71 -21
  241. package/src/react-native-bridge-modules.ts +70 -42
  242. package/src/react-native-host-modules.ts +34 -1
  243. package/src/reactNativeConfigurationContract.ts +14 -0
  244. package/src/reap-browser.ts +15 -3
  245. package/src/sdk.ts +5 -0
  246. package/src/settingsContract.ts +38 -1
  247. package/src/sim-client.ts +4 -1
  248. package/src/storageSnapshotContract.ts +32 -81
  249. package/src/swift-compile.ts +268 -89
  250. package/src/swift-coverage/indexdump.ts +22 -6
  251. package/src/swift-coverage/indexing.ts +4 -4
  252. package/src/swift-coverage/provenance.ts +1 -0
  253. package/src/swift-coverage/sdk.ts +104 -20
  254. package/src/swift-coverage/types.ts +34 -0
  255. package/src/swift-coverage/xcode-asset-symbols.ts +922 -0
  256. package/src/swift-coverage/xcode-assets.ts +89 -131
  257. package/src/swift-coverage/xcode.ts +10 -2
  258. package/src/swift-cxx-reachable.ts +2 -1
  259. package/src/swift-distribution.lock.json +20 -16
  260. package/src/swift-host.ts +98 -22
  261. package/src/swift-icu.ts +1 -1
  262. package/src/swift-link-roots.ts +118 -0
  263. package/src/swift-objc-compile.ts +144 -22
  264. package/src/swift-objc-import.ts +6 -0
  265. package/src/swift-objc-standin.ts +2 -14
  266. package/src/swift-package-manifest.ts +14 -6
  267. package/src/swift-packages.ts +75 -11
  268. package/src/swift-pdf-raster.ts +1 -0
  269. package/src/swift-root-lock.ts +17 -1
  270. package/src/swift-source-pods.ts +2 -10
  271. package/src/swift-standin-emit.ts +597 -57
  272. package/src/swift-standins.ts +599 -56
  273. package/src/swift-submit.ts +7 -1
  274. package/src/swift-toolchain.ts +140 -17
  275. package/src/turbo-bundle.ts +81 -0
  276. package/src/turbo-codegen-intake.ts +91 -0
  277. package/src/turbo-compile.ts +120 -0
  278. package/src/turbo-runtime.lock.json +69 -0
  279. package/src/turbo-source.ts +754 -0
  280. package/src/vite-plugin-one.ts +17 -12
  281. package/src/vite-plugin.ts +28 -15
  282. package/src/xcode-project.ts +223 -22
@@ -15,9 +15,12 @@ import fs from 'node:fs'
15
15
  import path from 'node:path'
16
16
  import { cacheDir as peachCacheDir, swiftPackageCacheOptions } from './home-paths'
17
17
  import { isRecord } from './is-record'
18
+ import { fetchNativeEntry, nativeEntryKey } from './native-prebuilt'
18
19
  import { outputLockStdio } from './output-lock-stdio'
19
20
  import { parsePlistSource } from './plist'
20
21
  import {
22
+ FOUNDATION_IMPLICIT_MODULES,
23
+ foundationOwnerOf,
21
24
  swiftDistributionAt,
22
25
  swiftImportNames,
23
26
  swiftMacroPlugins,
@@ -27,23 +30,30 @@ import {
27
30
  swiftSdkPaths,
28
31
  } from './swift-compile'
29
32
  import { emitCxxStandin } from './swift-cxx-standin'
33
+ import { IOS_SDK_TARGET } from './swift-objc-sdk'
30
34
  import {
31
35
  IOS_SDK_ID,
32
36
  NATIVE_GRAPH_ID,
33
37
  pinnedNativeSdkReady,
34
38
  WASI_EMULATION,
35
39
  WASI_EMULATION_FLAGS,
36
- materializeObjcSdk,
37
40
  nativeInterfaceView,
38
41
  moduleMapHeaders,
39
42
  objcView,
40
43
  type ObjcSurface,
41
44
  } from './swift-objc-standin'
42
- import { moduleMapName, swiftTargetModuleName } from './swift-package'
45
+ import {
46
+ moduleMapName,
47
+ swiftManifestDeclarations,
48
+ swiftTargetModuleName,
49
+ } from './swift-package'
43
50
  import { STANDIN_SOURCE_DIGEST, standinSourceDigest } from './swift-standin-digest'
44
51
  import {
45
52
  STANDIN_IMPORTS,
46
53
  emitSwiftStandin,
54
+ isSwiftDeclaration,
55
+ objcProtocolsInSwiftSignatures,
56
+ type SymbolFragment,
47
57
  type SymbolGraph,
48
58
  type SwiftStandinBehaviors,
49
59
  type StandinBehavior,
@@ -325,16 +335,21 @@ const files = (dir: string, test: (name: string) => boolean): string[] =>
325
335
  : []
326
336
 
327
337
  /** the module surfaces an iOS simulator slice describes. */
328
- type Surface =
338
+ type SliceSurface =
329
339
  | { kind: 'swift'; module: string; interface: string }
330
340
  | (ObjcSurface & { kind: 'mixed'; interface: string })
331
341
  | { kind: 'c'; module: string; modulemap: string; headers: string }
332
342
  | (ObjcSurface & { kind: 'objc' })
333
343
 
344
+ type Surface =
345
+ | SliceSurface
346
+ // a placeholder package's swift target, from its own sources
347
+ | { kind: 'source'; module: string; sources: string[]; flags: string[] }
348
+
334
349
  const OBJC = /^\s*(?:@interface|@protocol|@class|@import|#import)\b/m
335
350
 
336
351
  // a module map's surface: objective-c when any header it reaches is.
337
- function headerSurface(module: string, modulemap: string, headers: string): Surface {
352
+ function headerSurface(module: string, modulemap: string, headers: string): SliceSurface {
338
353
  const objc = files(headers, (name) => name.endsWith('.h')).some((file) =>
339
354
  OBJC.test(fs.readFileSync(path.join(headers, file), 'utf8')),
340
355
  )
@@ -354,7 +369,7 @@ function simulatorSlice(xcframeworkDir: string): string | null {
354
369
  function surfaces(
355
370
  xcframeworkDir: string,
356
371
  slice = simulatorSlice(xcframeworkDir),
357
- ): Surface[] {
372
+ ): SliceSurface[] {
358
373
  const standaloneMap = path.join(xcframeworkDir, 'module.modulemap')
359
374
  if (fs.existsSync(standaloneMap)) {
360
375
  const module = moduleMapName(fs.readFileSync(standaloneMap, 'utf8'))
@@ -364,7 +379,7 @@ function surfaces(
364
379
  const plain = !slice && xcframeworkDir.endsWith('.framework')
365
380
  if (!slice && !plain) throw new Error('it has no iOS simulator slice')
366
381
  const root = plain ? path.dirname(xcframeworkDir) : slice!
367
- const found: Surface[] = []
382
+ const found: SliceSurface[] = []
368
383
  for (const entry of plain
369
384
  ? [path.basename(xcframeworkDir)]
370
385
  : fs.readdirSync(root).sort()) {
@@ -421,6 +436,7 @@ export function standinsNeedNativeSdk(binaries: readonly SwiftPackageBinary[]):
421
436
  return binaries.some(
422
437
  (binary) =>
423
438
  binary.publicHeaders === undefined &&
439
+ binary.placeholder === undefined &&
424
440
  surfaces(xcframework(binary).dir).some((surface) => surface.kind === 'mixed'),
425
441
  )
426
442
  }
@@ -478,11 +494,24 @@ function peachModuleDirs(toolchain: Toolchain, imports: string[]): string[] {
478
494
  ],
479
495
  moduleRoot,
480
496
  )
481
- // a c target's module map sits in its own build directory, where swiftpm
482
- // generated it; a peach module re-exporting one fails to load without it
497
+ // a c target's module map is the one swiftpm reads: a system library's in
498
+ // its directory, a c target's own in its include directory, else the one
499
+ // swiftpm generated in its build directory. a peach module re-exporting one
500
+ // fails to load without it
483
501
  const release = path.join(moduleRoot, '.build', TRIPLE, 'release')
484
502
  return [
485
503
  path.join(release, 'Modules'),
504
+ ...swiftManifestDeclarations(
505
+ fs.readFileSync(path.join(moduleRoot, 'Package.swift'), 'utf8'),
506
+ )
507
+ .map((target) =>
508
+ path.join(
509
+ moduleRoot,
510
+ target.path,
511
+ target.kind === 'systemLibrary' ? '' : 'include',
512
+ ),
513
+ )
514
+ .filter((dir) => fs.existsSync(path.join(dir, 'module.modulemap'))),
486
515
  ...fs
487
516
  .readdirSync(release)
488
517
  .filter(
@@ -531,7 +560,12 @@ export function swiftInterfaceForWasm(text: string, cFlags: readonly string[]):
531
560
  const interop = /\s-enable-objc-interop\b/.test(targeted)
532
561
  ? targeted
533
562
  : `${targeted} -enable-objc-interop`
534
- return `${interop} ${cFlags.flatMap((flag) => ['-Xcc', flag]).join(' ')}`
563
+ // raw modules skip the object wrap the toolset's -mllvm sjlj pair aborts in.
564
+ const xcc = [...cFlags, '-Xclang', '-fmodule-format=raw'].flatMap((flag) => [
565
+ '-Xcc',
566
+ flag,
567
+ ])
568
+ return `${interop} ${xcc.join(' ')}`
535
569
  })
536
570
  return retargeted
537
571
  .split('\n')
@@ -563,15 +597,22 @@ function swiftStandin(
563
597
  const wasm = wasmFlags(toolchain, imports)
564
598
  const out = path.join(work, 'graphs')
565
599
  fs.mkdirSync(out, { recursive: true })
566
- // symbolgraph-extract rejects -Xfrontend. the same interop switch is its own flag.
600
+ // symbolgraph-extract rejects -Xfrontend and -Xllvm. the interop switch is its own flag.
567
601
  const symbolgraphWasm: string[] = ['-enable-objc-interop']
568
602
  for (let index = 0; index < wasm.length; index++) {
569
603
  if (wasm[index] === '-Xfrontend' && wasm[index + 1] === '-enable-objc-interop') {
570
604
  index += 1
571
605
  continue
572
606
  }
607
+ if (wasm[index] === '-Xllvm') {
608
+ index += 1
609
+ continue
610
+ }
573
611
  symbolgraphWasm.push(wasm[index]!)
574
612
  }
613
+ // no -Xllvm either, so the -mllvm half of the toolset's sjlj pair never reaches the
614
+ // backend that wraps each clang module as an object; raw modules skip it.
615
+ symbolgraphWasm.push('-Xcc', '-Xclang', '-Xcc', '-fmodule-format=raw')
575
616
  exec(path.join(toolchain.toolchainBin, 'swift-symbolgraph-extract'), [
576
617
  '-module-name',
577
618
  surface.module,
@@ -600,6 +641,99 @@ function swiftStandin(
600
641
  work,
601
642
  behavior,
602
643
  [...new Set(imports)].filter((module) => siblings.has(module)),
644
+ true,
645
+ )
646
+ }
647
+
648
+ // a placeholder package's swift target from its own sources. the gap that
649
+ // forces the placeholder is a compile error in them, so the frontend reads
650
+ // only the public interface, skipping bodies and internals, and emits it in
651
+ // spite of errors; a declaration whose type did not resolve is left out.
652
+ function placeholderStandin(
653
+ surface: Extract<Surface, { kind: 'source' }>,
654
+ binary: SwiftPackageBinary,
655
+ toolchain: Toolchain,
656
+ work: string,
657
+ behavior: ReadonlyMap<string, StandinBehavior>,
658
+ siblings: ReadonlySet<string>,
659
+ ): SwiftStandin {
660
+ const texts = surface.sources.map((file) => fs.readFileSync(file, 'utf8'))
661
+ const imports = [...new Set(texts.flatMap((text) => swiftImportNames(text)))]
662
+ // every package target sees the modules darwin's Foundation exports
663
+ const wasm = [
664
+ ...wasmFlags(toolchain, [...FOUNDATION_IMPLICIT_MODULES, ...imports]),
665
+ ...FOUNDATION_IMPLICIT_MODULES.flatMap((module) => [
666
+ '-Xfrontend',
667
+ '-import-module',
668
+ '-Xfrontend',
669
+ module,
670
+ ]),
671
+ ]
672
+ const out = path.join(work, 'graphs')
673
+ fs.mkdirSync(path.join(work, 'interface'), { recursive: true })
674
+ fs.mkdirSync(out, { recursive: true })
675
+ const extracted = spawnSync(
676
+ path.join(toolchain.toolchainBin, 'swiftc'),
677
+ [
678
+ '-emit-module',
679
+ '-parse-as-library',
680
+ '-wmo',
681
+ '-module-name',
682
+ surface.module,
683
+ ...surface.flags,
684
+ ...wasm,
685
+ ...WASM_EH_FLAGS,
686
+ '-enable-library-evolution',
687
+ '-emit-module-path',
688
+ path.join(work, 'interface', `${surface.module}.swiftmodule`),
689
+ '-Xfrontend',
690
+ '-experimental-lazy-typecheck',
691
+ '-Xfrontend',
692
+ '-experimental-skip-non-exportable-decls',
693
+ '-experimental-skip-non-inlinable-function-bodies',
694
+ '-Xfrontend',
695
+ '-experimental-allow-module-with-compiler-errors',
696
+ '-emit-symbol-graph',
697
+ '-emit-symbol-graph-dir',
698
+ out,
699
+ '-symbol-graph-minimum-access-level',
700
+ 'public',
701
+ '-Xfrontend',
702
+ '-symbol-graph-skip-synthesized-members',
703
+ ...surface.sources,
704
+ ],
705
+ { encoding: 'utf8', env: swiftEnvironment(), maxBuffer: 256 * 1024 * 1024 },
706
+ )
707
+ const graphs = fs
708
+ .readdirSync(out)
709
+ .filter((name) => name.endsWith('.symbols.json'))
710
+ .sort()
711
+ .map((name): SymbolGraph => JSON.parse(fs.readFileSync(path.join(out, name), 'utf8')))
712
+ if (!graphs.some((graph) => graph.module.name === surface.module))
713
+ throw new Error(
714
+ `its public interface did not extract: ${extracted.error?.message ?? extracted.stderr.trim().slice(-2000)}`,
715
+ )
716
+ return graphStandin(
717
+ surface.module,
718
+ graphs,
719
+ new Map(),
720
+ wasm,
721
+ binary,
722
+ toolchain,
723
+ work,
724
+ behavior,
725
+ imports.filter((module) => siblings.has(module)),
726
+ false,
727
+ new Map(),
728
+ {
729
+ peachImports: FOUNDATION_IMPLICIT_MODULES,
730
+ exported: texts.flatMap((text) =>
731
+ text
732
+ .split('\n')
733
+ .filter((line) => /^\s*@_exported\s+import\s/.test(line))
734
+ .map((line) => line.trim()),
735
+ ),
736
+ },
603
737
  )
604
738
  }
605
739
 
@@ -613,26 +747,217 @@ function mixedStandin(
613
747
  siblings: ReadonlySet<string>,
614
748
  ): SwiftStandin {
615
749
  const view = nativeInterfaceView(surface, frameworkDirs, work)
750
+ const interfaceText = fs.readFileSync(surface.interface, 'utf8')
751
+ const values = new Map(view.values)
752
+ // native symbol graphs omit swift enum raw values. the compiler's public
753
+ // interface spells them; accept only simple top-level integer cases.
754
+ for (const graph of view.graphs) {
755
+ const enumCases = new Map<string, { id: string; name: string }[]>()
756
+ for (const member of graph.symbols)
757
+ if (
758
+ member.kind.identifier === 'swift.enum.case' &&
759
+ member.pathComponents.length === 2
760
+ ) {
761
+ const [parent, name] = member.pathComponents
762
+ enumCases.set(parent!, [
763
+ ...(enumCases.get(parent!) ?? []),
764
+ { id: member.identifier.precise, name: name! },
765
+ ])
766
+ }
767
+ for (const symbol of graph.symbols) {
768
+ if (
769
+ symbol.kind.identifier !== 'swift.enum' ||
770
+ !/^c:@C?M@[^@]+@E@/.test(symbol.identifier.precise) ||
771
+ symbol.pathComponents.length !== 1
772
+ )
773
+ continue
774
+ const name = symbol.pathComponents[0]!
775
+ const declaration = new RegExp(
776
+ `^@objc(?:\\([^)]*\\))?\\s+(?:(?:public|open)\\s+)?enum\\s+${name}\\s*:[^{]+\\{\\s*$`,
777
+ 'm',
778
+ ).exec(interfaceText)
779
+ if (!declaration) continue
780
+ const body =
781
+ interfaceText
782
+ .slice(declaration.index + declaration[0].length)
783
+ .split(/^\s*}\s*$/m)[0] ?? ''
784
+ const cases = new Map<string, string>()
785
+ let next = 0n
786
+ let valid = true
787
+ for (const line of body.split('\n')) {
788
+ const match = /^\s*(?:@\w+(?:\([^)]*\))?\s+)*case\s+(.+?)\s*$/.exec(line)
789
+ if (!match) continue
790
+ for (const part of match[1]!.split(',')) {
791
+ const item = /^\s*`?(\w+)`?(?:\s*=\s*(-?\d[\d_]*))?\s*$/.exec(part)
792
+ if (!item) {
793
+ valid = false
794
+ break
795
+ }
796
+ if (item[2] !== undefined) next = BigInt(item[2].replaceAll('_', ''))
797
+ cases.set(item[1]!, next.toString())
798
+ next++
799
+ }
800
+ if (!valid) break
801
+ }
802
+ if (!valid) continue
803
+ for (const member of enumCases.get(name) ?? []) {
804
+ const value = cases.get(member.name)
805
+ if (value !== undefined) values.set(member.id, value)
806
+ }
807
+ }
808
+ }
616
809
  const provided = new Set(swiftPeachModules(toolchain.moduleRoot))
617
- const imports = [
618
- ...new Set(swiftImportNames(fs.readFileSync(surface.interface, 'utf8'))),
619
- ].filter(
810
+ const owners = new Set<string>()
811
+ const graphs = view.graphs.map((graph) => {
812
+ const own = new Set(graph.symbols.map((symbol) => symbol.identifier.precise))
813
+ // darwin's Foundation names these bare; the guest keeps them in their owning
814
+ // modules, and the sdk headers declare the networking types and NSErrorPointer
815
+ // a second time, which makes the bare name ambiguous
816
+ const qualify = (fragment: SymbolFragment) => {
817
+ const owner =
818
+ fragment.kind === 'typeIdentifier' &&
819
+ fragment.preciseIdentifier !== undefined &&
820
+ !own.has(fragment.preciseIdentifier)
821
+ ? foundationOwnerOf(fragment.spelling)
822
+ : undefined
823
+ if (
824
+ owner === undefined ||
825
+ (owner !== 'FoundationNetworking' && fragment.spelling !== 'NSErrorPointer')
826
+ )
827
+ return fragment
828
+ owners.add(owner)
829
+ return { ...fragment, spelling: `${owner}.${fragment.spelling}` }
830
+ }
831
+ return {
832
+ ...graph,
833
+ symbols: graph.symbols.map((symbol) => ({
834
+ ...symbol,
835
+ declarationFragments: symbol.declarationFragments?.map(qualify),
836
+ functionSignature: symbol.functionSignature && {
837
+ ...symbol.functionSignature,
838
+ returns: symbol.functionSignature.returns?.map(qualify),
839
+ },
840
+ })),
841
+ }
842
+ })
843
+ const imports = [...new Set([...swiftImportNames(interfaceText), ...owners])].filter(
620
844
  (module) =>
621
845
  module !== surface.module && (provided.has(module) || siblings.has(module)),
622
846
  )
623
847
  return graphStandin(
624
848
  surface.module,
625
- view.graphs,
626
- view.values,
849
+ graphs,
850
+ values,
627
851
  wasmFlags(toolchain, imports),
628
852
  binary,
629
853
  toolchain,
630
854
  work,
631
855
  behavior,
632
856
  imports,
857
+ true,
858
+ surface.kind === 'mixed'
859
+ ? nativeProtocolProperties(surface, view.graphs, frameworkDirs, work)
860
+ : new Map(),
633
861
  )
634
862
  }
635
863
 
864
+ // symbol graphs omit custom property accessors. clang reads those selectors
865
+ // from the same native headers before a referenced objc protocol is emitted.
866
+ function nativeProtocolProperties(
867
+ surface: Extract<Surface, { kind: 'mixed' }>,
868
+ graphs: SymbolGraph[],
869
+ frameworkDirs: readonly string[],
870
+ work: string,
871
+ ): Map<string, { getter: string; setter?: string }> {
872
+ const properties = new Map<string, { getter: string; setter?: string }>()
873
+ const protocols = objcProtocolsInSwiftSignatures(graphs)
874
+ if (protocols.size === 0) return properties
875
+ const headers = files(surface.headers, (name) => name.endsWith('.h')).map((name) => ({
876
+ file: path.join(surface.headers, name),
877
+ text: fs.readFileSync(path.join(surface.headers, name), 'utf8'),
878
+ }))
879
+ // runner-toolchain-platform: darwin
880
+ const sdk = exec('xcrun', ['--sdk', 'iphonesimulator', '--show-sdk-path']).trim()
881
+ // runner-toolchain-platform: darwin
882
+ const clang = exec('xcrun', ['--find', 'clang']).trim()
883
+ for (const id of protocols) {
884
+ const name = /^c:objc\(pl\)(\w+)$/.exec(id)?.[1]
885
+ if (!name) throw new Error(`unknown Objective-C protocol identity ${id}`)
886
+ const pattern = new RegExp(`@protocol\\s+${name}\\b(?!\\s*;)`)
887
+ const candidates = headers.filter((header) => pattern.test(header.text))
888
+ if (candidates.length === 0)
889
+ throw new Error(`native headers have no definition of Objective-C protocol ${name}`)
890
+ const probe = path.join(work, 'objc-protocol-properties.m')
891
+ fs.writeFileSync(
892
+ probe,
893
+ candidates.map(({ file }) => `#import ${JSON.stringify(file)}\n`).join(''),
894
+ )
895
+ const output = exec(clang, [
896
+ '-x',
897
+ 'objective-c',
898
+ '-fobjc-arc',
899
+ '-fblocks',
900
+ '-fmodules',
901
+ `-fmodule-name=${surface.module}`,
902
+ '-fsyntax-only',
903
+ '-target',
904
+ IOS_SDK_TARGET,
905
+ '-isysroot',
906
+ sdk,
907
+ ...frameworkDirs.flatMap((dir) => ['-F', dir]),
908
+ '-I',
909
+ surface.headers,
910
+ '-Xclang',
911
+ '-ast-dump-all=json',
912
+ '-Xclang',
913
+ '-ast-dump-filter',
914
+ '-Xclang',
915
+ name,
916
+ probe,
917
+ ])
918
+ let found = false
919
+ for (const chunk of output.split(/\n(?=\{\n)/).filter((part) => part.trim())) {
920
+ const declaration: unknown = JSON.parse(chunk)
921
+ if (
922
+ !isRecord(declaration) ||
923
+ declaration.kind !== 'ObjCProtocolDecl' ||
924
+ declaration.name !== name ||
925
+ !Array.isArray(declaration.inner)
926
+ )
927
+ continue
928
+ found = true
929
+ for (const child of declaration.inner) {
930
+ if (
931
+ !isRecord(child) ||
932
+ child.kind !== 'ObjCPropertyDecl' ||
933
+ typeof child.name !== 'string'
934
+ )
935
+ continue
936
+ const getter =
937
+ isRecord(child.getter) && typeof child.getter.name === 'string'
938
+ ? child.getter.name
939
+ : child.name
940
+ const setter =
941
+ isRecord(child.setter) && typeof child.setter.name === 'string'
942
+ ? child.setter.name
943
+ : `set${child.name[0]?.toUpperCase() ?? ''}${child.name.slice(1)}:`
944
+ properties.set(`${id}(py)${child.name}`, {
945
+ getter,
946
+ ...(child.readonly === true ? {} : { setter }),
947
+ })
948
+ }
949
+ }
950
+ if (!found)
951
+ throw new Error(`clang found no definition of Objective-C protocol ${name}`)
952
+ }
953
+ return properties
954
+ }
955
+
956
+ // the sdk toolset passes this to every swiftc compile; raw swiftc does not read the toolset,
957
+ // and emitting a module aborts without it when the source reaches the wasm exception model.
958
+ // swift-symbolgraph-extract has no -Xllvm.
959
+ const WASM_EH_FLAGS = ['-Xllvm', '-wasm-enable-eh']
960
+
636
961
  // the wasm target, sdk and peach module search paths a stand-in compiles with.
637
962
  function wasmFlags(toolchain: Toolchain, imports: string[]): string[] {
638
963
  const { sysroot, resources, cFlags } = swiftSdkPaths(
@@ -654,20 +979,19 @@ function wasmFlags(toolchain: Toolchain, imports: string[]): string[] {
654
979
  // symbolgraph caller translates it.
655
980
  '-Xfrontend',
656
981
  '-enable-objc-interop',
982
+ // the toolset's -Xcc -mllvm -wasm-enable-sjlj lets libc's setjmp import,
983
+ // and the module build aborts unless its -Xllvm half is passed with it.
984
+ '-Xllvm',
985
+ '-wasm-enable-sjlj',
986
+ // the intake's darwin/usr/include declares `os` with its explicit
987
+ // submodules (os.log), so a vendor interface naming one resolves as the
988
+ // ios sdk resolves it; a second `os` map on the path is a redefinition.
657
989
  ...cFlags.flatMap((flag) => ['-Xcc', flag]),
658
- '-I',
659
- clangImportDir(),
660
990
  ...peachModuleDirs(toolchain, imports).flatMap((dir) => ['-I', dir]),
661
991
  ...(toolchain.standinModules ? ['-I', toolchain.standinModules] : []),
662
992
  ]
663
993
  }
664
994
 
665
- // the staged headerless import dir (swift-objc-standin.ts), carrying only
666
- // the sdk's os module map.
667
- function clangImportDir(): string {
668
- return path.join(materializeObjcSdk(cacheDir), 'clang-import')
669
- }
670
-
671
995
  // the objective-c module's swift view, emitted as an interface's graphs are.
672
996
  function objcStandin(
673
997
  surface: Extract<Surface, { kind: 'objc' }>,
@@ -698,6 +1022,11 @@ function objcStandin(
698
1022
  )
699
1023
  }
700
1024
 
1025
+ const OBJC_UNREPRESENTABLE =
1026
+ "cannot be marked '@objc' because its type cannot be represented in Objective-C"
1027
+ const INERT_VALUE =
1028
+ /missing argument|cannot be constructed|has no member 'init'|no exact matches in call to initializer|cannot convert return expression|does not conform to '|extra argument|takes no arguments|could not be inferred|cannot infer/
1029
+
701
1030
  // emit, typecheck for wasm against peach's modules, and leave out each
702
1031
  // declaration the compiler rejects, its error the reason, until the source
703
1032
  // is clean; a round that removes nothing fails the stand-in
@@ -711,12 +1040,46 @@ function graphStandin(
711
1040
  work: string,
712
1041
  behavior: ReadonlyMap<string, StandinBehavior>,
713
1042
  imports: string[] = [],
1043
+ requireSwift = false,
1044
+ objcProperties: ReadonlyMap<string, { getter: string; setter?: string }> = new Map(),
1045
+ // peach modules the source names beside its sibling dependencies, and a
1046
+ // placeholder's own re-exports
1047
+ placeholder: { peachImports: string[]; exported: string[] } | null = null,
714
1048
  ): SwiftStandin {
715
1049
  const paths = new Set(
716
1050
  graphs.flatMap((graph) =>
717
1051
  graph.symbols.map((symbol) => symbol.pathComponents.join('.')),
718
1052
  ),
719
1053
  )
1054
+ const swiftPaths = new Set(
1055
+ requireSwift
1056
+ ? graphs.flatMap((graph) =>
1057
+ graph.symbols
1058
+ .filter(isSwiftDeclaration)
1059
+ .map((symbol) => symbol.pathComponents.join('.')),
1060
+ )
1061
+ : [],
1062
+ )
1063
+ if (requireSwift) {
1064
+ const protocols = objcProtocolsInSwiftSignatures(graphs)
1065
+ const names = new Map<string, string>()
1066
+ for (const graph of graphs)
1067
+ for (const symbol of graph.symbols)
1068
+ names.set(symbol.identifier.precise, symbol.pathComponents.join('.'))
1069
+ for (const graph of graphs)
1070
+ for (const edge of graph.relationships)
1071
+ if (
1072
+ protocols.has(edge.target) &&
1073
+ (edge.kind === 'requirementOf' || edge.kind === 'optionalRequirementOf')
1074
+ ) {
1075
+ const member = names.get(edge.source)
1076
+ if (member) swiftPaths.add(member)
1077
+ }
1078
+ for (const protocol of protocols) {
1079
+ const name = names.get(protocol)
1080
+ if (name) swiftPaths.add(name)
1081
+ }
1082
+ }
720
1083
  for (const name of behavior.keys()) {
721
1084
  if (!paths.has(name))
722
1085
  throw new Error(
@@ -725,7 +1088,16 @@ function graphStandin(
725
1088
  }
726
1089
  const omit = new Map<string, string>()
727
1090
  const plain = new Set<string>()
1091
+ const unobjc = new Set<string>()
1092
+ const trapped = new Set<string>()
728
1093
  const file = path.join(work, `${module}.swift`)
1094
+ // a source target whose implementation compiles keeps inert bodies, and a
1095
+ // placeholder sends nothing. a target replaced because assembly cannot
1096
+ // lower has no implementation to keep, so its declarations trap by name
1097
+ // like a binary framework's.
1098
+ const inert =
1099
+ binary.placeholder !== undefined ||
1100
+ (binary.publicHeaders !== undefined && binary.assemblySources === undefined)
729
1101
  for (;;) {
730
1102
  const { source, owners, skipped } = emitSwiftStandin(graphs, {
731
1103
  module,
@@ -734,14 +1106,21 @@ function graphStandin(
734
1106
  behavior,
735
1107
  omit,
736
1108
  plain,
737
- // a source target whose implementation compiles keeps inert bodies. a
738
- // target replaced because assembly cannot lower has no implementation to
739
- // keep, so its declarations trap by name like a binary framework's.
740
- inert: binary.publicHeaders !== undefined && binary.assemblySources === undefined,
741
- imports,
742
- family: imports,
1109
+ unobjc,
1110
+ trapped,
1111
+ inert,
1112
+ imports: [...(placeholder?.peachImports ?? []), ...imports],
1113
+ family: [...imports, ...(placeholder?.peachImports ?? [])],
1114
+ objcProperties,
1115
+ ...(placeholder
1116
+ ? { exported: placeholder.exported, placeholder: binary.placeholder!.gap }
1117
+ : {}),
743
1118
  })
744
1119
  for (const entry of skipped) {
1120
+ if (swiftPaths.has(entry.path))
1121
+ throw new Error(
1122
+ `Swift declaration ${module}.${entry.path} was not emitted: ${entry.reason}`,
1123
+ )
745
1124
  if (behavior.has(entry.path))
746
1125
  throw new Error(
747
1126
  `Configured Swift behavior ${module}.${entry.path} was not emitted: ${entry.reason}`,
@@ -760,6 +1139,7 @@ function graphStandin(
760
1139
  '-module-name',
761
1140
  module,
762
1141
  ...wasm,
1142
+ ...WASM_EH_FLAGS,
763
1143
  '-emit-module-path',
764
1144
  path.join(work, `${module}.swiftmodule`),
765
1145
  file,
@@ -773,20 +1153,33 @@ function graphStandin(
773
1153
  files: { [`${module}.swift`]: source },
774
1154
  ...(imports.length ? { dependencies: imports } : {}),
775
1155
  }
776
- const before = omit.size + plain.size
1156
+ const before = omit.size + plain.size + unobjc.size + trapped.size
777
1157
  const elsewhere: string[] = []
778
1158
  for (const match of check.stderr.matchAll(/^(\S+?):(\d+):\d+: error: (.*)$/gm)) {
779
1159
  const owner = match[1] === file ? owners[Number(match[2]) - 1] : undefined
1160
+ // the guest's protocol for an objc type is a swift protocol, so the
1161
+ // declaration that names it cannot be objc-visible
1162
+ if (owner && match[3]!.includes(OBJC_UNREPRESENTABLE)) {
1163
+ unobjc.add(owner)
1164
+ continue
1165
+ }
1166
+ if (owner && swiftPaths.has(owner))
1167
+ throw new Error(
1168
+ `Swift declaration ${module}.${owner} does not typecheck: ${match[3]}`,
1169
+ )
780
1170
  if (owner && behavior.has(owner))
781
1171
  throw new Error(
782
1172
  `Configured Swift behavior ${module}.${owner} does not typecheck: ${match[3]}`,
783
1173
  )
784
1174
  if (owner && /does not override any|overrides nothing/.test(match[3]!))
785
1175
  plain.add(owner)
1176
+ // an inert value the declaration's type cannot build: its body traps
1177
+ else if (owner && inert && INERT_VALUE.test(match[3]!) && !trapped.has(owner))
1178
+ trapped.add(owner)
786
1179
  else if (owner) omit.set(owner, `rejected by swiftc: ${match[3]}`)
787
1180
  else elsewhere.push(match[3]!)
788
1181
  }
789
- if (omit.size + plain.size === before)
1182
+ if (omit.size + plain.size + unobjc.size + trapped.size === before)
790
1183
  throw new Error(
791
1184
  `its generated source does not typecheck: ${elsewhere.slice(0, 3).join('; ') || check.stderr.trim().slice(0, 400)}`,
792
1185
  )
@@ -1204,7 +1597,7 @@ function cxxStandin(
1204
1597
  }
1205
1598
  }
1206
1599
 
1207
- const RUST_TARGET = 'wasm32-wasip1'
1600
+ export const RUST_TARGET = 'wasm32-wasip1'
1208
1601
  // directories a crate search never enters: build output and installed deps
1209
1602
  const UNSEARCHED = new Set([
1210
1603
  'node_modules',
@@ -1217,7 +1610,7 @@ const UNSEARCHED = new Set([
1217
1610
 
1218
1611
  // the crate whose static library is `lib<name>.a`: its `[lib] name`, or its
1219
1612
  // package name with dashes as underscores, as cargo names the library.
1220
- function crateFor(root: string, name: string): string | null {
1613
+ export function crateFor(root: string, name: string): string | null {
1221
1614
  const walk = (dir: string, depth: number): string | null => {
1222
1615
  const manifest = path.join(dir, 'Cargo.toml')
1223
1616
  if (fs.existsSync(manifest)) {
@@ -1275,12 +1668,92 @@ function simulatorLibraryName(dir: string): string | undefined {
1275
1668
  .find((name): name is string => typeof name === 'string' && /^lib.+\.a$/.test(name))
1276
1669
  }
1277
1670
 
1671
+ /**
1672
+ * the Rust toolchain the registry's `rust` entries are built with. a host looks
1673
+ * entries up under this release whatever cargo it has, so a host with no wasm
1674
+ * target links the archive the publisher built.
1675
+ */
1676
+ export const RUST_TOOLCHAIN = '1.94.0'
1677
+
1678
+ // the directories a crate's build reads: the workspace that owns its Cargo.lock
1679
+ // plus every path dependency that lies outside it.
1680
+ function crateSourceRoots(manifest: string): string[] {
1681
+ let workspace = path.dirname(manifest)
1682
+ while (!fs.existsSync(path.join(workspace, 'Cargo.lock'))) {
1683
+ const parent = path.dirname(workspace)
1684
+ if (parent === workspace)
1685
+ throw new Error(`${manifest} has no Cargo.lock; the build runs --locked`)
1686
+ workspace = parent
1687
+ }
1688
+ const roots = [workspace]
1689
+ const inside = (dir: string) =>
1690
+ roots.some((root) => !path.relative(root, dir).startsWith('..'))
1691
+ const manifests = (dir: string): string[] =>
1692
+ fs
1693
+ .readdirSync(dir, { withFileTypes: true })
1694
+ .flatMap((entry) =>
1695
+ entry.isDirectory() && !UNSEARCHED.has(entry.name)
1696
+ ? manifests(path.join(dir, entry.name))
1697
+ : entry.name === 'Cargo.toml'
1698
+ ? [path.join(dir, entry.name)]
1699
+ : [],
1700
+ )
1701
+ for (let scanned = 0; scanned < roots.length; scanned++)
1702
+ for (const file of manifests(roots[scanned])) {
1703
+ const text = fs.readFileSync(file, 'utf8')
1704
+ for (const match of text.matchAll(/\bpath\s*=\s*"([^"]+)"/g)) {
1705
+ const dir = path.resolve(path.dirname(file), match[1])
1706
+ if (!inside(dir)) roots.push(dir)
1707
+ }
1708
+ }
1709
+ return roots
1710
+ }
1711
+
1712
+ // a digest of every file under the roots a crate builds from, build output and
1713
+ // installed dependencies aside.
1714
+ function crateSourceDigest(manifest: string): string {
1715
+ const hash = createHash('sha256')
1716
+ const walk = (dir: string, root: string) => {
1717
+ for (const entry of fs
1718
+ .readdirSync(dir, { withFileTypes: true })
1719
+ .sort((left, right) => (left.name < right.name ? -1 : 1))) {
1720
+ const full = path.join(dir, entry.name)
1721
+ if (entry.isDirectory()) {
1722
+ if (!UNSEARCHED.has(entry.name)) walk(full, root)
1723
+ } else if (entry.isFile()) {
1724
+ hash.update(`${path.relative(root, full)}\0`)
1725
+ hash.update(fs.readFileSync(full))
1726
+ hash.update('\0')
1727
+ }
1728
+ }
1729
+ }
1730
+ for (const root of crateSourceRoots(manifest)) {
1731
+ hash.update(`root\0`)
1732
+ walk(root, root)
1733
+ }
1734
+ return hash.digest('hex')
1735
+ }
1736
+
1737
+ /** the registry entry a crate's static library lives under. */
1738
+ export function rustCrateEntry(manifest: string, library: string) {
1739
+ const inputs = {
1740
+ crate: library,
1741
+ source: crateSourceDigest(manifest),
1742
+ target: RUST_TARGET,
1743
+ build: 'release locked',
1744
+ rust: RUST_TOOLCHAIN,
1745
+ rustflags: process.env.RUSTFLAGS ?? '',
1746
+ }
1747
+ return { name: library, inputs, key: nativeEntryKey(inputs) }
1748
+ }
1749
+
1278
1750
  /**
1279
1751
  * the WebAssembly static library a c stand-in links in place of its traps,
1280
1752
  * or null when it has none. a build the developer supplies in the
1281
1753
  * xcframework (`wasm32-wasip1/lib<name>.a`) wins; otherwise the crate under
1282
- * `searchRoot` that builds the slice's library compiles with the developer's
1283
- * own cargo. peach ships no rust compiler.
1754
+ * `searchRoot` that builds the slice's library comes from the registry when
1755
+ * it holds that crate's source and toolchain, else compiles with the
1756
+ * developer's own cargo. peach ships no rust compiler.
1284
1757
  */
1285
1758
  export function standinLibrary(
1286
1759
  binary: SwiftPackageBinary,
@@ -1303,7 +1776,20 @@ export function standinLibrary(
1303
1776
  if (!library) return null
1304
1777
  const manifest = crateFor(searchRoot, library.slice('lib'.length, -'.a'.length))
1305
1778
  if (!manifest) return null
1779
+ const entry = rustCrateEntry(manifest, library)
1780
+ const prebuilt = fetchNativeEntry('rust', entry.name, entry.key)
1781
+ if (prebuilt) return path.join(prebuilt, library)
1782
+ return buildRustCrate(manifest, library, searchRoot)
1783
+ }
1784
+
1785
+ /** compiles a crate for the wasm target with the environment's cargo. */
1786
+ export function buildRustCrate(
1787
+ manifest: string,
1788
+ library: string,
1789
+ searchRoot: string,
1790
+ ): string {
1306
1791
  const result = spawnSync(
1792
+ // runner-toolchain-platform: darwin
1307
1793
  'cargo',
1308
1794
  [
1309
1795
  'build',
@@ -1391,7 +1877,14 @@ function sourceStandins(
1391
1877
  const headers = new Map<string, string>()
1392
1878
  for (const [index, binary] of binaries.entries()) {
1393
1879
  try {
1394
- if (binary.publicHeaders !== undefined) {
1880
+ if (binary.placeholder !== undefined) {
1881
+ const dir = path.join(artifact, 'placeholders', String(index))
1882
+ for (const source of binary.placeholder.sources) {
1883
+ fs.mkdirSync(path.dirname(path.join(dir, source)), { recursive: true })
1884
+ fs.copyFileSync(path.join(binary.dir, source), path.join(dir, source))
1885
+ }
1886
+ local.push({ ...binary, dir, url: null, checksum: null })
1887
+ } else if (binary.publicHeaders !== undefined) {
1395
1888
  const copyHeader = (relative: string) => {
1396
1889
  const source = fs.realpathSync(path.join(binary.dir, relative))
1397
1890
  let destination = headers.get(source)
@@ -1590,7 +2083,7 @@ export function standinGenerationInputs(
1590
2083
  const modules = new Set(
1591
2084
  binaries.flatMap((binary) => {
1592
2085
  try {
1593
- return binary.publicHeaders !== undefined
2086
+ return binary.publicHeaders !== undefined || binary.placeholder !== undefined
1594
2087
  ? [swiftTargetModuleName(binary.target)]
1595
2088
  : surfaces(xcframework(binary).dir).map((surface) => surface.module)
1596
2089
  } catch {
@@ -1613,6 +2106,21 @@ export function standinGenerationInputs(
1613
2106
  ),
1614
2107
  )
1615
2108
  for (const binary of binaries) {
2109
+ if (binary.placeholder !== undefined) {
2110
+ parts.push(
2111
+ binary.target,
2112
+ binary.source,
2113
+ `placeholder/${binary.placeholder.gap}`,
2114
+ ...binary.placeholder.flags,
2115
+ ...binary.placeholder.sources.flatMap((source) => [
2116
+ source,
2117
+ createHash('sha256')
2118
+ .update(fs.readFileSync(path.join(binary.dir, source)))
2119
+ .digest('hex'),
2120
+ ]),
2121
+ )
2122
+ continue
2123
+ }
1616
2124
  if (
1617
2125
  binary.publicHeaders !== undefined &&
1618
2126
  swiftTargetModuleName(binary.target) !== binary.target
@@ -1684,7 +2192,19 @@ function binaryStandins(
1684
2192
  let all: Surface[]
1685
2193
  let digest: string
1686
2194
  try {
1687
- if (binary.publicHeaders !== undefined) {
2195
+ if (binary.placeholder !== undefined) {
2196
+ all = [
2197
+ {
2198
+ kind: 'source',
2199
+ module: swiftTargetModuleName(binary.target),
2200
+ sources: binary.placeholder.sources.map((source) =>
2201
+ path.join(binary.dir, source),
2202
+ ),
2203
+ flags: binary.placeholder.flags,
2204
+ },
2205
+ ]
2206
+ digest = binary.placeholder.gap
2207
+ } else if (binary.publicHeaders !== undefined) {
1688
2208
  const headers = path.join(binary.dir, binary.publicHeaders)
1689
2209
  const module = swiftTargetModuleName(binary.target)
1690
2210
  const modulemap = path.join(work, 'module.modulemap')
@@ -1744,13 +2264,15 @@ function binaryStandins(
1744
2264
  hash.update(`${surface.kind}\0${surface.module}\0`)
1745
2265
  for (const file of surface.kind === 'swift'
1746
2266
  ? [surface.interface]
1747
- : [
1748
- ...(surface.kind === 'mixed' ? [surface.interface] : []),
1749
- surface.modulemap,
1750
- ...files(surface.headers, (name) => name.endsWith('.h')).map((file) =>
1751
- path.join(surface.headers, file),
1752
- ),
1753
- ])
2267
+ : surface.kind === 'source'
2268
+ ? surface.sources
2269
+ : [
2270
+ ...(surface.kind === 'mixed' ? [surface.interface] : []),
2271
+ surface.modulemap,
2272
+ ...files(surface.headers, (name) => name.endsWith('.h')).map((file) =>
2273
+ path.join(surface.headers, file),
2274
+ ),
2275
+ ])
1754
2276
  hash.update(fs.readFileSync(file))
1755
2277
  }
1756
2278
  const id = hash.digest('hex').slice(0, 32)
@@ -1775,36 +2297,45 @@ function binaryStandins(
1775
2297
  throw new Error(
1776
2298
  "Configured behavior requires the framework's Swift declarations",
1777
2299
  )
1778
- return surface.kind === 'mixed'
1779
- ? mixedStandin(
2300
+ return surface.kind === 'source'
2301
+ ? placeholderStandin(
1780
2302
  surface,
1781
2303
  binary,
1782
2304
  toolchain,
1783
2305
  dir,
1784
2306
  new Map(Object.entries(configured.get(surface.module) ?? {})),
1785
- frameworks.dirs,
1786
2307
  siblings,
1787
2308
  )
1788
- : surface.kind === 'swift'
1789
- ? swiftStandin(
2309
+ : surface.kind === 'mixed'
2310
+ ? mixedStandin(
1790
2311
  surface,
1791
2312
  binary,
1792
2313
  toolchain,
1793
2314
  dir,
1794
2315
  new Map(Object.entries(configured.get(surface.module) ?? {})),
2316
+ frameworks.dirs,
1795
2317
  siblings,
1796
2318
  )
1797
- : surface.kind === 'objc'
1798
- ? objcStandin(
2319
+ : surface.kind === 'swift'
2320
+ ? swiftStandin(
1799
2321
  surface,
1800
2322
  binary,
1801
2323
  toolchain,
1802
2324
  dir,
1803
2325
  new Map(Object.entries(configured.get(surface.module) ?? {})),
2326
+ siblings,
1804
2327
  )
1805
- : binary.cxxHeaders
1806
- ? cxxStandin(surface, binary, toolchain, dir)
1807
- : cStandin(surface, binary, toolchain, dir)
2328
+ : surface.kind === 'objc'
2329
+ ? objcStandin(
2330
+ surface,
2331
+ binary,
2332
+ toolchain,
2333
+ dir,
2334
+ new Map(Object.entries(configured.get(surface.module) ?? {})),
2335
+ )
2336
+ : binary.cxxHeaders
2337
+ ? cxxStandin(surface, binary, toolchain, dir)
2338
+ : cStandin(surface, binary, toolchain, dir)
1808
2339
  } catch (error) {
1809
2340
  throw new Error(
1810
2341
  `no stand-in for ${surface.module}: ${error instanceof Error && !('stderr' in error) ? error.message : failure(error)}`,
@@ -1882,6 +2413,17 @@ export function swiftStandins(
1882
2413
  // the modules each binary's swift interfaces import
1883
2414
  const interfaceImports = new Map<SwiftPackageBinary, Set<string>>()
1884
2415
  for (const binary of binaries) {
2416
+ if (binary.placeholder !== undefined) {
2417
+ interfaceImports.set(
2418
+ binary,
2419
+ new Set(
2420
+ binary.placeholder.sources.flatMap((source) =>
2421
+ swiftImportNames(fs.readFileSync(path.join(binary.dir, source), 'utf8')),
2422
+ ),
2423
+ ),
2424
+ )
2425
+ continue
2426
+ }
1885
2427
  if (binary.publicHeaders !== undefined) continue
1886
2428
  try {
1887
2429
  const artifact = xcframework(binary)
@@ -1977,7 +2519,8 @@ export function swiftStandins(
1977
2519
  '5',
1978
2520
  '-module-name',
1979
2521
  standin.module,
1980
- ...wasmFlags(generatedToolchain, standin.dependencies ?? []),
2522
+ ...wasmFlags(generatedToolchain, swiftImportNames(source)),
2523
+ ...WASM_EH_FLAGS,
1981
2524
  '-emit-module-path',
1982
2525
  path.join(modules, `${standin.module}.swiftmodule`),
1983
2526
  file,