peachsim 0.1.2623 → 0.1.2625

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 (140) 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 +1023 -621
  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-E6V72TM2.mjs → acorn-SHZZDBSI.mjs} +3 -3
  27. package/dist-lib/esm-chunks/{chunk-EIOVKQ2C.mjs → chunk-2LDG7OD4.mjs} +5 -5
  28. package/dist-lib/esm-chunks/{chunk-ISH6XB23.mjs → chunk-3XZGWT62.mjs} +1 -1
  29. package/dist-lib/esm-chunks/{chunk-Q7KCK4YO.mjs → chunk-6PZI7TYE.mjs} +1 -1
  30. package/dist-lib/esm-chunks/{chunk-RLWITQ5C.mjs → chunk-7Z7YJ4FU.mjs} +1 -1
  31. package/dist-lib/esm-chunks/{chunk-4GNQG2GT.mjs → chunk-A4R4YGFJ.mjs} +1 -1
  32. package/dist-lib/esm-chunks/{chunk-GP3KL2GC.mjs → chunk-A66VNSO6.mjs} +565 -290
  33. package/dist-lib/esm-chunks/{chunk-XOAOND7F.mjs → chunk-DVMKYLDN.mjs} +9 -8
  34. package/dist-lib/esm-chunks/{chunk-IGB7TRLS.mjs → chunk-F4YHL4UM.mjs} +1 -1
  35. package/dist-lib/esm-chunks/{chunk-4BJIJ3GP.mjs → chunk-JRU2VXUU.mjs} +1 -1
  36. package/dist-lib/esm-chunks/{chunk-EI7CZDMR.mjs → chunk-JVEJLPJC.mjs} +2 -2
  37. package/dist-lib/esm-chunks/{chunk-UCW7ITXE.mjs → chunk-KA4AQDVF.mjs} +1 -1
  38. package/dist-lib/esm-chunks/{chunk-RLWTJ6II.mjs → chunk-LJYNZ5HM.mjs} +1 -1
  39. package/dist-lib/esm-chunks/{chunk-RBXTTEI6.mjs → chunk-M2FEKSK3.mjs} +1 -1
  40. package/dist-lib/esm-chunks/{chunk-KPG4FUTY.mjs → chunk-OCCZJOSA.mjs} +1 -1
  41. package/dist-lib/esm-chunks/{chunk-CXNXKUNJ.mjs → chunk-P542MMIN.mjs} +1 -1
  42. package/dist-lib/esm-chunks/{chunk-NOM66H2G.mjs → chunk-Q5NI4L3Z.mjs} +1 -1
  43. package/dist-lib/esm-chunks/{chunk-V2CHR3OI.mjs → chunk-R4FFTWPL.mjs} +1 -1
  44. package/dist-lib/esm-chunks/{chunk-W3X7RP3H.mjs → chunk-RJWEWNAJ.mjs} +3 -3
  45. package/dist-lib/esm-chunks/{chunk-B5OQSD4B.mjs → chunk-RXXCTHJG.mjs} +1 -1
  46. package/dist-lib/esm-chunks/{chunk-B4APPUTT.mjs → chunk-SRIRVPNX.mjs} +1 -1
  47. package/dist-lib/esm-chunks/{chunk-6MNE57DC.mjs → chunk-ULHG6CND.mjs} +22 -13
  48. package/dist-lib/esm-chunks/{chunk-PAEJF2JP.mjs → chunk-W4HQINQ6.mjs} +4 -4
  49. package/dist-lib/esm-chunks/{chunk-RVM3WWST.mjs → chunk-XQBHCWS4.mjs} +1 -1
  50. package/dist-lib/esm-chunks/{chunk-Q4ZATFEN.mjs → chunk-Z6AH6AQC.mjs} +1 -1
  51. package/dist-lib/esm-chunks/{metro-layout-parser-OHVROLNJ.mjs → metro-layout-parser-GJHR7FCR.mjs} +3 -3
  52. package/dist-lib/esm-chunks/{swift-package-prebuilt-DRAUK6KR.mjs → swift-package-prebuilt-UVRGKTD4.mjs} +12 -12
  53. package/dist-lib/esm-chunks/{swift-sdk-app-module-U2M7CMKA.mjs → swift-sdk-app-module-U3MUJ7QD.mjs} +107 -20
  54. package/dist-lib/esm-chunks/{swift-sdk-image-ZHWSHCNI.mjs → swift-sdk-image-U4W7QMAT.mjs} +16 -18
  55. package/dist-lib/esm-chunks/{swift-sdk-image-producer-EOJUCK7N.mjs → swift-sdk-image-producer-TFFZDNRK.mjs} +5 -5
  56. package/dist-lib/expo-swift-bundle.cjs +918 -516
  57. package/dist-lib/expo-swift-metadata.cjs +1 -1
  58. package/dist-lib/expo-swift-metadata.mjs +7 -7
  59. package/dist-lib/home-paths.cjs +1 -1
  60. package/dist-lib/host/bridge-host.cjs +1025 -623
  61. package/dist-lib/host/fetch-proxy-handler.cjs +10 -9
  62. package/dist-lib/host/fetch-proxy-overrides.cjs +1 -1
  63. package/dist-lib/host/fetch-proxy-overrides.mjs +2 -2
  64. package/dist-lib/host/replacement-module-handler.cjs +1 -1
  65. package/dist-lib/host/websocket-proxy.cjs +1 -1
  66. package/dist-lib/index.cjs +1 -1
  67. package/dist-lib/kotlin-host.cjs +1 -1
  68. package/dist-lib/menu.cjs +1 -1
  69. package/dist-lib/menu.mjs +3 -3
  70. package/dist-lib/metro-fingerprint-registry.cjs +1 -1
  71. package/dist-lib/metro-fingerprint-registry.mjs +3 -3
  72. package/dist-lib/metro-production-bundle.cjs +1 -1
  73. package/dist-lib/metro-production-bundle.mjs +4 -4
  74. package/dist-lib/native-app-identity-project.cjs +33 -17
  75. package/dist-lib/native-app-identity-project.mjs +44 -29
  76. package/dist-lib/native-app-identity.cjs +1 -1
  77. package/dist-lib/native-app-identity.mjs +6 -6
  78. package/dist-lib/native-firebase-metadata.cjs +1 -1
  79. package/dist-lib/native-firebase-metadata.mjs +3 -3
  80. package/dist-lib/pdfium-raster.cjs +1 -1
  81. package/dist-lib/pdfium-raster.mjs +2 -2
  82. package/dist-lib/plist.cjs +1 -1
  83. package/dist-lib/plist.mjs +3 -3
  84. package/dist-lib/profiles.cjs +1 -1
  85. package/dist-lib/public-brand.cjs +1 -1
  86. package/dist-lib/public-origins-lib-esm.json +87 -86
  87. package/dist-lib/public-origins-lib.json +69 -64
  88. package/dist-lib/react-native-host-modules.cjs +1 -1
  89. package/dist-lib/react-native-host-modules.mjs +2 -2
  90. package/dist-lib/reap-browser.cjs +1 -1
  91. package/dist-lib/scripts/dev-server-scanner.cjs +1 -1
  92. package/dist-lib/sdk.cjs +1 -1
  93. package/dist-lib/sdk.mjs +8 -8
  94. package/dist-lib/skills.cjs +1062 -660
  95. package/dist-lib/swift-compile.cjs +708 -336
  96. package/dist-lib/swift-compile.mjs +12 -12
  97. package/dist-lib/swift-package.cjs +1 -1
  98. package/dist-lib/swift-package.mjs +3 -3
  99. package/dist-lib/swift-submit.cjs +1 -1
  100. package/dist-lib/swift-submit.mjs +6 -6
  101. package/dist-lib/swift-svg-raster.cjs +1 -1
  102. package/dist-lib/swift-svg-raster.mjs +2 -2
  103. package/dist-lib/swift-toolchain.cjs +1 -1
  104. package/dist-lib/swift.cjs +1 -1
  105. package/dist-lib/vendor/@peculiar/x509.cjs +1 -1
  106. package/dist-lib/vendor/@ruby/prism/src/nodes.js.cjs +1 -1
  107. package/dist-lib/vendor/@ruby/prism/src/parsePrism.js.cjs +1 -1
  108. package/dist-lib/vendor/acorn.cjs +1 -1
  109. package/dist-lib/vendor/magic-string.cjs +1 -1
  110. package/dist-lib/vendor/stream-json.cjs +1 -1
  111. package/dist-lib/vendor/sucrase/dist/index.js.cjs +1 -1
  112. package/dist-lib/vendor/sucrase/dist/parser/index.js.cjs +1 -1
  113. package/dist-lib/vendor/sucrase/dist/parser/tokenizer/index.js.cjs +1 -1
  114. package/dist-lib/vendor/sucrase/dist/parser/tokenizer/types.js.cjs +1 -1
  115. package/dist-lib/vendor/sucrase.cjs +1 -1
  116. package/dist-lib/vendor/terser.cjs +1 -1
  117. package/dist-lib/vendor/valibot.cjs +1 -1
  118. package/dist-lib/vendor/ws.cjs +1 -1
  119. package/dist-lib/vendor/zod.cjs +1 -1
  120. package/dist-lib/worklet-executable.cjs +1 -1
  121. package/dist-lib/worklet-executable.mjs +3 -3
  122. package/package.json +1 -1
  123. package/src/bazel-project.ts +34 -18
  124. package/src/binaryen.lock.json +3 -3
  125. package/src/swift-compile.ts +8 -0
  126. package/src/swift-distribution-contract.ts +6 -1
  127. package/src/swift-distribution.lock.json +4 -2
  128. package/src/swift-guest-compile.ts +170 -160
  129. package/src/swift-guest-dyld-table.ts +22 -9
  130. package/src/swift-guest-link.ts +116 -42
  131. package/src/swift-guest-linked-spans.ts +63 -14
  132. package/src/swift-guest-projection-storage.ts +7 -6
  133. package/src/swift-guest-source-inputs.ts +35 -21
  134. package/src/swift-objc-compile.ts +1 -0
  135. package/src/swift-sdk-app-module.ts +118 -15
  136. package/src/swift-sdk-image-recipe.ts +20 -0
  137. package/src/swift-sdk-image-wasm.ts +119 -17
  138. package/src/swift-sdk-image.ts +6 -5
  139. package/src/swift-wasm-prune.ts +71 -41
  140. package/src/wasm-narrow-boundary.ts +31 -6
@@ -217,6 +217,7 @@ export async function compileGuestLink(options: {
217
217
  splitInputs = value
218
218
  }
219
219
  : undefined,
220
+ deferFinalLink: Boolean(sdk),
220
221
  onDyldRows: (rows) => {
221
222
  lazyFunctions = rows.lazyFunctions
222
223
  dyldSymbols = rows.dyldSymbols
@@ -546,175 +547,184 @@ export async function compileGuestLink(options: {
546
547
  })
547
548
  if (!reserved || !projectionObject) throw new Error('guest projection was not reserved')
548
549
  if (sdk) {
549
- if (!splitInputs) throw new Error('guest link did not select shared SDK inputs')
550
- const {
551
- createSwiftSdkBinding,
552
- finishSwiftAppModule,
553
- linkSwiftAppModule,
554
- readSwiftSdkUsage,
555
- swiftSdkProjectionSpans,
556
- } = await import('./swift-sdk-app-module')
557
- const project = options.project
558
- const work = project
559
- ? mkdtempSync(path.join(directory, 'app-module-'))
560
- : path.join(directory, 'app-module')
561
- if (project) chownSync(work, project.uid, project.gid)
562
- else mkdirSync(work, { recursive: true })
563
- const layoutMap = path.join(work, 'app.map')
564
- const owners = new Map(splitInputs.appObjects).set(projectionObject, 0)
565
- const sdkObjectKeys = new Map<string, string>()
566
- const sdkObjectImages = new Map<string, number>()
567
- const sdkProductImages = new Map(prepared?.sdkProducts.images)
568
- for (const [file, { image, product, identity }] of splitInputs.sdkObjects) {
569
- const key = sdkGuestObjectKey(identity)
570
- sdkObjectKeys.set(file, key)
571
- sdkObjectImages.set(key, image)
572
- const priorImage = sdkProductImages.get(product)
573
- if (priorImage !== undefined && priorImage !== image)
574
- throw new Error(`SDK product ${product} has several guest images`)
575
- sdkProductImages.set(product, image)
576
- }
577
- const usage = readSwiftSdkUsage(result.mapPath, options.output, sdkObjectKeys)
578
- const linkedModule = path.join(work, 'app.wasm')
579
- // sdk spans depend only on the sdk manifest and image ownership, never on app.wasm;
580
- // reserving their capacity before the first app link avoids a duplicate link pass.
581
- const sdkSpans = swiftSdkProjectionSpans(
582
- sdk,
583
- (object, product) => sdkObjectImages.get(object) ?? sdkProductImages.get(product),
584
- )
585
- let storage = reserved
586
- if (sdkSpans.length > 0) {
587
- const sdkCounts = guestProjectionRegionCounts(storage.images.length, sdkSpans)
588
- storage = reserveGuestProjectionStorage(
589
- storage.images,
590
- storage.capacities.map((capacity, index) => ({
591
- ...capacity,
592
- regions: capacity.regions + sdkCounts[index]! + 16,
593
- })),
594
- )
595
- const projectionSource = path.join(work, 'projection.c')
596
- writeFileSync(projectionSource, storage.source)
597
- execFileSync(
598
- path.join(options.toolchainBin, 'clang'),
599
- [...clangOptions, '-c', projectionSource, '-o', projectionObject],
600
- { stdio: 'pipe' },
601
- )
602
- }
603
- let precomputedSymbols:
604
- | {
605
- readonly referenced: ReadonlySet<string>
606
- readonly defined: ReadonlySet<string>
607
- }
608
- | undefined
609
- if (splitInputs.appReferencedSymbols && splitInputs.appDefinedSymbols) {
610
- const referenced = new Set(splitInputs.appReferencedSymbols)
611
- const defined = new Set(splitInputs.appDefinedSymbols)
612
- const projSyms = guestWasmSymbols(readFileSync(projectionObject))
613
- for (const name of projSyms.undefined) referenced.add(name)
614
- for (const name of projSyms.defined) defined.add(name)
615
- precomputedSymbols = { referenced, defined }
616
- }
617
- for (;;) {
618
- const link = await linkSwiftAppModule({
619
- objects: [...owners.keys()],
620
- lazyFunctions: splitInputs.lazyFunctions,
621
- toolchainBin: options.toolchainBin,
622
- sdk,
623
- work,
624
- output: options.optimized,
625
- optimize: options.optimize,
626
- layoutMap,
627
- project,
628
- precomputedSymbols,
629
- loweringCache: project
630
- ? undefined
631
- : {
632
- file: path.join(
633
- options.buildDirectory,
634
- '.build',
635
- 'peach-app-module-lowered.wasm',
636
- ),
637
- compilerDigest: options.compilerDigest,
638
- },
639
- })
640
- const map = readFileSync(
641
- project ? projectOutputFile(work, layoutMap, project) : layoutMap,
642
- 'utf8',
643
- )
644
- // the host resolves the rows the binding would write at load; their memory stays empty.
645
- const values = guestDyldValueRange(map)
646
- const bindingRows = new Map<number, number>()
647
- const data: number[] = []
648
- for (let at = 0; at < link.linkage.data.length; at += 3) {
649
- const address = link.linkage.data[at]!
650
- const size = link.linkage.data[at + 1]!
651
- const value = link.linkage.data[at + 2]!
652
- if (
653
- values &&
654
- address >= values.address &&
655
- address < values.address + values.length
656
- ) {
657
- if (size !== 8 || (address - values.address) % 8)
658
- throw new Error('sdk binding splits a dyld value')
659
- bindingRows.set((address - values.address) / 8, value)
660
- } else data.push(address, size, value)
550
+ try {
551
+ if (!splitInputs) throw new Error('guest link did not select shared SDK inputs')
552
+ const {
553
+ createSwiftSdkBinding,
554
+ linkSwiftAppModule,
555
+ readSwiftSdkUsage,
556
+ swiftAppModuleThunkSection,
557
+ swiftSdkProjectionSpans,
558
+ } = await import('./swift-sdk-app-module')
559
+ const project = options.project
560
+ const work = project
561
+ ? mkdtempSync(path.join(directory, 'app-module-'))
562
+ : path.join(directory, 'app-module')
563
+ if (project) chownSync(work, project.uid, project.gid)
564
+ else mkdirSync(work, { recursive: true })
565
+ const layoutMap = path.join(work, 'app.map')
566
+ let appProjectionObject = projectionObject
567
+ const owners = new Map(splitInputs.appObjects).set(appProjectionObject, 0)
568
+ const sdkObjectKeys = new Map<string, string>()
569
+ const sdkObjectImages = new Map<string, number>()
570
+ const sdkProductImages = new Map(prepared?.sdkProducts.images)
571
+ for (const [file, { image, product, identity }] of splitInputs.sdkObjects) {
572
+ const key = sdkGuestObjectKey(identity)
573
+ sdkObjectKeys.set(file, key)
574
+ sdkObjectImages.set(key, image)
575
+ const priorImage = sdkProductImages.get(product)
576
+ if (priorImage !== undefined && priorImage !== image)
577
+ throw new Error(`SDK product ${product} has several guest images`)
578
+ sdkProductImages.set(product, image)
661
579
  }
662
- const linked = readFileSync(
663
- project ? projectOutputFile(work, linkedModule, project) : linkedModule,
580
+ const linkedModule = path.join(work, 'app.wasm')
581
+ // sdk spans depend only on the sdk manifest and image ownership, never on app.wasm;
582
+ // reserving their capacity before the first app link avoids a duplicate link pass.
583
+ const sdkSpans = swiftSdkProjectionSpans(
584
+ sdk,
585
+ (object, product) => sdkObjectImages.get(object) ?? sdkProductImages.get(product),
664
586
  )
665
- // optimization must preserve these addresses and table slots.
666
- const required = guestProjectionRegionCounts(storage.images.length, [
667
- ...guestLinkedSpans(map, linked, linked, owners, splitInputs.appSections, true),
668
- ...sdkSpans,
669
- ])
670
- if (storage.capacities.some(({ regions }, index) => required[index]! > regions)) {
671
- storage = reserveGuestProjectionStorage(
672
- storage.images,
673
- storage.capacities.map((capacity, index) => {
674
- const count = required[index]!
675
- return count > capacity.regions
676
- ? { ...capacity, regions: Math.max(count, capacity.regions * 2) }
677
- : capacity
678
- }),
679
- )
587
+ const compileAppProjection = (source: string) => {
680
588
  const projectionSource = path.join(work, 'projection.c')
681
- writeFileSync(projectionSource, storage.source)
589
+ const nextObject = path.join(work, 'projection.o')
590
+ writeFileSync(projectionSource, source)
682
591
  execFileSync(
683
592
  path.join(options.toolchainBin, 'clang'),
684
- [...clangOptions, '-c', projectionSource, '-o', projectionObject],
593
+ [...clangOptions, '-c', projectionSource, '-o', nextObject],
685
594
  { stdio: 'pipe' },
686
595
  )
687
- continue
596
+ if (nextObject !== appProjectionObject) {
597
+ owners.delete(appProjectionObject)
598
+ appProjectionObject = nextObject
599
+ owners.set(appProjectionObject, 0)
600
+ }
688
601
  }
689
- options.onStage('lowering the app image')
690
- // let lower() spawn its child process before synchronous SDK binding assembly runs.
691
- const lowerPromise = link.lower()
692
- await new Promise((resolve) => setImmediate(resolve))
693
- let binding: SwiftSdkBindingAsset
694
- try {
695
- binding = createSwiftSdkBinding({ ...link.linkage, data }, sdk, usage)
696
- } catch (error) {
697
- await lowerPromise.catch(() => {})
698
- throw error
602
+ let storage = reserved
603
+ if (sdkSpans.length > 0) {
604
+ const sdkCounts = guestProjectionRegionCounts(storage.images.length, sdkSpans)
605
+ storage = reserveGuestProjectionStorage(
606
+ storage.images,
607
+ storage.capacities.map((capacity, index) => ({
608
+ ...capacity,
609
+ regions: capacity.regions + sdkCounts[index]! + 16,
610
+ })),
611
+ )
612
+ compileAppProjection(storage.source)
699
613
  }
700
- await lowerPromise
701
- const finalized = finalizeGuestProjectionStorage({
702
- map,
703
- linked,
704
- optimized: readFileSync(options.optimized),
705
- owners,
706
- sectionOwners: splitInputs.appSections,
707
- projectionObject,
708
- reserved: storage,
709
- sdkSpans,
710
- })
711
- writeFileSync(
712
- options.optimized,
713
- finishSwiftAppModule(
714
- moveGuestDyldTable(map, finalized.wasm, splitInputs.dyldSymbols, bindingRows),
715
- ),
716
- )
717
- return binding
614
+ let precomputedSymbols:
615
+ | {
616
+ readonly referenced: ReadonlySet<string>
617
+ readonly defined: ReadonlySet<string>
618
+ }
619
+ | undefined
620
+ if (splitInputs.appReferencedSymbols && splitInputs.appDefinedSymbols) {
621
+ const referenced = new Set(splitInputs.appReferencedSymbols)
622
+ const defined = new Set(splitInputs.appDefinedSymbols)
623
+ const projSyms = guestWasmSymbols(readFileSync(appProjectionObject))
624
+ for (const name of projSyms.undefined) referenced.add(name)
625
+ for (const name of projSyms.defined) defined.add(name)
626
+ precomputedSymbols = { referenced, defined }
627
+ }
628
+ for (;;) {
629
+ const link = await linkSwiftAppModule({
630
+ objects: [...owners.keys()],
631
+ lazyFunctions: splitInputs.lazyFunctions,
632
+ toolchainBin: options.toolchainBin,
633
+ sdk,
634
+ work,
635
+ output: options.optimized,
636
+ optimize: options.optimize,
637
+ layoutMap,
638
+ project,
639
+ precomputedSymbols,
640
+ loweringCache: project
641
+ ? undefined
642
+ : {
643
+ file: path.join(
644
+ options.buildDirectory,
645
+ '.build',
646
+ 'peach-app-module-lowered.wasm',
647
+ ),
648
+ compilerDigest: options.compilerDigest,
649
+ },
650
+ })
651
+ const map = readFileSync(
652
+ project ? projectOutputFile(work, layoutMap, project) : layoutMap,
653
+ 'utf8',
654
+ )
655
+ // the host resolves the rows the binding would write at load; their memory stays empty.
656
+ const values = guestDyldValueRange(map)
657
+ const bindingRows = new Map<number, number>()
658
+ const data: number[] = []
659
+ for (let at = 0; at < link.linkage.data.length; at += 3) {
660
+ const address = link.linkage.data[at]!
661
+ const size = link.linkage.data[at + 1]!
662
+ const value = link.linkage.data[at + 2]!
663
+ if (
664
+ values &&
665
+ address >= values.address &&
666
+ address < values.address + values.length
667
+ ) {
668
+ if (size !== 8 || (address - values.address) % 8)
669
+ throw new Error('sdk binding splits a dyld value')
670
+ bindingRows.set((address - values.address) / 8, value)
671
+ } else data.push(address, size, value)
672
+ }
673
+ const linked = readFileSync(
674
+ project ? projectOutputFile(work, linkedModule, project) : linkedModule,
675
+ )
676
+ // optimization must preserve these addresses and table slots.
677
+ const required = guestProjectionRegionCounts(storage.images.length, [
678
+ ...guestLinkedSpans(map, linked, linked, owners, splitInputs.appSections, true),
679
+ ...sdkSpans,
680
+ ])
681
+ if (storage.capacities.some(({ regions }, index) => required[index]! > regions)) {
682
+ storage = reserveGuestProjectionStorage(
683
+ storage.images,
684
+ storage.capacities.map((capacity, index) => {
685
+ const count = required[index]!
686
+ return count > capacity.regions
687
+ ? { ...capacity, regions: Math.max(count, capacity.regions * 2) }
688
+ : capacity
689
+ }),
690
+ )
691
+ compileAppProjection(storage.source)
692
+ continue
693
+ }
694
+ options.onStage('lowering the app image')
695
+ // let lower() spawn its child process before awaiting the final link and assembling SDK bindings.
696
+ const lowerPromise = link.lower()
697
+ await new Promise((resolve) => setImmediate(resolve))
698
+ let binding: SwiftSdkBindingAsset
699
+ try {
700
+ if (result.finalLinkDone) await result.finalLinkDone
701
+ const usage = readSwiftSdkUsage(result.mapPath, options.output, sdkObjectKeys)
702
+ binding = createSwiftSdkBinding({ ...link.linkage, data }, sdk, usage)
703
+ } catch (error) {
704
+ await Promise.allSettled([lowerPromise])
705
+ throw error
706
+ }
707
+ await lowerPromise
708
+ const finalized = finalizeGuestProjectionStorage({
709
+ map,
710
+ linked,
711
+ optimized: readFileSync(options.optimized),
712
+ owners,
713
+ sectionOwners: splitInputs.appSections,
714
+ projectionObject: appProjectionObject,
715
+ reserved: storage,
716
+ sdkSpans,
717
+ })
718
+ writeFileSync(
719
+ options.optimized,
720
+ moveGuestDyldTable(map, finalized.wasm, splitInputs.dyldSymbols, bindingRows, [
721
+ swiftAppModuleThunkSection(finalized.wasm),
722
+ ]),
723
+ )
724
+ return binding
725
+ }
726
+ } finally {
727
+ if (result.finalLinkDone) await Promise.allSettled([result.finalLinkDone])
718
728
  }
719
729
  }
720
730
  options.onStage('lowering the app image')
@@ -11,9 +11,21 @@ import { takeGuestStartupData } from './swift-guest-linked-spans'
11
11
  import {
12
12
  dropWasmCustomSection,
13
13
  readWasmCustomSection,
14
- writeWasmCustomSection,
14
+ writeWasmCustomSections,
15
15
  } from './wasm-narrow-boundary'
16
16
 
17
+ function sha256Buffer(bytes: Buffer): Buffer {
18
+ if (typeof Bun !== 'undefined')
19
+ return Buffer.from(new Bun.CryptoHasher('sha256').update(bytes).digest())
20
+ return createHash('sha256').update(bytes).digest()
21
+ }
22
+
23
+ function sha256Hex(bytes: Buffer): string {
24
+ if (typeof Bun !== 'undefined')
25
+ return new Bun.CryptoHasher('sha256').update(bytes).digest('hex')
26
+ return createHash('sha256').update(bytes).digest('hex')
27
+ }
28
+
17
29
  /** raw bytes per pack: one range read answers a lookup without fetching its neighbors. */
18
30
  const PACK_BYTES = 64 * 1024
19
31
 
@@ -65,7 +77,7 @@ export function encodeGuestDyldTable(rows: readonly GuestDyldRow[]): {
65
77
  }
66
78
  const file = Buffer.concat(packs)
67
79
  const header = Buffer.alloc(40 + packs.length * 8)
68
- createHash('sha256').update(file).digest().copy(header, 0)
80
+ sha256Buffer(file).copy(header, 0)
69
81
  header.writeUInt32LE(file.length, 32)
70
82
  header.writeUInt32LE(packs.length, 36)
71
83
  let offset = 0
@@ -88,11 +100,12 @@ export function moveGuestDyldTable(
88
100
  wasm: Uint8Array,
89
101
  symbols: readonly { name: string; image: number }[],
90
102
  bindingValues: ReadonlyMap<number, number> = new Map(),
103
+ extraSections: readonly (readonly [string, Uint8Array])[] = [],
91
104
  ): Uint8Array {
92
105
  const range = guestDyldValueRange(map)
93
106
  if (!range) {
94
107
  if (symbols.length) throw new Error('link map has no dyld values')
95
- return wasm
108
+ return extraSections.length ? writeWasmCustomSections(wasm, extraSections) : wasm
96
109
  }
97
110
  if (range.length !== symbols.length * 8)
98
111
  throw new Error('linked dyld values do not match the ledger rows')
@@ -109,11 +122,11 @@ export function moveGuestDyldTable(
109
122
  : { name, image, kind: 1, value: BigInt(binding) }
110
123
  })
111
124
  const { index, packs } = encodeGuestDyldTable(rows)
112
- return writeWasmCustomSection(
113
- writeWasmCustomSection(taken.wasm, NATIVE_DYLD_TABLE_SECTION, index),
114
- NATIVE_DYLD_PACKS_SECTION,
115
- packs,
116
- )
125
+ return writeWasmCustomSections(taken.wasm, [
126
+ [NATIVE_DYLD_TABLE_SECTION, index],
127
+ [NATIVE_DYLD_PACKS_SECTION, packs],
128
+ ...extraSections,
129
+ ])
117
130
  }
118
131
 
119
132
  /** the linked address range of the ledger's dyld values, from the final link map. */
@@ -156,6 +169,6 @@ export function splitSwiftDyldTable(wasm: Uint8Array): {
156
169
  const bytes = Buffer.from(packs)
157
170
  return {
158
171
  module: dropWasmCustomSection(wasm, NATIVE_DYLD_PACKS_SECTION),
159
- table: { sha256: createHash('sha256').update(bytes).digest('hex'), bytes },
172
+ table: { sha256: sha256Hex(bytes), bytes },
160
173
  }
161
174
  }