peachsim 0.1.2589 → 0.1.2590

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 (137) 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 +1172 -446
  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-65FNEARV.mjs} +3 -3
  27. package/dist-lib/esm-chunks/{chunk-EAIFG4MQ.mjs → chunk-2GUB5OLB.mjs} +3 -3
  28. package/dist-lib/esm-chunks/{chunk-FQQ2DMXS.mjs → chunk-462CWTWI.mjs} +1 -1
  29. package/dist-lib/esm-chunks/{chunk-L3OSVIGA.mjs → chunk-4VCRUQ36.mjs} +1178 -452
  30. package/dist-lib/esm-chunks/{chunk-5QWQDXXD.mjs → chunk-5GNGHM5B.mjs} +1 -1
  31. package/dist-lib/esm-chunks/{chunk-JLOJZF2Y.mjs → chunk-ADKUIQXX.mjs} +1 -1
  32. package/dist-lib/esm-chunks/{chunk-M4KUWGU3.mjs → chunk-BZS5SIYK.mjs} +1 -1
  33. package/dist-lib/esm-chunks/{chunk-47K4QPVA.mjs → chunk-EFKDUMB5.mjs} +1 -1
  34. package/dist-lib/esm-chunks/{chunk-I22LAVD5.mjs → chunk-GBD26EBV.mjs} +1 -1
  35. package/dist-lib/esm-chunks/{chunk-CRIYVOMB.mjs → chunk-HZTK7TP5.mjs} +1 -1
  36. package/dist-lib/esm-chunks/{chunk-5EFD2VBY.mjs → chunk-JG4AANWA.mjs} +1 -1
  37. package/dist-lib/esm-chunks/{chunk-FMWEI6OI.mjs → chunk-N2II5SRJ.mjs} +4 -4
  38. package/dist-lib/esm-chunks/{chunk-33SCXU3Y.mjs → chunk-NZY4GOG5.mjs} +1 -1
  39. package/dist-lib/esm-chunks/{chunk-KFHCRS5K.mjs → chunk-OZJ66CXY.mjs} +2 -2
  40. package/dist-lib/esm-chunks/{chunk-464CBA5U.mjs → chunk-RGC2JNCL.mjs} +1 -1
  41. package/dist-lib/esm-chunks/{chunk-57FFWABF.mjs → chunk-SLPXTLBK.mjs} +5 -5
  42. package/dist-lib/esm-chunks/{chunk-Y4PV4HZB.mjs → chunk-SRFKOU76.mjs} +1 -1
  43. package/dist-lib/esm-chunks/{chunk-3VNUC4DT.mjs → chunk-V5TTEXYO.mjs} +1 -1
  44. package/dist-lib/esm-chunks/{chunk-MKSOJCRT.mjs → chunk-WPDY5IAD.mjs} +1 -1
  45. package/dist-lib/esm-chunks/{chunk-WQ73RKJY.mjs → chunk-WYDRQRJL.mjs} +5 -5
  46. package/dist-lib/esm-chunks/{chunk-BSAAJR25.mjs → chunk-X2EKFONU.mjs} +1 -1
  47. package/dist-lib/esm-chunks/{chunk-PUBH3EXI.mjs → chunk-XE3VHAU2.mjs} +1 -1
  48. package/dist-lib/esm-chunks/{chunk-XTKKROVU.mjs → chunk-YPPH6F77.mjs} +1 -1
  49. package/dist-lib/esm-chunks/{metro-layout-parser-T5QIJTEV.mjs → metro-layout-parser-CRI3GMCM.mjs} +3 -3
  50. package/dist-lib/esm-chunks/{swift-package-prebuilt-AWTZHPDM.mjs → swift-package-prebuilt-5XDZ2HZZ.mjs} +10 -10
  51. package/dist-lib/esm-chunks/{swift-sdk-app-module-MGTJTVKU.mjs → swift-sdk-app-module-6H3G6AXD.mjs} +14 -14
  52. package/dist-lib/esm-chunks/{swift-sdk-image-JABGF3ZR.mjs → swift-sdk-image-WAC7GG2R.mjs} +14 -14
  53. package/dist-lib/esm-chunks/{swift-sdk-image-producer-RJRJ3EFU.mjs → swift-sdk-image-producer-B3L27QTG.mjs} +4 -4
  54. package/dist-lib/expo-swift-bundle.cjs +1171 -445
  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 +1200 -469
  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 +1202 -471
  93. package/dist-lib/swift-compile.cjs +1168 -442
  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 +2 -2
  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 +255 -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-sdk-image-map.ts +137 -35
  136. package/src/swift-wasm-prune.ts +263 -85
  137. package/src/swiftGuestImageContract.ts +1 -1
@@ -88,6 +88,7 @@ export interface GuestLinkOptions {
88
88
  readonly resolvePlan?: (
89
89
  selection: readonly GuestLinkSelection[],
90
90
  plan: GuestLinkPlan,
91
+ readSelected?: (item: GuestLinkSelection) => Uint8Array,
91
92
  ) => GuestLinkPlan
92
93
  }
93
94
 
@@ -279,6 +280,7 @@ export function selectedGuestLinkInputs(
279
280
  args: readonly string[],
280
281
  whyExtract: string,
281
282
  linkTrace: string,
283
+ archiveCache?: Map<string, ReturnType<typeof readArMembers>>,
282
284
  ): GuestLinkSelection[] {
283
285
  const rows = whyExtract.trimEnd().split('\n')
284
286
  if (rows.shift() !== 'reference\textracted\tsymbol')
@@ -294,8 +296,38 @@ export function selectedGuestLinkInputs(
294
296
  for (const file of extracted)
295
297
  if (!traced.has(file))
296
298
  throw new Error(`extracted guest input is absent from link trace ${file}`)
299
+ const foundOccurrences = occurrences(plan, args)
300
+ const archivePaths = new Set<string>()
301
+ for (const { artifact } of foundOccurrences)
302
+ if (artifact.kind === 'archive') archivePaths.add(artifact.path)
303
+ const tracedMembers = new Map<string, string[]>()
304
+ if (archivePaths.size > 0) {
305
+ const archivePrefixes = [...archivePaths].map(
306
+ (archivePath) => [archivePath, `${archivePath}(`] as const,
307
+ )
308
+ for (const item of traced) {
309
+ if (!item.endsWith(')')) continue
310
+ const open = item.lastIndexOf('(')
311
+ if (open > 0) {
312
+ const archivePath = item.slice(0, open)
313
+ if (archivePaths.has(archivePath)) {
314
+ let list = tracedMembers.get(archivePath)
315
+ if (!list) tracedMembers.set(archivePath, (list = []))
316
+ list.push(item.slice(open + 1, -1))
317
+ continue
318
+ }
319
+ }
320
+ for (const [archivePath, prefix] of archivePrefixes) {
321
+ if (item.startsWith(prefix)) {
322
+ let list = tracedMembers.get(archivePath)
323
+ if (!list) tracedMembers.set(archivePath, (list = []))
324
+ list.push(item.slice(prefix.length, -1))
325
+ }
326
+ }
327
+ }
328
+ }
297
329
  const result: GuestLinkSelection[] = []
298
- for (const { artifact, lazy, whole } of occurrences(plan, args)) {
330
+ for (const { artifact, lazy, whole } of foundOccurrences) {
299
331
  if (artifact.kind === 'object') {
300
332
  if (!lazy && !traced.has(artifact.path))
301
333
  throw new Error(`eager guest input is absent from link trace ${artifact.path}`)
@@ -303,17 +335,21 @@ export function selectedGuestLinkInputs(
303
335
  result.push({ artifact: artifact.path, member: null })
304
336
  continue
305
337
  }
306
- const prefix = `${artifact.path}(`
307
- const names = [...traced]
308
- .filter((item) => item.startsWith(prefix) && item.endsWith(')'))
309
- .map((item) => item.slice(prefix.length, -1))
310
- const members = readArMembers(fs.readFileSync(artifact.path))
338
+ const names = tracedMembers.get(artifact.path) ?? []
339
+ const nameSet = new Set(names)
340
+ let members = archiveCache?.get(artifact.path)
341
+ if (!members) {
342
+ members = readArMembers(fs.readFileSync(artifact.path))
343
+ archiveCache?.set(artifact.path, members)
344
+ }
345
+ const memberCounts = new Map<string, number>()
311
346
  for (const member of members) {
312
- if (whole || names.includes(member.name))
347
+ memberCounts.set(member.name, (memberCounts.get(member.name) ?? 0) + 1)
348
+ if (whole || nameSet.has(member.name))
313
349
  result.push({ artifact: artifact.path, member: member.name })
314
350
  }
315
351
  for (const name of names)
316
- if (members.filter((member) => member.name === name).length !== 1)
352
+ if (memberCounts.get(name) !== 1)
317
353
  throw new Error(
318
354
  `ambiguous or unsupported archive member ${artifact.path}(${name})`,
319
355
  )
@@ -402,14 +438,25 @@ export function linkGuestImages(
402
438
  }
403
439
  const archives = new Map<string, ReturnType<typeof readArMembers>>()
404
440
  const memberBytes = new Map<string, Map<string, Buffer>>()
441
+ const objectBytes = new Map<string, Buffer>()
405
442
  const artifacts = new Map(plan.artifacts.map((artifact) => [artifact.path, artifact]))
406
443
  const providerSymbol = '__peach_dlsym_from_image'
407
444
  const readSelected = (item: GuestLinkSelection): Buffer => {
408
- if (item.member === null) return fs.readFileSync(item.artifact)
445
+ if (item.member === null) {
446
+ let bytes = objectBytes.get(item.artifact)
447
+ if (!bytes) {
448
+ bytes = fs.readFileSync(item.artifact)
449
+ objectBytes.set(item.artifact, bytes)
450
+ }
451
+ return bytes
452
+ }
409
453
  let byName = memberBytes.get(item.artifact)
410
454
  if (!byName) {
411
- const members = readArMembers(fs.readFileSync(item.artifact))
412
- archives.set(item.artifact, members)
455
+ let members = archives.get(item.artifact)
456
+ if (!members) {
457
+ members = readArMembers(fs.readFileSync(item.artifact))
458
+ archives.set(item.artifact, members)
459
+ }
413
460
  byName = new Map()
414
461
  for (const member of members)
415
462
  if (!byName.has(member.name))
@@ -433,7 +480,13 @@ export function linkGuestImages(
433
480
  `--why-extract=${whyPath}`,
434
481
  `--Map=${mapPath}`,
435
482
  ])
436
- return selectedGuestLinkInputs(plan, args, fs.readFileSync(whyPath, 'utf8'), trace)
483
+ return selectedGuestLinkInputs(
484
+ plan,
485
+ args,
486
+ fs.readFileSync(whyPath, 'utf8'),
487
+ trace,
488
+ archives,
489
+ )
437
490
  }
438
491
  const needsProvider = (item: GuestLinkSelection) => {
439
492
  if (!artifacts.get(item.artifact)!.instrumentDynamicLookups) return false
@@ -479,7 +532,7 @@ export function linkGuestImages(
479
532
  ),
480
533
  ])
481
534
  const originalOwnership = identity(plan)
482
- const resolved = options.resolvePlan(selected, plan)
535
+ const resolved = options.resolvePlan(selected, plan, readSelected)
483
536
  if (identity(resolved) !== originalOwnership)
484
537
  throw new Error('resolved guest scopes changed input ownership')
485
538
  plan = resolved
@@ -544,6 +597,7 @@ export function linkGuestImages(
544
597
  }
545
598
  // the final link adds generated inputs after this layout and changes no other order.
546
599
  let discoveryRegions: number[] | undefined
600
+ let liveSymbols: Set<string>
547
601
  if (discoveryProjection && options.projection) {
548
602
  const owners = new Map([[discoveryProjection, options.projection.image]])
549
603
  for (const item of selected)
@@ -552,10 +606,15 @@ export function linkGuestImages(
552
606
  selectedGuestInputImage(artifacts.get(item.artifact)!, item.member),
553
607
  )
554
608
  const wasm = fs.readFileSync(discovery)
609
+ liveSymbols = new Set<string>()
555
610
  discoveryRegions = guestProjectionRegionCounts(
556
611
  plan.images.length,
557
- guestLinkedSpans(guestLinkLines(mapPath), wasm, wasm, owners),
612
+ guestLinkedSpans(guestLinkLines(mapPath), wasm, wasm, owners, undefined, false, {
613
+ collectSymbols: liveSymbols,
614
+ }),
558
615
  )
616
+ } else {
617
+ liveSymbols = linkedMapSymbols(guestLinkLines(mapPath))
559
618
  }
560
619
  const ledger = createGuestImageLedger({
561
620
  images: plan.images.map((image) => ({
@@ -564,7 +623,7 @@ export function linkGuestImages(
564
623
  })),
565
624
  callers: plan.callers,
566
625
  symbols: plan.symbols,
567
- liveSymbols: linkedMapSymbols(guestLinkLines(mapPath)),
626
+ liveSymbols,
568
627
  commandLineSymbols: commandLineSymbols(args),
569
628
  directBindings: plan.directBindings,
570
629
  registerAppTables: sdkCallers !== undefined,
@@ -715,6 +774,7 @@ export function linkGuestImages(
715
774
  plan,
716
775
  selected,
717
776
  projection.identities,
777
+ readSelected,
718
778
  )
719
779
  const images = projection.images.map((image) =>
720
780
  createGuestImageManifest({
@@ -724,6 +784,9 @@ export function linkGuestImages(
724
784
  regions: [],
725
785
  }),
726
786
  )
787
+ objectBytes.clear()
788
+ memberBytes.clear()
789
+ archives.clear()
727
790
  if (!discoveryRegions) throw new Error('guest projection layout object missing')
728
791
  const regionsBefore = discoveryRegions
729
792
  // generated pieces belong to one image each: the lookup tables to the ledger data
@@ -829,10 +892,51 @@ export function linkGuestImages(
829
892
  // every symbol name a linker map lists, which is what the link kept.
830
893
  function linkedMapSymbols(lines: Iterable<string>): Set<string> {
831
894
  const names = new Set<string>()
832
- const symbol = /^ +\S+ +[0-9a-f]+ +[0-9a-f]+ {9,}([^\s/<]\S*)/
833
895
  for (const line of lines) {
834
- const match = symbol.exec(line)
835
- if (match) names.add(match[1]!)
896
+ const length = line.length
897
+ if (length < 15 || line.charCodeAt(0) !== 32) continue
898
+ let pos = 1
899
+ while (pos < length && line.charCodeAt(pos) === 32) pos++
900
+ // col 1: \S+
901
+ const c1 = pos
902
+ while (pos < length) {
903
+ const ch = line.charCodeAt(pos)
904
+ if (ch === 32 || (ch >= 9 && ch <= 13)) break
905
+ pos++
906
+ }
907
+ if (pos === c1 || pos >= length || line.charCodeAt(pos) !== 32) continue
908
+ while (pos < length && line.charCodeAt(pos) === 32) pos++
909
+ // col 2: [0-9a-f]+
910
+ const c2 = pos
911
+ while (pos < length) {
912
+ const ch = line.charCodeAt(pos)
913
+ if (!((ch >= 48 && ch <= 57) || (ch >= 97 && ch <= 102))) break
914
+ pos++
915
+ }
916
+ if (pos === c2 || pos >= length || line.charCodeAt(pos) !== 32) continue
917
+ while (pos < length && line.charCodeAt(pos) === 32) pos++
918
+ // col 3: [0-9a-f]+
919
+ const c3 = pos
920
+ while (pos < length) {
921
+ const ch = line.charCodeAt(pos)
922
+ if (!((ch >= 48 && ch <= 57) || (ch >= 97 && ch <= 102))) break
923
+ pos++
924
+ }
925
+ if (pos === c3 || pos >= length || line.charCodeAt(pos) !== 32) continue
926
+ // {9,} spaces
927
+ const sStart = pos
928
+ while (pos < length && line.charCodeAt(pos) === 32) pos++
929
+ if (pos - sStart < 9 || pos >= length) continue
930
+ const first = line.charCodeAt(pos)
931
+ if (first === 47 || first === 60 || first === 32 || (first >= 9 && first <= 13))
932
+ continue
933
+ let end = pos + 1
934
+ while (end < length) {
935
+ const ch = line.charCodeAt(end)
936
+ if (ch === 32 || (ch >= 9 && ch <= 13)) break
937
+ end++
938
+ }
939
+ names.add(line.slice(pos, end))
836
940
  }
837
941
  return names
838
942
  }
@@ -957,7 +1061,10 @@ export function discoverGuestLinkInputs(options: {
957
1061
  fs.mkdirSync(options.directory, { recursive: true })
958
1062
  const why = path.join(options.directory, 'preselection-why.txt')
959
1063
  const response = path.join(options.directory, 'preselection.rsp')
960
- const output = path.join(options.directory, 'preselection.wasm')
1064
+ const output =
1065
+ process.platform !== 'win32'
1066
+ ? '/dev/null'
1067
+ : path.join(options.directory, 'preselection.wasm')
961
1068
  const flags = [
962
1069
  ...withOutput(args, output, true),
963
1070
  ...options.objects,
@@ -1088,6 +1195,7 @@ export function prepareGuestRuntimeInputs(options: {
1088
1195
  export function selectedGuestSymbolInputs(
1089
1196
  plan: GuestLinkPlan,
1090
1197
  selected: readonly GuestLinkSelection[],
1198
+ readSelected?: (item: GuestLinkSelection) => Uint8Array,
1091
1199
  ): {
1092
1200
  readonly symbols: readonly string[]
1093
1201
  readonly definitions: readonly string[]
@@ -1112,22 +1220,27 @@ export function selectedGuestSymbolInputs(
1112
1220
  if (!artifact) throw new Error(`selected symbol input has no owner ${item.artifact}`)
1113
1221
  let bytes: Uint8Array
1114
1222
  let name = item.artifact
1115
- if (item.member === null) bytes = fs.readFileSync(item.artifact)
1116
- else {
1117
- let members = archives.get(item.artifact)
1118
- if (!members) {
1119
- members = Map.groupBy(
1120
- readArMembers(fs.readFileSync(item.artifact)),
1121
- (member) => member.name,
1122
- )
1123
- archives.set(item.artifact, members)
1223
+ if (item.member === null) {
1224
+ bytes = readSelected ? readSelected(item) : fs.readFileSync(item.artifact)
1225
+ } else {
1226
+ if (readSelected) {
1227
+ bytes = readSelected(item)
1228
+ } else {
1229
+ let members = archives.get(item.artifact)
1230
+ if (!members) {
1231
+ members = Map.groupBy(
1232
+ readArMembers(fs.readFileSync(item.artifact)),
1233
+ (member) => member.name,
1234
+ )
1235
+ archives.set(item.artifact, members)
1236
+ }
1237
+ const matching = members.get(item.member) ?? []
1238
+ if (matching.length !== 1)
1239
+ throw new Error(
1240
+ `ambiguous selected symbol input ${item.artifact}(${item.member})`,
1241
+ )
1242
+ bytes = matching[0]!.bytes
1124
1243
  }
1125
- const matching = members.get(item.member) ?? []
1126
- if (matching.length !== 1)
1127
- throw new Error(
1128
- `ambiguous selected symbol input ${item.artifact}(${item.member})`,
1129
- )
1130
- bytes = matching[0]!.bytes
1131
1244
  name += `(${item.member})`
1132
1245
  }
1133
1246
  if (owners.has(name)) throw new Error(`duplicate selected symbol input ${name}`)
@@ -1140,9 +1253,9 @@ export function selectedGuestSymbolInputs(
1140
1253
  for (const symbol of names.tags) tags.add(symbol)
1141
1254
  for (const symbol of names.defined) {
1142
1255
  definitions.add(symbol)
1143
- const objects = definitionObjects.get(symbol) ?? new Set<string>()
1256
+ let objects = definitionObjects.get(symbol)
1257
+ if (!objects) definitionObjects.set(symbol, (objects = new Set<string>()))
1144
1258
  objects.add(name)
1145
- definitionObjects.set(symbol, objects)
1146
1259
  }
1147
1260
  }
1148
1261
  return {
@@ -1196,7 +1309,10 @@ export function verifyGuestSymbolScopes(options: {
1196
1309
  if (!symbols.length) return { bindings: [], directBindings: [] }
1197
1310
  fs.mkdirSync(options.directory, { recursive: true })
1198
1311
  const response = path.join(options.directory, 'scope-trace.rsp')
1199
- const output = path.join(options.directory, 'scope-trace.wasm')
1312
+ const output =
1313
+ process.platform !== 'win32'
1314
+ ? '/dev/null'
1315
+ : path.join(options.directory, 'scope-trace.wasm')
1200
1316
  const args = [
1201
1317
  ...withOutput(normalizedGuestLinkArgs(options.args), output, true),
1202
1318
  ...options.objects,
@@ -1221,33 +1337,39 @@ export function verifyGuestSymbolScopes(options: {
1221
1337
  physicalModule: boolean
1222
1338
  }
1223
1339
  >()
1224
- const consume = (line: string) => {
1225
- const match = /^(.*): (reference to|definition of) (.*)$/.exec(line)
1226
- if (!match) return
1227
- const [, object, kind, symbol] = match
1228
- if (!requested.has(symbol!)) return
1229
- if (options.hostImports.has(symbol!)) return
1230
- const record = records.get(symbol!) ?? {
1231
- references: new Map<string, number>(),
1232
- providers: new Map<string, number>(),
1233
- physicalModule: false,
1340
+ for (const line of guestLinkLines(tracePath)) {
1341
+ const refSep = line.lastIndexOf(': reference to ')
1342
+ const defSep = line.lastIndexOf(': definition of ')
1343
+ if (refSep < 0 && defSep < 0) continue
1344
+ const isRef = refSep > defSep
1345
+ const sep = isRef ? refSep : defSep
1346
+ const symbol = line.slice(sep + (isRef ? 15 : 16))
1347
+ if (!requested.has(symbol) || options.hostImports.has(symbol)) continue
1348
+ let record = records.get(symbol)
1349
+ if (!record) {
1350
+ record = {
1351
+ references: new Map<string, number>(),
1352
+ providers: new Map<string, number>(),
1353
+ physicalModule: false,
1354
+ }
1355
+ records.set(symbol, record)
1234
1356
  }
1357
+ const object = line.slice(0, sep)
1235
1358
  // an internal reference is wasm-ld's own, such as the real half of a --wrap, with no object to scope
1236
1359
  if (object === '<internal>') {
1237
- if (kind === 'definition of') record.physicalModule = true
1360
+ if (!isRef) record.physicalModule = true
1238
1361
  } else {
1239
- const image = options.owners.get(object!)
1362
+ const image = options.owners.get(object)
1240
1363
  if (image === undefined)
1241
1364
  throw new Error(`traced guest object has no owner ${object}`)
1242
- ;(kind === 'reference to' ? record.references : record.providers).set(
1243
- object!,
1244
- image,
1245
- )
1365
+ ;(isRef ? record.references : record.providers).set(object, image)
1246
1366
  }
1247
- records.set(symbol!, record)
1248
1367
  }
1249
- for (const line of guestLinkLines(tracePath)) consume(line)
1250
1368
  const callers = new Map(options.callers.map((caller) => [caller.image, caller.next]))
1369
+ const callerSets = new Map(
1370
+ options.callers.map((caller) => [caller.image, new Set(caller.next)]),
1371
+ )
1372
+ const rootScope = callerSets.get(0)
1251
1373
  const result: {
1252
1374
  symbol: string
1253
1375
  caller: number
@@ -1258,28 +1380,38 @@ export function verifyGuestSymbolScopes(options: {
1258
1380
  const record = records.get(symbol)
1259
1381
  if (!record || !record.references.size) continue
1260
1382
  if (!record.providers.size && options.hostImports.has(symbol)) continue
1261
- const startupReferences = [...record.references].filter(
1262
- ([object]) =>
1263
- options.startupObjects.has(object) &&
1264
- options.weakReferences.get(object)?.has(symbol),
1265
- )
1266
- const otherReferences = [...record.references].filter(
1267
- ([object]) => !startupReferences.some(([startup]) => startup === object),
1268
- )
1383
+ const startupReferences: [string, number][] = []
1384
+ const otherReferences: [string, number][] = []
1385
+ let allWeakRefs = true
1386
+ for (const [object, caller] of record.references) {
1387
+ const isWeakRef = options.weakReferences.get(object)?.has(symbol) === true
1388
+ if (!isWeakRef) allWeakRefs = false
1389
+ if (isWeakRef && options.startupObjects.has(object)) {
1390
+ startupReferences.push([object, caller])
1391
+ } else {
1392
+ otherReferences.push([object, caller])
1393
+ }
1394
+ }
1269
1395
  for (const [, caller] of startupReferences)
1270
1396
  if (!callers.has(caller))
1271
1397
  throw new Error(`guest image has no caller scope ${caller}`)
1272
1398
  if (startupReferences.length && !record.physicalModule) {
1273
- const candidates = options.definitionObjects.get(symbol) ?? new Set<string>()
1274
- if (candidates.size > 1 || record.providers.size > 1)
1399
+ const candidates = options.definitionObjects.get(symbol)
1400
+ const candidateSize = candidates?.size ?? 0
1401
+ if (candidateSize > 1 || record.providers.size > 1)
1275
1402
  throw new Error(`ambiguous startup provider ${symbol}`)
1276
- if (
1277
- candidates.size !== record.providers.size ||
1278
- [...record.providers.keys()].some((object) => !candidates.has(object))
1279
- )
1280
- throw new Error(`unproven startup provider ${symbol}`)
1403
+ let unproven = candidateSize !== record.providers.size
1404
+ if (!unproven && candidates) {
1405
+ for (const object of record.providers.keys()) {
1406
+ if (!candidates.has(object)) {
1407
+ unproven = true
1408
+ break
1409
+ }
1410
+ }
1411
+ }
1412
+ if (unproven) throw new Error(`unproven startup provider ${symbol}`)
1281
1413
  if (record.providers.size === 1) {
1282
- const [providerObject, providerImage] = [...record.providers][0]!
1414
+ const [providerObject, providerImage] = record.providers.entries().next().value!
1283
1415
  if (options.weakDefinitions.get(providerObject)?.has(symbol))
1284
1416
  throw new Error(`weak startup provider ${symbol}`)
1285
1417
  for (const [object] of startupReferences)
@@ -1289,29 +1421,26 @@ export function verifyGuestSymbolScopes(options: {
1289
1421
  }
1290
1422
  // an unresolved weak import stays absent, as in dyld; wasm-ld reports it
1291
1423
  // as an internal undefined_weak definition.
1292
- if (
1293
- !record.providers.size &&
1294
- !record.physicalModule &&
1295
- [...record.references.keys()].every((object) =>
1296
- options.weakReferences.get(object)?.has(symbol),
1297
- )
1298
- )
1299
- continue
1424
+ if (!record.providers.size && !record.physicalModule && allWeakRefs) continue
1300
1425
  const providerImages = new Set(record.providers.values())
1301
1426
  // the flat trace can report only the selected copy. the image scopes use
1302
1427
  // all definitions from the selected objects.
1303
- const copies = [...(options.definitionObjects.get(symbol) ?? [])].flatMap(
1304
- (object) => {
1428
+ const candidates = options.definitionObjects.get(symbol)
1429
+ const copyImages = new Set<number>()
1430
+ const strongImages = new Set<number>()
1431
+ let weakDefinerImages: Set<number> | undefined
1432
+ if (candidates) {
1433
+ for (const object of candidates) {
1305
1434
  const image = options.owners.get(object)
1306
- return image === undefined
1307
- ? []
1308
- : [{ image, weak: !!options.weakDefinitions.get(object)?.has(symbol) }]
1309
- },
1310
- )
1311
- const copyImages = new Set(copies.map((copy) => copy.image))
1312
- const strongImages = new Set(
1313
- copies.filter((copy) => !copy.weak).map((copy) => copy.image),
1314
- )
1435
+ if (image === undefined) continue
1436
+ copyImages.add(image)
1437
+ if (options.weakDefinitions.get(object)?.has(symbol)) {
1438
+ ;(weakDefinerImages ??= new Set()).add(image)
1439
+ } else {
1440
+ strongImages.add(image)
1441
+ }
1442
+ }
1443
+ }
1315
1444
  const interposed = !record.physicalModule && copyImages.size > 1
1316
1445
  const providerCount = interposed
1317
1446
  ? 1
@@ -1321,11 +1450,13 @@ export function verifyGuestSymbolScopes(options: {
1321
1450
  if (!providerCount) continue
1322
1451
  if (providerCount !== 1)
1323
1452
  throw new Error(`guest symbol has ambiguous provider ${symbol}`)
1324
- const scopedReferences = record.physicalModule
1325
- ? [...record.references]
1326
- : otherReferences
1453
+ const scopedReferences = record.physicalModule ? record.references : otherReferences
1454
+ const firstProviderImage = providerImages.values().next().value
1327
1455
  const uniqueProvider =
1328
- strongImages.size > 1 ? undefined : ([...strongImages][0] ?? [...providerImages][0])
1456
+ strongImages.size > 1
1457
+ ? undefined
1458
+ : (strongImages.values().next().value ?? firstProviderImage)
1459
+ let interposedByCaller: Map<number, number | undefined> | undefined
1329
1460
  for (const [object, caller] of scopedReferences) {
1330
1461
  const scope = callers.get(caller)
1331
1462
  if (!scope) throw new Error(`guest image has no caller scope ${caller}`)
@@ -1333,9 +1464,27 @@ export function verifyGuestSymbolScopes(options: {
1333
1464
  result.push({ symbol, caller, provider: { kind: 'physical-module' } })
1334
1465
  continue
1335
1466
  }
1336
- const provider = interposed
1337
- ? ([caller, ...scope].find((image) => copyImages.has(image)) ?? uniqueProvider)
1338
- : [...providerImages][0]!
1467
+ let provider: number | undefined
1468
+ if (interposed) {
1469
+ if (interposedByCaller?.has(caller)) {
1470
+ provider = interposedByCaller.get(caller)
1471
+ } else {
1472
+ if (copyImages.has(caller)) {
1473
+ provider = caller
1474
+ } else {
1475
+ for (const image of scope) {
1476
+ if (copyImages.has(image)) {
1477
+ provider = image
1478
+ break
1479
+ }
1480
+ }
1481
+ if (provider === undefined) provider = uniqueProvider
1482
+ }
1483
+ ;(interposedByCaller ??= new Map()).set(caller, provider)
1484
+ }
1485
+ } else {
1486
+ provider = firstProviderImage!
1487
+ }
1339
1488
  if (provider === undefined) {
1340
1489
  if (options.weakReferences.get(object)?.has(symbol)) continue
1341
1490
  throw new Error(`guest scope ${caller} omits every provider of ${symbol}`)
@@ -1343,16 +1492,8 @@ export function verifyGuestSymbolScopes(options: {
1343
1492
  if (caller === provider) continue
1344
1493
  // every image carries its own copy of a weak definition, so a caller
1345
1494
  // whose image defines one binds to it and needs no other provider.
1346
- if (
1347
- [...(options.definitionObjects.get(symbol) ?? [])].some(
1348
- (definer) =>
1349
- options.owners.get(definer) === caller &&
1350
- options.weakDefinitions.get(definer)?.has(symbol),
1351
- )
1352
- )
1353
- continue
1354
- if (!scope.includes(provider)) {
1355
- const candidates = options.definitionObjects.get(symbol) ?? new Set<string>()
1495
+ if (weakDefinerImages?.has(caller)) continue
1496
+ if (!callerSets.get(caller)!.has(provider)) {
1356
1497
  const traced = [...record.providers.keys()]
1357
1498
  // the one strong definition a link traces wins over weak fallbacks beside it
1358
1499
  const strongTraced = traced.filter(
@@ -1368,29 +1509,30 @@ export function verifyGuestSymbolScopes(options: {
1368
1509
  continue
1369
1510
  // weak definitions of one symbol are copies of the same code, so a
1370
1511
  // strong reference may bind to whichever copy the link traced
1512
+ const candidateSet = candidates ?? new Set<string>()
1371
1513
  const equivalentCopies =
1372
1514
  providers.length === 1 &&
1373
- candidates.has(providers[0]!) &&
1374
- [...candidates].every((definer) =>
1515
+ candidateSet.has(providers[0]!) &&
1516
+ [...candidateSet].every((definer) =>
1375
1517
  options.weakDefinitions.get(definer)?.has(symbol),
1376
1518
  )
1377
- const strongCandidates = [...candidates].filter(
1519
+ const strongCandidates = [...candidateSet].filter(
1378
1520
  (definer) => !options.weakDefinitions.get(definer)?.has(symbol),
1379
1521
  )
1380
1522
  const soleCandidate =
1381
- candidates.size === 1 ||
1523
+ candidateSet.size === 1 ||
1382
1524
  (strongCandidates.length === 1 && strongCandidates[0] === providers[0])
1383
1525
  if (
1384
1526
  !equivalentCopies &&
1385
1527
  (!soleCandidate ||
1386
1528
  providers.length !== 1 ||
1387
- !candidates.has(providers[0]!) ||
1529
+ !candidateSet.has(providers[0]!) ||
1388
1530
  !(
1389
1531
  options.systemProviders.has(providers[0]!) ||
1390
1532
  // a strong undefined reference binds through the process global
1391
1533
  // scope, the root image and its dependency closure, as in elf.
1392
1534
  provider === 0 ||
1393
- callers.get(0)?.includes(provider) ||
1535
+ rootScope?.has(provider) ||
1394
1536
  (!options.weakReferences.get(object)?.has(symbol) &&
1395
1537
  options.frameworkProviders.get(object)?.has(providers[0]!))
1396
1538
  ))