peachsim 0.1.2589 → 0.1.2591

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 (139) 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 +1490 -534
  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-6FUOY6FX.mjs → acorn-EMX4WZ72.mjs} +3 -3
  27. package/dist-lib/esm-chunks/{chunk-33SCXU3Y.mjs → chunk-4QHHRGO4.mjs} +1 -1
  28. package/dist-lib/esm-chunks/{chunk-Y4PV4HZB.mjs → chunk-7627LKJK.mjs} +1 -1
  29. package/dist-lib/esm-chunks/{chunk-M4KUWGU3.mjs → chunk-AKKHSVJ4.mjs} +1 -1
  30. package/dist-lib/esm-chunks/{chunk-MKSOJCRT.mjs → chunk-EGKH2L6J.mjs} +1 -1
  31. package/dist-lib/esm-chunks/{chunk-5EFD2VBY.mjs → chunk-ILWFGZWW.mjs} +1 -1
  32. package/dist-lib/esm-chunks/{chunk-57FFWABF.mjs → chunk-J676B57D.mjs} +5 -5
  33. package/dist-lib/esm-chunks/{chunk-JLOJZF2Y.mjs → chunk-JHXDM3YU.mjs} +1 -1
  34. package/dist-lib/esm-chunks/{chunk-464CBA5U.mjs → chunk-MC6NMYWY.mjs} +1 -1
  35. package/dist-lib/esm-chunks/{chunk-CRIYVOMB.mjs → chunk-NX23AMX4.mjs} +1 -1
  36. package/dist-lib/esm-chunks/{chunk-EAIFG4MQ.mjs → chunk-Q4NA6PUR.mjs} +3 -3
  37. package/dist-lib/esm-chunks/{chunk-47K4QPVA.mjs → chunk-QBMFX3J2.mjs} +1 -1
  38. package/dist-lib/esm-chunks/{chunk-FQQ2DMXS.mjs → chunk-RKQZGWX2.mjs} +1 -1
  39. package/dist-lib/esm-chunks/{chunk-5QWQDXXD.mjs → chunk-SOAYP72T.mjs} +1 -1
  40. package/dist-lib/esm-chunks/{chunk-XTKKROVU.mjs → chunk-TFG4LZ5T.mjs} +1 -1
  41. package/dist-lib/esm-chunks/{chunk-KFHCRS5K.mjs → chunk-UIVXVMS5.mjs} +2 -2
  42. package/dist-lib/esm-chunks/{chunk-BSAAJR25.mjs → chunk-UYJXLDCG.mjs} +1 -1
  43. package/dist-lib/esm-chunks/{chunk-FMWEI6OI.mjs → chunk-VD27WNCT.mjs} +4 -4
  44. package/dist-lib/esm-chunks/{chunk-3VNUC4DT.mjs → chunk-VF5WVB3D.mjs} +1 -1
  45. package/dist-lib/esm-chunks/{chunk-WQ73RKJY.mjs → chunk-VRVXRZI4.mjs} +5 -5
  46. package/dist-lib/esm-chunks/{chunk-L3OSVIGA.mjs → chunk-VYZ5DC3M.mjs} +1359 -487
  47. package/dist-lib/esm-chunks/{chunk-I22LAVD5.mjs → chunk-YR6DNV76.mjs} +1 -1
  48. package/dist-lib/esm-chunks/{chunk-PUBH3EXI.mjs → chunk-ZL5N6ENT.mjs} +1 -1
  49. package/dist-lib/esm-chunks/{metro-layout-parser-T5QIJTEV.mjs → metro-layout-parser-ANKZARNW.mjs} +3 -3
  50. package/dist-lib/esm-chunks/{swift-package-prebuilt-AWTZHPDM.mjs → swift-package-prebuilt-4N5CXBOZ.mjs} +10 -10
  51. package/dist-lib/esm-chunks/{swift-sdk-app-module-MGTJTVKU.mjs → swift-sdk-app-module-5IUSRXYA.mjs} +151 -67
  52. package/dist-lib/esm-chunks/{swift-sdk-image-JABGF3ZR.mjs → swift-sdk-image-7T2LMQS5.mjs} +14 -14
  53. package/dist-lib/esm-chunks/{swift-sdk-image-producer-RJRJ3EFU.mjs → swift-sdk-image-producer-7QJ3BF6E.mjs} +4 -4
  54. package/dist-lib/expo-swift-bundle.cjs +1489 -533
  55. package/dist-lib/expo-swift-metadata.cjs +1 -1
  56. package/dist-lib/expo-swift-metadata.mjs +7 -7
  57. package/dist-lib/home-paths.cjs +1 -1
  58. package/dist-lib/host/bridge-host.cjs +1518 -557
  59. package/dist-lib/host/fetch-proxy-handler.cjs +4 -4
  60. package/dist-lib/host/fetch-proxy-overrides.cjs +1 -1
  61. package/dist-lib/host/fetch-proxy-overrides.mjs +2 -2
  62. package/dist-lib/host/replacement-module-handler.cjs +1 -1
  63. package/dist-lib/host/websocket-proxy.cjs +1 -1
  64. package/dist-lib/index.cjs +1 -1
  65. package/dist-lib/kotlin-host.cjs +1 -1
  66. package/dist-lib/menu.cjs +1 -1
  67. package/dist-lib/menu.mjs +3 -3
  68. package/dist-lib/metro-fingerprint-registry.cjs +1 -1
  69. package/dist-lib/metro-fingerprint-registry.mjs +3 -3
  70. package/dist-lib/metro-production-bundle.cjs +1 -1
  71. package/dist-lib/metro-production-bundle.mjs +4 -4
  72. package/dist-lib/native-app-identity-project.cjs +2 -2
  73. package/dist-lib/native-app-identity-project.mjs +16 -16
  74. package/dist-lib/native-app-identity.cjs +1 -1
  75. package/dist-lib/native-app-identity.mjs +6 -6
  76. package/dist-lib/native-firebase-metadata.cjs +1 -1
  77. package/dist-lib/native-firebase-metadata.mjs +3 -3
  78. package/dist-lib/pdfium-raster.cjs +1 -1
  79. package/dist-lib/pdfium-raster.mjs +2 -2
  80. package/dist-lib/plist.cjs +1 -1
  81. package/dist-lib/plist.mjs +3 -3
  82. package/dist-lib/profiles.cjs +1 -1
  83. package/dist-lib/public-brand.cjs +1 -1
  84. package/dist-lib/public-origins-lib-esm.json +81 -81
  85. package/dist-lib/public-origins-lib.json +69 -69
  86. package/dist-lib/react-native-host-modules.cjs +1 -1
  87. package/dist-lib/react-native-host-modules.mjs +2 -2
  88. package/dist-lib/reap-browser.cjs +1 -1
  89. package/dist-lib/scripts/dev-server-scanner.cjs +1 -1
  90. package/dist-lib/sdk.cjs +1 -1
  91. package/dist-lib/sdk.mjs +8 -8
  92. package/dist-lib/skills.cjs +1520 -559
  93. package/dist-lib/swift-compile.cjs +1486 -530
  94. package/dist-lib/swift-compile.mjs +10 -10
  95. package/dist-lib/swift-package.cjs +1 -1
  96. package/dist-lib/swift-package.mjs +3 -3
  97. package/dist-lib/swift-submit.cjs +1 -1
  98. package/dist-lib/swift-submit.mjs +6 -6
  99. package/dist-lib/swift-svg-raster.cjs +1 -1
  100. package/dist-lib/swift-svg-raster.mjs +2 -2
  101. package/dist-lib/swift-toolchain.cjs +1 -1
  102. package/dist-lib/swift.cjs +1 -1
  103. package/dist-lib/vendor/@peculiar/x509.cjs +1 -1
  104. package/dist-lib/vendor/@ruby/prism/src/nodes.js.cjs +1 -1
  105. package/dist-lib/vendor/@ruby/prism/src/parsePrism.js.cjs +1 -1
  106. package/dist-lib/vendor/@xmldom/xmldom.cjs +1 -1
  107. package/dist-lib/vendor/acorn.cjs +1 -1
  108. package/dist-lib/vendor/bplist-parser.cjs +1 -1
  109. package/dist-lib/vendor/ignore.cjs +1 -1
  110. package/dist-lib/vendor/magic-string.cjs +1 -1
  111. package/dist-lib/vendor/plist.cjs +1 -1
  112. package/dist-lib/vendor/smol-toml.cjs +1 -1
  113. package/dist-lib/vendor/stream-json.cjs +1 -1
  114. package/dist-lib/vendor/sucrase/dist/index.js.cjs +1 -1
  115. package/dist-lib/vendor/sucrase/dist/parser/index.js.cjs +1 -1
  116. package/dist-lib/vendor/sucrase/dist/parser/tokenizer/index.js.cjs +1 -1
  117. package/dist-lib/vendor/sucrase/dist/parser/tokenizer/types.js.cjs +1 -1
  118. package/dist-lib/vendor/sucrase.cjs +1 -1
  119. package/dist-lib/vendor/terser.cjs +1 -1
  120. package/dist-lib/vendor/valibot.cjs +1 -1
  121. package/dist-lib/vendor/ws.cjs +1 -1
  122. package/dist-lib/vendor/zod.cjs +1 -1
  123. package/dist-lib/worklet-executable.cjs +1 -1
  124. package/dist-lib/worklet-executable.mjs +3 -3
  125. package/kotlin/sdk.tar.gz +0 -0
  126. package/package.json +1 -1
  127. package/src/swift-guest-compile.ts +43 -10
  128. package/src/swift-guest-dyld-table.ts +9 -2
  129. package/src/swift-guest-image-ledger.ts +403 -208
  130. package/src/swift-guest-link.ts +258 -113
  131. package/src/swift-guest-linked-spans.ts +217 -28
  132. package/src/swift-guest-manifest.ts +24 -13
  133. package/src/swift-guest-projection-storage.ts +6 -8
  134. package/src/swift-guest-projection.ts +7 -6
  135. package/src/swift-guest-source-inputs.ts +158 -30
  136. package/src/swift-sdk-app-module.ts +146 -59
  137. package/src/swift-sdk-image-map.ts +137 -35
  138. package/src/swift-wasm-prune.ts +263 -85
  139. package/src/swiftGuestImageContract.ts +1 -1
@@ -53,6 +53,23 @@ export const chunkKey = (
53
53
  export const isPartitioned = (section: string) =>
54
54
  PARTITIONED.some((name) => section === name || section.startsWith(`${name}.`))
55
55
 
56
+ function isHexChar(code: number): boolean {
57
+ return (code >= 48 && code <= 57) || (code >= 97 && code <= 102)
58
+ }
59
+
60
+ function hexDigit(code: number): number {
61
+ return code <= 57 ? code - 48 : code - 87
62
+ }
63
+
64
+ function hasLineBreak(line: string, from: number): boolean {
65
+ if (line.indexOf('\r', from) >= 0) return true
66
+ for (let i = from; i < line.length; i++) {
67
+ const ch = line.charCodeAt(i)
68
+ if (ch === 0x2028 || ch === 0x2029) return true
69
+ }
70
+ return false
71
+ }
72
+
56
73
  /** reads a wasm-ld -Map file's data and code input chunks with the symbols each defines. */
57
74
  export function readWasmLinkMap(file: string, partition: boolean): WasmLinkMap {
58
75
  const segments = new Map<string, { address: number; size: number }>()
@@ -61,48 +78,133 @@ export function readWasmLinkMap(file: string, partition: boolean): WasmLinkMap {
61
78
  let kind = ''
62
79
  let segment = ''
63
80
  let previous: WasmMapChunk | undefined
64
- const hex = (value: string) => (value === '-' ? -1 : parseInt(value, 16))
65
81
  for (const line of guestLinkLines(file)) {
66
- const symbol = /^ *([0-9a-f]+|-) +[0-9a-f]+ +([0-9a-f]+) {17}(\S.*)$/.exec(line)
67
- if (symbol) {
68
- previous?.symbols.push({
69
- name: symbol[3]!,
70
- address: hex(symbol[1]!),
71
- size: parseInt(symbol[2]!, 16),
72
- })
73
- continue
82
+ const length = line.length
83
+ let pos = 0
84
+ while (pos < length && line.charCodeAt(pos) === 32) pos++
85
+ if (pos >= length) continue
86
+ const leadingSpaces = pos
87
+
88
+ // col 1: addr (lower hex or '-')
89
+ let address = -1
90
+ if (line.charCodeAt(pos) === 45) {
91
+ pos++
92
+ } else {
93
+ const start = pos
94
+ let value = 0
95
+ while (pos < length && isHexChar(line.charCodeAt(pos))) {
96
+ value = value * 16 + hexDigit(line.charCodeAt(pos++))
97
+ }
98
+ if (pos === start) continue
99
+ address = value
74
100
  }
75
- const output = /^ +- +[0-9a-f]+ +[0-9a-f]+ (\S+)$/.exec(line)
76
- if (output) {
77
- kind = output[1]!
101
+
102
+ // spaces between col 1 and col 2
103
+ const s1 = pos
104
+ while (pos < length && line.charCodeAt(pos) === 32) pos++
105
+ if (pos === s1 || pos >= length) continue
106
+
107
+ // col 2: offset (lower hex)
108
+ const offStart = pos
109
+ let offset = 0
110
+ while (pos < length && isHexChar(line.charCodeAt(pos))) {
111
+ offset = offset * 16 + hexDigit(line.charCodeAt(pos++))
112
+ }
113
+ if (pos === offStart) continue
114
+
115
+ // spaces between col 2 and col 3
116
+ const s2 = pos
117
+ while (pos < length && line.charCodeAt(pos) === 32) pos++
118
+ if (pos === s2 || pos >= length) continue
119
+
120
+ // col 3: size (lower hex)
121
+ const sizeStart = pos
122
+ let size = 0
123
+ while (pos < length && isHexChar(line.charCodeAt(pos))) {
124
+ size = size * 16 + hexDigit(line.charCodeAt(pos++))
125
+ }
126
+ if (pos === sizeStart || pos >= length) continue
127
+
128
+ // indent spaces
129
+ const indentStart = pos
130
+ while (pos < length && line.charCodeAt(pos) === 32) pos++
131
+ const indent = pos - indentStart
132
+ if (indent === 0 || pos >= length) continue
133
+
134
+ if (indent === 17) {
135
+ if (previous) {
136
+ const first = line.charCodeAt(pos)
137
+ if (first > 32 && !hasLineBreak(line, pos)) {
138
+ previous.symbols.push({
139
+ name: line.slice(pos),
140
+ address,
141
+ size,
142
+ })
143
+ }
144
+ }
78
145
  continue
79
146
  }
80
- if (kind === 'DATA') {
81
- const top = /^ *([0-9a-f]+) +[0-9a-f]+ +([0-9a-f]+) (\S+)$/.exec(line)
82
- if (top) {
83
- segment = top[3]!
84
- segments.set(segment, { address: hex(top[1]!), size: parseInt(top[2]!, 16) })
85
- continue
147
+
148
+ if (indent === 1) {
149
+ // single token with no whitespace for output / top-level DATA segment
150
+ let hasWhitespace = false
151
+ for (let i = pos; i < length; i++) {
152
+ const ch = line.charCodeAt(i)
153
+ if (ch <= 32) {
154
+ hasWhitespace = true
155
+ break
156
+ }
157
+ }
158
+ if (hasWhitespace) continue
159
+ if (address < 0 && leadingSpaces > 0) {
160
+ kind = line.slice(pos)
161
+ } else if (kind === 'DATA' && address >= 0) {
162
+ segment = line.slice(pos)
163
+ segments.set(segment, { address, size })
86
164
  }
165
+ continue
87
166
  }
88
- if (kind !== 'DATA' && kind !== 'CODE') continue
89
- const input = /^ *([0-9a-f]+|-) +([0-9a-f]+) +([0-9a-f]+) {9}([/<].*)$/.exec(line)
90
- if (!input) continue
91
- const name = input[4]!
92
- const split = /^(.*?):\((.*)\)$/s.exec(name)
93
- const path = split ? split[1]! : name
94
- const member = /\(([^()]+)\)$/.exec(path)
95
- previous = {
96
- inputFile: path,
97
- segment,
98
- address: hex(input[1]!),
99
- offset: parseInt(input[2]!, 16),
100
- size: parseInt(input[3]!, 16),
101
- object: (member ? member[1]! : basename(path)).replace(/^lib\w*?__/, ''),
102
- section: split ? split[2]! : '',
103
- symbols: [],
167
+
168
+ if (indent === 9 && (kind === 'DATA' || kind === 'CODE')) {
169
+ const first = line.charCodeAt(pos)
170
+ if (first !== 47 && first !== 60) continue
171
+ if (hasLineBreak(line, pos)) continue
172
+ const name = line.slice(pos)
173
+ let filePath = name
174
+ let section = ''
175
+ if (name.charCodeAt(name.length - 1) === 41) {
176
+ const splitAt = name.indexOf(':(')
177
+ if (splitAt >= 0) {
178
+ filePath = name.slice(0, splitAt)
179
+ section = name.slice(splitAt + 2, -1)
180
+ }
181
+ }
182
+ let objectName: string | undefined
183
+ if (filePath.charCodeAt(filePath.length - 1) === 41) {
184
+ const open = filePath.lastIndexOf('(')
185
+ if (open >= 0 && open + 1 < filePath.length - 1) {
186
+ const close = filePath.indexOf(')', open + 1)
187
+ if (close === filePath.length - 1) {
188
+ objectName = filePath.slice(open + 1, -1)
189
+ }
190
+ }
191
+ }
192
+ if (objectName === undefined) objectName = basename(filePath)
193
+ if (objectName.startsWith('lib') && objectName.includes('__')) {
194
+ objectName = objectName.replace(/^lib\w*?__/, '')
195
+ }
196
+ previous = {
197
+ inputFile: filePath,
198
+ segment,
199
+ address,
200
+ offset,
201
+ size,
202
+ object: objectName,
203
+ section,
204
+ symbols: [],
205
+ }
206
+ ;(kind === 'DATA' ? data : code).push(previous)
104
207
  }
105
- ;(kind === 'DATA' ? data : code).push(previous)
106
208
  }
107
209
  return { segments, data: partition ? data.flatMap(symbolPieces) : data, code }
108
210
  }
@@ -460,6 +460,9 @@ function parseWasmObjectBytes(
460
460
  isDist: boolean,
461
461
  symbolFlagOffsets?: number[],
462
462
  ): ParsedObject {
463
+ const rawBuf = Buffer.isBuffer(buf)
464
+ ? buf
465
+ : Buffer.from(buf.buffer, buf.byteOffset, buf.byteLength)
463
466
  let pos = 8
464
467
  let numImportFuncs = 0
465
468
  let importsDlsym = false
@@ -499,10 +502,10 @@ function parseWasmObjectBytes(
499
502
  let [cnt, cp] = readULEB(buf, p)
500
503
  for (let i = 0; i < cnt; i++) {
501
504
  const [mLen, m2] = readULEB(buf, cp)
502
- const mName = Buffer.from(buf.subarray(m2, m2 + mLen)).toString('utf8')
505
+ const mName = rawBuf.toString('utf8', m2, m2 + mLen)
503
506
  cp = m2 + mLen
504
507
  const [fLen, f2] = readULEB(buf, cp)
505
- const fName = Buffer.from(buf.subarray(f2, f2 + fLen)).toString('utf8')
508
+ const fName = rawBuf.toString('utf8', f2, f2 + fLen)
506
509
  cp = f2 + fLen
507
510
  const kind = buf[cp++]!
508
511
  if (kind === 0) {
@@ -582,7 +585,7 @@ function parseWasmObjectBytes(
582
585
  }
583
586
  } else if (id === 0) {
584
587
  const [nLen, np] = readULEB(buf, p)
585
- const name = Buffer.from(buf.subarray(np, np + nLen)).toString('utf8')
588
+ const name = rawBuf.toString('utf8', np, np + nLen)
586
589
  const cp = np + nLen
587
590
  if (name === 'linking') {
588
591
  let [, lp] = readULEB(buf, cp)
@@ -595,7 +598,7 @@ function parseWasmObjectBytes(
595
598
  let cur = p2
596
599
  for (let i = 0; i < cnt; i++) {
597
600
  const [sLen, s2] = readULEB(buf, cur)
598
- segNames.push(Buffer.from(buf.subarray(s2, s2 + sLen)).toString('utf8'))
601
+ segNames.push(rawBuf.toString('utf8', s2, s2 + sLen))
599
602
  cur = s2 + sLen
600
603
  const [, s3] = readULEB(buf, cur)
601
604
  const [fl, s4] = readULEB(buf, s3)
@@ -635,7 +638,7 @@ function parseWasmObjectBytes(
635
638
  let sName = ''
636
639
  if (!isUndef || isExplicitName) {
637
640
  const [sLen, c4] = readULEB(buf, cur)
638
- sName = Buffer.from(buf.subarray(c4, c4 + sLen)).toString('utf8')
641
+ sName = rawBuf.toString('utf8', c4, c4 + sLen)
639
642
  cur = c4 + sLen
640
643
  } else if (kind === 0) {
641
644
  sName = importFuncNames[el] ?? ''
@@ -652,7 +655,7 @@ function parseWasmObjectBytes(
652
655
  symNames.push(sName)
653
656
  } else if (kind === 1) {
654
657
  const [sLen, c3] = readULEB(buf, cur)
655
- const sName = Buffer.from(buf.subarray(c3, c3 + sLen)).toString('utf8')
658
+ const sName = rawBuf.toString('utf8', c3, c3 + sLen)
656
659
  cur = c3 + sLen
657
660
  if (!isUndef) {
658
661
  const [segIdx, c4] = readULEB(buf, cur)
@@ -771,9 +774,21 @@ function parseWasmObjectBytes(
771
774
  const offInSeg = ro - (sPayloadStarts[lo] ?? 0)
772
775
  let ownerSymIdx = -1
773
776
  const topList = segTopSyms.get(lo)
774
- if (topList) {
775
- const owner = topList.find((t) => offInSeg >= t.off && offInSeg < t.end)
776
- if (owner) ownerSymIdx = owner.symIdx
777
+ if (topList && topList.length > 0) {
778
+ let tLo = 0
779
+ let tHi = topList.length - 1
780
+ while (tLo <= tHi) {
781
+ const tMid = (tLo + tHi) >> 1
782
+ const top = topList[tMid]!
783
+ if (offInSeg < top.off) {
784
+ tHi = tMid - 1
785
+ } else if (offInSeg >= top.end) {
786
+ tLo = tMid + 1
787
+ } else {
788
+ ownerSymIdx = top.symIdx
789
+ break
790
+ }
791
+ }
777
792
  }
778
793
  if (ownerSymIdx !== -1) {
779
794
  let list = symDataRelocs.get(ownerSymIdx)
@@ -1096,58 +1111,160 @@ export function guestWasmSymbols(bytes: Uint8Array): {
1096
1111
  defined: string[]
1097
1112
  weakDefined: string[]
1098
1113
  } {
1099
- const object = parseWasmObjectBytes('', bytes, false)
1114
+ const rawBuf = Buffer.isBuffer(bytes)
1115
+ ? bytes
1116
+ : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)
1117
+ let cur = 8
1118
+ const readU = (): number => {
1119
+ const b0 = bytes[cur++]!
1120
+ if (b0 < 128) return b0
1121
+ let res = b0 & 0x7f
1122
+ let shift = 7
1123
+ while (true) {
1124
+ const byte = bytes[cur++]!
1125
+ res |= (byte & 0x7f) << shift
1126
+ if ((byte & 0x80) === 0) return res
1127
+ shift += 7
1128
+ }
1129
+ }
1130
+ let importsDlsym = false
1131
+ const importFuncNames: string[] = []
1132
+ const importFuncModules: string[] = []
1133
+ const importGlobalNames: string[] = []
1134
+ const importTableNames: string[] = []
1135
+ const importEventNames: string[] = []
1136
+ const undefinedSyms = new Set<string>()
1137
+ const tags = new Set<string>()
1138
+ const globals = new Set<string>()
1139
+ const hostImports = new Set<string>()
1140
+ const weakUndefined = new Set<string>()
1141
+ const defined = new Set<string>()
1142
+ const weakDefined = new Set<string>()
1143
+
1144
+ while (cur < bytes.length) {
1145
+ const id = bytes[cur++]!
1146
+ const sz = readU()
1147
+ const end = cur + sz
1148
+
1149
+ if (id === 2) {
1150
+ const cnt = readU()
1151
+ for (let i = 0; i < cnt; i++) {
1152
+ const mLen = readU()
1153
+ const mName = rawBuf.toString('utf8', cur, cur + mLen)
1154
+ cur += mLen
1155
+ const fLen = readU()
1156
+ const fName = rawBuf.toString('utf8', cur, cur + fLen)
1157
+ cur += fLen
1158
+ const kind = bytes[cur++]!
1159
+ if (kind === 0) {
1160
+ readU()
1161
+ importFuncNames.push(fName)
1162
+ importFuncModules.push(mName)
1163
+ if (mName === 'env' && fName === 'dlsym') importsDlsym = true
1164
+ } else if (kind === 1) {
1165
+ cur++
1166
+ const fl = readU()
1167
+ readU()
1168
+ if (fl & 1) readU()
1169
+ importTableNames.push(fName)
1170
+ } else if (kind === 2) {
1171
+ const fl = readU()
1172
+ readU()
1173
+ if (fl & 1) readU()
1174
+ } else if (kind === 3) {
1175
+ cur += 2
1176
+ importGlobalNames.push(fName)
1177
+ } else if (kind === 4) {
1178
+ cur++
1179
+ readU()
1180
+ importEventNames.push(fName)
1181
+ }
1182
+ }
1183
+ } else if (id === 0) {
1184
+ const nLen = readU()
1185
+ if (
1186
+ nLen === 7 &&
1187
+ bytes[cur] === 0x6c &&
1188
+ rawBuf.toString('utf8', cur, cur + nLen) === 'linking'
1189
+ ) {
1190
+ cur += nLen
1191
+ readU() // version
1192
+ while (cur < end) {
1193
+ const subId = bytes[cur++]!
1194
+ const subLen = readU()
1195
+ const subEnd = cur + subLen
1196
+ if (subId === 8) {
1197
+ const cnt = readU()
1198
+ for (let i = 0; i < cnt; i++) {
1199
+ const kind = bytes[cur++]!
1200
+ const fl = readU()
1201
+ const isUndef = (fl & 0x10) !== 0
1202
+ const isExplicitName = (fl & 0x40) !== 0
1203
+ let sName = ''
1204
+ if (kind === 0 || kind === 2 || kind === 4 || kind === 5) {
1205
+ const el = readU()
1206
+ if (!isUndef || isExplicitName) {
1207
+ const sLen = readU()
1208
+ sName = rawBuf.toString('utf8', cur, cur + sLen)
1209
+ cur += sLen
1210
+ } else if (kind === 0) {
1211
+ sName = importFuncNames[el] ?? ''
1212
+ } else if (kind === 2) {
1213
+ sName = importGlobalNames[el] ?? ''
1214
+ } else if (kind === 4) {
1215
+ sName = importEventNames[el] ?? ''
1216
+ } else if (kind === 5) {
1217
+ sName = importTableNames[el] ?? ''
1218
+ }
1219
+ if (isUndef && kind === 0 && importFuncModules[el] !== 'env')
1220
+ hostImports.add(sName)
1221
+ } else if (kind === 1) {
1222
+ const sLen = readU()
1223
+ sName = rawBuf.toString('utf8', cur, cur + sLen)
1224
+ cur += sLen
1225
+ if (!isUndef) {
1226
+ readU()
1227
+ readU()
1228
+ readU()
1229
+ }
1230
+ } else if (kind === 3) {
1231
+ readU()
1232
+ }
1233
+ if (sName) {
1234
+ if (isUndef) {
1235
+ undefinedSyms.add(sName)
1236
+ if ((fl & 0x13) === 0x11) weakUndefined.add(sName)
1237
+ } else if ((fl & 3) !== 2) {
1238
+ defined.add(sName)
1239
+ if ((fl & 0x13) === 0x01) weakDefined.add(sName)
1240
+ }
1241
+ if (kind === 4) tags.add(sName)
1242
+ else if (kind === 2 || kind === 5) globals.add(sName)
1243
+ }
1244
+ }
1245
+ break
1246
+ }
1247
+ cur = subEnd
1248
+ }
1249
+ break
1250
+ }
1251
+ }
1252
+ cur = end
1253
+ }
1254
+
1100
1255
  return {
1101
- importsDlsym: object.importsDlsym,
1102
- undefined: [
1103
- ...new Set(
1104
- object.symNames.filter(
1105
- (name, index) => name && (object.symFlags[index]! & 0x10) !== 0,
1106
- ),
1107
- ),
1108
- ],
1256
+ importsDlsym,
1257
+ undefined: [...undefinedSyms],
1109
1258
  // a wasm tag is one module entity unified by name across the link, as the
1110
1259
  // main module owns it for dynamic images, so no image scope applies.
1111
- tags: [
1112
- ...new Set(
1113
- object.symNames.filter((name, index) => name && object.symKind[index] === 4),
1114
- ),
1115
- ],
1116
- globals: [
1117
- ...new Set(
1118
- object.symNames.filter(
1119
- (name, index) =>
1120
- name && (object.symKind[index] === 2 || object.symKind[index] === 5),
1121
- ),
1122
- ),
1123
- ],
1260
+ tags: [...tags],
1261
+ globals: [...globals],
1124
1262
  // an undefined function imported from a module other than env is supplied
1125
1263
  // by the host, whatever linker symbol names it.
1126
- hostImports: [...object.hostImportNames],
1127
- weakUndefined: [
1128
- ...new Set(
1129
- object.symNames.filter(
1130
- (name, index) => name && (object.symFlags[index]! & 0x13) === 0x11,
1131
- ),
1132
- ),
1133
- ],
1134
- defined: [
1135
- ...new Set(
1136
- object.symNames.filter(
1137
- (name, index) =>
1138
- name &&
1139
- (object.symFlags[index]! & 0x10) === 0 &&
1140
- (object.symFlags[index]! & 3) !== 2,
1141
- ),
1142
- ),
1143
- ],
1144
- weakDefined: [
1145
- ...new Set(
1146
- object.symNames.filter(
1147
- (name, index) => name && (object.symFlags[index]! & 0x13) === 0x01,
1148
- ),
1149
- ),
1150
- ],
1264
+ hostImports: [...hostImports],
1265
+ weakUndefined: [...weakUndefined],
1266
+ defined: [...defined],
1267
+ weakDefined: [...weakDefined],
1151
1268
  }
1152
1269
  }
1153
1270
 
@@ -1955,9 +2072,12 @@ export function pruneSwiftWasmLink(
1955
2072
  }
1956
2073
  }
1957
2074
 
2075
+ const checkedProtocolTriggers = new Set<string>()
1958
2076
  const checkProtocolTrigger = (rawSymName: string | undefined) => {
1959
2077
  if (!rawSymName) return
1960
2078
  const sName = rawSymName.startsWith('.Lgot.') ? rawSymName.slice(6) : rawSymName
2079
+ if (checkedProtocolTriggers.has(sName)) return
2080
+ checkedProtocolTriggers.add(sName)
1961
2081
  if (knownProtocols.has(sName)) {
1962
2082
  markProtocolLive(sName)
1963
2083
  return
@@ -1991,6 +2111,57 @@ export function pruneSwiftWasmLink(
1991
2111
  }
1992
2112
  }
1993
2113
 
2114
+ const typeIndexes = new Map<
2115
+ ParsedObject,
2116
+ {
2117
+ mnByPrefix: Map<string, number[]>
2118
+ objcRegByPrefix: Map<string, number[]>
2119
+ objcClassByPrefix: Map<string, string[]>
2120
+ minPrefLen: number
2121
+ maxPrefLen: number
2122
+ }
2123
+ >()
2124
+ const getTypeIndex = (obj: ParsedObject) => {
2125
+ let idx = typeIndexes.get(obj)
2126
+ if (idx) return idx
2127
+ const mnByPrefix = new Map<string, number[]>()
2128
+ for (let i = 0; i < obj.symNames.length; i++) {
2129
+ const name = obj.symNames[i]!
2130
+ if (
2131
+ (obj.symFlags[i]! & 0x10) === 0 &&
2132
+ name.startsWith('$s') &&
2133
+ name.endsWith('Mn')
2134
+ ) {
2135
+ const pref = name.slice(0, -2)
2136
+ let list = mnByPrefix.get(pref)
2137
+ if (!list) mnByPrefix.set(pref, (list = []))
2138
+ list.push(i)
2139
+ }
2140
+ }
2141
+ const objcRegByPrefix = new Map<string, number[]>()
2142
+ for (const [lf, p] of obj.objcRegisterFuncToTypePrefix) {
2143
+ let list = objcRegByPrefix.get(p)
2144
+ if (!list) objcRegByPrefix.set(p, (list = []))
2145
+ list.push(lf)
2146
+ }
2147
+ const objcClassByPrefix = new Map<string, string[]>()
2148
+ for (const [classSym, p] of obj.objcClassSymToTypePrefix) {
2149
+ if (!classSym.startsWith('._OBJC_CLASS_')) continue
2150
+ let list = objcClassByPrefix.get(p)
2151
+ if (!list) objcClassByPrefix.set(p, (list = []))
2152
+ list.push(classSym.slice('._OBJC_CLASS_'.length))
2153
+ }
2154
+ let minPrefLen = Infinity
2155
+ let maxPrefLen = 0
2156
+ for (const p of obj.typePrefixesInObj) {
2157
+ if (p.length < minPrefLen) minPrefLen = p.length
2158
+ if (p.length > maxPrefLen) maxPrefLen = p.length
2159
+ }
2160
+ idx = { mnByPrefix, objcRegByPrefix, objcClassByPrefix, minPrefLen, maxPrefLen }
2161
+ typeIndexes.set(obj, idx)
2162
+ return idx
2163
+ }
2164
+
1994
2165
  const markTypeLive = (obj: ParsedObject, pref: string) => {
1995
2166
  const set = liveTypes.get(obj)!
1996
2167
  if (set.has(pref)) return
@@ -2008,22 +2179,10 @@ export function pruneSwiftWasmLink(
2008
2179
  if (obj.typePrefixesInObj.has(outer)) markTypeLive(obj, outer)
2009
2180
  }
2010
2181
 
2011
- const mnName = `${pref}Mn`
2012
- for (let i = 0; i < obj.symNames.length; i++) {
2013
- if (obj.symNames[i] === mnName && (obj.symFlags[i]! & 0x10) === 0) {
2014
- activateSymbolInObj(obj, i)
2015
- }
2016
- }
2017
- for (const [lf, p] of obj.objcRegisterFuncToTypePrefix) {
2018
- if (p === pref) {
2019
- markFuncLive(obj, lf)
2020
- }
2021
- }
2022
- for (const [classSym, p] of obj.objcClassSymToTypePrefix) {
2023
- if (p === pref && classSym.startsWith('._OBJC_CLASS_')) {
2024
- markObjCClassLive(classSym.slice('._OBJC_CLASS_'.length))
2025
- }
2026
- }
2182
+ const idx = getTypeIndex(obj)
2183
+ for (const i of idx.mnByPrefix.get(pref) ?? []) activateSymbolInObj(obj, i)
2184
+ for (const lf of idx.objcRegByPrefix.get(pref) ?? []) markFuncLive(obj, lf)
2185
+ for (const cls of idx.objcClassByPrefix.get(pref) ?? []) markObjCClassLive(cls)
2027
2186
  }
2028
2187
 
2029
2188
  const checkTypeTrigger = (obj: ParsedObject, rawSymName: string) => {
@@ -2050,13 +2209,15 @@ export function pruneSwiftWasmLink(
2050
2209
  }
2051
2210
  }
2052
2211
  if (
2053
- obj.file.endsWith('libswiftCore__Swift.o') ||
2054
- obj.file.endsWith('libswift_Concurrency___Concurrency.o')
2212
+ obj.typePrefixesInObj.size > 0 &&
2213
+ (obj.file.endsWith('libswiftCore__Swift.o') ||
2214
+ obj.file.endsWith('libswift_Concurrency___Concurrency.o'))
2055
2215
  ) {
2056
- for (const pref of obj.typePrefixesInObj) {
2057
- if (sName.startsWith(pref)) {
2058
- markTypeLive(obj, pref)
2059
- }
2216
+ const { minPrefLen, maxPrefLen } = getTypeIndex(obj)
2217
+ const hi = Math.min(sName.length, maxPrefLen)
2218
+ for (let n = minPrefLen; n <= hi; n++) {
2219
+ const cand = sName.slice(0, n)
2220
+ if (obj.typePrefixesInObj.has(cand)) markTypeLive(obj, cand)
2060
2221
  }
2061
2222
  }
2062
2223
  }
@@ -2160,7 +2321,8 @@ export function pruneSwiftWasmLink(
2160
2321
  // names it, and ld64 never dead-strips an objective-c class, so a class
2161
2322
  // with +load (`_c_<class>__load`) is a root like an initializer.
2162
2323
  for (const name of obj.symNames) {
2163
- const loaded = name?.match(/^_c_(\w+)__load$/)?.[1]
2324
+ if (!name || !name.startsWith('_c_') || !name.endsWith('__load')) continue
2325
+ const loaded = name.match(/^_c_(\w+)__load$/)?.[1]
2164
2326
  if (!loaded) continue
2165
2327
  const classSym = obj.symNames.indexOf(`._OBJC_CLASS_${loaded}`)
2166
2328
  if (classSym !== -1 && (obj.symFlags[classSym]! & 0x10) === 0)
@@ -2355,14 +2517,30 @@ export function pruneSwiftWasmLink(
2355
2517
  const keepObjCClassesNamedInSegment = (obj: ParsedObject, segIdx: number) => {
2356
2518
  const range = obj.segPayloadRanges[segIdx]
2357
2519
  if (!range) return
2520
+ const [rangeStart, rangeEnd] = range
2521
+ if (rangeEnd - rangeStart < 4) return
2358
2522
  let bytes = objectBytes.get(obj)
2359
2523
  if (!bytes) objectBytes.set(obj, (bytes = fs.readFileSync(obj.file)))
2360
- const text = bytes.toString('latin1', range[0], range[1])
2361
- for (const match of text.matchAll(/So(\d+)/g)) {
2362
- const start = match.index + 2 + match[1]!.length
2363
- const length = Number(match[1])
2364
- if (text[start + length] !== 'C') continue
2365
- const symbol = `._OBJC_CLASS_${text.slice(start, start + length)}`
2524
+ let pos: number = rangeStart
2525
+ while (pos < rangeEnd - 3) {
2526
+ const at: number = bytes.indexOf('So', pos)
2527
+ if (at < 0 || at >= rangeEnd - 2) break
2528
+ let digitEnd: number = at + 2
2529
+ let length = 0
2530
+ while (digitEnd < rangeEnd) {
2531
+ const ch = bytes[digitEnd]!
2532
+ if (ch < 0x30 || ch > 0x39) break
2533
+ length = length * 10 + (ch - 0x30)
2534
+ digitEnd++
2535
+ }
2536
+ if (digitEnd === at + 2) {
2537
+ pos = at + 2
2538
+ continue
2539
+ }
2540
+ pos = digitEnd
2541
+ const nameEnd = digitEnd + length
2542
+ if (nameEnd >= rangeEnd || bytes[nameEnd] !== 0x43) continue
2543
+ const symbol = `._OBJC_CLASS_${bytes.toString('latin1', digitEnd, nameEnd)}`
2366
2544
  for (const provider of lazyProviders.get(symbol) ?? []) {
2367
2545
  eagerDynamicObjects.add(provider)
2368
2546
  extractObject(provider)
@@ -56,7 +56,7 @@ export function isSwiftGuestImageManifest(
56
56
  value.length <= 4096 &&
57
57
  !value.includes('\0')
58
58
  const name = (value: unknown): value is string =>
59
- text(value) && new TextEncoder().encode(value).length <= 16
59
+ text(value) && Buffer.byteLength(value, 'utf8') <= 16
60
60
  const version = (value: unknown): boolean =>
61
61
  isRecord(value) &&
62
62
  uint(value.major, 65535) &&