peachsim 0.1.2277 → 0.1.2296

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 (114) hide show
  1. package/cli/commands/platform.ts +28 -8
  2. package/cli/commands/shell-url.ts +13 -4
  3. package/cli/outbound-endpoints.ts +2 -0
  4. package/dist-lib/agent-daemon-client.cjs +1 -1
  5. package/dist-lib/agent-events.cjs +1 -1
  6. package/dist-lib/attached-projects.cjs +1 -1
  7. package/dist-lib/auth/shared-session.cjs +1 -1
  8. package/dist-lib/backend-origin.cjs +1 -1
  9. package/dist-lib/beta.cjs +1 -1
  10. package/dist-lib/beta.mjs +1 -1
  11. package/dist-lib/bridge-constants.cjs +1 -1
  12. package/dist-lib/bridge-contract-input.cjs +1 -1
  13. package/dist-lib/bridge-contract-input.mjs +1 -1
  14. package/dist-lib/bridge-contract.cjs +1 -1
  15. package/dist-lib/bridge-contract.mjs +1 -1
  16. package/dist-lib/bundle-channel.cjs +2552 -2481
  17. package/dist-lib/capture-contract.cjs +1 -1
  18. package/dist-lib/capture-contract.mjs +1 -1
  19. package/dist-lib/cloud-contract.cjs +1 -1
  20. package/dist-lib/cloud-contract.mjs +1 -1
  21. package/dist-lib/cloud-test-args.cjs +1 -1
  22. package/dist-lib/cloud-test-args.mjs +1 -1
  23. package/dist-lib/cloud.cjs +1 -1
  24. package/dist-lib/cloud.mjs +1 -1
  25. package/dist-lib/config.cjs +1 -1
  26. package/dist-lib/config.mjs +1 -1
  27. package/dist-lib/detox/index.cjs +2 -2
  28. package/dist-lib/dev-bundle-resolution.cjs +1 -1
  29. package/dist-lib/expo-swift-bundle.cjs +1953 -1881
  30. package/dist-lib/expo-swift-metadata.cjs +1 -1
  31. package/dist-lib/expo-swift-metadata.mjs +1 -1
  32. package/dist-lib/home-paths.cjs +1 -1
  33. package/dist-lib/host/bridge-host.cjs +2765 -2694
  34. package/dist-lib/host/fetch-proxy-handler.cjs +16 -6
  35. package/dist-lib/host/fetch-proxy-overrides.cjs +1 -1
  36. package/dist-lib/host/fetch-proxy-overrides.mjs +1 -1
  37. package/dist-lib/host/replacement-module-handler.cjs +1 -1
  38. package/dist-lib/host/websocket-proxy.cjs +1 -1
  39. package/dist-lib/index.cjs +9865 -7701
  40. package/dist-lib/jump-to-source-babel.cjs +1 -1
  41. package/dist-lib/jump-to-source-native.cjs +1 -1
  42. package/dist-lib/kotlin-host.cjs +1 -1
  43. package/dist-lib/menu.cjs +1 -1
  44. package/dist-lib/menu.mjs +1 -1
  45. package/dist-lib/metro-fingerprint-registry.cjs +1 -1
  46. package/dist-lib/metro-fingerprint-registry.mjs +1 -1
  47. package/dist-lib/metro-production-bundle.cjs +1 -1
  48. package/dist-lib/metro-production-bundle.mjs +1 -1
  49. package/dist-lib/metro.cjs +6699 -4534
  50. package/dist-lib/native-app-identity-project.cjs +12 -4
  51. package/dist-lib/native-app-identity-project.mjs +12 -4
  52. package/dist-lib/native-app-identity.cjs +11 -3
  53. package/dist-lib/native-app-identity.mjs +11 -3
  54. package/dist-lib/native-firebase-metadata.cjs +1 -1
  55. package/dist-lib/native-firebase-metadata.mjs +1 -1
  56. package/dist-lib/pdfium-raster.cjs +1 -1
  57. package/dist-lib/pdfium-raster.mjs +1 -1
  58. package/dist-lib/plist.cjs +1 -1
  59. package/dist-lib/plist.mjs +1 -1
  60. package/dist-lib/profiles.cjs +1 -1
  61. package/dist-lib/public-brand.cjs +1 -1
  62. package/dist-lib/public-origins-lib-esm.json +26 -25
  63. package/dist-lib/public-origins-lib.json +92 -78
  64. package/dist-lib/react-native-host-modules.cjs +1 -1
  65. package/dist-lib/react-native-host-modules.mjs +1 -1
  66. package/dist-lib/reap-browser.cjs +1 -1
  67. package/dist-lib/scripts/dev-server-scanner.cjs +1 -1
  68. package/dist-lib/sdk.cjs +1 -1
  69. package/dist-lib/sdk.mjs +1 -1
  70. package/dist-lib/skills.cjs +3468 -3364
  71. package/dist-lib/swift-compile.cjs +1 -1
  72. package/dist-lib/swift-compile.mjs +1 -1
  73. package/dist-lib/swift-package.cjs +1 -1
  74. package/dist-lib/swift-package.mjs +1 -1
  75. package/dist-lib/swift-submit.cjs +1 -1
  76. package/dist-lib/swift-submit.mjs +1 -1
  77. package/dist-lib/swift-toolchain.cjs +1 -1
  78. package/dist-lib/swift.cjs +1 -1
  79. package/dist-lib/vite-swift.cjs +465 -446
  80. package/dist-lib/vite.cjs +16 -6
  81. package/dist-lib/worklet-executable.cjs +421 -118
  82. package/dist-lib/worklet-executable.mjs +427 -113
  83. package/dist-types/packages/peach-engine/src/engine/animation-graph.d.ts +3 -1
  84. package/dist-types/packages/peach-engine/src/engine/node.d.ts +4 -3
  85. package/dist-types/packages/peach-engine/src/engine/scene-table.d.ts +3 -1
  86. package/dist-types/packages/peach-engine/src/engine/scroll-momentum-math.d.ts +14 -15
  87. package/dist-types/packages/peach-engine/src/engine/scroll-platform.d.ts +17 -21
  88. package/dist-types/packages/peach-engine/src/engine/scroll-velocity.d.ts +0 -1
  89. package/dist-types/packages/peach-engine/src/kernel/kernel.d.ts +69 -0
  90. package/dist-types/packages/peach-engine/src/kernel/worklet-kernel.d.ts +28 -0
  91. package/dist-types/packages/peach-engine/src/platform/personality.d.ts +1 -1
  92. package/dist-types/packages/peach-engine/src/render-worker/app-load.d.ts +4 -0
  93. package/dist-types/packages/peach-engine/src/render-worker/graph-linked-value.d.ts +3 -3
  94. package/dist-types/packages/peach-engine/src/render-worker/worker-input-runtime.d.ts +1 -2
  95. package/dist-types/packages/peach-engine/src/render-worker/worklet-serialize.d.ts +5 -0
  96. package/dist-types/packages/peach-engine/src/render-worker/worklet-wire.d.ts +2 -0
  97. package/kotlin/sdk.tar.gz +0 -0
  98. package/package.json +1 -1
  99. package/runtime-companion.json +1 -1
  100. package/src/expo-native-prebuilt.ts +24 -0
  101. package/src/expo-native-workspace.ts +51 -5
  102. package/src/metro-plugin.ts +4 -1
  103. package/src/nitro-adapter.lock.json +265 -265
  104. package/src/nitro-superset.lock.json +4 -4
  105. package/src/swift-compiler-digest.ts +8 -1
  106. package/src/swift-distribution.lock.json +8 -8
  107. package/src/swift-helper.ts +38 -0
  108. package/src/swift-pdf-raster.ts +7 -33
  109. package/src/turbo-compile.ts +12 -1
  110. package/src/turbo-runtime.lock.json +19 -19
  111. package/src/worklet-executable.ts +4 -1
  112. package/src/worklet-kernel.ts +370 -0
  113. package/src/worklets-babel.ts +10 -0
  114. package/src/xcode-target-members.ts +17 -7
@@ -17,6 +17,7 @@ import {
17
17
  } from '../cli/internal-child-contract'
18
18
  import { rnxSelfInvocation } from '../cli/self-invocation'
19
19
  import { cacheDir } from './home-paths'
20
+ import { compiledSwiftHelper } from './swift-helper'
20
21
 
21
22
  // draws the pdf's first page at 3x into a transparent png. a bitmap context is
22
23
  // already y-up like the pdf page, so only scale. the media box
@@ -46,37 +47,6 @@ CGImageDestinationAddImage(dest, image, nil)
46
47
  guard CGImageDestinationFinalize(dest) else { exit(7) }
47
48
  `
48
49
 
49
- function rasterBinary(swift = RASTER_SWIFT, name = 'swift-pdf-raster'): string {
50
- const tag = createHash('sha256').update(swift).digest('hex').slice(0, 16)
51
- const binary = path.join(cacheDir(), `${name}-${tag}`)
52
- try {
53
- fs.accessSync(binary, fs.constants.X_OK)
54
- return binary
55
- } catch {
56
- // fall through to compile
57
- }
58
- try {
59
- fs.mkdirSync(cacheDir(), { recursive: true })
60
- const source = path.join(
61
- fs.mkdtempSync(path.join(os.tmpdir(), 'swift-pdf-raster-')),
62
- 'raster.swift',
63
- )
64
- fs.writeFileSync(source, swift)
65
- try {
66
- // runner-toolchain-platform: darwin
67
- execFileSync('xcrun', ['swiftc', '-o', binary, source], { stdio: 'pipe' })
68
- } finally {
69
- fs.rmSync(path.dirname(source), { recursive: true, force: true })
70
- }
71
- fs.accessSync(binary, fs.constants.X_OK)
72
- return binary
73
- } catch (error) {
74
- throw new Error(
75
- `swift pdf asset rasterizer did not compile (xcrun swiftc): ${error instanceof Error ? error.message : String(error)}`,
76
- )
77
- }
78
- }
79
-
80
50
  /**
81
51
  * the pdf file's page as 3x png bytes, cached by the pdf's sha256. throws
82
52
  * when the pdf cannot rasterize (corrupt, oversize), like actool failing
@@ -103,7 +73,11 @@ export function rasterizePdfFile(diskPath: string, pdfSha256: string): { bytes:
103
73
  )
104
74
  try {
105
75
  if (process.platform === 'darwin') {
106
- execFileSync(rasterBinary(), [diskPath, '3', out], { stdio: 'pipe' })
76
+ execFileSync(
77
+ compiledSwiftHelper(RASTER_SWIFT, 'swift-pdf-raster'),
78
+ [diskPath, '3', out],
79
+ { stdio: 'pipe' },
80
+ )
107
81
  } else {
108
82
  const { executable, prefixArgs } = rnxSelfInvocation()
109
83
  execFileSync(
@@ -169,7 +143,7 @@ export function rasterizeSvgFile(
169
143
  svgSha256: string,
170
144
  ): { bytes: Buffer } | null {
171
145
  if (process.platform !== 'darwin') return null
172
- const binary = rasterBinary(RASTER_SVG_SWIFT, 'swift-svg-raster')
146
+ const binary = compiledSwiftHelper(RASTER_SVG_SWIFT, 'swift-svg-raster')
173
147
  const tag = createHash('sha256').update(RASTER_SVG_SWIFT).digest('hex').slice(0, 16)
174
148
  const cached = path.join(cacheDir(), `swift-svg-png-${tag}-${svgSha256}.png`)
175
149
  if (fs.existsSync(cached)) return { bytes: fs.readFileSync(cached) }
@@ -106,9 +106,20 @@ export function collectTurboCompileInput(
106
106
  const swift = Object.keys(files).some((file) => file.endsWith('.swift'))
107
107
  const entry = `Sources/PeachTurboApp/main.${swift ? 'swift' : 'cpp'}`
108
108
  const targetNames = targets.map(({ name }) => name)
109
+ const loadTargetNames = new Set(collection.loads.map(({ target }) => target))
110
+ const swiftImportedTargetNames = targets
111
+ .filter(
112
+ (target) =>
113
+ target.name === TURBO_CORE_TARGET ||
114
+ loadTargetNames.has(target.name) ||
115
+ Object.keys(files).some(
116
+ (file) => file.startsWith(`${target.path}/`) && file.endsWith('.swift'),
117
+ ),
118
+ )
119
+ .map(({ name }) => name)
109
120
  files[entry] = swift
110
121
  ? '@_spi(Peach) import PeachFoundation\n' +
111
- targetNames.map((target) => `import ${target}\n`).join('') +
122
+ swiftImportedTargetNames.map((target) => `import ${target}\n`).join('') +
112
123
  `PeachHostDriver.installExecutor()\n${collection.loads.map(({ symbol }) => `${symbol}()\n`).join('')}${runtime ? 'peachTurboInitialize()\n' : ''}`
113
124
  : [
114
125
  'extern "C" void peachTurboInitialize();',
@@ -12,7 +12,7 @@
12
12
  "checksum": "477c394dd425b54e77260bbc2bab9621642e800e982ada3b99d60158cc3b8eaa"
13
13
  },
14
14
  "framework": {
15
- "distribution": "f4c3215552215bd8",
15
+ "distribution": "4ca2b3c83875439a",
16
16
  "wireAbi": "38eed69f6b7391584112981b7892914e7033d7e0c17c2e8d4a4448030618bd2b"
17
17
  },
18
18
  "archives": [
@@ -20,71 +20,71 @@
20
20
  "headers": {
21
21
  "reactNativeJsi": "0f8c2defbca69a27a848a81ae72363073bf9602fd316e57a75811be1a0528358"
22
22
  },
23
- "sha256": "ac578c19841f163e1d6b5ee28d42fbb4463ae11cd8d0d74daeac5b3faabf6389",
24
- "size": 26175
23
+ "sha256": "18f8d215b6d696ddca4cd99a10709c4e97280c3226cf71f96455206e9f64dd92",
24
+ "size": 26177
25
25
  },
26
26
  {
27
27
  "headers": {
28
28
  "reactNativeJsi": "21a624ea24fd17d56bf26832d496fc1177067f89640414c20e6486c7984dfb0a"
29
29
  },
30
- "sha256": "45a85c596fd3c51c7ab51247a812d8598543282d73ffaa6dc71942941d96ffc3",
31
- "size": 26912
30
+ "sha256": "cb16bdcad7a222e8f09da673fc1589c3d9c30fa4e076ae97108db1c0191de94e",
31
+ "size": 26910
32
32
  },
33
33
  {
34
34
  "headers": {
35
35
  "reactNativeJsi": "324aefd8591d9ce688bce7589c4c2b516cd33ecb95d35c47239a87a8fcf6a8da"
36
36
  },
37
- "sha256": "3e3bb4fe17120d78b2da6d31971fbaa1971a80f6f6301879931946b0750d4241",
38
- "size": 26169
37
+ "sha256": "8974e47ed58bf40755939bd6f5bf85e1862abf0b7ba54b1a8fb62f3828a16e93",
38
+ "size": 26170
39
39
  },
40
40
  {
41
41
  "headers": {
42
42
  "reactNativeJsi": "33596664ee7d7a6623f0796a6feb429543316c3a41b211150acda6e1d835286c"
43
43
  },
44
- "sha256": "eae48886086ce77bfc2b52fe9f2eb8d9d9a542cc2d6a9ede4e5e2db112bf8c6e",
45
- "size": 25955
44
+ "sha256": "e8933671813fd1ed8143d8246b905054c7c606dc875b7ffe962da73cdc3e5bfe",
45
+ "size": 25956
46
46
  },
47
47
  {
48
48
  "headers": {
49
49
  "reactNativeJsi": "66b2cd5a38511195207f6c8a76aba735f9b9cc66a52a0d00780ffe5d331623f7"
50
50
  },
51
- "sha256": "be46c06399c7fe83f31bd64bcc9fb403380506a3e2c6eee499babdb3fc0c9c10",
52
- "size": 26172
51
+ "sha256": "7e4223f63577ebc4795f7c76fdf796774ea588d2cfb860fba857644361447a9a",
52
+ "size": 26175
53
53
  },
54
54
  {
55
55
  "headers": {
56
56
  "reactNativeJsi": "96db4e758dd53e9320cef7cad485e00aa06e7a982cefd920f17007de8a5bd9f9"
57
57
  },
58
- "sha256": "5a2b794edeb5e44cd00a5a7367a1d7f0470edbd59a16ae7155eec19de67c7731",
58
+ "sha256": "86b44d75adb545dbe7fe05650d4a587d97d33ef83ce650731f6423c33e5aee5e",
59
59
  "size": 26910
60
60
  },
61
61
  {
62
62
  "headers": {
63
63
  "reactNativeJsi": "aeab590ddd1218df963462242d3954ce467b0fa88f70e91429e4ffc74d230893"
64
64
  },
65
- "sha256": "ad9d67c8713780c3c0cdae3e043e972d648dbdbaddc40581d3a980690ff3e537",
66
- "size": 26913
65
+ "sha256": "d7736f6945b3e0098e1aa8f9503af9041e51917b1e1830bf11f04c986f5b7fe0",
66
+ "size": 26912
67
67
  },
68
68
  {
69
69
  "headers": {
70
70
  "reactNativeJsi": "b5fb260522384dd7ab07d740cdc534b529922446fcec798341c7817d9eaa49f4"
71
71
  },
72
- "sha256": "7783caf01f47fc836cdb8c7fd4ffdc0a0455225790bc4527829d231df11623e2",
73
- "size": 26042
72
+ "sha256": "deb1724dcb520725faa297f38cb0fe3d0d6f97944cc6ebd034bc174ec808527a",
73
+ "size": 26045
74
74
  },
75
75
  {
76
76
  "headers": {
77
77
  "reactNativeJsi": "c6b391a1e870ccf7cdd534be63582862840f8245b99ebd253317deece1e6ffd5"
78
78
  },
79
- "sha256": "b65ceac9fa4e0a5ecc82bdc74984b7524ca94c013280998f173a6b58fe179849",
79
+ "sha256": "bd83c2c44f265ff8012854c558d0ba80a727dbd8e4810838752ddd6051022ec6",
80
80
  "size": 26910
81
81
  },
82
82
  {
83
83
  "headers": {
84
84
  "reactNativeJsi": "c8270f1315745ecc204c4209a4f50f58ff6f14b4a385ad6820a69800270b53c6"
85
85
  },
86
- "sha256": "508044d5bbd00c45069f6256bdee4b630a5219d9c307d8273f4a17c31cb9bd27",
87
- "size": 25918
86
+ "sha256": "7c09726a6340572398be6a2357330d9ff5d1d360601454cd80e03e0c180c3643",
87
+ "size": 25919
88
88
  }
89
89
  ]
90
90
  }
@@ -1,4 +1,5 @@
1
1
  import { parse } from 'acorn'
2
+ import { compileWorkletKernel } from './worklet-kernel.ts'
2
3
 
3
4
  interface SyntaxNode {
4
5
  type: string
@@ -524,7 +525,9 @@ export function compileWorkletExecutables(
524
525
  }
525
526
  const offset = Reflect.get(codeProperty, 'end')
526
527
  if (typeof offset !== 'number') throw new Error('peach: worklet code has no offset')
527
- return { offset, text: `, __peachExecutable: (${body})` }
528
+ const kernel = compileWorkletKernel(code)
529
+ const recipe = kernel ? `, __peachKernel: ${JSON.stringify(kernel)}` : ''
530
+ return { offset, text: `, __peachExecutable: (${body})${recipe}` }
528
531
  })
529
532
  const parts: string[] = []
530
533
  let cursor = source.length
@@ -0,0 +1,370 @@
1
+ import { parse } from 'acorn'
2
+ import type {
3
+ WorkletKernelSide,
4
+ WorkletKernelStep,
5
+ WorkletKernelOp,
6
+ WorkletKernel,
7
+ } from '../../peach-engine/src/kernel/worklet-kernel'
8
+
9
+ interface Node {
10
+ type: string
11
+ [key: string]: unknown
12
+ }
13
+
14
+ class Unsupported extends Error {}
15
+
16
+ function isNode(value: unknown): value is Node {
17
+ return (
18
+ value !== null &&
19
+ typeof value === 'object' &&
20
+ typeof Reflect.get(value, 'type') === 'string'
21
+ )
22
+ }
23
+
24
+ function node(value: unknown): Node {
25
+ if (!isNode(value)) throw new Unsupported()
26
+ return value
27
+ }
28
+
29
+ function nodes(value: unknown): Node[] {
30
+ if (!Array.isArray(value)) throw new Unsupported()
31
+ return value.map(node)
32
+ }
33
+
34
+ function name(value: unknown): string {
35
+ const n = node(value)
36
+ if (n.type !== 'Identifier' || typeof n.name !== 'string') throw new Unsupported()
37
+ return n.name
38
+ }
39
+
40
+ // the static key a non-computed member or property names
41
+ function key(owner: Node, value: unknown): string {
42
+ if (owner.computed === true) throw new Unsupported()
43
+ const n = node(value)
44
+ if (n.type === 'Identifier' && typeof n.name === 'string') return n.name
45
+ if (n.type === 'Literal' && typeof n.value === 'string') return n.value
46
+ throw new Unsupported()
47
+ }
48
+
49
+ const MATH: Record<string, { op: WorkletKernelOp; arity: number | 'any' }> = {
50
+ min: { op: 'min', arity: 'any' },
51
+ max: { op: 'max', arity: 'any' },
52
+ abs: { op: 'abs', arity: 1 },
53
+ sqrt: { op: 'sqrt', arity: 1 },
54
+ exp: { op: 'exp', arity: 1 },
55
+ sin: { op: 'sin', arity: 1 },
56
+ cos: { op: 'cos', arity: 1 },
57
+ pow: { op: 'pow', arity: 2 },
58
+ }
59
+
60
+ const BINARY: Record<string, WorkletKernelOp> = {
61
+ '+': 'add',
62
+ '-': 'sub',
63
+ '*': 'mul',
64
+ '/': 'quotient',
65
+ '**': 'pow',
66
+ }
67
+
68
+ class Recipe {
69
+ readonly steps: WorkletKernelStep[] = []
70
+ readonly #interned = new Map<string, number>()
71
+ readonly locals = new Map<string, number>()
72
+
73
+ constructor(
74
+ readonly closure: Map<string, number>,
75
+ readonly event: string | undefined,
76
+ ) {}
77
+
78
+ step(step: WorkletKernelStep): number {
79
+ const id = JSON.stringify(step)
80
+ const held = this.#interned.get(id)
81
+ if (held !== undefined) return held
82
+ this.steps.push(step)
83
+ this.#interned.set(id, this.steps.length - 1)
84
+ return this.steps.length - 1
85
+ }
86
+
87
+ slot(identifier: unknown): number {
88
+ const slot = this.closure.get(name(identifier))
89
+ if (slot === undefined) throw new Unsupported()
90
+ return slot
91
+ }
92
+
93
+ // a comparison as a kernel 1 or 0, for a conditional's test
94
+ test(expression: Node): number {
95
+ if (expression.type !== 'BinaryExpression') throw new Unsupported()
96
+ const left = this.value(node(expression.left))
97
+ const right = this.value(node(expression.right))
98
+ switch (expression.operator) {
99
+ case '>':
100
+ return this.step(['op', 'gt', left, right])
101
+ case '>=':
102
+ return this.step(['op', 'gte', left, right])
103
+ case '<':
104
+ return this.step(['op', 'gt', right, left])
105
+ case '<=':
106
+ return this.step(['op', 'gte', right, left])
107
+ }
108
+ throw new Unsupported()
109
+ }
110
+
111
+ side(expression: Node): WorkletKernelSide {
112
+ if (expression.type === 'Literal' && typeof expression.value === 'string')
113
+ return expression.value
114
+ if (expression.type === 'MemberExpression')
115
+ return [
116
+ 'member',
117
+ this.slot(expression.object),
118
+ key(expression, expression.property),
119
+ ]
120
+ throw new Unsupported()
121
+ }
122
+
123
+ extrapolation(argument: unknown): readonly [WorkletKernelSide, WorkletKernelSide] {
124
+ if (argument === undefined) return [null, null]
125
+ const expression = node(argument)
126
+ if (expression.type !== 'ObjectExpression') {
127
+ const side = this.side(expression)
128
+ return [side, side]
129
+ }
130
+ let left: WorkletKernelSide = null
131
+ let right: WorkletKernelSide = null
132
+ for (const property of nodes(expression.properties)) {
133
+ if (
134
+ property.type !== 'Property' ||
135
+ property.kind !== 'init' ||
136
+ property.method === true
137
+ )
138
+ throw new Unsupported()
139
+ const which = key(property, property.key)
140
+ if (which === 'extrapolateLeft') left = this.side(node(property.value))
141
+ else if (which === 'extrapolateRight') right = this.side(node(property.value))
142
+ else throw new Unsupported()
143
+ }
144
+ return [left, right]
145
+ }
146
+
147
+ list(argument: unknown): number[] {
148
+ const array = node(argument)
149
+ if (array.type !== 'ArrayExpression') throw new Unsupported()
150
+ return nodes(array.elements).map((element) => this.value(element))
151
+ }
152
+
153
+ value(expression: Node): number {
154
+ switch (expression.type) {
155
+ case 'Literal':
156
+ if (typeof expression.value !== 'number') throw new Unsupported()
157
+ return this.step(['number', expression.value])
158
+ case 'Identifier': {
159
+ const local = this.locals.get(name(expression))
160
+ if (local !== undefined) return local
161
+ return this.step(['closure', this.slot(expression)])
162
+ }
163
+ case 'MemberExpression': {
164
+ const object = node(expression.object)
165
+ const field = key(expression, expression.property)
166
+ if (field === 'value' && object.type === 'Identifier')
167
+ return this.step(['shared', this.slot(object)])
168
+ if (
169
+ this.event !== undefined &&
170
+ object.type === 'MemberExpression' &&
171
+ node(object.object).type === 'Identifier' &&
172
+ name(object.object) === this.event &&
173
+ key(object, object.property) === 'contentOffset' &&
174
+ (field === 'x' || field === 'y')
175
+ )
176
+ return this.step(['event', `contentOffset.${field}`])
177
+ throw new Unsupported()
178
+ }
179
+ case 'UnaryExpression':
180
+ if (expression.operator !== '-') throw new Unsupported()
181
+ return this.step(['op', 'neg', this.value(node(expression.argument))])
182
+ case 'BinaryExpression': {
183
+ const op = BINARY[String(expression.operator)]
184
+ if (!op) throw new Unsupported()
185
+ return this.step([
186
+ 'op',
187
+ op,
188
+ this.value(node(expression.left)),
189
+ this.value(node(expression.right)),
190
+ ])
191
+ }
192
+ case 'ConditionalExpression':
193
+ return this.step([
194
+ 'op',
195
+ 'select',
196
+ this.test(node(expression.test)),
197
+ this.value(node(expression.consequent)),
198
+ this.value(node(expression.alternate)),
199
+ ])
200
+ case 'CallExpression':
201
+ return this.call(expression)
202
+ }
203
+ throw new Unsupported()
204
+ }
205
+
206
+ call(expression: Node): number {
207
+ if (expression.optional === true) throw new Unsupported()
208
+ const callee = node(expression.callee)
209
+ const args = nodes(expression.arguments)
210
+ if (args.some((argument) => argument.type === 'SpreadElement'))
211
+ throw new Unsupported()
212
+ if (
213
+ callee.type === 'MemberExpression' &&
214
+ node(callee.object).type === 'Identifier' &&
215
+ name(callee.object) === 'Math' &&
216
+ !this.closure.has('Math')
217
+ ) {
218
+ const math = MATH[key(callee, callee.property)]
219
+ if (!math || args.length === 0) throw new Unsupported()
220
+ if (math.arity !== 'any' && args.length !== math.arity) throw new Unsupported()
221
+ const values = args.map((argument) => this.value(argument))
222
+ if (math.arity === 1) return this.step(['op', math.op, values[0]!])
223
+ return values
224
+ .slice(1)
225
+ .reduce((a, b) => this.step(['op', math.op, a, b]), values[0]!)
226
+ }
227
+ if (callee.type !== 'Identifier') throw new Unsupported()
228
+ const slot = this.slot(callee)
229
+ if (args.length === 3 && args[1]!.type !== 'ArrayExpression')
230
+ return this.step([
231
+ 'clamp',
232
+ slot,
233
+ this.value(args[0]!),
234
+ this.value(args[1]!),
235
+ this.value(args[2]!),
236
+ ])
237
+ if (args.length === 3 || args.length === 4) {
238
+ const inputs = this.list(args[1])
239
+ const outputs = this.list(args[2])
240
+ if (inputs.length < 2 || inputs.length !== outputs.length) throw new Unsupported()
241
+ return this.step([
242
+ 'interpolate',
243
+ slot,
244
+ this.value(args[0]!),
245
+ inputs,
246
+ outputs,
247
+ this.extrapolation(args[3]),
248
+ ])
249
+ }
250
+ throw new Unsupported()
251
+ }
252
+ }
253
+
254
+ // `const [a, b] = this.__closure` names each closure slot
255
+ function closureSlots(statement: Node): Map<string, number> | undefined {
256
+ if (statement.type !== 'VariableDeclaration') return undefined
257
+ const [declarator] = nodes(statement.declarations)
258
+ if (nodes(statement.declarations).length !== 1 || !declarator) return undefined
259
+ const init = declarator.init === null ? undefined : node(declarator.init)
260
+ if (
261
+ !init ||
262
+ init.type !== 'MemberExpression' ||
263
+ node(init.object).type !== 'ThisExpression' ||
264
+ key(init, init.property) !== '__closure'
265
+ )
266
+ return undefined
267
+ const pattern = node(declarator.id)
268
+ if (pattern.type !== 'ArrayPattern') throw new Unsupported()
269
+ const slots = new Map<string, number>()
270
+ nodes(pattern.elements).forEach((element, index) => slots.set(name(element), index))
271
+ return slots
272
+ }
273
+
274
+ function styleOutputs(
275
+ recipe: Recipe,
276
+ expression: Node,
277
+ path: string,
278
+ outputs: [string, number][],
279
+ ): void {
280
+ if (expression.type === 'ObjectExpression') {
281
+ for (const property of nodes(expression.properties)) {
282
+ if (
283
+ property.type !== 'Property' ||
284
+ property.kind !== 'init' ||
285
+ property.method === true
286
+ )
287
+ throw new Unsupported()
288
+ const field = key(property, property.key)
289
+ styleOutputs(
290
+ recipe,
291
+ node(property.value),
292
+ path ? `${path}.${field}` : field,
293
+ outputs,
294
+ )
295
+ }
296
+ return
297
+ }
298
+ if (expression.type === 'ArrayExpression') {
299
+ nodes(expression.elements).forEach((element, index) =>
300
+ styleOutputs(recipe, element, `${path}.${index}`, outputs),
301
+ )
302
+ return
303
+ }
304
+ outputs.push([path, recipe.value(expression)])
305
+ }
306
+
307
+ // the recipe of one worklet's code, as upstream's babel plugin emits it, or undefined when the
308
+ // worklet does anything a kernel cannot carry exactly
309
+ export function compileWorkletKernel(code: string): WorkletKernel | undefined {
310
+ try {
311
+ // the code is a function declaration or expression, named or not, as the executable pass reads it
312
+ const program = parse(`(${code})`, { ecmaVersion: 'latest' })
313
+ const [statement] = nodes(program.body)
314
+ if (!statement || statement.type !== 'ExpressionStatement') return undefined
315
+ const fn = node(statement.expression)
316
+ if (fn.type !== 'FunctionExpression' || fn.async === true || fn.generator === true)
317
+ return undefined
318
+ const params = nodes(fn.params)
319
+ const body = nodes(node(fn.body).body)
320
+ const slots = body[0] ? closureSlots(body[0]) : undefined
321
+ const statements = slots ? body.slice(1) : body
322
+ if (params.length === 0) {
323
+ const recipe = new Recipe(slots ?? new Map(), undefined)
324
+ for (const [index, item] of statements.entries()) {
325
+ if (item.type === 'ReturnStatement' && index === statements.length - 1) {
326
+ const outputs: [string, number][] = []
327
+ const returned = node(item.argument)
328
+ if (returned.type !== 'ObjectExpression') return undefined
329
+ styleOutputs(recipe, returned, '', outputs)
330
+ if (outputs.length === 0) return undefined
331
+ return { kind: 'style', steps: recipe.steps, outputs }
332
+ }
333
+ declareLocals(recipe, item)
334
+ }
335
+ return undefined
336
+ }
337
+ if (params.length > 2) return undefined
338
+ const recipe = new Recipe(slots ?? new Map(), name(params[0]))
339
+ const writes: [number, number][] = []
340
+ for (const item of statements) {
341
+ if (item.type === 'VariableDeclaration') {
342
+ declareLocals(recipe, item)
343
+ continue
344
+ }
345
+ if (item.type !== 'ExpressionStatement') return undefined
346
+ const assignment = node(item.expression)
347
+ if (assignment.type !== 'AssignmentExpression' || assignment.operator !== '=')
348
+ return undefined
349
+ const target = node(assignment.left)
350
+ if (target.type !== 'MemberExpression' || key(target, target.property) !== 'value')
351
+ return undefined
352
+ writes.push([recipe.slot(target.object), recipe.value(node(assignment.right))])
353
+ }
354
+ if (writes.length === 0) return undefined
355
+ return { kind: 'handler', steps: recipe.steps, writes }
356
+ } catch (error) {
357
+ if (error instanceof Unsupported) return undefined
358
+ throw error
359
+ }
360
+ }
361
+
362
+ function declareLocals(recipe: Recipe, item: Node): void {
363
+ if (item.type !== 'VariableDeclaration' || item.kind !== 'const')
364
+ throw new Unsupported()
365
+ for (const declarator of nodes(item.declarations)) {
366
+ const local = name(declarator.id)
367
+ if (recipe.closure.has(local) || recipe.locals.has(local)) throw new Unsupported()
368
+ recipe.locals.set(local, recipe.value(node(declarator.init)))
369
+ }
370
+ }
@@ -10,6 +10,7 @@ import {
10
10
  compileWorkletExecutables,
11
11
  workletExecutableBodies,
12
12
  } from './worklet-executable.ts'
13
+ import { compileWorkletKernel } from './worklet-kernel.ts'
13
14
  import type { PluginObj, TransformOptions } from '@babel/core'
14
15
  import type { TransformResult } from 'vite'
15
16
 
@@ -141,6 +142,7 @@ const CACHE_ROOT = path.join(homedir(), '.cache', 'peach-worklets')
141
142
  const CACHE_SCHEMA = createHash('sha256')
142
143
  .update(fs.readFileSync(path.join(sootsimRoot, 'src/worklets-babel.ts')))
143
144
  .update(fs.readFileSync(path.join(sootsimRoot, 'src/worklet-executable.ts')))
145
+ .update(fs.readFileSync(path.join(sootsimRoot, 'src/worklet-kernel.ts')))
144
146
  .digest('hex')
145
147
 
146
148
  // null once resolved on a tree we cannot write to (a read-only install), which
@@ -275,6 +277,14 @@ function executableWorkletsPlugin(
275
277
  api.template.expression.ast(`(${compileWorkletExecutables(code)})`),
276
278
  ),
277
279
  )
280
+ const kernel = compileWorkletKernel(code)
281
+ if (kernel)
282
+ properties.push(
283
+ api.types.objectProperty(
284
+ api.types.identifier('__peachKernel'),
285
+ api.types.valueToNode(kernel),
286
+ ),
287
+ )
278
288
  }
279
289
  },
280
290
  },
@@ -9,19 +9,29 @@ const FOLDER_REFERENCE = /^(?:folder|wrapper\.scnassets)$/
9
9
  const BUNDLE_DIR =
10
10
  /\.(xcassets|bundle|lproj|xcdatamodeld|scnassets|spriteatlas|rcproject|storyboardc|icon)$/
11
11
 
12
- export function xcodePathResolver(
13
- objects: PlistDict,
14
- projectDir: string,
15
- join: (base: string, own: string) => string,
16
- sourceTreeRoot?: (tree: string) => string,
17
- ): (id: string) => string | null {
18
- const parents = new Map<string, string>()
12
+ // a project's targets each build a resolver; the group parents are the same for all of them.
13
+ const groupParents = new WeakMap<PlistDict, Map<string, string>>()
14
+ function groupParentsOf(objects: PlistDict): Map<string, string> {
15
+ let parents = groupParents.get(objects)
16
+ if (parents) return parents
17
+ parents = new Map<string, string>()
19
18
  for (const [id, entry] of Object.entries(objects)) {
20
19
  if (!isDict(entry)) continue
21
20
  if (entry.isa === 'PBXGroup' || entry.isa === 'PBXVariantGroup') {
22
21
  for (const child of list(entry.children)) parents.set(child, id)
23
22
  }
24
23
  }
24
+ groupParents.set(objects, parents)
25
+ return parents
26
+ }
27
+
28
+ export function xcodePathResolver(
29
+ objects: PlistDict,
30
+ projectDir: string,
31
+ join: (base: string, own: string) => string,
32
+ sourceTreeRoot?: (tree: string) => string,
33
+ ): (id: string) => string | null {
34
+ const parents = groupParentsOf(objects)
25
35
  const paths = new Map<string, string | null>()
26
36
  const resolve = (id: string): string | null => {
27
37
  if (paths.has(id)) return paths.get(id)!