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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +22 -0
- data/README.md +8 -3
- data/THIRD_PARTY_LICENSES/README.md +71 -254
- data/THIRD_PARTY_LICENSES/{derive_builder_macro-0.20.2/LICENSE-APACHE → bytecount-0.6.9/LICENSE.Apache2} +0 -1
- data/THIRD_PARTY_LICENSES/{darling-0.20.11/LICENSE → bytecount-0.6.9/LICENSE.MIT} +1 -3
- data/THIRD_PARTY_LICENSES/{ident_case-1.0.1/LICENSE → cobs-0.3.0/LICENSE-MIT} +1 -1
- data/THIRD_PARTY_LICENSES/cow-utils-0.1.3/LICENSE +21 -0
- data/THIRD_PARTY_LICENSES/{hashbrown-0.14.5 → embedded-io-0.4.0}/LICENSE-MIT +1 -1
- data/THIRD_PARTY_LICENSES/{ahash-0.8.12 → embedded-io-0.6.1}/LICENSE-MIT +1 -1
- data/THIRD_PARTY_LICENSES/{browserslist-data-0.1.4 → escape-simd-0.1.1}/LICENSE +1 -1
- data/THIRD_PARTY_LICENSES/escape-simd-0.1.1/NOTICE +62 -0
- data/THIRD_PARTY_LICENSES/json-escape-simd-3.1.3/LICENSE +21 -0
- data/THIRD_PARTY_LICENSES/{miette-7.6.0/LICENSE → json-escape-simd-3.1.3/LICENSE-APACHE} +0 -1
- data/THIRD_PARTY_LICENSES/json-escape-simd-3.1.3/NOTICE +62 -0
- data/THIRD_PARTY_LICENSES/memchr-2.8.3/UNLICENSE +24 -0
- data/THIRD_PARTY_LICENSES/{derive_builder_core-0.20.2 → nonmax-0.5.5}/LICENSE-APACHE +1 -2
- data/THIRD_PARTY_LICENSES/{dashmap-5.5.3/LICENSE → nonmax-0.5.5/LICENSE-MIT} +2 -4
- data/THIRD_PARTY_LICENSES/{rand-0.8.5 → num-bigint-0.5.1}/LICENSE-MIT +0 -1
- data/THIRD_PARTY_LICENSES/oxc-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/{browserslist-rs-0.19.0 → oxc-browserslist-5.0.1}/LICENSE +1 -0
- data/THIRD_PARTY_LICENSES/oxc_allocator-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_allocator-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_ast-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_ast-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_ast_macros-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_ast_macros-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_ast_visit-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_ast_visit-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_codegen-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_codegen-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_compat-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_compat-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_data_structures-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_data_structures-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_diagnostics-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_diagnostics-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_ecmascript-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_ecmascript-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_estree-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_estree-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_index-5.0.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_parser-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_parser-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_regular_expression-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_regular_expression-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_semantic-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_semantic-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_span-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_span-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_str-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_str-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/oxc_syntax-0.157.0/LICENSE +22 -0
- data/THIRD_PARTY_LICENSES/oxc_syntax-0.157.0/THIRD-PARTY-LICENSE +763 -0
- data/THIRD_PARTY_LICENSES/{fixedbitset-0.5.7 → postcard-1.1.3}/LICENSE-MIT +1 -1
- data/THIRD_PARTY_LICENSES/{bytes-str-0.2.7/LICENSE → self_cell-1.3.0/LICENSE-APACHE} +1 -2
- data/THIRD_PARTY_LICENSES/self_cell-1.3.0/LICENSE-GPLv2 +339 -0
- data/THIRD_PARTY_LICENSES/{darling_core-0.20.11 → smawk-0.3.3}/LICENSE +1 -1
- data/UPSTREAM.md +14 -6
- data/ext/cfsolve/Cargo.lock +534 -1727
- data/ext/cfsolve/Cargo.toml +4 -0
- data/ext/cfsolve/src/main.rs +1 -1
- data/lib/cfsolve/profiles/chrome-157-macos/answers.json +4777 -0
- data/lib/cfsolve/profiles/chrome-157-macos.json +613 -0
- data/lib/cfsolve/profiles/safari-26-ios/answers.json +266 -265
- data/lib/cfsolve/profiles/safari-26-ios/challenge-page-answers.json +312 -312
- data/lib/cfsolve/profiles/safari-26-ios.json +106 -152
- data/lib/cfsolve/version.rb +2 -2
- data/lib/cfsolve.rb +1 -1
- data/vendor/cfsolve/Cargo.lock +561 -1729
- data/vendor/cfsolve/Cargo.toml +26 -8
- data/vendor/cfsolve/README.md +57 -5
- data/vendor/cfsolve/examples/check_answers.rs +2 -2
- data/vendor/cfsolve/examples/compare_session.rs +1 -2
- data/vendor/cfsolve/examples/solver_profile.rs +84 -0
- data/vendor/cfsolve/examples/solver_smoke.rs +9 -8
- data/vendor/cfsolve/examples/stage2_snapshot.rs +2 -2
- data/vendor/cfsolve/examples/tls_echo.rs +3 -3
- data/vendor/cfsolve/examples/turnstile_smoke.rs +58 -0
- data/vendor/cfsolve/examples/vm_snapshot.rs +32 -33
- data/vendor/cfsolve/profiles/chrome-157-macos/answers.json +4777 -0
- data/vendor/cfsolve/profiles/chrome-157-macos.json +613 -0
- data/vendor/cfsolve/profiles/safari-26-ios/answers.json +266 -265
- data/vendor/cfsolve/profiles/safari-26-ios/challenge-page-answers.json +312 -312
- data/vendor/cfsolve/profiles/safari-26-ios.json +106 -152
- data/vendor/cfsolve/src/bin/capture-proxy/main.rs +3 -2
- data/vendor/cfsolve/src/bin/challenge/cli.rs +8 -2
- data/vendor/cfsolve/src/bin/challenge/main.rs +7 -2
- data/vendor/cfsolve/src/capture/proxy/tests.rs +1 -1
- data/vendor/cfsolve/src/capture/proxy.rs +44 -13
- data/vendor/cfsolve/src/capture/sessions.rs +4 -3
- data/vendor/cfsolve/src/capture/upstream.rs +1 -1
- data/vendor/cfsolve/src/challenge/codec.rs +4 -2
- data/vendor/cfsolve/src/challenge/expression.rs +161 -117
- data/vendor/cfsolve/src/challenge/installed/audit.rs +11 -10
- data/vendor/cfsolve/src/challenge/installed.rs +13 -6
- data/vendor/cfsolve/src/challenge/merge.rs +120 -80
- data/vendor/cfsolve/src/challenge/orchestrator.rs +57 -44
- data/vendor/cfsolve/src/challenge/payload.rs +69 -31
- data/vendor/cfsolve/src/challenge/program.rs +197 -178
- data/vendor/cfsolve/src/challenge/reduced_timing.rs +55 -40
- data/vendor/cfsolve/src/challenge/source.rs +151 -96
- data/vendor/cfsolve/src/challenge/state.rs +180 -148
- data/vendor/cfsolve/src/challenge/tally.rs +64 -50
- data/vendor/cfsolve/src/challenge.rs +1 -0
- data/vendor/cfsolve/src/deobfuscate/analysis/bindings.rs +187 -185
- data/vendor/cfsolve/src/deobfuscate/analysis/effects.rs +42 -51
- data/vendor/cfsolve/src/deobfuscate/analysis/hoisting.rs +147 -228
- data/vendor/cfsolve/src/deobfuscate/analysis/properties.rs +250 -425
- data/vendor/cfsolve/src/deobfuscate/analysis/unconditional.rs +79 -0
- data/vendor/cfsolve/src/deobfuscate/analysis.rs +259 -41
- data/vendor/cfsolve/src/deobfuscate/argument_arrays.rs +70 -73
- data/vendor/cfsolve/src/deobfuscate/arrow_blocks.rs +91 -104
- data/vendor/cfsolve/src/deobfuscate/ast.rs +383 -0
- data/vendor/cfsolve/src/deobfuscate/bitwise.rs +44 -54
- data/vendor/cfsolve/src/deobfuscate/blocks.rs +36 -29
- data/vendor/cfsolve/src/deobfuscate/call_helpers.rs +80 -131
- data/vendor/cfsolve/src/deobfuscate/control_flow.rs +239 -297
- data/vendor/cfsolve/src/deobfuscate/copy_propagation.rs +114 -135
- data/vendor/cfsolve/src/deobfuscate/dead_branches.rs +62 -61
- data/vendor/cfsolve/src/deobfuscate/dead_properties.rs +197 -214
- data/vendor/cfsolve/src/deobfuscate/dispatch.rs +238 -298
- data/vendor/cfsolve/src/deobfuscate/encoded_strings.rs +198 -203
- data/vendor/cfsolve/src/deobfuscate/function_lengths.rs +32 -38
- data/vendor/cfsolve/src/deobfuscate/helper_objects.rs +338 -513
- data/vendor/cfsolve/src/deobfuscate/hygiene.rs +197 -0
- data/vendor/cfsolve/src/deobfuscate/int32_helpers.rs +121 -155
- data/vendor/cfsolve/src/deobfuscate/literals.rs +116 -67
- data/vendor/cfsolve/src/deobfuscate/loops.rs +43 -43
- data/vendor/cfsolve/src/deobfuscate/memoized_strings.rs +127 -131
- data/vendor/cfsolve/src/deobfuscate/merged_functions.rs +262 -220
- data/vendor/cfsolve/src/deobfuscate/pipeline.rs +199 -244
- data/vendor/cfsolve/src/deobfuscate/register_arrays.rs +353 -327
- data/vendor/cfsolve/src/deobfuscate/removal.rs +83 -52
- data/vendor/cfsolve/src/deobfuscate/simplify.rs +147 -0
- data/vendor/cfsolve/src/deobfuscate/state_machines.rs +88 -110
- data/vendor/cfsolve/src/deobfuscate/statements.rs +228 -228
- data/vendor/cfsolve/src/deobfuscate/static_members.rs +46 -31
- data/vendor/cfsolve/src/deobfuscate/testing.rs +49 -59
- data/vendor/cfsolve/src/deobfuscate/unused_bindings.rs +92 -102
- data/vendor/cfsolve/src/deobfuscate.rs +15 -11
- data/vendor/cfsolve/src/embedding_page/extra_params.rs +132 -24
- data/vendor/cfsolve/src/embedding_page/page.rs +41 -1
- data/vendor/cfsolve/src/embedding_page/render_sample.rs +184 -147
- data/vendor/cfsolve/src/error.rs +40 -5
- data/vendor/cfsolve/src/fingerprint.rs +8 -2
- data/vendor/cfsolve/src/http/client.rs +7 -10
- data/vendor/cfsolve/src/http/requests.rs +2 -2
- data/vendor/cfsolve/src/js/ast.rs +479 -263
- data/vendor/cfsolve/src/lib.rs +1 -0
- data/vendor/cfsolve/src/listing/fold.rs +19 -9
- data/vendor/cfsolve/src/listing/query.rs +1 -47
- data/vendor/cfsolve/src/performance_clock.rs +20 -1
- data/vendor/cfsolve/src/profiling.rs +49 -0
- data/vendor/cfsolve/src/script.rs +35 -24
- data/vendor/cfsolve/src/session/challenge_page.rs +41 -21
- data/vendor/cfsolve/src/session/channel.rs +2 -2
- data/vendor/cfsolve/src/session/initial.rs +12 -11
- data/vendor/cfsolve/src/session/probes.rs +1 -0
- data/vendor/cfsolve/src/session/sink.rs +26 -1
- data/vendor/cfsolve/src/session/tests/challenge_page.rs +36 -1
- data/vendor/cfsolve/src/session.rs +1 -1
- data/vendor/cfsolve/src/solver.rs +3 -1
- data/vendor/cfsolve/src/stage2/analysis/analyze.rs +33 -4
- data/vendor/cfsolve/src/stage2/analysis/audit.rs +4 -2
- data/vendor/cfsolve/src/stage2/analysis/discovery.rs +13 -2
- data/vendor/cfsolve/src/stage2/analysis/envelope.rs +2 -1
- data/vendor/cfsolve/src/stage2/audit_globals.rs +142 -74
- data/vendor/cfsolve/src/stage2/benchmark.rs +113 -41
- data/vendor/cfsolve/src/stage2/call_stack.rs +1564 -1041
- data/vendor/cfsolve/src/stage2/challenge_page.rs +3 -2
- data/vendor/cfsolve/src/stage2/fixtures/encoded_benchmark_worker.js +1 -0
- data/vendor/cfsolve/src/stage2/interaction/checkbox.rs +202 -150
- data/vendor/cfsolve/src/stage2/interaction/click.rs +2 -2
- data/vendor/cfsolve/src/stage2/interaction/plan.rs +5 -4
- data/vendor/cfsolve/src/stage2/payload/recover.rs +2 -2
- data/vendor/cfsolve/src/stage2/probes/clock.rs +1 -3
- data/vendor/cfsolve/src/stage2/probes/dates.rs +44 -0
- data/vendor/cfsolve/src/stage2/probes/parent_info_array.rs +326 -0
- data/vendor/cfsolve/src/stage2/probes/performance.rs +12 -8
- data/vendor/cfsolve/src/stage2/probes/stack.rs +117 -4
- data/vendor/cfsolve/src/stage2/probes/websocket_echo.rs +80 -2
- data/vendor/cfsolve/src/stage2/probes.rs +1 -0
- data/vendor/cfsolve/src/stage2/queries.rs +50 -0
- data/vendor/cfsolve/src/stage2/reference.rs +5 -0
- data/vendor/cfsolve/src/stage2/registry.rs +8 -2
- data/vendor/cfsolve/src/stage2/replay.rs +34 -3
- data/vendor/cfsolve/src/stage2/stylesheets.rs +35 -0
- data/vendor/cfsolve/src/stage2/syntax.rs +88 -0
- data/vendor/cfsolve/src/stage2/trace.rs +134 -108
- data/vendor/cfsolve/src/stage2/value_labels.rs +185 -0
- data/vendor/cfsolve/src/stage2/vocabulary.rs +112 -17
- data/vendor/cfsolve/src/stage2.rs +3 -0
- data/vendor/cfsolve/src/vm/disassemble.rs +75 -7
- data/vendor/cfsolve/src/vm/dispatch.rs +93 -65
- data/vendor/cfsolve/src/vm/encoded_names.rs +635 -0
- data/vendor/cfsolve/src/vm/extract.rs +550 -250
- data/vendor/cfsolve/src/vm/fixtures/free_functions/README.md +9 -0
- data/vendor/cfsolve/src/vm/fixtures/free_functions/machine.js +7438 -0
- data/vendor/cfsolve/src/vm/fixtures/free_functions/program.base64 +1 -0
- data/vendor/cfsolve/src/vm/interpret/access.rs +71 -50
- data/vendor/cfsolve/src/vm/interpret/builtin.rs +10 -10
- data/vendor/cfsolve/src/vm/interpret/call.rs +116 -52
- data/vendor/cfsolve/src/vm/interpret/compile.rs +79 -62
- data/vendor/cfsolve/src/vm/interpret/copies.rs +2 -2
- data/vendor/cfsolve/src/vm/interpret/evaluate/tests.rs +63 -42
- data/vendor/cfsolve/src/vm/interpret/evaluate.rs +65 -575
- data/vendor/cfsolve/src/vm/interpret/explore.rs +7 -10
- data/vendor/cfsolve/src/vm/interpret/formula.rs +131 -0
- data/vendor/cfsolve/src/vm/interpret/frozen.rs +3 -5
- data/vendor/cfsolve/src/vm/interpret/intrinsic.rs +160 -58
- data/vendor/cfsolve/src/vm/interpret/mix32.rs +189 -0
- data/vendor/cfsolve/src/vm/interpret/native_dispatch.rs +220 -0
- data/vendor/cfsolve/src/vm/interpret/pattern.rs +1765 -0
- data/vendor/cfsolve/src/vm/interpret/plan.rs +586 -338
- data/vendor/cfsolve/src/vm/interpret/register_helpers.rs +392 -0
- data/vendor/cfsolve/src/vm/interpret/run.rs +120 -45
- data/vendor/cfsolve/src/vm/interpret/semantics.rs +71 -46
- data/vendor/cfsolve/src/vm/interpret/setup.rs +80 -45
- data/vendor/cfsolve/src/vm/interpret/string_decoder.rs +482 -0
- data/vendor/cfsolve/src/vm/interpret.rs +9 -1
- data/vendor/cfsolve/src/vm/machine.rs +116 -51
- data/vendor/cfsolve/src/vm/output.rs +19 -6
- data/vendor/cfsolve/src/vm/packed_names.rs +11 -7
- data/vendor/cfsolve/src/vm/registers.rs +35 -21
- data/vendor/cfsolve/src/vm/session.rs +189 -121
- data/vendor/cfsolve/src/vm/value.rs +22 -19
- data/vendor/cfsolve/src/vm.rs +3 -1
- data/vendor/cfsolve/vendor/btls-sys/Cargo.toml +3 -0
- data/vendor/cfsolve/vendor/btls-sys/build/main.rs +20 -0
- metadata +106 -178
- data/THIRD_PARTY_LICENSES/Inflector-0.11.4/LICENSE.md +0 -22
- data/THIRD_PARTY_LICENSES/arrayvec-0.7.6/LICENSE-MIT +0 -25
- data/THIRD_PARTY_LICENSES/ascii-1.1.0/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/ascii-1.1.0/LICENSE-MIT +0 -22
- data/THIRD_PARTY_LICENSES/ast_node-5.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/better_scoped_tls-1.0.1/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/bitvec-1.0.1/LICENSE.txt +0 -21
- data/THIRD_PARTY_LICENSES/bstr-1.12.1/COPYING +0 -8
- data/THIRD_PARTY_LICENSES/bstr-1.12.1/LICENSE-MIT +0 -21
- data/THIRD_PARTY_LICENSES/cargo-platform-0.1.9/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/cargo_metadata-0.19.2/LICENSE-MIT +0 -23
- data/THIRD_PARTY_LICENSES/darling_macro-0.20.11/LICENSE +0 -21
- data/THIRD_PARTY_LICENSES/debugid-0.8.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/derive_builder-0.20.2/LICENSE-MIT +0 -21
- data/THIRD_PARTY_LICENSES/derive_builder_core-0.20.2/LICENSE-MIT +0 -21
- data/THIRD_PARTY_LICENSES/derive_builder_macro-0.20.2/LICENSE-MIT +0 -21
- data/THIRD_PARTY_LICENSES/foldhash-0.1.5/LICENSE +0 -19
- data/THIRD_PARTY_LICENSES/from_variant-3.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/funty-2.0.0/LICENSE.txt +0 -21
- data/THIRD_PARTY_LICENSES/hashbrown-0.16.1/LICENSE-MIT +0 -25
- data/THIRD_PARTY_LICENSES/heck-0.5.0/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/hermit-abi-0.5.2/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/hermit-abi-0.5.2/LICENSE-MIT +0 -23
- data/THIRD_PARTY_LICENSES/hstr-3.0.3/LICENSE +0 -202
- data/THIRD_PARTY_LICENSES/if_chain-1.0.3/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/if_chain-1.0.3/LICENSE-MIT +0 -25
- data/THIRD_PARTY_LICENSES/is-macro-0.3.7/LICENSE +0 -202
- data/THIRD_PARTY_LICENSES/itoa-1.0.17/LICENSE-MIT +0 -23
- data/THIRD_PARTY_LICENSES/jsonc-parser-0.26.3/LICENSE +0 -21
- data/THIRD_PARTY_LICENSES/lazy_static-1.5.0/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/lazy_static-1.5.0/LICENSE-MIT +0 -25
- data/THIRD_PARTY_LICENSES/lru-0.16.2/LICENSE +0 -21
- data/THIRD_PARTY_LICENSES/memchr-2.7.6/COPYING +0 -3
- data/THIRD_PARTY_LICENSES/memchr-2.7.6/LICENSE-MIT +0 -21
- data/THIRD_PARTY_LICENSES/miette-derive-7.6.0/LICENSE +0 -229
- data/THIRD_PARTY_LICENSES/normpath-0.2.0/COPYRIGHT +0 -7
- data/THIRD_PARTY_LICENSES/normpath-0.2.0/LICENSE-MIT +0 -21
- data/THIRD_PARTY_LICENSES/normpath-0.2.0/LICENSE-THIRD-PARTY +0 -210
- data/THIRD_PARTY_LICENSES/num_cpus-1.17.0/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/num_cpus-1.17.0/LICENSE-MIT +0 -20
- data/THIRD_PARTY_LICENSES/owo-colors-4.2.3/LICENSE +0 -21
- data/THIRD_PARTY_LICENSES/par-core-2.0.0/LICENSE +0 -202
- data/THIRD_PARTY_LICENSES/par-iter-2.0.0/LICENSE +0 -202
- data/THIRD_PARTY_LICENSES/path-clean-0.1.0/LICENSE-APACHE +0 -13
- data/THIRD_PARTY_LICENSES/path-clean-0.1.0/LICENSE-MIT +0 -18
- data/THIRD_PARTY_LICENSES/path-clean-1.0.1/LICENSE-APACHE +0 -13
- data/THIRD_PARTY_LICENSES/path-clean-1.0.1/LICENSE-MIT +0 -18
- data/THIRD_PARTY_LICENSES/pathdiff-0.2.3/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/pathdiff-0.2.3/LICENSE-MIT +0 -23
- data/THIRD_PARTY_LICENSES/petgraph-0.7.1/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/precomputed-map-0.2.0/LICENSE +0 -21
- data/THIRD_PARTY_LICENSES/preset_env_base-6.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/radium-0.7.0/LICENSE.txt +0 -21
- data/THIRD_PARTY_LICENSES/radix_fmt-1.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/rand-0.8.5/COPYRIGHT +0 -12
- data/THIRD_PARTY_LICENSES/rand-0.8.5/LICENSE-APACHE +0 -176
- data/THIRD_PARTY_LICENSES/rand_core-0.6.4/COPYRIGHT +0 -12
- data/THIRD_PARTY_LICENSES/rand_core-0.6.4/LICENSE-APACHE +0 -187
- data/THIRD_PARTY_LICENSES/rand_core-0.6.4/LICENSE-MIT +0 -26
- data/THIRD_PARTY_LICENSES/regress-0.10.5/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/regress-0.10.5/LICENSE-MIT +0 -21
- data/THIRD_PARTY_LICENSES/ryu-1.0.22/LICENSE-APACHE +0 -176
- data/THIRD_PARTY_LICENSES/ryu-1.0.22/LICENSE-BOOST +0 -23
- data/THIRD_PARTY_LICENSES/scoped-tls-1.0.1/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/scoped-tls-1.0.1/LICENSE-MIT +0 -25
- data/THIRD_PARTY_LICENSES/siphasher-0.3.11/COPYING +0 -7
- data/THIRD_PARTY_LICENSES/smallvec-1.15.1/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/smartstring-1.0.1/LICENCE.md +0 -355
- data/THIRD_PARTY_LICENSES/st-map-0.2.4/LICENSE +0 -202
- data/THIRD_PARTY_LICENSES/static-map-macro-0.3.4/LICENSE +0 -202
- data/THIRD_PARTY_LICENSES/string_enum-1.0.2/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/strsim-0.11.1/LICENSE +0 -23
- data/THIRD_PARTY_LICENSES/swc-52.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_allocator-4.0.1/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_atoms-9.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_common-18.0.1/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_compiler_base-45.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_config-3.1.2/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_config_macro-1.0.1/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_core-54.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_ast-19.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_codegen-21.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_codegen_macros-2.0.2/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_bugfixes-39.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_common-30.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2015-39.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2016-35.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2017-35.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2018-36.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2019-35.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2020-37.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2021-35.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_es2022-37.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_compat_es3-26.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_ext_transforms-25.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_hooks-0.3.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_loader-18.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_minifier-42.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_parser-32.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_preset_env-45.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transformer-6.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transforms-44.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transforms_base-35.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transforms_classes-35.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transforms_compat-40.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transforms_macros-1.0.1/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transforms_module-38.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transforms_optimization-37.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transforms_proposal-35.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transforms_react-38.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_transforms_typescript-38.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_usage_analyzer-27.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_utils-25.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_ecma_visit-19.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_eq_ignore_macros-1.0.1/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_error_reporters-20.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_macros_common-1.0.1/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_node_comments-18.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_timer-1.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_trace_macro-2.0.2/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_transform_common-12.0.0/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/swc_visit-2.0.1/LICENSE +0 -201
- data/THIRD_PARTY_LICENSES/tap-1.0.1/LICENSE.txt +0 -21
- data/THIRD_PARTY_LICENSES/tracing-0.1.44/LICENSE +0 -25
- data/THIRD_PARTY_LICENSES/tracing-attributes-0.1.31/LICENSE +0 -25
- data/THIRD_PARTY_LICENSES/tracing-core-0.1.36/LICENSE +0 -25
- data/THIRD_PARTY_LICENSES/triomphe-0.1.15/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/triomphe-0.1.15/LICENSE-MIT +0 -27
- data/THIRD_PARTY_LICENSES/unicode-width-0.1.14/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/unicode-width-0.1.14/LICENSE-MIT +0 -25
- data/THIRD_PARTY_LICENSES/uuid-1.19.0/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/uuid-1.19.0/LICENSE-MIT +0 -26
- data/THIRD_PARTY_LICENSES/vergen-9.0.6/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/vergen-9.0.6/LICENSE-MIT +0 -25
- data/THIRD_PARTY_LICENSES/vergen-lib-0.1.6/LICENSE-APACHE +0 -201
- data/THIRD_PARTY_LICENSES/vergen-lib-0.1.6/LICENSE-MIT +0 -25
- data/THIRD_PARTY_LICENSES/wyz-0.5.1/LICENSE.txt +0 -21
- data/vendor/cfsolve/src/deobfuscate/analysis/globals.rs +0 -33
- data/vendor/cfsolve/src/deobfuscate/analysis/names.rs +0 -52
- /data/THIRD_PARTY_LICENSES/{derive_builder-0.20.2 → cobs-0.3.0}/LICENSE-APACHE +0 -0
- /data/THIRD_PARTY_LICENSES/{compact_str-0.7.1 → compact_str-0.10.0}/LICENSE +0 -0
- /data/THIRD_PARTY_LICENSES/{ahash-0.8.12 → embedded-io-0.4.0}/LICENSE-APACHE +0 -0
- /data/THIRD_PARTY_LICENSES/{arrayvec-0.7.6 → embedded-io-0.6.1}/LICENSE-APACHE +0 -0
- /data/THIRD_PARTY_LICENSES/{normpath-0.2.0 → escape-simd-0.1.1}/LICENSE-APACHE +0 -0
- /data/THIRD_PARTY_LICENSES/{bstr-1.12.1 → itertools-0.15.0}/LICENSE-APACHE +0 -0
- /data/THIRD_PARTY_LICENSES/{petgraph-0.7.1 → itertools-0.15.0}/LICENSE-MIT +0 -0
- /data/THIRD_PARTY_LICENSES/{itoa-1.0.17 → itoa-1.0.19}/LICENSE-APACHE +0 -0
- /data/THIRD_PARTY_LICENSES/{camino-1.2.2 → itoa-1.0.19}/LICENSE-MIT +0 -0
- /data/THIRD_PARTY_LICENSES/{globset-0.4.18 → memchr-2.8.3}/COPYING +0 -0
- /data/THIRD_PARTY_LICENSES/{globset-0.4.18 → memchr-2.8.3}/LICENSE-MIT +0 -0
- /data/THIRD_PARTY_LICENSES/{camino-1.2.2 → num-bigint-0.5.1}/LICENSE-APACHE +0 -0
- /data/THIRD_PARTY_LICENSES/{swc_sourcemap-9.3.4 → oxc_sourcemap-9.0.0}/LICENSE +0 -0
- /data/THIRD_PARTY_LICENSES/{phf-0.11.3 → phf-0.14.0}/LICENSE +0 -0
- /data/THIRD_PARTY_LICENSES/{phf_generator-0.11.3 → phf_generator-0.14.0}/LICENSE +0 -0
- /data/THIRD_PARTY_LICENSES/{phf_macros-0.11.3 → phf_macros-0.14.0}/LICENSE +0 -0
- /data/THIRD_PARTY_LICENSES/{phf_shared-0.11.3 → phf_shared-0.14.0}/LICENSE +0 -0
- /data/THIRD_PARTY_LICENSES/{fixedbitset-0.5.7 → postcard-1.1.3}/LICENSE-APACHE +0 -0
- /data/THIRD_PARTY_LICENSES/{hashbrown-0.14.5 → smallvec-1.16.5}/LICENSE-APACHE +0 -0
- /data/THIRD_PARTY_LICENSES/{smallvec-1.15.1 → smallvec-1.16.5}/LICENSE-MIT +0 -0
- /data/THIRD_PARTY_LICENSES/{unicode-width-0.1.14 → unicode-segmentation-1.13.3}/COPYRIGHT +0 -0
- /data/THIRD_PARTY_LICENSES/{hashbrown-0.16.1 → unicode-segmentation-1.13.3}/LICENSE-APACHE +0 -0
- /data/THIRD_PARTY_LICENSES/{heck-0.5.0 → unicode-segmentation-1.13.3}/LICENSE-MIT +0 -0
- /data/THIRD_PARTY_LICENSES/{cargo-platform-0.1.9 → zmij_ecma-1.0.23}/LICENSE-MIT +0 -0
|
@@ -2,598 +2,418 @@
|
|
|
2
2
|
//!
|
|
3
3
|
//! ```js
|
|
4
4
|
//! h = { add: function (a, b) { return a + b; }, key: "name" };
|
|
5
|
-
//! h.add(x, 1); h.key; // becomes
|
|
5
|
+
//! h.add(x, 1); h.key; // becomes x + 1; "name"
|
|
6
6
|
//! ```
|
|
7
7
|
//!
|
|
8
8
|
//! Requires each property to be defined once on an object that does not escape (see
|
|
9
|
-
//! [`Properties::
|
|
10
|
-
//! [`
|
|
9
|
+
//! [`Properties::stable`]), and calls that preserve argument evaluation and order (see
|
|
10
|
+
//! [`substitute`]). Hoisted function bodies use only objects known wherever they can be called
|
|
11
11
|
//! (see [`hoisting`]).
|
|
12
12
|
|
|
13
|
-
use std::iter;
|
|
14
13
|
use std::rc::Rc;
|
|
14
|
+
use std::{iter, mem};
|
|
15
15
|
|
|
16
|
-
use
|
|
17
|
-
use
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
FnExpr, ForStmt, Function, Id, Ident, Lit, MemberExpr, MemberProp, MetaPropExpr, Number,
|
|
21
|
-
ObjectLit, Program, ReturnStmt, SeqExpr, SimpleAssignTarget, Stmt, Super, ThisExpr, UnaryOp,
|
|
22
|
-
VarDeclOrExpr,
|
|
16
|
+
use oxc::allocator::{CloneIn, GetAllocator, Vec as ArenaVec};
|
|
17
|
+
use oxc::ast::ast::{
|
|
18
|
+
Argument, AssignmentExpression, BlockStatement, Expression, Function, IdentifierReference,
|
|
19
|
+
ObjectPropertyKind, Program, SequenceExpression, Statement, VariableDeclarator,
|
|
23
20
|
};
|
|
24
|
-
use
|
|
25
|
-
|
|
26
|
-
use
|
|
27
|
-
use
|
|
28
|
-
|
|
29
|
-
use super::analysis::
|
|
30
|
-
use
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
let mut inline = InlineHelpers {
|
|
38
|
-
bindings,
|
|
39
|
-
properties,
|
|
40
|
-
globals,
|
|
41
|
-
hoisting: Hoisting::default(),
|
|
42
|
-
lists: 0,
|
|
43
|
-
defined_in: HashMap::default(),
|
|
44
|
-
changed: false,
|
|
45
|
-
};
|
|
46
|
-
program.visit_mut_with(&mut inline);
|
|
47
|
-
inline.changed
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/// Properties of one helper object that can replace property reads.
|
|
21
|
+
use oxc::ast_visit::{Visit, VisitMut, walk, walk_mut};
|
|
22
|
+
use oxc::syntax::operator::{AssignmentOperator, UnaryOperator};
|
|
23
|
+
use oxc::syntax::scope::ScopeFlags;
|
|
24
|
+
use oxc_ecmascript::side_effects::MayHaveSideEffects;
|
|
25
|
+
|
|
26
|
+
use super::analysis::effects::Effects;
|
|
27
|
+
use super::analysis::hoisting::{self, Facts, Hoisted, Hoisting, List};
|
|
28
|
+
use super::analysis::properties::Properties;
|
|
29
|
+
use super::analysis::unconditional::Unconditional;
|
|
30
|
+
use super::analysis::{Analysis, Id};
|
|
31
|
+
use super::{HashMap, ast};
|
|
32
|
+
|
|
33
|
+
/// The helper objects and functions known at one point, indexed by binding identity.
|
|
51
34
|
#[derive(Clone, Default)]
|
|
52
|
-
struct
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
impl ObjectProperties {
|
|
58
|
-
/// Every inlinable property of a helper object literal.
|
|
59
|
-
fn collect(object: &ObjectLit) -> Self {
|
|
60
|
-
let mut properties = Self::default();
|
|
61
|
-
for property in ast::key_values(object) {
|
|
62
|
-
if let Some(name) = analysis::prop_key(&property.key) {
|
|
63
|
-
properties.insert(name, &property.value);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
properties
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/// Records substitutable properties. Leaves unsupported properties as object reads.
|
|
70
|
-
fn insert(&mut self, name: Atom, value: &Expr) {
|
|
71
|
-
match value {
|
|
72
|
-
Expr::Fn(function) => {
|
|
73
|
-
self.functions.insert(name, function.clone());
|
|
74
|
-
}
|
|
75
|
-
// A regular expression carries state and identity, so it cannot
|
|
76
|
-
// stand in for every read.
|
|
77
|
-
Expr::Lit(Lit::Regex(_)) => {}
|
|
78
|
-
// Decoded strings may print as templates without substitutions.
|
|
79
|
-
Expr::Tpl(template) if !template.exprs.is_empty() => {}
|
|
80
|
-
Expr::Lit(_) | Expr::Tpl(_) => {
|
|
81
|
-
self.literals.insert(name, value.clone());
|
|
82
|
-
}
|
|
83
|
-
_ => {}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
35
|
+
struct Helpers<'a> {
|
|
36
|
+
/// Known object properties, shared between snapshots until a new definition extends them.
|
|
37
|
+
objects: HashMap<Id, Rc<HashMap<&'a str, &'a Expression<'a>>>>,
|
|
38
|
+
/// Single-return functions assigned to bindings with exactly one write.
|
|
39
|
+
functions: HashMap<Id, &'a Function<'a>>,
|
|
86
40
|
}
|
|
87
41
|
|
|
88
|
-
|
|
89
|
-
#[derive(Clone, Default)]
|
|
90
|
-
struct Helpers {
|
|
91
|
-
objects: HashMap<Id, Rc<ObjectProperties>>,
|
|
92
|
-
/// Functions assigned to a binding written once.
|
|
93
|
-
functions: HashMap<Id, Rc<FnExpr>>,
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
/// At a control-flow join, keeps the objects and properties known on both paths.
|
|
97
|
-
impl HoistFacts for Helpers {
|
|
42
|
+
impl Facts for Helpers<'_> {
|
|
98
43
|
fn meet(&mut self, other: &Self) {
|
|
99
44
|
self.objects.retain(|id, properties| {
|
|
100
|
-
let Some(
|
|
45
|
+
let Some(other) = other.objects.get(id) else {
|
|
101
46
|
return false;
|
|
102
47
|
};
|
|
103
|
-
if !Rc::ptr_eq(properties,
|
|
104
|
-
|
|
105
|
-
properties
|
|
106
|
-
.functions
|
|
107
|
-
.retain(|name, _| theirs.functions.contains_key(name));
|
|
108
|
-
properties
|
|
109
|
-
.literals
|
|
110
|
-
.retain(|name, _| theirs.literals.contains_key(name));
|
|
48
|
+
if !Rc::ptr_eq(properties, other) {
|
|
49
|
+
Rc::make_mut(properties).retain(|key, _| other.contains_key(key))
|
|
111
50
|
}
|
|
112
51
|
true
|
|
113
52
|
});
|
|
114
|
-
self.functions.meet(&other.functions)
|
|
53
|
+
self.functions.meet(&other.functions)
|
|
115
54
|
}
|
|
116
55
|
}
|
|
117
56
|
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
properties
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
57
|
+
/// Tracks helper objects and aliases only while their bindings have exactly one write.
|
|
58
|
+
pub(super) fn run<'a>(program: &mut Program<'a>, analysis: &mut Analysis<'a>) -> bool {
|
|
59
|
+
let properties = Properties::collect(program, analysis);
|
|
60
|
+
let mut inline = Inline {
|
|
61
|
+
analysis,
|
|
62
|
+
properties: &properties,
|
|
63
|
+
hoisting: Hoisting::default(),
|
|
64
|
+
lists: 0,
|
|
65
|
+
list: 0,
|
|
66
|
+
defined_in: HashMap::default(),
|
|
67
|
+
changed: false,
|
|
68
|
+
};
|
|
69
|
+
inline.visit_program(program);
|
|
70
|
+
inline.changed
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
struct Inline<'b, 'a> {
|
|
74
|
+
analysis: &'b Analysis<'a>,
|
|
75
|
+
properties: &'b Properties<'a>,
|
|
76
|
+
/// Aliases receive the properties known when their assignment runs.
|
|
77
|
+
hoisting: Hoisting<Rc<Helpers<'a>>>,
|
|
78
|
+
/// Counts entered statement lists and sequences, to identify each one.
|
|
125
79
|
lists: usize,
|
|
126
|
-
/// The statement list or sequence
|
|
80
|
+
/// The innermost statement list or sequence being visited.
|
|
81
|
+
list: usize,
|
|
82
|
+
/// The list or sequence that defines each object; only definitions in that same list may
|
|
83
|
+
/// extend it.
|
|
127
84
|
defined_in: HashMap<Id, usize>,
|
|
128
85
|
changed: bool,
|
|
129
86
|
}
|
|
130
87
|
|
|
131
|
-
impl
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
88
|
+
impl<'a> Hoisted<'a> for Inline<'_, 'a> {
|
|
89
|
+
type Facts = Rc<Helpers<'a>>;
|
|
90
|
+
|
|
91
|
+
fn analysis(&self) -> &Analysis<'a> {
|
|
92
|
+
self.analysis
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
fn hoisting(&mut self) -> &mut Hoisting<Self::Facts> {
|
|
96
|
+
&mut self.hoisting
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
impl<'a> Inline<'_, 'a> {
|
|
101
|
+
fn save_function(&mut self, id: Id, function: &Function<'a>) {
|
|
102
|
+
let function = self.analysis.save(function);
|
|
103
|
+
self.facts_mut().functions.insert(id, function);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/// Unshares the current facts before a change.
|
|
107
|
+
fn facts_mut(&mut self) -> &mut Helpers<'a> {
|
|
108
|
+
Rc::make_mut(&mut self.hoisting.facts)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/// Records a single-write object, function, or alias with the properties known at this
|
|
112
|
+
/// assignment.
|
|
113
|
+
fn record(&mut self, id: Id, value: &Expression<'a>) {
|
|
114
|
+
if !self.analysis.bindings.is_written_once(&id) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
match value.get_inner_expression() {
|
|
118
|
+
Expression::ObjectExpression(object) => {
|
|
119
|
+
let mut properties = HashMap::default();
|
|
120
|
+
for property in &object.properties {
|
|
121
|
+
if let ObjectPropertyKind::ObjectProperty(property) = property
|
|
122
|
+
&& let Some(key) = ast::key(&property.key, property.computed)
|
|
123
|
+
{
|
|
124
|
+
properties.insert(key, self.analysis.save(&property.value));
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
self.facts_mut().objects.insert(id, Rc::new(properties));
|
|
147
128
|
}
|
|
148
|
-
|
|
149
|
-
self.
|
|
150
|
-
statement.cases.visit_mut_with(self);
|
|
129
|
+
Expression::FunctionExpression(function) if ast::returned(function).is_some() => {
|
|
130
|
+
self.save_function(id, function)
|
|
151
131
|
}
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
}
|
|
132
|
+
Expression::Identifier(identifier) => {
|
|
133
|
+
if let Some(source) = self.analysis.id(identifier)
|
|
134
|
+
&& let Some(object) = self.hoisting.facts.objects.get(&source).cloned()
|
|
135
|
+
{
|
|
136
|
+
self.facts_mut().objects.insert(id, object);
|
|
158
137
|
}
|
|
159
138
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
body,
|
|
165
|
-
..
|
|
166
|
-
}) => {
|
|
167
|
-
self.top_level(init, list);
|
|
168
|
-
test.visit_mut_with(self);
|
|
169
|
-
update.visit_mut_with(self);
|
|
170
|
-
body.visit_mut_with(self);
|
|
139
|
+
_ => {
|
|
140
|
+
if let Some(Expression::FunctionExpression(function)) = self.stable_value(value) {
|
|
141
|
+
self.save_function(id, function)
|
|
142
|
+
}
|
|
171
143
|
}
|
|
172
|
-
statement => statement.visit_mut_with(self),
|
|
173
144
|
}
|
|
174
145
|
}
|
|
175
146
|
|
|
176
|
-
///
|
|
177
|
-
///
|
|
178
|
-
fn
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
return;
|
|
184
|
-
}
|
|
185
|
-
expression.visit_mut_with(self);
|
|
186
|
-
let Expr::Assign(assignment) = expression else {
|
|
187
|
-
return;
|
|
188
|
-
};
|
|
189
|
-
if let Some(target) = assignment.left.as_ident()
|
|
190
|
-
&& self.hoisting.facts.objects.contains_key(&target.to_id())
|
|
191
|
-
{
|
|
192
|
-
self.defined_in.insert(target.to_id(), list);
|
|
193
|
-
} else if assignment.op == AssignOp::Assign
|
|
194
|
-
&& let AssignTarget::Simple(SimpleAssignTarget::Member(member)) = &assignment.left
|
|
195
|
-
&& let Some(identifier) = member.obj.as_ident()
|
|
196
|
-
&& let Some(property) = analysis::member_key(&member.prop)
|
|
197
|
-
&& self.defined_in.get(&identifier.to_id()) == Some(&list)
|
|
198
|
-
&& let Some(object) = self.hoisting.facts.objects.get_mut(&identifier.to_id())
|
|
199
|
-
{
|
|
200
|
-
// Only property assignments beside the object's definition safely extend it.
|
|
201
|
-
// Elsewhere, a read might run before the assignment.
|
|
202
|
-
Rc::make_mut(object).insert(property, &assignment.right);
|
|
147
|
+
/// Resolves a static member read only when both property stability and execution-order
|
|
148
|
+
/// facts hold.
|
|
149
|
+
fn stable_value(&self, expression: &Expression<'a>) -> Option<&'a Expression<'a>> {
|
|
150
|
+
let member = expression.as_member_expression()?;
|
|
151
|
+
let id = self.analysis.expression_id(member.object())?;
|
|
152
|
+
let key = member.static_property_name()?.as_str()?;
|
|
153
|
+
if !self.properties.stable(id, key, self.analysis) {
|
|
154
|
+
return None;
|
|
203
155
|
}
|
|
156
|
+
self.hoisting.facts.objects.get(&id)?.get(key).copied()
|
|
204
157
|
}
|
|
205
158
|
|
|
206
|
-
///
|
|
207
|
-
fn
|
|
208
|
-
let
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
159
|
+
/// Returns a known helper function that a call can inline.
|
|
160
|
+
fn callee(&self, callee: &Expression<'a>) -> Option<&'a Function<'a>> {
|
|
161
|
+
let Some(reference) = callee.get_identifier_reference() else {
|
|
162
|
+
return match self.stable_value(callee)? {
|
|
163
|
+
Expression::FunctionExpression(function) => Some(&**function),
|
|
164
|
+
_ => None,
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
let id = self.analysis.id(reference)?;
|
|
168
|
+
let function = *self.hoisting.facts.functions.get(&id)?;
|
|
169
|
+
names_only(function, id, self.analysis).then_some(function)
|
|
214
170
|
}
|
|
215
171
|
}
|
|
216
172
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
173
|
+
/// Only property definitions in the object's defining list extend its known properties.
|
|
174
|
+
impl<'a> Unconditional<'a> for Inline<'_, 'a> {
|
|
175
|
+
fn assigned(&mut self, assignment: &AssignmentExpression<'a>) {
|
|
176
|
+
if let Some(id) = self.analysis.target_id(&assignment.left) {
|
|
177
|
+
self.record(id, &assignment.right);
|
|
178
|
+
self.defined_in.insert(id, self.list);
|
|
179
|
+
} else if let Some(member) = assignment.left.as_member_expression()
|
|
180
|
+
&& let Some(id) = self.analysis.expression_id(member.object())
|
|
181
|
+
&& let Some(key) = member.static_property_name().and_then(|key| key.as_str())
|
|
182
|
+
&& self.defined_in.get(&id) == Some(&self.list)
|
|
183
|
+
&& self.hoisting.facts.objects.contains_key(&id)
|
|
184
|
+
{
|
|
185
|
+
let value = self.analysis.save(&assignment.right);
|
|
186
|
+
let objects = &mut self.facts_mut().objects;
|
|
187
|
+
let properties = objects.get_mut(&id).expect("the object is known");
|
|
188
|
+
Rc::make_mut(properties).insert(key, value);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
220
191
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
&&
|
|
192
|
+
fn declared(&mut self, declarator: &VariableDeclarator<'a>) {
|
|
193
|
+
if let Some(id) = ast::pattern_id(&declarator.id)
|
|
194
|
+
&& let Some(value) = &declarator.init
|
|
224
195
|
{
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
.right
|
|
228
|
-
.as_member()
|
|
229
|
-
.and_then(|member| self.stable(member))
|
|
230
|
-
.and_then(|(object, property)| object.functions.get(&property))
|
|
231
|
-
.cloned();
|
|
232
|
-
// Track object definitions, helper functions, and copies of objects and helper methods
|
|
233
|
-
// by binding.
|
|
234
|
-
let helpers = &mut self.hoisting.facts;
|
|
235
|
-
if let Some(method) = method {
|
|
236
|
-
helpers.functions.insert(target, Rc::new(method));
|
|
237
|
-
} else if let Some(object) = assignment.right.as_object() {
|
|
238
|
-
let properties = Rc::new(ObjectProperties::collect(object));
|
|
239
|
-
helpers.objects.insert(target, properties);
|
|
240
|
-
} else if let Some(function) = assignment.right.as_fn_expr() {
|
|
241
|
-
helpers.functions.insert(target, Rc::new(function.clone()));
|
|
242
|
-
} else if let Some(alias) = assignment.right.as_ident()
|
|
243
|
-
&& let Some(object) = helpers.objects.get(&alias.to_id()).cloned()
|
|
244
|
-
{
|
|
245
|
-
helpers.objects.insert(target, object);
|
|
246
|
-
}
|
|
196
|
+
self.record(id, value);
|
|
197
|
+
self.defined_in.insert(id, self.list);
|
|
247
198
|
}
|
|
248
199
|
}
|
|
200
|
+
}
|
|
249
201
|
|
|
250
|
-
|
|
251
|
-
fn
|
|
202
|
+
impl<'a> VisitMut<'a> for Inline<'_, 'a> {
|
|
203
|
+
fn visit_statements(&mut self, statements: &mut ArenaVec<'a, Statement<'a>>) {
|
|
252
204
|
self.lists += 1;
|
|
253
|
-
let
|
|
254
|
-
// Function declarations are available from the start of a function body. Top-level
|
|
255
|
-
// declarations can be replaced by other scripts.
|
|
205
|
+
let outer = mem::replace(&mut self.list, self.lists);
|
|
256
206
|
if self.hoisting.list == List::FunctionBody {
|
|
257
207
|
for statement in statements.iter() {
|
|
258
|
-
if let
|
|
259
|
-
&&
|
|
208
|
+
if let Statement::FunctionDeclaration(function) = statement
|
|
209
|
+
&& let Some(id) = ast::function_id(function)
|
|
210
|
+
&& self.analysis.bindings.is_written_once(&id)
|
|
211
|
+
&& ast::returned(function).is_some()
|
|
260
212
|
{
|
|
261
|
-
|
|
262
|
-
self.hoisting
|
|
263
|
-
.facts
|
|
264
|
-
.functions
|
|
265
|
-
.insert(declaration.ident.to_id(), function);
|
|
213
|
+
self.save_function(id, function)
|
|
266
214
|
}
|
|
267
215
|
}
|
|
268
216
|
}
|
|
269
|
-
hoisting::
|
|
270
|
-
|
|
271
|
-
});
|
|
217
|
+
hoisting::statements(self, statements, Self::unconditional_statement);
|
|
218
|
+
self.list = outer;
|
|
272
219
|
}
|
|
273
220
|
|
|
274
|
-
fn
|
|
275
|
-
hoisting
|
|
221
|
+
fn visit_function(&mut self, function: &mut Function<'a>, _: ScopeFlags) {
|
|
222
|
+
let outer = self.hoisting.facts.clone();
|
|
223
|
+
hoisting::function(self, function);
|
|
224
|
+
self.hoisting.facts = outer;
|
|
276
225
|
}
|
|
277
226
|
|
|
278
|
-
fn
|
|
279
|
-
hoisting::
|
|
227
|
+
fn visit_block_statement(&mut self, block: &mut BlockStatement<'a>) {
|
|
228
|
+
hoisting::block(self, block)
|
|
280
229
|
}
|
|
281
230
|
|
|
282
|
-
fn
|
|
283
|
-
hoisting::reference(self,
|
|
231
|
+
fn visit_identifier_reference(&mut self, reference: &mut IdentifierReference<'a>) {
|
|
232
|
+
hoisting::reference(self, reference)
|
|
284
233
|
}
|
|
285
234
|
|
|
286
|
-
|
|
287
|
-
fn visit_mut_seq_expr(&mut self, sequence: &mut SeqExpr) {
|
|
235
|
+
fn visit_sequence_expression(&mut self, sequence: &mut SequenceExpression<'a>) {
|
|
288
236
|
self.lists += 1;
|
|
289
|
-
let
|
|
290
|
-
for
|
|
291
|
-
self.
|
|
237
|
+
let outer = mem::replace(&mut self.list, self.lists);
|
|
238
|
+
for expression in &mut sequence.expressions {
|
|
239
|
+
self.unconditional_expression(expression)
|
|
292
240
|
}
|
|
241
|
+
self.list = outer;
|
|
293
242
|
}
|
|
294
243
|
|
|
295
|
-
fn
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
if let Expr::Call(call) = expression
|
|
300
|
-
&& let Callee::Expr(callee) = &call.callee
|
|
301
|
-
&& let Expr::Member(member) = &**callee
|
|
302
|
-
&& let Some((object, property)) = self.stable(member)
|
|
303
|
-
&& let Some(function) = object.functions.get(&property)
|
|
304
|
-
&& let Some(result) = ast::returned(&function.function)
|
|
305
|
-
&& can_inline(function, result, &call.args, self.globals)
|
|
306
|
-
&& let Some(replacement) = substitute(function, result, &call.args)
|
|
244
|
+
fn visit_assignment_expression(&mut self, assignment: &mut AssignmentExpression<'a>) {
|
|
245
|
+
walk_mut::walk_assignment_expression(self, assignment);
|
|
246
|
+
if assignment.operator == AssignmentOperator::Assign
|
|
247
|
+
&& let Some(id) = self.analysis.target_id(&assignment.left)
|
|
307
248
|
{
|
|
308
|
-
|
|
309
|
-
self.changed = true;
|
|
249
|
+
self.record(id, &assignment.right)
|
|
310
250
|
}
|
|
251
|
+
}
|
|
311
252
|
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
if let
|
|
315
|
-
&& let Some(function) = self.
|
|
316
|
-
&& let Some(
|
|
317
|
-
&&
|
|
318
|
-
&& can_inline(function, result, args, self.globals)
|
|
319
|
-
&& let Some(replacement) = substitute(function, result, args)
|
|
253
|
+
fn visit_expression(&mut self, expression: &mut Expression<'a>) {
|
|
254
|
+
walk_mut::walk_expression(self, expression);
|
|
255
|
+
if let Expression::CallExpression(call) = expression
|
|
256
|
+
&& let Some(function) = self.callee(&call.callee)
|
|
257
|
+
&& let Some(value) = ast::returned(function)
|
|
258
|
+
&& let Some(replacement) = substitute(function, value, &call.arguments, self.analysis)
|
|
320
259
|
{
|
|
321
260
|
*expression = replacement;
|
|
322
261
|
self.changed = true;
|
|
262
|
+
return;
|
|
323
263
|
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
if let Some(member) = expression.as_member()
|
|
327
|
-
&& let Some((object, property)) = self.stable(member)
|
|
328
|
-
&& let Some(value) = object.literals.get(&property)
|
|
264
|
+
if let Some(value) = self.stable_value(expression)
|
|
265
|
+
&& ast::is_constant(value)
|
|
329
266
|
{
|
|
330
|
-
*expression = value.
|
|
267
|
+
*expression = value.clone_in_with_semantic_ids(self.analysis.allocator());
|
|
331
268
|
self.changed = true;
|
|
332
269
|
}
|
|
333
270
|
}
|
|
334
271
|
}
|
|
335
272
|
|
|
336
|
-
/// Checks that
|
|
337
|
-
///
|
|
338
|
-
fn names_only(
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
all_allowed: bool,
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
impl Visit for Names {
|
|
346
|
-
fn visit_ident(&mut self, identifier: &Ident) {
|
|
347
|
-
self.all_allowed &= self.scopes.contains(&identifier.ctxt)
|
|
348
|
-
|| self.parameters.contains(&identifier.to_id());
|
|
349
|
-
}
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
let mut names = Names {
|
|
353
|
-
parameters: function
|
|
354
|
-
.params
|
|
355
|
-
.iter()
|
|
356
|
-
.filter_map(|parameter| Some(parameter.pat.as_ident()?.to_id()))
|
|
357
|
-
.collect(),
|
|
358
|
-
scopes: [scope, globals.unresolved],
|
|
359
|
-
all_allowed: true,
|
|
273
|
+
/// Checks that a direct helper's result names only parameters, globals, or bindings visible to
|
|
274
|
+
/// callers.
|
|
275
|
+
fn names_only(function: &Function<'_>, binding: Id, analysis: &Analysis<'_>) -> bool {
|
|
276
|
+
let (Some(parameters), Some(value)) = (ast::parameters(function), ast::returned(function))
|
|
277
|
+
else {
|
|
278
|
+
return false;
|
|
360
279
|
};
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
&mut self.hoisting
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
/// Checks whether substituting `args` into `result` preserves evaluations and their order.
|
|
374
|
-
#[must_use]
|
|
375
|
-
fn can_inline(function: &FnExpr, result: &Expr, args: &[ExprOrSpread], globals: Globals) -> bool {
|
|
376
|
-
function.function.params.len() >= args.len()
|
|
377
|
-
&& trailing_parameters_unused(function, result, args.len())
|
|
378
|
-
&& args.iter().all(|argument| argument.spread.is_none())
|
|
379
|
-
&& !function.function.is_async
|
|
380
|
-
&& !function.function.is_generator
|
|
381
|
-
&& !uses_own_context(result)
|
|
382
|
-
&& (evaluates_arguments_safely(function, result, args, globals)
|
|
383
|
-
|| args
|
|
384
|
-
.iter()
|
|
385
|
-
.all(|argument| !effects::may_have_effects(&argument.expr, globals)))
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
/// Parameters without arguments disappear when the call is inlined. Allow this only for unused
|
|
389
|
-
/// plain identifiers. Destructuring may evaluate defaults, and referenced parameters would have
|
|
390
|
-
/// no binding after substitution.
|
|
391
|
-
fn trailing_parameters_unused(function: &FnExpr, result: &Expr, argument_count: usize) -> bool {
|
|
392
|
-
let params = &function.function.params;
|
|
393
|
-
let trailing = names::parameter_ids(¶ms[argument_count.min(params.len())..]);
|
|
394
|
-
trailing.is_some_and(|trailing| !names::mentions(result, &trailing))
|
|
280
|
+
let scope = analysis.scope(binding);
|
|
281
|
+
let uses = Effects::expression(value, analysis);
|
|
282
|
+
uses.reads.iter().chain(&uses.writes).all(|id| {
|
|
283
|
+
let mut ancestors = analysis.scoping.scope_ancestors(scope);
|
|
284
|
+
id.is_global()
|
|
285
|
+
|| parameters.contains(id)
|
|
286
|
+
|| ancestors.any(|scope| scope == analysis.scope(*id))
|
|
287
|
+
})
|
|
395
288
|
}
|
|
396
289
|
|
|
397
|
-
/// Checks for `this`, `arguments`, `new.target`, or `super` outside nested functions
|
|
398
|
-
///
|
|
399
|
-
fn
|
|
290
|
+
/// Checks for `this`, `arguments`, `new.target`, or `super` outside ordinary nested functions.
|
|
291
|
+
/// Inlining would otherwise resolve these names in the caller instead of the helper.
|
|
292
|
+
fn own_context(expression: &Expression<'_>) -> bool {
|
|
400
293
|
struct Context(bool);
|
|
401
294
|
|
|
402
|
-
impl Visit for Context {
|
|
403
|
-
fn
|
|
404
|
-
self.0
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
self.0 = true;
|
|
295
|
+
impl<'a> Visit<'a> for Context {
|
|
296
|
+
fn visit_expression(&mut self, expression: &Expression<'a>) {
|
|
297
|
+
self.0 |= match expression {
|
|
298
|
+
Expression::ThisExpression(_) | Expression::Super(_) | Expression::NewTarget(_) => {
|
|
299
|
+
true
|
|
300
|
+
}
|
|
301
|
+
Expression::Identifier(identifier) => ast::is_arguments(identifier),
|
|
302
|
+
_ => false,
|
|
303
|
+
};
|
|
304
|
+
walk::walk_expression(self, expression)
|
|
413
305
|
}
|
|
414
306
|
|
|
415
|
-
fn visit_function(&mut self, _: &Function) {}
|
|
307
|
+
fn visit_function(&mut self, _: &Function<'a>, _: ScopeFlags) {}
|
|
416
308
|
}
|
|
417
309
|
|
|
418
310
|
let mut context = Context(false);
|
|
419
|
-
|
|
420
|
-
context.0
|
|
311
|
+
context.visit_expression(expression);
|
|
312
|
+
context.0
|
|
421
313
|
}
|
|
422
314
|
|
|
423
|
-
///
|
|
424
|
-
///
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
)
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
.function
|
|
439
|
-
.params
|
|
440
|
-
.iter()
|
|
441
|
-
.take(args.len())
|
|
442
|
-
.map(|parameter| Some(parameter.pat.as_ident()?.to_id()))
|
|
443
|
-
.collect();
|
|
444
|
-
// The parameter each operand reads, in evaluation order.
|
|
445
|
-
let mut order = Vec::with_capacity(args.len());
|
|
446
|
-
for operand in operands {
|
|
447
|
-
let operand = operand.as_ident().map(Ident::to_id);
|
|
448
|
-
let Some(index) = unread
|
|
449
|
-
.iter()
|
|
450
|
-
.position(|parameter| parameter.is_some() && *parameter == operand)
|
|
451
|
-
else {
|
|
452
|
-
return false;
|
|
453
|
-
};
|
|
454
|
-
unread[index] = None;
|
|
455
|
-
order.push(index);
|
|
456
|
-
}
|
|
457
|
-
for (position, &first) in order.iter().enumerate() {
|
|
458
|
-
for &second in &order[position + 1..] {
|
|
459
|
-
if first > second && !commutes(&args[first].expr, &args[second].expr, globals) {
|
|
460
|
-
return false;
|
|
461
|
-
}
|
|
315
|
+
/// Lists operands that always run, in evaluation order. Looks through outer unary operators
|
|
316
|
+
/// that run after their operands. Excludes `delete`, which does not evaluate a value read.
|
|
317
|
+
fn operands<'b, 'a>(expression: &'b Expression<'a>) -> Option<Vec<&'b Expression<'a>>> {
|
|
318
|
+
match expression.get_inner_expression() {
|
|
319
|
+
Expression::UnaryExpression(unary) if unary.operator != UnaryOperator::Delete => {
|
|
320
|
+
operands(&unary.argument).or_else(|| Some(vec![&unary.argument]))
|
|
321
|
+
}
|
|
322
|
+
Expression::BinaryExpression(binary) => Some(vec![&binary.left, &binary.right]),
|
|
323
|
+
Expression::StaticMemberExpression(member) => Some(vec![&member.object]),
|
|
324
|
+
Expression::ComputedMemberExpression(member) => {
|
|
325
|
+
Some(vec![&member.object, &member.expression])
|
|
326
|
+
}
|
|
327
|
+
Expression::CallExpression(call) if !call.optional => {
|
|
328
|
+
let arguments = call.arguments.iter().map(Argument::as_expression);
|
|
329
|
+
iter::once(Some(&call.callee)).chain(arguments).collect()
|
|
462
330
|
}
|
|
331
|
+
_ => None,
|
|
463
332
|
}
|
|
464
|
-
true
|
|
465
333
|
}
|
|
466
334
|
|
|
467
|
-
/// Two arguments may swap if both have no effects, or one
|
|
468
|
-
/// the other writes.
|
|
469
|
-
///
|
|
470
|
-
fn commutes(
|
|
471
|
-
let
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
)
|
|
335
|
+
/// Two arguments may swap if both have no effects, or one is effect-free and reads no binding
|
|
336
|
+
/// the other writes. Opaque operations may write through closures, so local reads cannot cross
|
|
337
|
+
/// them.
|
|
338
|
+
fn commutes(left: &Expression<'_>, right: &Expression<'_>, analysis: &Analysis<'_>) -> bool {
|
|
339
|
+
let effects = (
|
|
340
|
+
left.may_have_side_effects(analysis),
|
|
341
|
+
right.may_have_side_effects(analysis),
|
|
342
|
+
);
|
|
343
|
+
let (pure, impure) = match effects {
|
|
475
344
|
(false, false) => return true,
|
|
476
|
-
(false, true) => (
|
|
477
|
-
(true, false) => (
|
|
345
|
+
(false, true) => (left, right),
|
|
346
|
+
(true, false) => (right, left),
|
|
478
347
|
(true, true) => return false,
|
|
479
348
|
};
|
|
480
|
-
let pure = Effects::
|
|
481
|
-
pure.reads.is_empty()
|
|
482
|
-
|
|
483
|
-
!impure.opaque && impure.writes.is_disjoint(&pure.reads)
|
|
349
|
+
let pure = Effects::expression(pure, analysis);
|
|
350
|
+
if pure.reads.is_empty() {
|
|
351
|
+
return true;
|
|
484
352
|
}
|
|
353
|
+
let impure = Effects::expression(impure, analysis);
|
|
354
|
+
!impure.opaque && impure.writes.is_disjoint(&pure.reads)
|
|
485
355
|
}
|
|
486
356
|
|
|
487
|
-
///
|
|
488
|
-
///
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
Expr::Call(call)
|
|
506
|
-
if let Callee::Expr(callee) = &call.callee
|
|
507
|
-
&& call.args.iter().all(|argument| argument.spread.is_none()) =>
|
|
508
|
-
{
|
|
509
|
-
Some(
|
|
510
|
-
iter::once(&**callee)
|
|
511
|
-
.chain(call.args.iter().map(|argument| &*argument.expr))
|
|
512
|
-
.collect(),
|
|
513
|
-
)
|
|
514
|
-
}
|
|
515
|
-
_ => None,
|
|
357
|
+
/// Checks argument evaluation before substituting a helper's single return expression.
|
|
358
|
+
/// Parameters must be plain identifiers; missing arguments are allowed only for unused trailing
|
|
359
|
+
/// parameters. Async functions, generators, helper-context reads, and parameter stores are kept.
|
|
360
|
+
/// Effectful arguments must occur exactly once in eager operands, with only commuting arguments
|
|
361
|
+
/// reordered. Nested arithmetic and short-circuit expressions need the stricter effect-free path.
|
|
362
|
+
fn substitute<'a>(
|
|
363
|
+
function: &Function<'a>,
|
|
364
|
+
value: &Expression<'a>,
|
|
365
|
+
arguments: &[Argument<'a>],
|
|
366
|
+
analysis: &Analysis<'a>,
|
|
367
|
+
) -> Option<Expression<'a>> {
|
|
368
|
+
let parameters = ast::parameters(function)?;
|
|
369
|
+
if function.r#async
|
|
370
|
+
|| function.generator
|
|
371
|
+
|| own_context(value)
|
|
372
|
+
|| arguments.len() > parameters.len()
|
|
373
|
+
{
|
|
374
|
+
return None;
|
|
516
375
|
}
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
376
|
+
let arguments: Vec<&Expression<'a>> = arguments
|
|
377
|
+
.iter()
|
|
378
|
+
.map(Argument::as_expression)
|
|
379
|
+
.collect::<Option<_>>()?;
|
|
380
|
+
let used = Effects::expression(value, analysis);
|
|
381
|
+
// Parameter stores need a fresh local environment, which expression substitution cannot
|
|
382
|
+
// supply.
|
|
383
|
+
if parameters.iter().any(|id| used.writes.contains(id))
|
|
384
|
+
|| parameters[arguments.len()..]
|
|
385
|
+
.iter()
|
|
386
|
+
.any(|id| used.reads.contains(id))
|
|
387
|
+
{
|
|
388
|
+
return None;
|
|
389
|
+
}
|
|
390
|
+
if arguments
|
|
391
|
+
.iter()
|
|
392
|
+
.any(|argument| argument.may_have_side_effects(analysis))
|
|
393
|
+
{
|
|
394
|
+
let operands = operands(value)?;
|
|
395
|
+
if operands.len() != arguments.len() {
|
|
531
396
|
return None;
|
|
532
397
|
}
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
let mut replacement = expression.clone();
|
|
542
|
-
replacement.visit_mut_with(&mut ParameterReplacer { parameters });
|
|
543
|
-
ast::parenthesized(Box::new(replacement))
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
struct ParameterReplacer<'arguments> {
|
|
547
|
-
parameters: HashMap<Id, &'arguments Expr>,
|
|
548
|
-
}
|
|
549
|
-
|
|
550
|
-
impl VisitMut for ParameterReplacer<'_> {
|
|
551
|
-
fn visit_mut_call_expr(&mut self, call: &mut CallExpr) {
|
|
552
|
-
use swc_core::common::Spanned;
|
|
553
|
-
|
|
554
|
-
let parameter_callee = matches!(&call.callee, Callee::Expr(callee)
|
|
555
|
-
if callee.as_ident().is_some_and(|id| self.parameters.contains_key(&id.to_id())));
|
|
556
|
-
call.visit_mut_children_with(self);
|
|
557
|
-
if parameter_callee
|
|
558
|
-
&& let Callee::Expr(callee) = &mut call.callee
|
|
559
|
-
&& needs_detaching(callee.unwrap_parens())
|
|
560
|
-
{
|
|
561
|
-
// The wrapper calls a parameter as a value. Substituting `obj.method`
|
|
562
|
-
// must not bind `obj` as `this`, or turn a passed `eval` into direct eval.
|
|
563
|
-
**callee = Expr::Seq(SeqExpr {
|
|
564
|
-
span: callee.span(),
|
|
565
|
-
exprs: vec![
|
|
566
|
-
Box::new(Expr::Lit(Lit::Num(Number {
|
|
567
|
-
span: callee.span(),
|
|
568
|
-
value: 0.0,
|
|
569
|
-
raw: None,
|
|
570
|
-
}))),
|
|
571
|
-
callee.clone(),
|
|
572
|
-
],
|
|
573
|
-
});
|
|
398
|
+
let mut order = vec![];
|
|
399
|
+
for operand in operands {
|
|
400
|
+
let id = analysis.expression_id(operand)?;
|
|
401
|
+
let index = parameters.iter().position(|parameter| *parameter == id)?;
|
|
402
|
+
if order.contains(&index) || index >= arguments.len() {
|
|
403
|
+
return None;
|
|
404
|
+
}
|
|
405
|
+
order.push(index)
|
|
574
406
|
}
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
&& let Some(value) = self.parameters.get(&identifier.to_id())
|
|
582
|
-
{
|
|
583
|
-
*expression = ast::parenthesized(Box::new((*value).clone()));
|
|
407
|
+
for (position, &first) in order.iter().enumerate() {
|
|
408
|
+
for &second in &order[position + 1..] {
|
|
409
|
+
if first > second && !commutes(arguments[first], arguments[second], analysis) {
|
|
410
|
+
return None;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
584
413
|
}
|
|
585
414
|
}
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
/// Checks whether a substituted callee needs `(0, callee)` to preserve call behavior. Member
|
|
589
|
-
/// references would otherwise receive `this`, and `eval` could become a direct eval call. A
|
|
590
|
-
/// local named `eval` may also hold the built-in function.
|
|
591
|
-
fn needs_detaching(callee: &Expr) -> bool {
|
|
592
|
-
match callee {
|
|
593
|
-
Expr::Member(_) | Expr::SuperProp(_) | Expr::OptChain(_) => true,
|
|
594
|
-
Expr::Ident(identifier) => identifier.sym == "eval",
|
|
595
|
-
_ => false,
|
|
596
|
-
}
|
|
415
|
+
let values = parameters.into_iter().zip(arguments).collect();
|
|
416
|
+
Some(ast::replace_parameters(value, &values, analysis))
|
|
597
417
|
}
|
|
598
418
|
|
|
599
419
|
#[cfg(test)]
|
|
@@ -603,42 +423,42 @@ mod tests {
|
|
|
603
423
|
/// Helper-call test cases and expected results. Calls remain unchanged when inlining could
|
|
604
424
|
/// alter evaluation or its order. `set()` writes the parameters read by the arguments;
|
|
605
425
|
/// `first()` and `second()` have unknown effects.
|
|
606
|
-
const CALLS: [(&str, &str, &str);
|
|
607
|
-
// Inlined results
|
|
426
|
+
const CALLS: [(&str, &str, &str); 28] = [
|
|
427
|
+
// Inlined results keep the precedence of their substituted arguments.
|
|
608
428
|
(
|
|
609
429
|
"function(a, b) { return a & b; }",
|
|
610
430
|
"h.op(local >>> 16, 65535) * 8133",
|
|
611
|
-
"(
|
|
431
|
+
"(local >>> 16 & 65535) * 8133",
|
|
612
432
|
),
|
|
613
433
|
// Eager operators evaluate their operands in argument order.
|
|
614
434
|
(
|
|
615
435
|
"function(a, b) { return a + b; }",
|
|
616
436
|
"h.op(values[i++], first())",
|
|
617
|
-
"
|
|
437
|
+
"values[i++] + first()",
|
|
618
438
|
),
|
|
619
439
|
// Reordering is safe when only one operand has effects and the other reads no affected
|
|
620
440
|
// binding.
|
|
621
441
|
(
|
|
622
442
|
"function(a, b) { return b - a; }",
|
|
623
443
|
"h.op(values[i++], 2)",
|
|
624
|
-
"
|
|
444
|
+
"2 - values[i++]",
|
|
625
445
|
),
|
|
626
446
|
(
|
|
627
447
|
"function(a, b) { return b === a; }",
|
|
628
448
|
"h.op(typeof values, 'object')",
|
|
629
|
-
"
|
|
449
|
+
r#""object" === typeof values"#,
|
|
630
450
|
),
|
|
631
451
|
(
|
|
632
452
|
"function(a, b) { return b | a; }",
|
|
633
453
|
"h.op(first(), 2)",
|
|
634
|
-
"
|
|
454
|
+
"2 | first()",
|
|
635
455
|
),
|
|
636
456
|
// An effect-free operand may move past a write only if it does not read the written
|
|
637
457
|
// binding.
|
|
638
458
|
(
|
|
639
459
|
"function(a, b) { return b | a; }",
|
|
640
460
|
"h.op(values[i++], local)",
|
|
641
|
-
"
|
|
461
|
+
"local | values[i++]",
|
|
642
462
|
),
|
|
643
463
|
(
|
|
644
464
|
"function(a, b) { return b | a; }",
|
|
@@ -660,17 +480,22 @@ mod tests {
|
|
|
660
480
|
"h.op(first(), second())",
|
|
661
481
|
"h.op(first(), second())",
|
|
662
482
|
),
|
|
483
|
+
(
|
|
484
|
+
"function(a, b) { return b + a; }",
|
|
485
|
+
"h.op(v, [v] = [2])",
|
|
486
|
+
"h.op(v, [v] = [2])",
|
|
487
|
+
),
|
|
663
488
|
// An outer unary operator runs after both operands, so `!(b !== a)` permits the same
|
|
664
489
|
// reordering as `b === a`. `delete` has different operand evaluation rules.
|
|
665
490
|
(
|
|
666
491
|
"function(aX, ae) { return !(ae !== aX); }",
|
|
667
492
|
"h.op(q._cf_chl_opt.EcMJs8, 1)",
|
|
668
|
-
"!(
|
|
493
|
+
"!(1 !== q._cf_chl_opt.EcMJs8)",
|
|
669
494
|
),
|
|
670
495
|
(
|
|
671
496
|
"function(a, b) { return -(b - a); }",
|
|
672
497
|
"h.op(q.x, 2)",
|
|
673
|
-
"-(
|
|
498
|
+
"-(2 - q.x)",
|
|
674
499
|
),
|
|
675
500
|
(
|
|
676
501
|
"function(aX, ae) { return !(ae !== aX); }",
|
|
@@ -686,7 +511,7 @@ mod tests {
|
|
|
686
511
|
(
|
|
687
512
|
"function(a) { return a.key; }",
|
|
688
513
|
"h.op(first())",
|
|
689
|
-
"
|
|
514
|
+
"first().key",
|
|
690
515
|
),
|
|
691
516
|
(
|
|
692
517
|
"function(a, b) { return b[a]; }",
|
|
@@ -697,12 +522,12 @@ mod tests {
|
|
|
697
522
|
(
|
|
698
523
|
"function(rn, rw, ri, ru, KA) { return rn(rw, ri, ru); }",
|
|
699
524
|
"h.op(local, 1, 2, 3)",
|
|
700
|
-
"
|
|
525
|
+
"local(1, 2, 3)",
|
|
701
526
|
),
|
|
702
527
|
(
|
|
703
528
|
"function(a, b, extra) { return b | a; }",
|
|
704
529
|
"h.op(local, 2, 3)",
|
|
705
|
-
"
|
|
530
|
+
"2 | local",
|
|
706
531
|
),
|
|
707
532
|
(
|
|
708
533
|
"function(a, b, extra) { return extra + b; }",
|
|
@@ -721,7 +546,7 @@ mod tests {
|
|
|
721
546
|
"h.op(missing, 1)",
|
|
722
547
|
"h.op(missing, 1)",
|
|
723
548
|
),
|
|
724
|
-
("function(a, b) { return b; }", "h.op(Math, 1)", "
|
|
549
|
+
("function(a, b) { return b; }", "h.op(Math, 1)", "return 1;"),
|
|
725
550
|
// Tests bodies that skip, repeat, or reorder operands within larger expressions.
|
|
726
551
|
(
|
|
727
552
|
"function(a, b) { return a + a; }",
|
|
@@ -751,7 +576,7 @@ mod tests {
|
|
|
751
576
|
];
|
|
752
577
|
|
|
753
578
|
fn transform(source: &str) -> String {
|
|
754
|
-
testing::rewrite(source, super::run
|
|
579
|
+
testing::rewrite(source, super::run)
|
|
755
580
|
}
|
|
756
581
|
|
|
757
582
|
#[test]
|
|
@@ -773,9 +598,9 @@ mod tests {
|
|
|
773
598
|
return h.invoke(eval, "typeof local");
|
|
774
599
|
}"#,
|
|
775
600
|
);
|
|
776
|
-
testing::assert_contains(&output, &["(0, obj.method)(effect())", "plain(1);"]
|
|
601
|
+
testing::assert_contains(&output, &["(0, obj.method)(effect())", "plain(1);"]);
|
|
777
602
|
let expected = ["(0, eval)(\"typeof local\")", "Number(obj.value);"];
|
|
778
|
-
testing::assert_contains(&output, &expected
|
|
603
|
+
testing::assert_contains(&output, &expected);
|
|
779
604
|
assert!(!output.contains("(0, plain)"), "{output}");
|
|
780
605
|
}
|
|
781
606
|
|
|
@@ -785,7 +610,7 @@ mod tests {
|
|
|
785
610
|
"function f(h, p) { h = {}; h.op = function (a, b) { return a + b; }; \
|
|
786
611
|
h.name = \"n\"; p = h; return [p.op(1, 2), p.name, h.name]; }",
|
|
787
612
|
);
|
|
788
|
-
assert!(output.contains("
|
|
613
|
+
assert!(output.contains("1 + 2"), "{output}");
|
|
789
614
|
testing::assert_lacks(&output, &["p.name", "h.name]"]);
|
|
790
615
|
// Static templates inline into later callbacks, as in a payload built after a delay.
|
|
791
616
|
let output = transform(
|
|
@@ -795,7 +620,7 @@ mod tests {
|
|
|
795
620
|
// A unary helper evaluates its argument first either way, even a member read.
|
|
796
621
|
let output =
|
|
797
622
|
transform("function f(n) { n = function (a) { return !a; }; if (n(w.x)) g(); }");
|
|
798
|
-
assert!(output.contains("!
|
|
623
|
+
assert!(output.contains("!w.x"), "{output}");
|
|
799
624
|
// Helper functions inline when they name only what callers see.
|
|
800
625
|
let output = transform(
|
|
801
626
|
"function f(add, call, read, g) { add = function(a, b) { return a + b; }; \
|
|
@@ -803,21 +628,21 @@ mod tests {
|
|
|
803
628
|
g = function(local) { read = function() { return local; }; }; \
|
|
804
629
|
g(5); return [call(2), add(3, 4), read()]; }",
|
|
805
630
|
);
|
|
806
|
-
let expected = ["
|
|
807
|
-
testing::assert_contains(&output, &
|
|
631
|
+
let expected = ["2 + 1", "Math.max(2)", "3 + 4", "read()]"];
|
|
632
|
+
testing::assert_contains(&output, &expected);
|
|
808
633
|
// Function declarations inside function bodies are also stable; top-level declarations are
|
|
809
634
|
// not.
|
|
810
635
|
let output = transform(
|
|
811
636
|
"function f(x) { return g(x, 1); function g(a, b) { return a + b; } } \
|
|
812
637
|
function h(a) { return a; } k(h(2));",
|
|
813
638
|
);
|
|
814
|
-
testing::assert_contains(&output, &[
|
|
639
|
+
testing::assert_contains(&output, &["return x + 1;", "k(h(2));"]);
|
|
815
640
|
// Copies of helper methods are stable too.
|
|
816
641
|
let output = transform(
|
|
817
642
|
"function f(h, m) { h = { shr: function (a, b) { return a >> b; } }; m = h.shr; \
|
|
818
643
|
return m(8, 1); }",
|
|
819
644
|
);
|
|
820
|
-
assert!(output.contains("
|
|
645
|
+
assert!(output.contains("8 >> 1"), "{output}");
|
|
821
646
|
}
|
|
822
647
|
|
|
823
648
|
#[test]
|
|
@@ -837,11 +662,11 @@ mod tests {
|
|
|
837
662
|
return [alias.add(1, 2), alias.name, h.add(1, 2)];
|
|
838
663
|
}"#,
|
|
839
664
|
);
|
|
840
|
-
testing::assert_contains(&output, &["h.add(1, 2)", "h.add(3, 4)"]
|
|
665
|
+
testing::assert_contains(&output, &["h.add(1, 2)", "h.add(3, 4)"]);
|
|
841
666
|
assert!(output.matches("h.name").count() == 2, "{output}");
|
|
842
|
-
testing::assert_contains(&output, &["alias.add(1, 2)", "alias.name"]
|
|
667
|
+
testing::assert_contains(&output, &["alias.add(1, 2)", "alias.name"]);
|
|
843
668
|
assert!(
|
|
844
|
-
!output.contains("
|
|
669
|
+
!output.contains("1 + 2"),
|
|
845
670
|
"`h` is copied into a binding that is assigned again, which could change it before \
|
|
846
671
|
that, so its properties are not trusted either: {output}"
|
|
847
672
|
);
|