peachsim 0.1.2747 → 0.1.2748

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 (130) 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 +3888 -3879
  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-264PERTU.mjs → acorn-VEWLHGUF.mjs} +3 -3
  27. package/dist-lib/esm-chunks/{chunk-OS777HDT.mjs → chunk-4QUOVEXG.mjs} +3 -3
  28. package/dist-lib/esm-chunks/{chunk-6A6ESU7M.mjs → chunk-62Z4HAA5.mjs} +16 -15
  29. package/dist-lib/esm-chunks/{chunk-VBVWVPFV.mjs → chunk-6ZVR2CVC.mjs} +1 -1
  30. package/dist-lib/esm-chunks/{chunk-RQYO274I.mjs → chunk-A2EC74CI.mjs} +1 -1
  31. package/dist-lib/esm-chunks/{chunk-H5VBNND3.mjs → chunk-BYYBOHZI.mjs} +1 -1
  32. package/dist-lib/esm-chunks/{chunk-UX3TKSSG.mjs → chunk-C6IVVMZN.mjs} +1 -1
  33. package/dist-lib/esm-chunks/{chunk-SSEYL4BD.mjs → chunk-GWZFPIGW.mjs} +1 -1
  34. package/dist-lib/esm-chunks/{chunk-OGKCUDUY.mjs → chunk-HMPLB7MA.mjs} +1 -1
  35. package/dist-lib/esm-chunks/{chunk-DKOHFKVO.mjs → chunk-HPANCGMI.mjs} +2 -2
  36. package/dist-lib/esm-chunks/{chunk-BTOHCTM5.mjs → chunk-HRS3DSAZ.mjs} +6 -6
  37. package/dist-lib/esm-chunks/{chunk-LETYBISI.mjs → chunk-I3KV46CL.mjs} +1 -1
  38. package/dist-lib/esm-chunks/{chunk-3PIRJQS3.mjs → chunk-IE2LTKEG.mjs} +1 -1
  39. package/dist-lib/esm-chunks/{chunk-YL5WAVSA.mjs → chunk-KD3TI7UV.mjs} +4 -4
  40. package/dist-lib/esm-chunks/{chunk-DNPF3SJV.mjs → chunk-KNNMOD5T.mjs} +1 -1
  41. package/dist-lib/esm-chunks/{chunk-NGFHBDMP.mjs → chunk-NNVYKFSY.mjs} +1 -1
  42. package/dist-lib/esm-chunks/{chunk-SZAKV76H.mjs → chunk-RU7J2QST.mjs} +1 -1
  43. package/dist-lib/esm-chunks/{chunk-V5QS3LC7.mjs → chunk-TQACQLZ6.mjs} +1 -1
  44. package/dist-lib/esm-chunks/{chunk-VFVDWLZL.mjs → chunk-TW5FZFU7.mjs} +1 -1
  45. package/dist-lib/esm-chunks/{chunk-SNIXC3LK.mjs → chunk-U5SZIAIF.mjs} +1 -1
  46. package/dist-lib/esm-chunks/{chunk-C4WNCPE7.mjs → chunk-UFQHPARI.mjs} +119 -115
  47. package/dist-lib/esm-chunks/{chunk-R5URHHLY.mjs → chunk-UNOAFCHH.mjs} +1 -1
  48. package/dist-lib/esm-chunks/{chunk-AASYNA7O.mjs → chunk-VKYISXKK.mjs} +5 -5
  49. package/dist-lib/esm-chunks/{chunk-2KP4Q4QG.mjs → chunk-WN22FGGA.mjs} +1 -1
  50. package/dist-lib/esm-chunks/{chunk-ZGKZH72O.mjs → chunk-XM3QYC5V.mjs} +1 -1
  51. package/dist-lib/esm-chunks/{metro-layout-parser-X53TI6UE.mjs → metro-layout-parser-B74NVOKQ.mjs} +3 -3
  52. package/dist-lib/esm-chunks/{swift-package-prebuilt-BCWD6FXB.mjs → swift-package-prebuilt-B4QBXBQL.mjs} +12 -12
  53. package/dist-lib/esm-chunks/{swift-sdk-app-module-PVYYHAOX.mjs → swift-sdk-app-module-5OCMAJPP.mjs} +16 -16
  54. package/dist-lib/esm-chunks/{swift-sdk-image-YU237KAS.mjs → swift-sdk-image-LQKEASAW.mjs} +16 -16
  55. package/dist-lib/esm-chunks/{swift-sdk-image-producer-E6S3PF4W.mjs → swift-sdk-image-producer-ZMYZXL7Z.mjs} +5 -5
  56. package/dist-lib/expo-swift-bundle.cjs +124 -115
  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 +415 -406
  61. package/dist-lib/host/fetch-proxy-handler.cjs +13 -4
  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 +1 -1
  75. package/dist-lib/native-app-identity-project.mjs +15 -15
  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 +86 -85
  87. package/dist-lib/public-origins-lib.json +73 -67
  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 +415 -406
  95. package/dist-lib/swift-compile.cjs +120 -111
  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/dist-types/packages/peach-engine/src/engine/native-paint-facts.d.ts +3 -1
  123. package/dist-types/packages/peach-engine/src/engine/paint-value-input.d.ts +7 -0
  124. package/dist-types/packages/peach-engine/src/engine/scene-native-binding.d.ts +1 -1
  125. package/package.json +1 -1
  126. package/src/one-native-controls-package.ts +5 -5
  127. package/src/one-sdk-bundle.ts +1 -1
  128. package/src/swift-compiler-digest.ts +1 -0
  129. package/src/swift-sdk-surface.ts +1 -117
  130. package/src/one-sdk/shard.ts +0 -382
@@ -9,6 +9,11 @@ import {
9
9
  renderSwiftSourceGlue,
10
10
  writeSwiftPackageArtifacts,
11
11
  } from '../../one-bundler/src/nativeSourceContract'
12
+ import {
13
+ shardOneSDKBindingSource,
14
+ translateShardedOneSDKDiagnostic,
15
+ type ShardedBindingFile,
16
+ } from '../../peach-globals/build/one-sdk-shard'
12
17
  import { swiftGuestControls } from '../../peach-globals/build/one-swift-control-source'
13
18
  import { swiftUIControls } from '../../peach-globals/build/one-swift-ui-controls'
14
19
  import {
@@ -16,11 +21,6 @@ import {
16
21
  PEACH_ONE_DIRECT_HOST_MM,
17
22
  PEACH_ONE_HOST_SHIMS,
18
23
  } from './one-direct-host-source'
19
- import {
20
- shardOneSDKBindingSource,
21
- translateShardedOneSDKDiagnostic,
22
- type ShardedBindingFile,
23
- } from './one-sdk/shard'
24
24
  import { oneSDKHost, oneSDKModule, oneSDKTransport } from './one-sdk/source'
25
25
  import {
26
26
  ONE_NATIVE_CONTROLS_CONTAINER_VIEWS,
@@ -1,10 +1,10 @@
1
1
  import { createHash } from 'node:crypto'
2
2
  import fs from 'node:fs'
3
3
  import path from 'node:path'
4
+ import { shardOneSDKBindingSource } from '../../peach-globals/build/one-sdk-shard'
4
5
  import { prepareExpoNativeProject } from './expo-native-workspace'
5
6
  import { compiledModuleAssets, compiledModuleRequestHandler } from './nitro-bundle'
6
7
  import { selectOneSDK } from './one-sdk-preparation'
7
- import { shardOneSDKBindingSource } from './one-sdk/shard'
8
8
  import { oneSDKApp, oneSDKHost, oneSDKModule, oneSDKTransport } from './one-sdk/source'
9
9
  import { ONE_SDK_SOURCE_DIRECTORY, readOneSDKSourceManifest } from './oneSdkContract'
10
10
  import { syncSwiftTree } from './swift-compile'
@@ -12,6 +12,7 @@ import { readIosSdkTree } from './swift-objc-sdk-tree'
12
12
  export const SWIFT_DIGEST_SOURCES = [
13
13
  'swift-compile.ts',
14
14
  'swift-sdk-surface.ts',
15
+ '../../peach-globals/build/swift-source-mask.ts',
15
16
  'swift-wasm-archive.ts',
16
17
  'swift-lexical.ts',
17
18
  'swift-header-compiler.ts',
@@ -17,6 +17,7 @@ import {
17
17
  } from 'node:fs'
18
18
  import os from 'node:os'
19
19
  import path from 'node:path'
20
+ import { maskSwiftCode } from '../../peach-globals/build/swift-source-mask'
20
21
  import { windowAdmission } from './admitted-mac-build'
21
22
  import {
22
23
  parseSwiftDistributionManifest,
@@ -1081,123 +1082,6 @@ export function swiftCodeMask(source: string): string {
1081
1082
  return masked
1082
1083
  }
1083
1084
 
1084
- function maskSwiftCode(source: string): string {
1085
- const chars = source.split('')
1086
- const blank = (from: number, to: number): void => {
1087
- for (let i = Math.max(0, from); i < Math.min(to, chars.length); i++)
1088
- if (chars[i] !== '\n') chars[i] = ' '
1089
- }
1090
- const hashesAt = (at: number): number => {
1091
- let end = at
1092
- while (source[end] === '#') end++
1093
- return end - at
1094
- }
1095
- const scanString = (quote: number, hashes: number): number => {
1096
- const multiline = source.startsWith('"""', quote)
1097
- let i = quote + (multiline ? 3 : 1)
1098
- const keep: [number, number][] = []
1099
- const quoteEnd = (at: number): boolean => {
1100
- const mark = multiline ? '"""' : '"'
1101
- return (
1102
- source.startsWith(mark, at) &&
1103
- source.startsWith('#'.repeat(hashes), at + mark.length)
1104
- )
1105
- }
1106
- while (i < chars.length) {
1107
- if (source[i] === '\\' && source.startsWith('#'.repeat(hashes), i + 1)) {
1108
- const escaped = i + 1 + hashes
1109
- if (source[escaped] === '(') {
1110
- const codeStart = escaped + 1
1111
- const codeEnd = scanCode(codeStart, true)
1112
- keep.push([codeStart, codeEnd])
1113
- i = codeEnd + 1
1114
- } else {
1115
- i = escaped + 1
1116
- }
1117
- continue
1118
- }
1119
- if (quoteEnd(i)) {
1120
- const close = i + (multiline ? 3 : 1) + hashes
1121
- let cursor = quote - hashes
1122
- const ranges: [number, number][] = [...keep, [close, close]]
1123
- for (const [from, to] of ranges)
1124
- if (from > cursor) {
1125
- blank(cursor, from)
1126
- cursor = to
1127
- } else cursor = Math.max(cursor, to)
1128
- return close
1129
- }
1130
- i++
1131
- }
1132
- blank(quote - hashes, chars.length)
1133
- return chars.length
1134
- }
1135
- // an interpolation's `)` ends the scan only when no paren is open inside
1136
- const scanCode = (i: number, nested: boolean): number => {
1137
- let parens = 0
1138
- while (i < chars.length) {
1139
- const char = source[i]!
1140
- if (char === '/' && source[i + 1] === '/') {
1141
- const end = source.indexOf('\n', i)
1142
- blank(i, end === -1 ? chars.length : end)
1143
- i = end === -1 ? chars.length : end
1144
- continue
1145
- }
1146
- if (char === '/' && source[i + 1] === '*') {
1147
- let j = i + 2
1148
- let depth = 1
1149
- while (j < chars.length && depth > 0) {
1150
- if (source.startsWith('/*', j)) {
1151
- depth++
1152
- j += 2
1153
- } else if (source.startsWith('*/', j)) {
1154
- depth--
1155
- j += 2
1156
- } else j++
1157
- }
1158
- blank(i, j)
1159
- i = j
1160
- continue
1161
- }
1162
- if (char === '#') {
1163
- const hashes = hashesAt(i)
1164
- const at = i + hashes
1165
- if (source[at] === '"') {
1166
- i = scanString(at, hashes)
1167
- continue
1168
- }
1169
- if (hashes > 0 && source[at] === '/') {
1170
- const mark = '#'.repeat(hashes)
1171
- let j = at + 1
1172
- while (
1173
- j < chars.length &&
1174
- !(source[j] === '/' && source.startsWith(mark, j + 1))
1175
- )
1176
- j++
1177
- blank(i, Math.min(chars.length, j + 1 + hashes))
1178
- i = Math.min(chars.length, j + 1 + hashes)
1179
- continue
1180
- }
1181
- i++
1182
- continue
1183
- }
1184
- if (char === '"') {
1185
- i = scanString(i, 0)
1186
- continue
1187
- }
1188
- if (char === '(') parens++
1189
- else if (char === ')') {
1190
- if (nested && parens === 0) return i
1191
- parens--
1192
- }
1193
- i++
1194
- }
1195
- return i
1196
- }
1197
- scanCode(0, false)
1198
- return chars.join('')
1199
- }
1200
-
1201
1085
  // darwin declares these in Foundation; the guest networking module owns them.
1202
1086
  export const FOUNDATION_NETWORKING_TYPES = new Set([
1203
1087
  'NSURLConnection',
@@ -1,382 +0,0 @@
1
- import { createHash } from 'node:crypto'
2
- import path from 'node:path'
3
- import { swiftCodeMask } from '../swift-compile'
4
-
5
- export const ONE_SDK_BINDING_SHARDS = 8
6
- export const ONE_SDK_BINDING_SHARD_MIN_BYTES = 256 * 1024
7
-
8
- interface SourceLine {
9
- text: string
10
- line: number
11
- }
12
-
13
- type BindingChunk =
14
- | { kind: 'enumDecl'; enumName: string; lines: SourceLine[] }
15
- | { kind: 'top'; key: string; lines: SourceLine[] }
16
- | { kind: 'enumMember'; enumName: string; key: string; lines: SourceLine[] }
17
-
18
- export interface ShardedBindingFile {
19
- name: string
20
- source: string
21
- originalName: string
22
- // 1-based original source line for each 0-based line in `source` (0 for synthetic lines)
23
- lineMap: number[]
24
- }
25
-
26
- function unsharded(name: string, source: string): ShardedBindingFile[] {
27
- const lineCount = source.split('\n').length
28
- return [
29
- {
30
- name,
31
- source,
32
- originalName: name,
33
- lineMap: Array.from({ length: lineCount }, (_, index) => index + 1),
34
- },
35
- ]
36
- }
37
-
38
- function chunkKey(lines: readonly SourceLine[]): string {
39
- for (const { text } of lines) {
40
- const trimmed = text.trim()
41
- if (trimmed !== '' && !trimmed.startsWith('//')) return trimmed
42
- }
43
- return lines.map((item) => item.text).join('\n')
44
- }
45
-
46
- function stableShardIndex(key: string, shardCount: number): number {
47
- return createHash('sha256').update(key).digest().readUInt32BE(0) % shardCount
48
- }
49
-
50
- // split large generated binding files across compilation units so swiftc can
51
- // compile OneSDKBindings in parallel under -no-whole-module-optimization and
52
- // incremental rebuilds recompile only changed shards.
53
- export function shardOneSDKBindingSource(
54
- name: string,
55
- source: string,
56
- shardCount = ONE_SDK_BINDING_SHARDS,
57
- ): ShardedBindingFile[] {
58
- if (
59
- shardCount <= 1 ||
60
- !name.endsWith('.swift') ||
61
- name.endsWith('.types.swift') ||
62
- source.length < ONE_SDK_BINDING_SHARD_MIN_BYTES
63
- ) {
64
- return unsharded(name, source)
65
- }
66
- const masked = swiftCodeMask(source)
67
- if (/\bfileprivate\b/.test(masked)) return unsharded(name, source)
68
- const lines = source.split('\n')
69
- const maskedLines = masked.split('\n')
70
- if (lines.length !== maskedLines.length) return unsharded(name, source)
71
-
72
- const headerLines: SourceLine[] = []
73
- let index = 0
74
- let headerIfDepth = 0
75
- while (index < lines.length) {
76
- const text = lines[index]!
77
- const trimmed = text.trim()
78
- if (
79
- trimmed === '' ||
80
- trimmed.startsWith('// generated ') ||
81
- trimmed.startsWith('import ')
82
- ) {
83
- headerLines.push({ text, line: index + 1 })
84
- index++
85
- continue
86
- }
87
- if (trimmed.startsWith('#if ')) {
88
- headerIfDepth++
89
- headerLines.push({ text, line: index + 1 })
90
- index++
91
- continue
92
- }
93
- if (
94
- headerIfDepth > 0 &&
95
- (trimmed.startsWith('#elseif ') || trimmed === '#else' || trimmed === '#endif')
96
- ) {
97
- if (trimmed === '#endif') headerIfDepth--
98
- headerLines.push({ text, line: index + 1 })
99
- index++
100
- continue
101
- }
102
- break
103
- }
104
- if (headerIfDepth !== 0) return unsharded(name, source)
105
- while (headerLines.length > 0 && headerLines.at(-1)!.text.trim() === '') {
106
- headerLines.pop()
107
- }
108
-
109
- const chunks: BindingChunk[] = []
110
- let depth = 0
111
- let pending: SourceLine[] = []
112
- let hadBrace = false
113
- let inImplementation = false
114
- let inOpen = false
115
- let openDeclarations = 0
116
- let activeEnum: string | null = null
117
- let enumDeclLine = 0
118
- let memberLines: SourceLine[] = []
119
- let memberHadBrace = false
120
-
121
- for (; index < lines.length; index++) {
122
- const text = lines[index]!
123
- const lineNo = index + 1
124
- const maskedLine = maskedLines[index]!
125
- if (depth <= 1 && /^\s*#/.test(maskedLine)) return unsharded(name, source)
126
- let delta = 0
127
- for (let i = 0; i < maskedLine.length; i++) {
128
- const ch = maskedLine[i]
129
- if (ch === '{') delta++
130
- else if (ch === '}') delta--
131
- }
132
- if (activeEnum === null) {
133
- if (
134
- depth === 0 &&
135
- !hadBrace &&
136
- /^\s*(?:@\w+(?:\([^)]*\))?\s+)*private\b/.test(maskedLine)
137
- ) {
138
- return unsharded(name, source)
139
- }
140
- if (
141
- depth === 0 &&
142
- !hadBrace &&
143
- !inImplementation &&
144
- text.startsWith('// implementation ')
145
- ) {
146
- inImplementation = true
147
- pending.push({ text, line: lineNo })
148
- continue
149
- }
150
- if (inImplementation) {
151
- pending.push({ text, line: lineNo })
152
- depth += delta
153
- if (depth < 0) return unsharded(name, source)
154
- if (depth === 0 && text === '// end implementation') {
155
- chunks.push({ kind: 'top', key: chunkKey(pending), lines: pending })
156
- pending = []
157
- hadBrace = false
158
- inImplementation = false
159
- }
160
- continue
161
- }
162
- if (depth === 0 && !hadBrace && !inOpen && text.startsWith('// open ')) {
163
- inOpen = true
164
- openDeclarations = 0
165
- pending.push({ text, line: lineNo })
166
- continue
167
- }
168
- if (inOpen) {
169
- pending.push({ text, line: lineNo })
170
- if (delta !== 0 || depth > 0) hadBrace = true
171
- depth += delta
172
- if (depth < 0) return unsharded(name, source)
173
- if (hadBrace && depth === 0) {
174
- openDeclarations++
175
- hadBrace = false
176
- // each `// open` block emits a protocol followed by its conforming extension
177
- if (openDeclarations === 2) {
178
- chunks.push({ kind: 'top', key: chunkKey(pending), lines: pending })
179
- pending = []
180
- inOpen = false
181
- openDeclarations = 0
182
- }
183
- }
184
- continue
185
- }
186
- const enumMatch =
187
- depth === 0 &&
188
- !hadBrace &&
189
- pending.every(
190
- (item) => item.text.trim() === '' || item.text.trim().startsWith('//'),
191
- )
192
- ? /^enum (OneSDK_\w+)\s*\{\s*$/.exec(maskedLine)
193
- : null
194
- if (enumMatch && delta === 1) {
195
- const enumName = enumMatch[1]!
196
- const prefix = pending.filter((item) => item.text.trim() !== '')
197
- enumDeclLine = lineNo
198
- chunks.push({
199
- kind: 'enumDecl',
200
- enumName,
201
- lines: [...prefix, { text: `enum ${enumName} {}`, line: lineNo }],
202
- })
203
- pending = []
204
- activeEnum = enumName
205
- depth = 1
206
- continue
207
- }
208
- if (depth === 0 && !hadBrace && text.trim() === '' && pending.length === 0) {
209
- continue
210
- }
211
- pending.push({ text, line: lineNo })
212
- if (delta !== 0 || depth > 0) hadBrace = true
213
- depth += delta
214
- if (depth < 0) return unsharded(name, source)
215
- if (hadBrace && depth === 0) {
216
- chunks.push({ kind: 'top', key: chunkKey(pending), lines: pending })
217
- pending = []
218
- hadBrace = false
219
- }
220
- } else {
221
- const nextDepth = depth + delta
222
- if (nextDepth < 0) return unsharded(name, source)
223
- if (depth === 1 && nextDepth === 0) {
224
- if (memberHadBrace || maskedLine.trim() !== '}') return unsharded(name, source)
225
- if (
226
- memberLines.some(
227
- (item) => item.text.trim() !== '' && !item.text.trim().startsWith('//'),
228
- )
229
- ) {
230
- chunks.push({
231
- kind: 'enumMember',
232
- enumName: activeEnum,
233
- key: chunkKey(memberLines),
234
- lines: memberLines,
235
- })
236
- }
237
- activeEnum = null
238
- depth = 0
239
- memberLines = []
240
- continue
241
- }
242
- if (
243
- depth === 1 &&
244
- !memberHadBrace &&
245
- /^\s*(?:@\w+(?:\([^)]*\))?\s+)*private\b/.test(maskedLine)
246
- ) {
247
- return unsharded(name, source)
248
- }
249
- if (depth === 1 && !memberHadBrace && text.trim() === '') {
250
- depth = nextDepth
251
- continue
252
- }
253
- memberLines.push({ text, line: lineNo })
254
- if (nextDepth > 1 || delta !== 0) memberHadBrace = true
255
- depth = nextDepth
256
- if (
257
- (memberHadBrace && depth === 1) ||
258
- (!memberHadBrace &&
259
- depth === 1 &&
260
- /^\s*(?:@\w+(?:\([^)]*\))?\s+)*static\s+let\b/.test(maskedLine))
261
- ) {
262
- chunks.push({
263
- kind: 'enumMember',
264
- enumName: activeEnum,
265
- key: chunkKey(memberLines),
266
- lines: memberLines,
267
- })
268
- memberLines = []
269
- memberHadBrace = false
270
- }
271
- }
272
- }
273
-
274
- if (
275
- depth !== 0 ||
276
- activeEnum !== null ||
277
- inImplementation ||
278
- inOpen ||
279
- pending.some(
280
- (item) => item.text.trim() !== '' && !item.text.trim().startsWith('//'),
281
- ) ||
282
- chunks.length < shardCount
283
- ) {
284
- return unsharded(name, source)
285
- }
286
-
287
- const buckets: BindingChunk[][] = Array.from({ length: shardCount }, () => [])
288
- for (const chunk of chunks) {
289
- if (chunk.kind === 'enumDecl') {
290
- buckets[0]!.push(chunk)
291
- } else {
292
- buckets[stableShardIndex(chunk.key, shardCount)]!.push(chunk)
293
- }
294
- }
295
- const nonEmpty = buckets
296
- .map((bucket, index) => ({ bucket, index }))
297
- .filter(({ bucket }) => bucket.length > 0)
298
- if (nonEmpty.length < 2) return unsharded(name, source)
299
-
300
- const stem = name.slice(0, -'.swift'.length)
301
- return nonEmpty.map(({ bucket, index: shardIndex }) => {
302
- const outLines: string[] = []
303
- const lineMap: number[] = []
304
- const pushLine = (text: string, origLine: number) => {
305
- outLines.push(text)
306
- lineMap.push(origLine)
307
- }
308
- for (const item of headerLines) pushLine(item.text, item.line)
309
- pushLine('', 0)
310
-
311
- let openEnum: string | null = null
312
- let enumBody: SourceLine[][] = []
313
- let hasPart = false
314
- const startPart = () => {
315
- if (hasPart) pushLine('', 0)
316
- hasPart = true
317
- }
318
- const flushEnum = () => {
319
- if (!openEnum) return
320
- startPart()
321
- pushLine(`extension ${openEnum} {`, enumDeclLine)
322
- for (let i = 0; i < enumBody.length; i++) {
323
- if (i > 0) pushLine('', 0)
324
- for (const item of enumBody[i]!) pushLine(item.text, item.line)
325
- }
326
- pushLine('}', enumDeclLine)
327
- openEnum = null
328
- enumBody = []
329
- }
330
- for (const chunk of bucket) {
331
- if (chunk.kind === 'enumMember') {
332
- if (openEnum !== chunk.enumName) {
333
- flushEnum()
334
- openEnum = chunk.enumName
335
- }
336
- enumBody.push(chunk.lines)
337
- } else {
338
- flushEnum()
339
- startPart()
340
- for (const item of chunk.lines) pushLine(item.text, item.line)
341
- }
342
- }
343
- flushEnum()
344
- pushLine('', 0)
345
- return {
346
- name: `${stem}_${shardIndex}.swift`,
347
- source: outLines.join('\n'),
348
- originalName: name,
349
- lineMap,
350
- }
351
- })
352
- }
353
-
354
- // maps sharded OneSDKBindings diagnostics (swiftc line errors and wasm-ld
355
- // object filenames) back to the unsharded source filename and line numbers so
356
- // sdk-generator's reemitForWasi matches entries against its unsharded output.
357
- export function translateShardedOneSDKDiagnostic(
358
- diagnostic: string,
359
- shards: ReadonlyMap<string, ShardedBindingFile> | undefined,
360
- ): string {
361
- if (!shards?.size) return diagnostic
362
- return diagnostic
363
- .split('\n')
364
- .map((line) => {
365
- const source = /^(.*?)([^/:\s]+_\d+\.swift):(\d+):(\d+)(:.*)$/.exec(line)
366
- if (source) {
367
- const shard = shards.get(source[2]!)
368
- if (shard) {
369
- const lineNumber = Number(source[3])
370
- const originalLine = shard.lineMap[lineNumber - 1]
371
- if (!originalLine) return ''
372
- const originalBase = path.posix.basename(shard.originalName)
373
- return `${source[1]}${originalBase}:${originalLine}:${source[4]}${source[5]}`
374
- }
375
- }
376
- return line.replace(/([^/:\s]+_\d+\.swift)\.o\b/g, (full, file: string) => {
377
- const shard = shards.get(file)
378
- return shard ? `${path.posix.basename(shard.originalName)}.o` : full
379
- })
380
- })
381
- .join('\n')
382
- }