peachsim 0.1.1881 → 0.1.1886

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 (153) hide show
  1. package/cli/commands/control.ts +11 -10
  2. package/cli/commands/reset.ts +1 -1
  3. package/cli/outbound-endpoints.ts +1 -1
  4. package/detox/index.ts +62 -1
  5. package/dist-lib/agent-daemon-client.cjs +1 -1
  6. package/dist-lib/agent-events.cjs +1 -1
  7. package/dist-lib/attached-projects.cjs +1 -1
  8. package/dist-lib/auth/shared-session.cjs +1 -1
  9. package/dist-lib/backend-origin.cjs +1 -1
  10. package/dist-lib/beta.cjs +1 -1
  11. package/dist-lib/beta.mjs +1 -1
  12. package/dist-lib/bridge-constants.cjs +1 -1
  13. package/dist-lib/bridge-contract-input.cjs +1 -1
  14. package/dist-lib/bridge-contract-input.mjs +1 -1
  15. package/dist-lib/bridge-contract.cjs +1 -1
  16. package/dist-lib/bridge-contract.mjs +1 -1
  17. package/dist-lib/bundle-channel.cjs +8914 -4264
  18. package/dist-lib/capture-contract.cjs +1 -1
  19. package/dist-lib/capture-contract.mjs +1 -1
  20. package/dist-lib/cloud-contract.cjs +1 -1
  21. package/dist-lib/cloud-contract.mjs +1 -1
  22. package/dist-lib/cloud-test-args.cjs +1 -1
  23. package/dist-lib/cloud-test-args.mjs +1 -1
  24. package/dist-lib/cloud.cjs +1 -1
  25. package/dist-lib/cloud.mjs +1 -1
  26. package/dist-lib/config.cjs +1 -1
  27. package/dist-lib/config.mjs +1 -1
  28. package/dist-lib/detox/index.cjs +35 -2
  29. package/dist-lib/dev-bundle-resolution.cjs +1 -1
  30. package/dist-lib/expo-swift-bundle.cjs +8494 -3955
  31. package/dist-lib/expo-swift-metadata.cjs +5 -1
  32. package/dist-lib/expo-swift-metadata.mjs +5 -1
  33. package/dist-lib/home-paths.cjs +1 -1
  34. package/dist-lib/host/bridge-host.cjs +9679 -5057
  35. package/dist-lib/host/fetch-proxy-handler.cjs +93 -13
  36. package/dist-lib/host/fetch-proxy-overrides.cjs +10 -2
  37. package/dist-lib/host/fetch-proxy-overrides.mjs +8 -2
  38. package/dist-lib/host/replacement-module-handler.cjs +1 -1
  39. package/dist-lib/host/websocket-proxy.cjs +1 -1
  40. package/dist-lib/index.cjs +525 -120
  41. package/dist-lib/jump-to-source-babel.cjs +1 -1
  42. package/dist-lib/jump-to-source-native.cjs +1 -1
  43. package/dist-lib/kotlin-host.cjs +1 -1
  44. package/dist-lib/menu.cjs +1 -1
  45. package/dist-lib/menu.mjs +1 -1
  46. package/dist-lib/metro-fingerprint-registry.cjs +1 -1
  47. package/dist-lib/metro-fingerprint-registry.mjs +1 -1
  48. package/dist-lib/metro-production-bundle.cjs +1 -1
  49. package/dist-lib/metro-production-bundle.mjs +1 -1
  50. package/dist-lib/metro.cjs +523 -118
  51. package/dist-lib/native-app-identity-project.cjs +395 -72
  52. package/dist-lib/native-app-identity-project.mjs +720 -375
  53. package/dist-lib/native-app-identity.cjs +37 -1
  54. package/dist-lib/native-app-identity.mjs +36 -1
  55. package/dist-lib/native-firebase-metadata.cjs +1 -1
  56. package/dist-lib/native-firebase-metadata.mjs +1 -1
  57. package/dist-lib/pdfium-raster.cjs +1 -1
  58. package/dist-lib/pdfium-raster.mjs +1 -1
  59. package/dist-lib/plist.cjs +1 -1
  60. package/dist-lib/plist.mjs +1 -1
  61. package/dist-lib/profiles.cjs +1 -1
  62. package/dist-lib/public-brand.cjs +1 -1
  63. package/dist-lib/public-origins-lib-esm.json +58 -28
  64. package/dist-lib/public-origins-lib.json +233 -92
  65. package/dist-lib/react-native-host-modules.cjs +1 -1
  66. package/dist-lib/react-native-host-modules.mjs +1 -1
  67. package/dist-lib/reap-browser.cjs +1 -1
  68. package/dist-lib/scripts/dev-server-scanner.cjs +1 -1
  69. package/dist-lib/sdk.cjs +1 -1
  70. package/dist-lib/sdk.mjs +1 -1
  71. package/dist-lib/skills.cjs +30711 -25956
  72. package/dist-lib/swift-compile.cjs +6782 -2533
  73. package/dist-lib/swift-compile.mjs +6112 -1864
  74. package/dist-lib/swift-package.cjs +20 -3
  75. package/dist-lib/swift-package.mjs +20 -3
  76. package/dist-lib/swift-submit.cjs +2 -2
  77. package/dist-lib/swift-submit.mjs +2 -2
  78. package/dist-lib/swift-toolchain.cjs +19 -1
  79. package/dist-lib/swift.cjs +1 -1
  80. package/dist-lib/vite-swift.cjs +8640 -4109
  81. package/dist-lib/vite.cjs +93 -13
  82. package/dist-lib/worklet-executable.cjs +1 -1
  83. package/dist-lib/worklet-executable.mjs +1 -1
  84. package/dist-types/packages/peach-engine/src/engine/canvaskit-api.d.ts +1 -1
  85. package/dist-types/packages/peach-engine/src/engine/fontDataContract.d.ts +2 -0
  86. package/dist-types/packages/peach-engine/src/render-worker/host-service-protocol.d.ts +3 -0
  87. package/dist-types/packages/peach-engine/src/render-worker/native-ui-protocol.d.ts +1 -0
  88. package/dist-types/packages/peach-engine/src/render-worker/protocol.d.ts +11 -1
  89. package/dist-types/packages/peach-engine/src/render-worker/shell-tree-wire.d.ts +2 -1
  90. package/dist-types/packages/peach-globals/src/nodeContract.d.ts +1 -0
  91. package/dist-types/packages/peach-globals/src/testBridgeContract.d.ts +10 -1
  92. package/dist-types/packages/peach-kit/src/capabilities.d.ts +1 -0
  93. package/dist-types/packages/peach-kit/src/ui/popover-placement.d.ts +1 -1
  94. package/dist-types/packages/sootsim-swift/src/securityHostContract.d.ts +10 -0
  95. package/package.json +1 -1
  96. package/runtime-companion.json +1 -1
  97. package/sdk/ios/System/Library/Frameworks/CFNetwork.framework/Headers/CFHost.h +555 -0
  98. package/sdk/ios/System/Library/Frameworks/CFNetwork.framework/Headers/CFNetwork.h +1 -0
  99. package/sdk/ios/System/Library/Frameworks/Foundation.framework/Headers/Foundation.apinotes +6 -0
  100. package/sdk/ios/System/Library/Frameworks/Foundation.framework/Headers/NSURLProtocol.h +0 -1
  101. package/sdk/ios/System/Library/Frameworks/QuartzCore.framework/Modules/module.modulemap +6 -0
  102. package/sdk/ios/System/Library/Frameworks/UIKit.framework/Headers/UIBarButtonItemBadge.h +1 -1
  103. package/sdk/ios/System/Library/Frameworks/UIKit.framework/Headers/UIBarMinimization.h +1 -1
  104. package/sdk/ios/System/Library/Frameworks/UIKit.framework/Headers/UIScrollView.h +1 -1
  105. package/sdk/ios/System/Library/Frameworks/UIKit.framework/Headers/UISearchTab.h +1 -1
  106. package/sdk/ios/System/Library/Frameworks/UIKit.framework/Headers/UITab.h +1 -1
  107. package/sdk/ios/System/Library/Frameworks/UIKit.framework/Headers/UITabAccessory.h +2 -2
  108. package/sdk/ios/usr/include/os/activity.h +534 -0
  109. package/sdk/ios/usr/include/os.modulemap +15 -0
  110. package/sdk/ios/wasm/include/fcntl.h +16 -0
  111. package/sdk/ios/wasm/include/string.h +5 -0
  112. package/sdk/native-hashes.json +1 -1
  113. package/src/dns-over-https.ts +61 -9
  114. package/src/expo-swift-host.ts +11 -3
  115. package/src/host/fetch-proxy-handler.ts +51 -2
  116. package/src/host/fetch-proxy-overrides.ts +4 -0
  117. package/src/host/hostname-resolver.ts +9 -5
  118. package/src/native-app-identity-project.ts +13 -2
  119. package/src/native-app-identity.ts +47 -0
  120. package/src/nitro-adapter.lock.json +260 -260
  121. package/src/nitro-superset.lock.json +4 -4
  122. package/src/storageSnapshotContract.ts +20 -1
  123. package/src/swift-compile.ts +399 -72
  124. package/src/swift-compiler-digest.ts +15 -0
  125. package/src/swift-coverage/xcode-packages.ts +3 -2
  126. package/src/swift-distribution.lock.json +12 -12
  127. package/src/swift-guest-archive.ts +153 -0
  128. package/src/swift-guest-compile.ts +449 -0
  129. package/src/swift-guest-image-ledger.ts +656 -0
  130. package/src/swift-guest-link-plan.ts +52 -0
  131. package/src/swift-guest-link.ts +1414 -0
  132. package/src/swift-guest-linked-spans.ts +338 -0
  133. package/src/swift-guest-manifest.ts +141 -0
  134. package/src/swift-guest-projection-storage.ts +310 -0
  135. package/src/swift-guest-projection.ts +252 -0
  136. package/src/swift-guest-runtime-plan.ts +834 -0
  137. package/src/swift-guest-source-inputs.ts +427 -0
  138. package/src/swift-guest-source-plan.ts +279 -0
  139. package/src/swift-host.ts +125 -17
  140. package/src/swift-link-roots.ts +27 -50
  141. package/src/swift-package-manifest.ts +34 -18
  142. package/src/swift-package.ts +32 -2
  143. package/src/swift-packages.ts +138 -17
  144. package/src/swift-runtime-products.ts +73 -0
  145. package/src/swift-shared-sdk.ts +1 -1
  146. package/src/swift-submit.ts +13 -3
  147. package/src/swift-toolchain.ts +18 -0
  148. package/src/swift-wasm-prune.ts +313 -14
  149. package/src/swiftContract.ts +83 -0
  150. package/src/swiftRuntimeProductContract.ts +85 -45
  151. package/src/turbo-runtime.lock.json +23 -23
  152. package/src/wasm-optimize.ts +16 -4
  153. package/src/xcode-project.ts +142 -14
@@ -0,0 +1,338 @@
1
+ /**
2
+ * Address ranges attributable to linked objects. Linear-memory addresses are
3
+ * byte addresses; function-table addresses are table indices, never code bytes.
4
+ */
5
+ export interface GuestLinkedSpan {
6
+ readonly image: number
7
+ readonly space: 'linear-memory' | 'function-table'
8
+ readonly address: number
9
+ readonly length: number
10
+ }
11
+
12
+ interface WasmLayout {
13
+ readonly functionsByCodeOffset: Map<number, number>
14
+ readonly functionsByTableSlot: Map<number, number>
15
+ readonly data: readonly { address: number; bytes: Uint8Array }[]
16
+ readonly memoryBytes: number
17
+ }
18
+
19
+ function number(bytes: Uint8Array, start: number): [number, number] {
20
+ let value = 0
21
+ let shift = 0
22
+ let pos = start
23
+ for (;;) {
24
+ if (pos >= bytes.length || shift > 28) throw new Error('invalid wasm integer')
25
+ const byte = bytes[pos++]!
26
+ value += (byte & 127) * 2 ** shift
27
+ if (!(byte & 128)) return [value, pos]
28
+ shift += 7
29
+ }
30
+ }
31
+
32
+ function signedAddress(bytes: Uint8Array, start: number): [number, number] {
33
+ const [value, end] = number(bytes, start)
34
+ const bits = (end - start) * 7
35
+ const signed = bits < 32 && bytes[end - 1]! & 0x40 ? value - 2 ** bits : value
36
+ return [signed >>> 0, end]
37
+ }
38
+
39
+ function layout(bytes: Uint8Array): WasmLayout {
40
+ if (Buffer.from(bytes.subarray(0, 8)).toString('hex') !== '0061736d01000000')
41
+ throw new Error('expected linked wasm module')
42
+ const sections = new Map<number, [number, number]>()
43
+ let offset = 8
44
+ while (offset < bytes.length) {
45
+ const id = bytes[offset++]!
46
+ const [size, start] = number(bytes, offset)
47
+ if (start + size > bytes.length) throw new Error('invalid wasm section')
48
+ if (id) sections.set(id, [start, start + size])
49
+ offset = start + size
50
+ }
51
+ const exports = sections.get(7)
52
+ if (!exports) throw new Error('guest wasm does not export memory and table')
53
+ let exportPosition = exports[0]
54
+ const readExport = () => {
55
+ const [value, next] = number(bytes, exportPosition)
56
+ exportPosition = next
57
+ return value
58
+ }
59
+ const exported = new Set<number>()
60
+ const exportCount = readExport()
61
+ for (let i = 0; i < exportCount; i++) {
62
+ const nameLength = readExport()
63
+ exportPosition += nameLength
64
+ const kind = bytes[exportPosition++]!
65
+ const index = readExport()
66
+ if ((kind === 1 || kind === 2) && index === 0) exported.add(kind)
67
+ }
68
+ if (exportPosition !== exports[1] || !exported.has(1) || !exported.has(2))
69
+ throw new Error('guest wasm does not export memory and table')
70
+ let imports = 0
71
+ let memoryPages: number | undefined
72
+ const section = sections.get(2)
73
+ if (section) {
74
+ let pos = section[0]
75
+ const read = () => {
76
+ const [value, next] = number(bytes, pos)
77
+ pos = next
78
+ return value
79
+ }
80
+ const string = () => {
81
+ const length = read()
82
+ pos += length
83
+ if (pos > section[1]) throw new Error('invalid wasm import')
84
+ }
85
+ const limits = () => {
86
+ const flags = read()
87
+ if (flags & ~3) throw new Error('unsupported wasm limits')
88
+ const minimum = read()
89
+ if (flags & 1) read()
90
+ return minimum
91
+ }
92
+ const count = read()
93
+ for (let i = 0; i < count; i++) {
94
+ string()
95
+ string()
96
+ const kind = bytes[pos++]!
97
+ if (kind === 0) {
98
+ imports++
99
+ read()
100
+ } else if (kind === 1) {
101
+ const type = bytes[pos++]!
102
+ if (type !== 0x70 && type !== 0x6f) throw new Error('unsupported wasm table type')
103
+ limits()
104
+ } else if (kind === 2) {
105
+ if (memoryPages !== undefined) throw new Error('unsupported wasm memories')
106
+ memoryPages = limits()
107
+ } else if (kind === 3) pos += 2
108
+ else if (kind === 4) {
109
+ read()
110
+ read()
111
+ } else throw new Error('unsupported wasm import kind')
112
+ }
113
+ if (pos !== section[1]) throw new Error('invalid wasm imports')
114
+ }
115
+ const memory = sections.get(5)
116
+ if (memory) {
117
+ let pos = memory[0]
118
+ const read = () => {
119
+ const [value, next] = number(bytes, pos)
120
+ pos = next
121
+ return value
122
+ }
123
+ if (read() !== 1 || memoryPages !== undefined)
124
+ throw new Error('unsupported wasm memories')
125
+ const flags = read()
126
+ if (flags & ~3) throw new Error('unsupported wasm memory limits')
127
+ memoryPages = read()
128
+ if (flags & 1) read()
129
+ if (pos !== memory[1]) throw new Error('invalid wasm memory section')
130
+ }
131
+ if (memoryPages === undefined) throw new Error('guest wasm has no memory')
132
+ const functionsByCodeOffset = new Map<number, number>()
133
+ const code = sections.get(10)
134
+ if (code) {
135
+ let pos = code[0]
136
+ let count: number
137
+ ;[count, pos] = number(bytes, pos)
138
+ for (let i = 0; i < count; i++) {
139
+ const prefix = pos
140
+ let size: number
141
+ ;[size, pos] = number(bytes, pos)
142
+ functionsByCodeOffset.set(prefix - code[0], imports + i)
143
+ pos += size
144
+ if (pos > code[1]) throw new Error('invalid wasm code')
145
+ }
146
+ if (pos !== code[1]) throw new Error('invalid wasm code section')
147
+ }
148
+ const functionsByTableSlot = new Map<number, number>()
149
+ const elements = sections.get(9)
150
+ if (elements) {
151
+ let pos = elements[0]
152
+ const read = () => {
153
+ const [value, next] = number(bytes, pos)
154
+ pos = next
155
+ return value
156
+ }
157
+ const count = read()
158
+ for (let i = 0; i < count; i++) {
159
+ const flags = read()
160
+ if (flags !== 0 && flags !== 2)
161
+ throw new Error(`unsupported wasm element segment ${flags}`)
162
+ if (flags === 2 && read() !== 0) throw new Error('unsupported wasm table index')
163
+ if (bytes[pos++] !== 0x41) throw new Error('nonconstant wasm table offset')
164
+ let base: number
165
+ ;[base, pos] = signedAddress(bytes, pos)
166
+ if (bytes[pos++] !== 0x0b) throw new Error('invalid wasm table offset')
167
+ if (flags === 2 && bytes[pos++] !== 0)
168
+ throw new Error('unsupported wasm element type')
169
+ const length = read()
170
+ for (let j = 0; j < length; j++) {
171
+ const slot = base + j
172
+ if (functionsByTableSlot.has(slot))
173
+ throw new Error('overlapping wasm table segments')
174
+ functionsByTableSlot.set(slot, read())
175
+ }
176
+ }
177
+ if (pos !== elements[1]) throw new Error('invalid wasm element section')
178
+ }
179
+ const data: { address: number; bytes: Uint8Array }[] = []
180
+ const dataSection = sections.get(11)
181
+ if (dataSection) {
182
+ let pos = dataSection[0]
183
+ const read = () => {
184
+ const [value, next] = number(bytes, pos)
185
+ pos = next
186
+ return value
187
+ }
188
+ const count = read()
189
+ for (let i = 0; i < count; i++) {
190
+ const flags = read()
191
+ if (flags !== 0 && flags !== 2)
192
+ throw new Error(`unsupported wasm data segment ${flags}`)
193
+ if (flags === 2 && read() !== 0) throw new Error('unsupported wasm memory index')
194
+ if (bytes[pos++] !== 0x41) throw new Error('nonconstant wasm data offset')
195
+ let address: number
196
+ ;[address, pos] = signedAddress(bytes, pos)
197
+ if (bytes[pos++] !== 0x0b) throw new Error('invalid wasm data offset')
198
+ const length = read()
199
+ if (pos + length > dataSection[1]) throw new Error('invalid wasm data')
200
+ data.push({ address, bytes: bytes.subarray(pos, pos + length) })
201
+ pos += length
202
+ }
203
+ if (pos !== dataSection[1]) throw new Error('invalid wasm data section')
204
+ }
205
+ return {
206
+ functionsByCodeOffset,
207
+ functionsByTableSlot,
208
+ data,
209
+ memoryBytes: memoryPages * 65536,
210
+ }
211
+ }
212
+
213
+ /** File offsets and guest addresses of the linked module's active data segments. */
214
+ export function guestWasmDataSegments(bytes: Uint8Array): {
215
+ readonly address: number
216
+ readonly offset: number
217
+ readonly length: number
218
+ }[] {
219
+ return layout(bytes).data.map((segment) => ({
220
+ address: segment.address,
221
+ offset: segment.bytes.byteOffset - bytes.byteOffset,
222
+ length: segment.bytes.length,
223
+ }))
224
+ }
225
+
226
+ /**
227
+ * Read only ranges belonging to the explicitly owned final-link inputs. The
228
+ * caller must retain the complete selected-input ownership separately. A map
229
+ * of original paths is insufficient: names must match the linker's rewritten
230
+ * objects and archives. Generated wrappers can have distinct per-section
231
+ * owners. Post-optimization slots and data must remain observable at the same
232
+ * addresses; a missing or cross-image folded slot is rejected.
233
+ */
234
+ export function guestLinkedSpans(
235
+ map: string,
236
+ before: Uint8Array,
237
+ after: Uint8Array,
238
+ owners: ReadonlyMap<string, number>,
239
+ sectionOwners: ReadonlyMap<string, number> = new Map(),
240
+ ): GuestLinkedSpan[] {
241
+ const original = layout(before)
242
+ const optimized = layout(after)
243
+ let codeOffset: number | undefined
244
+ let outputSection = ''
245
+ const codeOwners = new Map<number, number>()
246
+ const spans: GuestLinkedSpan[] = []
247
+ for (const line of map.split('\n')) {
248
+ const match = /^\s*(\S+)\s+([\da-f]+)\s+([\da-f]+)\s+(.+)$/.exec(line)
249
+ if (!match) continue
250
+ const [, address, offset, size, source] = match
251
+ if (address === '-' && /^[A-Z][A-Z0-9_]*$/.test(source!)) outputSection = source!
252
+ if (source === 'CODE') {
253
+ codeOffset = parseInt(offset!, 16)
254
+ continue
255
+ }
256
+ const separator = source!.indexOf(':(')
257
+ if (separator < 0) continue
258
+ const image = sectionOwners.get(source!) ?? owners.get(source!.slice(0, separator))
259
+ if (image === undefined) continue
260
+ const length = parseInt(size!, 16)
261
+ if (!length) continue
262
+ if (address === '-') {
263
+ if (outputSection !== 'CODE') continue
264
+ if (codeOffset === undefined) throw new Error('link map has no code section')
265
+ const index = original.functionsByCodeOffset.get(parseInt(offset!, 16) - codeOffset)
266
+ if (index === undefined) throw new Error('link map function has no wasm body')
267
+ const existing = codeOwners.get(index)
268
+ if (existing !== undefined && existing !== image)
269
+ throw new Error('wasm function belongs to multiple images')
270
+ codeOwners.set(index, image)
271
+ } else {
272
+ if (outputSection !== 'DATA') continue
273
+ spans.push({
274
+ image,
275
+ space: 'linear-memory',
276
+ address: parseInt(address!, 16),
277
+ length,
278
+ })
279
+ }
280
+ }
281
+ const optimizedOwners = new Map<number, number>()
282
+ const dataAt = (module: WasmLayout, start: number, length: number): Buffer => {
283
+ const result = Buffer.alloc(length)
284
+ for (const segment of module.data) {
285
+ const first = Math.max(start, segment.address)
286
+ const last = Math.min(start + length, segment.address + segment.bytes.length)
287
+ if (first < last)
288
+ result.set(
289
+ segment.bytes.subarray(first - segment.address, last - segment.address),
290
+ first - start,
291
+ )
292
+ }
293
+ return result
294
+ }
295
+ for (const span of spans) {
296
+ if (
297
+ span.address + span.length > original.memoryBytes ||
298
+ span.address + span.length > optimized.memoryBytes
299
+ )
300
+ throw new Error(`linked data lies outside guest memory at ${span.address}`)
301
+ if (
302
+ !dataAt(original, span.address, span.length).equals(
303
+ dataAt(optimized, span.address, span.length),
304
+ )
305
+ )
306
+ throw new Error(`optimization changed linked data at ${span.address}`)
307
+ }
308
+ let previous: GuestLinkedSpan | undefined
309
+ for (const span of [...spans].sort((a, b) => a.address - b.address)) {
310
+ if (
311
+ previous &&
312
+ span.address < previous.address + previous.length &&
313
+ span.image !== previous.image
314
+ )
315
+ throw new Error('linked data belongs to multiple images')
316
+ if (!previous || span.address + span.length > previous.address + previous.length)
317
+ previous = span
318
+ }
319
+ for (const [slot, originalFunction] of original.functionsByTableSlot) {
320
+ const image = codeOwners.get(originalFunction)
321
+ if (image === undefined) continue
322
+ const optimizedFunction = optimized.functionsByTableSlot.get(slot)
323
+ if (optimizedFunction === undefined)
324
+ throw new Error(`optimized wasm removed table slot ${slot}`)
325
+ const existing = optimizedOwners.get(optimizedFunction)
326
+ if (existing !== undefined && existing !== image)
327
+ throw new Error('optimization merged callable functions from different images')
328
+ optimizedOwners.set(optimizedFunction, image)
329
+ spans.push({ image, space: 'function-table', address: slot, length: 1 })
330
+ }
331
+ for (const [slot, optimizedFunction] of optimized.functionsByTableSlot) {
332
+ if (!optimizedOwners.has(optimizedFunction)) continue
333
+ const originalFunction = original.functionsByTableSlot.get(slot)
334
+ if (originalFunction === undefined || !codeOwners.has(originalFunction))
335
+ throw new Error('optimization merged an unowned callable function')
336
+ }
337
+ return spans
338
+ }
@@ -0,0 +1,141 @@
1
+ import { createHash } from 'node:crypto'
2
+ import { readFileSync } from 'node:fs'
3
+ import {
4
+ selectedGuestInputImage,
5
+ type GuestLinkPlan,
6
+ type GuestLinkSelection,
7
+ } from './swift-guest-link-plan'
8
+ import { readArMembers } from './swift-wasm-prune'
9
+ import type {
10
+ SwiftGuestImageManifest,
11
+ SwiftGuestImageRegion,
12
+ } from './swiftGuestImageContract'
13
+
14
+ /** A selected linker input with a stable identity, independent of cache paths. */
15
+ export interface GuestManifestInput {
16
+ readonly identity: string
17
+ readonly bytes: Uint8Array
18
+ }
19
+
20
+ export interface GuestManifestImage {
21
+ readonly id: string
22
+ readonly installName: string
23
+ readonly fileType: SwiftGuestImageManifest['fileType']
24
+ readonly buildVersion: SwiftGuestImageManifest['buildVersion']
25
+ readonly inputs: readonly GuestManifestInput[]
26
+ /** effective options with logical input identities in place of host paths. */
27
+ readonly linkOptions: readonly string[]
28
+ readonly regions: SwiftGuestImageRegion[]
29
+ }
30
+
31
+ /** Read only selected original objects, grouped by their resolved image. */
32
+ export function selectedGuestManifestInputs(
33
+ plan: GuestLinkPlan,
34
+ selection: readonly GuestLinkSelection[],
35
+ identities: ReadonlyMap<string, string>,
36
+ ): Map<number, GuestManifestInput[]> {
37
+ const artifacts = new Map(plan.artifacts.map((artifact) => [artifact.path, artifact]))
38
+ const archives = new Map<string, ReturnType<typeof readArMembers>>()
39
+ const images = new Map<number, GuestManifestInput[]>()
40
+ for (const item of selection) {
41
+ const artifact = artifacts.get(item.artifact)
42
+ const identity = identities.get(item.artifact)
43
+ if (!artifact || identity === undefined)
44
+ throw new Error(`selected input has no logical identity: ${item.artifact}`)
45
+ let bytes: Uint8Array
46
+ let memberIdentity: readonly [string, number] | null = null
47
+ if (item.member === null) bytes = readFileSync(item.artifact)
48
+ else {
49
+ let members = archives.get(item.artifact)
50
+ if (!members) {
51
+ members = readArMembers(readFileSync(item.artifact))
52
+ archives.set(item.artifact, members)
53
+ }
54
+ const matching = members.filter((member) => member.name === item.member)
55
+ if (matching.length !== 1)
56
+ throw new Error(
57
+ `selected archive member is ambiguous: ${item.artifact}(${item.member})`,
58
+ )
59
+ bytes = matching[0]!.bytes
60
+ const origin = artifact.memberOrigins?.get(item.member)
61
+ if (artifact.memberOrigins && !origin)
62
+ throw new Error(
63
+ `selected member has no original identity: ${item.artifact}(${item.member})`,
64
+ )
65
+ if (origin && createHash('sha256').update(bytes).digest('hex') !== origin.sha256)
66
+ throw new Error(
67
+ `selected member differs from original: ${item.artifact}(${item.member})`,
68
+ )
69
+ memberIdentity = [origin?.name ?? item.member, origin?.ordinal ?? 1]
70
+ }
71
+ const image = selectedGuestInputImage(artifact, item.member)
72
+ let inputs = images.get(image)
73
+ if (!inputs) {
74
+ inputs = []
75
+ images.set(image, inputs)
76
+ }
77
+ inputs.push({
78
+ identity:
79
+ memberIdentity === null
80
+ ? identity
81
+ : JSON.stringify([identity, ...memberIdentity]),
82
+ bytes,
83
+ })
84
+ }
85
+ return images
86
+ }
87
+
88
+ /** Build the manifest after selection and final address assignment. */
89
+ export function createGuestImageManifest(
90
+ image: GuestManifestImage,
91
+ ): SwiftGuestImageManifest {
92
+ const target = {
93
+ triple: 'wasm32-unknown-wasip1',
94
+ pointerWidth: 32,
95
+ byteOrder: 'little',
96
+ cpuType: -1,
97
+ cpuSubtype: 0,
98
+ } satisfies SwiftGuestImageManifest['target']
99
+ const digest = createHash('sha256')
100
+ // length framing distinguishes different sequences without relying on a
101
+ // separator that an input name, compiler option, or object can contain.
102
+ const add = (bytes: Uint8Array) => {
103
+ const size = Buffer.alloc(8)
104
+ size.writeBigUInt64BE(BigInt(bytes.byteLength))
105
+ digest.update(size)
106
+ digest.update(bytes)
107
+ }
108
+ const text = (value: string) => add(Buffer.from(value, 'utf8'))
109
+ const version = image.buildVersion
110
+ text('swift-guest-image-v1')
111
+ text(image.id)
112
+ text(image.installName)
113
+ text(image.fileType)
114
+ text(target.triple)
115
+ text(
116
+ `${target.pointerWidth}:${target.byteOrder}:${target.cpuType}:${target.cpuSubtype}`,
117
+ )
118
+ text(
119
+ `${version.platform}:${version.minimum.major}:${version.minimum.minor}:${version.minimum.patch}`,
120
+ )
121
+ text(`${version.sdk.major}:${version.sdk.minor}:${version.sdk.patch}`)
122
+ text(String(image.linkOptions.length))
123
+ for (const option of image.linkOptions) text(option)
124
+ text(String(image.inputs.length))
125
+ for (const input of image.inputs) {
126
+ text(input.identity)
127
+ add(input.bytes)
128
+ }
129
+ const inputsSha256 = digest.digest('hex')
130
+ return {
131
+ version: 1,
132
+ id: image.id,
133
+ installName: image.installName,
134
+ fileType: image.fileType,
135
+ target,
136
+ inputsSha256,
137
+ uuid: inputsSha256.slice(0, 32),
138
+ buildVersion: image.buildVersion,
139
+ regions: image.regions,
140
+ }
141
+ }