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
@@ -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
@@ -756,6 +819,9 @@ export function linkGuestImages(
756
819
  { stdio: 'pipe' },
757
820
  )
758
821
  finalTrace = linkFinal(projectionObject, callerMap ? undefined : layoutMap)
822
+ // sharedSdk builds never finalize final.wasm; app-module reserves and validates
823
+ // the combined app + SDK projection storage against app.wasm instead.
824
+ if (sharedSdk) break
759
825
  const wasm = fs.readFileSync(finalOutput)
760
826
  const actual = guestProjectionRegionCounts(
761
827
  images.length,
@@ -829,10 +895,51 @@ export function linkGuestImages(
829
895
  // every symbol name a linker map lists, which is what the link kept.
830
896
  function linkedMapSymbols(lines: Iterable<string>): Set<string> {
831
897
  const names = new Set<string>()
832
- const symbol = /^ +\S+ +[0-9a-f]+ +[0-9a-f]+ {9,}([^\s/<]\S*)/
833
898
  for (const line of lines) {
834
- const match = symbol.exec(line)
835
- if (match) names.add(match[1]!)
899
+ const length = line.length
900
+ if (length < 15 || line.charCodeAt(0) !== 32) continue
901
+ let pos = 1
902
+ while (pos < length && line.charCodeAt(pos) === 32) pos++
903
+ // col 1: \S+
904
+ const c1 = pos
905
+ while (pos < length) {
906
+ const ch = line.charCodeAt(pos)
907
+ if (ch === 32 || (ch >= 9 && ch <= 13)) break
908
+ pos++
909
+ }
910
+ if (pos === c1 || pos >= length || line.charCodeAt(pos) !== 32) continue
911
+ while (pos < length && line.charCodeAt(pos) === 32) pos++
912
+ // col 2: [0-9a-f]+
913
+ const c2 = pos
914
+ while (pos < length) {
915
+ const ch = line.charCodeAt(pos)
916
+ if (!((ch >= 48 && ch <= 57) || (ch >= 97 && ch <= 102))) break
917
+ pos++
918
+ }
919
+ if (pos === c2 || pos >= length || line.charCodeAt(pos) !== 32) continue
920
+ while (pos < length && line.charCodeAt(pos) === 32) pos++
921
+ // col 3: [0-9a-f]+
922
+ const c3 = pos
923
+ while (pos < length) {
924
+ const ch = line.charCodeAt(pos)
925
+ if (!((ch >= 48 && ch <= 57) || (ch >= 97 && ch <= 102))) break
926
+ pos++
927
+ }
928
+ if (pos === c3 || pos >= length || line.charCodeAt(pos) !== 32) continue
929
+ // {9,} spaces
930
+ const sStart = pos
931
+ while (pos < length && line.charCodeAt(pos) === 32) pos++
932
+ if (pos - sStart < 9 || pos >= length) continue
933
+ const first = line.charCodeAt(pos)
934
+ if (first === 47 || first === 60 || first === 32 || (first >= 9 && first <= 13))
935
+ continue
936
+ let end = pos + 1
937
+ while (end < length) {
938
+ const ch = line.charCodeAt(end)
939
+ if (ch === 32 || (ch >= 9 && ch <= 13)) break
940
+ end++
941
+ }
942
+ names.add(line.slice(pos, end))
836
943
  }
837
944
  return names
838
945
  }
@@ -957,7 +1064,10 @@ export function discoverGuestLinkInputs(options: {
957
1064
  fs.mkdirSync(options.directory, { recursive: true })
958
1065
  const why = path.join(options.directory, 'preselection-why.txt')
959
1066
  const response = path.join(options.directory, 'preselection.rsp')
960
- const output = path.join(options.directory, 'preselection.wasm')
1067
+ const output =
1068
+ process.platform !== 'win32'
1069
+ ? '/dev/null'
1070
+ : path.join(options.directory, 'preselection.wasm')
961
1071
  const flags = [
962
1072
  ...withOutput(args, output, true),
963
1073
  ...options.objects,
@@ -1088,6 +1198,7 @@ export function prepareGuestRuntimeInputs(options: {
1088
1198
  export function selectedGuestSymbolInputs(
1089
1199
  plan: GuestLinkPlan,
1090
1200
  selected: readonly GuestLinkSelection[],
1201
+ readSelected?: (item: GuestLinkSelection) => Uint8Array,
1091
1202
  ): {
1092
1203
  readonly symbols: readonly string[]
1093
1204
  readonly definitions: readonly string[]
@@ -1112,22 +1223,27 @@ export function selectedGuestSymbolInputs(
1112
1223
  if (!artifact) throw new Error(`selected symbol input has no owner ${item.artifact}`)
1113
1224
  let bytes: Uint8Array
1114
1225
  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)
1226
+ if (item.member === null) {
1227
+ bytes = readSelected ? readSelected(item) : fs.readFileSync(item.artifact)
1228
+ } else {
1229
+ if (readSelected) {
1230
+ bytes = readSelected(item)
1231
+ } else {
1232
+ let members = archives.get(item.artifact)
1233
+ if (!members) {
1234
+ members = Map.groupBy(
1235
+ readArMembers(fs.readFileSync(item.artifact)),
1236
+ (member) => member.name,
1237
+ )
1238
+ archives.set(item.artifact, members)
1239
+ }
1240
+ const matching = members.get(item.member) ?? []
1241
+ if (matching.length !== 1)
1242
+ throw new Error(
1243
+ `ambiguous selected symbol input ${item.artifact}(${item.member})`,
1244
+ )
1245
+ bytes = matching[0]!.bytes
1124
1246
  }
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
1247
  name += `(${item.member})`
1132
1248
  }
1133
1249
  if (owners.has(name)) throw new Error(`duplicate selected symbol input ${name}`)
@@ -1140,9 +1256,9 @@ export function selectedGuestSymbolInputs(
1140
1256
  for (const symbol of names.tags) tags.add(symbol)
1141
1257
  for (const symbol of names.defined) {
1142
1258
  definitions.add(symbol)
1143
- const objects = definitionObjects.get(symbol) ?? new Set<string>()
1259
+ let objects = definitionObjects.get(symbol)
1260
+ if (!objects) definitionObjects.set(symbol, (objects = new Set<string>()))
1144
1261
  objects.add(name)
1145
- definitionObjects.set(symbol, objects)
1146
1262
  }
1147
1263
  }
1148
1264
  return {
@@ -1196,7 +1312,10 @@ export function verifyGuestSymbolScopes(options: {
1196
1312
  if (!symbols.length) return { bindings: [], directBindings: [] }
1197
1313
  fs.mkdirSync(options.directory, { recursive: true })
1198
1314
  const response = path.join(options.directory, 'scope-trace.rsp')
1199
- const output = path.join(options.directory, 'scope-trace.wasm')
1315
+ const output =
1316
+ process.platform !== 'win32'
1317
+ ? '/dev/null'
1318
+ : path.join(options.directory, 'scope-trace.wasm')
1200
1319
  const args = [
1201
1320
  ...withOutput(normalizedGuestLinkArgs(options.args), output, true),
1202
1321
  ...options.objects,
@@ -1221,33 +1340,39 @@ export function verifyGuestSymbolScopes(options: {
1221
1340
  physicalModule: boolean
1222
1341
  }
1223
1342
  >()
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,
1343
+ for (const line of guestLinkLines(tracePath)) {
1344
+ const refSep = line.lastIndexOf(': reference to ')
1345
+ const defSep = line.lastIndexOf(': definition of ')
1346
+ if (refSep < 0 && defSep < 0) continue
1347
+ const isRef = refSep > defSep
1348
+ const sep = isRef ? refSep : defSep
1349
+ const symbol = line.slice(sep + (isRef ? 15 : 16))
1350
+ if (!requested.has(symbol) || options.hostImports.has(symbol)) continue
1351
+ let record = records.get(symbol)
1352
+ if (!record) {
1353
+ record = {
1354
+ references: new Map<string, number>(),
1355
+ providers: new Map<string, number>(),
1356
+ physicalModule: false,
1357
+ }
1358
+ records.set(symbol, record)
1234
1359
  }
1360
+ const object = line.slice(0, sep)
1235
1361
  // an internal reference is wasm-ld's own, such as the real half of a --wrap, with no object to scope
1236
1362
  if (object === '<internal>') {
1237
- if (kind === 'definition of') record.physicalModule = true
1363
+ if (!isRef) record.physicalModule = true
1238
1364
  } else {
1239
- const image = options.owners.get(object!)
1365
+ const image = options.owners.get(object)
1240
1366
  if (image === undefined)
1241
1367
  throw new Error(`traced guest object has no owner ${object}`)
1242
- ;(kind === 'reference to' ? record.references : record.providers).set(
1243
- object!,
1244
- image,
1245
- )
1368
+ ;(isRef ? record.references : record.providers).set(object, image)
1246
1369
  }
1247
- records.set(symbol!, record)
1248
1370
  }
1249
- for (const line of guestLinkLines(tracePath)) consume(line)
1250
1371
  const callers = new Map(options.callers.map((caller) => [caller.image, caller.next]))
1372
+ const callerSets = new Map(
1373
+ options.callers.map((caller) => [caller.image, new Set(caller.next)]),
1374
+ )
1375
+ const rootScope = callerSets.get(0)
1251
1376
  const result: {
1252
1377
  symbol: string
1253
1378
  caller: number
@@ -1258,28 +1383,38 @@ export function verifyGuestSymbolScopes(options: {
1258
1383
  const record = records.get(symbol)
1259
1384
  if (!record || !record.references.size) continue
1260
1385
  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
- )
1386
+ const startupReferences: [string, number][] = []
1387
+ const otherReferences: [string, number][] = []
1388
+ let allWeakRefs = true
1389
+ for (const [object, caller] of record.references) {
1390
+ const isWeakRef = options.weakReferences.get(object)?.has(symbol) === true
1391
+ if (!isWeakRef) allWeakRefs = false
1392
+ if (isWeakRef && options.startupObjects.has(object)) {
1393
+ startupReferences.push([object, caller])
1394
+ } else {
1395
+ otherReferences.push([object, caller])
1396
+ }
1397
+ }
1269
1398
  for (const [, caller] of startupReferences)
1270
1399
  if (!callers.has(caller))
1271
1400
  throw new Error(`guest image has no caller scope ${caller}`)
1272
1401
  if (startupReferences.length && !record.physicalModule) {
1273
- const candidates = options.definitionObjects.get(symbol) ?? new Set<string>()
1274
- if (candidates.size > 1 || record.providers.size > 1)
1402
+ const candidates = options.definitionObjects.get(symbol)
1403
+ const candidateSize = candidates?.size ?? 0
1404
+ if (candidateSize > 1 || record.providers.size > 1)
1275
1405
  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}`)
1406
+ let unproven = candidateSize !== record.providers.size
1407
+ if (!unproven && candidates) {
1408
+ for (const object of record.providers.keys()) {
1409
+ if (!candidates.has(object)) {
1410
+ unproven = true
1411
+ break
1412
+ }
1413
+ }
1414
+ }
1415
+ if (unproven) throw new Error(`unproven startup provider ${symbol}`)
1281
1416
  if (record.providers.size === 1) {
1282
- const [providerObject, providerImage] = [...record.providers][0]!
1417
+ const [providerObject, providerImage] = record.providers.entries().next().value!
1283
1418
  if (options.weakDefinitions.get(providerObject)?.has(symbol))
1284
1419
  throw new Error(`weak startup provider ${symbol}`)
1285
1420
  for (const [object] of startupReferences)
@@ -1289,29 +1424,26 @@ export function verifyGuestSymbolScopes(options: {
1289
1424
  }
1290
1425
  // an unresolved weak import stays absent, as in dyld; wasm-ld reports it
1291
1426
  // 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
1427
+ if (!record.providers.size && !record.physicalModule && allWeakRefs) continue
1300
1428
  const providerImages = new Set(record.providers.values())
1301
1429
  // the flat trace can report only the selected copy. the image scopes use
1302
1430
  // all definitions from the selected objects.
1303
- const copies = [...(options.definitionObjects.get(symbol) ?? [])].flatMap(
1304
- (object) => {
1431
+ const candidates = options.definitionObjects.get(symbol)
1432
+ const copyImages = new Set<number>()
1433
+ const strongImages = new Set<number>()
1434
+ let weakDefinerImages: Set<number> | undefined
1435
+ if (candidates) {
1436
+ for (const object of candidates) {
1305
1437
  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
- )
1438
+ if (image === undefined) continue
1439
+ copyImages.add(image)
1440
+ if (options.weakDefinitions.get(object)?.has(symbol)) {
1441
+ ;(weakDefinerImages ??= new Set()).add(image)
1442
+ } else {
1443
+ strongImages.add(image)
1444
+ }
1445
+ }
1446
+ }
1315
1447
  const interposed = !record.physicalModule && copyImages.size > 1
1316
1448
  const providerCount = interposed
1317
1449
  ? 1
@@ -1321,11 +1453,13 @@ export function verifyGuestSymbolScopes(options: {
1321
1453
  if (!providerCount) continue
1322
1454
  if (providerCount !== 1)
1323
1455
  throw new Error(`guest symbol has ambiguous provider ${symbol}`)
1324
- const scopedReferences = record.physicalModule
1325
- ? [...record.references]
1326
- : otherReferences
1456
+ const scopedReferences = record.physicalModule ? record.references : otherReferences
1457
+ const firstProviderImage = providerImages.values().next().value
1327
1458
  const uniqueProvider =
1328
- strongImages.size > 1 ? undefined : ([...strongImages][0] ?? [...providerImages][0])
1459
+ strongImages.size > 1
1460
+ ? undefined
1461
+ : (strongImages.values().next().value ?? firstProviderImage)
1462
+ let interposedByCaller: Map<number, number | undefined> | undefined
1329
1463
  for (const [object, caller] of scopedReferences) {
1330
1464
  const scope = callers.get(caller)
1331
1465
  if (!scope) throw new Error(`guest image has no caller scope ${caller}`)
@@ -1333,9 +1467,27 @@ export function verifyGuestSymbolScopes(options: {
1333
1467
  result.push({ symbol, caller, provider: { kind: 'physical-module' } })
1334
1468
  continue
1335
1469
  }
1336
- const provider = interposed
1337
- ? ([caller, ...scope].find((image) => copyImages.has(image)) ?? uniqueProvider)
1338
- : [...providerImages][0]!
1470
+ let provider: number | undefined
1471
+ if (interposed) {
1472
+ if (interposedByCaller?.has(caller)) {
1473
+ provider = interposedByCaller.get(caller)
1474
+ } else {
1475
+ if (copyImages.has(caller)) {
1476
+ provider = caller
1477
+ } else {
1478
+ for (const image of scope) {
1479
+ if (copyImages.has(image)) {
1480
+ provider = image
1481
+ break
1482
+ }
1483
+ }
1484
+ if (provider === undefined) provider = uniqueProvider
1485
+ }
1486
+ ;(interposedByCaller ??= new Map()).set(caller, provider)
1487
+ }
1488
+ } else {
1489
+ provider = firstProviderImage!
1490
+ }
1339
1491
  if (provider === undefined) {
1340
1492
  if (options.weakReferences.get(object)?.has(symbol)) continue
1341
1493
  throw new Error(`guest scope ${caller} omits every provider of ${symbol}`)
@@ -1343,16 +1495,8 @@ export function verifyGuestSymbolScopes(options: {
1343
1495
  if (caller === provider) continue
1344
1496
  // every image carries its own copy of a weak definition, so a caller
1345
1497
  // 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>()
1498
+ if (weakDefinerImages?.has(caller)) continue
1499
+ if (!callerSets.get(caller)!.has(provider)) {
1356
1500
  const traced = [...record.providers.keys()]
1357
1501
  // the one strong definition a link traces wins over weak fallbacks beside it
1358
1502
  const strongTraced = traced.filter(
@@ -1368,29 +1512,30 @@ export function verifyGuestSymbolScopes(options: {
1368
1512
  continue
1369
1513
  // weak definitions of one symbol are copies of the same code, so a
1370
1514
  // strong reference may bind to whichever copy the link traced
1515
+ const candidateSet = candidates ?? new Set<string>()
1371
1516
  const equivalentCopies =
1372
1517
  providers.length === 1 &&
1373
- candidates.has(providers[0]!) &&
1374
- [...candidates].every((definer) =>
1518
+ candidateSet.has(providers[0]!) &&
1519
+ [...candidateSet].every((definer) =>
1375
1520
  options.weakDefinitions.get(definer)?.has(symbol),
1376
1521
  )
1377
- const strongCandidates = [...candidates].filter(
1522
+ const strongCandidates = [...candidateSet].filter(
1378
1523
  (definer) => !options.weakDefinitions.get(definer)?.has(symbol),
1379
1524
  )
1380
1525
  const soleCandidate =
1381
- candidates.size === 1 ||
1526
+ candidateSet.size === 1 ||
1382
1527
  (strongCandidates.length === 1 && strongCandidates[0] === providers[0])
1383
1528
  if (
1384
1529
  !equivalentCopies &&
1385
1530
  (!soleCandidate ||
1386
1531
  providers.length !== 1 ||
1387
- !candidates.has(providers[0]!) ||
1532
+ !candidateSet.has(providers[0]!) ||
1388
1533
  !(
1389
1534
  options.systemProviders.has(providers[0]!) ||
1390
1535
  // a strong undefined reference binds through the process global
1391
1536
  // scope, the root image and its dependency closure, as in elf.
1392
1537
  provider === 0 ||
1393
- callers.get(0)?.includes(provider) ||
1538
+ rootScope?.has(provider) ||
1394
1539
  (!options.weakReferences.get(object)?.has(symbol) &&
1395
1540
  options.frameworkProviders.get(object)?.has(providers[0]!))
1396
1541
  ))