peachsim 0.1.2778 → 0.1.2780

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. package/dist-lib/agent-daemon-client.cjs +1 -1
  2. package/dist-lib/agent-events.cjs +1 -1
  3. package/dist-lib/attached-projects.cjs +1 -1
  4. package/dist-lib/auth/shared-session.cjs +1 -1
  5. package/dist-lib/backend-origin.cjs +1 -1
  6. package/dist-lib/beta.cjs +1 -1
  7. package/dist-lib/beta.mjs +2 -2
  8. package/dist-lib/bridge-constants.cjs +1 -1
  9. package/dist-lib/bridge-contract-input.cjs +1 -1
  10. package/dist-lib/bridge-contract-input.mjs +5 -5
  11. package/dist-lib/bridge-contract.cjs +1 -1
  12. package/dist-lib/bridge-contract.mjs +3 -3
  13. package/dist-lib/bundle-channel.cjs +295 -480
  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-RWBIRNWO.mjs → acorn-SPK2673W.mjs} +3 -3
  27. package/dist-lib/esm-chunks/{chunk-ZWOS2EDN.mjs → chunk-3BAOVPYB.mjs} +118 -361
  28. package/dist-lib/esm-chunks/{chunk-MRIDS3GE.mjs → chunk-664T7XWT.mjs} +5 -5
  29. package/dist-lib/esm-chunks/{chunk-QKTKE4OX.mjs → chunk-7B4ZRXZW.mjs} +1 -1
  30. package/dist-lib/esm-chunks/{chunk-G7UJ4BAC.mjs → chunk-7UJZBQ6G.mjs} +1 -1
  31. package/dist-lib/esm-chunks/{chunk-EXU474NE.mjs → chunk-B4DO5WJD.mjs} +1 -1
  32. package/dist-lib/esm-chunks/{chunk-IQSFMB4R.mjs → chunk-DO3C6MOG.mjs} +4 -4
  33. package/dist-lib/esm-chunks/{chunk-K3ULPN2R.mjs → chunk-DZZZCBJY.mjs} +1 -1
  34. package/dist-lib/esm-chunks/{chunk-5TIUTQ55.mjs → chunk-EEN3DGCH.mjs} +1 -1
  35. package/dist-lib/esm-chunks/{chunk-ZDWMPXYI.mjs → chunk-JIGACUAS.mjs} +1 -1
  36. package/dist-lib/esm-chunks/{chunk-X25PQNNH.mjs → chunk-JIKQAODQ.mjs} +1 -1
  37. package/dist-lib/esm-chunks/{chunk-6YHF4VYF.mjs → chunk-MIESONPY.mjs} +1 -1
  38. package/dist-lib/esm-chunks/{chunk-3RPELUA2.mjs → chunk-NDMVV5PE.mjs} +6 -6
  39. package/dist-lib/esm-chunks/{chunk-KX7LV722.mjs → chunk-NLIWJ2AO.mjs} +1 -1
  40. package/dist-lib/esm-chunks/{chunk-5WASPTSS.mjs → chunk-P7DZLDBL.mjs} +1 -1
  41. package/dist-lib/esm-chunks/{chunk-F3OPIKJC.mjs → chunk-RDFBVP4E.mjs} +2 -2
  42. package/dist-lib/esm-chunks/{chunk-DLBUPWTI.mjs → chunk-SWCGFJ5A.mjs} +1 -1
  43. package/dist-lib/esm-chunks/{chunk-KLA74B7J.mjs → chunk-T6THDRRU.mjs} +1 -1
  44. package/dist-lib/esm-chunks/{chunk-3WQANOYY.mjs → chunk-TBKPPEGS.mjs} +1 -1
  45. package/dist-lib/esm-chunks/{chunk-QUVLFF6I.mjs → chunk-UHTFNN5A.mjs} +1 -1
  46. package/dist-lib/esm-chunks/{chunk-GSTHJQMW.mjs → chunk-VV2TNQK4.mjs} +115 -18
  47. package/dist-lib/esm-chunks/{chunk-VZ3NXXUC.mjs → chunk-WKE7Z3LU.mjs} +1 -1
  48. package/dist-lib/esm-chunks/{chunk-LQVLXDRN.mjs → chunk-WXCV7NSJ.mjs} +1 -1
  49. package/dist-lib/esm-chunks/{chunk-KBYVKTVK.mjs → chunk-X2OBXRGN.mjs} +3 -3
  50. package/dist-lib/esm-chunks/{chunk-N2PEXZGL.mjs → chunk-ZDW2NOAO.mjs} +1 -1
  51. package/dist-lib/esm-chunks/{metro-layout-parser-RRIZOLBE.mjs → metro-layout-parser-6N4ZZDLM.mjs} +3 -3
  52. package/dist-lib/esm-chunks/{swift-package-prebuilt-EIZ7MCKR.mjs → swift-package-prebuilt-GF27A4NU.mjs} +12 -12
  53. package/dist-lib/esm-chunks/{swift-sdk-app-module-S2LQ4OXD.mjs → swift-sdk-app-module-WWTSGAS7.mjs} +47 -69
  54. package/dist-lib/esm-chunks/{swift-sdk-image-77FFUGXI.mjs → swift-sdk-image-OQMMOQLZ.mjs} +16 -16
  55. package/dist-lib/esm-chunks/{swift-sdk-image-producer-XFSFUXQJ.mjs → swift-sdk-image-producer-NOH5SG6O.mjs} +5 -5
  56. package/dist-lib/expo-swift-bundle.cjs +295 -480
  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 +301 -486
  61. package/dist-lib/host/fetch-proxy-handler.cjs +22 -11
  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 -86
  87. package/dist-lib/public-origins-lib.json +70 -70
  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 +301 -486
  95. package/dist-lib/swift-compile.cjs +288 -465
  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/render-worker/compositor-protocol.d.ts +2 -0
  123. package/dist-types/packages/peach-engine/src/render-worker/worklet-runtime.d.ts +15 -2
  124. package/dist-types/packages/peach-engine/src/render-worker/worklet-wire.d.ts +10 -1
  125. package/package.json +1 -1
  126. package/src/swift-compiler-digest.ts +1 -0
  127. package/src/swift-guest-dyld-functions.ts +1 -11
  128. package/src/swift-guest-dyld-table.ts +2 -9
  129. package/src/swift-guest-image-ledger.ts +9 -35
  130. package/src/swift-guest-source-inputs.ts +5 -22
  131. package/src/swift-icu.ts +15 -28
  132. package/src/swift-link-roots.ts +4 -8
  133. package/src/swift-sdk-app-module.ts +5 -27
  134. package/src/swift-sdk-image-wasm.ts +12 -41
  135. package/src/swift-sdk-surface.ts +1 -0
  136. package/src/swift-shared-sdk.ts +1 -13
  137. package/src/swift-wasm-archive.ts +6 -11
  138. package/src/swift-wasm-prune.ts +22 -100
  139. package/src/wasm-binary-sections.ts +61 -7
@@ -1,5 +1,6 @@
1
1
  import fs from 'node:fs'
2
2
  import path from 'node:path'
3
+ import { readULEB } from './wasm-binary-sections'
3
4
 
4
5
  export const OBJC_RUNTIME_LOOKUP_SECTION = 'peach_objc_runtime_lookup'
5
6
 
@@ -11,14 +12,9 @@ export const OBJC_RUNTIME_LOOKUP_SECTION = 'peach_objc_runtime_lookup'
11
12
  // becomes live, alongside Swift metadata reachability.
12
13
 
13
14
  const leb = (bytes: Uint8Array, at: { offset: number }): number => {
14
- let value = 0
15
- let scale = 1
16
- for (;;) {
17
- const byte = bytes[at.offset++]!
18
- value += (byte & 0x7f) * scale
19
- if (!(byte & 0x80)) return value
20
- scale *= 128
21
- }
15
+ const [value, next] = readULEB(bytes, at.offset)
16
+ at.offset = next
17
+ return value
22
18
  }
23
19
 
24
20
  // category and alias definitions share their segments with weak null records.
@@ -64,6 +64,11 @@ import {
64
64
  SWIFT_SDK_FUNCTION_ADDRESS_PREFIX,
65
65
  } from './swiftSdkLinkContract'
66
66
  import { SWIFT_WASM_TRIPLE } from './swiftTargetContract'
67
+ import {
68
+ encodeWasmName as encodedName,
69
+ encodeWasmNumber as uleb,
70
+ encodeWasmSigned as sleb,
71
+ } from './wasm-binary-sections'
67
72
  import { writeWasmCustomSection } from './wasm-narrow-boundary'
68
73
  import { KEEP_WASM_NAMES, lowerWasm, type WasmOptimization } from './wasm-optimize'
69
74
  import type {
@@ -753,33 +758,6 @@ const SDK_TLS_DELTA = '__peach_sdk_tls_delta.'
753
758
  const OBJC_SECTION_BOUNDARY =
754
759
  /^__(?:start|stop)_(__objc_(?:selectors|classes|class_refs|cats|protocols|protocol_refs|class_aliases|constant_string))$/
755
760
 
756
- const uleb = (value: number) => {
757
- const out: number[] = []
758
- do {
759
- let byte = value % 128
760
- value = Math.floor(value / 128)
761
- if (value) byte |= 128
762
- out.push(byte)
763
- } while (value)
764
- return out
765
- }
766
- const sleb = (value: bigint) => {
767
- const out: number[] = []
768
- for (;;) {
769
- const byte = Number(value & 127n)
770
- value >>= 7n
771
- if ((value === 0n && !(byte & 64)) || (value === -1n && byte & 64)) {
772
- out.push(byte)
773
- return out
774
- }
775
- out.push(byte | 128)
776
- }
777
- }
778
- const encodedName = (name: string) => {
779
- const bytes = [...Buffer.from(name)]
780
- return [...uleb(bytes.length), ...bytes]
781
- }
782
-
783
761
  // imports and globals keep their indices when an sdk image moves its symbols.
784
762
  function resolveSdkReferences(
785
763
  source: Buffer,
@@ -1,5 +1,12 @@
1
1
  import { chunkKey, type WasmLinkMap } from './swift-sdk-image-map'
2
2
  import { OBJC_LOAD_FUNCTION } from './swiftSdkLinkContract'
3
+ import {
4
+ encodeWasmName as encodedName,
5
+ encodeWasmNumber as uleb,
6
+ encodeWasmSigned as sleb,
7
+ readSLEB,
8
+ readULEB,
9
+ } from './wasm-binary-sections'
3
10
 
4
11
  export interface WasmSection {
5
12
  id: number
@@ -17,23 +24,13 @@ export function wasmReader(bytes: Uint8Array, start: number) {
17
24
  : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)
18
25
  let at = start
19
26
  const u = () => {
20
- let value = 0
21
- for (let shift = 0; ; shift += 7) {
22
- const byte = bytes[at++]!
23
- value += (byte & 127) * 2 ** shift
24
- if (!(byte & 128)) return value
25
- }
27
+ const [value, next] = readULEB(bytes, at)
28
+ at = next
29
+ return value
26
30
  }
27
31
  const s = () => {
28
- let value = 0n
29
- let shift = 0n
30
- let byte = 0
31
- do {
32
- byte = bytes[at++]!
33
- value |= BigInt(byte & 127) << shift
34
- shift += 7n
35
- } while (byte & 128)
36
- if (byte & 64) value -= 1n << shift
32
+ const [value, next] = readSLEB(bytes, at)
33
+ at = next
37
34
  return value
38
35
  }
39
36
  return {
@@ -72,32 +69,6 @@ export function wasmSections(bytes: Uint8Array): WasmSection[] {
72
69
  return out
73
70
  }
74
71
 
75
- const uleb = (value: number) => {
76
- const out: number[] = []
77
- do {
78
- let byte = value % 128
79
- value = Math.floor(value / 128)
80
- if (value) byte |= 128
81
- out.push(byte)
82
- } while (value)
83
- return out
84
- }
85
- const sleb = (value: bigint) => {
86
- const out: number[] = []
87
- for (;;) {
88
- const byte = Number(value & 127n)
89
- value >>= 7n
90
- if ((value === 0n && !(byte & 64)) || (value === -1n && byte & 64)) {
91
- out.push(byte)
92
- return out
93
- }
94
- out.push(byte | 128)
95
- }
96
- }
97
- const encodedName = (name: string) => {
98
- const bytes = [...Buffer.from(name)]
99
- return [...uleb(bytes.length), ...bytes]
100
- }
101
72
  const section = (id: number, payload: number[]) =>
102
73
  Uint8Array.from([id, ...uleb(payload.length), ...payload])
103
74
 
@@ -438,6 +438,7 @@ export function swiftModuleInputFiles(root: string): string[] {
438
438
  ...swiftSharedSdkInputFiles(root),
439
439
  '../peachsim/src/admitted-mac-build.ts',
440
440
  '../peachsim/src/swift-shared-sdk.ts',
441
+ '../peachsim/src/wasm-binary-sections.ts',
441
442
  '../peachsim/src/swift-runtime-products.ts',
442
443
  '../peachsim/src/swiftRuntimeProductContract.ts',
443
444
  '../peachsim/src/swiftTargetContract.ts',
@@ -10,6 +10,7 @@ import {
10
10
  swiftSdkOptions,
11
11
  } from './swift-toolchain'
12
12
  import { SWIFT_WASM_TRIPLE, wasmThreadFlags } from './swiftTargetContract.ts'
13
+ import { readULEB } from './wasm-binary-sections'
13
14
 
14
15
  /** names a build root's distribution mirror (swift-distribution-mirror.ts). */
15
16
  export const SWIFT_DISTRIBUTION_MIRROR_RECORD = '.peach-mirror.json'
@@ -81,19 +82,6 @@ export function swiftSharedSdkFlags(moduleRoot: string, distribution: boolean):
81
82
  ]
82
83
  }
83
84
 
84
- function readULEB(b: Uint8Array, o: number): [number, number] {
85
- let res = 0
86
- let shift = 0
87
- let p = o
88
- while (true) {
89
- const byte = b[p++]!
90
- res |= (byte & 0x7f) << shift
91
- if ((byte & 0x80) === 0) break
92
- shift += 7
93
- }
94
- return [res, p]
95
- }
96
-
97
85
  // public runtimes pinned to a distribution before 7eb5f6473249e4be still
98
86
  // override preferredLocalizations in PeachFoundation-Localization.o.
99
87
  const WEAKEN_FOUNDATION_SYMS = new Set([
@@ -1,3 +1,5 @@
1
+ import { readULEB, skipLEB } from './wasm-binary-sections'
2
+
1
3
  export function readArMembers(
2
4
  buf: Uint8Array,
3
5
  includeBitcode = false,
@@ -80,19 +82,12 @@ export function localizeDefinedWasmSymbols(
80
82
  const output = new Uint8Array(bytes)
81
83
  let cur = 8
82
84
  const readU = (): number => {
83
- const b0 = bytes[cur++]!
84
- if (b0 < 128) return b0
85
- let res = b0 & 0x7f
86
- let shift = 7
87
- while (true) {
88
- const byte = bytes[cur++]!
89
- res |= (byte & 0x7f) << shift
90
- if ((byte & 0x80) === 0) return res
91
- shift += 7
92
- }
85
+ const [res, next] = readULEB(bytes, cur)
86
+ cur = next
87
+ return res
93
88
  }
94
89
  const skipU = (): void => {
95
- while ((bytes[cur++]! & 0x80) !== 0) {}
90
+ cur = skipLEB(bytes, cur)
96
91
  }
97
92
  const importFuncNames: string[] = []
98
93
  const importGlobalNames: string[] = []
@@ -37,47 +37,15 @@ import {
37
37
  SWIFT_FRAMEWORK_PRODUCT_FILE,
38
38
  swiftFrameworkProductSchema,
39
39
  } from './swiftRuntimeProductContract'
40
+ import {
41
+ encodeWasmNumber as encodeULEB,
42
+ readSLEB,
43
+ readULEB,
44
+ skipLEB,
45
+ } from './wasm-binary-sections'
40
46
 
41
47
  export { localizeDefinedWasmSymbols, readArMembers }
42
48
 
43
- function readULEB(b: Uint8Array, o: number): [number, number] {
44
- let res = 0
45
- let shift = 0
46
- let p = o
47
- while (true) {
48
- const byte = b[p++]!
49
- res |= (byte & 0x7f) << shift
50
- if ((byte & 0x80) === 0) break
51
- shift += 7
52
- }
53
- return [res, p]
54
- }
55
-
56
- function readSLEB(b: Uint8Array, o: number): [number, number] {
57
- let res = 0
58
- let shift = 0
59
- let p = o
60
- let byte = 0
61
- do {
62
- byte = b[p++]!
63
- res |= (byte & 0x7f) << shift
64
- shift += 7
65
- } while (byte & 0x80)
66
- if (shift < 32 && byte & 0x40) res |= ~0 << shift
67
- return [res, p]
68
- }
69
-
70
- function encodeULEB(val: number): number[] {
71
- const out: number[] = []
72
- do {
73
- let byte = val & 0x7f
74
- val >>>= 7
75
- if (val !== 0) byte |= 0x80
76
- out.push(byte)
77
- } while (val !== 0)
78
- return out
79
- }
80
-
81
49
  // the 64-bit address, table index and function offset relocations patch eight bytes.
82
50
  const WIDE_DATA_RELOCS = new Set([16, 19, 22, 27])
83
51
 
@@ -209,39 +177,14 @@ function decodeRelocations(
209
177
  for (let k = 0; k < sectionCount; k++, i++) {
210
178
  start[i] = length + cur - baseAt
211
179
  const rType = buf[cur++]!
212
- let offset = buf[cur++]!
213
- if (offset >= 128) {
214
- offset &= 0x7f
215
- let shift = 7
216
- while (true) {
217
- const byte = buf[cur++]!
218
- offset |= (byte & 0x7f) << shift
219
- if (byte < 128) break
220
- shift += 7
221
- }
222
- }
223
- let symbol = buf[cur++]!
224
- if (symbol >= 128) {
225
- symbol &= 0x7f
226
- let shift = 7
227
- while (true) {
228
- const byte = buf[cur++]!
229
- symbol |= (byte & 0x7f) << shift
230
- if (byte < 128) break
231
- shift += 7
232
- }
233
- }
180
+ let offset: number
181
+ ;[offset, cur] = readULEB(buf, cur)
182
+ let symbol: number
183
+ ;[symbol, cur] = readULEB(buf, cur)
234
184
  if (rType < 32 && (ADDEND_RELOC_MASK & (1 << rType)) !== 0) {
235
- let byte = buf[cur++]!
236
- let value = byte & 0x7f
237
- let shift = 7
238
- while (byte >= 128) {
239
- byte = buf[cur++]!
240
- value |= (byte & 0x7f) << shift
241
- shift += 7
242
- }
243
- if (shift < 32 && (byte & 0x40) !== 0) value |= ~0 << shift
244
- addend[i] = value
185
+ let value: bigint
186
+ ;[value, cur] = readSLEB(buf, cur)
187
+ addend[i] = Number(BigInt.asIntN(32, value))
245
188
  }
246
189
  ro[i] = offset
247
190
  rs[i] = symbol
@@ -410,8 +353,7 @@ export function patchSwiftrtObject(src: Uint8Array): Uint8Array {
410
353
  }
411
354
  cur = c3
412
355
  if (ADDEND_RELOCATIONS.has(rType)) {
413
- const [, c4] = readSLEB(buf, cur)
414
- cur = c4
356
+ cur = skipLEB(buf, cur)
415
357
  }
416
358
  }
417
359
  }
@@ -433,27 +375,14 @@ function parseWasmObjectBytes(
433
375
  let pos = 8
434
376
  let cur = 8
435
377
  const readU = (): number => {
436
- const b0 = buf[cur++]!
437
- if (b0 < 128) return b0
438
- let res = b0 & 0x7f
439
- let shift = 7
440
- while (true) {
441
- const byte = buf[cur++]!
442
- res |= (byte & 0x7f) << shift
443
- if (byte < 128) return res
444
- shift += 7
445
- }
378
+ const [res, next] = readULEB(buf, cur)
379
+ cur = next
380
+ return res
446
381
  }
447
382
  const skipU = (): void => {
448
- while (buf[cur++]! >= 128) {
449
- // advance past continuation bytes
450
- }
451
- }
452
- const skipS = (): void => {
453
- while (buf[cur++]! >= 128) {
454
- // advance past continuation bytes
455
- }
383
+ cur = skipLEB(buf, cur)
456
384
  }
385
+ const skipS = skipU
457
386
  let numImportFuncs = 0
458
387
  let importsDlsym = false
459
388
  const importFuncNames: string[] = []
@@ -1129,16 +1058,9 @@ export function guestWasmSymbols(bytes: Uint8Array): {
1129
1058
  : Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength)
1130
1059
  let cur = 8
1131
1060
  const readU = (): number => {
1132
- const b0 = bytes[cur++]!
1133
- if (b0 < 128) return b0
1134
- let res = b0 & 0x7f
1135
- let shift = 7
1136
- while (true) {
1137
- const byte = bytes[cur++]!
1138
- res |= (byte & 0x7f) << shift
1139
- if ((byte & 0x80) === 0) return res
1140
- shift += 7
1141
- }
1061
+ const [res, next] = readULEB(bytes, cur)
1062
+ cur = next
1063
+ return res
1142
1064
  }
1143
1065
  let importsDlsym = false
1144
1066
  const importFuncNames: string[] = []
@@ -8,21 +8,71 @@ export function encodeWasmNumber(value: number): number[] {
8
8
  }
9
9
  }
10
10
 
11
+ export function encodeWasmSigned(value: bigint): number[] {
12
+ const out: number[] = []
13
+ for (;;) {
14
+ const byte = Number(value & 127n)
15
+ value >>= 7n
16
+ if ((value === 0n && !(byte & 64)) || (value === -1n && byte & 64)) {
17
+ out.push(byte)
18
+ return out
19
+ }
20
+ out.push(byte | 128)
21
+ }
22
+ }
23
+
11
24
  export function encodeWasmName(name: string): number[] {
12
25
  const bytes = new TextEncoder().encode(name)
13
26
  return [...encodeWasmNumber(bytes.length), ...bytes]
14
27
  }
15
28
 
29
+ export function readULEB(
30
+ bytes: Uint8Array,
31
+ offset: number,
32
+ end = bytes.length,
33
+ ): [number, number] {
34
+ if (offset < end) {
35
+ const first = bytes[offset]!
36
+ if (first < 128) return [first, offset + 1]
37
+ }
38
+ let value = 0
39
+ for (let shift = 0; ; shift += 7) {
40
+ if (offset >= end || shift > 63) throw new Error('invalid wasm integer')
41
+ const byte = bytes[offset++]!
42
+ value += (byte & 127) * 2 ** shift
43
+ if (!(byte & 128)) return [value, offset]
44
+ }
45
+ }
46
+
47
+ export function readSLEB(
48
+ bytes: Uint8Array,
49
+ offset: number,
50
+ end = bytes.length,
51
+ ): [bigint, number] {
52
+ let value = 0n
53
+ let shift = 0n
54
+ let byte = 0
55
+ do {
56
+ if (offset >= end || shift > 70n) throw new Error('invalid wasm integer')
57
+ byte = bytes[offset++]!
58
+ value |= BigInt(byte & 0x7f) << shift
59
+ shift += 7n
60
+ } while (byte & 0x80)
61
+ if (byte & 0x40) value -= 1n << shift
62
+ return [value, offset]
63
+ }
64
+
65
+ export function skipLEB(bytes: Uint8Array, offset: number): number {
66
+ while ((bytes[offset++]! & 0x80) !== 0) {}
67
+ return offset
68
+ }
69
+
16
70
  export function readWasmSection(bytes: Uint8Array, start: number, end: number) {
17
71
  let at = start
18
72
  const number = () => {
19
- let value = 0
20
- for (let shift = 0; ; shift += 7) {
21
- if (at >= end || shift > 63) throw new Error('invalid wasm integer')
22
- const byte = bytes[at++]!
23
- value += (byte & 127) * 2 ** shift
24
- if (!(byte & 128)) return value
25
- }
73
+ const [value, next] = readULEB(bytes, at, end)
74
+ at = next
75
+ return value
26
76
  }
27
77
  return {
28
78
  number,
@@ -36,6 +86,10 @@ export function readWasmSection(bytes: Uint8Array, start: number, end: number) {
36
86
  if (at > end) throw new Error('truncated wasm name')
37
87
  return Buffer.from(bytes.buffer, bytes.byteOffset + at - length, length).toString()
38
88
  },
89
+ skip: (length: number) => {
90
+ at += length
91
+ if (at > end) throw new Error('truncated wasm section')
92
+ },
39
93
  position: () => at,
40
94
  done: () => at === end,
41
95
  }