peachsim 0.1.2777 → 0.1.2779

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 (154) hide show
  1. package/dist-lib/agent-daemon-client.cjs +1 -1
  2. package/dist-lib/agent-events.cjs +1 -1
  3. package/dist-lib/attached-projects.cjs +1 -1
  4. package/dist-lib/auth/shared-session.cjs +1 -1
  5. package/dist-lib/backend-origin.cjs +1 -1
  6. package/dist-lib/beta.cjs +1 -1
  7. package/dist-lib/beta.mjs +2 -2
  8. package/dist-lib/bridge-constants.cjs +1 -1
  9. package/dist-lib/bridge-contract-input.cjs +1 -1
  10. package/dist-lib/bridge-contract-input.mjs +5 -5
  11. package/dist-lib/bridge-contract.cjs +1 -1
  12. package/dist-lib/bridge-contract.mjs +3 -3
  13. package/dist-lib/bundle-channel.cjs +669 -840
  14. package/dist-lib/capture-contract.cjs +1 -1
  15. package/dist-lib/capture-contract.mjs +3 -3
  16. package/dist-lib/cloud-contract.cjs +1 -1
  17. package/dist-lib/cloud-contract.mjs +3 -3
  18. package/dist-lib/cloud-test-args.cjs +1 -1
  19. package/dist-lib/cloud-test-args.mjs +2 -2
  20. package/dist-lib/cloud.cjs +1 -1
  21. package/dist-lib/cloud.mjs +7 -7
  22. package/dist-lib/config.cjs +1 -1
  23. package/dist-lib/config.mjs +5 -5
  24. package/dist-lib/detox/index.cjs +1 -1
  25. package/dist-lib/dev-bundle-resolution.cjs +1 -1
  26. package/dist-lib/esm-chunks/{acorn-M5O633NZ.mjs → acorn-SCZ56W4Q.mjs} +3 -3
  27. package/dist-lib/esm-chunks/{chunk-NJHTAVEI.mjs → chunk-6BNEOOBT.mjs} +124 -18
  28. package/dist-lib/esm-chunks/{chunk-LB2X5KYV.mjs → chunk-7VWBLM73.mjs} +1 -1
  29. package/dist-lib/esm-chunks/{chunk-V2I633CU.mjs → chunk-DX5V3733.mjs} +4 -4
  30. package/dist-lib/esm-chunks/{chunk-THHP5XKD.mjs → chunk-E56EZ3QT.mjs} +3 -3
  31. package/dist-lib/esm-chunks/{chunk-TRSHEQVJ.mjs → chunk-FNQXQNQK.mjs} +1 -1
  32. package/dist-lib/esm-chunks/{chunk-WSHBJUCP.mjs → chunk-H2V3ZZTO.mjs} +1 -1
  33. package/dist-lib/esm-chunks/{chunk-OVCIWUV2.mjs → chunk-H6JAFCAZ.mjs} +1 -1
  34. package/dist-lib/esm-chunks/{chunk-ZKPBT6FD.mjs → chunk-HMFIO57I.mjs} +1 -1
  35. package/dist-lib/esm-chunks/{chunk-DPL3I4WY.mjs → chunk-HWK2V6R4.mjs} +1 -1
  36. package/dist-lib/esm-chunks/{chunk-OSESKVWT.mjs → chunk-KAMO4C4X.mjs} +1 -1
  37. package/dist-lib/esm-chunks/{chunk-TONCJKKR.mjs → chunk-KFDP4372.mjs} +1 -1
  38. package/dist-lib/esm-chunks/{chunk-FHYWKCOZ.mjs → chunk-KOULWLTF.mjs} +2 -2
  39. package/dist-lib/esm-chunks/{chunk-Q42OLP7O.mjs → chunk-LVEDBGWX.mjs} +1 -1
  40. package/dist-lib/esm-chunks/{chunk-RX3WDTYI.mjs → chunk-MJXJYVF5.mjs} +1 -1
  41. package/dist-lib/esm-chunks/{chunk-GISUEYXO.mjs → chunk-MK4VAM4G.mjs} +1 -1
  42. package/dist-lib/esm-chunks/{chunk-AGWZKA24.mjs → chunk-Q6XMYYNV.mjs} +168 -421
  43. package/dist-lib/esm-chunks/{chunk-KCSUAVJQ.mjs → chunk-SFTGQN5D.mjs} +1 -1
  44. package/dist-lib/esm-chunks/{chunk-Q53QPYEU.mjs → chunk-SM5MJSQP.mjs} +1 -1
  45. package/dist-lib/esm-chunks/{chunk-3UJO4NG3.mjs → chunk-SNMGOZXQ.mjs} +1 -1
  46. package/dist-lib/esm-chunks/{chunk-OLCLLGZF.mjs → chunk-SZI5IZ76.mjs} +5 -5
  47. package/dist-lib/esm-chunks/{chunk-FB27T2JY.mjs → chunk-T7DTXCV5.mjs} +1 -1
  48. package/dist-lib/esm-chunks/{chunk-IPX66HMP.mjs → chunk-UBM772FD.mjs} +1 -1
  49. package/dist-lib/esm-chunks/{chunk-GMN43CNC.mjs → chunk-VJSHIZ7J.mjs} +1 -1
  50. package/dist-lib/esm-chunks/{chunk-GSTVX33B.mjs → chunk-Z42YJEC4.mjs} +36 -14
  51. package/dist-lib/esm-chunks/{metro-layout-parser-PFRFHGDY.mjs → metro-layout-parser-7S5M3EG6.mjs} +3 -3
  52. package/dist-lib/esm-chunks/{swift-package-prebuilt-NUAEDMSQ.mjs → swift-package-prebuilt-MJSFHIOP.mjs} +12 -12
  53. package/dist-lib/esm-chunks/{swift-sdk-app-module-P3BARE2W.mjs → swift-sdk-app-module-ZIN3LFLS.mjs} +47 -69
  54. package/dist-lib/esm-chunks/{swift-sdk-image-Y6XT7KI4.mjs → swift-sdk-image-MIWPBZEN.mjs} +16 -16
  55. package/dist-lib/esm-chunks/{swift-sdk-image-producer-DYR47O6H.mjs → swift-sdk-image-producer-OTH543HF.mjs} +5 -5
  56. package/dist-lib/expo-swift-bundle.cjs +592 -763
  57. package/dist-lib/expo-swift-metadata.cjs +1 -1
  58. package/dist-lib/expo-swift-metadata.mjs +7 -7
  59. package/dist-lib/home-paths.cjs +1 -1
  60. package/dist-lib/host/bridge-host.cjs +735 -901
  61. package/dist-lib/host/fetch-proxy-handler.cjs +117 -93
  62. package/dist-lib/host/fetch-proxy-overrides.cjs +1 -1
  63. package/dist-lib/host/fetch-proxy-overrides.mjs +2 -2
  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 +1 -1
  67. package/dist-lib/kotlin-host.cjs +1 -1
  68. package/dist-lib/menu.cjs +1 -1
  69. package/dist-lib/menu.mjs +3 -3
  70. package/dist-lib/metro-fingerprint-registry.cjs +1 -1
  71. package/dist-lib/metro-fingerprint-registry.mjs +3 -3
  72. package/dist-lib/metro-production-bundle.cjs +1 -1
  73. package/dist-lib/metro-production-bundle.mjs +4 -4
  74. package/dist-lib/native-app-identity-project.cjs +20 -15
  75. package/dist-lib/native-app-identity-project.mjs +27 -32
  76. package/dist-lib/native-app-identity.cjs +1 -1
  77. package/dist-lib/native-app-identity.mjs +6 -6
  78. package/dist-lib/native-firebase-metadata.cjs +1 -1
  79. package/dist-lib/native-firebase-metadata.mjs +3 -3
  80. package/dist-lib/pdfium-raster.cjs +1 -1
  81. package/dist-lib/pdfium-raster.mjs +2 -2
  82. package/dist-lib/plist.cjs +1 -1
  83. package/dist-lib/plist.mjs +3 -3
  84. package/dist-lib/profiles.cjs +1 -1
  85. package/dist-lib/public-brand.cjs +1 -1
  86. package/dist-lib/public-origins-lib-esm.json +87 -86
  87. package/dist-lib/public-origins-lib.json +77 -70
  88. package/dist-lib/react-native-host-modules.cjs +1 -1
  89. package/dist-lib/react-native-host-modules.mjs +2 -2
  90. package/dist-lib/reap-browser.cjs +1 -1
  91. package/dist-lib/scripts/dev-server-scanner.cjs +1 -1
  92. package/dist-lib/sdk.cjs +1 -1
  93. package/dist-lib/sdk.mjs +8 -8
  94. package/dist-lib/skills.cjs +774 -940
  95. package/dist-lib/swift-compile.cjs +376 -525
  96. package/dist-lib/swift-compile.mjs +12 -12
  97. package/dist-lib/swift-package.cjs +1 -1
  98. package/dist-lib/swift-package.mjs +3 -3
  99. package/dist-lib/swift-submit.cjs +1 -1
  100. package/dist-lib/swift-submit.mjs +6 -6
  101. package/dist-lib/swift-svg-raster.cjs +1 -1
  102. package/dist-lib/swift-svg-raster.mjs +2 -2
  103. package/dist-lib/swift-toolchain.cjs +1 -1
  104. package/dist-lib/swift.cjs +1 -1
  105. package/dist-lib/vendor/@peculiar/x509.cjs +1 -1
  106. package/dist-lib/vendor/@ruby/prism/src/nodes.js.cjs +1 -1
  107. package/dist-lib/vendor/@ruby/prism/src/parsePrism.js.cjs +1 -1
  108. package/dist-lib/vendor/acorn.cjs +1 -1
  109. package/dist-lib/vendor/magic-string.cjs +1 -1
  110. package/dist-lib/vendor/stream-json.cjs +1 -1
  111. package/dist-lib/vendor/sucrase/dist/index.js.cjs +1 -1
  112. package/dist-lib/vendor/sucrase/dist/parser/index.js.cjs +1 -1
  113. package/dist-lib/vendor/sucrase/dist/parser/tokenizer/index.js.cjs +1 -1
  114. package/dist-lib/vendor/sucrase/dist/parser/tokenizer/types.js.cjs +1 -1
  115. package/dist-lib/vendor/sucrase.cjs +1 -1
  116. package/dist-lib/vendor/terser.cjs +1 -1
  117. package/dist-lib/vendor/valibot.cjs +1 -1
  118. package/dist-lib/vendor/ws.cjs +1 -1
  119. package/dist-lib/vendor/zod.cjs +1 -1
  120. package/dist-lib/worklet-executable.cjs +1 -1
  121. package/dist-lib/worklet-executable.mjs +3 -3
  122. package/dist-types/packages/peach-engine/core-abi/web/sceneClientContract.d.ts +0 -6
  123. package/dist-types/packages/peach-engine/src/components/native-ui/webview-frame-bridge.d.ts +0 -3
  124. package/dist-types/packages/peach-engine/src/render-worker/worklet-runtime.d.ts +15 -2
  125. package/dist-types/packages/peach-engine/src/render-worker/worklet-wire.d.ts +10 -1
  126. package/dist-types/packages/peach-engine/src/webview-automation-host.d.ts +0 -4
  127. package/package.json +1 -1
  128. package/src/bazel-project.ts +2 -6
  129. package/src/build-cache.ts +3 -2
  130. package/src/sha256.ts +8 -0
  131. package/src/swift-compiler-digest.ts +1 -0
  132. package/src/swift-coverage/indexing.ts +3 -4
  133. package/src/swift-coverage/package.ts +1 -4
  134. package/src/swift-coverage/reader.ts +1 -4
  135. package/src/swift-coverage/source.ts +1 -4
  136. package/src/swift-coverage/xcode-assets.ts +1 -4
  137. package/src/swift-coverage/xcode-bridging.ts +1 -4
  138. package/src/swift-coverage/xcode-packages.ts +1 -4
  139. package/src/swift-coverage/xcode-strings.ts +1 -4
  140. package/src/swift-coverage/xcode.ts +1 -4
  141. package/src/swift-guest-dyld-functions.ts +1 -11
  142. package/src/swift-guest-dyld-table.ts +3 -15
  143. package/src/swift-guest-image-ledger.ts +9 -35
  144. package/src/swift-guest-source-inputs.ts +8 -37
  145. package/src/swift-icu.ts +15 -28
  146. package/src/swift-link-roots.ts +4 -8
  147. package/src/swift-objc-compile.ts +2 -7
  148. package/src/swift-sdk-app-module.ts +5 -27
  149. package/src/swift-sdk-image-wasm.ts +12 -41
  150. package/src/swift-sdk-image.ts +50 -12
  151. package/src/swift-shared-sdk.ts +1 -13
  152. package/src/swift-wasm-archive.ts +6 -11
  153. package/src/swift-wasm-prune.ts +25 -109
  154. package/src/wasm-binary-sections.ts +61 -7
@@ -1,7 +1,7 @@
1
1
  import { execFileSync } from 'node:child_process'
2
- import { createHash } from 'node:crypto'
3
2
  import { mkdirSync, realpathSync, writeFileSync } from 'node:fs'
4
3
  import { basename, isAbsolute, join, relative, resolve, sep } from 'node:path'
4
+ import { sha256Hex as sha256 } from '../sha256'
5
5
  import { swiftImplicitResourcePaths } from '../swift-package'
6
6
  import {
7
7
  swiftPackageDescription,
@@ -16,9 +16,6 @@ import { generatePackageAssetSymbols } from './xcode-assets'
16
16
  import type { readXcodeAppTarget } from '../xcode-project'
17
17
  import type { SwiftSourceLeg } from './types'
18
18
 
19
- const sha256 = (bytes: string | Buffer) =>
20
- createHash('sha256').update(bytes).digest('hex')
21
-
22
19
  // the compilation conditions swiftpm defines for a target in a build configuration.
23
20
  export const swiftPackageConditions = (
24
21
  target: SwiftManifestTarget,
@@ -1,12 +1,9 @@
1
- import { createHash } from 'node:crypto'
2
1
  import { mkdirSync, readFileSync, writeFileSync } from 'node:fs'
3
2
  import { basename, join } from 'node:path'
4
3
  import { isRecord } from '../is-record'
4
+ import { sha256Hex as sha256 } from '../sha256'
5
5
  import { swiftCoverageProvenance } from './provenance'
6
6
 
7
- const sha256 = (bytes: string | Buffer) =>
8
- createHash('sha256').update(bytes).digest('hex')
9
-
10
7
  export type SwiftStringPlan = { catalogs: { path: string; sha256: string }[] }
11
8
 
12
9
  // Xcode runs `xcstringstool generate-symbols --language swift` once per catalog
@@ -1,7 +1,7 @@
1
- import { createHash } from 'node:crypto'
2
1
  import { existsSync, mkdtempSync, realpathSync, rmSync } from 'node:fs'
3
2
  import { tmpdir } from 'node:os'
4
3
  import { basename, dirname, isAbsolute, join, relative, sep } from 'node:path'
4
+ import { sha256Hex as sha256 } from '../sha256'
5
5
  import { readPackageFile } from '../swift-source-file'
6
6
  import {
7
7
  isDict,
@@ -32,9 +32,6 @@ export interface SwiftXcodeSourceInput extends Omit<
32
32
  configuration?: string
33
33
  }
34
34
 
35
- const sha256 = (bytes: string | Buffer) =>
36
- createHash('sha256').update(bytes).digest('hex')
37
-
38
35
  // every file the project reader consulted besides sources: the snapshot must hold them all.
39
36
  export const xcodeMetadataFiles = (
40
37
  app: Pick<
@@ -1,16 +1,6 @@
1
1
  import { NATIVE_DYLD_PLACE_EXPORT } from '../../peach-kit/src/nativeWasmContract'
2
2
  import { wasmReader, wasmSections, type WasmSection } from './swift-sdk-image-wasm'
3
-
4
- const uleb = (value: number) => {
5
- const out: number[] = []
6
- do {
7
- let byte = value % 128
8
- value = Math.floor(value / 128)
9
- if (value) byte |= 128
10
- out.push(byte)
11
- } while (value)
12
- return out
13
- }
3
+ import { encodeWasmNumber as uleb } from './wasm-binary-sections'
14
4
 
15
5
  // wasm requires known sections in this order; custom sections may sit anywhere.
16
6
  const SECTION_ORDER = [1, 2, 3, 4, 5, 13, 6, 7, 8, 9, 12, 10, 11]
@@ -6,8 +6,10 @@ import {
6
6
  nativeDyldTablePath,
7
7
  readNativeDyldTableIndex,
8
8
  } from '../../peach-kit/src/nativeDyldTableContract'
9
+ import { sha256Hex } from './sha256'
9
10
  import { GUEST_DYLD_VALUES_SECTION } from './swift-guest-image-ledger'
10
11
  import { takeGuestStartupData } from './swift-guest-linked-spans'
12
+ import { encodeWasmNumber } from './wasm-binary-sections'
11
13
  import {
12
14
  dropWasmCustomSection,
13
15
  readWasmCustomSection,
@@ -20,12 +22,6 @@ function sha256Buffer(bytes: Buffer): Buffer {
20
22
  return createHash('sha256').update(bytes).digest()
21
23
  }
22
24
 
23
- function sha256Hex(bytes: Buffer): string {
24
- if (typeof Bun !== 'undefined')
25
- return new Bun.CryptoHasher('sha256').update(bytes).digest('hex')
26
- return createHash('sha256').update(bytes).digest('hex')
27
- }
28
-
29
25
  /** raw bytes per pack: one range read answers a lookup without fetching its neighbors. */
30
26
  const PACK_BYTES = 64 * 1024
31
27
 
@@ -36,14 +32,6 @@ export interface GuestDyldRow {
36
32
  readonly value: bigint
37
33
  }
38
34
 
39
- function uleb(value: number): number[] {
40
- const bytes: number[] = []
41
- for (let rest = value; ; rest = Math.floor(rest / 128)) {
42
- if (rest < 128) return [...bytes, rest]
43
- bytes.push((rest % 128) | 128)
44
- }
45
- }
46
-
47
35
  /** the index and packs sections for a dlsym table (peach-kit nativeDyldTableContract). */
48
36
  export function encodeGuestDyldTable(rows: readonly GuestDyldRow[]): {
49
37
  index: Buffer
@@ -62,7 +50,7 @@ export function encodeGuestDyldTable(rows: readonly GuestDyldRow[]): {
62
50
  value.writeBigUInt64LE(row.value)
63
51
  const bytes = Buffer.concat([
64
52
  row.bytes,
65
- Buffer.from([0, ...uleb(row.image), row.kind]),
53
+ Buffer.from([0, ...encodeWasmNumber(row.image), row.kind]),
66
54
  value,
67
55
  ])
68
56
  if (!current.length) firsts.push(row.bytes)
@@ -10,6 +10,7 @@ import {
10
10
  OBJC_LOAD_FUNCTION,
11
11
  SWIFT_SDK_FUNCTION_ADDRESS_PREFIX,
12
12
  } from './swiftSdkLinkContract'
13
+ import { encodeWasmNumber, readULEB as readNumber } from './wasm-binary-sections'
13
14
 
14
15
  export interface GuestImageObject {
15
16
  readonly path: string
@@ -102,16 +103,13 @@ class ByteWriter {
102
103
  this.buf[this.pos++] = value
103
104
  }
104
105
  uleb(value: number) {
105
- this.ensure(10)
106
- let pos = this.pos
107
- const buf = this.buf
108
- do {
109
- let byte = value % 128
110
- value = Math.floor(value / 128)
111
- if (value) byte |= 128
112
- buf[pos++] = byte
113
- } while (value)
114
- this.pos = pos
106
+ if (value < 128) {
107
+ this.byte(value)
108
+ return
109
+ }
110
+ const encoded = encodeWasmNumber(value)
111
+ this.ensure(encoded.length)
112
+ for (let i = 0; i < encoded.length; i++) this.buf[this.pos++] = encoded[i]!
115
113
  }
116
114
  bytes(source: Uint8Array, start = 0, end = source.length) {
117
115
  const len = end - start
@@ -134,32 +132,8 @@ class ByteWriter {
134
132
  }
135
133
  }
136
134
 
137
- function readNumber(bytes: Uint8Array, position: number): [number, number] {
138
- if (position < bytes.length) {
139
- const first = bytes[position]!
140
- if (first < 128) return [first, position + 1]
141
- }
142
- let value = 0
143
- let shift = 0
144
- for (;;) {
145
- if (position >= bytes.length || shift > 63) throw new Error('invalid wasm integer')
146
- const byte = bytes[position++]!
147
- value += (byte & 127) * 2 ** shift
148
- if (!(byte & 128)) return [value, position]
149
- shift += 7
150
- }
151
- }
152
-
153
135
  function encodeNumber(value: number): Buffer {
154
- if (value < 128) return Buffer.from([value])
155
- const bytes: number[] = []
156
- do {
157
- let byte = value % 128
158
- value = Math.floor(value / 128)
159
- if (value) byte |= 128
160
- bytes.push(byte)
161
- } while (value)
162
- return Buffer.from(bytes)
136
+ return Buffer.from(encodeWasmNumber(value))
163
137
  }
164
138
 
165
139
  function readString(bytes: Uint8Array, position: number): [string, number] {
@@ -1,5 +1,4 @@
1
1
  import { execFileSync } from 'node:child_process'
2
- import { createHash } from 'node:crypto'
3
2
  import {
4
3
  existsSync,
5
4
  mkdirSync,
@@ -11,6 +10,7 @@ import {
11
10
  } from 'node:fs'
12
11
  import path from 'node:path'
13
12
  import { PEACH_FORKED_SCRIPT_ENV } from './forked-script-contract'
13
+ import { sha256Hex } from './sha256'
14
14
  import {
15
15
  libraryArchiveTarget,
16
16
  prebuiltSourceObjectPath,
@@ -18,6 +18,7 @@ import {
18
18
  } from './swift-guest-source-plan'
19
19
  import { frameworkRuntimeProductId } from './swift-runtime-products'
20
20
  import { readArMembers } from './swift-wasm-prune'
21
+ import { readULEB } from './wasm-binary-sections'
21
22
  import type { SwiftSourceLinkGraph } from './swiftContract'
22
23
  import type {
23
24
  GuestFrameworkProvider,
@@ -115,12 +116,6 @@ function clangBinaryStamp(file: string): string {
115
116
  return [file, stat.size, stat.mtimeNs].join(':')
116
117
  }
117
118
 
118
- function sha256Hex(bytes: Buffer | string): string {
119
- if (typeof Bun !== 'undefined')
120
- return new Bun.CryptoHasher('sha256').update(bytes).digest('hex')
121
- return createHash('sha256').update(bytes).digest('hex')
122
- }
123
-
124
119
  function clangWrapperStamp(file: string): string {
125
120
  return sha256Hex(readFileSync(file))
126
121
  }
@@ -502,30 +497,12 @@ function wasmAutolinkEntries(bytes: Uint8Array, out: string[]): boolean {
502
497
  const len = bytes.length
503
498
  while (pos < len) {
504
499
  const id = bytes[pos++]!
505
- let sz = bytes[pos++]!
506
- if (sz >= 128) {
507
- sz &= 0x7f
508
- let shift = 7
509
- while (true) {
510
- const b = bytes[pos++]!
511
- sz |= (b & 0x7f) << shift
512
- if (b < 128) break
513
- shift += 7
514
- }
515
- }
500
+ let sz: number
501
+ ;[sz, pos] = readULEB(bytes, pos, len)
516
502
  const end = pos + sz
517
503
  if (id === 0) {
518
- let nLen = bytes[pos++]!
519
- if (nLen >= 128) {
520
- nLen &= 0x7f
521
- let shift = 7
522
- while (true) {
523
- const b = bytes[pos++]!
524
- nLen |= (b & 0x7f) << shift
525
- if (b < 128) break
526
- shift += 7
527
- }
528
- }
504
+ let nLen: number
505
+ ;[nLen, pos] = readULEB(bytes, pos, end)
529
506
  if (
530
507
  nLen === 24 &&
531
508
  raw.toString('ascii', pos, pos + 24) === '.swift1_autolink_entries'
@@ -643,10 +620,7 @@ export function sourceGuestRuntimeLinks(options: {
643
620
  const file = path.resolve(directory, alias.moduleMap)
644
621
  if (!file.startsWith(directory + path.sep))
645
622
  throw new Error(`invalid framework module map ${alias.moduleMap}`)
646
- if (
647
- createHash('sha256').update(readFileSync(file)).digest('hex') !==
648
- alias.moduleMapSha256
649
- )
623
+ if (sha256Hex(readFileSync(file)) !== alias.moduleMapSha256)
650
624
  throw new Error(`framework module map changed ${alias.moduleMap}`)
651
625
  sdkAliases.set(alias.name, {
652
626
  product: product.id,
@@ -712,10 +686,7 @@ export function sourceGuestRuntimeLinks(options: {
712
686
  const map = path.resolve(root, alias.moduleMap)
713
687
  if (!map.startsWith(root + path.sep))
714
688
  throw new Error(`invalid framework module map ${alias.moduleMap}`)
715
- if (
716
- createHash('sha256').update(readFileSync(map)).digest('hex') !==
717
- alias.moduleMapSha256
718
- )
689
+ if (sha256Hex(readFileSync(map)) !== alias.moduleMapSha256)
719
690
  throw new Error(`framework module map changed ${alias.moduleMap}`)
720
691
  frameworkAliases.set(alias.name, { kind: 'package', targets: closure(alias.targets) })
721
692
  }
package/src/swift-icu.ts CHANGED
@@ -15,6 +15,7 @@ import {
15
15
  import { SWIFT_ICU_PACKED, swiftIcuPack } from '../../sootsim-swift/src/icuData.ts'
16
16
  import { swiftEnvironment, swiftSdkOptions } from './swift-toolchain.ts'
17
17
  import { SWIFT_WASM_TRIPLE } from './swiftTargetContract.ts'
18
+ import { readWasmSection, wasmSections } from './wasm-binary-sections'
18
19
 
19
20
  /**
20
21
  * the icu common data a swift artifact reserves and its host fills
@@ -191,36 +192,22 @@ function archiveMember(archive: Buffer, name: string): Buffer {
191
192
  // data-header magic (0xda27): the packaged data carries no relocations, so
192
193
  // the segment is the blob.
193
194
  function icuSegment(object: Buffer): Buffer {
194
- let at = 8
195
- const leb = (): number => {
196
- let result = 0
197
- let shift = 0
198
- let byte: number
199
- do {
200
- byte = object[at++]
201
- result += (byte & 0x7f) * 2 ** shift
202
- shift += 7
203
- } while (byte & 0x80)
204
- return result
205
- }
206
195
  const found: Buffer[] = []
207
- while (at < object.length) {
208
- const id = object[at++]
209
- const size = leb()
210
- const end = at + size
211
- if (id === 11) {
212
- for (let count = leb(); count > 0; count--) {
213
- if (leb() !== 0) throw new Error('swift-icu: unexpected data segment kind')
214
- at++ // i32.const
215
- leb()
216
- at++ // end
217
- const length = leb()
218
- const bytes = object.subarray(at, at + length)
219
- if (bytes[2] === 0xda && bytes[3] === 0x27) found.push(bytes)
220
- at += length
221
- }
196
+ for (const section of wasmSections(object)) {
197
+ if (section.id !== 11) continue
198
+ const reader = readWasmSection(object, section.start, section.end)
199
+ for (let count = reader.number(); count > 0; count--) {
200
+ if (reader.number() !== 0)
201
+ throw new Error('swift-icu: unexpected data segment kind')
202
+ reader.byte() // i32.const
203
+ reader.number()
204
+ reader.byte() // end
205
+ const length = reader.number()
206
+ const start = reader.position()
207
+ const bytes = object.subarray(start, start + length)
208
+ if (bytes[2] === 0xda && bytes[3] === 0x27) found.push(bytes)
209
+ reader.skip(length)
222
210
  }
223
- at = end
224
211
  }
225
212
  if (found.length !== 1)
226
213
  throw new Error(`swift-icu: expected one icu data segment, found ${found.length}`)
@@ -1,5 +1,6 @@
1
1
  import fs from 'node:fs'
2
2
  import path from 'node:path'
3
+ import { readULEB } from './wasm-binary-sections'
3
4
 
4
5
  export const OBJC_RUNTIME_LOOKUP_SECTION = 'peach_objc_runtime_lookup'
5
6
 
@@ -11,14 +12,9 @@ export const OBJC_RUNTIME_LOOKUP_SECTION = 'peach_objc_runtime_lookup'
11
12
  // becomes live, alongside Swift metadata reachability.
12
13
 
13
14
  const leb = (bytes: Uint8Array, at: { offset: number }): number => {
14
- let value = 0
15
- let scale = 1
16
- for (;;) {
17
- const byte = bytes[at.offset++]!
18
- value += (byte & 0x7f) * scale
19
- if (!(byte & 0x80)) return value
20
- scale *= 128
21
- }
15
+ const [value, next] = readULEB(bytes, at.offset)
16
+ at.offset = next
17
+ return value
22
18
  }
23
19
 
24
20
  // category and alias definitions share their segments with weak null records.
@@ -1,9 +1,10 @@
1
1
  import { spawn, spawnSync } from 'node:child_process'
2
- import { createHash, randomUUID } from 'node:crypto'
2
+ import { randomUUID } from 'node:crypto'
3
3
  import fs from 'node:fs'
4
4
  import path from 'node:path'
5
5
  import { mapLimit } from '../../../src/helpers/mapLimit.ts'
6
6
  import { isRecord } from './is-record'
7
+ import { sha256Hex } from './sha256'
7
8
  import { swiftClangImporterFlags } from './swift-objc-import'
8
9
  import { swiftObjCRuntimeFlags } from './swift-objc-runtime'
9
10
  import { iosDeploymentMinimum, objcSdkIntakeFlags } from './swift-objc-standin'
@@ -252,12 +253,6 @@ function compiledFrameworkImports(
252
253
  return imports
253
254
  }
254
255
 
255
- function sha256Hex(bytes: Buffer | string): string {
256
- if (typeof Bun !== 'undefined')
257
- return new Bun.CryptoHasher('sha256').update(bytes).digest('hex')
258
- return createHash('sha256').update(bytes).digest('hex')
259
- }
260
-
261
256
  function regularFileStamp(file: string): string | null {
262
257
  const stat = fs.lstatSync(file, { bigint: true, throwIfNoEntry: false })
263
258
  if (!stat || !stat.isFile()) return null
@@ -64,6 +64,11 @@ import {
64
64
  SWIFT_SDK_FUNCTION_ADDRESS_PREFIX,
65
65
  } from './swiftSdkLinkContract'
66
66
  import { SWIFT_WASM_TRIPLE } from './swiftTargetContract'
67
+ import {
68
+ encodeWasmName as encodedName,
69
+ encodeWasmNumber as uleb,
70
+ encodeWasmSigned as sleb,
71
+ } from './wasm-binary-sections'
67
72
  import { writeWasmCustomSection } from './wasm-narrow-boundary'
68
73
  import { KEEP_WASM_NAMES, lowerWasm, type WasmOptimization } from './wasm-optimize'
69
74
  import type {
@@ -753,33 +758,6 @@ const SDK_TLS_DELTA = '__peach_sdk_tls_delta.'
753
758
  const OBJC_SECTION_BOUNDARY =
754
759
  /^__(?:start|stop)_(__objc_(?:selectors|classes|class_refs|cats|protocols|protocol_refs|class_aliases|constant_string))$/
755
760
 
756
- const uleb = (value: number) => {
757
- const out: number[] = []
758
- do {
759
- let byte = value % 128
760
- value = Math.floor(value / 128)
761
- if (value) byte |= 128
762
- out.push(byte)
763
- } while (value)
764
- return out
765
- }
766
- const sleb = (value: bigint) => {
767
- const out: number[] = []
768
- for (;;) {
769
- const byte = Number(value & 127n)
770
- value >>= 7n
771
- if ((value === 0n && !(byte & 64)) || (value === -1n && byte & 64)) {
772
- out.push(byte)
773
- return out
774
- }
775
- out.push(byte | 128)
776
- }
777
- }
778
- const encodedName = (name: string) => {
779
- const bytes = [...Buffer.from(name)]
780
- return [...uleb(bytes.length), ...bytes]
781
- }
782
-
783
761
  // imports and globals keep their indices when an sdk image moves its symbols.
784
762
  function resolveSdkReferences(
785
763
  source: Buffer,
@@ -1,5 +1,12 @@
1
1
  import { chunkKey, type WasmLinkMap } from './swift-sdk-image-map'
2
2
  import { OBJC_LOAD_FUNCTION } from './swiftSdkLinkContract'
3
+ import {
4
+ encodeWasmName as encodedName,
5
+ encodeWasmNumber as uleb,
6
+ encodeWasmSigned as sleb,
7
+ readSLEB,
8
+ readULEB,
9
+ } from './wasm-binary-sections'
3
10
 
4
11
  export interface WasmSection {
5
12
  id: number
@@ -17,23 +24,13 @@ export function wasmReader(bytes: Uint8Array, start: number) {
17
24
  : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)
18
25
  let at = start
19
26
  const u = () => {
20
- let value = 0
21
- for (let shift = 0; ; shift += 7) {
22
- const byte = bytes[at++]!
23
- value += (byte & 127) * 2 ** shift
24
- if (!(byte & 128)) return value
25
- }
27
+ const [value, next] = readULEB(bytes, at)
28
+ at = next
29
+ return value
26
30
  }
27
31
  const s = () => {
28
- let value = 0n
29
- let shift = 0n
30
- let byte = 0
31
- do {
32
- byte = bytes[at++]!
33
- value |= BigInt(byte & 127) << shift
34
- shift += 7n
35
- } while (byte & 128)
36
- if (byte & 64) value -= 1n << shift
32
+ const [value, next] = readSLEB(bytes, at)
33
+ at = next
37
34
  return value
38
35
  }
39
36
  return {
@@ -72,32 +69,6 @@ export function wasmSections(bytes: Uint8Array): WasmSection[] {
72
69
  return out
73
70
  }
74
71
 
75
- const uleb = (value: number) => {
76
- const out: number[] = []
77
- do {
78
- let byte = value % 128
79
- value = Math.floor(value / 128)
80
- if (value) byte |= 128
81
- out.push(byte)
82
- } while (value)
83
- return out
84
- }
85
- const sleb = (value: bigint) => {
86
- const out: number[] = []
87
- for (;;) {
88
- const byte = Number(value & 127n)
89
- value >>= 7n
90
- if ((value === 0n && !(byte & 64)) || (value === -1n && byte & 64)) {
91
- out.push(byte)
92
- return out
93
- }
94
- out.push(byte | 128)
95
- }
96
- }
97
- const encodedName = (name: string) => {
98
- const bytes = [...Buffer.from(name)]
99
- return [...uleb(bytes.length), ...bytes]
100
- }
101
72
  const section = (id: number, payload: number[]) =>
102
73
  Uint8Array.from([id, ...uleb(payload.length), ...payload])
103
74
 
@@ -204,6 +204,15 @@ export function swiftSdkImageKey(
204
204
  .slice(0, 32)
205
205
  }
206
206
 
207
+ function swiftSdkImageVariantKey(baseKey: string, required: readonly string[]): string {
208
+ if (!required.length) return baseKey
209
+ return createHash('sha256')
210
+ .update(baseKey)
211
+ .update(JSON.stringify(required))
212
+ .digest('hex')
213
+ .slice(0, 32)
214
+ }
215
+
207
216
  const IMAGE_FILE = 'sdk.wasm'
208
217
  const IMAGE_DIGEST = 'sdk.sha256'
209
218
 
@@ -299,16 +308,39 @@ export async function buildSwiftSdkImage(
299
308
  moduleRoot: string,
300
309
  scopedLookups = false,
301
310
  frameworkImports: readonly string[] = [],
302
- options?: { producer?: boolean; signal?: AbortSignal },
311
+ options?: {
312
+ producer?: boolean
313
+ signal?: AbortSignal
314
+ requiredGuestSymbols?: readonly string[]
315
+ },
303
316
  ): Promise<SwiftSdkImage> {
304
317
  const { imports, products } = swiftSdkImageProducts(moduleRoot, frameworkImports)
305
- const key = swiftSdkImageKey(moduleRoot, scopedLookups, products)
306
- const directory = path.join(swiftCacheDir(), 'swift-sdk-images', key)
318
+ const images = path.join(swiftCacheDir(), 'swift-sdk-images')
319
+ const baseKey = swiftSdkImageKey(moduleRoot, scopedLookups, products)
320
+ const required = [...new Set(options?.requiredGuestSymbols)].sort()
321
+ const base = cached(path.join(images, baseKey), baseKey)
322
+ if (base) {
323
+ if (!required.length) return base
324
+ const { functions, data, tls } = base.sdkInterface
325
+ if (
326
+ required.every(
327
+ (name) =>
328
+ functions[name] !== undefined ||
329
+ data[name] !== undefined ||
330
+ tls[name] !== undefined,
331
+ )
332
+ )
333
+ return base
334
+ }
335
+ const key = swiftSdkImageVariantKey(baseKey, required)
336
+ const directory = path.join(images, key)
307
337
  const existing = cached(directory, key)
308
338
  if (existing) return existing
339
+ const distribution = swiftDistributionAt(moduleRoot)
309
340
  if (
310
341
  !options?.producer &&
311
- !swiftDistributionAt(moduleRoot) &&
342
+ !required.length &&
343
+ !distribution &&
312
344
  path.resolve(moduleRoot) === sourceSwiftCheckout() &&
313
345
  process.platform === 'linux' &&
314
346
  process.env.TM_SESSION
@@ -335,7 +367,7 @@ export async function buildSwiftSdkImage(
335
367
  withSwiftRootLock(directory, async () => {
336
368
  const previous = cached(directory, key)
337
369
  if (previous) return previous
338
- if (swiftDistributionAt(moduleRoot)) {
370
+ if (distribution && !required.length) {
339
371
  const entry = swiftSdkImageEntry(key, scopedLookups)
340
372
  const fetched = fetchNativeEntry('sdk-image', entry.name, entry.key)
341
373
  if (fetched) {
@@ -376,6 +408,7 @@ export async function buildSwiftSdkImage(
376
408
  work,
377
409
  scopedLookups,
378
410
  imports,
411
+ required,
379
412
  releaseFrameworks,
380
413
  ),
381
414
  )
@@ -393,6 +426,7 @@ async function linkSwiftSdkImage(
393
426
  work: string,
394
427
  scopedLookups: boolean,
395
428
  imports: readonly string[],
429
+ required: readonly string[],
396
430
  releaseFrameworks: () => void,
397
431
  ): Promise<SwiftSdkImage> {
398
432
  rmSync(work, { recursive: true, force: true })
@@ -579,7 +613,12 @@ async function linkSwiftSdkImage(
579
613
  const quote = (value: string) => JSON.stringify(value)
580
614
  writeFileSync(
581
615
  path.join(work, 'exports.rsp'),
582
- [...defined].map((name) => quote(`--export-if-defined=${name}`)).join('\n'),
616
+ [
617
+ ...[...defined].map((name) => quote(`--export-if-defined=${name}`)),
618
+ ...required
619
+ .filter((name) => !defined.has(name))
620
+ .map((name) => quote(`--export=${name}`)),
621
+ ].join('\n'),
583
622
  )
584
623
  const guestServices = path.join(work, 'peach-guest-services.allow')
585
624
  writeFileSync(guestServices, peachGuestServiceAllowlist)
@@ -674,13 +713,12 @@ async function linkSwiftSdkImage(
674
713
  ? new Bun.CryptoHasher('sha256').update(image).digest('hex')
675
714
  : createHash('sha256').update(image).digest('hex'),
676
715
  )
677
- if (
678
- swiftSdkImageKey(
679
- moduleRoot,
680
- scopedLookups,
681
- swiftSdkImageProducts(moduleRoot, imports).products,
682
- ) !== key
716
+ const baseKey = swiftSdkImageKey(
717
+ moduleRoot,
718
+ scopedLookups,
719
+ swiftSdkImageProducts(moduleRoot, imports).products,
683
720
  )
721
+ if (swiftSdkImageVariantKey(baseKey, required) !== key)
684
722
  throw new Error('Swift SDK image inputs changed during compilation')
685
723
  for (const name of readdirSync(work))
686
724
  if (!SWIFT_SDK_IMAGE_FILES.includes(name))
@@ -10,6 +10,7 @@ import {
10
10
  swiftSdkOptions,
11
11
  } from './swift-toolchain'
12
12
  import { SWIFT_WASM_TRIPLE, wasmThreadFlags } from './swiftTargetContract.ts'
13
+ import { readULEB } from './wasm-binary-sections'
13
14
 
14
15
  /** names a build root's distribution mirror (swift-distribution-mirror.ts). */
15
16
  export const SWIFT_DISTRIBUTION_MIRROR_RECORD = '.peach-mirror.json'
@@ -81,19 +82,6 @@ export function swiftSharedSdkFlags(moduleRoot: string, distribution: boolean):
81
82
  ]
82
83
  }
83
84
 
84
- function readULEB(b: Uint8Array, o: number): [number, number] {
85
- let res = 0
86
- let shift = 0
87
- let p = o
88
- while (true) {
89
- const byte = b[p++]!
90
- res |= (byte & 0x7f) << shift
91
- if ((byte & 0x80) === 0) break
92
- shift += 7
93
- }
94
- return [res, p]
95
- }
96
-
97
85
  // public runtimes pinned to a distribution before 7eb5f6473249e4be still
98
86
  // override preferredLocalizations in PeachFoundation-Localization.o.
99
87
  const WEAKEN_FOUNDATION_SYMS = new Set([