cfsolve 0.1.2 → 0.1.4

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 (395) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +22 -0
  3. data/README.md +8 -3
  4. data/THIRD_PARTY_LICENSES/README.md +71 -254
  5. data/THIRD_PARTY_LICENSES/{derive_builder_macro-0.20.2/LICENSE-APACHE → bytecount-0.6.9/LICENSE.Apache2} +0 -1
  6. data/THIRD_PARTY_LICENSES/{darling-0.20.11/LICENSE → bytecount-0.6.9/LICENSE.MIT} +1 -3
  7. data/THIRD_PARTY_LICENSES/{ident_case-1.0.1/LICENSE → cobs-0.3.0/LICENSE-MIT} +1 -1
  8. data/THIRD_PARTY_LICENSES/cow-utils-0.1.3/LICENSE +21 -0
  9. data/THIRD_PARTY_LICENSES/{hashbrown-0.14.5 → embedded-io-0.4.0}/LICENSE-MIT +1 -1
  10. data/THIRD_PARTY_LICENSES/{ahash-0.8.12 → embedded-io-0.6.1}/LICENSE-MIT +1 -1
  11. data/THIRD_PARTY_LICENSES/{browserslist-data-0.1.4 → escape-simd-0.1.1}/LICENSE +1 -1
  12. data/THIRD_PARTY_LICENSES/escape-simd-0.1.1/NOTICE +62 -0
  13. data/THIRD_PARTY_LICENSES/json-escape-simd-3.1.3/LICENSE +21 -0
  14. data/THIRD_PARTY_LICENSES/{miette-7.6.0/LICENSE → json-escape-simd-3.1.3/LICENSE-APACHE} +0 -1
  15. data/THIRD_PARTY_LICENSES/json-escape-simd-3.1.3/NOTICE +62 -0
  16. data/THIRD_PARTY_LICENSES/memchr-2.8.3/UNLICENSE +24 -0
  17. data/THIRD_PARTY_LICENSES/{derive_builder_core-0.20.2 → nonmax-0.5.5}/LICENSE-APACHE +1 -2
  18. data/THIRD_PARTY_LICENSES/{dashmap-5.5.3/LICENSE → nonmax-0.5.5/LICENSE-MIT} +2 -4
  19. data/THIRD_PARTY_LICENSES/{rand-0.8.5 → num-bigint-0.5.1}/LICENSE-MIT +0 -1
  20. data/THIRD_PARTY_LICENSES/oxc-0.157.0/LICENSE +22 -0
  21. data/THIRD_PARTY_LICENSES/oxc-0.157.0/THIRD-PARTY-LICENSE +763 -0
  22. data/THIRD_PARTY_LICENSES/{browserslist-rs-0.19.0 → oxc-browserslist-5.0.1}/LICENSE +1 -0
  23. data/THIRD_PARTY_LICENSES/oxc_allocator-0.157.0/LICENSE +22 -0
  24. data/THIRD_PARTY_LICENSES/oxc_allocator-0.157.0/THIRD-PARTY-LICENSE +763 -0
  25. data/THIRD_PARTY_LICENSES/oxc_ast-0.157.0/LICENSE +22 -0
  26. data/THIRD_PARTY_LICENSES/oxc_ast-0.157.0/THIRD-PARTY-LICENSE +763 -0
  27. data/THIRD_PARTY_LICENSES/oxc_ast_macros-0.157.0/LICENSE +22 -0
  28. data/THIRD_PARTY_LICENSES/oxc_ast_macros-0.157.0/THIRD-PARTY-LICENSE +763 -0
  29. data/THIRD_PARTY_LICENSES/oxc_ast_visit-0.157.0/LICENSE +22 -0
  30. data/THIRD_PARTY_LICENSES/oxc_ast_visit-0.157.0/THIRD-PARTY-LICENSE +763 -0
  31. data/THIRD_PARTY_LICENSES/oxc_codegen-0.157.0/LICENSE +22 -0
  32. data/THIRD_PARTY_LICENSES/oxc_codegen-0.157.0/THIRD-PARTY-LICENSE +763 -0
  33. data/THIRD_PARTY_LICENSES/oxc_compat-0.157.0/LICENSE +22 -0
  34. data/THIRD_PARTY_LICENSES/oxc_compat-0.157.0/THIRD-PARTY-LICENSE +763 -0
  35. data/THIRD_PARTY_LICENSES/oxc_data_structures-0.157.0/LICENSE +22 -0
  36. data/THIRD_PARTY_LICENSES/oxc_data_structures-0.157.0/THIRD-PARTY-LICENSE +763 -0
  37. data/THIRD_PARTY_LICENSES/oxc_diagnostics-0.157.0/LICENSE +22 -0
  38. data/THIRD_PARTY_LICENSES/oxc_diagnostics-0.157.0/THIRD-PARTY-LICENSE +763 -0
  39. data/THIRD_PARTY_LICENSES/oxc_ecmascript-0.157.0/LICENSE +22 -0
  40. data/THIRD_PARTY_LICENSES/oxc_ecmascript-0.157.0/THIRD-PARTY-LICENSE +763 -0
  41. data/THIRD_PARTY_LICENSES/oxc_estree-0.157.0/LICENSE +22 -0
  42. data/THIRD_PARTY_LICENSES/oxc_estree-0.157.0/THIRD-PARTY-LICENSE +763 -0
  43. data/THIRD_PARTY_LICENSES/oxc_index-5.0.0/LICENSE +22 -0
  44. data/THIRD_PARTY_LICENSES/oxc_parser-0.157.0/LICENSE +22 -0
  45. data/THIRD_PARTY_LICENSES/oxc_parser-0.157.0/THIRD-PARTY-LICENSE +763 -0
  46. data/THIRD_PARTY_LICENSES/oxc_regular_expression-0.157.0/LICENSE +22 -0
  47. data/THIRD_PARTY_LICENSES/oxc_regular_expression-0.157.0/THIRD-PARTY-LICENSE +763 -0
  48. data/THIRD_PARTY_LICENSES/oxc_semantic-0.157.0/LICENSE +22 -0
  49. data/THIRD_PARTY_LICENSES/oxc_semantic-0.157.0/THIRD-PARTY-LICENSE +763 -0
  50. data/THIRD_PARTY_LICENSES/oxc_span-0.157.0/LICENSE +22 -0
  51. data/THIRD_PARTY_LICENSES/oxc_span-0.157.0/THIRD-PARTY-LICENSE +763 -0
  52. data/THIRD_PARTY_LICENSES/oxc_str-0.157.0/LICENSE +22 -0
  53. data/THIRD_PARTY_LICENSES/oxc_str-0.157.0/THIRD-PARTY-LICENSE +763 -0
  54. data/THIRD_PARTY_LICENSES/oxc_syntax-0.157.0/LICENSE +22 -0
  55. data/THIRD_PARTY_LICENSES/oxc_syntax-0.157.0/THIRD-PARTY-LICENSE +763 -0
  56. data/THIRD_PARTY_LICENSES/{fixedbitset-0.5.7 → postcard-1.1.3}/LICENSE-MIT +1 -1
  57. data/THIRD_PARTY_LICENSES/{bytes-str-0.2.7/LICENSE → self_cell-1.3.0/LICENSE-APACHE} +1 -2
  58. data/THIRD_PARTY_LICENSES/self_cell-1.3.0/LICENSE-GPLv2 +339 -0
  59. data/THIRD_PARTY_LICENSES/{darling_core-0.20.11 → smawk-0.3.3}/LICENSE +1 -1
  60. data/UPSTREAM.md +14 -6
  61. data/ext/cfsolve/Cargo.lock +534 -1727
  62. data/ext/cfsolve/Cargo.toml +4 -0
  63. data/ext/cfsolve/src/main.rs +1 -1
  64. data/lib/cfsolve/profiles/chrome-157-macos/answers.json +4777 -0
  65. data/lib/cfsolve/profiles/chrome-157-macos.json +613 -0
  66. data/lib/cfsolve/profiles/safari-26-ios/answers.json +266 -265
  67. data/lib/cfsolve/profiles/safari-26-ios/challenge-page-answers.json +312 -312
  68. data/lib/cfsolve/profiles/safari-26-ios.json +106 -152
  69. data/lib/cfsolve/version.rb +2 -2
  70. data/lib/cfsolve.rb +1 -1
  71. data/vendor/cfsolve/Cargo.lock +561 -1729
  72. data/vendor/cfsolve/Cargo.toml +26 -8
  73. data/vendor/cfsolve/README.md +57 -5
  74. data/vendor/cfsolve/examples/check_answers.rs +2 -2
  75. data/vendor/cfsolve/examples/compare_session.rs +1 -2
  76. data/vendor/cfsolve/examples/solver_profile.rs +84 -0
  77. data/vendor/cfsolve/examples/solver_smoke.rs +9 -8
  78. data/vendor/cfsolve/examples/stage2_snapshot.rs +2 -2
  79. data/vendor/cfsolve/examples/tls_echo.rs +3 -3
  80. data/vendor/cfsolve/examples/turnstile_smoke.rs +58 -0
  81. data/vendor/cfsolve/examples/vm_snapshot.rs +32 -33
  82. data/vendor/cfsolve/profiles/chrome-157-macos/answers.json +4777 -0
  83. data/vendor/cfsolve/profiles/chrome-157-macos.json +613 -0
  84. data/vendor/cfsolve/profiles/safari-26-ios/answers.json +266 -265
  85. data/vendor/cfsolve/profiles/safari-26-ios/challenge-page-answers.json +312 -312
  86. data/vendor/cfsolve/profiles/safari-26-ios.json +106 -152
  87. data/vendor/cfsolve/src/bin/capture-proxy/main.rs +3 -2
  88. data/vendor/cfsolve/src/bin/challenge/cli.rs +8 -2
  89. data/vendor/cfsolve/src/bin/challenge/main.rs +7 -2
  90. data/vendor/cfsolve/src/capture/proxy/tests.rs +1 -1
  91. data/vendor/cfsolve/src/capture/proxy.rs +44 -13
  92. data/vendor/cfsolve/src/capture/sessions.rs +4 -3
  93. data/vendor/cfsolve/src/capture/upstream.rs +1 -1
  94. data/vendor/cfsolve/src/challenge/codec.rs +4 -2
  95. data/vendor/cfsolve/src/challenge/expression.rs +161 -117
  96. data/vendor/cfsolve/src/challenge/installed/audit.rs +11 -10
  97. data/vendor/cfsolve/src/challenge/installed.rs +13 -6
  98. data/vendor/cfsolve/src/challenge/merge.rs +120 -80
  99. data/vendor/cfsolve/src/challenge/orchestrator.rs +57 -44
  100. data/vendor/cfsolve/src/challenge/payload.rs +69 -31
  101. data/vendor/cfsolve/src/challenge/program.rs +197 -178
  102. data/vendor/cfsolve/src/challenge/reduced_timing.rs +55 -40
  103. data/vendor/cfsolve/src/challenge/source.rs +151 -96
  104. data/vendor/cfsolve/src/challenge/state.rs +180 -148
  105. data/vendor/cfsolve/src/challenge/tally.rs +64 -50
  106. data/vendor/cfsolve/src/challenge.rs +1 -0
  107. data/vendor/cfsolve/src/deobfuscate/analysis/bindings.rs +187 -185
  108. data/vendor/cfsolve/src/deobfuscate/analysis/effects.rs +42 -51
  109. data/vendor/cfsolve/src/deobfuscate/analysis/hoisting.rs +147 -228
  110. data/vendor/cfsolve/src/deobfuscate/analysis/properties.rs +250 -425
  111. data/vendor/cfsolve/src/deobfuscate/analysis/unconditional.rs +79 -0
  112. data/vendor/cfsolve/src/deobfuscate/analysis.rs +259 -41
  113. data/vendor/cfsolve/src/deobfuscate/argument_arrays.rs +70 -73
  114. data/vendor/cfsolve/src/deobfuscate/arrow_blocks.rs +91 -104
  115. data/vendor/cfsolve/src/deobfuscate/ast.rs +383 -0
  116. data/vendor/cfsolve/src/deobfuscate/bitwise.rs +44 -54
  117. data/vendor/cfsolve/src/deobfuscate/blocks.rs +36 -29
  118. data/vendor/cfsolve/src/deobfuscate/call_helpers.rs +80 -131
  119. data/vendor/cfsolve/src/deobfuscate/control_flow.rs +239 -297
  120. data/vendor/cfsolve/src/deobfuscate/copy_propagation.rs +114 -135
  121. data/vendor/cfsolve/src/deobfuscate/dead_branches.rs +62 -61
  122. data/vendor/cfsolve/src/deobfuscate/dead_properties.rs +197 -214
  123. data/vendor/cfsolve/src/deobfuscate/dispatch.rs +238 -298
  124. data/vendor/cfsolve/src/deobfuscate/encoded_strings.rs +198 -203
  125. data/vendor/cfsolve/src/deobfuscate/function_lengths.rs +32 -38
  126. data/vendor/cfsolve/src/deobfuscate/helper_objects.rs +338 -513
  127. data/vendor/cfsolve/src/deobfuscate/hygiene.rs +197 -0
  128. data/vendor/cfsolve/src/deobfuscate/int32_helpers.rs +121 -155
  129. data/vendor/cfsolve/src/deobfuscate/literals.rs +116 -67
  130. data/vendor/cfsolve/src/deobfuscate/loops.rs +43 -43
  131. data/vendor/cfsolve/src/deobfuscate/memoized_strings.rs +127 -131
  132. data/vendor/cfsolve/src/deobfuscate/merged_functions.rs +262 -220
  133. data/vendor/cfsolve/src/deobfuscate/pipeline.rs +199 -244
  134. data/vendor/cfsolve/src/deobfuscate/register_arrays.rs +353 -327
  135. data/vendor/cfsolve/src/deobfuscate/removal.rs +83 -52
  136. data/vendor/cfsolve/src/deobfuscate/simplify.rs +147 -0
  137. data/vendor/cfsolve/src/deobfuscate/state_machines.rs +88 -110
  138. data/vendor/cfsolve/src/deobfuscate/statements.rs +228 -228
  139. data/vendor/cfsolve/src/deobfuscate/static_members.rs +46 -31
  140. data/vendor/cfsolve/src/deobfuscate/testing.rs +49 -59
  141. data/vendor/cfsolve/src/deobfuscate/unused_bindings.rs +92 -102
  142. data/vendor/cfsolve/src/deobfuscate.rs +15 -11
  143. data/vendor/cfsolve/src/embedding_page/extra_params.rs +132 -24
  144. data/vendor/cfsolve/src/embedding_page/page.rs +41 -1
  145. data/vendor/cfsolve/src/embedding_page/render_sample.rs +184 -147
  146. data/vendor/cfsolve/src/error.rs +40 -5
  147. data/vendor/cfsolve/src/fingerprint.rs +8 -2
  148. data/vendor/cfsolve/src/http/client.rs +7 -10
  149. data/vendor/cfsolve/src/http/requests.rs +2 -2
  150. data/vendor/cfsolve/src/js/ast.rs +479 -263
  151. data/vendor/cfsolve/src/lib.rs +1 -0
  152. data/vendor/cfsolve/src/listing/fold.rs +19 -9
  153. data/vendor/cfsolve/src/listing/query.rs +1 -47
  154. data/vendor/cfsolve/src/performance_clock.rs +20 -1
  155. data/vendor/cfsolve/src/profiling.rs +49 -0
  156. data/vendor/cfsolve/src/script.rs +35 -24
  157. data/vendor/cfsolve/src/session/challenge_page.rs +41 -21
  158. data/vendor/cfsolve/src/session/channel.rs +2 -2
  159. data/vendor/cfsolve/src/session/initial.rs +12 -11
  160. data/vendor/cfsolve/src/session/probes.rs +1 -0
  161. data/vendor/cfsolve/src/session/sink.rs +26 -1
  162. data/vendor/cfsolve/src/session/tests/challenge_page.rs +36 -1
  163. data/vendor/cfsolve/src/session.rs +1 -1
  164. data/vendor/cfsolve/src/solver.rs +3 -1
  165. data/vendor/cfsolve/src/stage2/analysis/analyze.rs +33 -4
  166. data/vendor/cfsolve/src/stage2/analysis/audit.rs +4 -2
  167. data/vendor/cfsolve/src/stage2/analysis/discovery.rs +13 -2
  168. data/vendor/cfsolve/src/stage2/analysis/envelope.rs +2 -1
  169. data/vendor/cfsolve/src/stage2/audit_globals.rs +142 -74
  170. data/vendor/cfsolve/src/stage2/benchmark.rs +113 -41
  171. data/vendor/cfsolve/src/stage2/call_stack.rs +1564 -1041
  172. data/vendor/cfsolve/src/stage2/challenge_page.rs +3 -2
  173. data/vendor/cfsolve/src/stage2/fixtures/encoded_benchmark_worker.js +1 -0
  174. data/vendor/cfsolve/src/stage2/interaction/checkbox.rs +202 -150
  175. data/vendor/cfsolve/src/stage2/interaction/click.rs +2 -2
  176. data/vendor/cfsolve/src/stage2/interaction/plan.rs +5 -4
  177. data/vendor/cfsolve/src/stage2/payload/recover.rs +2 -2
  178. data/vendor/cfsolve/src/stage2/probes/clock.rs +1 -3
  179. data/vendor/cfsolve/src/stage2/probes/dates.rs +44 -0
  180. data/vendor/cfsolve/src/stage2/probes/parent_info_array.rs +326 -0
  181. data/vendor/cfsolve/src/stage2/probes/performance.rs +12 -8
  182. data/vendor/cfsolve/src/stage2/probes/stack.rs +117 -4
  183. data/vendor/cfsolve/src/stage2/probes/websocket_echo.rs +80 -2
  184. data/vendor/cfsolve/src/stage2/probes.rs +1 -0
  185. data/vendor/cfsolve/src/stage2/queries.rs +50 -0
  186. data/vendor/cfsolve/src/stage2/reference.rs +5 -0
  187. data/vendor/cfsolve/src/stage2/registry.rs +8 -2
  188. data/vendor/cfsolve/src/stage2/replay.rs +34 -3
  189. data/vendor/cfsolve/src/stage2/stylesheets.rs +35 -0
  190. data/vendor/cfsolve/src/stage2/syntax.rs +88 -0
  191. data/vendor/cfsolve/src/stage2/trace.rs +134 -108
  192. data/vendor/cfsolve/src/stage2/value_labels.rs +185 -0
  193. data/vendor/cfsolve/src/stage2/vocabulary.rs +112 -17
  194. data/vendor/cfsolve/src/stage2.rs +3 -0
  195. data/vendor/cfsolve/src/vm/disassemble.rs +75 -7
  196. data/vendor/cfsolve/src/vm/dispatch.rs +93 -65
  197. data/vendor/cfsolve/src/vm/encoded_names.rs +635 -0
  198. data/vendor/cfsolve/src/vm/extract.rs +550 -250
  199. data/vendor/cfsolve/src/vm/fixtures/free_functions/README.md +9 -0
  200. data/vendor/cfsolve/src/vm/fixtures/free_functions/machine.js +7438 -0
  201. data/vendor/cfsolve/src/vm/fixtures/free_functions/program.base64 +1 -0
  202. data/vendor/cfsolve/src/vm/interpret/access.rs +71 -50
  203. data/vendor/cfsolve/src/vm/interpret/builtin.rs +10 -10
  204. data/vendor/cfsolve/src/vm/interpret/call.rs +116 -52
  205. data/vendor/cfsolve/src/vm/interpret/compile.rs +79 -62
  206. data/vendor/cfsolve/src/vm/interpret/copies.rs +2 -2
  207. data/vendor/cfsolve/src/vm/interpret/evaluate/tests.rs +63 -42
  208. data/vendor/cfsolve/src/vm/interpret/evaluate.rs +65 -575
  209. data/vendor/cfsolve/src/vm/interpret/explore.rs +7 -10
  210. data/vendor/cfsolve/src/vm/interpret/formula.rs +131 -0
  211. data/vendor/cfsolve/src/vm/interpret/frozen.rs +3 -5
  212. data/vendor/cfsolve/src/vm/interpret/intrinsic.rs +160 -58
  213. data/vendor/cfsolve/src/vm/interpret/mix32.rs +189 -0
  214. data/vendor/cfsolve/src/vm/interpret/native_dispatch.rs +220 -0
  215. data/vendor/cfsolve/src/vm/interpret/pattern.rs +1765 -0
  216. data/vendor/cfsolve/src/vm/interpret/plan.rs +586 -338
  217. data/vendor/cfsolve/src/vm/interpret/register_helpers.rs +392 -0
  218. data/vendor/cfsolve/src/vm/interpret/run.rs +120 -45
  219. data/vendor/cfsolve/src/vm/interpret/semantics.rs +71 -46
  220. data/vendor/cfsolve/src/vm/interpret/setup.rs +80 -45
  221. data/vendor/cfsolve/src/vm/interpret/string_decoder.rs +482 -0
  222. data/vendor/cfsolve/src/vm/interpret.rs +9 -1
  223. data/vendor/cfsolve/src/vm/machine.rs +116 -51
  224. data/vendor/cfsolve/src/vm/output.rs +19 -6
  225. data/vendor/cfsolve/src/vm/packed_names.rs +11 -7
  226. data/vendor/cfsolve/src/vm/registers.rs +35 -21
  227. data/vendor/cfsolve/src/vm/session.rs +189 -121
  228. data/vendor/cfsolve/src/vm/value.rs +22 -19
  229. data/vendor/cfsolve/src/vm.rs +3 -1
  230. data/vendor/cfsolve/vendor/btls-sys/Cargo.toml +3 -0
  231. data/vendor/cfsolve/vendor/btls-sys/build/main.rs +20 -0
  232. metadata +106 -178
  233. data/THIRD_PARTY_LICENSES/Inflector-0.11.4/LICENSE.md +0 -22
  234. data/THIRD_PARTY_LICENSES/arrayvec-0.7.6/LICENSE-MIT +0 -25
  235. data/THIRD_PARTY_LICENSES/ascii-1.1.0/LICENSE-APACHE +0 -201
  236. data/THIRD_PARTY_LICENSES/ascii-1.1.0/LICENSE-MIT +0 -22
  237. data/THIRD_PARTY_LICENSES/ast_node-5.0.0/LICENSE +0 -201
  238. data/THIRD_PARTY_LICENSES/better_scoped_tls-1.0.1/LICENSE +0 -201
  239. data/THIRD_PARTY_LICENSES/bitvec-1.0.1/LICENSE.txt +0 -21
  240. data/THIRD_PARTY_LICENSES/bstr-1.12.1/COPYING +0 -8
  241. data/THIRD_PARTY_LICENSES/bstr-1.12.1/LICENSE-MIT +0 -21
  242. data/THIRD_PARTY_LICENSES/cargo-platform-0.1.9/LICENSE-APACHE +0 -201
  243. data/THIRD_PARTY_LICENSES/cargo_metadata-0.19.2/LICENSE-MIT +0 -23
  244. data/THIRD_PARTY_LICENSES/darling_macro-0.20.11/LICENSE +0 -21
  245. data/THIRD_PARTY_LICENSES/debugid-0.8.0/LICENSE +0 -201
  246. data/THIRD_PARTY_LICENSES/derive_builder-0.20.2/LICENSE-MIT +0 -21
  247. data/THIRD_PARTY_LICENSES/derive_builder_core-0.20.2/LICENSE-MIT +0 -21
  248. data/THIRD_PARTY_LICENSES/derive_builder_macro-0.20.2/LICENSE-MIT +0 -21
  249. data/THIRD_PARTY_LICENSES/foldhash-0.1.5/LICENSE +0 -19
  250. data/THIRD_PARTY_LICENSES/from_variant-3.0.0/LICENSE +0 -201
  251. data/THIRD_PARTY_LICENSES/funty-2.0.0/LICENSE.txt +0 -21
  252. data/THIRD_PARTY_LICENSES/hashbrown-0.16.1/LICENSE-MIT +0 -25
  253. data/THIRD_PARTY_LICENSES/heck-0.5.0/LICENSE-APACHE +0 -201
  254. data/THIRD_PARTY_LICENSES/hermit-abi-0.5.2/LICENSE-APACHE +0 -201
  255. data/THIRD_PARTY_LICENSES/hermit-abi-0.5.2/LICENSE-MIT +0 -23
  256. data/THIRD_PARTY_LICENSES/hstr-3.0.3/LICENSE +0 -202
  257. data/THIRD_PARTY_LICENSES/if_chain-1.0.3/LICENSE-APACHE +0 -201
  258. data/THIRD_PARTY_LICENSES/if_chain-1.0.3/LICENSE-MIT +0 -25
  259. data/THIRD_PARTY_LICENSES/is-macro-0.3.7/LICENSE +0 -202
  260. data/THIRD_PARTY_LICENSES/itoa-1.0.17/LICENSE-MIT +0 -23
  261. data/THIRD_PARTY_LICENSES/jsonc-parser-0.26.3/LICENSE +0 -21
  262. data/THIRD_PARTY_LICENSES/lazy_static-1.5.0/LICENSE-APACHE +0 -201
  263. data/THIRD_PARTY_LICENSES/lazy_static-1.5.0/LICENSE-MIT +0 -25
  264. data/THIRD_PARTY_LICENSES/lru-0.16.2/LICENSE +0 -21
  265. data/THIRD_PARTY_LICENSES/memchr-2.7.6/COPYING +0 -3
  266. data/THIRD_PARTY_LICENSES/memchr-2.7.6/LICENSE-MIT +0 -21
  267. data/THIRD_PARTY_LICENSES/miette-derive-7.6.0/LICENSE +0 -229
  268. data/THIRD_PARTY_LICENSES/normpath-0.2.0/COPYRIGHT +0 -7
  269. data/THIRD_PARTY_LICENSES/normpath-0.2.0/LICENSE-MIT +0 -21
  270. data/THIRD_PARTY_LICENSES/normpath-0.2.0/LICENSE-THIRD-PARTY +0 -210
  271. data/THIRD_PARTY_LICENSES/num_cpus-1.17.0/LICENSE-APACHE +0 -201
  272. data/THIRD_PARTY_LICENSES/num_cpus-1.17.0/LICENSE-MIT +0 -20
  273. data/THIRD_PARTY_LICENSES/owo-colors-4.2.3/LICENSE +0 -21
  274. data/THIRD_PARTY_LICENSES/par-core-2.0.0/LICENSE +0 -202
  275. data/THIRD_PARTY_LICENSES/par-iter-2.0.0/LICENSE +0 -202
  276. data/THIRD_PARTY_LICENSES/path-clean-0.1.0/LICENSE-APACHE +0 -13
  277. data/THIRD_PARTY_LICENSES/path-clean-0.1.0/LICENSE-MIT +0 -18
  278. data/THIRD_PARTY_LICENSES/path-clean-1.0.1/LICENSE-APACHE +0 -13
  279. data/THIRD_PARTY_LICENSES/path-clean-1.0.1/LICENSE-MIT +0 -18
  280. data/THIRD_PARTY_LICENSES/pathdiff-0.2.3/LICENSE-APACHE +0 -201
  281. data/THIRD_PARTY_LICENSES/pathdiff-0.2.3/LICENSE-MIT +0 -23
  282. data/THIRD_PARTY_LICENSES/petgraph-0.7.1/LICENSE-APACHE +0 -201
  283. data/THIRD_PARTY_LICENSES/precomputed-map-0.2.0/LICENSE +0 -21
  284. data/THIRD_PARTY_LICENSES/preset_env_base-6.0.0/LICENSE +0 -201
  285. data/THIRD_PARTY_LICENSES/radium-0.7.0/LICENSE.txt +0 -21
  286. data/THIRD_PARTY_LICENSES/radix_fmt-1.0.0/LICENSE +0 -201
  287. data/THIRD_PARTY_LICENSES/rand-0.8.5/COPYRIGHT +0 -12
  288. data/THIRD_PARTY_LICENSES/rand-0.8.5/LICENSE-APACHE +0 -176
  289. data/THIRD_PARTY_LICENSES/rand_core-0.6.4/COPYRIGHT +0 -12
  290. data/THIRD_PARTY_LICENSES/rand_core-0.6.4/LICENSE-APACHE +0 -187
  291. data/THIRD_PARTY_LICENSES/rand_core-0.6.4/LICENSE-MIT +0 -26
  292. data/THIRD_PARTY_LICENSES/regress-0.10.5/LICENSE-APACHE +0 -201
  293. data/THIRD_PARTY_LICENSES/regress-0.10.5/LICENSE-MIT +0 -21
  294. data/THIRD_PARTY_LICENSES/ryu-1.0.22/LICENSE-APACHE +0 -176
  295. data/THIRD_PARTY_LICENSES/ryu-1.0.22/LICENSE-BOOST +0 -23
  296. data/THIRD_PARTY_LICENSES/scoped-tls-1.0.1/LICENSE-APACHE +0 -201
  297. data/THIRD_PARTY_LICENSES/scoped-tls-1.0.1/LICENSE-MIT +0 -25
  298. data/THIRD_PARTY_LICENSES/siphasher-0.3.11/COPYING +0 -7
  299. data/THIRD_PARTY_LICENSES/smallvec-1.15.1/LICENSE-APACHE +0 -201
  300. data/THIRD_PARTY_LICENSES/smartstring-1.0.1/LICENCE.md +0 -355
  301. data/THIRD_PARTY_LICENSES/st-map-0.2.4/LICENSE +0 -202
  302. data/THIRD_PARTY_LICENSES/static-map-macro-0.3.4/LICENSE +0 -202
  303. data/THIRD_PARTY_LICENSES/string_enum-1.0.2/LICENSE +0 -201
  304. data/THIRD_PARTY_LICENSES/strsim-0.11.1/LICENSE +0 -23
  305. data/THIRD_PARTY_LICENSES/swc-52.0.0/LICENSE +0 -201
  306. data/THIRD_PARTY_LICENSES/swc_allocator-4.0.1/LICENSE +0 -201
  307. data/THIRD_PARTY_LICENSES/swc_atoms-9.0.0/LICENSE +0 -201
  308. data/THIRD_PARTY_LICENSES/swc_common-18.0.1/LICENSE +0 -201
  309. data/THIRD_PARTY_LICENSES/swc_compiler_base-45.0.0/LICENSE +0 -201
  310. data/THIRD_PARTY_LICENSES/swc_config-3.1.2/LICENSE +0 -201
  311. data/THIRD_PARTY_LICENSES/swc_config_macro-1.0.1/LICENSE +0 -201
  312. data/THIRD_PARTY_LICENSES/swc_core-54.0.0/LICENSE +0 -201
  313. data/THIRD_PARTY_LICENSES/swc_ecma_ast-19.0.0/LICENSE +0 -201
  314. data/THIRD_PARTY_LICENSES/swc_ecma_codegen-21.0.0/LICENSE +0 -201
  315. data/THIRD_PARTY_LICENSES/swc_ecma_codegen_macros-2.0.2/LICENSE +0 -201
  316. data/THIRD_PARTY_LICENSES/swc_ecma_compat_bugfixes-39.0.0/LICENSE +0 -201
  317. data/THIRD_PARTY_LICENSES/swc_ecma_compat_common-30.0.0/LICENSE +0 -201
  318. data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2015-39.0.0/LICENSE +0 -201
  319. data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2016-35.0.0/LICENSE +0 -201
  320. data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2017-35.0.0/LICENSE +0 -201
  321. data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2018-36.0.0/LICENSE +0 -201
  322. data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2019-35.0.0/LICENSE +0 -201
  323. data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2020-37.0.0/LICENSE +0 -201
  324. data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2021-35.0.0/LICENSE +0 -201
  325. data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2022-37.0.0/LICENSE +0 -201
  326. data/THIRD_PARTY_LICENSES/swc_ecma_compat_es3-26.0.0/LICENSE +0 -201
  327. data/THIRD_PARTY_LICENSES/swc_ecma_ext_transforms-25.0.0/LICENSE +0 -201
  328. data/THIRD_PARTY_LICENSES/swc_ecma_hooks-0.3.0/LICENSE +0 -201
  329. data/THIRD_PARTY_LICENSES/swc_ecma_loader-18.0.0/LICENSE +0 -201
  330. data/THIRD_PARTY_LICENSES/swc_ecma_minifier-42.0.0/LICENSE +0 -201
  331. data/THIRD_PARTY_LICENSES/swc_ecma_parser-32.0.0/LICENSE +0 -201
  332. data/THIRD_PARTY_LICENSES/swc_ecma_preset_env-45.0.0/LICENSE +0 -201
  333. data/THIRD_PARTY_LICENSES/swc_ecma_transformer-6.0.0/LICENSE +0 -201
  334. data/THIRD_PARTY_LICENSES/swc_ecma_transforms-44.0.0/LICENSE +0 -201
  335. data/THIRD_PARTY_LICENSES/swc_ecma_transforms_base-35.0.0/LICENSE +0 -201
  336. data/THIRD_PARTY_LICENSES/swc_ecma_transforms_classes-35.0.0/LICENSE +0 -201
  337. data/THIRD_PARTY_LICENSES/swc_ecma_transforms_compat-40.0.0/LICENSE +0 -201
  338. data/THIRD_PARTY_LICENSES/swc_ecma_transforms_macros-1.0.1/LICENSE +0 -201
  339. data/THIRD_PARTY_LICENSES/swc_ecma_transforms_module-38.0.0/LICENSE +0 -201
  340. data/THIRD_PARTY_LICENSES/swc_ecma_transforms_optimization-37.0.0/LICENSE +0 -201
  341. data/THIRD_PARTY_LICENSES/swc_ecma_transforms_proposal-35.0.0/LICENSE +0 -201
  342. data/THIRD_PARTY_LICENSES/swc_ecma_transforms_react-38.0.0/LICENSE +0 -201
  343. data/THIRD_PARTY_LICENSES/swc_ecma_transforms_typescript-38.0.0/LICENSE +0 -201
  344. data/THIRD_PARTY_LICENSES/swc_ecma_usage_analyzer-27.0.0/LICENSE +0 -201
  345. data/THIRD_PARTY_LICENSES/swc_ecma_utils-25.0.0/LICENSE +0 -201
  346. data/THIRD_PARTY_LICENSES/swc_ecma_visit-19.0.0/LICENSE +0 -201
  347. data/THIRD_PARTY_LICENSES/swc_eq_ignore_macros-1.0.1/LICENSE +0 -201
  348. data/THIRD_PARTY_LICENSES/swc_error_reporters-20.0.0/LICENSE +0 -201
  349. data/THIRD_PARTY_LICENSES/swc_macros_common-1.0.1/LICENSE +0 -201
  350. data/THIRD_PARTY_LICENSES/swc_node_comments-18.0.0/LICENSE +0 -201
  351. data/THIRD_PARTY_LICENSES/swc_timer-1.0.0/LICENSE +0 -201
  352. data/THIRD_PARTY_LICENSES/swc_trace_macro-2.0.2/LICENSE +0 -201
  353. data/THIRD_PARTY_LICENSES/swc_transform_common-12.0.0/LICENSE +0 -201
  354. data/THIRD_PARTY_LICENSES/swc_visit-2.0.1/LICENSE +0 -201
  355. data/THIRD_PARTY_LICENSES/tap-1.0.1/LICENSE.txt +0 -21
  356. data/THIRD_PARTY_LICENSES/tracing-0.1.44/LICENSE +0 -25
  357. data/THIRD_PARTY_LICENSES/tracing-attributes-0.1.31/LICENSE +0 -25
  358. data/THIRD_PARTY_LICENSES/tracing-core-0.1.36/LICENSE +0 -25
  359. data/THIRD_PARTY_LICENSES/triomphe-0.1.15/LICENSE-APACHE +0 -201
  360. data/THIRD_PARTY_LICENSES/triomphe-0.1.15/LICENSE-MIT +0 -27
  361. data/THIRD_PARTY_LICENSES/unicode-width-0.1.14/LICENSE-APACHE +0 -201
  362. data/THIRD_PARTY_LICENSES/unicode-width-0.1.14/LICENSE-MIT +0 -25
  363. data/THIRD_PARTY_LICENSES/uuid-1.19.0/LICENSE-APACHE +0 -201
  364. data/THIRD_PARTY_LICENSES/uuid-1.19.0/LICENSE-MIT +0 -26
  365. data/THIRD_PARTY_LICENSES/vergen-9.0.6/LICENSE-APACHE +0 -201
  366. data/THIRD_PARTY_LICENSES/vergen-9.0.6/LICENSE-MIT +0 -25
  367. data/THIRD_PARTY_LICENSES/vergen-lib-0.1.6/LICENSE-APACHE +0 -201
  368. data/THIRD_PARTY_LICENSES/vergen-lib-0.1.6/LICENSE-MIT +0 -25
  369. data/THIRD_PARTY_LICENSES/wyz-0.5.1/LICENSE.txt +0 -21
  370. data/vendor/cfsolve/src/deobfuscate/analysis/globals.rs +0 -33
  371. data/vendor/cfsolve/src/deobfuscate/analysis/names.rs +0 -52
  372. /data/THIRD_PARTY_LICENSES/{derive_builder-0.20.2 → cobs-0.3.0}/LICENSE-APACHE +0 -0
  373. /data/THIRD_PARTY_LICENSES/{compact_str-0.7.1 → compact_str-0.10.0}/LICENSE +0 -0
  374. /data/THIRD_PARTY_LICENSES/{ahash-0.8.12 → embedded-io-0.4.0}/LICENSE-APACHE +0 -0
  375. /data/THIRD_PARTY_LICENSES/{arrayvec-0.7.6 → embedded-io-0.6.1}/LICENSE-APACHE +0 -0
  376. /data/THIRD_PARTY_LICENSES/{normpath-0.2.0 → escape-simd-0.1.1}/LICENSE-APACHE +0 -0
  377. /data/THIRD_PARTY_LICENSES/{bstr-1.12.1 → itertools-0.15.0}/LICENSE-APACHE +0 -0
  378. /data/THIRD_PARTY_LICENSES/{petgraph-0.7.1 → itertools-0.15.0}/LICENSE-MIT +0 -0
  379. /data/THIRD_PARTY_LICENSES/{itoa-1.0.17 → itoa-1.0.19}/LICENSE-APACHE +0 -0
  380. /data/THIRD_PARTY_LICENSES/{camino-1.2.2 → itoa-1.0.19}/LICENSE-MIT +0 -0
  381. /data/THIRD_PARTY_LICENSES/{globset-0.4.18 → memchr-2.8.3}/COPYING +0 -0
  382. /data/THIRD_PARTY_LICENSES/{globset-0.4.18 → memchr-2.8.3}/LICENSE-MIT +0 -0
  383. /data/THIRD_PARTY_LICENSES/{camino-1.2.2 → num-bigint-0.5.1}/LICENSE-APACHE +0 -0
  384. /data/THIRD_PARTY_LICENSES/{swc_sourcemap-9.3.4 → oxc_sourcemap-9.0.0}/LICENSE +0 -0
  385. /data/THIRD_PARTY_LICENSES/{phf-0.11.3 → phf-0.14.0}/LICENSE +0 -0
  386. /data/THIRD_PARTY_LICENSES/{phf_generator-0.11.3 → phf_generator-0.14.0}/LICENSE +0 -0
  387. /data/THIRD_PARTY_LICENSES/{phf_macros-0.11.3 → phf_macros-0.14.0}/LICENSE +0 -0
  388. /data/THIRD_PARTY_LICENSES/{phf_shared-0.11.3 → phf_shared-0.14.0}/LICENSE +0 -0
  389. /data/THIRD_PARTY_LICENSES/{fixedbitset-0.5.7 → postcard-1.1.3}/LICENSE-APACHE +0 -0
  390. /data/THIRD_PARTY_LICENSES/{hashbrown-0.14.5 → smallvec-1.16.5}/LICENSE-APACHE +0 -0
  391. /data/THIRD_PARTY_LICENSES/{smallvec-1.15.1 → smallvec-1.16.5}/LICENSE-MIT +0 -0
  392. /data/THIRD_PARTY_LICENSES/{unicode-width-0.1.14 → unicode-segmentation-1.13.3}/COPYRIGHT +0 -0
  393. /data/THIRD_PARTY_LICENSES/{hashbrown-0.16.1 → unicode-segmentation-1.13.3}/LICENSE-APACHE +0 -0
  394. /data/THIRD_PARTY_LICENSES/{heck-0.5.0 → unicode-segmentation-1.13.3}/LICENSE-MIT +0 -0
  395. /data/THIRD_PARTY_LICENSES/{cargo-platform-0.1.9 → zmij_ecma-1.0.23}/LICENSE-MIT +0 -0
@@ -2,99 +2,169 @@
2
2
  //! helper functions, state fields, and program runner. [`super::session`] evaluates what this
3
3
  //! module finds.
4
4
 
5
+ use std::ptr;
6
+
5
7
  use anyhow::{Context, Result, bail, ensure};
6
- use swc_core::atoms::Atom;
7
- use swc_core::common::Span;
8
- use swc_core::ecma::ast::{
9
- AssignExpr, AssignOp, AssignTarget, BlockStmt, CallExpr, Class, ClassMember, Decl, Expr,
10
- ExprOrSpread, Function, Lit, ReturnStmt, Script, SimpleAssignTarget, Stmt, SwitchStmt,
8
+ use oxc::ast::AstKind;
9
+ use oxc::ast::ast::{
10
+ Argument as CallArgument, AssignmentExpression, AssignmentTarget, BindingIdentifier,
11
+ BindingPattern, CallExpression, Class, ClassElement, Expression, FormalParameters, Function,
12
+ FunctionBody, MemberExpression, MethodDefinitionKind, Statement, SwitchStatement,
11
13
  };
12
- use swc_core::ecma::visit::{Visit, VisitWith};
14
+ use oxc::ast_visit::{Visit, walk};
15
+ use oxc::semantic::{ScopeFlags, SymbolId};
16
+ use oxc::span::GetSpan;
17
+ use oxc::syntax::operator::AssignmentOperator;
13
18
 
14
19
  use super::output::Position;
15
20
  use super::{HashMap, convert};
16
- use crate::js::ast;
17
-
18
- /// Finds a class with a `run` method and the innermost function body declaring it.
19
- #[derive(Default)]
20
- pub(super) struct Finder {
21
- pub found: Option<(BlockStmt, Class)>,
21
+ use crate::candidates;
22
+ use crate::js::ast::{self, Binding, Script};
23
+
24
+ pub(super) struct MachineScope<'a> {
25
+ pub body: &'a FunctionBody<'a>,
26
+ pub class: &'a Class<'a>,
27
+ pub run: &'a Function<'a>,
28
+ /// Newer scripts pass the machine as the first argument of a free function.
29
+ pub machine: Option<SymbolId>,
22
30
  }
23
31
 
24
- impl Visit for Finder {
25
- fn visit_function(&mut self, function: &Function) {
26
- if let Some(body) = &function.body
27
- && let Some(class) = machine_class(body)
28
- {
29
- self.found = Some((body.clone(), class.clone()));
30
- }
31
- function.visit_children_with(self);
32
- }
32
+ /// Finds the installed VM runner and its class, either a `run` method or a free function,
33
+ /// in the innermost declaring function body.
34
+ pub(super) fn find<'a>(script: Script<'a>) -> Option<MachineScope<'a>> {
35
+ script
36
+ .functions()
37
+ .filter_map(|function| {
38
+ let body = function.body.as_deref()?;
39
+ let runner = body.statements.iter().find_map(installed_runner)?;
40
+ let Expression::Identifier(class_name) = runner.class else {
41
+ return None;
42
+ };
43
+ let class = body
44
+ .statements
45
+ .iter()
46
+ .find_map(|statement| match statement {
47
+ Statement::ClassDeclaration(class)
48
+ if class.id.as_ref().map(|id| id.symbol_id())
49
+ == script.symbol(class_name) =>
50
+ {
51
+ Some(&**class)
52
+ }
53
+ _ => None,
54
+ })?;
55
+ let (run, machine) = match runner.function {
56
+ Some(Expression::Identifier(name)) => {
57
+ let run = *functions(&body.statements).get(&script.symbol(name)?)?;
58
+ (run, Some(parameter(&run.params, 0)?.symbol_id()))
59
+ }
60
+ None => (method(class, "run")?, None),
61
+ _ => return None,
62
+ };
63
+ Some(MachineScope {
64
+ body,
65
+ class,
66
+ run,
67
+ machine,
68
+ })
69
+ })
70
+ .last()
33
71
  }
34
72
 
35
73
  /// Collects the statements that run before the VM's scope is entered, both at script level and
36
- /// in each enclosing function.
37
- pub(super) fn prelude(script: &Script, body: &BlockStmt) -> Vec<Stmt> {
38
- use swc_core::common::Spanned;
39
-
40
- /// The outermost function body inside a statement that encloses `target`.
41
- struct Enclosing {
42
- target: Span,
43
- body: Option<BlockStmt>,
44
- }
45
-
46
- impl Visit for Enclosing {
47
- fn visit_function(&mut self, function: &Function) {
48
- match &function.body {
49
- Some(body) if body.span.contains(self.target) => self.body = Some(body.clone()),
50
- _ => function.visit_children_with(self),
51
- }
52
- }
53
- }
54
-
74
+ /// in each enclosing function: at each level, the statements before the one containing `body`.
75
+ pub(super) fn prelude<'a>(
76
+ script: Script<'a>,
77
+ body: &'a FunctionBody<'a>,
78
+ ) -> Vec<&'a Statement<'a>> {
79
+ let mut enclosing: Vec<&'a [Statement<'a>]> = script
80
+ .nodes
81
+ .ancestor_kinds(body.node_id.get())
82
+ .filter_map(|kind| match kind {
83
+ AstKind::Function(function) => function.body.as_deref(),
84
+ _ => None,
85
+ })
86
+ .filter(|enclosing| !ptr::eq(*enclosing, body))
87
+ .map(|enclosing| &enclosing.statements[..])
88
+ .collect();
89
+ enclosing.push(&script.program.body);
55
90
  let mut prelude = Vec::new();
56
- let mut statements = script.body.clone();
57
- while let Some(index) = statements
58
- .iter()
59
- .position(|statement| statement.span().contains(body.span))
60
- {
61
- let mut enclosing = Enclosing {
62
- target: body.span,
63
- body: None,
91
+ for statements in enclosing.into_iter().rev() {
92
+ let Some(index) = statements
93
+ .iter()
94
+ .position(|statement| statement.span().contains_inclusive(body.span))
95
+ else {
96
+ break;
64
97
  };
65
- statements[index].visit_with(&mut enclosing);
66
- statements.truncate(index);
67
- prelude.append(&mut statements);
68
- match enclosing.body {
69
- Some(enclosing) if enclosing.span != body.span => statements = enclosing.stmts,
70
- _ => break,
71
- }
98
+ prelude.extend(&statements[..index]);
72
99
  }
73
100
  prelude
74
101
  }
75
102
 
76
103
  /// Functions declared beside the class: handlers and their helpers.
77
- pub(super) fn functions(statements: &[Stmt]) -> HashMap<Atom, &Function> {
104
+ pub(super) fn functions<'a>(
105
+ statements: &'a [Statement<'a>],
106
+ ) -> HashMap<SymbolId, &'a Function<'a>> {
78
107
  statements
79
108
  .iter()
80
- .filter_map(|statement| {
81
- let function = statement.as_decl()?.as_fn_decl()?;
82
- Some((function.ident.sym.clone(), &*function.function))
109
+ .filter_map(|statement| match statement {
110
+ Statement::FunctionDeclaration(function) => {
111
+ Some((function.id.as_ref()?.symbol_id(), &**function))
112
+ }
113
+ _ => None,
83
114
  })
84
115
  .collect()
85
116
  }
86
117
 
87
- pub(super) fn constructor(class: &Class) -> Result<&BlockStmt> {
118
+ /// Every parameter, if each is a plain identifier: no pattern, default, or rest.
119
+ pub(super) fn parameters<'a>(
120
+ parameters: &'a FormalParameters<'a>,
121
+ ) -> Option<Vec<&'a BindingIdentifier<'a>>> {
122
+ if parameters.rest.is_some() {
123
+ return None;
124
+ }
125
+ (0..parameters.items.len())
126
+ .map(|index| parameter(parameters, index))
127
+ .collect()
128
+ }
129
+
130
+ /// The parameter at `index`, if it is a plain identifier.
131
+ pub(super) fn parameter<'a>(
132
+ parameters: &'a FormalParameters<'a>,
133
+ index: usize,
134
+ ) -> Option<&'a BindingIdentifier<'a>> {
135
+ let parameter = parameters.items.get(index)?;
136
+ match &parameter.pattern {
137
+ BindingPattern::BindingIdentifier(name) if parameter.initializer.is_none() => Some(name),
138
+ _ => None,
139
+ }
140
+ }
141
+
142
+ /// The number of parameters, including a rest parameter.
143
+ pub(super) fn parameter_count(parameters: &FormalParameters<'_>) -> usize {
144
+ parameters.items.len() + usize::from(parameters.rest.is_some())
145
+ }
146
+
147
+ /// The constructor's statements.
148
+ pub(super) fn constructor<'a>(class: &'a Class<'a>) -> Result<&'a [Statement<'a>]> {
88
149
  class
150
+ .body
89
151
  .body
90
152
  .iter()
91
- .find_map(|member| member.as_constructor()?.body.as_ref())
153
+ .find_map(|element| match element {
154
+ ClassElement::MethodDefinition(method)
155
+ if method.kind == MethodDefinitionKind::Constructor =>
156
+ {
157
+ Some(&method.value.body.as_deref()?.statements[..])
158
+ }
159
+ _ => None,
160
+ })
92
161
  .context("the VM class has no constructor")
93
162
  }
94
163
 
95
164
  /// What a `run` local holds.
96
165
  #[derive(Clone)]
97
166
  enum Source {
167
+ Machine,
98
168
  Parameter(usize),
99
169
  /// `this.<name>`.
100
170
  Field(String),
@@ -108,6 +178,7 @@ enum Source {
108
178
 
109
179
  /// Register layout and state setup recovered from `run`.
110
180
  pub(super) struct RunMethod<'ast> {
181
+ pub access: super::registers::Access<'ast>,
111
182
  /// Field containing the register file. Fields on that file identify the program counter and
112
183
  /// dispatch key registers.
113
184
  pub registers: String,
@@ -120,7 +191,7 @@ pub(super) struct RunMethod<'ast> {
120
191
  /// Dispatch function declared beside the class and called by `run` with the machine. Some builds
121
192
  /// move both loops out of `run`; both dispatch the same handlers.
122
193
  pub(crate) struct DispatchLoop<'ast> {
123
- pub function: &'ast Function,
194
+ pub function: &'ast Function<'ast>,
124
195
  /// Argument values passed by `run`, in parameter order.
125
196
  pub arguments: Vec<Argument>,
126
197
  }
@@ -136,65 +207,96 @@ pub(crate) enum Argument {
136
207
 
137
208
  impl DispatchLoop<'_> {
138
209
  /// The parameter receiving the machine.
139
- fn machine(&self) -> Option<&Atom> {
210
+ fn machine(&self) -> Option<SymbolId> {
140
211
  let position = self
141
212
  .arguments
142
213
  .iter()
143
214
  .position(|argument| *argument == Argument::Machine)?;
144
- Some(&self.function.params.get(position)?.pat.as_ident()?.sym)
215
+ Some(parameter(&self.function.params, position)?.symbol_id())
145
216
  }
146
217
  }
147
218
 
219
+ /// The locals of `run` and what they hold.
220
+ struct Locals<'a> {
221
+ script: Script<'a>,
222
+ sources: HashMap<Binding<'a>, Source>,
223
+ free: bool,
224
+ }
225
+
148
226
  /// Tracks assignments in `run`, including copies of register fields and indices, program counter
149
227
  /// and key setup, storing the third argument in the cells register, reading the program register,
150
228
  /// and calling dispatch loops. An unsupported assignment clears the known value of its local.
151
229
  pub(super) fn run_method<'a>(
152
- functions: &HashMap<Atom, &'a Function>,
153
- run: &Function,
230
+ script: Script<'a>,
231
+ functions: &HashMap<SymbolId, &'a Function<'a>>,
232
+ run: &'a Function<'a>,
233
+ machine: Option<SymbolId>,
154
234
  ) -> Result<RunMethod<'a>> {
155
- let body = run.body.as_ref().context("the run method has no body")?;
156
- let mut locals: HashMap<Atom, Source> = run
157
- .params
158
- .iter()
159
- .enumerate()
160
- .filter_map(|(index, parameter)| {
161
- Some((
162
- parameter.pat.as_ident()?.sym.clone(),
163
- Source::Parameter(index),
164
- ))
235
+ let body = run.body.as_deref().context("the run method has no body")?;
236
+ let sources = (0..run.params.items.len())
237
+ .filter_map(|index| {
238
+ let name = parameter(&run.params, index)?;
239
+ let source = if machine == Some(name.symbol_id()) {
240
+ Source::Machine
241
+ } else {
242
+ Source::Parameter(index - usize::from(machine.is_some()))
243
+ };
244
+ Some((Binding::Symbol(name.symbol_id()), source))
165
245
  })
166
246
  .collect();
247
+ let mut locals = Locals {
248
+ script,
249
+ sources,
250
+ free: machine.is_some(),
251
+ };
167
252
  let mut cells = None;
168
253
  let mut loaded = Vec::new();
169
- for statement in &body.stmts {
170
- let Stmt::Expr(statement) = statement else {
254
+ let mut reader = None;
255
+ let mut writer = None;
256
+ let function = |expression: &Expression<'a>| {
257
+ let Expression::CallExpression(call) = expression else {
258
+ return None;
259
+ };
260
+ let Expression::Identifier(callee) = &call.callee else {
261
+ return None;
262
+ };
263
+ functions.get(&script.symbol(callee)?).copied()
264
+ };
265
+ for statement in &body.statements {
266
+ let Statement::ExpressionStatement(statement) = statement else {
171
267
  continue;
172
268
  };
173
- let expressions: Vec<&Expr> = match &*statement.expr {
174
- Expr::Seq(sequence) => sequence.exprs.iter().map(|expr| &**expr).collect(),
175
- expr => vec![expr],
269
+ let expressions: Vec<&Expression<'a>> = match &statement.expression {
270
+ Expression::SequenceExpression(sequence) => sequence.expressions.iter().collect(),
271
+ expression => vec![expression],
176
272
  };
177
- for expr in expressions {
178
- if let Some((Source::Field(registers), arguments)) = method_call(&locals, expr, "write")
273
+ for expression in expressions {
274
+ if let Some((Source::Field(registers), arguments)) =
275
+ locals.method_call(expression, "write")
179
276
  && let [Some(Source::Field(index)), Some(Source::Parameter(2))] = &arguments[..]
180
277
  {
181
278
  cells = Some((registers, index.clone()));
279
+ writer = function(expression);
182
280
  continue;
183
281
  }
184
- let Expr::Assign(assign) = expr else { continue };
185
- let AssignTarget::Simple(SimpleAssignTarget::Ident(name)) = &assign.left else {
282
+ let Expression::AssignmentExpression(assignment) = expression else {
186
283
  continue;
187
284
  };
188
- let value = match assign.op {
189
- AssignOp::Assign => source(&locals, &assign.right),
285
+ let AssignmentTarget::AssignmentTargetIdentifier(name) = &assignment.left else {
286
+ continue;
287
+ };
288
+ let value = match assignment.operator {
289
+ AssignmentOperator::Assign => locals.source(&assignment.right),
190
290
  _ => None,
191
291
  };
192
292
  if let Some(Source::Register { registers, index }) = &value {
193
293
  loaded.push((registers.clone(), index.clone()));
294
+ reader = function(&assignment.right);
194
295
  }
296
+ let binding = script.binding(name);
195
297
  match value {
196
- Some(value) => locals.insert(name.sym.clone(), value),
197
- None => locals.remove(&name.sym),
298
+ Some(value) => locals.sources.insert(binding, value),
299
+ None => locals.sources.remove(&binding),
198
300
  };
199
301
  }
200
302
  }
@@ -211,19 +313,22 @@ pub(super) fn run_method<'a>(
211
313
  };
212
314
  let program = program.clone();
213
315
  let mut calls = DispatchCall {
316
+ script,
214
317
  functions,
318
+ machine,
215
319
  found: None,
216
320
  };
217
- body.visit_with(&mut calls);
321
+ calls.visit_function_body(body);
218
322
  let (function, call) = calls
219
323
  .found
220
324
  .context("the run method calls no dispatch loop with the machine")?;
221
325
  let arguments = call
222
- .args
326
+ .arguments
223
327
  .iter()
224
- .map(|argument| match &*argument.expr {
225
- Expr::This(_) => Argument::Machine,
226
- Expr::Ident(name) => match locals.get(&name.sym) {
328
+ .map(|argument| match ast::argument_expression(argument) {
329
+ Expression::ThisExpression(_) => Argument::Machine,
330
+ Expression::Identifier(name) => match locals.sources.get(&script.binding(name)) {
331
+ Some(Source::Machine) => Argument::Machine,
227
332
  Some(Source::Field(field)) if *field == registers => Argument::Registers,
228
333
  Some(Source::Register { index, .. }) if *index == program => Argument::Program,
229
334
  _ => Argument::Other,
@@ -232,6 +337,14 @@ pub(super) fn run_method<'a>(
232
337
  })
233
338
  .collect();
234
339
  Ok(RunMethod {
340
+ access: if machine.is_some() {
341
+ super::registers::Access::Functions {
342
+ read: reader.context("the free VM runner has no register reader")?,
343
+ write: writer.context("the free VM runner has no register writer")?,
344
+ }
345
+ } else {
346
+ super::registers::Access::Methods
347
+ },
235
348
  registers,
236
349
  cells,
237
350
  program,
@@ -244,122 +357,182 @@ pub(super) fn run_method<'a>(
244
357
 
245
358
  /// Finds the first call in `run` whose first argument is the machine and whose target contains a
246
359
  /// dispatch switch.
247
- struct DispatchCall<'functions, 'ast> {
248
- functions: &'functions HashMap<Atom, &'ast Function>,
249
- found: Option<(&'ast Function, CallExpr)>,
360
+ struct DispatchCall<'functions, 'a> {
361
+ script: Script<'a>,
362
+ functions: &'functions HashMap<SymbolId, &'a Function<'a>>,
363
+ machine: Option<SymbolId>,
364
+ found: Option<(&'a Function<'a>, &'a CallExpression<'a>)>,
250
365
  }
251
366
 
252
- impl Visit for DispatchCall<'_, '_> {
253
- fn visit_call_expr(&mut self, call: &CallExpr) {
367
+ impl<'a> Visit<'a> for DispatchCall<'_, 'a> {
368
+ fn visit_call_expression(&mut self, call: &CallExpression<'a>) {
254
369
  if self.found.is_none()
255
- && call
256
- .args
257
- .first()
258
- .is_some_and(|argument| argument.expr.is_this())
259
- && let Some(callee) = call.callee.as_expr().and_then(|callee| callee.as_ident())
260
- && let Some(&function) = self.functions.get(&callee.sym)
370
+ && call.arguments.first().is_some_and(|first| {
371
+ let first = ast::argument_expression(first);
372
+ matches!(first, Expression::ThisExpression(_))
373
+ || self
374
+ .machine
375
+ .is_some_and(|machine| self.script.refers_to(first, machine))
376
+ })
377
+ && let Expression::Identifier(callee) = &call.callee
378
+ && let Some(symbol) = self.script.symbol(callee)
379
+ && let Some(&function) = self.functions.get(&symbol)
261
380
  && first_switch_in(function).is_some()
262
381
  {
263
- self.found = Some((function, call.clone()));
382
+ self.found = Some((
383
+ function,
384
+ self.script.lend(call, AstKind::as_call_expression),
385
+ ));
264
386
  }
265
- call.visit_children_with(self);
387
+ walk::walk_call_expression(self, call);
266
388
  }
267
389
  }
268
390
 
269
- /// Resolves `<object>.<name>(<arguments>)`, including the values of the object and arguments.
270
- fn method_call(
271
- locals: &HashMap<Atom, Source>,
272
- expr: &Expr,
273
- name: &str,
274
- ) -> Option<(Source, Vec<Option<Source>>)> {
275
- let (object, arguments) = ast::method_call(expr, name)?;
276
- let arguments = arguments
277
- .iter()
278
- .map(|argument| source(locals, &argument.expr))
279
- .collect();
280
- Some((source(locals, object)?, arguments))
281
- }
391
+ impl<'a> Locals<'a> {
392
+ /// Resolves `<object>.<name>(<arguments>)`, including the values of the object and
393
+ /// arguments.
394
+ fn method_call(
395
+ &self,
396
+ expression: &Expression<'a>,
397
+ name: &str,
398
+ ) -> Option<(Source, Vec<Option<Source>>)> {
399
+ let (object, arguments) = if let Some(call) = ast::method_call(expression, name) {
400
+ call
401
+ } else if self.free
402
+ && let Expression::CallExpression(call) = expression
403
+ {
404
+ let [object, arguments @ ..] = &call.arguments[..] else {
405
+ return None;
406
+ };
407
+ if arguments.len() != if name == "read" { 1 } else { 2 } {
408
+ return None;
409
+ }
410
+ (ast::argument_expression(object), arguments)
411
+ } else {
412
+ return None;
413
+ };
414
+ let arguments = arguments
415
+ .iter()
416
+ .map(|argument| self.source(ast::argument_expression(argument)))
417
+ .collect();
418
+ Some((self.source(object)?, arguments))
419
+ }
282
420
 
283
- fn source(locals: &HashMap<Atom, Source>, expr: &Expr) -> Option<Source> {
284
- match expr {
285
- Expr::Ident(name) => locals.get(&name.sym).cloned(),
286
- Expr::Paren(paren) => source(locals, &paren.expr),
287
- Expr::Member(member) if member.obj.is_this() => {
288
- Some(Source::Field(ast::member_str(&member.prop)?.to_owned()))
289
- }
290
- Expr::Call(_) => match method_call(locals, expr, "read")? {
291
- (Source::Field(registers), arguments) => match &arguments[..] {
292
- [Some(Source::Field(index))] => Some(Source::Register {
293
- registers,
294
- index: index.clone(),
295
- }),
421
+ fn source(&self, expression: &Expression<'a>) -> Option<Source> {
422
+ match expression {
423
+ Expression::Identifier(name) => self.sources.get(&self.script.binding(name)).cloned(),
424
+ Expression::ThisExpression(_) => Some(Source::Machine),
425
+ Expression::ParenthesizedExpression(paren) => self.source(&paren.expression),
426
+ Expression::CallExpression(_) => match self.method_call(expression, "read")? {
427
+ (Source::Field(registers), arguments) => match &arguments[..] {
428
+ [Some(Source::Field(index))] => Some(Source::Register {
429
+ registers,
430
+ index: index.clone(),
431
+ }),
432
+ _ => None,
433
+ },
296
434
  _ => None,
297
435
  },
298
- _ => None,
299
- },
436
+ expression => {
437
+ let member = expression.as_member_expression()?;
438
+ matches!(self.source(member.object()), Some(Source::Machine))
439
+ .then(|| ast::member_str(member).map(|name| Source::Field(name.to_owned())))?
440
+ }
441
+ }
442
+ }
443
+ }
444
+
445
+ /// The field `this.<field>` reads.
446
+ pub(super) fn this_field<'a>(expression: &Expression<'a>) -> Option<&'a str> {
447
+ let member = expression.as_member_expression()?;
448
+ match member.object() {
449
+ Expression::ThisExpression(_) => ast::member_str(member),
300
450
  _ => None,
301
451
  }
302
452
  }
303
453
 
304
454
  /// Finds the string decryption function called by the register reader as
305
455
  /// `decrypt(this.<values>[index])`.
306
- pub(super) fn record_decoder(body: &BlockStmt) -> Option<Atom> {
456
+ pub(super) fn record_decoder<'a>(
457
+ script: Script<'a>,
458
+ body: &'a FunctionBody<'a>,
459
+ access: super::registers::Access<'a>,
460
+ ) -> Option<SymbolId> {
307
461
  /// Matches `<decoder>(this.<values>[<index>])`.
308
- struct Decoders {
309
- index: Atom,
310
- found: Vec<Atom>,
462
+ struct Decoders<'a> {
463
+ script: Script<'a>,
464
+ index: SymbolId,
465
+ object: Option<SymbolId>,
466
+ access: super::registers::Access<'a>,
467
+ found: Vec<Binding<'a>>,
311
468
  }
312
469
 
313
- impl Visit for Decoders {
314
- fn visit_call_expr(&mut self, call: &CallExpr) {
315
- if let Some(callee) = call.callee.as_expr().and_then(|callee| callee.as_ident())
316
- && let [argument] = &call.args[..]
317
- && let Some(member) = argument.expr.as_member()
318
- && member
319
- .obj
320
- .as_member()
321
- .is_some_and(|values| values.obj.is_this())
470
+ impl<'a> Visit<'a> for Decoders<'a> {
471
+ fn visit_call_expression(&mut self, call: &CallExpression<'a>) {
472
+ if let Expression::Identifier(callee) = &call.callee
473
+ && let [argument] = &call.arguments[..]
474
+ && let Some(member) = ast::argument_expression(argument).as_member_expression()
322
475
  && member
323
- .prop
324
- .as_computed()
325
- .and_then(|index| index.expr.as_ident())
326
- .is_some_and(|name| name.sym == self.index)
476
+ .object()
477
+ .as_member_expression()
478
+ .is_some_and(|values| {
479
+ matches!(values.object(), Expression::ThisExpression(_))
480
+ || (self.object.is_some_and(|object| {
481
+ self.script.refers_to(values.object(), object)
482
+ }) && ast::member_str(values)
483
+ .and_then(|name| self.access.member(name))
484
+ == Some(super::registers::Member::Values))
485
+ })
486
+ && let MemberExpression::ComputedMemberExpression(member) = member
487
+ && self.script.refers_to(&member.expression, self.index)
327
488
  {
328
- self.found.push(callee.sym.clone());
489
+ self.found.push(self.script.binding(callee));
329
490
  }
330
- call.visit_children_with(self);
491
+ walk::walk_call_expression(self, call);
331
492
  }
332
493
  }
333
494
 
334
- let read = body.stmts.iter().find_map(|statement| match statement {
335
- Stmt::Decl(Decl::Class(declaration)) if method(&declaration.class, "write").is_some() => {
336
- method(&declaration.class, "read")
495
+ let (read, object, index) = match access {
496
+ super::registers::Access::Methods => {
497
+ let read = body
498
+ .statements
499
+ .iter()
500
+ .find_map(|statement| match statement {
501
+ Statement::ClassDeclaration(class) if method(class, "write").is_some() => {
502
+ method(class, "read")
503
+ }
504
+ _ => None,
505
+ })?;
506
+ (read, None, 0)
337
507
  }
338
- _ => None,
339
- })?;
508
+ super::registers::Access::Functions { read, .. } => {
509
+ (read, Some(parameter(&read.params, 0)?.symbol_id()), 1)
510
+ }
511
+ };
340
512
  let mut decoders = Decoders {
341
- index: read.params.first()?.pat.as_ident()?.sym.clone(),
513
+ script,
514
+ object,
515
+ access,
516
+ index: parameter(&read.params, index)?.symbol_id(),
342
517
  found: Vec::new(),
343
518
  };
344
- read.visit_with(&mut decoders);
345
- let [decoder]: [Atom; 1] = decoders.found.try_into().ok()?;
519
+ decoders.visit_function(read, ScopeFlags::Function);
520
+ decoders.found.sort();
521
+ decoders.found.dedup();
522
+ let [Binding::Symbol(decoder)] = decoders.found[..] else {
523
+ return None;
524
+ };
346
525
  Some(decoder)
347
526
  }
348
527
 
349
- /// The first class declaration with a `run` method.
350
- fn machine_class(body: &BlockStmt) -> Option<&Class> {
351
- body.stmts.iter().find_map(|statement| match statement {
352
- Stmt::Decl(Decl::Class(declaration)) if method(&declaration.class, "run").is_some() => {
353
- Some(&*declaration.class)
354
- }
355
- _ => None,
356
- })
357
- }
358
-
359
- pub(super) fn method<'a>(class: &'a Class, name: &str) -> Option<&'a Function> {
360
- class.body.iter().find_map(|member| match member {
361
- ClassMember::Method(method) if ast::static_key_str(&method.key) == Some(name) => {
362
- Some(&*method.function)
528
+ /// The method, getter, or setter named `name`, excluding the constructor.
529
+ pub(super) fn method<'a>(class: &'a Class<'a>, name: &str) -> Option<&'a Function<'a>> {
530
+ class.body.body.iter().find_map(|element| match element {
531
+ ClassElement::MethodDefinition(method)
532
+ if method.kind != MethodDefinitionKind::Constructor
533
+ && ast::static_key_str(&method.key, method.computed) == Some(name) =>
534
+ {
535
+ Some(&*method.value)
363
536
  }
364
537
  _ => None,
365
538
  })
@@ -367,40 +540,46 @@ pub(super) fn method<'a>(class: &'a Class, name: &str) -> Option<&'a Function> {
367
540
 
368
541
  /// Extracts opcode and handler pairs from `case <opcode>: <handler>(<machine>); break;`.
369
542
  pub(super) fn handlers<'a>(
370
- functions: &HashMap<Atom, &'a Function>,
543
+ script: Script<'a>,
544
+ functions: &HashMap<SymbolId, &'a Function<'a>>,
371
545
  dispatch: &DispatchLoop<'a>,
372
- ) -> Result<HashMap<u8, &'a Function>> {
546
+ ) -> Result<HashMap<u8, &'a Function<'a>>> {
373
547
  let switch = first_switch_in(dispatch.function).context("the dispatch loop has no switch")?;
374
548
  let machine = dispatch.machine();
375
- let is_machine = |argument: &ExprOrSpread| {
376
- argument.spread.is_none()
377
- && argument
378
- .expr
379
- .as_ident()
380
- .is_some_and(|name| Some(&name.sym) == machine)
549
+ let is_machine = |argument: &CallArgument<'_>| {
550
+ argument.as_expression().is_some_and(|argument| {
551
+ machine.is_some_and(|machine| script.refers_to(argument, machine))
552
+ })
381
553
  };
382
554
  let mut handlers = HashMap::default();
383
555
  for case in &switch.cases {
384
556
  let Some(test) = &case.test else { continue };
385
- let Expr::Lit(Lit::Num(opcode)) = &**test else {
557
+ let Expression::NumericLiteral(opcode) = test else {
386
558
  bail!("a dispatch case is not a number");
387
559
  };
388
560
  let opcode = convert::byte(opcode.value).context("a dispatch case is not an opcode")?;
389
561
  let call = case
390
- .cons
562
+ .consequent
391
563
  .iter()
392
- .find_map(|statement| statement.as_expr()?.expr.as_call())
564
+ .find_map(|statement| match statement {
565
+ Statement::ExpressionStatement(statement) => match &statement.expression {
566
+ Expression::CallExpression(call) => Some(call),
567
+ _ => None,
568
+ },
569
+ _ => None,
570
+ })
393
571
  .with_context(|| format!("dispatch case {opcode} calls no handler"))?;
394
- let Some(name) = call.callee.as_expr().and_then(|callee| callee.as_ident()) else {
572
+ let Expression::Identifier(name) = &call.callee else {
395
573
  bail!("dispatch case {opcode} calls no named handler");
396
574
  };
397
- let function = functions
398
- .get(&name.sym)
399
- .with_context(|| format!("no handler function {}", name.sym))?;
575
+ let function = script
576
+ .symbol(name)
577
+ .and_then(|symbol| functions.get(&symbol))
578
+ .with_context(|| format!("no handler function {}", name.name))?;
400
579
  ensure!(
401
- matches!(&call.args[..], [argument] if is_machine(argument)),
580
+ matches!(&call.arguments[..], [argument] if is_machine(argument)),
402
581
  "dispatch case {opcode} does not pass {} only the machine",
403
- name.sym
582
+ name.name
404
583
  );
405
584
  let previous = handlers.insert(opcode, *function);
406
585
  ensure!(previous.is_none(), "opcode {opcode} has two dispatch cases");
@@ -409,30 +588,35 @@ pub(super) fn handlers<'a>(
409
588
  }
410
589
 
411
590
  /// Finds the first dispatch switch in the body of `function`.
412
- fn first_switch_in(function: &Function) -> Option<&SwitchStmt> {
413
- function.body.as_ref()?.stmts.iter().find_map(first_switch)
591
+ fn first_switch_in<'a>(function: &'a Function<'a>) -> Option<&'a SwitchStatement<'a>> {
592
+ function
593
+ .body
594
+ .as_deref()?
595
+ .statements
596
+ .iter()
597
+ .find_map(first_switch)
414
598
  }
415
599
 
416
600
  /// Finds the first dispatch `switch`; both dispatch loops use the same form.
417
- fn first_switch(statement: &Stmt) -> Option<&SwitchStmt> {
601
+ fn first_switch<'a>(statement: &'a Statement<'a>) -> Option<&'a SwitchStatement<'a>> {
418
602
  match statement {
419
- Stmt::Switch(switch) => Some(switch),
420
- Stmt::Block(block) => block.stmts.iter().find_map(first_switch),
421
- Stmt::While(looping) => first_switch(&looping.body),
422
- Stmt::If(branch) => {
423
- first_switch(&branch.cons).or_else(|| first_switch(branch.alt.as_deref()?))
603
+ Statement::SwitchStatement(switch) => Some(switch),
604
+ Statement::BlockStatement(block) => block.body.iter().find_map(first_switch),
605
+ Statement::WhileStatement(looping) => first_switch(&looping.body),
606
+ Statement::IfStatement(branch) => {
607
+ first_switch(&branch.consequent).or_else(|| first_switch(branch.alternate.as_ref()?))
424
608
  }
425
- Stmt::Try(attempt) => attempt.block.stmts.iter().find_map(first_switch),
609
+ Statement::TryStatement(attempt) => attempt.block.body.iter().find_map(first_switch),
426
610
  _ => None,
427
611
  }
428
612
  }
429
613
 
430
614
  /// Where the program runner starts a program (see [`Runner`]).
431
- pub(super) fn entry(body: &BlockStmt) -> Result<Position> {
615
+ pub(super) fn entry(body: &FunctionBody<'_>) -> Result<Position> {
432
616
  let runner = body
433
- .stmts
617
+ .statements
434
618
  .iter()
435
- .find_map(|statement| Runner::installed_by(statement.as_expr()?.expr.as_assign()?))
619
+ .find_map(installed_runner)
436
620
  .context("no program runner in the VM's scope")?;
437
621
  let pc = convert::position(runner.pc).context("the runner starts at no program position")?;
438
622
  let key = convert::byte(runner.key).context("the runner starts with no dispatch key")?;
@@ -448,75 +632,108 @@ struct Runner<'ast> {
448
632
  pc: f64,
449
633
  /// Initial dispatch key from the source.
450
634
  key: f64,
635
+ class: &'ast Expression<'ast>,
636
+ function: Option<&'ast Expression<'ast>>,
637
+ }
638
+
639
+ fn installed_runner<'a>(statement: &'a Statement<'a>) -> Option<Runner<'a>> {
640
+ let Statement::ExpressionStatement(statement) = statement else {
641
+ return None;
642
+ };
643
+ let Expression::AssignmentExpression(assignment) = &statement.expression else {
644
+ return None;
645
+ };
646
+ Runner::installed_by(assignment)
451
647
  }
452
648
 
453
649
  impl<'ast> Runner<'ast> {
454
- fn installed_by(assign: &'ast AssignExpr) -> Option<Self> {
455
- let AssignTarget::Simple(SimpleAssignTarget::Member(target)) = &assign.left else {
650
+ fn installed_by(assignment: &'ast AssignmentExpression<'ast>) -> Option<Self> {
651
+ let target = assignment.left.as_member_expression()?;
652
+ if matches!(target.object(), Expression::Super(_)) {
653
+ return None;
654
+ }
655
+ let Expression::FunctionExpression(function) = &assignment.right else {
456
656
  return None;
457
657
  };
458
- let body = assign.right.as_fn_expr()?.function.body.as_ref()?;
459
- let [
460
- Stmt::Return(ReturnStmt {
461
- arg: Some(call), ..
462
- }),
463
- ] = after_bare_declarations(&body.stmts)
658
+ let body = function.body.as_deref()?;
659
+ if !body.directives.is_empty() {
660
+ return None;
661
+ }
662
+ let [Statement::ReturnStatement(returned)] = after_bare_declarations(&body.statements)
464
663
  else {
465
664
  return None;
466
665
  };
467
- let (machine, [pc, key, ..]) = ast::method_call(call, "run")? else {
666
+ let expression = returned.argument.as_ref()?;
667
+ let (machine, arguments, function) =
668
+ if let Some((machine, arguments)) = ast::method_call(expression, "run") {
669
+ (machine, arguments, None)
670
+ } else if let Expression::CallExpression(call) = expression {
671
+ let [machine, arguments @ ..] = &call.arguments[..] else {
672
+ return None;
673
+ };
674
+ (
675
+ ast::argument_expression(machine),
676
+ arguments,
677
+ Some(&call.callee),
678
+ )
679
+ } else {
680
+ return None;
681
+ };
682
+ let [pc, key, ..] = arguments else {
468
683
  return None;
469
684
  };
470
- let (Expr::New(_), Expr::Lit(Lit::Num(pc)), Expr::Lit(Lit::Num(key))) =
471
- (machine, &*pc.expr, &*key.expr)
685
+ let (
686
+ Expression::NewExpression(new),
687
+ Expression::NumericLiteral(pc),
688
+ Expression::NumericLiteral(key),
689
+ ) = (
690
+ machine,
691
+ ast::argument_expression(pc),
692
+ ast::argument_expression(key),
693
+ )
472
694
  else {
473
695
  return None;
474
696
  };
475
697
  Some(Self {
476
- name: ast::member_str(&target.prop)?,
698
+ name: ast::member_str(target)?,
477
699
  pc: pc.value,
478
700
  key: key.value,
701
+ class: &new.callee,
702
+ function,
479
703
  })
480
704
  }
481
705
  }
482
706
 
483
707
  /// Returns the installed program runner's name if exactly one exists (see [`Runner`]).
484
- pub(crate) fn runner_name(script: &Script) -> Option<String> {
485
- #[derive(Default)]
486
- struct Names(Vec<String>);
487
-
488
- impl Visit for Names {
489
- fn visit_assign_expr(&mut self, assign: &AssignExpr) {
490
- if let Some(runner) = Runner::installed_by(assign) {
491
- self.0.push(runner.name.to_owned());
492
- }
493
- assign.visit_children_with(self);
494
- }
495
- }
496
-
497
- let mut names = Names::default();
498
- script.visit_with(&mut names);
499
- let [name]: [String; 1] = names.0.try_into().ok()?;
500
- Some(name)
708
+ pub(crate) fn runner_name(script: Script<'_>) -> Option<String> {
709
+ let runners = script.nodes.iter().filter_map(|node| match node.kind() {
710
+ AstKind::AssignmentExpression(assignment) => Runner::installed_by(assignment),
711
+ _ => None,
712
+ });
713
+ let runner = candidates::exactly_one(runners, "program runner").ok()?;
714
+ Some(runner.name.to_owned())
501
715
  }
502
716
 
503
717
  /// Whether `call` calls the runner `name` installs, as `name(...)` or `<global>.name(...)`.
504
- pub(crate) fn calls_runner(call: &CallExpr, name: &str) -> bool {
505
- match call.callee.as_expr().map(|callee| callee.unwrap_parens()) {
506
- Some(Expr::Ident(callee)) => callee.sym == name,
507
- Some(Expr::Member(member)) => ast::member_str(&member.prop) == Some(name),
508
- _ => false,
718
+ pub(crate) fn calls_runner(call: &CallExpression<'_>, name: &str) -> bool {
719
+ match call.callee.without_parentheses() {
720
+ Expression::Identifier(callee) => callee.name == name,
721
+ callee => callee.as_member_expression().is_some_and(|member| {
722
+ !matches!(member.object(), Expression::Super(_))
723
+ && ast::member_str(member) == Some(name)
724
+ }),
509
725
  }
510
726
  }
511
727
 
512
728
  /// Skips leading `var x;` declarations. Deobfuscation removes helper objects used to calculate
513
729
  /// `run` arguments but may leave their declarations.
514
- fn after_bare_declarations(statements: &[Stmt]) -> &[Stmt] {
730
+ fn after_bare_declarations<'b, 'a>(statements: &'b [Statement<'a>]) -> &'b [Statement<'a>] {
515
731
  let start = statements
516
732
  .iter()
517
733
  .position(|statement| {
518
- !matches!(statement, Stmt::Decl(Decl::Var(declaration))
519
- if declaration.decls.iter().all(|declarator| declarator.init.is_none()))
734
+ !matches!(statement, Statement::VariableDeclaration(declaration)
735
+ if !declaration.kind.is_using()
736
+ && declaration.declarations.iter().all(|declarator| declarator.init.is_none()))
520
737
  })
521
738
  .unwrap_or(statements.len());
522
739
  &statements[start..]
@@ -525,13 +742,15 @@ fn after_bare_declarations(statements: &[Stmt]) -> &[Stmt] {
525
742
  #[cfg(test)]
526
743
  mod tests {
527
744
  use super::*;
745
+ use crate::js::ast::ParsedScript;
528
746
 
529
747
  fn entry_of(source: &str) -> Result<Position> {
530
- let parsed = ast::parse_javascript("t", source).unwrap();
531
- let Some(Stmt::Decl(Decl::Fn(function))) = parsed.script.body.first() else {
748
+ let parsed = ParsedScript::parse("t", source).unwrap();
749
+ let script = parsed.script();
750
+ let Some(Statement::FunctionDeclaration(function)) = script.program.body.first() else {
532
751
  panic!("the test source declares no function");
533
752
  };
534
- entry(function.function.body.as_ref().unwrap())
753
+ entry(function.body.as_deref().unwrap())
535
754
  }
536
755
 
537
756
  #[test]
@@ -539,6 +758,7 @@ mod tests {
539
758
  for source in [
540
759
  "function f(O) { O.vlJM8 = function(p) { return new M(p).run(0, 238, []); }; }",
541
760
  "function f(O) { O.vlJM8 = function(p) { var a; var b, c; return new M(p).run(0, 238, []); }; }",
761
+ "function f(O) { O.vlJM8 = function(p) { return execute(new M(p), 0, 238, []); }; }",
542
762
  ] {
543
763
  assert_eq!(entry_of(source).unwrap(), Position::new(0, 238), "{source}");
544
764
  }
@@ -549,22 +769,102 @@ mod tests {
549
769
  );
550
770
  }
551
771
 
772
+ #[test]
773
+ fn extracts_free_runner_and_register_helpers_by_binding() {
774
+ let parsed = ParsedScript::parse("moved VM", r#"
775
+ function scope(global) {
776
+ class Unrelated { run() {} }
777
+ class Registers { constructor() {} }
778
+ class Machine { constructor(program) {} }
779
+ function load(registers, index) { return registers.H[index]; }
780
+ function store(registers, index, value) { registers.H[index] = value; }
781
+ function opcode(machine) {}
782
+ function dispatch(machine, registers, pending, program) {
783
+ while (true) { switch (program[0]) { case 42: opcode(machine); break; } }
784
+ }
785
+ function execute(machine, pc, key, cells) {
786
+ var registers, index, program;
787
+ registers = machine.registers;
788
+ index = machine.cells;
789
+ store(registers, index, cells);
790
+ index = machine.program;
791
+ program = load(registers, index);
792
+ dispatch(machine, registers, null, program);
793
+ }
794
+ global.start = function(program) { return execute(new Machine(program), 0, 231, []); };
795
+ }
796
+ "#).unwrap();
797
+ let script = parsed.script();
798
+ assert_eq!(runner_name(script).as_deref(), Some("start"));
799
+ let found = find(script).unwrap();
800
+ assert_eq!(found.class.id.as_ref().unwrap().name, "Machine");
801
+ assert_eq!(entry(found.body).unwrap(), Position::new(0, 231));
802
+ let functions = functions(&found.body.statements);
803
+ let layout = run_method(script, &functions, found.run, found.machine).unwrap();
804
+ assert_eq!(
805
+ (&*layout.registers, &*layout.cells, &*layout.program),
806
+ ("registers", "cells", "program")
807
+ );
808
+ let super::super::registers::Access::Functions { read, write } = layout.access else {
809
+ panic!("expected free register accessors");
810
+ };
811
+ assert_eq!(read.id.as_ref().unwrap().name, "load");
812
+ assert_eq!(write.id.as_ref().unwrap().name, "store");
813
+ assert_eq!(
814
+ layout.dispatch.arguments,
815
+ vec![
816
+ Argument::Machine,
817
+ Argument::Registers,
818
+ Argument::Other,
819
+ Argument::Program
820
+ ]
821
+ );
822
+ let handlers = handlers(script, &functions, &layout.dispatch).unwrap();
823
+ assert_eq!(handlers[&42].id.as_ref().unwrap().name, "opcode");
824
+ }
825
+
826
+ #[test]
827
+ fn method_runner_selects_its_constructed_class() {
828
+ let parsed = ParsedScript::parse(
829
+ "two classes",
830
+ r#"
831
+ function scope(global) {
832
+ class Unrelated { run() {} }
833
+ class Machine { run() {} }
834
+ global.start = function(program) { return new Machine(program).run(0, 1, []); };
835
+ }
836
+ "#,
837
+ )
838
+ .unwrap();
839
+ let found = find(parsed.script()).unwrap();
840
+ assert_eq!(found.class.id.as_ref().unwrap().name, "Machine");
841
+ assert!(found.machine.is_none());
842
+ }
843
+
552
844
  #[test]
553
845
  fn names_the_runner_its_callers_call() {
554
- let parsed = ast::parse_javascript(
846
+ let parsed = ParsedScript::parse(
555
847
  "t",
556
848
  "O.vlJM8 = function(p) { return new M(p).run(0, 238, []); }; \
557
849
  O.other = function(p) { return M(p).run(0, 238, []); }; O.vlJM8('a'); vlJM8('b');",
558
850
  )
559
851
  .unwrap();
560
- let name = runner_name(&parsed.script).unwrap();
852
+ let script = parsed.script();
853
+ let name = runner_name(script).unwrap();
561
854
  assert_eq!(name, "vlJM8");
562
- let calls: Vec<&CallExpr> = parsed
563
- .script
855
+ let calls: Vec<&CallExpression<'_>> = script
856
+ .program
564
857
  .body
565
858
  .iter()
566
- .filter_map(|statement| statement.as_expr()?.expr.as_call())
859
+ .filter_map(|statement| match statement {
860
+ Statement::ExpressionStatement(statement) => match &statement.expression {
861
+ Expression::CallExpression(call) => Some(&**call),
862
+ _ => None,
863
+ },
864
+ _ => None,
865
+ })
567
866
  .collect();
867
+ assert_eq!(calls.len(), 2);
568
868
  assert!(calls.iter().all(|call| calls_runner(call, &name)));
569
869
  }
570
870
  }