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,598 +2,418 @@
2
2
  //!
3
3
  //! ```js
4
4
  //! h = { add: function (a, b) { return a + b; }, key: "name" };
5
- //! h.add(x, 1); h.key; // becomes (x) + (1); "name"
5
+ //! h.add(x, 1); h.key; // becomes x + 1; "name"
6
6
  //! ```
7
7
  //!
8
8
  //! Requires each property to be defined once on an object that does not escape (see
9
- //! [`Properties::is_stable`]), and calls that preserve argument evaluation and order (see
10
- //! [`can_inline`]). Hoisted function bodies use only objects known wherever they can be called
9
+ //! [`Properties::stable`]), and calls that preserve argument evaluation and order (see
10
+ //! [`substitute`]). Hoisted function bodies use only objects known wherever they can be called
11
11
  //! (see [`hoisting`]).
12
12
 
13
- use std::iter;
14
13
  use std::rc::Rc;
14
+ use std::{iter, mem};
15
15
 
16
- use swc_core::atoms::Atom;
17
- use swc_core::common::SyntaxContext;
18
- use swc_core::ecma::ast::{
19
- AssignExpr, AssignOp, AssignTarget, BlockStmt, CallExpr, Callee, Decl, Expr, ExprOrSpread,
20
- FnExpr, ForStmt, Function, Id, Ident, Lit, MemberExpr, MemberProp, MetaPropExpr, Number,
21
- ObjectLit, Program, ReturnStmt, SeqExpr, SimpleAssignTarget, Stmt, Super, ThisExpr, UnaryOp,
22
- VarDeclOrExpr,
16
+ use oxc::allocator::{CloneIn, GetAllocator, Vec as ArenaVec};
17
+ use oxc::ast::ast::{
18
+ Argument, AssignmentExpression, BlockStatement, Expression, Function, IdentifierReference,
19
+ ObjectPropertyKind, Program, SequenceExpression, Statement, VariableDeclarator,
23
20
  };
24
- use swc_core::ecma::visit::{Visit, VisitMut, VisitMutWith, VisitWith};
25
-
26
- use super::HashMap;
27
- use super::analysis::effects::{self, Effects};
28
- use super::analysis::hoisting::{self, HoistFacts, Hoisted, Hoisting, List};
29
- use super::analysis::{self, Analysis, Bindings, Globals, Properties, names};
30
- use crate::js::ast;
31
-
32
- /// Tracks helper objects and aliases only while their bindings have exactly one write, leaving
33
- /// reassigned bindings unchanged.
34
- pub(super) fn run(program: &mut Program, analysis: &mut Analysis) -> bool {
35
- let globals = analysis.globals;
36
- let (bindings, properties) = analysis.properties(program);
37
- let mut inline = InlineHelpers {
38
- bindings,
39
- properties,
40
- globals,
41
- hoisting: Hoisting::default(),
42
- lists: 0,
43
- defined_in: HashMap::default(),
44
- changed: false,
45
- };
46
- program.visit_mut_with(&mut inline);
47
- inline.changed
48
- }
49
-
50
- /// Properties of one helper object that can replace property reads.
21
+ use oxc::ast_visit::{Visit, VisitMut, walk, walk_mut};
22
+ use oxc::syntax::operator::{AssignmentOperator, UnaryOperator};
23
+ use oxc::syntax::scope::ScopeFlags;
24
+ use oxc_ecmascript::side_effects::MayHaveSideEffects;
25
+
26
+ use super::analysis::effects::Effects;
27
+ use super::analysis::hoisting::{self, Facts, Hoisted, Hoisting, List};
28
+ use super::analysis::properties::Properties;
29
+ use super::analysis::unconditional::Unconditional;
30
+ use super::analysis::{Analysis, Id};
31
+ use super::{HashMap, ast};
32
+
33
+ /// The helper objects and functions known at one point, indexed by binding identity.
51
34
  #[derive(Clone, Default)]
52
- struct ObjectProperties {
53
- functions: HashMap<Atom, FnExpr>,
54
- literals: HashMap<Atom, Expr>,
55
- }
56
-
57
- impl ObjectProperties {
58
- /// Every inlinable property of a helper object literal.
59
- fn collect(object: &ObjectLit) -> Self {
60
- let mut properties = Self::default();
61
- for property in ast::key_values(object) {
62
- if let Some(name) = analysis::prop_key(&property.key) {
63
- properties.insert(name, &property.value);
64
- }
65
- }
66
- properties
67
- }
68
-
69
- /// Records substitutable properties. Leaves unsupported properties as object reads.
70
- fn insert(&mut self, name: Atom, value: &Expr) {
71
- match value {
72
- Expr::Fn(function) => {
73
- self.functions.insert(name, function.clone());
74
- }
75
- // A regular expression carries state and identity, so it cannot
76
- // stand in for every read.
77
- Expr::Lit(Lit::Regex(_)) => {}
78
- // Decoded strings may print as templates without substitutions.
79
- Expr::Tpl(template) if !template.exprs.is_empty() => {}
80
- Expr::Lit(_) | Expr::Tpl(_) => {
81
- self.literals.insert(name, value.clone());
82
- }
83
- _ => {}
84
- }
85
- }
35
+ struct Helpers<'a> {
36
+ /// Known object properties, shared between snapshots until a new definition extends them.
37
+ objects: HashMap<Id, Rc<HashMap<&'a str, &'a Expression<'a>>>>,
38
+ /// Single-return functions assigned to bindings with exactly one write.
39
+ functions: HashMap<Id, &'a Function<'a>>,
86
40
  }
87
41
 
88
- /// The helper objects and functions known at one point, by binding.
89
- #[derive(Clone, Default)]
90
- struct Helpers {
91
- objects: HashMap<Id, Rc<ObjectProperties>>,
92
- /// Functions assigned to a binding written once.
93
- functions: HashMap<Id, Rc<FnExpr>>,
94
- }
95
-
96
- /// At a control-flow join, keeps the objects and properties known on both paths.
97
- impl HoistFacts for Helpers {
42
+ impl Facts for Helpers<'_> {
98
43
  fn meet(&mut self, other: &Self) {
99
44
  self.objects.retain(|id, properties| {
100
- let Some(theirs) = other.objects.get(id) else {
45
+ let Some(other) = other.objects.get(id) else {
101
46
  return false;
102
47
  };
103
- if !Rc::ptr_eq(properties, theirs) {
104
- let properties = Rc::make_mut(properties);
105
- properties
106
- .functions
107
- .retain(|name, _| theirs.functions.contains_key(name));
108
- properties
109
- .literals
110
- .retain(|name, _| theirs.literals.contains_key(name));
48
+ if !Rc::ptr_eq(properties, other) {
49
+ Rc::make_mut(properties).retain(|key, _| other.contains_key(key))
111
50
  }
112
51
  true
113
52
  });
114
- self.functions.meet(&other.functions);
53
+ self.functions.meet(&other.functions)
115
54
  }
116
55
  }
117
56
 
118
- struct InlineHelpers<'analysis> {
119
- bindings: &'analysis Bindings,
120
- properties: &'analysis Properties,
121
- globals: Globals,
122
- /// Tracks known helpers. An alias gets the properties known when its assignment runs.
123
- hoisting: Hoisting<Helpers>,
124
- /// Counter assigning an identity to each entered statement list or sequence.
57
+ /// Tracks helper objects and aliases only while their bindings have exactly one write.
58
+ pub(super) fn run<'a>(program: &mut Program<'a>, analysis: &mut Analysis<'a>) -> bool {
59
+ let properties = Properties::collect(program, analysis);
60
+ let mut inline = Inline {
61
+ analysis,
62
+ properties: &properties,
63
+ hoisting: Hoisting::default(),
64
+ lists: 0,
65
+ list: 0,
66
+ defined_in: HashMap::default(),
67
+ changed: false,
68
+ };
69
+ inline.visit_program(program);
70
+ inline.changed
71
+ }
72
+
73
+ struct Inline<'b, 'a> {
74
+ analysis: &'b Analysis<'a>,
75
+ properties: &'b Properties<'a>,
76
+ /// Aliases receive the properties known when their assignment runs.
77
+ hoisting: Hoisting<Rc<Helpers<'a>>>,
78
+ /// Counts entered statement lists and sequences, to identify each one.
125
79
  lists: usize,
126
- /// The statement list or sequence whose top level assigns each object binding.
80
+ /// The innermost statement list or sequence being visited.
81
+ list: usize,
82
+ /// The list or sequence that defines each object; only definitions in that same list may
83
+ /// extend it.
127
84
  defined_in: HashMap<Id, usize>,
128
85
  changed: bool,
129
86
  }
130
87
 
131
- impl InlineHelpers<'_> {
132
- /// Visits a statement in `list`, recording expressions that always execute before visiting
133
- /// its children. These include expression statements, return and throw values, loop
134
- /// initializers, branch tests, switch discriminants, and `var` initializers.
135
- fn statement(&mut self, statement: &mut Stmt, list: usize) {
136
- match statement {
137
- Stmt::Expr(statement) => self.top_level(&mut statement.expr, list),
138
- Stmt::Return(ReturnStmt {
139
- arg: Some(argument),
140
- ..
141
- }) => self.top_level(argument, list),
142
- Stmt::Throw(statement) => self.top_level(&mut statement.arg, list),
143
- Stmt::If(statement) => {
144
- self.top_level(&mut statement.test, list);
145
- statement.cons.visit_mut_with(self);
146
- statement.alt.visit_mut_with(self);
88
+ impl<'a> Hoisted<'a> for Inline<'_, 'a> {
89
+ type Facts = Rc<Helpers<'a>>;
90
+
91
+ fn analysis(&self) -> &Analysis<'a> {
92
+ self.analysis
93
+ }
94
+
95
+ fn hoisting(&mut self) -> &mut Hoisting<Self::Facts> {
96
+ &mut self.hoisting
97
+ }
98
+ }
99
+
100
+ impl<'a> Inline<'_, 'a> {
101
+ fn save_function(&mut self, id: Id, function: &Function<'a>) {
102
+ let function = self.analysis.save(function);
103
+ self.facts_mut().functions.insert(id, function);
104
+ }
105
+
106
+ /// Unshares the current facts before a change.
107
+ fn facts_mut(&mut self) -> &mut Helpers<'a> {
108
+ Rc::make_mut(&mut self.hoisting.facts)
109
+ }
110
+
111
+ /// Records a single-write object, function, or alias with the properties known at this
112
+ /// assignment.
113
+ fn record(&mut self, id: Id, value: &Expression<'a>) {
114
+ if !self.analysis.bindings.is_written_once(&id) {
115
+ return;
116
+ }
117
+ match value.get_inner_expression() {
118
+ Expression::ObjectExpression(object) => {
119
+ let mut properties = HashMap::default();
120
+ for property in &object.properties {
121
+ if let ObjectPropertyKind::ObjectProperty(property) = property
122
+ && let Some(key) = ast::key(&property.key, property.computed)
123
+ {
124
+ properties.insert(key, self.analysis.save(&property.value));
125
+ }
126
+ }
127
+ self.facts_mut().objects.insert(id, Rc::new(properties));
147
128
  }
148
- Stmt::Switch(statement) => {
149
- self.top_level(&mut statement.discriminant, list);
150
- statement.cases.visit_mut_with(self);
129
+ Expression::FunctionExpression(function) if ast::returned(function).is_some() => {
130
+ self.save_function(id, function)
151
131
  }
152
- Stmt::Decl(Decl::Var(declaration)) => {
153
- for declarator in &mut declaration.decls {
154
- declarator.name.visit_mut_with(self);
155
- if let Some(init) = &mut declarator.init {
156
- self.top_level(init, list);
157
- }
132
+ Expression::Identifier(identifier) => {
133
+ if let Some(source) = self.analysis.id(identifier)
134
+ && let Some(object) = self.hoisting.facts.objects.get(&source).cloned()
135
+ {
136
+ self.facts_mut().objects.insert(id, object);
158
137
  }
159
138
  }
160
- Stmt::For(ForStmt {
161
- init: Some(VarDeclOrExpr::Expr(init)),
162
- test,
163
- update,
164
- body,
165
- ..
166
- }) => {
167
- self.top_level(init, list);
168
- test.visit_mut_with(self);
169
- update.visit_mut_with(self);
170
- body.visit_mut_with(self);
139
+ _ => {
140
+ if let Some(Expression::FunctionExpression(function)) = self.stable_value(value) {
141
+ self.save_function(id, function)
142
+ }
171
143
  }
172
- statement => statement.visit_mut_with(self),
173
144
  }
174
145
  }
175
146
 
176
- /// Visits an expression guaranteed to run with its statement or sequence, preserving
177
- /// sequence order.
178
- fn top_level(&mut self, expression: &mut Expr, list: usize) {
179
- if let Expr::Seq(sequence) = expression {
180
- for operand in &mut sequence.exprs {
181
- self.top_level(operand, list);
182
- }
183
- return;
184
- }
185
- expression.visit_mut_with(self);
186
- let Expr::Assign(assignment) = expression else {
187
- return;
188
- };
189
- if let Some(target) = assignment.left.as_ident()
190
- && self.hoisting.facts.objects.contains_key(&target.to_id())
191
- {
192
- self.defined_in.insert(target.to_id(), list);
193
- } else if assignment.op == AssignOp::Assign
194
- && let AssignTarget::Simple(SimpleAssignTarget::Member(member)) = &assignment.left
195
- && let Some(identifier) = member.obj.as_ident()
196
- && let Some(property) = analysis::member_key(&member.prop)
197
- && self.defined_in.get(&identifier.to_id()) == Some(&list)
198
- && let Some(object) = self.hoisting.facts.objects.get_mut(&identifier.to_id())
199
- {
200
- // Only property assignments beside the object's definition safely extend it.
201
- // Elsewhere, a read might run before the assignment.
202
- Rc::make_mut(object).insert(property, &assignment.right);
147
+ /// Resolves a static member read only when both property stability and execution-order
148
+ /// facts hold.
149
+ fn stable_value(&self, expression: &Expression<'a>) -> Option<&'a Expression<'a>> {
150
+ let member = expression.as_member_expression()?;
151
+ let id = self.analysis.expression_id(member.object())?;
152
+ let key = member.static_property_name()?.as_str()?;
153
+ if !self.properties.stable(id, key, self.analysis) {
154
+ return None;
203
155
  }
156
+ self.hoisting.facts.objects.get(&id)?.get(key).copied()
204
157
  }
205
158
 
206
- /// Resolves a stable `helper.name` read to its helper object and property.
207
- fn stable(&self, member: &MemberExpr) -> Option<(&ObjectProperties, Atom)> {
208
- let identifier = member.obj.as_ident()?.to_id();
209
- let property = analysis::member_key(&member.prop)?;
210
- if !self.properties.is_stable(&identifier, &property) {
211
- return None;
212
- }
213
- Some((self.hoisting.facts.objects.get(&identifier)?, property))
159
+ /// Returns a known helper function that a call can inline.
160
+ fn callee(&self, callee: &Expression<'a>) -> Option<&'a Function<'a>> {
161
+ let Some(reference) = callee.get_identifier_reference() else {
162
+ return match self.stable_value(callee)? {
163
+ Expression::FunctionExpression(function) => Some(&**function),
164
+ _ => None,
165
+ };
166
+ };
167
+ let id = self.analysis.id(reference)?;
168
+ let function = *self.hoisting.facts.functions.get(&id)?;
169
+ names_only(function, id, self.analysis).then_some(function)
214
170
  }
215
171
  }
216
172
 
217
- impl VisitMut for InlineHelpers<'_> {
218
- fn visit_mut_assign_expr(&mut self, assignment: &mut AssignExpr) {
219
- assignment.visit_mut_children_with(self);
173
+ /// Only property definitions in the object's defining list extend its known properties.
174
+ impl<'a> Unconditional<'a> for Inline<'_, 'a> {
175
+ fn assigned(&mut self, assignment: &AssignmentExpression<'a>) {
176
+ if let Some(id) = self.analysis.target_id(&assignment.left) {
177
+ self.record(id, &assignment.right);
178
+ self.defined_in.insert(id, self.list);
179
+ } else if let Some(member) = assignment.left.as_member_expression()
180
+ && let Some(id) = self.analysis.expression_id(member.object())
181
+ && let Some(key) = member.static_property_name().and_then(|key| key.as_str())
182
+ && self.defined_in.get(&id) == Some(&self.list)
183
+ && self.hoisting.facts.objects.contains_key(&id)
184
+ {
185
+ let value = self.analysis.save(&assignment.right);
186
+ let objects = &mut self.facts_mut().objects;
187
+ let properties = objects.get_mut(&id).expect("the object is known");
188
+ Rc::make_mut(properties).insert(key, value);
189
+ }
190
+ }
220
191
 
221
- if assignment.op == AssignOp::Assign
222
- && let Some(target) = assignment.left.as_ident()
223
- && self.bindings.is_written_once(&target.to_id())
192
+ fn declared(&mut self, declarator: &VariableDeclarator<'a>) {
193
+ if let Some(id) = ast::pattern_id(&declarator.id)
194
+ && let Some(value) = &declarator.init
224
195
  {
225
- let target = target.to_id();
226
- let method = assignment
227
- .right
228
- .as_member()
229
- .and_then(|member| self.stable(member))
230
- .and_then(|(object, property)| object.functions.get(&property))
231
- .cloned();
232
- // Track object definitions, helper functions, and copies of objects and helper methods
233
- // by binding.
234
- let helpers = &mut self.hoisting.facts;
235
- if let Some(method) = method {
236
- helpers.functions.insert(target, Rc::new(method));
237
- } else if let Some(object) = assignment.right.as_object() {
238
- let properties = Rc::new(ObjectProperties::collect(object));
239
- helpers.objects.insert(target, properties);
240
- } else if let Some(function) = assignment.right.as_fn_expr() {
241
- helpers.functions.insert(target, Rc::new(function.clone()));
242
- } else if let Some(alias) = assignment.right.as_ident()
243
- && let Some(object) = helpers.objects.get(&alias.to_id()).cloned()
244
- {
245
- helpers.objects.insert(target, object);
246
- }
196
+ self.record(id, value);
197
+ self.defined_in.insert(id, self.list);
247
198
  }
248
199
  }
200
+ }
249
201
 
250
- /// Visit function declarations last with objects known at every possible call.
251
- fn visit_mut_stmts(&mut self, statements: &mut Vec<Stmt>) {
202
+ impl<'a> VisitMut<'a> for Inline<'_, 'a> {
203
+ fn visit_statements(&mut self, statements: &mut ArenaVec<'a, Statement<'a>>) {
252
204
  self.lists += 1;
253
- let list = self.lists;
254
- // Function declarations are available from the start of a function body. Top-level
255
- // declarations can be replaced by other scripts.
205
+ let outer = mem::replace(&mut self.list, self.lists);
256
206
  if self.hoisting.list == List::FunctionBody {
257
207
  for statement in statements.iter() {
258
- if let Stmt::Decl(Decl::Fn(declaration)) = statement
259
- && self.bindings.is_written_once(&declaration.ident.to_id())
208
+ if let Statement::FunctionDeclaration(function) = statement
209
+ && let Some(id) = ast::function_id(function)
210
+ && self.analysis.bindings.is_written_once(&id)
211
+ && ast::returned(function).is_some()
260
212
  {
261
- let function = Rc::new(FnExpr::from(declaration.function.clone()));
262
- self.hoisting
263
- .facts
264
- .functions
265
- .insert(declaration.ident.to_id(), function);
213
+ self.save_function(id, function)
266
214
  }
267
215
  }
268
216
  }
269
- hoisting::visit_mut_stmts(self, statements, |inline, statement| {
270
- inline.statement(statement, list);
271
- });
217
+ hoisting::statements(self, statements, Self::unconditional_statement);
218
+ self.list = outer;
272
219
  }
273
220
 
274
- fn visit_mut_function(&mut self, function: &mut Function) {
275
- hoisting::visit_mut_function(self, function);
221
+ fn visit_function(&mut self, function: &mut Function<'a>, _: ScopeFlags) {
222
+ let outer = self.hoisting.facts.clone();
223
+ hoisting::function(self, function);
224
+ self.hoisting.facts = outer;
276
225
  }
277
226
 
278
- fn visit_mut_block_stmt(&mut self, block: &mut BlockStmt) {
279
- hoisting::visit_mut_block(self, block);
227
+ fn visit_block_statement(&mut self, block: &mut BlockStatement<'a>) {
228
+ hoisting::block(self, block)
280
229
  }
281
230
 
282
- fn visit_mut_ident(&mut self, identifier: &mut Ident) {
283
- hoisting::reference(self, identifier);
231
+ fn visit_identifier_reference(&mut self, reference: &mut IdentifierReference<'a>) {
232
+ hoisting::reference(self, reference)
284
233
  }
285
234
 
286
- /// The operands of a sequence run together and in order, like the statements of a list.
287
- fn visit_mut_seq_expr(&mut self, sequence: &mut SeqExpr) {
235
+ fn visit_sequence_expression(&mut self, sequence: &mut SequenceExpression<'a>) {
288
236
  self.lists += 1;
289
- let list = self.lists;
290
- for operand in &mut sequence.exprs {
291
- self.top_level(operand, list);
237
+ let outer = mem::replace(&mut self.list, self.lists);
238
+ for expression in &mut sequence.expressions {
239
+ self.unconditional_expression(expression)
292
240
  }
241
+ self.list = outer;
293
242
  }
294
243
 
295
- fn visit_mut_expr(&mut self, expression: &mut Expr) {
296
- expression.visit_mut_children_with(self);
297
-
298
- // `helper.name(...)`, with the expression the function returns.
299
- if let Expr::Call(call) = expression
300
- && let Callee::Expr(callee) = &call.callee
301
- && let Expr::Member(member) = &**callee
302
- && let Some((object, property)) = self.stable(member)
303
- && let Some(function) = object.functions.get(&property)
304
- && let Some(result) = ast::returned(&function.function)
305
- && can_inline(function, result, &call.args, self.globals)
306
- && let Some(replacement) = substitute(function, result, &call.args)
244
+ fn visit_assignment_expression(&mut self, assignment: &mut AssignmentExpression<'a>) {
245
+ walk_mut::walk_assignment_expression(self, assignment);
246
+ if assignment.operator == AssignmentOperator::Assign
247
+ && let Some(id) = self.analysis.target_id(&assignment.left)
307
248
  {
308
- *expression = replacement;
309
- self.changed = true;
249
+ self.record(id, &assignment.right)
310
250
  }
251
+ }
311
252
 
312
- // Inline a helper call only when its result uses parameters, sibling bindings, or
313
- // globals visible to every caller.
314
- if let Some((callee, args)) = ast::direct_call(expression)
315
- && let Some(function) = self.hoisting.facts.functions.get(&callee.to_id())
316
- && let Some(result) = ast::returned(&function.function)
317
- && names_only(result, &function.function, callee.ctxt, self.globals)
318
- && can_inline(function, result, args, self.globals)
319
- && let Some(replacement) = substitute(function, result, args)
253
+ fn visit_expression(&mut self, expression: &mut Expression<'a>) {
254
+ walk_mut::walk_expression(self, expression);
255
+ if let Expression::CallExpression(call) = expression
256
+ && let Some(function) = self.callee(&call.callee)
257
+ && let Some(value) = ast::returned(function)
258
+ && let Some(replacement) = substitute(function, value, &call.arguments, self.analysis)
320
259
  {
321
260
  *expression = replacement;
322
261
  self.changed = true;
262
+ return;
323
263
  }
324
-
325
- // A `helper.name` read of a literal.
326
- if let Some(member) = expression.as_member()
327
- && let Some((object, property)) = self.stable(member)
328
- && let Some(value) = object.literals.get(&property)
264
+ if let Some(value) = self.stable_value(expression)
265
+ && ast::is_constant(value)
329
266
  {
330
- *expression = value.clone();
267
+ *expression = value.clone_in_with_semantic_ids(self.analysis.allocator());
331
268
  self.changed = true;
332
269
  }
333
270
  }
334
271
  }
335
272
 
336
- /// Checks that every name in `result` is a parameter of `function`, a binding in `scope`, or a
337
- /// global.
338
- fn names_only(result: &Expr, function: &Function, scope: SyntaxContext, globals: Globals) -> bool {
339
- struct Names {
340
- parameters: Vec<Id>,
341
- scopes: [SyntaxContext; 2],
342
- all_allowed: bool,
343
- }
344
-
345
- impl Visit for Names {
346
- fn visit_ident(&mut self, identifier: &Ident) {
347
- self.all_allowed &= self.scopes.contains(&identifier.ctxt)
348
- || self.parameters.contains(&identifier.to_id());
349
- }
350
- }
351
-
352
- let mut names = Names {
353
- parameters: function
354
- .params
355
- .iter()
356
- .filter_map(|parameter| Some(parameter.pat.as_ident()?.to_id()))
357
- .collect(),
358
- scopes: [scope, globals.unresolved],
359
- all_allowed: true,
273
+ /// Checks that a direct helper's result names only parameters, globals, or bindings visible to
274
+ /// callers.
275
+ fn names_only(function: &Function<'_>, binding: Id, analysis: &Analysis<'_>) -> bool {
276
+ let (Some(parameters), Some(value)) = (ast::parameters(function), ast::returned(function))
277
+ else {
278
+ return false;
360
279
  };
361
- result.visit_with(&mut names);
362
- names.all_allowed
363
- }
364
-
365
- impl Hoisted for InlineHelpers<'_> {
366
- type Facts = Helpers;
367
-
368
- fn hoisting(&mut self) -> &mut Hoisting<Helpers> {
369
- &mut self.hoisting
370
- }
371
- }
372
-
373
- /// Checks whether substituting `args` into `result` preserves evaluations and their order.
374
- #[must_use]
375
- fn can_inline(function: &FnExpr, result: &Expr, args: &[ExprOrSpread], globals: Globals) -> bool {
376
- function.function.params.len() >= args.len()
377
- && trailing_parameters_unused(function, result, args.len())
378
- && args.iter().all(|argument| argument.spread.is_none())
379
- && !function.function.is_async
380
- && !function.function.is_generator
381
- && !uses_own_context(result)
382
- && (evaluates_arguments_safely(function, result, args, globals)
383
- || args
384
- .iter()
385
- .all(|argument| !effects::may_have_effects(&argument.expr, globals)))
386
- }
387
-
388
- /// Parameters without arguments disappear when the call is inlined. Allow this only for unused
389
- /// plain identifiers. Destructuring may evaluate defaults, and referenced parameters would have
390
- /// no binding after substitution.
391
- fn trailing_parameters_unused(function: &FnExpr, result: &Expr, argument_count: usize) -> bool {
392
- let params = &function.function.params;
393
- let trailing = names::parameter_ids(&params[argument_count.min(params.len())..]);
394
- trailing.is_some_and(|trailing| !names::mentions(result, &trailing))
280
+ let scope = analysis.scope(binding);
281
+ let uses = Effects::expression(value, analysis);
282
+ uses.reads.iter().chain(&uses.writes).all(|id| {
283
+ let mut ancestors = analysis.scoping.scope_ancestors(scope);
284
+ id.is_global()
285
+ || parameters.contains(id)
286
+ || ancestors.any(|scope| scope == analysis.scope(*id))
287
+ })
395
288
  }
396
289
 
397
- /// Checks for `this`, `arguments`, `new.target`, or `super` outside nested functions, which
398
- /// inlining would resolve in the caller instead.
399
- fn uses_own_context(result: &Expr) -> bool {
290
+ /// Checks for `this`, `arguments`, `new.target`, or `super` outside ordinary nested functions.
291
+ /// Inlining would otherwise resolve these names in the caller instead of the helper.
292
+ fn own_context(expression: &Expression<'_>) -> bool {
400
293
  struct Context(bool);
401
294
 
402
- impl Visit for Context {
403
- fn visit_this_expr(&mut self, _: &ThisExpr) {
404
- self.0 = true;
405
- }
406
-
407
- fn visit_super(&mut self, _: &Super) {
408
- self.0 = true;
409
- }
410
-
411
- fn visit_meta_prop_expr(&mut self, _: &MetaPropExpr) {
412
- self.0 = true;
295
+ impl<'a> Visit<'a> for Context {
296
+ fn visit_expression(&mut self, expression: &Expression<'a>) {
297
+ self.0 |= match expression {
298
+ Expression::ThisExpression(_) | Expression::Super(_) | Expression::NewTarget(_) => {
299
+ true
300
+ }
301
+ Expression::Identifier(identifier) => ast::is_arguments(identifier),
302
+ _ => false,
303
+ };
304
+ walk::walk_expression(self, expression)
413
305
  }
414
306
 
415
- fn visit_function(&mut self, _: &Function) {}
307
+ fn visit_function(&mut self, _: &Function<'a>, _: ScopeFlags) {}
416
308
  }
417
309
 
418
310
  let mut context = Context(false);
419
- result.visit_with(&mut context);
420
- context.0 || names::uses_arguments(result)
311
+ context.visit_expression(expression);
312
+ context.0
421
313
  }
422
314
 
423
- /// Requires each supplied parameter to appear exactly once in an operand that always runs.
424
- /// Reordering operands must preserve behavior. Coercions must run after operand evaluation, so
425
- /// nested arithmetic and short-circuit expressions need stricter checks.
426
- fn evaluates_arguments_safely(
427
- function: &FnExpr,
428
- result: &Expr,
429
- args: &[ExprOrSpread],
430
- globals: Globals,
431
- ) -> bool {
432
- let Some(operands) = eager_operands(result).filter(|operands| operands.len() == args.len())
433
- else {
434
- return false;
435
- };
436
- // The supplied parameters not yet read by an operand.
437
- let mut unread: Vec<Option<Id>> = function
438
- .function
439
- .params
440
- .iter()
441
- .take(args.len())
442
- .map(|parameter| Some(parameter.pat.as_ident()?.to_id()))
443
- .collect();
444
- // The parameter each operand reads, in evaluation order.
445
- let mut order = Vec::with_capacity(args.len());
446
- for operand in operands {
447
- let operand = operand.as_ident().map(Ident::to_id);
448
- let Some(index) = unread
449
- .iter()
450
- .position(|parameter| parameter.is_some() && *parameter == operand)
451
- else {
452
- return false;
453
- };
454
- unread[index] = None;
455
- order.push(index);
456
- }
457
- for (position, &first) in order.iter().enumerate() {
458
- for &second in &order[position + 1..] {
459
- if first > second && !commutes(&args[first].expr, &args[second].expr, globals) {
460
- return false;
461
- }
315
+ /// Lists operands that always run, in evaluation order. Looks through outer unary operators
316
+ /// that run after their operands. Excludes `delete`, which does not evaluate a value read.
317
+ fn operands<'b, 'a>(expression: &'b Expression<'a>) -> Option<Vec<&'b Expression<'a>>> {
318
+ match expression.get_inner_expression() {
319
+ Expression::UnaryExpression(unary) if unary.operator != UnaryOperator::Delete => {
320
+ operands(&unary.argument).or_else(|| Some(vec![&unary.argument]))
321
+ }
322
+ Expression::BinaryExpression(binary) => Some(vec![&binary.left, &binary.right]),
323
+ Expression::StaticMemberExpression(member) => Some(vec![&member.object]),
324
+ Expression::ComputedMemberExpression(member) => {
325
+ Some(vec![&member.object, &member.expression])
326
+ }
327
+ Expression::CallExpression(call) if !call.optional => {
328
+ let arguments = call.arguments.iter().map(Argument::as_expression);
329
+ iter::once(Some(&call.callee)).chain(arguments).collect()
462
330
  }
331
+ _ => None,
463
332
  }
464
- true
465
333
  }
466
334
 
467
- /// Two arguments may swap if both have no effects, or one has no effects and reads no binding
468
- /// the other writes. Calls and other opaque operations may write through closures, so binding
469
- /// reads cannot move across them.
470
- fn commutes(first: &Expr, second: &Expr, globals: Globals) -> bool {
471
- let (pure, impure) = match (
472
- effects::may_have_effects(first, globals),
473
- effects::may_have_effects(second, globals),
474
- ) {
335
+ /// Two arguments may swap if both have no effects, or one is effect-free and reads no binding
336
+ /// the other writes. Opaque operations may write through closures, so local reads cannot cross
337
+ /// them.
338
+ fn commutes(left: &Expression<'_>, right: &Expression<'_>, analysis: &Analysis<'_>) -> bool {
339
+ let effects = (
340
+ left.may_have_side_effects(analysis),
341
+ right.may_have_side_effects(analysis),
342
+ );
343
+ let (pure, impure) = match effects {
475
344
  (false, false) => return true,
476
- (false, true) => (first, second),
477
- (true, false) => (second, first),
345
+ (false, true) => (left, right),
346
+ (true, false) => (right, left),
478
347
  (true, true) => return false,
479
348
  };
480
- let pure = Effects::collect(pure);
481
- pure.reads.is_empty() || {
482
- let impure = Effects::collect(impure);
483
- !impure.opaque && impure.writes.is_disjoint(&pure.reads)
349
+ let pure = Effects::expression(pure, analysis);
350
+ if pure.reads.is_empty() {
351
+ return true;
484
352
  }
353
+ let impure = Effects::expression(impure, analysis);
354
+ !impure.opaque && impure.writes.is_disjoint(&pure.reads)
485
355
  }
486
356
 
487
- /// Lists operands in evaluation order. Looks through outer unary operators such as `!` in `!(b
488
- /// !== a)`, which run after their operands. Excludes `delete`, which does not read its operand.
489
- fn eager_operands(result: &Expr) -> Option<Vec<&Expr>> {
490
- match result {
491
- Expr::Paren(paren) => eager_operands(&paren.expr),
492
- // A unary operator applies to its evaluated operand, as the call would.
493
- Expr::Unary(unary) if unary.op != UnaryOp::Delete => {
494
- eager_operands(&unary.arg).or_else(|| Some(vec![&unary.arg]))
495
- }
496
- Expr::Bin(binary) if !binary.op.may_short_circuit() => {
497
- Some(vec![&binary.left, &binary.right])
498
- }
499
- // The property key is converted after both operands are evaluated.
500
- Expr::Member(member) => match &member.prop {
501
- MemberProp::Ident(_) => Some(vec![&member.obj]),
502
- MemberProp::Computed(key) => Some(vec![&member.obj, &key.expr]),
503
- MemberProp::PrivateName(_) => None,
504
- },
505
- Expr::Call(call)
506
- if let Callee::Expr(callee) = &call.callee
507
- && call.args.iter().all(|argument| argument.spread.is_none()) =>
508
- {
509
- Some(
510
- iter::once(&**callee)
511
- .chain(call.args.iter().map(|argument| &*argument.expr))
512
- .collect(),
513
- )
514
- }
515
- _ => None,
357
+ /// Checks argument evaluation before substituting a helper's single return expression.
358
+ /// Parameters must be plain identifiers; missing arguments are allowed only for unused trailing
359
+ /// parameters. Async functions, generators, helper-context reads, and parameter stores are kept.
360
+ /// Effectful arguments must occur exactly once in eager operands, with only commuting arguments
361
+ /// reordered. Nested arithmetic and short-circuit expressions need the stricter effect-free path.
362
+ fn substitute<'a>(
363
+ function: &Function<'a>,
364
+ value: &Expression<'a>,
365
+ arguments: &[Argument<'a>],
366
+ analysis: &Analysis<'a>,
367
+ ) -> Option<Expression<'a>> {
368
+ let parameters = ast::parameters(function)?;
369
+ if function.r#async
370
+ || function.generator
371
+ || own_context(value)
372
+ || arguments.len() > parameters.len()
373
+ {
374
+ return None;
516
375
  }
517
- }
518
-
519
- /// Substitutes arguments for `function` parameters in `result`. Returns `None` unless
520
- /// parameters are distinct plain identifiers.
521
- #[must_use]
522
- fn substitute(function: &FnExpr, result: &Expr, args: &[ExprOrSpread]) -> Option<Expr> {
523
- let mut parameters = HashMap::default();
524
- let bound = function.function.params.iter().zip(args);
525
- for (parameter, argument) in bound {
526
- let parameter = parameter.pat.as_ident()?;
527
- if parameters
528
- .insert(parameter.to_id(), &*argument.expr)
529
- .is_some()
530
- {
376
+ let arguments: Vec<&Expression<'a>> = arguments
377
+ .iter()
378
+ .map(Argument::as_expression)
379
+ .collect::<Option<_>>()?;
380
+ let used = Effects::expression(value, analysis);
381
+ // Parameter stores need a fresh local environment, which expression substitution cannot
382
+ // supply.
383
+ if parameters.iter().any(|id| used.writes.contains(id))
384
+ || parameters[arguments.len()..]
385
+ .iter()
386
+ .any(|id| used.reads.contains(id))
387
+ {
388
+ return None;
389
+ }
390
+ if arguments
391
+ .iter()
392
+ .any(|argument| argument.may_have_side_effects(analysis))
393
+ {
394
+ let operands = operands(value)?;
395
+ if operands.len() != arguments.len() {
531
396
  return None;
532
397
  }
533
- }
534
- Some(replace_parameters(result, parameters))
535
- }
536
-
537
- /// Substitutes parenthesized arguments for parameter references and parenthesizes the result.
538
- /// Preserves calls without a receiver; see [`needs_detaching`].
539
- #[must_use]
540
- pub(super) fn replace_parameters(expression: &Expr, parameters: HashMap<Id, &Expr>) -> Expr {
541
- let mut replacement = expression.clone();
542
- replacement.visit_mut_with(&mut ParameterReplacer { parameters });
543
- ast::parenthesized(Box::new(replacement))
544
- }
545
-
546
- struct ParameterReplacer<'arguments> {
547
- parameters: HashMap<Id, &'arguments Expr>,
548
- }
549
-
550
- impl VisitMut for ParameterReplacer<'_> {
551
- fn visit_mut_call_expr(&mut self, call: &mut CallExpr) {
552
- use swc_core::common::Spanned;
553
-
554
- let parameter_callee = matches!(&call.callee, Callee::Expr(callee)
555
- if callee.as_ident().is_some_and(|id| self.parameters.contains_key(&id.to_id())));
556
- call.visit_mut_children_with(self);
557
- if parameter_callee
558
- && let Callee::Expr(callee) = &mut call.callee
559
- && needs_detaching(callee.unwrap_parens())
560
- {
561
- // The wrapper calls a parameter as a value. Substituting `obj.method`
562
- // must not bind `obj` as `this`, or turn a passed `eval` into direct eval.
563
- **callee = Expr::Seq(SeqExpr {
564
- span: callee.span(),
565
- exprs: vec![
566
- Box::new(Expr::Lit(Lit::Num(Number {
567
- span: callee.span(),
568
- value: 0.0,
569
- raw: None,
570
- }))),
571
- callee.clone(),
572
- ],
573
- });
398
+ let mut order = vec![];
399
+ for operand in operands {
400
+ let id = analysis.expression_id(operand)?;
401
+ let index = parameters.iter().position(|parameter| *parameter == id)?;
402
+ if order.contains(&index) || index >= arguments.len() {
403
+ return None;
404
+ }
405
+ order.push(index)
574
406
  }
575
- }
576
-
577
- fn visit_mut_expr(&mut self, expression: &mut Expr) {
578
- expression.visit_mut_children_with(self);
579
-
580
- if let Expr::Ident(identifier) = expression
581
- && let Some(value) = self.parameters.get(&identifier.to_id())
582
- {
583
- *expression = ast::parenthesized(Box::new((*value).clone()));
407
+ for (position, &first) in order.iter().enumerate() {
408
+ for &second in &order[position + 1..] {
409
+ if first > second && !commutes(arguments[first], arguments[second], analysis) {
410
+ return None;
411
+ }
412
+ }
584
413
  }
585
414
  }
586
- }
587
-
588
- /// Checks whether a substituted callee needs `(0, callee)` to preserve call behavior. Member
589
- /// references would otherwise receive `this`, and `eval` could become a direct eval call. A
590
- /// local named `eval` may also hold the built-in function.
591
- fn needs_detaching(callee: &Expr) -> bool {
592
- match callee {
593
- Expr::Member(_) | Expr::SuperProp(_) | Expr::OptChain(_) => true,
594
- Expr::Ident(identifier) => identifier.sym == "eval",
595
- _ => false,
596
- }
415
+ let values = parameters.into_iter().zip(arguments).collect();
416
+ Some(ast::replace_parameters(value, &values, analysis))
597
417
  }
598
418
 
599
419
  #[cfg(test)]
@@ -603,42 +423,42 @@ mod tests {
603
423
  /// Helper-call test cases and expected results. Calls remain unchanged when inlining could
604
424
  /// alter evaluation or its order. `set()` writes the parameters read by the arguments;
605
425
  /// `first()` and `second()` have unknown effects.
606
- const CALLS: [(&str, &str, &str); 27] = [
607
- // Inlined results are parenthesized, as are their substituted arguments.
426
+ const CALLS: [(&str, &str, &str); 28] = [
427
+ // Inlined results keep the precedence of their substituted arguments.
608
428
  (
609
429
  "function(a, b) { return a & b; }",
610
430
  "h.op(local >>> 16, 65535) * 8133",
611
- "((local >>> 16) & (65535)) * 8133",
431
+ "(local >>> 16 & 65535) * 8133",
612
432
  ),
613
433
  // Eager operators evaluate their operands in argument order.
614
434
  (
615
435
  "function(a, b) { return a + b; }",
616
436
  "h.op(values[i++], first())",
617
- "(values[i++]) + (first())",
437
+ "values[i++] + first()",
618
438
  ),
619
439
  // Reordering is safe when only one operand has effects and the other reads no affected
620
440
  // binding.
621
441
  (
622
442
  "function(a, b) { return b - a; }",
623
443
  "h.op(values[i++], 2)",
624
- "(2) - (values[i++])",
444
+ "2 - values[i++]",
625
445
  ),
626
446
  (
627
447
  "function(a, b) { return b === a; }",
628
448
  "h.op(typeof values, 'object')",
629
- "('object') === (typeof values)",
449
+ r#""object" === typeof values"#,
630
450
  ),
631
451
  (
632
452
  "function(a, b) { return b | a; }",
633
453
  "h.op(first(), 2)",
634
- "(2) | (first())",
454
+ "2 | first()",
635
455
  ),
636
456
  // An effect-free operand may move past a write only if it does not read the written
637
457
  // binding.
638
458
  (
639
459
  "function(a, b) { return b | a; }",
640
460
  "h.op(values[i++], local)",
641
- "(local) | (values[i++])",
461
+ "local | values[i++]",
642
462
  ),
643
463
  (
644
464
  "function(a, b) { return b | a; }",
@@ -660,17 +480,22 @@ mod tests {
660
480
  "h.op(first(), second())",
661
481
  "h.op(first(), second())",
662
482
  ),
483
+ (
484
+ "function(a, b) { return b + a; }",
485
+ "h.op(v, [v] = [2])",
486
+ "h.op(v, [v] = [2])",
487
+ ),
663
488
  // An outer unary operator runs after both operands, so `!(b !== a)` permits the same
664
489
  // reordering as `b === a`. `delete` has different operand evaluation rules.
665
490
  (
666
491
  "function(aX, ae) { return !(ae !== aX); }",
667
492
  "h.op(q._cf_chl_opt.EcMJs8, 1)",
668
- "!((1) !== (q._cf_chl_opt.EcMJs8))",
493
+ "!(1 !== q._cf_chl_opt.EcMJs8)",
669
494
  ),
670
495
  (
671
496
  "function(a, b) { return -(b - a); }",
672
497
  "h.op(q.x, 2)",
673
- "-((2) - (q.x))",
498
+ "-(2 - q.x)",
674
499
  ),
675
500
  (
676
501
  "function(aX, ae) { return !(ae !== aX); }",
@@ -686,7 +511,7 @@ mod tests {
686
511
  (
687
512
  "function(a) { return a.key; }",
688
513
  "h.op(first())",
689
- "(first()).key",
514
+ "first().key",
690
515
  ),
691
516
  (
692
517
  "function(a, b) { return b[a]; }",
@@ -697,12 +522,12 @@ mod tests {
697
522
  (
698
523
  "function(rn, rw, ri, ru, KA) { return rn(rw, ri, ru); }",
699
524
  "h.op(local, 1, 2, 3)",
700
- "(local)((1), (2), (3))",
525
+ "local(1, 2, 3)",
701
526
  ),
702
527
  (
703
528
  "function(a, b, extra) { return b | a; }",
704
529
  "h.op(local, 2, 3)",
705
- "(2) | (local)",
530
+ "2 | local",
706
531
  ),
707
532
  (
708
533
  "function(a, b, extra) { return extra + b; }",
@@ -721,7 +546,7 @@ mod tests {
721
546
  "h.op(missing, 1)",
722
547
  "h.op(missing, 1)",
723
548
  ),
724
- ("function(a, b) { return b; }", "h.op(Math, 1)", "((1))"),
549
+ ("function(a, b) { return b; }", "h.op(Math, 1)", "return 1;"),
725
550
  // Tests bodies that skip, repeat, or reorder operands within larger expressions.
726
551
  (
727
552
  "function(a, b) { return a + a; }",
@@ -751,7 +576,7 @@ mod tests {
751
576
  ];
752
577
 
753
578
  fn transform(source: &str) -> String {
754
- testing::rewrite(source, super::run, /* fix= */ false)
579
+ testing::rewrite(source, super::run)
755
580
  }
756
581
 
757
582
  #[test]
@@ -773,9 +598,9 @@ mod tests {
773
598
  return h.invoke(eval, "typeof local");
774
599
  }"#,
775
600
  );
776
- testing::assert_contains(&output, &["(0, obj.method)(effect())", "plain(1);"], &[]);
601
+ testing::assert_contains(&output, &["(0, obj.method)(effect())", "plain(1);"]);
777
602
  let expected = ["(0, eval)(\"typeof local\")", "Number(obj.value);"];
778
- testing::assert_contains(&output, &expected, &[]);
603
+ testing::assert_contains(&output, &expected);
779
604
  assert!(!output.contains("(0, plain)"), "{output}");
780
605
  }
781
606
 
@@ -785,7 +610,7 @@ mod tests {
785
610
  "function f(h, p) { h = {}; h.op = function (a, b) { return a + b; }; \
786
611
  h.name = \"n\"; p = h; return [p.op(1, 2), p.name, h.name]; }",
787
612
  );
788
- assert!(output.contains("(1) + (2)"), "{output}");
613
+ assert!(output.contains("1 + 2"), "{output}");
789
614
  testing::assert_lacks(&output, &["p.name", "h.name]"]);
790
615
  // Static templates inline into later callbacks, as in a payload built after a delay.
791
616
  let output = transform(
@@ -795,7 +620,7 @@ mod tests {
795
620
  // A unary helper evaluates its argument first either way, even a member read.
796
621
  let output =
797
622
  transform("function f(n) { n = function (a) { return !a; }; if (n(w.x)) g(); }");
798
- assert!(output.contains("!(w.x)"), "{output}");
623
+ assert!(output.contains("!w.x"), "{output}");
799
624
  // Helper functions inline when they name only what callers see.
800
625
  let output = transform(
801
626
  "function f(add, call, read, g) { add = function(a, b) { return a + b; }; \
@@ -803,21 +628,21 @@ mod tests {
803
628
  g = function(local) { read = function() { return local; }; }; \
804
629
  g(5); return [call(2), add(3, 4), read()]; }",
805
630
  );
806
- let expected = ["((2)) + (1))", "Math.max((2))", "(3) + (4)", "read()]"];
807
- testing::assert_contains(&output, &[], &expected);
631
+ let expected = ["2 + 1", "Math.max(2)", "3 + 4", "read()]"];
632
+ testing::assert_contains(&output, &expected);
808
633
  // Function declarations inside function bodies are also stable; top-level declarations are
809
634
  // not.
810
635
  let output = transform(
811
636
  "function f(x) { return g(x, 1); function g(a, b) { return a + b; } } \
812
637
  function h(a) { return a; } k(h(2));",
813
638
  );
814
- testing::assert_contains(&output, &[], &["return ((x) + (1));", "k(h(2));"]);
639
+ testing::assert_contains(&output, &["return x + 1;", "k(h(2));"]);
815
640
  // Copies of helper methods are stable too.
816
641
  let output = transform(
817
642
  "function f(h, m) { h = { shr: function (a, b) { return a >> b; } }; m = h.shr; \
818
643
  return m(8, 1); }",
819
644
  );
820
- assert!(output.contains("(8) >> (1)"), "{output}");
645
+ assert!(output.contains("8 >> 1"), "{output}");
821
646
  }
822
647
 
823
648
  #[test]
@@ -837,11 +662,11 @@ mod tests {
837
662
  return [alias.add(1, 2), alias.name, h.add(1, 2)];
838
663
  }"#,
839
664
  );
840
- testing::assert_contains(&output, &["h.add(1, 2)", "h.add(3, 4)"], &[]);
665
+ testing::assert_contains(&output, &["h.add(1, 2)", "h.add(3, 4)"]);
841
666
  assert!(output.matches("h.name").count() == 2, "{output}");
842
- testing::assert_contains(&output, &["alias.add(1, 2)", "alias.name"], &[]);
667
+ testing::assert_contains(&output, &["alias.add(1, 2)", "alias.name"]);
843
668
  assert!(
844
- !output.contains("(1) + (2)"),
669
+ !output.contains("1 + 2"),
845
670
  "`h` is copied into a binding that is assigned again, which could change it before \
846
671
  that, so its properties are not trusted either: {output}"
847
672
  );