peachsim 0.1.2596 → 0.1.2598

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 +969 -423
  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-WGTQK4JN.mjs → acorn-KBQ33SLR.mjs} +3 -3
  27. package/dist-lib/esm-chunks/{chunk-ZVBBDMPW.mjs → chunk-37W5LRA3.mjs} +1 -1
  28. package/dist-lib/esm-chunks/{chunk-DY6PUVXF.mjs → chunk-54WIQP6N.mjs} +1 -1
  29. package/dist-lib/esm-chunks/{chunk-WPX7HLFR.mjs → chunk-B2DVYADK.mjs} +7 -6
  30. package/dist-lib/esm-chunks/{chunk-SPKD6UYK.mjs → chunk-C4NFHNW5.mjs} +3 -3
  31. package/dist-lib/esm-chunks/{chunk-BNXCKF2B.mjs → chunk-CQHPA2VS.mjs} +1 -1
  32. package/dist-lib/esm-chunks/{chunk-C3C5OJG4.mjs → chunk-FKSCP3EF.mjs} +5 -5
  33. package/dist-lib/esm-chunks/{chunk-4UUWP6NP.mjs → chunk-I7POLNK4.mjs} +1 -1
  34. package/dist-lib/esm-chunks/{chunk-SYCOZ2CT.mjs → chunk-IB7EAJUO.mjs} +4 -4
  35. package/dist-lib/esm-chunks/{chunk-4MMPS5YL.mjs → chunk-J2DSD5ZC.mjs} +1 -1
  36. package/dist-lib/esm-chunks/{chunk-HZPXGVY4.mjs → chunk-J5SU2CGP.mjs} +1 -1
  37. package/dist-lib/esm-chunks/{chunk-3C2YVQZH.mjs → chunk-J6DI5ND2.mjs} +1 -1
  38. package/dist-lib/esm-chunks/{chunk-UCIOSXXU.mjs → chunk-NTVANYOL.mjs} +963 -490
  39. package/dist-lib/esm-chunks/{chunk-FDMS7FWE.mjs → chunk-OQYMCUNM.mjs} +2 -2
  40. package/dist-lib/esm-chunks/{chunk-LZF4BVEY.mjs → chunk-PZDQUPFX.mjs} +1 -1
  41. package/dist-lib/esm-chunks/{chunk-BAEOHCN5.mjs → chunk-QPRSUFLC.mjs} +1 -1
  42. package/dist-lib/esm-chunks/{chunk-TNZG7SFG.mjs → chunk-SYTFW2W6.mjs} +1 -1
  43. package/dist-lib/esm-chunks/{chunk-ISZE675P.mjs → chunk-SYWNAT3Z.mjs} +1 -1
  44. package/dist-lib/esm-chunks/{chunk-TILPABC4.mjs → chunk-T2I3EXKH.mjs} +1 -1
  45. package/dist-lib/esm-chunks/{chunk-5NZPSLS4.mjs → chunk-U3X55VHR.mjs} +1 -1
  46. package/dist-lib/esm-chunks/{chunk-4PMXT2UO.mjs → chunk-UCLJ5PDG.mjs} +1 -1
  47. package/dist-lib/esm-chunks/{chunk-QEGUUPRI.mjs → chunk-UKE7QBBD.mjs} +1 -1
  48. package/dist-lib/esm-chunks/{chunk-IGMABYTC.mjs → chunk-Z7BYGNNX.mjs} +1 -1
  49. package/dist-lib/esm-chunks/{metro-layout-parser-QNM3HYV7.mjs → metro-layout-parser-SOAXKW5M.mjs} +3 -3
  50. package/dist-lib/esm-chunks/{swift-package-prebuilt-BXXQB7Q2.mjs → swift-package-prebuilt-KK3VRVOF.mjs} +10 -10
  51. package/dist-lib/esm-chunks/{swift-sdk-app-module-FKL3X2ZC.mjs → swift-sdk-app-module-TEOC7LDK.mjs} +126 -56
  52. package/dist-lib/esm-chunks/{swift-sdk-image-CCVWDFHB.mjs → swift-sdk-image-ZBA4EYBW.mjs} +14 -14
  53. package/dist-lib/esm-chunks/{swift-sdk-image-producer-OTMZVBDN.mjs → swift-sdk-image-producer-3VLSKMBD.mjs} +4 -4
  54. package/dist-lib/expo-swift-bundle.cjs +968 -422
  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 +969 -423
  59. package/dist-lib/host/fetch-proxy-handler.cjs +36 -6
  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 +34 -4
  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 +970 -424
  93. package/dist-lib/swift-compile.cjs +966 -420
  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/dist-types/packages/peach-engine/src/render-worker/compositor-protocol.d.ts +1 -0
  126. package/dist-types/packages/peach-engine/src/render-worker/native-hot-ops.d.ts +1 -0
  127. package/dist-types/packages/peach-engine/src/render-worker/nativeAnimMirrorContract.d.ts +0 -2
  128. package/package.json +1 -1
  129. package/src/swift-compile.ts +421 -175
  130. package/src/swift-guest-compile.ts +16 -0
  131. package/src/swift-guest-image-ledger.ts +65 -14
  132. package/src/swift-guest-link.ts +21 -0
  133. package/src/swift-guest-manifest.ts +27 -5
  134. package/src/swift-guest-source-inputs.ts +119 -14
  135. package/src/swift-sdk-app-module.ts +128 -55
  136. package/src/swift-sdk-image-wasm.ts +76 -39
  137. package/src/swift-sdk-image.ts +2 -1
  138. package/src/swift-wasm-prune.ts +282 -222
  139. package/src/wasm-optimize.ts +1 -1
  140. package/src/write-build-file.ts +9 -8
@@ -175,6 +175,17 @@ export function swiftAppModuleInputs(
175
175
  return inputs
176
176
  }
177
177
 
178
+ function llvmQuoted(value: string): string {
179
+ const bytes = Buffer.from(value, 'utf8')
180
+ let out = '"'
181
+ for (let i = 0; i < bytes.length; i++) {
182
+ const b = bytes[i]!
183
+ if (b >= 0x20 && b <= 0x7e && b !== 0x22 && b !== 0x5c) out += String.fromCharCode(b)
184
+ else out += `\\${b.toString(16).toUpperCase().padStart(2, '0')}`
185
+ }
186
+ return out + '"'
187
+ }
188
+
178
189
  // a weak definition with the sdk's own signature for each sdk function the app calls but does
179
190
  // not define, forwarding to its import: references bind as they would to the sdk's definition
180
191
  // in a whole-program link, so a caller declaring another signature gets wasm-ld's mismatch stub
@@ -187,71 +198,112 @@ async function compileSdkForwarders(
187
198
  output: string,
188
199
  project: SwiftProjectUser | undefined,
189
200
  readInput: (file: string) => Uint8Array,
201
+ precomputedSymbols?: {
202
+ readonly referenced: ReadonlySet<string>
203
+ readonly defined: ReadonlySet<string>
204
+ },
190
205
  ) {
191
206
  const { signatures, tls } = sdk.sdkInterface
192
- const directories = inputs
193
- .filter((input) => input.startsWith('-L'))
194
- .map((input) => input.slice(2))
195
- const referenced = new Set<string>()
196
- const defined = new Set<string>()
197
- const scan = (bytes: Uint8Array) => {
198
- const symbols = guestWasmSymbols(bytes)
199
- for (const name of symbols.undefined) referenced.add(name)
200
- for (const name of symbols.defined) defined.add(name)
201
- }
202
- for (const input of inputs) {
203
- const file = input.startsWith('-l')
204
- ? directories
205
- .map((directory) => path.join(directory, `lib${input.slice(2)}.a`))
206
- .find((candidate) => existsSync(candidate))
207
- : input.startsWith('-')
208
- ? undefined
209
- : input
210
- if (!file) continue
211
- const bytes = readInput(file)
212
- if (file.endsWith('.a')) for (const member of readArMembers(bytes)) scan(member.bytes)
213
- else scan(bytes)
214
- }
215
- const c: Record<string, string> = { i: 'int', j: 'long long', f: 'float', d: 'double' }
216
- const type = (kinds: string) =>
217
- [...kinds].map((kind) => {
218
- const name = c[kind]
207
+ let referenced: ReadonlySet<string>
208
+ let defined: ReadonlySet<string>
209
+ if (precomputedSymbols) {
210
+ referenced = precomputedSymbols.referenced
211
+ defined = precomputedSymbols.defined
212
+ } else {
213
+ const directories = inputs
214
+ .filter((input) => input.startsWith('-L'))
215
+ .map((input) => input.slice(2))
216
+ const refSet = new Set<string>()
217
+ const defSet = new Set<string>()
218
+ const scan = (bytes: Uint8Array) => {
219
+ const symbols = guestWasmSymbols(bytes)
220
+ for (const name of symbols.undefined) refSet.add(name)
221
+ for (const name of symbols.defined) defSet.add(name)
222
+ }
223
+ for (const input of inputs) {
224
+ const file = input.startsWith('-l')
225
+ ? directories
226
+ .map((directory) => path.join(directory, `lib${input.slice(2)}.a`))
227
+ .find((candidate) => existsSync(candidate))
228
+ : input.startsWith('-')
229
+ ? undefined
230
+ : input
231
+ if (!file) continue
232
+ const bytes = readInput(file)
233
+ if (file.endsWith('.a'))
234
+ for (const member of readArMembers(bytes)) scan(member.bytes)
235
+ else scan(bytes)
236
+ }
237
+ referenced = refSet
238
+ defined = defSet
239
+ }
240
+ const irType: Record<string, string> = {
241
+ i: 'i32',
242
+ j: 'i64',
243
+ f: 'float',
244
+ d: 'double',
245
+ }
246
+ const type = (kinds: string) => {
247
+ const out: string[] = new Array(kinds.length)
248
+ for (let i = 0; i < kinds.length; i++) {
249
+ const kind = kinds[i]!
250
+ const name = irType[kind]
219
251
  if (!name) throw new Error(`sdk signature kind ${kind}`)
220
- return name
221
- })
222
- const literal = (name: string) => JSON.stringify(name)
223
- const lines: string[] = []
224
- if (signatures.__objc_send_load_messages === ':')
252
+ out[i] = name
253
+ }
254
+ return out
255
+ }
256
+ const lines: string[] = [`target triple = "${SWIFT_WASM_TRIPLE}"`]
257
+ const attrs: string[] = []
258
+ const addImportAttr = (name: string) => {
259
+ const id = attrs.length
260
+ attrs.push(
261
+ `attributes #${id} = { "wasm-import-module"="env" "wasm-import-name"=${llvmQuoted(name)} }`,
262
+ )
263
+ return id
264
+ }
265
+ if (signatures.__objc_send_load_messages === ':') {
225
266
  // wasm-ld orders equal-priority constructors by input order. this object links last.
267
+ const attr = addImportAttr('__objc_send_load_messages')
226
268
  lines.push(
227
- '__attribute__((import_module("env"), import_name("__objc_send_load_messages"))) void sdk_send_load_messages(void);',
228
- '__attribute__((constructor)) static void send_app_load_messages(void) { sdk_send_load_messages(); }',
269
+ '@llvm.global_ctors = appending global [1 x { i32, ptr, ptr }] [{ i32, ptr, ptr } { i32 65535, ptr @send_app_load_messages, ptr null }]',
270
+ `declare void @sdk_send_load_messages() #${attr}`,
271
+ 'define internal void @send_app_load_messages() {',
272
+ ' tail call void @sdk_send_load_messages()',
273
+ ' ret void',
274
+ '}',
229
275
  )
276
+ }
230
277
  const forwarders = new Set<string>()
231
278
  let index = 0
232
279
  for (const name of referenced) {
233
280
  const signature = signatures[name]
234
281
  if (signature === undefined || defined.has(name)) continue
235
- const [params = '', results = ''] = signature.split(':')
282
+ const colon = signature.indexOf(':')
283
+ const params = colon < 0 ? signature : signature.slice(0, colon)
284
+ const results = colon < 0 ? '' : signature.slice(colon + 1)
236
285
  if (results.length > 1) throw new Error(`sdk function ${name} returns several values`)
237
- const result = results ? type(results)[0] : 'void'
238
- const declared =
239
- type(params)
240
- .map((kind, at) => `${kind} a${at}`)
241
- .join(', ') || 'void'
242
- const passed = type(params)
243
- .map((_, at) => `a${at}`)
244
- .join(', ')
286
+ const result = results ? type(results)[0]! : 'void'
287
+ const paramTypes = type(params)
288
+ const declParams = paramTypes.join(', ')
289
+ const defParams = paramTypes.map((kind, at) => `${kind} %a${at}`).join(', ')
290
+ const callArgs = paramTypes.map((kind, at) => `${kind} %a${at}`).join(', ')
291
+ const attr = addImportAttr(name)
292
+ const quoted = llvmQuoted(name)
245
293
  lines.push(
246
- `__attribute__((import_module("env"), import_name(${literal(name)}))) ${result} i${index}(${declared});`,
247
- `__attribute__((weak)) ${result} f${index}(${declared}) __asm__(${literal(name)});`,
248
- `${result} f${index}(${declared}) { ${result === 'void' ? '' : 'return '}i${index}(${passed}); }`,
294
+ `declare ${result} @i${index}(${declParams}) #${attr}`,
295
+ `define weak hidden ${result} @${quoted}(${defParams}) {`,
296
+ result === 'void'
297
+ ? ` tail call void @i${index}(${callArgs})\n ret void`
298
+ : ` %r = tail call ${result} @i${index}(${callArgs})\n ret ${result} %r`,
299
+ '}',
249
300
  )
250
301
  forwarders.add(name)
251
302
  index++
252
303
  }
253
304
  const errno = tls.errno
254
305
  let declaredErrno = false
306
+ const usedHelpers: string[] = []
255
307
  for (const name of referenced) {
256
308
  const offset = tls[name]
257
309
  if (offset === undefined || defined.has(name)) continue
@@ -259,29 +311,45 @@ async function compileSdkForwarders(
259
311
  throw new Error('the sdk image has no errno to find its thread block by')
260
312
  if (!declaredErrno) {
261
313
  declaredErrno = true
314
+ const attr = addImportAttr('__errno_location')
262
315
  lines.push(
263
- `__attribute__((import_module("env"), import_name("__errno_location"))) char *__peach_errno(void);`,
316
+ `declare ptr @__peach_errno() #${attr}`,
317
+ 'declare ptr @llvm.wasm.tls.base()',
264
318
  )
265
319
  }
320
+ const helper = `@${llvmQuoted(SDK_TLS_HELPER + name)}`
321
+ const delta = `@${llvmQuoted(SDK_TLS_DELTA + name)}`
322
+ usedHelpers.push(`ptr ${helper}`)
266
323
  // the sdk's thread block starts errno's offset below errno
267
324
  lines.push(
268
- `__attribute__((weak)) _Thread_local char t${index} __asm__(${literal(name)});`,
269
- `__attribute__((used)) long long h${index}(void) __asm__(${literal(SDK_TLS_HELPER + name)});`,
270
- `extern char d${index} __asm__(${literal(SDK_TLS_DELTA + name)});`,
271
- `long long h${index}(void) { return (long long)__peach_errno() + (long long)&d${index} - (long long)__builtin_wasm_tls_base(); }`,
325
+ `@${llvmQuoted(name)} = weak hidden thread_local global i8 0, align 1`,
326
+ `${delta} = external global i8, align 1`,
327
+ `define hidden i64 ${helper}() {`,
328
+ ' %e = tail call ptr @__peach_errno()',
329
+ ' %ei = ptrtoint ptr %e to i64',
330
+ ` %di = ptrtoint ptr ${delta} to i64`,
331
+ ' %s = add i64 %ei, %di',
332
+ ' %b = tail call ptr @llvm.wasm.tls.base()',
333
+ ' %bi = ptrtoint ptr %b to i64',
334
+ ' %r = sub i64 %s, %bi',
335
+ ' ret i64 %r',
336
+ '}',
272
337
  )
273
338
  index++
274
339
  }
275
- const source = `${output.slice(0, -2)}.c`
276
- writeFileSync(source, lines.join('\n') + '\n')
340
+ if (usedHelpers.length > 0) {
341
+ lines.push(
342
+ `@llvm.used = appending global [${usedHelpers.length} x ptr] [${usedHelpers.join(', ')}], section "llvm.metadata"`,
343
+ )
344
+ }
345
+ const source = `${output.slice(0, -2)}.ll`
346
+ writeFileSync(source, [...lines, ...attrs].join('\n') + '\n')
277
347
  await run(
278
348
  path.join(toolchainBin, 'clang'),
279
349
  [
280
350
  `--target=${SWIFT_WASM_TRIPLE}`,
281
- '-O2',
282
351
  '-matomics',
283
352
  '-mbulk-memory',
284
- '-ffreestanding',
285
353
  '-c',
286
354
  source,
287
355
  '-o',
@@ -305,6 +373,10 @@ export async function linkSwiftAppModule(options: {
305
373
  loweringCache?: { readonly file: string; readonly compilerDigest: string }
306
374
  project?: SwiftProjectUser
307
375
  readInput?: (file: string) => Uint8Array
376
+ precomputedSymbols?: {
377
+ readonly referenced: ReadonlySet<string>
378
+ readonly defined: ReadonlySet<string>
379
+ }
308
380
  }) {
309
381
  const { objects, toolchainBin, sdk, work } = options
310
382
  const forwarders = path.join(work, 'sdk-forwarders.o')
@@ -315,6 +387,7 @@ export async function linkSwiftAppModule(options: {
315
387
  forwarders,
316
388
  options.project,
317
389
  options.readInput ?? readFileSync,
390
+ options.precomputedSymbols,
318
391
  )
319
392
  const lazy = options.lazyFunctions ?? []
320
393
  const lazyExports = path.join(work, 'lazy-exports.rsp')
@@ -628,6 +628,12 @@ const MEMORY_RELOCATIONS = new Set([3, 4, 5, 7, 11, 14, 15, 16, 17, 21, 22, 23,
628
628
  export const ADDEND_RELOCATIONS = new Set([
629
629
  3, 4, 5, 8, 9, 11, 14, 15, 16, 17, 21, 22, 23, 25, 27,
630
630
  ])
631
+ let FUNCTION_RELOC_MASK = 0
632
+ for (const type of FUNCTION_RELOCATIONS) FUNCTION_RELOC_MASK |= 1 << type
633
+ let MEMORY_RELOC_MASK = 0
634
+ for (const type of MEMORY_RELOCATIONS) MEMORY_RELOC_MASK |= 1 << type
635
+ let ADDEND_RELOC_MASK = 0
636
+ for (const type of ADDEND_RELOCATIONS) ADDEND_RELOC_MASK |= 1 << type
631
637
 
632
638
  /**
633
639
  * dependencies from a link emitted with --emit-relocs, including function pointers that can
@@ -640,13 +646,11 @@ export function relocationEdges(
640
646
  const sections = wasmSections(bytes)
641
647
  const linking = sections.find((s) => s.name === 'linking')
642
648
  if (!linking) throw new Error('relocation link has no linking section')
643
- const symbols: {
644
- kind: number
645
- flags: number
646
- segment: number
647
- offset: number
648
- index: number
649
- }[] = []
649
+ let symKind = new Uint8Array(0)
650
+ let symFlags = new Uint32Array(0)
651
+ let symSegment = new Int32Array(0)
652
+ let symOffset = new Int32Array(0)
653
+ let symIndex = new Int32Array(0)
650
654
  const segmentNames: string[] = []
651
655
  {
652
656
  const r = wasmReader(bytes, linking.start)
@@ -654,27 +658,31 @@ export function relocationEdges(
654
658
  while (r.at < linking.end) {
655
659
  const type = r.byte()
656
660
  const end = r.u() + r.at
657
- if (type === 8)
658
- for (let n = r.u(); n > 0; n--) {
661
+ if (type === 8) {
662
+ const count = r.u()
663
+ symKind = new Uint8Array(count)
664
+ symFlags = new Uint32Array(count)
665
+ symSegment = new Int32Array(count).fill(-1)
666
+ symOffset = new Int32Array(count)
667
+ symIndex = new Int32Array(count).fill(-1)
668
+ for (let i = 0; i < count; i++) {
659
669
  const kind = r.byte()
660
670
  const flags = r.u()
661
- let segment = -1
662
- let offset = 0
663
- let index = -1
671
+ symKind[i] = kind
672
+ symFlags[i] = flags
664
673
  if (kind === 0 || kind === 2 || kind === 4 || kind === 5) {
665
- index = r.u()
666
- if (!(flags & 0x10) || flags & 0x40) r.name()
674
+ symIndex[i] = r.u()
675
+ if (!(flags & 0x10) || flags & 0x40) r.skip(r.u())
667
676
  } else if (kind === 1) {
668
- r.name()
677
+ r.skip(r.u())
669
678
  if (!(flags & 0x10)) {
670
- segment = r.u()
671
- offset = r.u()
679
+ symSegment[i] = r.u()
680
+ symOffset[i] = r.u()
672
681
  r.u()
673
682
  }
674
683
  } else if (kind === 3) r.u()
675
- symbols.push({ kind, flags, segment, offset, index })
676
684
  }
677
- else if (type === 5)
685
+ } else if (type === 5)
678
686
  for (let n = r.u(); n > 0; n--) {
679
687
  segmentNames.push(r.name())
680
688
  r.u()
@@ -713,42 +721,71 @@ export function relocationEdges(
713
721
  const importedFunctions = readModule(bytes).importedFuncs
714
722
  const data: number[] = []
715
723
  const dataFunctions: number[] = []
716
- const code: number[][] = bodyStarts.map(() => [])
717
- const calls: number[][] = bodyStarts.map(() => [])
724
+ const bodyCount = bodyStarts.length
725
+ const code: number[][] = Array.from({ length: bodyCount }, () => [])
726
+ const calls: number[][] = Array.from({ length: bodyCount }, () => [])
718
727
  for (const s of sections) {
719
728
  if (!s.name?.startsWith('reloc.')) continue
720
- const r = wasmReader(bytes, s.start)
721
- const target = r.u()
729
+ let cur = s.start
730
+ const readU = (): number => {
731
+ const b0 = bytes[cur++]!
732
+ if (b0 < 128) return b0
733
+ let value = b0 & 127
734
+ let shift = 7
735
+ for (;;) {
736
+ const byte = bytes[cur++]!
737
+ value += (byte & 127) * 2 ** shift
738
+ if (byte < 128) return value
739
+ shift += 7
740
+ }
741
+ }
742
+ const readS = (): number => {
743
+ const b0 = bytes[cur++]!
744
+ if (b0 < 128) return b0 & 64 ? b0 - 128 : b0
745
+ let value = b0 & 127
746
+ let shift = 7
747
+ let byte = b0
748
+ while (byte >= 128) {
749
+ byte = bytes[cur++]!
750
+ value += (byte & 127) * 2 ** shift
751
+ shift += 7
752
+ }
753
+ if (byte & 64) value -= 2 ** shift
754
+ return value
755
+ }
756
+ const target = readU()
722
757
  if (target !== dataIndex && target !== codeIndex) continue
723
758
  let segment = 0
724
759
  let body = 0
725
- for (let n = r.u(); n > 0; n--) {
726
- const type = r.byte()
727
- const offset = r.u()
728
- const index = r.u()
729
- const addend = ADDEND_RELOCATIONS.has(type) ? Number(r.s()) : 0
730
- const functionRelocation = FUNCTION_RELOCATIONS.has(type)
731
- const memoryRelocation = MEMORY_RELOCATIONS.has(type)
760
+ const isData = target === dataIndex
761
+ for (let n = readU(); n > 0; n--) {
762
+ const type = bytes[cur++]!
763
+ const offset = readU()
764
+ const index = readU()
765
+ const bit = type < 32 ? 1 << type : 0
766
+ const addend = (ADDEND_RELOC_MASK & bit) !== 0 ? readS() : 0
767
+ const functionRelocation = (FUNCTION_RELOC_MASK & bit) !== 0
768
+ const memoryRelocation = (MEMORY_RELOC_MASK & bit) !== 0
732
769
  if (!functionRelocation && !memoryRelocation) continue
733
- const symbol = symbols[index]!
734
- const functionReference = functionRelocation && symbol.kind === 0
735
- if (symbol.flags & 0x10) continue
770
+ if (symFlags[index]! & 0x10) continue
771
+ const kind = symKind[index]!
772
+ const functionReference = functionRelocation && kind === 0
736
773
  if (
737
774
  !functionReference &&
738
- (!memoryRelocation || symbol.kind !== 1 || symbol.segment < 0)
775
+ (!memoryRelocation || kind !== 1 || symSegment[index]! < 0)
739
776
  )
740
777
  continue
741
778
  const to = functionReference
742
- ? symbol.index - importedFunctions
743
- : addresses[symbol.segment]! + symbol.offset + addend
744
- if (functionReference && (to < 0 || to >= bodyStarts.length)) continue
745
- if (target === dataIndex) {
779
+ ? symIndex[index]! - importedFunctions
780
+ : addresses[symSegment[index]!]! + symOffset[index]! + addend
781
+ if (functionReference && (to < 0 || to >= bodyCount)) continue
782
+ if (isData) {
746
783
  while (payloads[segment]!.end <= offset) segment++
747
784
  const payload = payloads[segment]!
748
785
  const edges = functionReference ? dataFunctions : data
749
786
  edges.push(payload.address + offset - payload.start, to)
750
787
  } else {
751
- while (body + 1 < bodyStarts.length && bodyStarts[body + 1]! <= offset) body++
788
+ while (body + 1 < bodyCount && bodyStarts[body + 1]! <= offset) body++
752
789
  const edges = functionReference ? calls : code
753
790
  edges[body]!.push(to)
754
791
  }
@@ -756,9 +756,10 @@ function sdkHostExports(appRoot: string): string[] {
756
756
  if (!(byte & 128)) return value
757
757
  }
758
758
  }
759
+ const raw = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)
759
760
  for (let n = u(); n > 0; n--) {
760
761
  const length = u()
761
- names.push(Buffer.from(bytes.subarray(at, at + length)).toString())
762
+ names.push(raw.toString('utf8', at, at + length))
762
763
  at += length + 1
763
764
  u()
764
765
  }