peachsim 0.1.2255 → 0.1.2257

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 (116) hide show
  1. package/cli/bridge-flow-runner.ts +41 -0
  2. package/cli/commands/control.ts +8 -2
  3. package/cli/commands/flow.ts +9 -0
  4. package/cli/commands/swift.ts +5 -0
  5. package/cli/current-sim.ts +23 -15
  6. package/cli/outbound-endpoints.ts +0 -2
  7. package/cli/secure-file.ts +8 -2
  8. package/dist-lib/agent-daemon-client.cjs +1 -1
  9. package/dist-lib/agent-events.cjs +1 -1
  10. package/dist-lib/attached-projects.cjs +1 -1
  11. package/dist-lib/auth/shared-session.cjs +1 -1
  12. package/dist-lib/backend-origin.cjs +1 -1
  13. package/dist-lib/beta.cjs +1 -1
  14. package/dist-lib/beta.mjs +1 -1
  15. package/dist-lib/bridge-constants.cjs +1 -1
  16. package/dist-lib/bridge-contract-input.cjs +1 -1
  17. package/dist-lib/bridge-contract-input.mjs +1 -1
  18. package/dist-lib/bridge-contract.cjs +1 -1
  19. package/dist-lib/bridge-contract.mjs +1 -1
  20. package/dist-lib/bundle-channel.cjs +390 -130
  21. package/dist-lib/capture-contract.cjs +1 -1
  22. package/dist-lib/capture-contract.mjs +1 -1
  23. package/dist-lib/cloud-contract.cjs +1 -1
  24. package/dist-lib/cloud-contract.mjs +1 -1
  25. package/dist-lib/cloud-test-args.cjs +1 -1
  26. package/dist-lib/cloud-test-args.mjs +1 -1
  27. package/dist-lib/cloud.cjs +1 -1
  28. package/dist-lib/cloud.mjs +1 -1
  29. package/dist-lib/config.cjs +1 -1
  30. package/dist-lib/config.mjs +1 -1
  31. package/dist-lib/detox/index.cjs +16 -12
  32. package/dist-lib/dev-bundle-resolution.cjs +1 -1
  33. package/dist-lib/expo-swift-bundle.cjs +352 -113
  34. package/dist-lib/expo-swift-metadata.cjs +1 -1
  35. package/dist-lib/expo-swift-metadata.mjs +1 -1
  36. package/dist-lib/home-paths.cjs +1 -1
  37. package/dist-lib/host/bridge-host.cjs +911 -582
  38. package/dist-lib/host/fetch-proxy-handler.cjs +3607 -1627
  39. package/dist-lib/host/fetch-proxy-overrides.cjs +1 -1
  40. package/dist-lib/host/fetch-proxy-overrides.mjs +1 -1
  41. package/dist-lib/host/replacement-module-handler.cjs +1 -1
  42. package/dist-lib/host/websocket-proxy.cjs +1 -1
  43. package/dist-lib/index.cjs +12611 -11232
  44. package/dist-lib/jump-to-source-babel.cjs +1 -1
  45. package/dist-lib/jump-to-source-native.cjs +1 -1
  46. package/dist-lib/kotlin-host.cjs +5 -4
  47. package/dist-lib/menu.cjs +1 -1
  48. package/dist-lib/menu.mjs +1 -1
  49. package/dist-lib/metro-fingerprint-registry.cjs +1 -1
  50. package/dist-lib/metro-fingerprint-registry.mjs +1 -1
  51. package/dist-lib/metro-production-bundle.cjs +1 -1
  52. package/dist-lib/metro-production-bundle.mjs +1 -1
  53. package/dist-lib/metro.cjs +9700 -8326
  54. package/dist-lib/native-app-identity-project.cjs +2 -2
  55. package/dist-lib/native-app-identity-project.mjs +2 -2
  56. package/dist-lib/native-app-identity.cjs +1 -1
  57. package/dist-lib/native-app-identity.mjs +1 -1
  58. package/dist-lib/native-firebase-metadata.cjs +1 -1
  59. package/dist-lib/native-firebase-metadata.mjs +1 -1
  60. package/dist-lib/pdfium-raster.cjs +1 -1
  61. package/dist-lib/pdfium-raster.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 +24 -24
  67. package/dist-lib/public-origins-lib.json +606 -228
  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/reap-browser.cjs +1 -1
  71. package/dist-lib/scripts/dev-server-scanner.cjs +1 -1
  72. package/dist-lib/sdk.cjs +1 -1
  73. package/dist-lib/sdk.mjs +1 -1
  74. package/dist-lib/skills.cjs +1164 -784
  75. package/dist-lib/swift-compile.cjs +334 -99
  76. package/dist-lib/swift-compile.mjs +334 -99
  77. package/dist-lib/swift-package.cjs +1 -1
  78. package/dist-lib/swift-package.mjs +1 -1
  79. package/dist-lib/swift-submit.cjs +1 -1
  80. package/dist-lib/swift-submit.mjs +1 -1
  81. package/dist-lib/swift-toolchain.cjs +1 -1
  82. package/dist-lib/swift.cjs +1 -1
  83. package/dist-lib/vite-swift.cjs +335 -100
  84. package/dist-lib/vite.cjs +4729 -2900
  85. package/dist-lib/worklet-executable.cjs +1 -1
  86. package/dist-lib/worklet-executable.mjs +1 -1
  87. package/dist-types/packages/peach-engine/src/render-worker/reanimated-readiness.d.ts +1 -0
  88. package/dist-types/packages/peach-globals/src/appLifecycleBridgeContract.d.ts +1 -0
  89. package/dist-types/packages/peach-globals/src/index.d.ts +1 -0
  90. package/dist-types/packages/peach-globals/src/lockstepCaptureBridgeContract.d.ts +1 -0
  91. package/dist-types/packages/peach-kit/src/transitions/screen-transition-math.d.ts +6 -0
  92. package/java-platform/licenses/android.txt +183 -0
  93. package/java-platform/licenses/arm-optimized-routines.txt +256 -0
  94. package/java-platform/licenses/fdlibm.txt +72 -0
  95. package/java-platform/licenses/icu.txt +50 -0
  96. package/java-platform/licenses/openjdk.txt +395 -0
  97. package/java-platform/licenses/teavm.txt +182 -0
  98. package/java-platform/licenses/threeten-backport.txt +29 -0
  99. package/kotlin/README.md +1 -1
  100. package/kotlin/sdk.tar.gz +0 -0
  101. package/package.json +2 -1
  102. package/runtime-companion.json +1 -1
  103. package/src/expo-autolinking.ts +47 -4
  104. package/src/expo-pod-prebuilt.ts +96 -9
  105. package/src/expo-swift-host.ts +29 -9
  106. package/src/kotlin-host.ts +3 -2
  107. package/src/kotlinSdkContract.ts +1 -0
  108. package/src/native-package-slice.ts +810 -0
  109. package/src/native-prebuilt.ts +4 -0
  110. package/src/native-seam-manifest.ts +0 -8
  111. package/src/nitro-adapter.lock.json +255 -255
  112. package/src/nitro-superset.lock.json +4 -4
  113. package/src/swift-distribution.lock.json +11 -11
  114. package/src/swift-link-roots.ts +23 -22
  115. package/src/swift-wasm-prune.ts +436 -105
  116. package/src/turbo-runtime.lock.json +23 -23
@@ -22,7 +22,6 @@ import { createHash } from 'node:crypto'
22
22
  import fs from 'node:fs'
23
23
  import path from 'node:path'
24
24
  import * as v from 'valibot'
25
- import { foundationCategoryObjects } from './swift-foundation'
26
25
  import { swiftCacheDir, swiftFileChecksum } from './swift-toolchain'
27
26
  import {
28
27
  SWIFT_FRAMEWORK_PRODUCT_FILE,
@@ -154,6 +153,7 @@ interface DataReloc {
154
153
  rawBytes: Uint8Array
155
154
  ro: number
156
155
  rs: number
156
+ addend: number
157
157
  segIdx: number
158
158
  offInSeg: number
159
159
  ownerSymIdx: number
@@ -191,6 +191,10 @@ interface ParsedObject {
191
191
  objcRegisterFuncToTypePrefix: Map<number, string>
192
192
  objcClassSymToTypePrefix: Map<string, string>
193
193
  typePrefixesInObj: Set<string>
194
+ /** For each `Mc` symbol index in `swift5_protocol_conformances`, target `$s...Mp` protocol descriptor name. */
195
+ conformanceProtocols: Map<number, string>
196
+ /** ObjC class names targeted by `__objc_cats` categories in this object. */
197
+ objcCategoryTargets: string[]
194
198
  rootFuncs: number[]
195
199
  rootSegs: number[]
196
200
  /** absolute file offsets of each data segment's payload. */
@@ -398,7 +402,12 @@ function parseWasmObjectBytes(
398
402
  const initFuncs: number[] = []
399
403
 
400
404
  const rawCodeRelocs: { rawBytes: Uint8Array; ro: number; rs: number }[] = []
401
- const rawDataRelocs: { rawBytes: Uint8Array; ro: number; rs: number }[] = []
405
+ const rawDataRelocs: {
406
+ rawBytes: Uint8Array
407
+ ro: number
408
+ rs: number
409
+ addend: number
410
+ }[] = []
402
411
 
403
412
  while (pos < buf.length) {
404
413
  const id = buf[pos++]!
@@ -586,18 +595,22 @@ function parseWasmObjectBytes(
586
595
  const [, r1] = readULEB(buf, cp)
587
596
  const [cnt, r2] = readULEB(buf, r1)
588
597
  let cur = r2
589
- const target = name === 'reloc.CODE' ? rawCodeRelocs : rawDataRelocs
598
+ const isData = name === 'reloc.DATA'
590
599
  for (let i = 0; i < cnt; i++) {
591
600
  const rStart = cur
592
601
  const rType = buf[cur++]!
593
602
  const [ro, c2] = readULEB(buf, cur)
594
603
  const [rs, c3] = readULEB(buf, c2)
595
604
  cur = c3
605
+ let addend = 0
596
606
  if (HAS_ADDEND.has(rType)) {
597
- const [, c4] = readSLEB(buf, cur)
607
+ const [val, c4] = readSLEB(buf, cur)
608
+ addend = val
598
609
  cur = c4
599
610
  }
600
- target.push({ rawBytes: new Uint8Array(buf.subarray(rStart, cur)), ro, rs })
611
+ const rawBytes = new Uint8Array(buf.subarray(rStart, cur))
612
+ if (isData) rawDataRelocs.push({ rawBytes, ro, rs, addend })
613
+ else rawCodeRelocs.push({ rawBytes, ro, rs })
601
614
  }
602
615
  }
603
616
  }
@@ -765,6 +778,105 @@ function parseWasmObjectBytes(
765
778
  }
766
779
  }
767
780
 
781
+ const conformanceProtocols = new Map<number, string>()
782
+ const objcCategoryTargets: string[] = []
783
+ if (isDist) {
784
+ const relocAt = new Map<string, { rs: number; addend: number }>()
785
+ for (const r of dataRelocList) {
786
+ relocAt.set(`${r.segIdx}:${r.offInSeg}`, { rs: r.rs, addend: r.addend })
787
+ }
788
+ const confSegs = new Set<number>()
789
+ let hasCatsSeg = false
790
+ for (let i = 0; i < segNames.length; i++) {
791
+ if (segNames[i] === 'swift5_protocol_conformances') confSegs.add(i)
792
+ else if (segNames[i] === '__objc_cats') hasCatsSeg = true
793
+ }
794
+ if (confSegs.size > 0) {
795
+ const segOffToMpSym = new Map<string, string>()
796
+ for (let i = 0; i < symKind.length; i++) {
797
+ if (
798
+ symKind[i] === 1 &&
799
+ (symFlags[i]! & 0x10) === 0 &&
800
+ symElemIdx[i]! >= 0 &&
801
+ symNames[i]!.startsWith('$s') &&
802
+ symNames[i]!.endsWith('Mp')
803
+ ) {
804
+ segOffToMpSym.set(`${symElemIdx[i]!}:${symOff[i]!}`, symNames[i]!)
805
+ }
806
+ }
807
+ const dv = new DataView(buf.buffer, buf.byteOffset, buf.byteLength)
808
+ for (const r of dataRelocList) {
809
+ if (!confSegs.has(r.segIdx)) continue
810
+ const mcIdx = r.rs
811
+ if (
812
+ symKind[mcIdx] !== 1 ||
813
+ (symFlags[mcIdx]! & 0x10) !== 0 ||
814
+ symElemIdx[mcIdx]! < 0
815
+ ) {
816
+ continue
817
+ }
818
+ const mcSeg = symElemIdx[mcIdx]!
819
+ const mcOff = symOff[mcIdx]!
820
+ const pr = relocAt.get(`${mcSeg}:${mcOff}`)
821
+ let mp = ''
822
+ if (pr) {
823
+ const raw = symNames[pr.rs]!
824
+ const stripped = raw.startsWith('.Lgot.') ? raw.slice(6) : raw
825
+ if (stripped.startsWith('$s') && stripped.endsWith('Mp')) {
826
+ mp = stripped
827
+ } else if (symKind[pr.rs] === 3 || stripped === '') {
828
+ mp = segOffToMpSym.get(`${mcSeg}:${pr.addend}`) ?? ''
829
+ }
830
+ } else {
831
+ const fileOff = dataStart + sPayloadStarts[mcSeg]! + mcOff
832
+ if (fileOff + 4 <= buf.length) {
833
+ const delta = dv.getInt32(fileOff, true)
834
+ mp =
835
+ segOffToMpSym.get(`${mcSeg}:${mcOff + delta}`) ??
836
+ segOffToMpSym.get(`${mcSeg}:${mcOff + (delta & ~1)}`) ??
837
+ ''
838
+ }
839
+ }
840
+ if (mp) conformanceProtocols.set(mcIdx, mp)
841
+ }
842
+ }
843
+ if (hasCatsSeg) {
844
+ const seenClasses = new Set<string>()
845
+ for (let i = 0; i < symKind.length; i++) {
846
+ if (
847
+ symKind[i] !== 1 ||
848
+ (symFlags[i]! & 0x10) !== 0 ||
849
+ symElemIdx[i]! < 0 ||
850
+ segNames[symElemIdx[i]!] !== '__objc_cats' ||
851
+ !symNames[i]!.startsWith('.objc_category_')
852
+ ) {
853
+ continue
854
+ }
855
+ const cSeg = symElemIdx[i]!
856
+ const cOff = symOff[i]!
857
+ const cr = relocAt.get(`${cSeg}:${cOff + 8}`)
858
+ if (
859
+ cr &&
860
+ symKind[cr.rs] === 1 &&
861
+ (symFlags[cr.rs]! & 0x10) === 0 &&
862
+ symElemIdx[cr.rs]! >= 0
863
+ ) {
864
+ const tSeg = symElemIdx[cr.rs]!
865
+ const strOff = dataStart + sPayloadStarts[tSeg]! + symOff[cr.rs]! + cr.addend
866
+ let sEnd = strOff
867
+ while (sEnd < buf.length && buf[sEnd] !== 0) sEnd++
868
+ if (sEnd > strOff) {
869
+ const clsName = Buffer.from(buf.subarray(strOff, sEnd)).toString('utf8')
870
+ if (!seenClasses.has(clsName)) {
871
+ seenClasses.add(clsName)
872
+ objcCategoryTargets.push(clsName)
873
+ }
874
+ }
875
+ }
876
+ }
877
+ }
878
+ }
879
+
768
880
  return {
769
881
  file,
770
882
  isDist,
@@ -789,6 +901,8 @@ function parseWasmObjectBytes(
789
901
  objcRegisterFuncToTypePrefix,
790
902
  objcClassSymToTypePrefix,
791
903
  typePrefixesInObj,
904
+ conformanceProtocols,
905
+ objcCategoryTargets,
792
906
  rootFuncs,
793
907
  rootSegs,
794
908
  segPayloadRanges: sEnds.map((end, i) => [
@@ -805,7 +919,7 @@ function parseWasmObjectCached(
805
919
  ): ParsedObject {
806
920
  if (!useCache)
807
921
  return parseWasmObjectBytes(file, new Uint8Array(fs.readFileSync(file)), isDist)
808
- const key = `${isDist ? '1' : '0'}:${file}`
922
+ const key = `v2:${isDist ? '1' : '0'}:${file}`
809
923
  const stat = fs.statSync(file, { bigint: true })
810
924
  const stamp = `${stat.ino}:${stat.size}:${stat.mtimeNs}:${stat.ctimeNs}`
811
925
  const hit = parsedObjectCache.get(key)
@@ -1110,6 +1224,30 @@ function getModulePrefix(file: string): string {
1110
1224
  return dash > 0 ? base.slice(0, dash) : base
1111
1225
  }
1112
1226
 
1227
+ function moduleFamily(file: string): string {
1228
+ const mod = getModulePrefix(file)
1229
+ if (
1230
+ mod === 'Foundation' ||
1231
+ mod === 'PeachFoundation' ||
1232
+ mod === 'PeachObjC' ||
1233
+ mod === 'PeachObjCRuntime' ||
1234
+ mod === 'ObjectiveC' ||
1235
+ mod === 'Darwin' ||
1236
+ mod === 'libFoundation' ||
1237
+ mod === 'libObjectiveC' ||
1238
+ mod === 'libDarwin' ||
1239
+ mod === 'libswiftCore'
1240
+ ) {
1241
+ return 'Foundation'
1242
+ }
1243
+ if (mod.startsWith('UIKit')) return 'UIKit'
1244
+ if (mod === 'CoreData' || mod === 'CoreDataConstants') return 'CoreData'
1245
+ if (mod === 'AVFoundation' || mod === 'AVFoundationConstants' || mod === 'AVFAudio') {
1246
+ return 'AVFoundation'
1247
+ }
1248
+ return mod
1249
+ }
1250
+
1113
1251
  // protocol descriptors of the SwiftUI protocols whose conformers declare @State, @Environment and other DynamicProperty fields
1114
1252
  const SWIFTUI_WRAPPER_HOST_PROTOCOLS = new Set(
1115
1253
  [
@@ -1249,13 +1387,7 @@ export function pruneSwiftWasmLink(
1249
1387
  }
1250
1388
  if (t.endsWith('.o')) {
1251
1389
  if (path.resolve(t).startsWith(distDir + path.sep)) {
1252
- if (
1253
- !foundationCategoryObjects.some(
1254
- (name) => path.basename(t) === `Foundation-${name}.o`,
1255
- )
1256
- ) {
1257
- distFiles.push(t)
1258
- }
1390
+ distFiles.push(t)
1259
1391
  if (!inStartLib) eagerFiles.push(t)
1260
1392
  } else if (!inStartLib) {
1261
1393
  if (t.endsWith('/swiftrt.o')) {
@@ -1352,6 +1484,15 @@ export function pruneSwiftWasmLink(
1352
1484
  .filter((f) => f.endsWith('.o') || f.endsWith('.obj'))
1353
1485
  .sort()
1354
1486
  const sdkFiles = allSdkObjFiles.map((f) => path.join(sdkObjsDir, f))
1487
+ const extraSdkObjs = allSdkObjFiles
1488
+ .filter(
1489
+ (f) =>
1490
+ f.startsWith('libDarwin__') ||
1491
+ (f.startsWith('libswiftCore__') && f !== 'libswiftCore__Swift.o') ||
1492
+ (f.startsWith('libswift_Concurrency__') &&
1493
+ f !== 'libswift_Concurrency___Concurrency.o'),
1494
+ )
1495
+ .map((f) => path.join(sdkObjsDir, f))
1355
1496
 
1356
1497
  const distObjs = new Map<string, ParsedObject>()
1357
1498
  const lazyProviders = new Map<string, ParsedObject[]>()
@@ -1431,14 +1572,61 @@ export function pruneSwiftWasmLink(
1431
1572
  workQueue.push({ kind: 'datasym', obj, symIdx })
1432
1573
  }
1433
1574
 
1434
- // foreign conformances wait for their type, including types defined in SDK
1435
- // objects that the linker keeps whole. no value of a dead type can reach a cast.
1575
+ // foreign conformances wait for both their conforming type and protocol to be live.
1576
+ // categories wait for their target ObjC class (and module family when extending Foundation classes).
1436
1577
  const knownTypes = new Set<string>()
1437
- for (const o of distObjs.values())
1578
+ const knownProtocols = new Set<string>()
1579
+ const protoPrefixes = new Set<string>()
1580
+ for (const o of distObjs.values()) {
1438
1581
  for (const t of o.typePrefixesInObj) knownTypes.add(t)
1582
+ for (let i = 0; i < o.symNames.length; i++) {
1583
+ if (
1584
+ o.symKind[i] === 1 &&
1585
+ (o.symFlags[i]! & 0x10) === 0 &&
1586
+ o.symNames[i]!.startsWith('$s') &&
1587
+ o.symNames[i]!.endsWith('Mp')
1588
+ ) {
1589
+ const mp = o.symNames[i]!
1590
+ knownProtocols.add(mp)
1591
+ protoPrefixes.add(mp.slice(0, -2))
1592
+ }
1593
+ }
1594
+ for (const mp of o.conformanceProtocols.values()) {
1595
+ knownProtocols.add(mp)
1596
+ protoPrefixes.add(mp.slice(0, -2))
1597
+ }
1598
+ }
1599
+
1600
+ interface PendingConformance {
1601
+ obj: ParsedObject
1602
+ symIdx: number
1603
+ typeKey: { kind: 'swift'; value: string } | { kind: 'objc'; value: string } | null
1604
+ proto: string | undefined
1605
+ isForeign: boolean
1606
+ activated: boolean
1607
+ }
1608
+
1439
1609
  const globalLiveTypes = new Set<string>()
1440
- const pendingConformances = new Map<string, { obj: ParsedObject; symIdx: number }[]>()
1610
+ const liveProtocols = new Set<string>()
1611
+ const liveObjCClasses = new Set<string>()
1612
+ const liveModuleFamilies = new Set<string>()
1613
+ const categoryOnlyExtracted = new Set<ParsedObject>()
1441
1614
  const eagerDynamicObjects = new Set<ParsedObject>()
1615
+
1616
+ const pendingBySwiftType = new Map<string, PendingConformance[]>()
1617
+ const pendingByObjcClass = new Map<string, PendingConformance[]>()
1618
+ const pendingByProto = new Map<string, PendingConformance[]>()
1619
+
1620
+ interface PendingCategory {
1621
+ obj: ParsedObject
1622
+ cls: string
1623
+ catFamily: string
1624
+ classFamily: string | undefined
1625
+ activated: boolean
1626
+ }
1627
+ const pendingCategoriesByClass = new Map<string, PendingCategory[]>()
1628
+ const pendingCategoriesByFamily = new Map<string, PendingCategory[]>()
1629
+
1442
1630
  const conformingType = (name: string): string | undefined => {
1443
1631
  for (let n = name.length - 1; n > 2; n--) {
1444
1632
  const prefix = name.slice(0, n)
@@ -1447,26 +1635,130 @@ export function pruneSwiftWasmLink(
1447
1635
  return undefined
1448
1636
  }
1449
1637
 
1638
+ const objcClassOfConformance = (name: string): string | undefined => {
1639
+ const m = /^\$sSo(\d+)([A-Za-z0-9_]+)/.exec(name)
1640
+ if (!m) return undefined
1641
+ const len = Number(m[1])
1642
+ const rest = m[2]!
1643
+ if (rest.length <= len || rest[len] !== 'C') return undefined
1644
+ const cls = rest.slice(0, len)
1645
+ return lazyProviders.has(`._OBJC_CLASS_${cls}`) ? cls : undefined
1646
+ }
1647
+
1648
+ const objcClassFamilyOf = (cls: string): string | undefined => {
1649
+ const providers = lazyProviders.get(`._OBJC_CLASS_${cls}`)
1650
+ if (!providers || providers.length === 0) return undefined
1651
+ return moduleFamily(providers[0]!.file)
1652
+ }
1653
+
1654
+ const tryActivateConformance = (entry: PendingConformance) => {
1655
+ if (entry.activated) return
1656
+ const typeReady =
1657
+ entry.typeKey === null
1658
+ ? true
1659
+ : entry.typeKey.kind === 'swift'
1660
+ ? globalLiveTypes.has(entry.typeKey.value)
1661
+ : liveObjCClasses.has(entry.typeKey.value)
1662
+ if (!typeReady) return
1663
+ if (entry.proto !== undefined && !liveProtocols.has(entry.proto)) return
1664
+ if (entry.isForeign) {
1665
+ eagerDynamicObjects.add(entry.obj)
1666
+ extractObject(entry.obj)
1667
+ } else if (!extractedObjs.has(entry.obj)) {
1668
+ return
1669
+ }
1670
+ entry.activated = true
1671
+ activateSymbolInObj(entry.obj, entry.symIdx)
1672
+ }
1673
+
1674
+ const tryActivateCategory = (entry: PendingCategory) => {
1675
+ if (entry.activated || !liveObjCClasses.has(entry.cls)) return
1676
+ const allowed =
1677
+ entry.catFamily === 'Foundation' ||
1678
+ (entry.classFamily !== undefined && entry.classFamily !== 'Foundation') ||
1679
+ liveModuleFamilies.has(entry.catFamily)
1680
+ if (!allowed) return
1681
+ entry.activated = true
1682
+ eagerDynamicObjects.add(entry.obj)
1683
+ extractObject(entry.obj, true)
1684
+ }
1685
+
1686
+ const markModuleFamilyLive = (family: string) => {
1687
+ if (liveModuleFamilies.has(family)) return
1688
+ liveModuleFamilies.add(family)
1689
+ const waiting = pendingCategoriesByFamily.get(family)
1690
+ if (waiting) {
1691
+ pendingCategoriesByFamily.delete(family)
1692
+ for (const entry of waiting) tryActivateCategory(entry)
1693
+ }
1694
+ }
1695
+
1696
+ const markObjCClassLive = (cls: string) => {
1697
+ if (!cls || liveObjCClasses.has(cls)) return
1698
+ liveObjCClasses.add(cls)
1699
+ const confs = pendingByObjcClass.get(cls)
1700
+ if (confs) {
1701
+ for (const entry of confs) tryActivateConformance(entry)
1702
+ }
1703
+ const cats = pendingCategoriesByClass.get(cls)
1704
+ if (cats) {
1705
+ for (const entry of cats) tryActivateCategory(entry)
1706
+ }
1707
+ }
1708
+
1709
+ const markProtocolLive = (mpName: string) => {
1710
+ if (!mpName || liveProtocols.has(mpName)) return
1711
+ liveProtocols.add(mpName)
1712
+ const waiting = pendingByProto.get(mpName)
1713
+ if (waiting) {
1714
+ for (const entry of waiting) tryActivateConformance(entry)
1715
+ }
1716
+ }
1717
+
1718
+ const checkProtocolTrigger = (rawSymName: string | undefined) => {
1719
+ if (!rawSymName) return
1720
+ const sName = rawSymName.startsWith('.Lgot.') ? rawSymName.slice(6) : rawSymName
1721
+ if (knownProtocols.has(sName)) {
1722
+ markProtocolLive(sName)
1723
+ return
1724
+ }
1725
+ if (!sName.startsWith('$s') || sName.endsWith('Mc')) return
1726
+ if (sName.endsWith('TL')) {
1727
+ const mp = `${sName.slice(0, -2)}Mp`
1728
+ if (knownProtocols.has(mp)) {
1729
+ markProtocolLive(mp)
1730
+ return
1731
+ }
1732
+ }
1733
+ if (sName.startsWith('$sS') && sName.length > 4) {
1734
+ const pref = sName.slice(0, 4)
1735
+ if (protoPrefixes.has(pref)) {
1736
+ const ch = sName[4]!
1737
+ if ((ch >= '0' && ch <= '9') || ch === 's' || ch === 'P') {
1738
+ markProtocolLive(`${pref}Mp`)
1739
+ return
1740
+ }
1741
+ }
1742
+ }
1743
+ for (let n = 5; n < sName.length; n++) {
1744
+ if (sName[n] === 'P') {
1745
+ const pref = sName.slice(0, n)
1746
+ if (protoPrefixes.has(pref)) {
1747
+ markProtocolLive(`${pref}Mp`)
1748
+ return
1749
+ }
1750
+ }
1751
+ }
1752
+ }
1753
+
1450
1754
  const markTypeLive = (obj: ParsedObject, pref: string) => {
1451
1755
  const set = liveTypes.get(obj)!
1452
1756
  if (set.has(pref)) return
1453
1757
  set.add(pref)
1454
1758
  globalLiveTypes.add(pref)
1455
- const waiting = pendingConformances.get(pref)
1759
+ const waiting = pendingBySwiftType.get(pref)
1456
1760
  if (waiting) {
1457
- pendingConformances.delete(pref)
1458
- for (const w of waiting) {
1459
- eagerDynamicObjects.add(w.obj)
1460
- extractObject(w.obj)
1461
- activateSymbolInObj(w.obj, w.symIdx)
1462
- }
1463
- }
1464
-
1465
- // a nested type's descriptor reaches its parent by a relative offset the
1466
- // assembler resolves without a relocation when both sit in one segment.
1467
- for (let n = pref.length - 1; n > 2; n--) {
1468
- const outer = pref.slice(0, n)
1469
- if (obj.typePrefixesInObj.has(outer)) markTypeLive(obj, outer)
1761
+ for (const entry of waiting) tryActivateConformance(entry)
1470
1762
  }
1471
1763
 
1472
1764
  // a nested type's descriptor reaches its parent by a relative offset the
@@ -1487,16 +1779,9 @@ export function pruneSwiftWasmLink(
1487
1779
  markFuncLive(obj, lf)
1488
1780
  }
1489
1781
  }
1490
- for (let sIdx = 0; sIdx < obj.segNames.length; sIdx++) {
1491
- if (obj.segNames[sIdx] === 'swift5_protocol_conformances') {
1492
- for (const r of obj.dataRelocList) {
1493
- if (r.segIdx === sIdx) {
1494
- const mcName = obj.symNames[r.rs]!
1495
- if (mcName.startsWith(pref)) {
1496
- activateSymbolInObj(obj, r.rs)
1497
- }
1498
- }
1499
- }
1782
+ for (const [classSym, p] of obj.objcClassSymToTypePrefix) {
1783
+ if (p === pref && classSym.startsWith('._OBJC_CLASS_')) {
1784
+ markObjCClassLive(classSym.slice('._OBJC_CLASS_'.length))
1500
1785
  }
1501
1786
  }
1502
1787
  }
@@ -1504,6 +1789,11 @@ export function pruneSwiftWasmLink(
1504
1789
  const checkTypeTrigger = (obj: ParsedObject, rawSymName: string) => {
1505
1790
  if (!rawSymName) return
1506
1791
  const sName = rawSymName.startsWith('.Lgot.') ? rawSymName.slice(6) : rawSymName
1792
+ if (sName.startsWith('._OBJC_CLASS_')) {
1793
+ markObjCClassLive(sName.slice('._OBJC_CLASS_'.length))
1794
+ } else if (sName.startsWith('._OBJC_METACLASS_')) {
1795
+ markObjCClassLive(sName.slice('._OBJC_METACLASS_'.length))
1796
+ }
1507
1797
  const objcPref = obj.objcClassSymToTypePrefix.get(sName)
1508
1798
  if (objcPref) {
1509
1799
  markTypeLive(obj, objcPref)
@@ -1534,6 +1824,7 @@ export function pruneSwiftWasmLink(
1534
1824
  const activateGlobalName = (name: string) => {
1535
1825
  if (!name) return
1536
1826
  if (!liveGlobalNames.has(name)) liveGlobalNames.add(name)
1827
+ checkProtocolTrigger(name)
1537
1828
  const defs = extractedSymbolDefs.get(name)
1538
1829
  if (defs) {
1539
1830
  for (const d of defs) {
@@ -1549,7 +1840,12 @@ export function pruneSwiftWasmLink(
1549
1840
  const fl = obj.symFlags[symIdx]!
1550
1841
  const isUndef = (fl & 0x10) !== 0
1551
1842
  const isLocal = (fl & 0x03) === 0x02
1843
+ if (obj.isDist && categoryOnlyExtracted.has(obj)) {
1844
+ categoryOnlyExtracted.delete(obj)
1845
+ markModuleFamilyLive(moduleFamily(obj.file))
1846
+ }
1552
1847
  checkTypeTrigger(obj, sName)
1848
+ checkProtocolTrigger(sName)
1553
1849
 
1554
1850
  if (!isUndef && !isLocal && sName && !liveGlobalNames.has(sName)) {
1555
1851
  activateGlobalName(sName)
@@ -1563,14 +1859,26 @@ export function pruneSwiftWasmLink(
1563
1859
  markDataSymLive(obj, pIdx)
1564
1860
  const parentName = obj.symNames[pIdx]!
1565
1861
  checkTypeTrigger(obj, parentName)
1862
+ checkProtocolTrigger(parentName)
1566
1863
  }
1567
1864
  markSegLive(obj, el)
1568
1865
  }
1569
1866
  }
1570
1867
 
1571
- const extractObject = (obj: ParsedObject) => {
1572
- if (extractedObjs.has(obj)) return
1868
+ const extractObject = (obj: ParsedObject, fromObjCCategory = false) => {
1869
+ if (extractedObjs.has(obj)) {
1870
+ if (!fromObjCCategory && categoryOnlyExtracted.has(obj)) {
1871
+ categoryOnlyExtracted.delete(obj)
1872
+ if (obj.isDist) markModuleFamilyLive(moduleFamily(obj.file))
1873
+ }
1874
+ return
1875
+ }
1573
1876
  extractedObjs.add(obj)
1877
+ if (fromObjCCategory && obj.isDist) {
1878
+ categoryOnlyExtracted.add(obj)
1879
+ } else if (obj.isDist) {
1880
+ markModuleFamilyLive(moduleFamily(obj.file))
1881
+ }
1574
1882
  liveFuncs.set(obj, new Uint8Array(obj.numFuncs))
1575
1883
  liveSegs.set(obj, new Uint8Array(obj.numSegments))
1576
1884
  liveDataSyms.set(obj, new Set<number>())
@@ -1615,6 +1923,13 @@ export function pruneSwiftWasmLink(
1615
1923
  activateSymbolInObj(obj, classSym)
1616
1924
  }
1617
1925
 
1926
+ if (!obj.isDist) {
1927
+ for (let i = 0; i < obj.symNames.length; i++) {
1928
+ checkTypeTrigger(obj, obj.symNames[i]!)
1929
+ checkProtocolTrigger(obj.symNames[i])
1930
+ }
1931
+ }
1932
+
1618
1933
  // app classes can be addressed by module-qualified names in plists or
1619
1934
  // NSClassFromString even when their Objective-C names are mangled.
1620
1935
  if (obj.isDist) {
@@ -1633,55 +1948,98 @@ export function pruneSwiftWasmLink(
1633
1948
  }
1634
1949
 
1635
1950
  if (obj.isDist) {
1636
- for (let sIdx = 0; sIdx < obj.segNames.length; sIdx++) {
1637
- if (obj.segNames[sIdx] === 'swift5_protocol_conformances') {
1638
- for (const r of obj.dataRelocList) {
1639
- if (r.segIdx === sIdx) {
1640
- const mcName = obj.symNames[r.rs]!
1641
- let matchesLocalType = false
1642
- for (const pref of obj.typePrefixesInObj) {
1643
- if (mcName.startsWith(pref)) {
1644
- matchesLocalType = true
1645
- break
1646
- }
1647
- }
1648
- if (!matchesLocalType) {
1649
- const type = conformingType(mcName)
1650
- if (type === undefined || globalLiveTypes.has(type))
1651
- activateSymbolInObj(obj, r.rs)
1652
- }
1653
- }
1654
- }
1655
- }
1951
+ for (const entries of pendingByObj.get(obj) ?? []) {
1952
+ tryActivateConformance(entries)
1656
1953
  }
1657
1954
  }
1658
1955
  }
1659
1956
 
1660
- // a dynamic cast reaches foreign conformances without referencing a symbol
1661
- // in their object. index them before extracting any provider so a live type
1662
- // can extract its extensions as well as its own metadata.
1957
+ // index every distribution protocol conformance (type + protocol two-sided gate)
1958
+ // and every ObjC category (target class + module family gate) before extracting roots.
1959
+ const pendingByObj = new Map<ParsedObject, PendingConformance[]>()
1663
1960
  for (const obj of distObjs.values()) {
1664
1961
  if (!obj.isDist) continue
1665
1962
  for (const r of obj.dataRelocList) {
1666
1963
  if (obj.segNames[r.segIdx] !== 'swift5_protocol_conformances') continue
1667
- const type = conformingType(obj.symNames[r.rs]!)
1668
- if (type === undefined || obj.typePrefixesInObj.has(type)) continue
1669
- let list = pendingConformances.get(type)
1670
- if (!list) pendingConformances.set(type, (list = []))
1671
- list.push({ obj, symIdx: r.rs })
1964
+ const mcIdx = r.rs
1965
+ const mcName = obj.symNames[mcIdx]!
1966
+ const swiftType = conformingType(mcName)
1967
+ const objcType =
1968
+ swiftType === undefined ? objcClassOfConformance(mcName) : undefined
1969
+ const typeKey: PendingConformance['typeKey'] =
1970
+ swiftType !== undefined
1971
+ ? { kind: 'swift', value: swiftType }
1972
+ : objcType !== undefined
1973
+ ? { kind: 'objc', value: objcType }
1974
+ : null
1975
+ const proto = obj.conformanceProtocols.get(mcIdx)
1976
+ const isForeign =
1977
+ swiftType !== undefined
1978
+ ? !obj.typePrefixesInObj.has(swiftType)
1979
+ : objcType !== undefined
1980
+ ? !obj.symNames.some(
1981
+ (n, i) =>
1982
+ n === `._OBJC_CLASS_${objcType}` && (obj.symFlags[i]! & 0x10) === 0,
1983
+ )
1984
+ : false
1985
+ const entry: PendingConformance = {
1986
+ obj,
1987
+ symIdx: mcIdx,
1988
+ typeKey,
1989
+ proto,
1990
+ isForeign,
1991
+ activated: false,
1992
+ }
1993
+ let objList = pendingByObj.get(obj)
1994
+ if (!objList) pendingByObj.set(obj, (objList = []))
1995
+ objList.push(entry)
1996
+ if (swiftType !== undefined) {
1997
+ let list = pendingBySwiftType.get(swiftType)
1998
+ if (!list) pendingBySwiftType.set(swiftType, (list = []))
1999
+ list.push(entry)
2000
+ } else if (objcType !== undefined) {
2001
+ let list = pendingByObjcClass.get(objcType)
2002
+ if (!list) pendingByObjcClass.set(objcType, (list = []))
2003
+ list.push(entry)
2004
+ }
2005
+ if (proto !== undefined) {
2006
+ let list = pendingByProto.get(proto)
2007
+ if (!list) pendingByProto.set(proto, (list = []))
2008
+ list.push(entry)
2009
+ }
2010
+ }
2011
+ if (obj.objcCategoryTargets.length > 0) {
2012
+ const catFamily = moduleFamily(obj.file)
2013
+ for (const cls of obj.objcCategoryTargets) {
2014
+ const classFamily = objcClassFamilyOf(cls)
2015
+ const entry: PendingCategory = {
2016
+ obj,
2017
+ cls,
2018
+ catFamily,
2019
+ classFamily,
2020
+ activated: false,
2021
+ }
2022
+ let byCls = pendingCategoriesByClass.get(cls)
2023
+ if (!byCls) pendingCategoriesByClass.set(cls, (byCls = []))
2024
+ byCls.push(entry)
2025
+ if (
2026
+ catFamily !== 'Foundation' &&
2027
+ (classFamily === undefined || classFamily === 'Foundation')
2028
+ ) {
2029
+ let byFam = pendingCategoriesByFamily.get(catFamily)
2030
+ if (!byFam) pendingCategoriesByFamily.set(catFamily, (byFam = []))
2031
+ byFam.push(entry)
2032
+ }
2033
+ }
1672
2034
  }
1673
2035
  }
1674
2036
 
1675
- let hasClangObjCEager = false
1676
2037
  for (const f of eagerFiles) {
1677
2038
  const isDist = f.startsWith(distDir)
1678
2039
  // the app's own objects link whole, as ld64 keeps every swift type record:
1679
2040
  // storyboards, data models and NSClassFromString name app classes by string.
1680
2041
  const obj = distObjs.get(f) ?? parseWasmObjectCached(f, isDist, false)
1681
2042
  extractObject(obj)
1682
- if (!isDist && !hasClangObjCEager && obj.symNames.includes('__objc_load')) {
1683
- hasClangObjCEager = true
1684
- }
1685
2043
  if (isDist) {
1686
2044
  const base = path.basename(f)
1687
2045
  if (
@@ -1717,20 +2075,6 @@ export function pruneSwiftWasmLink(
1717
2075
  }
1718
2076
  }
1719
2077
 
1720
- const eagerSdkCategoryFiles: string[] = []
1721
- if (hasClangObjCEager) {
1722
- for (const name of foundationCategoryObjects.map(
1723
- (name) => `libFoundation__${name}.objc.o`,
1724
- )) {
1725
- const sdkFile = path.join(sdkObjsDir, name)
1726
- const obj = distObjs.get(sdkFile)
1727
- if (obj) {
1728
- extractObject(obj)
1729
- eagerSdkCategoryFiles.push(sdkFile)
1730
- }
1731
- }
1732
- }
1733
-
1734
2078
  for (const symName of ['_initialize', '__main_argc_argv', 'main']) {
1735
2079
  const def = definedGlobalSyms.get(symName)
1736
2080
  if (def) activateSymbolInObj(def.obj, def.symIdx)
@@ -1794,6 +2138,9 @@ export function pruneSwiftWasmLink(
1794
2138
 
1795
2139
  for (const symIdx of relocs) {
1796
2140
  if (obj.isDist && dropsReflection(obj, obj.symNames[symIdx])) continue
2141
+ const symName = obj.symNames[symIdx]!
2142
+ checkTypeTrigger(obj, symName)
2143
+ checkProtocolTrigger(symName)
1797
2144
  const fl = obj.symFlags[symIdx]!
1798
2145
  const isUndef = (fl & 0x10) !== 0
1799
2146
  const isLocal = (fl & 0x03) === 0x02
@@ -2196,28 +2543,12 @@ export function pruneSwiftWasmLink(
2196
2543
  prunedMap.set(o.file, outPath)
2197
2544
  }
2198
2545
 
2199
- const extraSdkObjs = allSdkObjFiles
2200
- .filter(
2201
- (f) =>
2202
- f.startsWith('libDarwin__') ||
2203
- (f.startsWith('libswiftCore__') && f !== 'libswiftCore__Swift.o') ||
2204
- (f.startsWith('libswift_Concurrency__') &&
2205
- f !== 'libswift_Concurrency___Concurrency.o'),
2206
- )
2207
- .map((f) => path.join(sdkObjsDir, f))
2208
-
2209
2546
  const outTokens: string[] = []
2210
2547
  const emittedPruned = new Set<string>()
2211
2548
  inStartLib = false
2212
2549
  for (const t of tokens) {
2213
2550
  if (t === '--start-lib') {
2214
2551
  inStartLib = true
2215
- for (const ef of eagerSdkCategoryFiles) {
2216
- if (!emittedPruned.has(ef) && prunedMap.has(ef)) {
2217
- outTokens.push(prunedMap.get(ef)!)
2218
- emittedPruned.add(ef)
2219
- }
2220
- }
2221
2552
  // these providers were reached through runtime type lookup, so no
2222
2553
  // undefined symbol would extract them from the following archive region.
2223
2554
  for (const obj of eagerDynamicObjects) {