peachsim 0.1.2592 → 0.1.2594

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 (138) 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 +1737 -1648
  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-67PU73QE.mjs → acorn-M67PRRGJ.mjs} +3 -3
  27. package/dist-lib/esm-chunks/{chunk-EWHA6X3Z.mjs → chunk-3VEDDAPT.mjs} +5 -5
  28. package/dist-lib/esm-chunks/{chunk-DFF5XZ5M.mjs → chunk-4AOZYVB2.mjs} +1 -1
  29. package/dist-lib/esm-chunks/{chunk-PITO2NI2.mjs → chunk-6MZ5FKMK.mjs} +2 -2
  30. package/dist-lib/esm-chunks/{chunk-6DB5FR7S.mjs → chunk-EDBGKIQ2.mjs} +1 -1
  31. package/dist-lib/esm-chunks/{chunk-EITX573K.mjs → chunk-FFHDCOXF.mjs} +1 -1
  32. package/dist-lib/esm-chunks/{chunk-GY6TE4B4.mjs → chunk-GER2Z6BI.mjs} +4 -4
  33. package/dist-lib/esm-chunks/{chunk-QSFWBVUO.mjs → chunk-HJJI36KV.mjs} +1 -1
  34. package/dist-lib/esm-chunks/{chunk-EHRSWFTD.mjs → chunk-INOLFCG5.mjs} +3 -3
  35. package/dist-lib/esm-chunks/{chunk-CF3MTPMY.mjs → chunk-IUDVBSA4.mjs} +1 -1
  36. package/dist-lib/esm-chunks/{chunk-GQ3EO2UV.mjs → chunk-NN73CP4F.mjs} +1 -1
  37. package/dist-lib/esm-chunks/{chunk-XZ3FX3HW.mjs → chunk-PESI6OUB.mjs} +1 -1
  38. package/dist-lib/esm-chunks/{chunk-RV4ACE6R.mjs → chunk-QHYHKB3J.mjs} +1 -1
  39. package/dist-lib/esm-chunks/{chunk-KDI5YYQ6.mjs → chunk-QSPPJOT6.mjs} +1 -1
  40. package/dist-lib/esm-chunks/{chunk-R3WTQB5J.mjs → chunk-QUMKCF67.mjs} +1 -1
  41. package/dist-lib/esm-chunks/{chunk-FYXXFYML.mjs → chunk-RESE3ERA.mjs} +1 -1
  42. package/dist-lib/esm-chunks/{chunk-TJZPNFVF.mjs → chunk-SBHXHWKH.mjs} +1 -1
  43. package/dist-lib/esm-chunks/{chunk-M4QLI2SY.mjs → chunk-SV76NV36.mjs} +1 -1
  44. package/dist-lib/esm-chunks/{chunk-XSQH4R5E.mjs → chunk-UAEIMMNH.mjs} +69 -48
  45. package/dist-lib/esm-chunks/{chunk-D4LYKRRB.mjs → chunk-X242Y5KS.mjs} +1 -1
  46. package/dist-lib/esm-chunks/{chunk-L5T3HBPO.mjs → chunk-XM3OLATO.mjs} +240 -206
  47. package/dist-lib/esm-chunks/{chunk-SW4PSSTF.mjs → chunk-YGDCMWUX.mjs} +1 -1
  48. package/dist-lib/esm-chunks/{chunk-5FTHILIE.mjs → chunk-Z6E7GAQS.mjs} +1 -1
  49. package/dist-lib/esm-chunks/{metro-layout-parser-EX65MARK.mjs → metro-layout-parser-ZJUAU4C3.mjs} +3 -3
  50. package/dist-lib/esm-chunks/{swift-package-prebuilt-3BPDQVA7.mjs → swift-package-prebuilt-2FFVA7SB.mjs} +10 -10
  51. package/dist-lib/esm-chunks/{swift-sdk-app-module-CRX6E3TR.mjs → swift-sdk-app-module-A2ZJMCBX.mjs} +30 -20
  52. package/dist-lib/esm-chunks/{swift-sdk-image-EJD5TPQH.mjs → swift-sdk-image-HJLWK2B6.mjs} +14 -14
  53. package/dist-lib/esm-chunks/{swift-sdk-image-producer-QAZOSLTZ.mjs → swift-sdk-image-producer-7EM7U2LR.mjs} +4 -4
  54. package/dist-lib/expo-swift-bundle.cjs +324 -235
  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 +480 -255
  59. package/dist-lib/host/fetch-proxy-handler.cjs +67 -16
  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 +5 -5
  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 +71 -71
  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 +480 -255
  93. package/dist-lib/swift-compile.cjs +286 -230
  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/binaryen.lock.json +3 -3
  128. package/src/host/fetch-proxy-handler.ts +63 -9
  129. package/src/one-native-controls-package.ts +95 -10
  130. package/src/one-sdk-preparation.ts +40 -2
  131. package/src/swift-guest-compile.ts +11 -2
  132. package/src/swift-guest-projection-storage.ts +45 -26
  133. package/src/swift-guest-runtime-plan.ts +1 -1
  134. package/src/swift-sdk-app-module.ts +15 -6
  135. package/src/swift-sdk-image-wasm.ts +5 -2
  136. package/src/swift-sdk-image.ts +69 -49
  137. package/src/swift-wasm-prune.ts +51 -34
  138. package/src/wasm-optimize.ts +146 -140
@@ -810,18 +810,32 @@ function parseWasmObjectBytes(
810
810
  dataOwnerSymIdx[kept] = ownerSymIdx
811
811
  kept++
812
812
  }
813
- const dataRelocs: DataRelocs = {
814
- count: kept,
815
- bytes: rawData.bytes,
816
- start: rawData.start.slice(0, kept),
817
- end: rawData.end.slice(0, kept),
818
- ro: rawData.ro.slice(0, kept),
819
- rs: rawData.rs.slice(0, kept),
820
- addend: rawData.addend.slice(0, kept),
821
- segIdx: dataSegIdx.slice(0, kept),
822
- offInSeg: dataOffInSeg.slice(0, kept),
823
- ownerSymIdx: dataOwnerSymIdx.slice(0, kept),
824
- }
813
+ const dataRelocs: DataRelocs =
814
+ kept === rawData.count
815
+ ? {
816
+ count: kept,
817
+ bytes: rawData.bytes,
818
+ start: rawData.start,
819
+ end: rawData.end,
820
+ ro: rawData.ro,
821
+ rs: rawData.rs,
822
+ addend: rawData.addend,
823
+ segIdx: dataSegIdx,
824
+ offInSeg: dataOffInSeg,
825
+ ownerSymIdx: dataOwnerSymIdx,
826
+ }
827
+ : {
828
+ count: kept,
829
+ bytes: rawData.bytes,
830
+ start: rawData.start.slice(0, kept),
831
+ end: rawData.end.slice(0, kept),
832
+ ro: rawData.ro.slice(0, kept),
833
+ rs: rawData.rs.slice(0, kept),
834
+ addend: rawData.addend.slice(0, kept),
835
+ segIdx: dataSegIdx.slice(0, kept),
836
+ offInSeg: dataOffInSeg.slice(0, kept),
837
+ ownerSymIdx: dataOwnerSymIdx.slice(0, kept),
838
+ }
825
839
 
826
840
  // swift places constant metadata in one explicit .rodata segment, and the
827
841
  // assembler folds a relative pointer between two symbols of that segment
@@ -829,26 +843,29 @@ function parseWasmObjectBytes(
829
843
  // nested type's parent). a relocation-free word that lands on another
830
844
  // symbol's start, past swift's two low int-pair flag bits, keeps that symbol
831
845
  // live as a relocation would; a coincidental match only keeps extra data.
832
- const relocatedWords = new Map<number, Set<number>>()
833
- for (let i = 0; i < dataRelocs.count; i++) {
834
- const segIdx = dataRelocs.segIdx[i]!
835
- let words = relocatedWords.get(segIdx)
836
- if (!words) relocatedWords.set(segIdx, (words = new Set()))
837
- words.add(dataRelocs.offInSeg[i]!)
838
- }
839
- for (const [segIdx, tops] of segTopSyms) {
840
- const starts = new Map(tops.map((top) => [top.off, top.symIdx]))
841
- const relocated = relocatedWords.get(segIdx)
842
- const payload = dataStart + sPayloadStarts[segIdx]!
843
- const view = new DataView(buf.buffer, buf.byteOffset + payload)
844
- for (const top of tops) {
845
- for (let at = top.off; at + 4 <= top.end; at += 4) {
846
- if (relocated?.has(at)) continue
847
- const target = starts.get(at + (view.getInt32(at, true) & ~3))
848
- if (target === undefined || target === top.symIdx) continue
849
- let list = symDataRelocs.get(top.symIdx)
850
- if (!list) symDataRelocs.set(top.symIdx, (list = []))
851
- list.push(target)
846
+ if (segTopSyms.size > 0) {
847
+ const relocatedWords = new Map<number, Set<number>>()
848
+ for (let i = 0; i < dataRelocs.count; i++) {
849
+ const segIdx = dataRelocs.segIdx[i]!
850
+ if (!segTopSyms.has(segIdx)) continue
851
+ let words = relocatedWords.get(segIdx)
852
+ if (!words) relocatedWords.set(segIdx, (words = new Set()))
853
+ words.add(dataRelocs.offInSeg[i]!)
854
+ }
855
+ for (const [segIdx, tops] of segTopSyms) {
856
+ const starts = new Map(tops.map((top) => [top.off, top.symIdx]))
857
+ const relocated = relocatedWords.get(segIdx)
858
+ const payload = dataStart + sPayloadStarts[segIdx]!
859
+ const view = new DataView(buf.buffer, buf.byteOffset + payload)
860
+ for (const top of tops) {
861
+ for (let at = top.off; at + 4 <= top.end; at += 4) {
862
+ if (relocated?.has(at)) continue
863
+ const target = starts.get(at + (view.getInt32(at, true) & ~3))
864
+ if (target === undefined || target === top.symIdx) continue
865
+ let list = symDataRelocs.get(top.symIdx)
866
+ if (!list) symDataRelocs.set(top.symIdx, (list = []))
867
+ list.push(target)
868
+ }
852
869
  }
853
870
  }
854
871
  }
@@ -1056,7 +1073,7 @@ function parseWasmObjectBytes(
1056
1073
  }
1057
1074
 
1058
1075
  function parseWasmObjectFile(file: string, isDist: boolean): ParsedObject {
1059
- return parseWasmObjectBytes(file, new Uint8Array(fs.readFileSync(file)), isDist)
1076
+ return parseWasmObjectBytes(file, fs.readFileSync(file), isDist)
1060
1077
  }
1061
1078
 
1062
1079
  function parseWasmObjectCached(
@@ -1080,7 +1097,7 @@ export function localizeDefinedWasmSymbols(
1080
1097
  ): Uint8Array {
1081
1098
  const offsets: number[] = []
1082
1099
  const object = parseWasmObjectBytes('', bytes, false, offsets)
1083
- const output = Uint8Array.from(bytes)
1100
+ const output = new Uint8Array(bytes)
1084
1101
  let changed = 0
1085
1102
  for (let index = 0; index < object.symNames.length; index++) {
1086
1103
  if (!names.has(object.symNames[index]!)) continue
@@ -1,5 +1,14 @@
1
1
  import { execFile } from 'node:child_process'
2
- import { existsSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs'
2
+ import {
3
+ closeSync,
4
+ existsSync,
5
+ fstatSync,
6
+ openSync,
7
+ readFileSync,
8
+ readSync,
9
+ rmSync,
10
+ writeFileSync,
11
+ } from 'node:fs'
3
12
  import { dirname, join } from 'node:path'
4
13
  import { promisify } from 'node:util'
5
14
  import { installVerifiedTarball } from '@contrast/runtime-delivery'
@@ -17,7 +26,6 @@ import {
17
26
  NARROW_MERGE_NAMES,
18
27
  narrowWasmBoundary,
19
28
  readWasmMemory,
20
- hasWasmExport,
21
29
  withWasmTableMinimum,
22
30
  } from './wasm-narrow-boundary'
23
31
 
@@ -50,6 +58,48 @@ export const WASM_LOWER_IDENTITY = `binaryen ${BINARYEN_VERSION} narrow ${LOWERI
50
58
 
51
59
  const BINARYEN_RELEASES = new Map(Object.entries(binaryenLock.platforms))
52
60
 
61
+ // reads only the wasm prefix through section 5 (memory) so multi-hundred-MB
62
+ // threaded artifacts are never loaded into JS memory just to inspect memory flags.
63
+ function readWasmMemoryFromFile(file: string) {
64
+ const fd = openSync(file, 'r')
65
+ try {
66
+ const total = fstatSync(fd).size
67
+ const header = Buffer.allocUnsafe(8)
68
+ if (total < 8 || readSync(fd, header, 0, 8, 0) < 8) return undefined
69
+ let offset = 8
70
+ const probe = Buffer.allocUnsafe(16)
71
+ let prefixEnd = 8
72
+ while (offset < total) {
73
+ const read = readSync(fd, probe, 0, Math.min(16, total - offset), offset)
74
+ if (read < 2) break
75
+ const id = probe[0]!
76
+ let size = 0
77
+ let shift = 0
78
+ let at = 1
79
+ for (;;) {
80
+ const byte = probe[at++]!
81
+ size += (byte & 127) * 2 ** shift
82
+ if (!(byte & 128)) break
83
+ shift += 7
84
+ }
85
+ const next = offset + at + size
86
+ if (id === 2 || id === 5) prefixEnd = next
87
+ if (id > 5) break
88
+ offset = next
89
+ }
90
+ const prefix = Buffer.allocUnsafe(prefixEnd)
91
+ let filled = 0
92
+ while (filled < prefixEnd) {
93
+ const n = readSync(fd, prefix, filled, prefixEnd - filled, filled)
94
+ if (n <= 0) break
95
+ filled += n
96
+ }
97
+ return readWasmMemory(prefix.subarray(0, filled))
98
+ } finally {
99
+ closeSync(fd)
100
+ }
101
+ }
102
+
53
103
  // skipPasses names Binaryen passes to leave out. a guest link with several images
54
104
  // skips duplicate-function-elimination, which would merge identical functions
55
105
  // that different images must keep separate.
@@ -80,25 +130,89 @@ async function runWasmOpt(
80
130
  }
81
131
  // an unoptimized link keeps its name section, as before lowering, so a trap names its function
82
132
  const optimizeSize = optimize === true || optimize === 'app-module'
83
- const optimizeBeforeGuard = optimize === true
84
133
  const relink = optimize === 'relink'
85
- const names = optimizeBeforeGuard && !KEEP_WASM_NAMES ? [] : ['-g']
86
- const outputNames = (optimizeSize || relink) && !KEEP_WASM_NAMES ? [] : names
87
- let input = readFileSync(file)
88
- const memory = readWasmMemory(input)
89
- const checked = `${output}.stack-checked`
134
+ const keepNames = (!optimizeSize && !relink) || KEEP_WASM_NAMES
135
+ const memory = readWasmMemoryFromFile(file)
90
136
  if (memory?.shared && memory.maximumPages !== 65536)
91
137
  throw new Error('threaded native memory must declare a 65536-page maximum')
92
- if (memory?.shared && optimizeBeforeGuard) {
93
- await runBinaryen(binary, [...features, '-g', '--stack-check', file, '-o', checked], {
94
- env: environment,
95
- })
96
- input = readFileSync(checked)
97
- if (!hasWasmExport(input, '__set_stack_limits'))
98
- throw new Error(
99
- 'threaded native artifact must retain its __stack_pointer name for bounds checks',
138
+ if (memory?.shared) {
139
+ // narrow, lower, guard, adapt and optimize in one Binaryen process instead of
140
+ // writing and reparsing hundreds of megabytes across separate tool invocations.
141
+ const wideExportsFile = wideExports.size ? `${output}.wide-exports` : undefined
142
+ const tableTool = join(dirname(binary), 'wasm-table-guard')
143
+ try {
144
+ if (wideExportsFile)
145
+ writeFileSync(wideExportsFile, [...wideExports].join('\n') + '\n')
146
+ const pipelineArgs = [
147
+ '--pipeline',
148
+ '--in',
149
+ file,
150
+ '--out',
151
+ output,
152
+ '--narrow',
153
+ ...(wideExportsFile ? ['--wide-exports-file', wideExportsFile] : []),
154
+ '--lower',
155
+ '--shared',
156
+ ...(keepNames ? ['--debug'] : []),
157
+ ...(sdkTableEnd !== undefined
158
+ ? ['--slots-range', '1', String(sdkTableEnd), '--adapt']
159
+ : []),
160
+ ...(optimizeSize ? ['--optimize', 'oz'] : relink ? ['--optimize', 'relink'] : []),
161
+ ...skipPasses.flatMap((pass) => ['--skip-pass', pass]),
162
+ ]
163
+ const { stdout } = await runBinaryen(tableTool, pipelineArgs, { env: environment })
164
+ const metadata: unknown = JSON.parse(stdout)
165
+ if (
166
+ typeof metadata !== 'object' ||
167
+ metadata === null ||
168
+ !('tableMinimum' in metadata) ||
169
+ typeof metadata.tableMinimum !== 'number' ||
170
+ !Number.isSafeInteger(metadata.tableMinimum) ||
171
+ metadata.tableMinimum < 0
172
+ )
173
+ throw new Error('native table tool returned an invalid table minimum')
174
+ const sectionName = new TextEncoder().encode(NATIVE_IMAGE_SECTION)
175
+ const layout = new TextEncoder().encode(
176
+ JSON.stringify({
177
+ initialPages: memory.initialPages,
178
+ tableMinimum: metadata.tableMinimum,
179
+ }),
180
+ )
181
+ const encode = (value: number) => {
182
+ const bytes: number[] = []
183
+ do {
184
+ const byte = value & 127
185
+ value >>>= 7
186
+ bytes.push(byte | (value ? 128 : 0))
187
+ } while (value)
188
+ return bytes
189
+ }
190
+ const body = new Uint8Array([
191
+ ...encode(sectionName.length),
192
+ ...sectionName,
193
+ ...layout,
194
+ ])
195
+ const bytes = readFileSync(output)
196
+ // a thread table that grows after its instance exists costs a second copy.
197
+ writeFileSync(
198
+ output,
199
+ Buffer.concat([
200
+ sdkTableEnd === undefined
201
+ ? withWasmTableMinimum(
202
+ bytes,
203
+ metadata.tableMinimum + NATIVE_TABLE_RESERVE_SLOTS,
204
+ )
205
+ : bytes,
206
+ Buffer.from([0, ...encode(body.length), ...body]),
207
+ ]),
100
208
  )
209
+ } finally {
210
+ if (wideExportsFile) rmSync(wideExportsFile, { force: true })
211
+ }
212
+ return
101
213
  }
214
+ const outputNames = keepNames ? ['-g'] : []
215
+ const input = readFileSync(file)
102
216
  const narrowed = narrowWasmBoundary(input, wideExports)
103
217
  const merged = `${output}.narrowed`
104
218
  if (narrowed) {
@@ -110,7 +224,7 @@ async function runWasmOpt(
110
224
  join(dirname(binary), 'wasm-merge'),
111
225
  [
112
226
  ...features,
113
- ...names,
227
+ ...outputNames,
114
228
  guest,
115
229
  NARROW_MERGE_NAMES.guest,
116
230
  thunks,
@@ -124,133 +238,25 @@ async function runWasmOpt(
124
238
  rmSync(guest)
125
239
  rmSync(thunks)
126
240
  }
127
- const lowered = memory?.shared ? `${output}.lowered` : output
128
- const compiled = narrowed
129
- ? merged
130
- : memory?.shared && optimizeBeforeGuard
131
- ? checked
132
- : file
241
+ const compiled = narrowed ? merged : file
133
242
  const skip = skipPasses.flatMap((pass) => ['--skip-pass', pass])
134
243
  try {
135
- if (!memory?.shared || optimizeBeforeGuard)
136
- await runBinaryen(
137
- binary,
138
- [
139
- ...LOWERING,
140
- ...(optimizeSize ? ['-Oz'] : relink ? RELINK_PASSES : []),
141
- ...outputNames,
142
- ...skip,
143
- ...features,
144
- compiled,
145
- '-o',
146
- lowered,
147
- ],
148
- { env: environment, maxBuffer: Infinity },
149
- )
150
- if (memory?.shared) {
151
- const guarded = `${output}.table.wasm`
152
- const sdkGuarded = `${output}.sdk-table.wasm`
153
- const sdkSlots = `${output}.sdk-table-slots`
154
- const tableTool = join(dirname(binary), 'wasm-table-guard')
155
- try {
156
- const { stdout } = await runBinaryen(
157
- tableTool,
158
- [
159
- optimizeBeforeGuard ? lowered : compiled,
160
- guarded,
161
- names.length ? '1' : '0',
162
- ...(optimizeBeforeGuard ? [] : ['lower']),
163
- ],
164
- { env: environment },
165
- )
166
- const metadata: unknown = JSON.parse(stdout)
167
- if (
168
- typeof metadata !== 'object' ||
169
- metadata === null ||
170
- !('tableMinimum' in metadata) ||
171
- typeof metadata.tableMinimum !== 'number' ||
172
- !Number.isSafeInteger(metadata.tableMinimum) ||
173
- metadata.tableMinimum < 0
174
- )
175
- throw new Error('native table tool returned an invalid table minimum')
176
- if (sdkTableEnd !== undefined) {
177
- // sdk functions can move to lazy parts without changing their app slot addresses.
178
- writeFileSync(
179
- sdkSlots,
180
- Array.from({ length: sdkTableEnd - 1 }, (_, index) => index + 1).join('\n'),
181
- )
182
- await runBinaryen(
183
- tableTool,
184
- [guarded, sdkGuarded, names.length ? '1' : '0', '0', sdkSlots, 'adapt'],
185
- { env: environment },
186
- )
187
- renameSync(sdkGuarded, guarded)
188
- }
189
- if (optimize)
190
- await runBinaryen(
191
- binary,
192
- [
193
- ...features,
194
- // an adapted call tests its target's type with a typed function reference.
195
- ...(sdkTableEnd === undefined ? [] : ['--enable-gc']),
196
- ...(optimizeSize ? ['-Oz'] : RELINK_PASSES),
197
- ...outputNames,
198
- ...skip,
199
- guarded,
200
- '-o',
201
- output,
202
- ],
203
- { env: environment },
204
- )
205
- const sectionName = new TextEncoder().encode(NATIVE_IMAGE_SECTION)
206
- const layout = new TextEncoder().encode(
207
- JSON.stringify({
208
- initialPages: memory.initialPages,
209
- tableMinimum: metadata.tableMinimum,
210
- }),
211
- )
212
- const encode = (value: number) => {
213
- const bytes: number[] = []
214
- do {
215
- const byte = value & 127
216
- value >>>= 7
217
- bytes.push(byte | (value ? 128 : 0))
218
- } while (value)
219
- return bytes
220
- }
221
- const body = new Uint8Array([
222
- ...encode(sectionName.length),
223
- ...sectionName,
224
- ...layout,
225
- ])
226
- const image = optimize ? output : guarded
227
- const bytes = readFileSync(image)
228
- // a thread table that grows after its instance exists costs a second copy.
229
- writeFileSync(
230
- image,
231
- Buffer.concat([
232
- sdkTableEnd === undefined
233
- ? withWasmTableMinimum(
234
- bytes,
235
- metadata.tableMinimum + NATIVE_TABLE_RESERVE_SLOTS,
236
- )
237
- : bytes,
238
- Buffer.from([0, ...encode(body.length), ...body]),
239
- ]),
240
- )
241
- if (!optimize) renameSync(guarded, output)
242
- } finally {
243
- rmSync(guarded, { force: true })
244
- rmSync(sdkGuarded, { force: true })
245
- rmSync(sdkSlots, { force: true })
246
- }
247
- }
244
+ await runBinaryen(
245
+ binary,
246
+ [
247
+ ...LOWERING,
248
+ ...(optimizeSize ? ['-Oz'] : relink ? RELINK_PASSES : []),
249
+ ...outputNames,
250
+ ...skip,
251
+ ...features,
252
+ compiled,
253
+ '-o',
254
+ output,
255
+ ],
256
+ { env: environment, maxBuffer: Infinity },
257
+ )
248
258
  } finally {
249
259
  if (narrowed) rmSync(merged, { force: true })
250
- if (memory?.shared) {
251
- rmSync(checked, { force: true })
252
- rmSync(lowered, { force: true })
253
- }
254
260
  }
255
261
  }
256
262