cfsolve 0.1.1 → 0.1.3

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 (404) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +18 -0
  3. data/README.md +10 -9
  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 +10 -8
  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/safari-26-ios/answers.json +2570 -1894
  65. data/lib/cfsolve/profiles/safari-26-ios/challenge-page-answers.json +1867 -1192
  66. data/lib/cfsolve/profiles/safari-26-ios.json +102 -156
  67. data/lib/cfsolve/version.rb +2 -2
  68. data/vendor/cfsolve/Cargo.lock +561 -1729
  69. data/vendor/cfsolve/Cargo.toml +26 -8
  70. data/vendor/cfsolve/README.md +4 -35
  71. data/vendor/cfsolve/examples/build_profile.rs +17 -15
  72. data/vendor/cfsolve/examples/capture_session.rs +37 -0
  73. data/vendor/cfsolve/examples/check_answers.rs +66 -0
  74. data/vendor/cfsolve/examples/compare_session.rs +189 -0
  75. data/vendor/cfsolve/examples/solver_profile.rs +84 -0
  76. data/vendor/cfsolve/examples/solver_smoke.rs +9 -8
  77. data/vendor/cfsolve/examples/stage2_snapshot.rs +6 -3
  78. data/vendor/cfsolve/examples/tls_echo.rs +3 -3
  79. data/vendor/cfsolve/examples/vm_snapshot.rs +32 -33
  80. data/vendor/cfsolve/profiles/safari-26-ios/answers.json +2570 -1894
  81. data/vendor/cfsolve/profiles/safari-26-ios/challenge-page-answers.json +1867 -1192
  82. data/vendor/cfsolve/profiles/safari-26-ios.json +102 -156
  83. data/vendor/cfsolve/scripts/capture_cssom.mjs +42 -0
  84. data/vendor/cfsolve/scripts/serve_cssom.py +111 -0
  85. data/vendor/cfsolve/src/bin/capture-proxy/main.rs +3 -2
  86. data/vendor/cfsolve/src/bin/challenge/cli.rs +8 -2
  87. data/vendor/cfsolve/src/bin/challenge/main.rs +7 -2
  88. data/vendor/cfsolve/src/capture/profile.rs +7 -18
  89. data/vendor/cfsolve/src/capture/proxy/tests.rs +1 -1
  90. data/vendor/cfsolve/src/capture/proxy.rs +44 -13
  91. data/vendor/cfsolve/src/capture/recorded.rs +42 -7
  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 +132 -0
  97. data/vendor/cfsolve/src/challenge/installed.rs +127 -269
  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 +175 -35
  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 +236 -122
  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 +201 -205
  125. data/vendor/cfsolve/src/deobfuscate/function_lengths.rs +32 -38
  126. data/vendor/cfsolve/src/deobfuscate/helper_objects.rs +346 -536
  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 +361 -322
  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 +23 -32
  144. data/vendor/cfsolve/src/embedding_page/page.rs +28 -2
  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 +9 -1
  149. data/vendor/cfsolve/src/http/requests.rs +2 -2
  150. data/vendor/cfsolve/src/js/ast.rs +479 -254
  151. data/vendor/cfsolve/src/js/objects.rs +9 -2
  152. data/vendor/cfsolve/src/lib.rs +1 -0
  153. data/vendor/cfsolve/src/listing/fold.rs +19 -92
  154. data/vendor/cfsolve/src/listing/query.rs +26 -291
  155. data/vendor/cfsolve/src/listing.rs +0 -1
  156. data/vendor/cfsolve/src/performance_clock.rs +20 -1
  157. data/vendor/cfsolve/src/profiling.rs +49 -0
  158. data/vendor/cfsolve/src/script.rs +35 -24
  159. data/vendor/cfsolve/src/session/challenge_page.rs +41 -21
  160. data/vendor/cfsolve/src/session/channel.rs +2 -2
  161. data/vendor/cfsolve/src/session/initial.rs +19 -10
  162. data/vendor/cfsolve/src/session/probes.rs +160 -125
  163. data/vendor/cfsolve/src/session/sink.rs +26 -1
  164. data/vendor/cfsolve/src/session/tests/challenge_page.rs +36 -1
  165. data/vendor/cfsolve/src/session/timing.rs +58 -53
  166. data/vendor/cfsolve/src/session/widget.rs +5 -6
  167. data/vendor/cfsolve/src/session.rs +1 -1
  168. data/vendor/cfsolve/src/solver.rs +3 -1
  169. data/vendor/cfsolve/src/stage2/analysis/analyze.rs +50 -33
  170. data/vendor/cfsolve/src/stage2/analysis/audit.rs +4 -2
  171. data/vendor/cfsolve/src/stage2/analysis/discovery.rs +61 -66
  172. data/vendor/cfsolve/src/stage2/analysis/envelope.rs +33 -22
  173. data/vendor/cfsolve/src/stage2/analysis/recognize.rs +3 -5
  174. data/vendor/cfsolve/src/stage2/analysis.rs +0 -1
  175. data/vendor/cfsolve/src/stage2/audit_globals.rs +378 -0
  176. data/vendor/cfsolve/src/stage2/benchmark.rs +66 -38
  177. data/vendor/cfsolve/src/stage2/call_stack.rs +1463 -1017
  178. data/vendor/cfsolve/src/stage2/challenge_page.rs +3 -2
  179. data/vendor/cfsolve/src/stage2/interaction/checkbox.rs +202 -150
  180. data/vendor/cfsolve/src/stage2/interaction/click.rs +2 -2
  181. data/vendor/cfsolve/src/stage2/interaction/native.rs +2 -1
  182. data/vendor/cfsolve/src/stage2/interaction/plan.rs +5 -7
  183. data/vendor/cfsolve/src/stage2/payload/recover.rs +56 -25
  184. data/vendor/cfsolve/src/stage2/probes/clock.rs +1 -3
  185. data/vendor/cfsolve/src/stage2/probes/ice.rs +11 -10
  186. data/vendor/cfsolve/src/stage2/probes/media.rs +47 -33
  187. data/vendor/cfsolve/src/stage2/probes/performance.rs +13 -8
  188. data/vendor/cfsolve/src/stage2/probes/rects.rs +12 -17
  189. data/vendor/cfsolve/src/stage2/probes/stack.rs +50 -103
  190. data/vendor/cfsolve/src/stage2/probes/websocket_echo.rs +17 -86
  191. data/vendor/cfsolve/src/stage2/probes.rs +0 -1
  192. data/vendor/cfsolve/src/stage2/queries.rs +50 -0
  193. data/vendor/cfsolve/src/stage2/records.rs +4 -13
  194. data/vendor/cfsolve/src/stage2/reference.json +1 -1
  195. data/vendor/cfsolve/src/stage2/reference.rs +98 -100
  196. data/vendor/cfsolve/src/stage2/registry.rs +41 -34
  197. data/vendor/cfsolve/src/stage2/replay.rs +182 -254
  198. data/vendor/cfsolve/src/stage2/stylesheets.rs +176 -0
  199. data/vendor/cfsolve/src/stage2/syntax.rs +88 -0
  200. data/vendor/cfsolve/src/stage2/trace.rs +152 -108
  201. data/vendor/cfsolve/src/stage2/vocabulary.rs +103 -403
  202. data/vendor/cfsolve/src/stage2.rs +4 -1
  203. data/vendor/cfsolve/src/vm/disassemble.rs +46 -13
  204. data/vendor/cfsolve/src/vm/dispatch.rs +93 -65
  205. data/vendor/cfsolve/src/vm/extract.rs +347 -250
  206. data/vendor/cfsolve/src/vm/interpret/access.rs +67 -68
  207. data/vendor/cfsolve/src/vm/interpret/builtin.rs +16 -63
  208. data/vendor/cfsolve/src/vm/interpret/call.rs +192 -51
  209. data/vendor/cfsolve/src/vm/interpret/compile.rs +643 -0
  210. data/vendor/cfsolve/src/vm/interpret/copies.rs +232 -120
  211. data/vendor/cfsolve/src/vm/interpret/evaluate/tests.rs +149 -40
  212. data/vendor/cfsolve/src/vm/interpret/evaluate.rs +189 -572
  213. data/vendor/cfsolve/src/vm/interpret/explore.rs +159 -46
  214. data/vendor/cfsolve/src/vm/interpret/formula.rs +131 -0
  215. data/vendor/cfsolve/src/vm/interpret/frozen.rs +169 -0
  216. data/vendor/cfsolve/src/vm/interpret/intrinsic.rs +414 -0
  217. data/vendor/cfsolve/src/vm/interpret/mix32.rs +189 -0
  218. data/vendor/cfsolve/src/vm/interpret/native_dispatch.rs +220 -0
  219. data/vendor/cfsolve/src/vm/interpret/pattern.rs +1736 -0
  220. data/vendor/cfsolve/src/vm/interpret/plan.rs +605 -330
  221. data/vendor/cfsolve/src/vm/interpret/register_helpers.rs +384 -0
  222. data/vendor/cfsolve/src/vm/interpret/run.rs +314 -54
  223. data/vendor/cfsolve/src/vm/interpret/semantics.rs +71 -46
  224. data/vendor/cfsolve/src/vm/interpret/setup.rs +80 -45
  225. data/vendor/cfsolve/src/vm/interpret/string_decoder.rs +482 -0
  226. data/vendor/cfsolve/src/vm/interpret.rs +16 -3
  227. data/vendor/cfsolve/src/vm/machine.rs +58 -47
  228. data/vendor/cfsolve/src/vm/output.rs +13 -5
  229. data/vendor/cfsolve/src/vm/packed_names.rs +311 -0
  230. data/vendor/cfsolve/src/vm/session.rs +197 -110
  231. data/vendor/cfsolve/src/vm/value.rs +35 -19
  232. data/vendor/cfsolve/src/vm.rs +3 -1
  233. data/vendor/cfsolve/vendor/btls-sys/Cargo.toml +3 -0
  234. data/vendor/cfsolve/vendor/btls-sys/build/main.rs +20 -0
  235. data/vendor/cfsolve/vendor/btls-sys/src/lib.rs +3 -0
  236. metadata +106 -183
  237. data/THIRD_PARTY_LICENSES/Inflector-0.11.4/LICENSE.md +0 -22
  238. data/THIRD_PARTY_LICENSES/arrayvec-0.7.6/LICENSE-MIT +0 -25
  239. data/THIRD_PARTY_LICENSES/ascii-1.1.0/LICENSE-APACHE +0 -201
  240. data/THIRD_PARTY_LICENSES/ascii-1.1.0/LICENSE-MIT +0 -22
  241. data/THIRD_PARTY_LICENSES/ast_node-5.0.0/LICENSE +0 -201
  242. data/THIRD_PARTY_LICENSES/better_scoped_tls-1.0.1/LICENSE +0 -201
  243. data/THIRD_PARTY_LICENSES/bitvec-1.0.1/LICENSE.txt +0 -21
  244. data/THIRD_PARTY_LICENSES/bstr-1.12.1/COPYING +0 -8
  245. data/THIRD_PARTY_LICENSES/bstr-1.12.1/LICENSE-MIT +0 -21
  246. data/THIRD_PARTY_LICENSES/cargo-platform-0.1.9/LICENSE-APACHE +0 -201
  247. data/THIRD_PARTY_LICENSES/cargo_metadata-0.19.2/LICENSE-MIT +0 -23
  248. data/THIRD_PARTY_LICENSES/darling_macro-0.20.11/LICENSE +0 -21
  249. data/THIRD_PARTY_LICENSES/debugid-0.8.0/LICENSE +0 -201
  250. data/THIRD_PARTY_LICENSES/derive_builder-0.20.2/LICENSE-MIT +0 -21
  251. data/THIRD_PARTY_LICENSES/derive_builder_core-0.20.2/LICENSE-MIT +0 -21
  252. data/THIRD_PARTY_LICENSES/derive_builder_macro-0.20.2/LICENSE-MIT +0 -21
  253. data/THIRD_PARTY_LICENSES/foldhash-0.1.5/LICENSE +0 -19
  254. data/THIRD_PARTY_LICENSES/from_variant-3.0.0/LICENSE +0 -201
  255. data/THIRD_PARTY_LICENSES/funty-2.0.0/LICENSE.txt +0 -21
  256. data/THIRD_PARTY_LICENSES/hashbrown-0.16.1/LICENSE-MIT +0 -25
  257. data/THIRD_PARTY_LICENSES/heck-0.5.0/LICENSE-APACHE +0 -201
  258. data/THIRD_PARTY_LICENSES/hermit-abi-0.5.2/LICENSE-APACHE +0 -201
  259. data/THIRD_PARTY_LICENSES/hermit-abi-0.5.2/LICENSE-MIT +0 -23
  260. data/THIRD_PARTY_LICENSES/hstr-3.0.3/LICENSE +0 -202
  261. data/THIRD_PARTY_LICENSES/if_chain-1.0.3/LICENSE-APACHE +0 -201
  262. data/THIRD_PARTY_LICENSES/if_chain-1.0.3/LICENSE-MIT +0 -25
  263. data/THIRD_PARTY_LICENSES/is-macro-0.3.7/LICENSE +0 -202
  264. data/THIRD_PARTY_LICENSES/itoa-1.0.17/LICENSE-MIT +0 -23
  265. data/THIRD_PARTY_LICENSES/jsonc-parser-0.26.3/LICENSE +0 -21
  266. data/THIRD_PARTY_LICENSES/lazy_static-1.5.0/LICENSE-APACHE +0 -201
  267. data/THIRD_PARTY_LICENSES/lazy_static-1.5.0/LICENSE-MIT +0 -25
  268. data/THIRD_PARTY_LICENSES/lru-0.16.2/LICENSE +0 -21
  269. data/THIRD_PARTY_LICENSES/memchr-2.7.6/COPYING +0 -3
  270. data/THIRD_PARTY_LICENSES/memchr-2.7.6/LICENSE-MIT +0 -21
  271. data/THIRD_PARTY_LICENSES/miette-derive-7.6.0/LICENSE +0 -229
  272. data/THIRD_PARTY_LICENSES/normpath-0.2.0/COPYRIGHT +0 -7
  273. data/THIRD_PARTY_LICENSES/normpath-0.2.0/LICENSE-MIT +0 -21
  274. data/THIRD_PARTY_LICENSES/normpath-0.2.0/LICENSE-THIRD-PARTY +0 -210
  275. data/THIRD_PARTY_LICENSES/num_cpus-1.17.0/LICENSE-APACHE +0 -201
  276. data/THIRD_PARTY_LICENSES/num_cpus-1.17.0/LICENSE-MIT +0 -20
  277. data/THIRD_PARTY_LICENSES/owo-colors-4.2.3/LICENSE +0 -21
  278. data/THIRD_PARTY_LICENSES/par-core-2.0.0/LICENSE +0 -202
  279. data/THIRD_PARTY_LICENSES/par-iter-2.0.0/LICENSE +0 -202
  280. data/THIRD_PARTY_LICENSES/path-clean-0.1.0/LICENSE-APACHE +0 -13
  281. data/THIRD_PARTY_LICENSES/path-clean-0.1.0/LICENSE-MIT +0 -18
  282. data/THIRD_PARTY_LICENSES/path-clean-1.0.1/LICENSE-APACHE +0 -13
  283. data/THIRD_PARTY_LICENSES/path-clean-1.0.1/LICENSE-MIT +0 -18
  284. data/THIRD_PARTY_LICENSES/pathdiff-0.2.3/LICENSE-APACHE +0 -201
  285. data/THIRD_PARTY_LICENSES/pathdiff-0.2.3/LICENSE-MIT +0 -23
  286. data/THIRD_PARTY_LICENSES/petgraph-0.7.1/LICENSE-APACHE +0 -201
  287. data/THIRD_PARTY_LICENSES/precomputed-map-0.2.0/LICENSE +0 -21
  288. data/THIRD_PARTY_LICENSES/preset_env_base-6.0.0/LICENSE +0 -201
  289. data/THIRD_PARTY_LICENSES/radium-0.7.0/LICENSE.txt +0 -21
  290. data/THIRD_PARTY_LICENSES/radix_fmt-1.0.0/LICENSE +0 -201
  291. data/THIRD_PARTY_LICENSES/rand-0.8.5/COPYRIGHT +0 -12
  292. data/THIRD_PARTY_LICENSES/rand-0.8.5/LICENSE-APACHE +0 -176
  293. data/THIRD_PARTY_LICENSES/rand_core-0.6.4/COPYRIGHT +0 -12
  294. data/THIRD_PARTY_LICENSES/rand_core-0.6.4/LICENSE-APACHE +0 -187
  295. data/THIRD_PARTY_LICENSES/rand_core-0.6.4/LICENSE-MIT +0 -26
  296. data/THIRD_PARTY_LICENSES/regress-0.10.5/LICENSE-APACHE +0 -201
  297. data/THIRD_PARTY_LICENSES/regress-0.10.5/LICENSE-MIT +0 -21
  298. data/THIRD_PARTY_LICENSES/ryu-1.0.22/LICENSE-APACHE +0 -176
  299. data/THIRD_PARTY_LICENSES/ryu-1.0.22/LICENSE-BOOST +0 -23
  300. data/THIRD_PARTY_LICENSES/scoped-tls-1.0.1/LICENSE-APACHE +0 -201
  301. data/THIRD_PARTY_LICENSES/scoped-tls-1.0.1/LICENSE-MIT +0 -25
  302. data/THIRD_PARTY_LICENSES/siphasher-0.3.11/COPYING +0 -7
  303. data/THIRD_PARTY_LICENSES/smallvec-1.15.1/LICENSE-APACHE +0 -201
  304. data/THIRD_PARTY_LICENSES/smartstring-1.0.1/LICENCE.md +0 -355
  305. data/THIRD_PARTY_LICENSES/st-map-0.2.4/LICENSE +0 -202
  306. data/THIRD_PARTY_LICENSES/static-map-macro-0.3.4/LICENSE +0 -202
  307. data/THIRD_PARTY_LICENSES/string_enum-1.0.2/LICENSE +0 -201
  308. data/THIRD_PARTY_LICENSES/strsim-0.11.1/LICENSE +0 -23
  309. data/THIRD_PARTY_LICENSES/swc-52.0.0/LICENSE +0 -201
  310. data/THIRD_PARTY_LICENSES/swc_allocator-4.0.1/LICENSE +0 -201
  311. data/THIRD_PARTY_LICENSES/swc_atoms-9.0.0/LICENSE +0 -201
  312. data/THIRD_PARTY_LICENSES/swc_common-18.0.1/LICENSE +0 -201
  313. data/THIRD_PARTY_LICENSES/swc_compiler_base-45.0.0/LICENSE +0 -201
  314. data/THIRD_PARTY_LICENSES/swc_config-3.1.2/LICENSE +0 -201
  315. data/THIRD_PARTY_LICENSES/swc_config_macro-1.0.1/LICENSE +0 -201
  316. data/THIRD_PARTY_LICENSES/swc_core-54.0.0/LICENSE +0 -201
  317. data/THIRD_PARTY_LICENSES/swc_ecma_ast-19.0.0/LICENSE +0 -201
  318. data/THIRD_PARTY_LICENSES/swc_ecma_codegen-21.0.0/LICENSE +0 -201
  319. data/THIRD_PARTY_LICENSES/swc_ecma_codegen_macros-2.0.2/LICENSE +0 -201
  320. data/THIRD_PARTY_LICENSES/swc_ecma_compat_bugfixes-39.0.0/LICENSE +0 -201
  321. data/THIRD_PARTY_LICENSES/swc_ecma_compat_common-30.0.0/LICENSE +0 -201
  322. data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2015-39.0.0/LICENSE +0 -201
  323. data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2016-35.0.0/LICENSE +0 -201
  324. data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2017-35.0.0/LICENSE +0 -201
  325. data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2018-36.0.0/LICENSE +0 -201
  326. data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2019-35.0.0/LICENSE +0 -201
  327. data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2020-37.0.0/LICENSE +0 -201
  328. data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2021-35.0.0/LICENSE +0 -201
  329. data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2022-37.0.0/LICENSE +0 -201
  330. data/THIRD_PARTY_LICENSES/swc_ecma_compat_es3-26.0.0/LICENSE +0 -201
  331. data/THIRD_PARTY_LICENSES/swc_ecma_ext_transforms-25.0.0/LICENSE +0 -201
  332. data/THIRD_PARTY_LICENSES/swc_ecma_hooks-0.3.0/LICENSE +0 -201
  333. data/THIRD_PARTY_LICENSES/swc_ecma_loader-18.0.0/LICENSE +0 -201
  334. data/THIRD_PARTY_LICENSES/swc_ecma_minifier-42.0.0/LICENSE +0 -201
  335. data/THIRD_PARTY_LICENSES/swc_ecma_parser-32.0.0/LICENSE +0 -201
  336. data/THIRD_PARTY_LICENSES/swc_ecma_preset_env-45.0.0/LICENSE +0 -201
  337. data/THIRD_PARTY_LICENSES/swc_ecma_transformer-6.0.0/LICENSE +0 -201
  338. data/THIRD_PARTY_LICENSES/swc_ecma_transforms-44.0.0/LICENSE +0 -201
  339. data/THIRD_PARTY_LICENSES/swc_ecma_transforms_base-35.0.0/LICENSE +0 -201
  340. data/THIRD_PARTY_LICENSES/swc_ecma_transforms_classes-35.0.0/LICENSE +0 -201
  341. data/THIRD_PARTY_LICENSES/swc_ecma_transforms_compat-40.0.0/LICENSE +0 -201
  342. data/THIRD_PARTY_LICENSES/swc_ecma_transforms_macros-1.0.1/LICENSE +0 -201
  343. data/THIRD_PARTY_LICENSES/swc_ecma_transforms_module-38.0.0/LICENSE +0 -201
  344. data/THIRD_PARTY_LICENSES/swc_ecma_transforms_optimization-37.0.0/LICENSE +0 -201
  345. data/THIRD_PARTY_LICENSES/swc_ecma_transforms_proposal-35.0.0/LICENSE +0 -201
  346. data/THIRD_PARTY_LICENSES/swc_ecma_transforms_react-38.0.0/LICENSE +0 -201
  347. data/THIRD_PARTY_LICENSES/swc_ecma_transforms_typescript-38.0.0/LICENSE +0 -201
  348. data/THIRD_PARTY_LICENSES/swc_ecma_usage_analyzer-27.0.0/LICENSE +0 -201
  349. data/THIRD_PARTY_LICENSES/swc_ecma_utils-25.0.0/LICENSE +0 -201
  350. data/THIRD_PARTY_LICENSES/swc_ecma_visit-19.0.0/LICENSE +0 -201
  351. data/THIRD_PARTY_LICENSES/swc_eq_ignore_macros-1.0.1/LICENSE +0 -201
  352. data/THIRD_PARTY_LICENSES/swc_error_reporters-20.0.0/LICENSE +0 -201
  353. data/THIRD_PARTY_LICENSES/swc_macros_common-1.0.1/LICENSE +0 -201
  354. data/THIRD_PARTY_LICENSES/swc_node_comments-18.0.0/LICENSE +0 -201
  355. data/THIRD_PARTY_LICENSES/swc_timer-1.0.0/LICENSE +0 -201
  356. data/THIRD_PARTY_LICENSES/swc_trace_macro-2.0.2/LICENSE +0 -201
  357. data/THIRD_PARTY_LICENSES/swc_transform_common-12.0.0/LICENSE +0 -201
  358. data/THIRD_PARTY_LICENSES/swc_visit-2.0.1/LICENSE +0 -201
  359. data/THIRD_PARTY_LICENSES/tap-1.0.1/LICENSE.txt +0 -21
  360. data/THIRD_PARTY_LICENSES/tracing-0.1.44/LICENSE +0 -25
  361. data/THIRD_PARTY_LICENSES/tracing-attributes-0.1.31/LICENSE +0 -25
  362. data/THIRD_PARTY_LICENSES/tracing-core-0.1.36/LICENSE +0 -25
  363. data/THIRD_PARTY_LICENSES/triomphe-0.1.15/LICENSE-APACHE +0 -201
  364. data/THIRD_PARTY_LICENSES/triomphe-0.1.15/LICENSE-MIT +0 -27
  365. data/THIRD_PARTY_LICENSES/unicode-width-0.1.14/LICENSE-APACHE +0 -201
  366. data/THIRD_PARTY_LICENSES/unicode-width-0.1.14/LICENSE-MIT +0 -25
  367. data/THIRD_PARTY_LICENSES/uuid-1.19.0/LICENSE-APACHE +0 -201
  368. data/THIRD_PARTY_LICENSES/uuid-1.19.0/LICENSE-MIT +0 -26
  369. data/THIRD_PARTY_LICENSES/vergen-9.0.6/LICENSE-APACHE +0 -201
  370. data/THIRD_PARTY_LICENSES/vergen-9.0.6/LICENSE-MIT +0 -25
  371. data/THIRD_PARTY_LICENSES/vergen-lib-0.1.6/LICENSE-APACHE +0 -201
  372. data/THIRD_PARTY_LICENSES/vergen-lib-0.1.6/LICENSE-MIT +0 -25
  373. data/THIRD_PARTY_LICENSES/wyz-0.5.1/LICENSE.txt +0 -21
  374. data/vendor/cfsolve/src/deobfuscate/analysis/globals.rs +0 -33
  375. data/vendor/cfsolve/src/deobfuscate/analysis/names.rs +0 -52
  376. data/vendor/cfsolve/src/listing/normalize.rs +0 -264
  377. data/vendor/cfsolve/src/stage2/analysis/outputs.rs +0 -327
  378. data/vendor/cfsolve/src/stage2/probes/stylesheets.rs +0 -249
  379. data/vendor/cfsolve/src/stage2/reference-expanded.json +0 -42
  380. data/vendor/cfsolve/src/stage2/session_names.rs +0 -470
  381. /data/THIRD_PARTY_LICENSES/{derive_builder-0.20.2 → cobs-0.3.0}/LICENSE-APACHE +0 -0
  382. /data/THIRD_PARTY_LICENSES/{compact_str-0.7.1 → compact_str-0.10.0}/LICENSE +0 -0
  383. /data/THIRD_PARTY_LICENSES/{ahash-0.8.12 → embedded-io-0.4.0}/LICENSE-APACHE +0 -0
  384. /data/THIRD_PARTY_LICENSES/{arrayvec-0.7.6 → embedded-io-0.6.1}/LICENSE-APACHE +0 -0
  385. /data/THIRD_PARTY_LICENSES/{normpath-0.2.0 → escape-simd-0.1.1}/LICENSE-APACHE +0 -0
  386. /data/THIRD_PARTY_LICENSES/{bstr-1.12.1 → itertools-0.15.0}/LICENSE-APACHE +0 -0
  387. /data/THIRD_PARTY_LICENSES/{petgraph-0.7.1 → itertools-0.15.0}/LICENSE-MIT +0 -0
  388. /data/THIRD_PARTY_LICENSES/{itoa-1.0.17 → itoa-1.0.19}/LICENSE-APACHE +0 -0
  389. /data/THIRD_PARTY_LICENSES/{camino-1.2.2 → itoa-1.0.19}/LICENSE-MIT +0 -0
  390. /data/THIRD_PARTY_LICENSES/{globset-0.4.18 → memchr-2.8.3}/COPYING +0 -0
  391. /data/THIRD_PARTY_LICENSES/{globset-0.4.18 → memchr-2.8.3}/LICENSE-MIT +0 -0
  392. /data/THIRD_PARTY_LICENSES/{camino-1.2.2 → num-bigint-0.5.1}/LICENSE-APACHE +0 -0
  393. /data/THIRD_PARTY_LICENSES/{swc_sourcemap-9.3.4 → oxc_sourcemap-9.0.0}/LICENSE +0 -0
  394. /data/THIRD_PARTY_LICENSES/{phf-0.11.3 → phf-0.14.0}/LICENSE +0 -0
  395. /data/THIRD_PARTY_LICENSES/{phf_generator-0.11.3 → phf_generator-0.14.0}/LICENSE +0 -0
  396. /data/THIRD_PARTY_LICENSES/{phf_macros-0.11.3 → phf_macros-0.14.0}/LICENSE +0 -0
  397. /data/THIRD_PARTY_LICENSES/{phf_shared-0.11.3 → phf_shared-0.14.0}/LICENSE +0 -0
  398. /data/THIRD_PARTY_LICENSES/{fixedbitset-0.5.7 → postcard-1.1.3}/LICENSE-APACHE +0 -0
  399. /data/THIRD_PARTY_LICENSES/{hashbrown-0.14.5 → smallvec-1.16.5}/LICENSE-APACHE +0 -0
  400. /data/THIRD_PARTY_LICENSES/{smallvec-1.15.1 → smallvec-1.16.5}/LICENSE-MIT +0 -0
  401. /data/THIRD_PARTY_LICENSES/{unicode-width-0.1.14 → unicode-segmentation-1.13.3}/COPYRIGHT +0 -0
  402. /data/THIRD_PARTY_LICENSES/{hashbrown-0.16.1 → unicode-segmentation-1.13.3}/LICENSE-APACHE +0 -0
  403. /data/THIRD_PARTY_LICENSES/{heck-0.5.0 → unicode-segmentation-1.13.3}/LICENSE-MIT +0 -0
  404. /data/THIRD_PARTY_LICENSES/{cargo-platform-0.1.9 → zmij_ecma-1.0.23}/LICENSE-MIT +0 -0
@@ -2,595 +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(literal) if !matches!(literal, Lit::Regex(_)) => {
78
- self.literals.insert(name, value.clone());
79
- }
80
- Expr::Tpl(template) if template.exprs.is_empty() => {
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
- }
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
+ })
371
288
  }
372
289
 
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))
395
- }
396
-
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
- Expr::Unary(unary) if unary.op != UnaryOp::Delete => eager_operands(&unary.arg),
493
- Expr::Bin(binary) if !binary.op.may_short_circuit() => {
494
- Some(vec![&binary.left, &binary.right])
495
- }
496
- // The property key is converted after both operands are evaluated.
497
- Expr::Member(member) => match &member.prop {
498
- MemberProp::Ident(_) => Some(vec![&member.obj]),
499
- MemberProp::Computed(key) => Some(vec![&member.obj, &key.expr]),
500
- MemberProp::PrivateName(_) => None,
501
- },
502
- Expr::Call(call)
503
- if let Callee::Expr(callee) = &call.callee
504
- && call.args.iter().all(|argument| argument.spread.is_none()) =>
505
- {
506
- Some(
507
- iter::once(&**callee)
508
- .chain(call.args.iter().map(|argument| &*argument.expr))
509
- .collect(),
510
- )
511
- }
512
- _ => 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;
513
375
  }
514
- }
515
-
516
- /// Substitutes arguments for `function` parameters in `result`. Returns `None` unless
517
- /// parameters are distinct plain identifiers.
518
- #[must_use]
519
- fn substitute(function: &FnExpr, result: &Expr, args: &[ExprOrSpread]) -> Option<Expr> {
520
- let mut parameters = HashMap::default();
521
- let bound = function.function.params.iter().zip(args);
522
- for (parameter, argument) in bound {
523
- let parameter = parameter.pat.as_ident()?;
524
- if parameters
525
- .insert(parameter.to_id(), &*argument.expr)
526
- .is_some()
527
- {
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() {
528
396
  return None;
529
397
  }
530
- }
531
- Some(replace_parameters(result, parameters))
532
- }
533
-
534
- /// Substitutes parenthesized arguments for parameter references and parenthesizes the result.
535
- /// Preserves calls without a receiver; see [`needs_detaching`].
536
- #[must_use]
537
- pub(super) fn replace_parameters(expression: &Expr, parameters: HashMap<Id, &Expr>) -> Expr {
538
- let mut replacement = expression.clone();
539
- replacement.visit_mut_with(&mut ParameterReplacer { parameters });
540
- ast::parenthesized(Box::new(replacement))
541
- }
542
-
543
- struct ParameterReplacer<'arguments> {
544
- parameters: HashMap<Id, &'arguments Expr>,
545
- }
546
-
547
- impl VisitMut for ParameterReplacer<'_> {
548
- fn visit_mut_call_expr(&mut self, call: &mut CallExpr) {
549
- use swc_core::common::Spanned;
550
-
551
- let parameter_callee = matches!(&call.callee, Callee::Expr(callee)
552
- if callee.as_ident().is_some_and(|id| self.parameters.contains_key(&id.to_id())));
553
- call.visit_mut_children_with(self);
554
- if parameter_callee
555
- && let Callee::Expr(callee) = &mut call.callee
556
- && needs_detaching(callee.unwrap_parens())
557
- {
558
- // The wrapper calls a parameter as a value. Substituting `obj.method`
559
- // must not bind `obj` as `this`, or turn a passed `eval` into direct eval.
560
- **callee = Expr::Seq(SeqExpr {
561
- span: callee.span(),
562
- exprs: vec![
563
- Box::new(Expr::Lit(Lit::Num(Number {
564
- span: callee.span(),
565
- value: 0.0,
566
- raw: None,
567
- }))),
568
- callee.clone(),
569
- ],
570
- });
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)
571
406
  }
572
- }
573
-
574
- fn visit_mut_expr(&mut self, expression: &mut Expr) {
575
- expression.visit_mut_children_with(self);
576
-
577
- if let Expr::Ident(identifier) = expression
578
- && let Some(value) = self.parameters.get(&identifier.to_id())
579
- {
580
- *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
+ }
581
413
  }
582
414
  }
583
- }
584
-
585
- /// Checks whether a substituted callee needs `(0, callee)` to preserve call behavior. Member
586
- /// references would otherwise receive `this`, and `eval` could become a direct eval call. A
587
- /// local named `eval` may also hold the built-in function.
588
- fn needs_detaching(callee: &Expr) -> bool {
589
- match callee {
590
- Expr::Member(_) | Expr::SuperProp(_) | Expr::OptChain(_) => true,
591
- Expr::Ident(identifier) => identifier.sym == "eval",
592
- _ => false,
593
- }
415
+ let values = parameters.into_iter().zip(arguments).collect();
416
+ Some(ast::replace_parameters(value, &values, analysis))
594
417
  }
595
418
 
596
419
  #[cfg(test)]
@@ -600,42 +423,42 @@ mod tests {
600
423
  /// Helper-call test cases and expected results. Calls remain unchanged when inlining could
601
424
  /// alter evaluation or its order. `set()` writes the parameters read by the arguments;
602
425
  /// `first()` and `second()` have unknown effects.
603
- const CALLS: [(&str, &str, &str); 27] = [
604
- // 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.
605
428
  (
606
429
  "function(a, b) { return a & b; }",
607
430
  "h.op(local >>> 16, 65535) * 8133",
608
- "((local >>> 16) & (65535)) * 8133",
431
+ "(local >>> 16 & 65535) * 8133",
609
432
  ),
610
433
  // Eager operators evaluate their operands in argument order.
611
434
  (
612
435
  "function(a, b) { return a + b; }",
613
436
  "h.op(values[i++], first())",
614
- "(values[i++]) + (first())",
437
+ "values[i++] + first()",
615
438
  ),
616
439
  // Reordering is safe when only one operand has effects and the other reads no affected
617
440
  // binding.
618
441
  (
619
442
  "function(a, b) { return b - a; }",
620
443
  "h.op(values[i++], 2)",
621
- "(2) - (values[i++])",
444
+ "2 - values[i++]",
622
445
  ),
623
446
  (
624
447
  "function(a, b) { return b === a; }",
625
448
  "h.op(typeof values, 'object')",
626
- "('object') === (typeof values)",
449
+ r#""object" === typeof values"#,
627
450
  ),
628
451
  (
629
452
  "function(a, b) { return b | a; }",
630
453
  "h.op(first(), 2)",
631
- "(2) | (first())",
454
+ "2 | first()",
632
455
  ),
633
456
  // An effect-free operand may move past a write only if it does not read the written
634
457
  // binding.
635
458
  (
636
459
  "function(a, b) { return b | a; }",
637
460
  "h.op(values[i++], local)",
638
- "(local) | (values[i++])",
461
+ "local | values[i++]",
639
462
  ),
640
463
  (
641
464
  "function(a, b) { return b | a; }",
@@ -657,17 +480,22 @@ mod tests {
657
480
  "h.op(first(), second())",
658
481
  "h.op(first(), second())",
659
482
  ),
483
+ (
484
+ "function(a, b) { return b + a; }",
485
+ "h.op(v, [v] = [2])",
486
+ "h.op(v, [v] = [2])",
487
+ ),
660
488
  // An outer unary operator runs after both operands, so `!(b !== a)` permits the same
661
489
  // reordering as `b === a`. `delete` has different operand evaluation rules.
662
490
  (
663
491
  "function(aX, ae) { return !(ae !== aX); }",
664
492
  "h.op(q._cf_chl_opt.EcMJs8, 1)",
665
- "!((1) !== (q._cf_chl_opt.EcMJs8))",
493
+ "!(1 !== q._cf_chl_opt.EcMJs8)",
666
494
  ),
667
495
  (
668
496
  "function(a, b) { return -(b - a); }",
669
497
  "h.op(q.x, 2)",
670
- "-((2) - (q.x))",
498
+ "-(2 - q.x)",
671
499
  ),
672
500
  (
673
501
  "function(aX, ae) { return !(ae !== aX); }",
@@ -683,7 +511,7 @@ mod tests {
683
511
  (
684
512
  "function(a) { return a.key; }",
685
513
  "h.op(first())",
686
- "(first()).key",
514
+ "first().key",
687
515
  ),
688
516
  (
689
517
  "function(a, b) { return b[a]; }",
@@ -694,12 +522,12 @@ mod tests {
694
522
  (
695
523
  "function(rn, rw, ri, ru, KA) { return rn(rw, ri, ru); }",
696
524
  "h.op(local, 1, 2, 3)",
697
- "(local)((1), (2), (3))",
525
+ "local(1, 2, 3)",
698
526
  ),
699
527
  (
700
528
  "function(a, b, extra) { return b | a; }",
701
529
  "h.op(local, 2, 3)",
702
- "(2) | (local)",
530
+ "2 | local",
703
531
  ),
704
532
  (
705
533
  "function(a, b, extra) { return extra + b; }",
@@ -718,7 +546,7 @@ mod tests {
718
546
  "h.op(missing, 1)",
719
547
  "h.op(missing, 1)",
720
548
  ),
721
- ("function(a, b) { return b; }", "h.op(Math, 1)", "((1))"),
549
+ ("function(a, b) { return b; }", "h.op(Math, 1)", "return 1;"),
722
550
  // Tests bodies that skip, repeat, or reorder operands within larger expressions.
723
551
  (
724
552
  "function(a, b) { return a + a; }",
@@ -748,7 +576,7 @@ mod tests {
748
576
  ];
749
577
 
750
578
  fn transform(source: &str) -> String {
751
- testing::rewrite(source, super::run, /* fix= */ false)
579
+ testing::rewrite(source, super::run)
752
580
  }
753
581
 
754
582
  #[test]
@@ -770,47 +598,29 @@ mod tests {
770
598
  return h.invoke(eval, "typeof local");
771
599
  }"#,
772
600
  );
773
- testing::assert_contains(&output, &["(0, obj.method)(effect())", "plain(1);"], &[]);
601
+ testing::assert_contains(&output, &["(0, obj.method)(effect())", "plain(1);"]);
774
602
  let expected = ["(0, eval)(\"typeof local\")", "Number(obj.value);"];
775
- testing::assert_contains(&output, &expected, &[]);
603
+ testing::assert_contains(&output, &expected);
776
604
  assert!(!output.contains("(0, plain)"), "{output}");
777
605
  }
778
606
 
779
- #[test]
780
- fn inlines_static_template_properties_without_evaluating_interpolation() {
781
- let output = transform(
782
- r"function f() {
783
- var helper;
784
- helper = { first: `fixed` };
785
- helper.second = `line\n\u{1f600}\ud800`;
786
- helper.dynamic = `value:${read()}`;
787
- return function() { return [helper.first, helper.second, helper.dynamic]; };
788
- }",
789
- );
790
- testing::assert_contains(
791
- &output,
792
- &[],
793
- &[r"return [`fixed`, `line\n\u{1f600}\ud800`, helper.dynamic]"],
794
- );
795
- let output = transform(
796
- r"function f() {
797
- var helper;
798
- helper = { name: `before` };
799
- helper.name = `after`;
800
- return helper.name;
801
- }",
802
- );
803
- assert!(output.contains("return helper.name;"), "{output}");
804
- }
805
-
806
607
  #[test]
807
608
  fn inlines_member_assigned_properties_and_helper_functions() {
808
609
  let output = transform(
809
610
  "function f(h, p) { h = {}; h.op = function (a, b) { return a + b; }; \
810
611
  h.name = \"n\"; p = h; return [p.op(1, 2), p.name, h.name]; }",
811
612
  );
812
- assert!(output.contains("(1) + (2)"), "{output}");
613
+ assert!(output.contains("1 + 2"), "{output}");
813
614
  testing::assert_lacks(&output, &["p.name", "h.name]"]);
615
+ // Static templates inline into later callbacks, as in a payload built after a delay.
616
+ let output = transform(
617
+ "function f(h, g) { h = { key: `_field` }; g(function () { return w[h.key]; }); }",
618
+ );
619
+ assert!(output.contains("w[`_field`]"), "{output}");
620
+ // A unary helper evaluates its argument first either way, even a member read.
621
+ let output =
622
+ transform("function f(n) { n = function (a) { return !a; }; if (n(w.x)) g(); }");
623
+ assert!(output.contains("!w.x"), "{output}");
814
624
  // Helper functions inline when they name only what callers see.
815
625
  let output = transform(
816
626
  "function f(add, call, read, g) { add = function(a, b) { return a + b; }; \
@@ -818,21 +628,21 @@ mod tests {
818
628
  g = function(local) { read = function() { return local; }; }; \
819
629
  g(5); return [call(2), add(3, 4), read()]; }",
820
630
  );
821
- let expected = ["((2)) + (1))", "Math.max((2))", "(3) + (4)", "read()]"];
822
- testing::assert_contains(&output, &[], &expected);
631
+ let expected = ["2 + 1", "Math.max(2)", "3 + 4", "read()]"];
632
+ testing::assert_contains(&output, &expected);
823
633
  // Function declarations inside function bodies are also stable; top-level declarations are
824
634
  // not.
825
635
  let output = transform(
826
636
  "function f(x) { return g(x, 1); function g(a, b) { return a + b; } } \
827
637
  function h(a) { return a; } k(h(2));",
828
638
  );
829
- testing::assert_contains(&output, &[], &["return ((x) + (1));", "k(h(2));"]);
639
+ testing::assert_contains(&output, &["return x + 1;", "k(h(2));"]);
830
640
  // Copies of helper methods are stable too.
831
641
  let output = transform(
832
642
  "function f(h, m) { h = { shr: function (a, b) { return a >> b; } }; m = h.shr; \
833
643
  return m(8, 1); }",
834
644
  );
835
- assert!(output.contains("(8) >> (1)"), "{output}");
645
+ assert!(output.contains("8 >> 1"), "{output}");
836
646
  }
837
647
 
838
648
  #[test]
@@ -852,11 +662,11 @@ mod tests {
852
662
  return [alias.add(1, 2), alias.name, h.add(1, 2)];
853
663
  }"#,
854
664
  );
855
- 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)"]);
856
666
  assert!(output.matches("h.name").count() == 2, "{output}");
857
- testing::assert_contains(&output, &["alias.add(1, 2)", "alias.name"], &[]);
667
+ testing::assert_contains(&output, &["alias.add(1, 2)", "alias.name"]);
858
668
  assert!(
859
- !output.contains("(1) + (2)"),
669
+ !output.contains("1 + 2"),
860
670
  "`h` is copied into a binding that is assigned again, which could change it before \
861
671
  that, so its properties are not trusted either: {output}"
862
672
  );