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
@@ -1,132 +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, SourceFile, SourceMap, Spanned, 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,
13
18
  };
14
- use swc_core::ecma::ast::{
15
- AssignExpr, AssignOp, BinaryOp, BlockStmt, Expr, ExprOrSpread, ExprStmt, FnDecl, Function, Id,
16
- Ident, KeyValueProp, Lit, MemberExpr, MemberProp, ObjectLit, ParenExpr, Pat, PropName,
17
- PropOrSpread, Script, Stmt, UnaryOp, VarDeclarator,
18
- };
19
- use swc_core::ecma::parser::{Parser, StringInput, Syntax};
20
- use swc_core::ecma::visit::{Visit, VisitWith};
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;
21
28
 
22
29
  use crate::engine::{self, Lines};
23
30
 
24
- /// A parsed script and its original source text.
25
- pub(crate) struct ParsedScript {
26
- pub script: Script,
27
- 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>,
28
42
  }
29
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
+
30
65
  impl ParsedScript {
31
- /// 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`.
32
149
  ///
33
150
  /// # Panics
34
151
  ///
35
152
  /// Panics if `node` was not parsed from this script.
36
153
  #[must_use]
37
- pub(crate) fn text(&self, node: &dyn Spanned) -> &str {
38
- let span = node.span();
39
- let offset = |position: BytePos| (position.0 - self.file.start_pos.0) as usize;
40
- &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)
41
156
  }
42
157
 
43
- /// 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.
44
160
  ///
45
161
  /// # Panics
46
162
  ///
47
163
  /// Panics if `at` is outside this script.
48
164
  #[must_use]
49
- pub(crate) fn position(&self, at: BytePos) -> (usize, usize) {
50
- let before = &self.file.src[..(at.0 - self.file.start_pos.0) as usize];
51
- 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);
52
167
  (line as usize + 1, column as usize + 1)
53
168
  }
54
- }
55
169
 
56
- pub(crate) fn parse_iframe_javascript(source: &str) -> Result<ParsedScript> {
57
- parse_javascript("deobfuscated iframe", source)
58
- }
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
+ }
59
177
 
60
- /// Parses `source`, using `what` to identify it in errors.
61
- pub(crate) fn parse_javascript(what: &str, source: &str) -> Result<ParsedScript> {
62
- let source_map = SourceMap::default();
63
- let file = source_map.new_source_file(FileName::Custom(what.into()).into(), source.to_owned());
64
- let mut parser = Parser::new(Syntax::default(), StringInput::from(&*file), None);
65
- let script = parser
66
- .parse_script()
67
- .map_err(|error| anyhow::anyhow!("cannot parse {what}: {error:?}"))?;
68
- ensure!(parser.take_errors().is_empty(), "{what} has parse errors");
69
- Ok(ParsedScript { script, file })
70
- }
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
+ }
71
186
 
72
- /// Parses one expression, without its surrounding parentheses.
73
- #[cfg(test)]
74
- pub(crate) fn parse_expression(what: &str, text: &str) -> Result<Expr> {
75
- // Parentheses make leading braces parse as an object literal.
76
- let parsed = parse_javascript(what, &format!("({text})"))?;
77
- let [Stmt::Expr(statement)] = parsed.script.body.as_slice() else {
78
- anyhow::bail!("{what} is not one expression");
79
- };
80
- 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
+ }
81
294
  }
82
295
 
83
- /// Returns a string literal or template without substitutions as WTF-8, preserving lone
84
- /// 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.
85
298
  #[must_use]
86
- pub(crate) fn static_wtf8(expression: &Expr) -> Option<&Wtf8Atom> {
299
+ pub(crate) fn static_string<'a>(expression: &Expression<'a>) -> Option<JSStr<'a>> {
87
300
  match expression {
88
- Expr::Lit(Lit::Str(value)) => Some(&value.value),
89
- Expr::Tpl(template) if template.exprs.is_empty() => {
90
- 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
91
304
  }
92
305
  _ => None,
93
306
  }
94
307
  }
95
308
 
96
- /// 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
97
310
  /// replacing them with U+FFFD and changing the recovered value.
98
311
  #[must_use]
99
- pub(crate) fn static_str(expression: &Expr) -> Option<&str> {
100
- static_wtf8(expression)?.as_str()
312
+ pub(crate) fn static_str<'a>(expression: &Expression<'a>) -> Option<&'a str> {
313
+ static_string(expression)?.as_str()
101
314
  }
102
315
 
103
316
  /// The static name of a member property: `object.name`, `object["name"]`, or
104
317
  /// ``object[`name`]`` when the template has no substitutions.
105
318
  #[must_use]
106
- pub(crate) fn member_str(property: &MemberProp) -> Option<&str> {
107
- match property {
108
- MemberProp::Ident(name) => Some(&*name.sym),
109
- MemberProp::Computed(computed) => static_str(&computed.expr),
110
- _ => 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,
111
324
  }
112
325
  }
113
326
 
114
327
  /// Matches a property read directly from an identifier, such as a namespace or local, using
115
328
  /// [`member_str`].
116
329
  #[must_use]
117
- pub(crate) fn ident_property(member: &MemberExpr) -> Option<&str> {
118
- match &*member.obj {
119
- 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),
120
333
  _ => None,
121
334
  }
122
335
  }
123
336
 
124
- /// 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.
125
338
  #[must_use]
126
- pub(crate) fn key_str(key: &PropName) -> Option<&str> {
339
+ pub(crate) fn key_str<'a>(key: &PropertyKey<'a>, computed: bool) -> Option<&'a str> {
127
340
  match key {
128
- PropName::Ident(name) => Some(&*name.sym),
129
- 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(),
130
344
  _ => None,
131
345
  }
132
346
  }
@@ -134,165 +348,162 @@ pub(crate) fn key_str(key: &PropName) -> Option<&str> {
134
348
  /// Reads keys accepted by [`key_str`], plus computed static strings such as `["name"]` or
135
349
  /// ``[`name`]``.
136
350
  #[must_use]
137
- pub(crate) fn static_key_str(key: &PropName) -> Option<&str> {
138
- match key {
139
- PropName::Computed(name) => static_str(&name.expr),
140
- 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(),
141
365
  }
142
366
  }
143
367
 
144
368
  /// Matches `key: value` properties, excluding spreads, methods, accessors, and shorthand
145
369
  /// entries.
146
370
  #[must_use]
147
- pub(crate) fn key_value(property: &PropOrSpread) -> Option<&KeyValueProp> {
148
- 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
+ }
149
382
  }
150
383
 
151
384
  /// Returns only [`key_value`] properties from an object literal.
152
- pub(crate) fn key_values(object: &ObjectLit) -> impl Iterator<Item = &KeyValueProp> {
153
- 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)
154
389
  }
155
390
 
156
- /// 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)`.
157
393
  #[must_use]
158
- pub(crate) fn string_literals(node: &impl VisitWith<StringLiterals>) -> BTreeSet<String> {
159
- let mut literals = StringLiterals(BTreeSet::new());
160
- node.visit_with(&mut literals);
161
- 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
162
398
  }
163
399
 
164
- 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
+ }
165
408
 
166
- impl Visit for StringLiterals {
167
- fn visit_expr(&mut self, expression: &Expr) {
168
- if let Some(literal) = static_str(expression) {
169
- self.0.insert(literal.to_owned());
170
- }
171
- 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);
172
413
  }
173
414
  }
174
415
 
175
- /// Applies `read` to each function declaration in `script`, in source order, keeping what it
176
- /// returns.
177
- pub(crate) fn in_declared_functions<T>(script: &Script, read: fn(&FnDecl) -> Option<T>) -> Vec<T> {
178
- struct Found<T> {
179
- read: fn(&FnDecl) -> Option<T>,
180
- results: Vec<T>,
181
- }
182
- impl<T> Visit for Found<T> {
183
- fn visit_fn_decl(&mut self, declaration: &FnDecl) {
184
- self.results.extend((self.read)(declaration));
185
- 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());
186
420
  }
421
+ walk::walk_expression(self, expression);
187
422
  }
188
- let mut found = Found {
189
- read,
190
- results: Vec::new(),
191
- };
192
- script.visit_with(&mut found);
193
- found.results
194
- }
195
423
 
196
- /// Returns function declarations in `script` by name, in source order.
197
- #[must_use]
198
- pub(crate) fn declared_functions(script: &Script) -> Vec<(String, Function)> {
199
- in_declared_functions(script, |declaration| {
200
- Some((
201
- declaration.ident.sym.to_string(),
202
- (*declaration.function).clone(),
203
- ))
204
- })
205
- }
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
+ }
206
429
 
207
- /// Returns declared, assigned, and variable-initialized functions by binding ID in source order.
208
- #[must_use]
209
- pub(crate) fn named_functions(node: &impl VisitWith<NamedFunctions>) -> Vec<(Id, Function)> {
210
- let mut functions = NamedFunctions(Vec::new());
211
- node.visit_with(&mut functions);
212
- functions.0
213
- }
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
+ }
214
435
 
215
- 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
+ }
216
440
 
217
- impl Visit for NamedFunctions {
218
- fn visit_fn_decl(&mut self, declaration: &FnDecl) {
219
- let function = (*declaration.function).clone();
220
- self.0.push((declaration.ident.to_id(), function));
221
- 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);
222
444
  }
223
445
 
224
- fn visit_assign_expr(&mut self, assignment: &AssignExpr) {
225
- if assignment.op == AssignOp::Assign
226
- && let Some(name) = assignment.left.as_ident()
227
- && let Expr::Fn(function) = &*assignment.right
228
- {
229
- self.0.push((name.to_id(), (*function.function).clone()));
230
- }
231
- 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);
232
450
  }
233
451
 
234
- fn visit_var_declarator(&mut self, declaration: &VarDeclarator) {
235
- if let Pat::Ident(name) = &declaration.name
236
- && let Some(Expr::Fn(function)) = declaration.init.as_deref()
237
- {
238
- 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);
239
457
  }
240
- declaration.visit_children_with(self);
241
458
  }
242
- }
243
-
244
- /// Whether an expression is a `Date.now()` call.
245
- #[must_use]
246
- pub(crate) fn is_date_now(expression: &Expr) -> bool {
247
- expression
248
- .as_call()
249
- .and_then(|call| call.callee.as_expr()?.as_member())
250
- .is_some_and(|member| is_method_of(member, "Date", "now"))
251
- }
252
-
253
- /// Matches a method call on the plain identifier `object`, such as `<object>.<property>(...)`.
254
- #[must_use]
255
- pub(crate) fn is_method_of(member: &MemberExpr, object: &str, property: &str) -> bool {
256
- matches!(&*member.obj, Expr::Ident(ident) if ident.sym == object)
257
- && member_str(&member.prop) == Some(property)
258
- }
259
459
 
260
- /// Returns the binding and assigned value from `binding = value`.
261
- #[must_use]
262
- pub(crate) fn assignment(expression: &Expr) -> Option<(Id, &Expr)> {
263
- let assignment = expression.as_assign()?;
264
- if assignment.op != AssignOp::Assign {
265
- 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
+ }
266
466
  }
267
- Some((assignment.left.as_ident()?.to_id(), &assignment.right))
268
467
  }
269
468
 
270
469
  /// Matches `callee(args)` when the callee is a plain identifier.
271
470
  #[must_use]
272
- pub(crate) fn direct_call(expression: &Expr) -> Option<(&Ident, &[ExprOrSpread])> {
273
- let call = expression.as_call()?;
274
- 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
+ }
275
480
  }
276
481
 
277
482
  /// Returns the object and arguments from `object.name(args)` or `object["name"](args)`.
278
483
  #[must_use]
279
- pub(crate) fn method_call<'a>(
280
- expression: &'a Expr,
484
+ pub(crate) fn method_call<'b, 'a>(
485
+ expression: &'b Expression<'a>,
281
486
  name: &str,
282
- ) -> Option<(&'a Expr, &'a [ExprOrSpread])> {
283
- let call = expression.as_call()?;
284
- let member = call.callee.as_expr()?.as_member()?;
285
- (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[..]))
286
493
  }
287
494
 
288
495
  /// A numeric literal, possibly signed or parenthesized.
289
496
  #[must_use]
290
- pub(crate) fn number(expression: &Expr) -> Option<f64> {
497
+ pub(crate) fn number(expression: &Expression<'_>) -> Option<f64> {
291
498
  match expression {
292
- Expr::Lit(Lit::Num(value)) => Some(value.value),
293
- Expr::Paren(value) => number(&value.expr),
294
- Expr::Unary(value) if value.op == UnaryOp::Minus => Some(-number(&value.arg)?),
295
- 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
+ }
296
507
  _ => None,
297
508
  }
298
509
  }
@@ -300,34 +511,37 @@ pub(crate) fn number(expression: &Expr) -> Option<f64> {
300
511
  /// Evaluates numeric arithmetic: literals, parentheses, and the unary and binary numeric
301
512
  /// operators. `leaf` supplies the value of anything else, such as a known parameter.
302
513
  #[must_use]
303
- pub(crate) fn fold_number(expression: &Expr, leaf: &impl Fn(&Expr) -> Option<f64>) -> Option<f64> {
304
- 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);
305
519
  match expression {
306
- Expr::Lit(Lit::Num(value)) => Some(value.value),
307
- Expr::Paren(value) => fold(&value.expr),
308
- Expr::Unary(value) => {
309
- let operand = fold(&value.arg)?;
310
- match value.op {
311
- UnaryOp::Minus => Some(-operand),
312
- UnaryOp::Plus => Some(operand),
313
- 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)),
314
528
  _ => None,
315
529
  }
316
530
  }
317
- Expr::Bin(binary) => {
531
+ Expression::BinaryExpression(binary) => {
318
532
  let (left, right) = (fold(&binary.left)?, fold(&binary.right)?);
319
- match binary.op {
320
- BinaryOp::Add => Some(left + right),
321
- BinaryOp::Sub => Some(left - right),
322
- BinaryOp::Mul => Some(left * right),
323
- BinaryOp::Div => Some(left / right),
324
- BinaryOp::Mod => Some(left % right),
325
- BinaryOp::BitAnd => Some(super::bit_and(left, right)),
326
- BinaryOp::BitOr => Some(super::bit_or(left, right)),
327
- BinaryOp::BitXor => Some(super::bit_xor(left, right)),
328
- BinaryOp::LShift => Some(super::shl(left, right)),
329
- BinaryOp::RShift => Some(super::shr(left, right)),
330
- 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)),
331
545
  _ => None,
332
546
  }
333
547
  }
@@ -345,68 +559,79 @@ pub(crate) fn array_index(value: f64) -> Option<usize> {
345
559
  /// Matches a numeric-literal [`array_index`]. Unlike [`number`], does not unwrap signs or
346
560
  /// parentheses.
347
561
  #[must_use]
348
- pub(crate) fn index_literal(expression: &Expr) -> Option<usize> {
562
+ pub(crate) fn index_literal(expression: &Expression<'_>) -> Option<usize> {
349
563
  match expression {
350
- Expr::Lit(Lit::Num(value)) => array_index(value.value),
351
- _ => None,
352
- }
353
- }
354
-
355
- /// Returns the expression from a function whose entire body is `return value;`.
356
- #[must_use]
357
- pub(crate) fn returned(function: &Function) -> Option<&Expr> {
358
- match function.body.as_ref()?.stmts.as_slice() {
359
- [Stmt::Return(returned)] => returned.arg.as_deref(),
564
+ Expression::NumericLiteral(value) => array_index(value.value),
360
565
  _ => None,
361
566
  }
362
567
  }
363
568
 
364
- /// A block grouping statements that came from one place.
365
- #[must_use]
366
- pub(crate) fn block(statements: Vec<Stmt>) -> Stmt {
367
- Stmt::Block(BlockStmt {
368
- span: DUMMY_SP,
369
- ctxt: SyntaxContext::default(),
370
- stmts: statements,
371
- })
372
- }
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;
373
577
 
374
- /// Creates an expression statement with the expression's source span.
375
- #[must_use]
376
- pub(crate) fn expression_statement(expression: Box<Expr>) -> Stmt {
377
- Stmt::Expr(ExprStmt {
378
- span: expression.span(),
379
- expr: expression,
380
- })
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())
381
585
  }
382
586
 
383
- /// Wraps an expression in parentheses with the same span so an inlined value remains a single
384
- /// operand.
385
- #[must_use]
386
- pub(crate) fn parenthesized(expression: Box<Expr>) -> Expr {
387
- Expr::Paren(ParenExpr {
388
- span: expression.span(),
389
- expr: expression,
390
- })
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
+ }
391
593
  }
392
594
 
393
595
  #[cfg(test)]
394
596
  mod tests {
597
+ use oxc::ast::ast::CallExpression;
598
+
395
599
  use super::*;
396
600
 
397
601
  /// Only a static key reads through a computed string literal.
398
602
  #[test]
399
603
  fn static_keys_also_accept_computed_strings() {
400
- let Expr::Object(object) =
401
- parse_expression("t", r#"{a: 1, "b": 2, ["c"]: 3, [d]: 4, 5: 5}"#).unwrap()
402
- else {
403
- panic!("not an object")
404
- };
405
- let keys: Vec<_> = key_values(&object).map(|p| key_str(&p.key)).collect();
406
- assert_eq!(keys, [Some("a"), Some("b"), None, None, None]);
407
- let keys: Vec<_> = key_values(&object)
408
- .map(|p| static_key_str(&p.key))
409
- .collect();
410
- 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]);
411
636
  }
412
637
  }