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
@@ -1,141 +1,346 @@
1
- //! JavaScript parsing, syntax matchers, and node constructors shared by extraction and
2
- //! deobfuscation.
1
+ //! Oxc parsing, semantic analysis, and syntax matchers used by extraction and the VM.
3
2
  //!
4
- //! Matchers accept different syntax depending on the extractor's requirements.
3
+ //! Every parsed script carries its [`Scoping`], so code identifies bindings by [`SymbolId`].
4
+ //! Names identify only unresolved references, which are globals. Matchers accept different
5
+ //! syntax depending on the extractor's requirements.
5
6
 
6
7
  use std::collections::BTreeSet;
8
+ use std::{fmt, mem};
7
9
 
8
10
  use anyhow::{Result, ensure};
9
- use swc_core::atoms::Wtf8Atom;
10
- use swc_core::common::sync::Lrc;
11
- use swc_core::common::{
12
- BytePos, DUMMY_SP, FileName, GLOBALS, Globals, Mark, SourceFile, SourceMap, Spanned,
13
- SyntaxContext,
11
+ use oxc::allocator::Allocator;
12
+ use oxc::ast::AstKind;
13
+ use oxc::ast::ast::{
14
+ AccessorProperty, Argument, AssignmentTarget, BindingPattern, BindingProperty, Directive,
15
+ Expression, Function, IdentifierReference, MemberExpression, MethodDefinition,
16
+ ObjectExpression, ObjectProperty, ObjectPropertyKind, Program, PropertyDefinition, PropertyKey,
17
+ PropertyKind,
14
18
  };
15
- use swc_core::ecma::ast::{
16
- AssignExpr, AssignOp, BinaryOp, BlockStmt, Expr, ExprOrSpread, ExprStmt, FnDecl, Function, Id,
17
- Ident, KeyValueProp, Lit, MemberExpr, MemberProp, ObjectLit, ParenExpr, Pat, PropName,
18
- PropOrSpread, Script, Stmt, UnaryOp, VarDeclarator,
19
- };
20
- use swc_core::ecma::parser::{Parser, StringInput, Syntax};
21
- use swc_core::ecma::visit::{Visit, VisitMutWith, VisitWith};
22
- use swc_ecma_transforms::resolver;
19
+ use oxc::ast_visit::{Visit, walk};
20
+ use oxc::parser::{ParseOptions, Parser};
21
+ use oxc::semantic::{AstNodes, Scoping, SemanticBuilder, SymbolId};
22
+ use oxc::span::{GetSpan, SourceType};
23
+ use oxc::str::JSStr;
24
+ use oxc::syntax::node::GetNodeId;
25
+ use oxc::syntax::operator::{AssignmentOperator, BinaryOperator, UnaryOperator};
26
+ use oxc::syntax::scope::ScopeFlags;
27
+ use self_cell::self_cell;
23
28
 
24
29
  use crate::engine::{self, Lines};
25
30
 
26
- /// A parsed script and its original source text.
27
- pub(crate) struct ParsedScript {
28
- pub script: Script,
29
- file: Lrc<SourceFile>,
31
+ /// The arena and source text a [`Parsed`] tree borrows.
32
+ struct Owner {
33
+ allocator: Allocator,
34
+ source: String,
35
+ }
36
+
37
+ /// A tree, allocated in its arena, and its semantic analysis.
38
+ struct Parsed<'a> {
39
+ program: &'a Program<'a>,
40
+ scoping: Scoping,
41
+ nodes: AstNodes<'a>,
30
42
  }
31
43
 
44
+ self_cell!(
45
+ struct Cell {
46
+ owner: Owner,
47
+ #[covariant]
48
+ dependent: Parsed,
49
+ }
50
+ );
51
+
52
+ /// A parsed script that owns its arena, source text, and semantic analysis. See [`Script`] for
53
+ /// the borrowed view extractors use.
54
+ pub(crate) struct ParsedScript(Cell);
55
+
56
+ // SAFETY: Oxc trees are `!Send` and `!Sync` because their arena vectors can allocate through a
57
+ // shared `&Allocator`, and their nodes hold semantic IDs in `Cell`s. A `ParsedScript` owns the
58
+ // arena together with every node allocated in it, so moving it moves them all, and it never
59
+ // exposes the `Allocator`. Semantic IDs are written only by `SemanticBuilder` while the script is
60
+ // built; afterwards the tree is only read, so shared references cannot race.
61
+ unsafe impl Send for ParsedScript {}
62
+ // SAFETY: See `Send` above: after construction nothing allocates in the arena or writes a node.
63
+ unsafe impl Sync for ParsedScript {}
64
+
32
65
  impl ParsedScript {
33
- /// Returns the original source text for `node`.
66
+ /// Parses `source` as a classic script, using `what` to identify it in errors. Rejects
67
+ /// recoverable parse errors and early errors. Parentheses remain in the tree.
68
+ pub(crate) fn parse(what: &str, source: &str) -> Result<Self> {
69
+ Self::build(what, source.to_owned(), |allocator, source| {
70
+ let parsed = Parser::new(allocator, source, SourceType::script())
71
+ .with_options(ParseOptions::default())
72
+ .parse();
73
+ ensure!(
74
+ parsed.diagnostics.is_empty(),
75
+ "cannot parse {what}: {:?}",
76
+ parsed.diagnostics
77
+ );
78
+ Ok(parsed.program)
79
+ })
80
+ }
81
+
82
+ /// Builds a script from the tree `build` returns for `source` and analyzes its scopes.
83
+ /// `build` may rewrite the tree, such as by decoding strings; spans keep pointing into
84
+ /// `source`.
85
+ pub(crate) fn build(
86
+ what: &str,
87
+ source: String,
88
+ build: impl for<'a> FnOnce(&'a Allocator, &'a str) -> Result<Program<'a>>,
89
+ ) -> Result<Self> {
90
+ let owner = Owner {
91
+ allocator: Allocator::default(),
92
+ source,
93
+ };
94
+ Cell::try_new(owner, |owner| {
95
+ let program = owner
96
+ .allocator
97
+ .alloc(build(&owner.allocator, &owner.source)?);
98
+ let semantic = SemanticBuilder::new()
99
+ .with_check_syntax_error(true)
100
+ .with_build_nodes(true)
101
+ .build(program);
102
+ ensure!(
103
+ semantic.diagnostics.is_empty(),
104
+ "{what} has early errors: {:?}",
105
+ semantic.diagnostics
106
+ );
107
+ let (scoping, nodes) = semantic.semantic.into_scoping_and_nodes();
108
+ Ok(Parsed {
109
+ program,
110
+ scoping,
111
+ nodes,
112
+ })
113
+ })
114
+ .map(Self)
115
+ }
116
+
117
+ #[must_use]
118
+ pub(crate) fn script(&self) -> Script<'_> {
119
+ let parsed = self.0.borrow_dependent();
120
+ Script {
121
+ program: parsed.program,
122
+ scoping: &parsed.scoping,
123
+ nodes: &parsed.nodes,
124
+ source: &self.0.borrow_owner().source,
125
+ }
126
+ }
127
+ }
128
+
129
+ /// A program with its scopes, semantic nodes, and source text. Spans are byte offsets into the
130
+ /// source. [`AstNodes`] list every node in source order with its parent, and lend nodes for the
131
+ /// program's lifetime, unlike [`Visit`], whose borrows end with each visit.
132
+ #[derive(Clone, Copy)]
133
+ pub(crate) struct Script<'a> {
134
+ pub program: &'a Program<'a>,
135
+ pub scoping: &'a Scoping,
136
+ pub nodes: &'a AstNodes<'a>,
137
+ pub source: &'a str,
138
+ }
139
+
140
+ /// What an identifier reference resolves to: a binding in the script, or a global by name.
141
+ #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
142
+ pub(crate) enum Binding<'a> {
143
+ Symbol(SymbolId),
144
+ Global(&'a str),
145
+ }
146
+
147
+ impl<'a> Script<'a> {
148
+ /// Returns the original source text of `node`.
34
149
  ///
35
150
  /// # Panics
36
151
  ///
37
152
  /// Panics if `node` was not parsed from this script.
38
153
  #[must_use]
39
- pub(crate) fn text(&self, node: &dyn Spanned) -> &str {
40
- let span = node.span();
41
- let offset = |position: BytePos| (position.0 - self.file.start_pos.0) as usize;
42
- &self.file.src[offset(span.lo)..offset(span.hi)]
154
+ pub(crate) fn text(&self, node: &impl GetSpan) -> &'a str {
155
+ node.span().source_text(self.source)
43
156
  }
44
157
 
45
- /// Returns the one-based line and UTF-16 column at `at`, matching V8 stack traces.
158
+ /// Returns the one-based line and UTF-16 column of byte offset `at`, matching V8 stack
159
+ /// traces.
46
160
  ///
47
161
  /// # Panics
48
162
  ///
49
163
  /// Panics if `at` is outside this script.
50
164
  #[must_use]
51
- pub(crate) fn position(&self, at: BytePos) -> (usize, usize) {
52
- let before = &self.file.src[..(at.0 - self.file.start_pos.0) as usize];
53
- let (line, column) = engine::line_and_column(before, Lines::Script);
165
+ pub(crate) fn position(&self, at: u32) -> (usize, usize) {
166
+ let (line, column) = engine::line_and_column(&self.source[..at as usize], Lines::Script);
54
167
  (line as usize + 1, column as usize + 1)
55
168
  }
56
- }
57
169
 
58
- /// Parses the deobfuscated iframe script and resolves its scopes, so a binding's [`Id`]
59
- /// identifies it even where other scopes reuse its name.
60
- pub(crate) fn parse_iframe_javascript(source: &str) -> Result<ParsedScript> {
61
- let mut parsed = parse_javascript("deobfuscated iframe", source)?;
62
- GLOBALS.set(&Globals::new(), || {
63
- let mut resolve = resolver(Mark::new(), Mark::new(), false);
64
- parsed.script.visit_mut_with(&mut resolve);
65
- });
66
- Ok(parsed)
67
- }
170
+ /// The binding `reference` resolves to, or `None` for a global.
171
+ #[must_use]
172
+ pub(crate) fn symbol(&self, reference: &IdentifierReference<'_>) -> Option<SymbolId> {
173
+ self.scoping
174
+ .get_reference(reference.reference_id())
175
+ .symbol_id()
176
+ }
68
177
 
69
- /// Parses `source`, using `what` to identify it in errors.
70
- pub(crate) fn parse_javascript(what: &str, source: &str) -> Result<ParsedScript> {
71
- let source_map = SourceMap::default();
72
- let file = source_map.new_source_file(FileName::Custom(what.into()).into(), source.to_owned());
73
- let mut parser = Parser::new(Syntax::default(), StringInput::from(&*file), None);
74
- let script = parser
75
- .parse_script()
76
- .map_err(|error| anyhow::anyhow!("cannot parse {what}: {error:?}"))?;
77
- ensure!(parser.take_errors().is_empty(), "{what} has parse errors");
78
- Ok(ParsedScript { script, file })
79
- }
178
+ /// What `reference` resolves to.
179
+ #[must_use]
180
+ pub(crate) fn binding(&self, reference: &IdentifierReference<'a>) -> Binding<'a> {
181
+ match self.symbol(reference) {
182
+ Some(symbol) => Binding::Symbol(symbol),
183
+ None => Binding::Global(reference.name.as_str()),
184
+ }
185
+ }
80
186
 
81
- /// Parses one expression, without its surrounding parentheses.
82
- #[cfg(test)]
83
- pub(crate) fn parse_expression(what: &str, text: &str) -> Result<Expr> {
84
- // Parentheses make leading braces parse as an object literal.
85
- let parsed = parse_javascript(what, &format!("({text})"))?;
86
- let [Stmt::Expr(statement)] = parsed.script.body.as_slice() else {
87
- anyhow::bail!("{what} is not one expression");
88
- };
89
- Ok(statement.expr.unwrap_parens().clone())
187
+ /// Whether `expression` is a reference to the global `name`, which no binding shadows.
188
+ #[must_use]
189
+ pub(crate) fn is_global(&self, expression: &Expression<'_>, name: &str) -> bool {
190
+ matches!(expression, Expression::Identifier(reference)
191
+ if reference.name == name && self.symbol(reference).is_none())
192
+ }
193
+
194
+ /// Whether `expression` is a reference to `symbol`.
195
+ #[must_use]
196
+ pub(crate) fn refers_to(&self, expression: &Expression<'_>, symbol: SymbolId) -> bool {
197
+ matches!(expression, Expression::Identifier(reference)
198
+ if self.symbol(reference) == Some(symbol))
199
+ }
200
+
201
+ /// Whether an expression is a `Date.now()` call on the global `Date`.
202
+ #[must_use]
203
+ pub(crate) fn is_date_now(&self, expression: &Expression<'_>) -> bool {
204
+ matches!(expression, Expression::CallExpression(call) if call
205
+ .callee
206
+ .as_member_expression()
207
+ .is_some_and(|member| self.is_method_of(member, "Date", "now")))
208
+ }
209
+
210
+ /// Matches `<object>.<property>` where `object` is an unshadowed global, such as
211
+ /// `Math.floor`.
212
+ #[must_use]
213
+ pub(crate) fn is_method_of(
214
+ &self,
215
+ member: &MemberExpression<'_>,
216
+ object: &str,
217
+ property: &str,
218
+ ) -> bool {
219
+ self.is_global(member.object(), object) && member_str(member) == Some(property)
220
+ }
221
+
222
+ /// Every function in the program, declared, expressed, or a method, in source order.
223
+ pub(crate) fn functions(&self) -> impl Iterator<Item = &'a Function<'a>> + use<'a> {
224
+ self.nodes
225
+ .iter()
226
+ .filter_map(|node| node.kind().as_function())
227
+ }
228
+
229
+ /// Applies `read` to each function declaration in the program, in source order, keeping
230
+ /// what it returns.
231
+ pub(crate) fn in_declared_functions<T>(
232
+ &self,
233
+ read: impl FnMut(&'a Function<'a>) -> Option<T>,
234
+ ) -> Vec<T> {
235
+ self.functions()
236
+ .filter(|function| function.is_declaration())
237
+ .filter_map(read)
238
+ .collect()
239
+ }
240
+
241
+ /// Returns declared, assigned, and variable-initialized functions by binding, in source
242
+ /// order, including functions assigned to globals.
243
+ #[must_use]
244
+ pub(crate) fn named_functions(&self) -> Vec<(Binding<'a>, &'a Function<'a>)> {
245
+ self.nodes
246
+ .iter()
247
+ .filter_map(|node| match node.kind() {
248
+ AstKind::Function(function) if function.is_declaration() => {
249
+ Some((Binding::Symbol(function.id.as_ref()?.symbol_id()), function))
250
+ }
251
+ AstKind::AssignmentExpression(assignment)
252
+ if assignment.operator == AssignmentOperator::Assign
253
+ && let AssignmentTarget::AssignmentTargetIdentifier(name) =
254
+ &assignment.left
255
+ && let Expression::FunctionExpression(function) = &assignment.right =>
256
+ {
257
+ Some((self.binding(name), &**function))
258
+ }
259
+ AstKind::VariableDeclarator(declaration)
260
+ if let BindingPattern::BindingIdentifier(name) = &declaration.id
261
+ && let Some(Expression::FunctionExpression(function)) =
262
+ &declaration.init =>
263
+ {
264
+ Some((Binding::Symbol(name.symbol_id()), &**function))
265
+ }
266
+ _ => None,
267
+ })
268
+ .collect()
269
+ }
270
+
271
+ /// The source name of `binding`: its declared name, or the global's.
272
+ #[must_use]
273
+ pub(crate) fn name(&self, binding: Binding<'a>) -> &'a str {
274
+ match binding {
275
+ Binding::Symbol(symbol) => self.scoping.symbol_name(symbol),
276
+ Binding::Global(name) => name,
277
+ }
278
+ }
279
+
280
+ /// Lends `node`, which a visitor borrowed briefly, for the program's lifetime, using `kind`
281
+ /// such as `AstKind::as_call_expression`.
282
+ ///
283
+ /// # Panics
284
+ ///
285
+ /// Panics if `node` is not from this program.
286
+ #[must_use]
287
+ pub(crate) fn lend<T: GetNodeId>(
288
+ &self,
289
+ node: &T,
290
+ kind: fn(AstKind<'a>) -> Option<&'a T>,
291
+ ) -> &'a T {
292
+ kind(self.nodes.kind(node.node_id())).expect("semantic nodes match the tree")
293
+ }
90
294
  }
91
295
 
92
- /// Returns a string literal or template without substitutions as WTF-8, preserving lone
93
- /// surrogates. Does not unwrap parentheses.
296
+ /// Returns a string literal or template without substitutions as JavaScript code units,
297
+ /// preserving lone surrogates. Does not unwrap parentheses.
94
298
  #[must_use]
95
- pub(crate) fn static_wtf8(expression: &Expr) -> Option<&Wtf8Atom> {
299
+ pub(crate) fn static_string<'a>(expression: &Expression<'a>) -> Option<JSStr<'a>> {
96
300
  match expression {
97
- Expr::Lit(Lit::Str(value)) => Some(&value.value),
98
- Expr::Tpl(template) if template.exprs.is_empty() => {
99
- template.quasis.first()?.cooked.as_ref()
301
+ Expression::StringLiteral(value) => Some(value.value),
302
+ Expression::TemplateLiteral(template) if template.expressions.is_empty() => {
303
+ template.quasis.first()?.value.cooked
100
304
  }
101
305
  _ => None,
102
306
  }
103
307
  }
104
308
 
105
- /// Converts [`static_wtf8`] to a Rust string. Returns `None` for lone surrogates instead of
309
+ /// Converts [`static_string`] to a Rust string. Returns `None` for lone surrogates instead of
106
310
  /// replacing them with U+FFFD and changing the recovered value.
107
311
  #[must_use]
108
- pub(crate) fn static_str(expression: &Expr) -> Option<&str> {
109
- static_wtf8(expression)?.as_str()
312
+ pub(crate) fn static_str<'a>(expression: &Expression<'a>) -> Option<&'a str> {
313
+ static_string(expression)?.as_str()
110
314
  }
111
315
 
112
316
  /// The static name of a member property: `object.name`, `object["name"]`, or
113
317
  /// ``object[`name`]`` when the template has no substitutions.
114
318
  #[must_use]
115
- pub(crate) fn member_str(property: &MemberProp) -> Option<&str> {
116
- match property {
117
- MemberProp::Ident(name) => Some(&*name.sym),
118
- MemberProp::Computed(computed) => static_str(&computed.expr),
119
- _ => None,
319
+ pub(crate) fn member_str<'a>(member: &MemberExpression<'a>) -> Option<&'a str> {
320
+ match member {
321
+ MemberExpression::StaticMemberExpression(member) => Some(member.property.name.as_str()),
322
+ MemberExpression::ComputedMemberExpression(member) => static_str(&member.expression),
323
+ MemberExpression::PrivateFieldExpression(_) => None,
120
324
  }
121
325
  }
122
326
 
123
327
  /// Matches a property read directly from an identifier, such as a namespace or local, using
124
328
  /// [`member_str`].
125
329
  #[must_use]
126
- pub(crate) fn ident_property(member: &MemberExpr) -> Option<&str> {
127
- match &*member.obj {
128
- Expr::Ident(_) => member_str(&member.prop),
330
+ pub(crate) fn ident_property<'a>(member: &MemberExpression<'a>) -> Option<&'a str> {
331
+ match member.object() {
332
+ Expression::Identifier(_) => member_str(member),
129
333
  _ => None,
130
334
  }
131
335
  }
132
336
 
133
- /// Reads an identifier or quoted object-literal key. Excludes numeric and computed keys.
337
+ /// Reads an identifier or quoted property key. Excludes numeric and computed keys.
134
338
  #[must_use]
135
- pub(crate) fn key_str(key: &PropName) -> Option<&str> {
339
+ pub(crate) fn key_str<'a>(key: &PropertyKey<'a>, computed: bool) -> Option<&'a str> {
136
340
  match key {
137
- PropName::Ident(name) => Some(&*name.sym),
138
- PropName::Str(name) => name.value.as_str(),
341
+ _ if computed => None,
342
+ PropertyKey::StaticIdentifier(name) => Some(name.name.as_str()),
343
+ PropertyKey::StringLiteral(name) => name.value.as_str(),
139
344
  _ => None,
140
345
  }
141
346
  }
@@ -143,165 +348,162 @@ pub(crate) fn key_str(key: &PropName) -> Option<&str> {
143
348
  /// Reads keys accepted by [`key_str`], plus computed static strings such as `["name"]` or
144
349
  /// ``[`name`]``.
145
350
  #[must_use]
146
- pub(crate) fn static_key_str(key: &PropName) -> Option<&str> {
147
- match key {
148
- PropName::Computed(name) => static_str(&name.expr),
149
- key => key_str(key),
351
+ pub(crate) fn static_key_str<'a>(key: &PropertyKey<'a>, computed: bool) -> Option<&'a str> {
352
+ if computed {
353
+ static_str(key.as_expression()?)
354
+ } else {
355
+ key_str(key, false)
356
+ }
357
+ }
358
+
359
+ /// The expression an argument passes, through a spread, as `x` in `f(...x)`.
360
+ #[must_use]
361
+ pub(crate) fn argument_expression<'b, 'a>(argument: &'b Argument<'a>) -> &'b Expression<'a> {
362
+ match argument {
363
+ Argument::SpreadElement(spread) => &spread.argument,
364
+ argument => argument.to_expression(),
150
365
  }
151
366
  }
152
367
 
153
368
  /// Matches `key: value` properties, excluding spreads, methods, accessors, and shorthand
154
369
  /// entries.
155
370
  #[must_use]
156
- pub(crate) fn key_value(property: &PropOrSpread) -> Option<&KeyValueProp> {
157
- property.as_prop()?.as_key_value()
371
+ pub(crate) fn key_value<'b, 'a>(
372
+ property: &'b ObjectPropertyKind<'a>,
373
+ ) -> Option<&'b ObjectProperty<'a>> {
374
+ match property {
375
+ ObjectPropertyKind::ObjectProperty(property)
376
+ if property.kind == PropertyKind::Init && !property.method && !property.shorthand =>
377
+ {
378
+ Some(property)
379
+ }
380
+ _ => None,
381
+ }
158
382
  }
159
383
 
160
384
  /// Returns only [`key_value`] properties from an object literal.
161
- pub(crate) fn key_values(object: &ObjectLit) -> impl Iterator<Item = &KeyValueProp> {
162
- object.props.iter().filter_map(key_value)
385
+ pub(crate) fn key_values<'b, 'a>(
386
+ object: &'b ObjectExpression<'a>,
387
+ ) -> impl Iterator<Item = &'b ObjectProperty<'a>> {
388
+ object.properties.iter().filter_map(key_value)
163
389
  }
164
390
 
165
- /// Collects distinct static strings from `node`; see [`static_str`].
391
+ /// Collects distinct static strings, see [`static_str`], from the nodes `visit` walks, such as
392
+ /// `|literals| literals.visit_program(program)`.
166
393
  #[must_use]
167
- pub(crate) fn string_literals(node: &impl VisitWith<StringLiterals>) -> BTreeSet<String> {
168
- let mut literals = StringLiterals(BTreeSet::new());
169
- node.visit_with(&mut literals);
170
- literals.0
394
+ pub(crate) fn string_literals(visit: impl FnOnce(&mut StringLiterals)) -> BTreeSet<String> {
395
+ let mut literals = StringLiterals::default();
396
+ visit(&mut literals);
397
+ literals.literals
171
398
  }
172
399
 
173
- pub(crate) struct StringLiterals(BTreeSet<String>);
400
+ /// Collects static strings in expression position. Directives such as `"use strict"` count;
401
+ /// non-computed keys such as `{"key": value}` are names, not expressions, and don't.
402
+ #[derive(Default)]
403
+ pub(crate) struct StringLiterals {
404
+ literals: BTreeSet<String>,
405
+ /// Whether the property key about to be visited is computed.
406
+ computed_key: bool,
407
+ }
174
408
 
175
- impl Visit for StringLiterals {
176
- fn visit_expr(&mut self, expression: &Expr) {
177
- if let Some(literal) = static_str(expression) {
178
- self.0.insert(literal.to_owned());
179
- }
180
- expression.visit_children_with(self);
409
+ impl StringLiterals {
410
+ fn key(&mut self, computed: bool, key: &PropertyKey<'_>) {
411
+ self.computed_key = computed;
412
+ self.visit_property_key(key);
181
413
  }
182
414
  }
183
415
 
184
- /// Applies `read` to each function declaration in `script`, in source order, keeping what it
185
- /// returns.
186
- pub(crate) fn in_declared_functions<T>(script: &Script, read: fn(&FnDecl) -> Option<T>) -> Vec<T> {
187
- struct Found<T> {
188
- read: fn(&FnDecl) -> Option<T>,
189
- results: Vec<T>,
190
- }
191
- impl<T> Visit for Found<T> {
192
- fn visit_fn_decl(&mut self, declaration: &FnDecl) {
193
- self.results.extend((self.read)(declaration));
194
- declaration.visit_children_with(self);
416
+ impl<'a> Visit<'a> for StringLiterals {
417
+ fn visit_expression(&mut self, expression: &Expression<'a>) {
418
+ if let Some(literal) = static_str(expression) {
419
+ self.literals.insert(literal.to_owned());
195
420
  }
421
+ walk::walk_expression(self, expression);
196
422
  }
197
- let mut found = Found {
198
- read,
199
- results: Vec::new(),
200
- };
201
- script.visit_with(&mut found);
202
- found.results
203
- }
204
423
 
205
- /// Returns function declarations in `script` by name, in source order.
206
- #[must_use]
207
- pub(crate) fn declared_functions(script: &Script) -> Vec<(String, Function)> {
208
- in_declared_functions(script, |declaration| {
209
- Some((
210
- declaration.ident.sym.to_string(),
211
- (*declaration.function).clone(),
212
- ))
213
- })
214
- }
424
+ fn visit_directive(&mut self, directive: &Directive<'a>) {
425
+ if let Some(literal) = directive.expression.value.as_str() {
426
+ self.literals.insert(literal.to_owned());
427
+ }
428
+ }
215
429
 
216
- /// Returns declared, assigned, and variable-initialized functions by binding ID in source order.
217
- #[must_use]
218
- pub(crate) fn named_functions(node: &impl VisitWith<NamedFunctions>) -> Vec<(Id, Function)> {
219
- let mut functions = NamedFunctions(Vec::new());
220
- node.visit_with(&mut functions);
221
- functions.0
222
- }
430
+ fn visit_property_key(&mut self, key: &PropertyKey<'a>) {
431
+ if mem::take(&mut self.computed_key) {
432
+ walk::walk_property_key(self, key);
433
+ }
434
+ }
223
435
 
224
- pub(crate) struct NamedFunctions(Vec<(Id, Function)>);
436
+ fn visit_object_property(&mut self, property: &ObjectProperty<'a>) {
437
+ self.key(property.computed, &property.key);
438
+ self.visit_expression(&property.value);
439
+ }
225
440
 
226
- impl Visit for NamedFunctions {
227
- fn visit_fn_decl(&mut self, declaration: &FnDecl) {
228
- let function = (*declaration.function).clone();
229
- self.0.push((declaration.ident.to_id(), function));
230
- declaration.visit_children_with(self);
441
+ fn visit_binding_property(&mut self, property: &BindingProperty<'a>) {
442
+ self.key(property.computed, &property.key);
443
+ self.visit_binding_pattern(&property.value);
231
444
  }
232
445
 
233
- fn visit_assign_expr(&mut self, assignment: &AssignExpr) {
234
- if assignment.op == AssignOp::Assign
235
- && let Some(name) = assignment.left.as_ident()
236
- && let Expr::Fn(function) = &*assignment.right
237
- {
238
- self.0.push((name.to_id(), (*function.function).clone()));
239
- }
240
- assignment.visit_children_with(self);
446
+ fn visit_method_definition(&mut self, method: &MethodDefinition<'a>) {
447
+ self.visit_decorators(&method.decorators);
448
+ self.key(method.computed, &method.key);
449
+ self.visit_function(&method.value, ScopeFlags::Function);
241
450
  }
242
451
 
243
- fn visit_var_declarator(&mut self, declaration: &VarDeclarator) {
244
- if let Pat::Ident(name) = &declaration.name
245
- && let Some(Expr::Fn(function)) = declaration.init.as_deref()
246
- {
247
- self.0.push((name.to_id(), (*function.function).clone()));
452
+ fn visit_property_definition(&mut self, property: &PropertyDefinition<'a>) {
453
+ self.visit_decorators(&property.decorators);
454
+ self.key(property.computed, &property.key);
455
+ if let Some(value) = &property.value {
456
+ self.visit_expression(value);
248
457
  }
249
- declaration.visit_children_with(self);
250
458
  }
251
- }
252
-
253
- /// Whether an expression is a `Date.now()` call.
254
- #[must_use]
255
- pub(crate) fn is_date_now(expression: &Expr) -> bool {
256
- expression
257
- .as_call()
258
- .and_then(|call| call.callee.as_expr()?.as_member())
259
- .is_some_and(|member| is_method_of(member, "Date", "now"))
260
- }
261
-
262
- /// Matches a method call on the plain identifier `object`, such as `<object>.<property>(...)`.
263
- #[must_use]
264
- pub(crate) fn is_method_of(member: &MemberExpr, object: &str, property: &str) -> bool {
265
- matches!(&*member.obj, Expr::Ident(ident) if ident.sym == object)
266
- && member_str(&member.prop) == Some(property)
267
- }
268
459
 
269
- /// Returns the binding and assigned value from `binding = value`.
270
- #[must_use]
271
- pub(crate) fn assignment(expression: &Expr) -> Option<(Id, &Expr)> {
272
- let assignment = expression.as_assign()?;
273
- if assignment.op != AssignOp::Assign {
274
- return None;
460
+ fn visit_accessor_property(&mut self, property: &AccessorProperty<'a>) {
461
+ self.visit_decorators(&property.decorators);
462
+ self.key(property.computed, &property.key);
463
+ if let Some(value) = &property.value {
464
+ self.visit_expression(value);
465
+ }
275
466
  }
276
- Some((assignment.left.as_ident()?.to_id(), &assignment.right))
277
467
  }
278
468
 
279
469
  /// Matches `callee(args)` when the callee is a plain identifier.
280
470
  #[must_use]
281
- pub(crate) fn direct_call(expression: &Expr) -> Option<(&Ident, &[ExprOrSpread])> {
282
- let call = expression.as_call()?;
283
- Some((call.callee.as_expr()?.as_ident()?, &call.args))
471
+ pub(crate) fn direct_call<'b, 'a>(
472
+ expression: &'b Expression<'a>,
473
+ ) -> Option<(&'b IdentifierReference<'a>, &'b [Argument<'a>])> {
474
+ match expression {
475
+ Expression::CallExpression(call) if let Expression::Identifier(callee) = &call.callee => {
476
+ Some((callee, &call.arguments))
477
+ }
478
+ _ => None,
479
+ }
284
480
  }
285
481
 
286
482
  /// Returns the object and arguments from `object.name(args)` or `object["name"](args)`.
287
483
  #[must_use]
288
- pub(crate) fn method_call<'a>(
289
- expression: &'a Expr,
484
+ pub(crate) fn method_call<'b, 'a>(
485
+ expression: &'b Expression<'a>,
290
486
  name: &str,
291
- ) -> Option<(&'a Expr, &'a [ExprOrSpread])> {
292
- let call = expression.as_call()?;
293
- let member = call.callee.as_expr()?.as_member()?;
294
- (member_str(&member.prop) == Some(name)).then_some((&*member.obj, &call.args[..]))
487
+ ) -> Option<(&'b Expression<'a>, &'b [Argument<'a>])> {
488
+ let Expression::CallExpression(call) = expression else {
489
+ return None;
490
+ };
491
+ let member = call.callee.as_member_expression()?;
492
+ (member_str(member) == Some(name)).then_some((member.object(), &call.arguments[..]))
295
493
  }
296
494
 
297
495
  /// A numeric literal, possibly signed or parenthesized.
298
496
  #[must_use]
299
- pub(crate) fn number(expression: &Expr) -> Option<f64> {
497
+ pub(crate) fn number(expression: &Expression<'_>) -> Option<f64> {
300
498
  match expression {
301
- Expr::Lit(Lit::Num(value)) => Some(value.value),
302
- Expr::Paren(value) => number(&value.expr),
303
- Expr::Unary(value) if value.op == UnaryOp::Minus => Some(-number(&value.arg)?),
304
- Expr::Unary(value) if value.op == UnaryOp::Plus => number(&value.arg),
499
+ Expression::NumericLiteral(value) => Some(value.value),
500
+ Expression::ParenthesizedExpression(value) => number(&value.expression),
501
+ Expression::UnaryExpression(value) if value.operator == UnaryOperator::UnaryNegation => {
502
+ Some(-number(&value.argument)?)
503
+ }
504
+ Expression::UnaryExpression(value) if value.operator == UnaryOperator::UnaryPlus => {
505
+ number(&value.argument)
506
+ }
305
507
  _ => None,
306
508
  }
307
509
  }
@@ -309,34 +511,37 @@ pub(crate) fn number(expression: &Expr) -> Option<f64> {
309
511
  /// Evaluates numeric arithmetic: literals, parentheses, and the unary and binary numeric
310
512
  /// operators. `leaf` supplies the value of anything else, such as a known parameter.
311
513
  #[must_use]
312
- pub(crate) fn fold_number(expression: &Expr, leaf: &impl Fn(&Expr) -> Option<f64>) -> Option<f64> {
313
- let fold = |expression: &Expr| fold_number(expression, leaf);
514
+ pub(crate) fn fold_number<'a>(
515
+ expression: &Expression<'a>,
516
+ leaf: &impl Fn(&Expression<'a>) -> Option<f64>,
517
+ ) -> Option<f64> {
518
+ let fold = |expression: &Expression<'a>| fold_number(expression, leaf);
314
519
  match expression {
315
- Expr::Lit(Lit::Num(value)) => Some(value.value),
316
- Expr::Paren(value) => fold(&value.expr),
317
- Expr::Unary(value) => {
318
- let operand = fold(&value.arg)?;
319
- match value.op {
320
- UnaryOp::Minus => Some(-operand),
321
- UnaryOp::Plus => Some(operand),
322
- UnaryOp::Tilde => Some(super::bit_not(operand)),
520
+ Expression::NumericLiteral(value) => Some(value.value),
521
+ Expression::ParenthesizedExpression(value) => fold(&value.expression),
522
+ Expression::UnaryExpression(value) => {
523
+ let operand = fold(&value.argument)?;
524
+ match value.operator {
525
+ UnaryOperator::UnaryNegation => Some(-operand),
526
+ UnaryOperator::UnaryPlus => Some(operand),
527
+ UnaryOperator::BitwiseNot => Some(super::bit_not(operand)),
323
528
  _ => None,
324
529
  }
325
530
  }
326
- Expr::Bin(binary) => {
531
+ Expression::BinaryExpression(binary) => {
327
532
  let (left, right) = (fold(&binary.left)?, fold(&binary.right)?);
328
- match binary.op {
329
- BinaryOp::Add => Some(left + right),
330
- BinaryOp::Sub => Some(left - right),
331
- BinaryOp::Mul => Some(left * right),
332
- BinaryOp::Div => Some(left / right),
333
- BinaryOp::Mod => Some(left % right),
334
- BinaryOp::BitAnd => Some(super::bit_and(left, right)),
335
- BinaryOp::BitOr => Some(super::bit_or(left, right)),
336
- BinaryOp::BitXor => Some(super::bit_xor(left, right)),
337
- BinaryOp::LShift => Some(super::shl(left, right)),
338
- BinaryOp::RShift => Some(super::shr(left, right)),
339
- BinaryOp::ZeroFillRShift => Some(super::ushr(left, right)),
533
+ match binary.operator {
534
+ BinaryOperator::Addition => Some(left + right),
535
+ BinaryOperator::Subtraction => Some(left - right),
536
+ BinaryOperator::Multiplication => Some(left * right),
537
+ BinaryOperator::Division => Some(left / right),
538
+ BinaryOperator::Remainder => Some(left % right),
539
+ BinaryOperator::BitwiseAnd => Some(super::bit_and(left, right)),
540
+ BinaryOperator::BitwiseOR => Some(super::bit_or(left, right)),
541
+ BinaryOperator::BitwiseXOR => Some(super::bit_xor(left, right)),
542
+ BinaryOperator::ShiftLeft => Some(super::shl(left, right)),
543
+ BinaryOperator::ShiftRight => Some(super::shr(left, right)),
544
+ BinaryOperator::ShiftRightZeroFill => Some(super::ushr(left, right)),
340
545
  _ => None,
341
546
  }
342
547
  }
@@ -354,68 +559,79 @@ pub(crate) fn array_index(value: f64) -> Option<usize> {
354
559
  /// Matches a numeric-literal [`array_index`]. Unlike [`number`], does not unwrap signs or
355
560
  /// parentheses.
356
561
  #[must_use]
357
- pub(crate) fn index_literal(expression: &Expr) -> Option<usize> {
562
+ pub(crate) fn index_literal(expression: &Expression<'_>) -> Option<usize> {
358
563
  match expression {
359
- Expr::Lit(Lit::Num(value)) => array_index(value.value),
360
- _ => None,
361
- }
362
- }
363
-
364
- /// Returns the expression from a function whose entire body is `return value;`.
365
- #[must_use]
366
- pub(crate) fn returned(function: &Function) -> Option<&Expr> {
367
- match function.body.as_ref()?.stmts.as_slice() {
368
- [Stmt::Return(returned)] => returned.arg.as_deref(),
564
+ Expression::NumericLiteral(value) => array_index(value.value),
369
565
  _ => None,
370
566
  }
371
567
  }
372
568
 
373
- /// A block grouping statements that came from one place.
374
- #[must_use]
375
- pub(crate) fn block(statements: Vec<Stmt>) -> Stmt {
376
- Stmt::Block(BlockStmt {
377
- span: DUMMY_SP,
378
- ctxt: SyntaxContext::default(),
379
- stmts: statements,
380
- })
381
- }
569
+ /// Parses `source` and passes its first statement's expression, without the parentheses
570
+ /// wrapped around it, to `read`.
571
+ #[cfg(test)]
572
+ pub(crate) fn with_expression<T>(
573
+ source: &str,
574
+ read: impl for<'a> FnOnce(Script<'a>, &'a Expression<'a>) -> T,
575
+ ) -> T {
576
+ use oxc::ast::ast::Statement;
382
577
 
383
- /// Creates an expression statement with the expression's source span.
384
- #[must_use]
385
- pub(crate) fn expression_statement(expression: Box<Expr>) -> Stmt {
386
- Stmt::Expr(ExprStmt {
387
- span: expression.span(),
388
- expr: expression,
389
- })
578
+ // Parentheses make leading braces parse as an object literal.
579
+ let parsed = ParsedScript::parse("expression", &format!("({source})")).unwrap();
580
+ let script = parsed.script();
581
+ let [Statement::ExpressionStatement(statement)] = script.program.body.as_slice() else {
582
+ panic!("{source} is not one expression");
583
+ };
584
+ read(script, statement.expression.without_parentheses())
390
585
  }
391
586
 
392
- /// Wraps an expression in parentheses with the same span so an inlined value remains a single
393
- /// operand.
394
- #[must_use]
395
- pub(crate) fn parenthesized(expression: Box<Expr>) -> Expr {
396
- Expr::Paren(ParenExpr {
397
- span: expression.span(),
398
- expr: expression,
399
- })
587
+ impl fmt::Debug for ParsedScript {
588
+ fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
589
+ formatter
590
+ .debug_struct("ParsedScript")
591
+ .finish_non_exhaustive()
592
+ }
400
593
  }
401
594
 
402
595
  #[cfg(test)]
403
596
  mod tests {
597
+ use oxc::ast::ast::CallExpression;
598
+
404
599
  use super::*;
405
600
 
406
601
  /// Only a static key reads through a computed string literal.
407
602
  #[test]
408
603
  fn static_keys_also_accept_computed_strings() {
409
- let Expr::Object(object) =
410
- parse_expression("t", r#"{a: 1, "b": 2, ["c"]: 3, [d]: 4, 5: 5}"#).unwrap()
411
- else {
412
- panic!("not an object")
413
- };
414
- let keys: Vec<_> = key_values(&object).map(|p| key_str(&p.key)).collect();
415
- assert_eq!(keys, [Some("a"), Some("b"), None, None, None]);
416
- let keys: Vec<_> = key_values(&object)
417
- .map(|p| static_key_str(&p.key))
418
- .collect();
419
- assert_eq!(keys, [Some("a"), Some("b"), Some("c"), None, None]);
604
+ with_expression(r#"{a: 1, "b": 2, ["c"]: 3, [d]: 4, 5: 5}"#, |_, object| {
605
+ let Expression::ObjectExpression(object) = object else {
606
+ panic!("not an object")
607
+ };
608
+ let keys: Vec<_> = key_values(object)
609
+ .map(|p| key_str(&p.key, p.computed))
610
+ .collect();
611
+ assert_eq!(keys, [Some("a"), Some("b"), None, None, None]);
612
+ let keys: Vec<_> = key_values(object)
613
+ .map(|p| static_key_str(&p.key, p.computed))
614
+ .collect();
615
+ assert_eq!(keys, [Some("a"), Some("b"), Some("c"), None, None]);
616
+ });
617
+ }
618
+
619
+ #[test]
620
+ fn globals_are_unshadowed_references() {
621
+ let parsed =
622
+ ParsedScript::parse("t", "Date.now(); function f(Date) { Date.now(); }").unwrap();
623
+ let script = parsed.script();
624
+ let mut found = Vec::new();
625
+ struct Calls<'s, 'a>(Script<'a>, &'s mut Vec<bool>);
626
+ impl<'a> Visit<'a> for Calls<'_, 'a> {
627
+ fn visit_call_expression(&mut self, call: &CallExpression<'a>) {
628
+ let Calls(script, found) = self;
629
+ let member = call.callee.as_member_expression().unwrap();
630
+ found.push(script.is_method_of(member, "Date", "now"));
631
+ walk::walk_call_expression(self, call);
632
+ }
633
+ }
634
+ Calls(script, &mut found).visit_program(script.program);
635
+ assert_eq!(found, [true, false]);
420
636
  }
421
637
  }