From 1d834c49e88cf0d65dea4aa896b8b33d6bcea8ad Mon Sep 17 00:00:00 2001 From: Toshit Chawda Date: Fri, 7 Mar 2025 15:18:36 -0800 Subject: [PATCH] make everything use oxc allocator --- rewriter/Cargo.lock | 194 +++++++++++++++---------------- rewriter/Cargo.toml | 1 + rewriter/native/src/main.rs | 56 +++++---- rewriter/rewriter/src/cfg.rs | 32 ++--- rewriter/rewriter/src/changes.rs | 105 +++++++++-------- rewriter/rewriter/src/lib.rs | 35 +++--- rewriter/rewriter/src/visitor.rs | 78 ++++++------- rewriter/wasm/src/lib.rs | 123 ++++++++++++-------- 8 files changed, 328 insertions(+), 296 deletions(-) diff --git a/rewriter/Cargo.lock b/rewriter/Cargo.lock index 131380b..81daada 100644 --- a/rewriter/Cargo.lock +++ b/rewriter/Cargo.lock @@ -55,9 +55,9 @@ checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" [[package]] name = "bitflags" -version = "2.8.0" +version = "2.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36" +checksum = "5c8214115b7bf84099f1309324e63141d4c5d7cc26862f97a0a857dbefe165bd" [[package]] name = "boa_ast" @@ -209,9 +209,9 @@ dependencies = [ [[package]] name = "bytemuck" -version = "1.21.0" +version = "1.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef657dfab802224e671f5818e9a4935f9b1957ed18e58292690cc39e7a4092a3" +checksum = "b6b1fc10dbac614ebc03540c9dbd60e83887fda27794998c6528f1782047d540" dependencies = [ "bytemuck_derive", ] @@ -289,18 +289,18 @@ dependencies = [ [[package]] name = "clap" -version = "4.5.29" +version = "4.5.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8acebd8ad879283633b343856142139f2da2317c96b05b4dd6181c61e2480184" +checksum = "027bb0d98429ae334a8698531da7077bdf906419543a35a55c2cb1b66437d767" dependencies = [ "clap_builder", ] [[package]] name = "clap_builder" -version = "4.5.29" +version = "4.5.31" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6ba32cbda51c7e1dfd49acc1457ba1a7dec5b64fe360e828acb13ca8dc9c2f9" +checksum = "5589e0cba072e0f3d23791efac0fd8627b49c829c196a492e88168e6a669d863" dependencies = [ "anstyle", "clap_lex", @@ -435,9 +435,9 @@ dependencies = [ [[package]] name = "either" -version = "1.13.0" +version = "1.15.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60b1af1c220855b6ceac025d3f6ecdd2b7c4894bfe9cd9bda4fbb4bc7c0d4cf0" +checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719" [[package]] name = "equivalent" @@ -506,9 +506,9 @@ dependencies = [ [[package]] name = "hermit-abi" -version = "0.4.0" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" +checksum = "fbd780fe5cc30f81464441920d82ac8740e2e46b29a6fad543ddd075229ce37e" [[package]] name = "icu_collections" @@ -682,9 +682,9 @@ dependencies = [ [[package]] name = "is-terminal" -version = "0.4.15" +version = "0.4.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e19b23d53f35ce9f56aebc7d1bb4e6ac1e9c0db7ac85c8d1760c04379edced37" +checksum = "e04d7f318608d35d4b61ddd75cbdaee86b023ebe2bd5a66ee0915f0bf93095a9" dependencies = [ "hermit-abi", "libc", @@ -711,9 +711,9 @@ dependencies = [ [[package]] name = "itoa" -version = "1.0.14" +version = "1.0.15" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" +checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c" [[package]] name = "js-sys" @@ -727,15 +727,15 @@ dependencies = [ [[package]] name = "libc" -version = "0.2.169" +version = "0.2.170" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b5aba8db14291edd000dfcc4d620c7ebfb122c613afb886ca8803fa4e128a20a" +checksum = "875b3680cb2f8f71bdcf9a30f38d48282f5d3c95cbf9b3fa57269bb5d5c06828" [[package]] name = "litemap" -version = "0.7.4" +version = "0.7.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4ee93343901ab17bd981295f2cf0026d4ad018c7c31ba84549a4ddbb47a45104" +checksum = "23fb14cb19457329c82206317a5663005a4d404783dc74f4252769b0d5f42856" [[package]] name = "lock_api" @@ -749,9 +749,9 @@ dependencies = [ [[package]] name = "log" -version = "0.4.25" +version = "0.4.26" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "04cbf5b083de1c7e0222a7a51dbfdba1cbe1c6ab0b15e29fff3f6c077fd9cd9f" +checksum = "30bde2b3dc3671ae49d8e2e9f044c7c005836e7a023ee57cffa25ab82764bb9e" [[package]] name = "memchr" @@ -861,21 +861,21 @@ checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" [[package]] name = "oorandom" -version = "11.1.4" +version = "11.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b410bbe7e14ab526a0e86877eb47c6996a2bd7746f027ba551028c925390e4e9" +checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e" [[package]] name = "owo-colors" -version = "4.1.0" +version = "4.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fb37767f6569cd834a413442455e0f066d0d522de8630436e2a1761d9726ba56" +checksum = "1036865bb9422d3300cf723f657c2851d0e9ab12567854b1f4eba3d77decf564" [[package]] name = "oxc" -version = "0.56.0" +version = "0.56.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5b4a5aba9c3f445db5401f9975266b23a5c5b49a8b4ccb1efa049132e96069dc" +checksum = "cae6276febbc5abc1f1e4cf49167d54ab341818656ae4f622d5992b65fcdd371" dependencies = [ "oxc_allocator", "oxc_ast", @@ -898,7 +898,7 @@ dependencies = [ "oxc-miette-derive", "textwrap", "thiserror 1.0.69", - "unicode-width 0.2.0", + "unicode-width", ] [[package]] @@ -914,9 +914,9 @@ dependencies = [ [[package]] name = "oxc_allocator" -version = "0.56.0" +version = "0.56.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7844c292ad3f79021bf3066538129c662a65bb4235a3450d124edd81bc9094df" +checksum = "22cc5cd078806a1b7061fa146dc4228a57d0765da6c85e99500d069b86f57e94" dependencies = [ "allocator-api2", "bumpalo", @@ -927,9 +927,9 @@ dependencies = [ [[package]] name = "oxc_ast" -version = "0.56.0" +version = "0.56.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c4005b2e219aa87cb0cbde495bcd6543748a4d77c8583ec573c6db2984bb249f" +checksum = "e4722414ac21a2e28a16b76de8390672c01a39adcb703d405b848149cfaeeaf7" dependencies = [ "bitflags", "cow-utils", @@ -944,9 +944,9 @@ dependencies = [ [[package]] name = "oxc_ast_macros" -version = "0.56.0" +version = "0.56.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93405fb1ad62247fa9296ad73265671b9c4aaa39dbbe142af00a6de415e61606" +checksum = "8f1505d8622b2ea6ed0274f355bd5e4ee3f09df5d9b39c8a3a673f344d87b82a" dependencies = [ "proc-macro2", "quote", @@ -955,9 +955,9 @@ dependencies = [ [[package]] name = "oxc_ast_visit" -version = "0.56.0" +version = "0.56.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f333b2ae5cbf1328986ce1eca317bc7f2f2e256c3d4a0f72b0d935a30c86b641" +checksum = "cf45370c6da0dd142a70468e5b25127d6a34caa71056105c85087559c8ee9afb" dependencies = [ "oxc_allocator", "oxc_ast", @@ -967,9 +967,9 @@ dependencies = [ [[package]] name = "oxc_data_structures" -version = "0.56.0" +version = "0.56.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dfc814bebc24ca429f257fba498dee171a98fa2ef1ea0f9c9eb3d69f0993baa6" +checksum = "cb49a2ee880952c2079b61851ecc35d7671d9d3509e306f5e704ccacd2783984" dependencies = [ "assert-unchecked", "ropey", @@ -977,9 +977,9 @@ dependencies = [ [[package]] name = "oxc_diagnostics" -version = "0.56.0" +version = "0.56.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0fd1f43ffbdabf86a563b48970c2abdbc0ac860e49558c83ab23a010b1a774f8" +checksum = "ea83fe2415b0580980ac83364c1ae943f8ee9c00becf5395a89e800a9526a080" dependencies = [ "cow-utils", "oxc-miette", @@ -987,9 +987,9 @@ dependencies = [ [[package]] name = "oxc_ecmascript" -version = "0.56.0" +version = "0.56.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a23d0e112ff9ea0f39dd2c9c921f0938e313e26ee9e4022f60c8d81c561e518" +checksum = "6c7e7bcc382cf901e93a16f86e70f2737c507aaa833656b0d8484d64f8ae358a" dependencies = [ "cow-utils", "num-bigint", @@ -1001,9 +1001,9 @@ dependencies = [ [[package]] name = "oxc_estree" -version = "0.56.0" +version = "0.56.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd9818707125125e32d25fd18c5331a4418fbde084e33c69dee123df5b2531d2" +checksum = "1cb66484735d21f096b07c894badc96d89a0c6d31b4bdd46b33b3e44da9b97ac" [[package]] name = "oxc_index" @@ -1013,9 +1013,9 @@ checksum = "2fa07b0cfa997730afed43705766ef27792873fdf5215b1391949fec678d2392" [[package]] name = "oxc_parser" -version = "0.56.0" +version = "0.56.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cce20364085569f8caebeb879aec455c5c8f6e5dcb5d0ecfb88208ae5e709331" +checksum = "7a7414e779b9723b0bd2880fe69b0ee517e583f88c5118a7c9053cf3317b95b1" dependencies = [ "assert-unchecked", "bitflags", @@ -1036,9 +1036,9 @@ dependencies = [ [[package]] name = "oxc_regular_expression" -version = "0.56.0" +version = "0.56.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "89e1f1e21ed9b3e4d1c6eb10dffc1c71202e8fe111968cf6636f5484ad443b0b" +checksum = "d18196c212eac24a7faf613e373e5f9317d3542578a088d01dc1a548fa1e1cb3" dependencies = [ "oxc_allocator", "oxc_ast_macros", @@ -1052,9 +1052,9 @@ dependencies = [ [[package]] name = "oxc_span" -version = "0.56.0" +version = "0.56.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e59da8e765f44892357f6b75bdb16f486d61956c5bd92a345429ad704d4853fd" +checksum = "318f925e26bd118adc082d290538d07611fe2434987a5c60cf5084381ecb42e6" dependencies = [ "compact_str", "oxc-miette", @@ -1065,9 +1065,9 @@ dependencies = [ [[package]] name = "oxc_syntax" -version = "0.56.0" +version = "0.56.5" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "47b8bd67fdd8df27cfddbec577b282f1a6601509ecd750697f590d7cdc2d88b7" +checksum = "8cb68ceb7c6902f3043fe8fe49bb886826b1d7741dc2904337297d53692b1b9c" dependencies = [ "assert-unchecked", "bitflags", @@ -1187,9 +1187,9 @@ checksum = "2f3a9f18d041e6d0e102a0a46750538147e5e8992d3b4873aaafee2520b00ce3" [[package]] name = "portable-atomic" -version = "1.10.0" +version = "1.11.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "280dc24453071f1b63954171985a0b0d30058d287960968b9b2aca264c8d4ee6" +checksum = "350e9b48cbc6b0e028b0473b114454c6316e57336ee184ceab6e53f72c178b3e" [[package]] name = "powerfmt" @@ -1208,27 +1208,27 @@ dependencies = [ [[package]] name = "proc-macro-crate" -version = "3.2.0" +version = "3.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ecf48c7ca261d60b74ab1a7b20da18bede46776b2e55535cb958eb595c5fa7b" +checksum = "edce586971a4dfaa28950c6f18ed55e0406c1ab88bbce2c6f6293a7aaba73d35" dependencies = [ "toml_edit", ] [[package]] name = "proc-macro2" -version = "1.0.93" +version = "1.0.94" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "60946a68e5f9d28b0dc1c21bb8a97ee7d018a8b322fa57838ba31cc878e22d99" +checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84" dependencies = [ "unicode-ident", ] [[package]] name = "quote" -version = "1.0.38" +version = "1.0.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0e4dccaaaf89514f546c693ddc140f729f958c247918a13380cccc6078391acc" +checksum = "c1f1914ce909e1658d9907913b4b91947430c7d9be598b15a1912935b8c04801" dependencies = [ "proc-macro2", ] @@ -1285,9 +1285,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.8" +version = "0.5.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834" +checksum = "0b8c0c260b63a8219631167be35e6a988e9554dbd323f8bd08439c8ed1302bd1" dependencies = [ "bitflags", ] @@ -1358,15 +1358,15 @@ checksum = "357703d41365b4b27c590e3ed91eabb1b663f07c4c084095e60cbed4362dff0d" [[package]] name = "rustversion" -version = "1.0.19" +version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f7c45b9784283f1b2e7fb61b42047c2fd678ef0960d4f6f1eba131594cc369d4" +checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2" [[package]] name = "ryu" -version = "1.0.19" +version = "1.0.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ea1a2d0a644769cc99faa24c3ad26b379b786fe7c36fd3c546254801650e6dd" +checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f" [[package]] name = "ryu-js" @@ -1391,24 +1391,24 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "seq-macro" -version = "0.3.5" +version = "0.3.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a3f0bf26fd526d2a95683cd0f87bf103b8539e2ca1ef48ce002d67aad59aa0b4" +checksum = "1bc711410fbe7399f390ca1c3b60ad0f53f80e95c5eb935e52268a0e2cd49acc" [[package]] name = "serde" -version = "1.0.217" +version = "1.0.218" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "02fc4265df13d6fa1d00ecff087228cc0a2b5f3c0e87e258d8b94a156e984c70" +checksum = "e8dfc9d19bdbf6d17e22319da49161d5d0108e4188e8b680aef6299eed22df60" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.217" +version = "1.0.218" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5a9bf7cf98d04a2b28aead066b7496853d4779c9cc183c440dbac457641e19a0" +checksum = "f09503e191f4e797cb8aac08e9a4a4695c5edf6a2e70e376d961ddd5c969f82b" dependencies = [ "proc-macro2", "quote", @@ -1417,9 +1417,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.138" +version = "1.0.140" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d434192e7da787e94a6ea7e9670b26a036d0ca41e0b7efb2676dd32bae872949" +checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373" dependencies = [ "itoa", "memchr", @@ -1477,9 +1477,9 @@ checksum = "d08889ec5408683408db66ad89e0e1f93dff55c73a4ccc71c427d5b277ee47e6" [[package]] name = "syn" -version = "2.0.98" +version = "2.0.99" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "36147f1a48ae0ec2b5b3bc5b537d267457555a10dc06f3dbc8cb11ba3006d3b1" +checksum = "e02e925281e18ffd9d640e234264753c43edc62d64b2d4cf898f1bc5e75f3fc2" dependencies = [ "proc-macro2", "quote", @@ -1505,13 +1505,13 @@ checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" [[package]] name = "textwrap" -version = "0.16.1" +version = "0.16.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "23d434d3f8967a09480fb04132ebe0a3e088c173e6d0ee7897abbdf4eab0f8b9" +checksum = "c13547615a44dc9c452a8a534638acdf07120d4b6847c8178705da06306a3057" dependencies = [ "smawk", "unicode-linebreak", - "unicode-width 0.1.14", + "unicode-width", ] [[package]] @@ -1562,9 +1562,9 @@ dependencies = [ [[package]] name = "time" -version = "0.3.37" +version = "0.3.39" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "35e7868883861bd0e56d9ac6efcaaca0d6d5d82a2a7ec8209ff492c07cf37b21" +checksum = "dad298b01a40a23aac4580b67e3dbedb7cc8402f3592d7f49469de2ea4aecdd8" dependencies = [ "deranged", "itoa", @@ -1580,15 +1580,15 @@ dependencies = [ [[package]] name = "time-core" -version = "0.1.2" +version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" +checksum = "765c97a5b985b7c11d7bc27fa927dc4fe6af3a6dfb021d28deb60d3bf51e76ef" [[package]] name = "time-macros" -version = "0.2.19" +version = "0.2.20" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2834e6017e3e5e4b9834939793b282bc03b37a3336245fa820e35e233e2a85de" +checksum = "e8093bc3e81c3bc5f7879de09619d06c9a5a5e45ca44dfeeb7225bae38005c5c" dependencies = [ "num-conv", "time-core", @@ -1639,9 +1639,9 @@ checksum = "2f322b60f6b9736017344fa0635d64be2f458fbc04eef65f6be22976dd1ffd5b" [[package]] name = "unicode-ident" -version = "1.0.16" +version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a210d160f08b701c8721ba1c726c11662f877ea6b7094007e1ca9a1041945034" +checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" [[package]] name = "unicode-linebreak" @@ -1649,12 +1649,6 @@ version = "0.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b09c83c3c29d37506a3e260c08c03743a6bb66a9cd432c6934ab501a190571f" -[[package]] -name = "unicode-width" -version = "0.1.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" - [[package]] name = "unicode-width" version = "0.2.0" @@ -1871,9 +1865,9 @@ checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "winnow" -version = "0.7.2" +version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "59690dea168f2198d1a3b0cac23b8063efcd11012f10ae4698f284808c8ef603" +checksum = "0e7f4ea97f6f78012141bcdb6a216b2609f0979ada50b20ca5b52dde2eac2bb1" dependencies = [ "memchr", ] @@ -1937,18 +1931,18 @@ dependencies = [ [[package]] name = "zerofrom" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cff3ee08c995dee1859d998dea82f7374f2826091dd9cd47def953cae446cd2e" +checksum = "50cc42e0333e05660c3587f3bf9d0478688e15d870fab3346451ce7f8c9fbea5" dependencies = [ "zerofrom-derive", ] [[package]] name = "zerofrom-derive" -version = "0.1.5" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "595eed982f7d355beb85837f651fa22e90b3c044842dc7f2c2842c086f295808" +checksum = "d71e5d6e06ab090c67b5e44993ec16b72dcbaabc526db883a360057678b48502" dependencies = [ "proc-macro2", "quote", diff --git a/rewriter/Cargo.toml b/rewriter/Cargo.toml index ccf7c26..5bc3acc 100644 --- a/rewriter/Cargo.toml +++ b/rewriter/Cargo.toml @@ -17,3 +17,4 @@ pedantic = { level = "warn", priority = -1 } struct-excessive-bools = "allow" missing-errors-doc = "allow" cast-possible-truncation = "allow" +must-use-candidate = "allow" diff --git a/rewriter/native/src/main.rs b/rewriter/native/src/main.rs index 6e4b879..ebcd3f6 100644 --- a/rewriter/native/src/main.rs +++ b/rewriter/native/src/main.rs @@ -2,39 +2,43 @@ use std::{env, fs, str::FromStr, sync::Arc}; use anyhow::{Context, Result}; use bytes::{Buf, Bytes, BytesMut}; -use oxc::diagnostics::NamedSource; +use oxc::{ + allocator::{Allocator, String}, + diagnostics::NamedSource, +}; use rewriter::{cfg::Config, rewrite, RewriteResult}; use url::Url; use urlencoding::encode; -fn dorewrite(data: &str) -> Result { +fn dorewrite<'alloc>(alloc: &'alloc Allocator, data: &str) -> Result> { let url = Url::from_str("https://google.com/glorngle/si.js").context("failed to make url")?; rewrite( + alloc, data, - true, - 1024, Config { - prefix: "/scrammedjet/".to_string(), - base: url.to_string(), - urlrewriter: Box::new(move |x: String| { - encode(url.join(&x).unwrap().as_str()).to_string() + prefix: "/scrammedjet/", + base: "https://google.com/glorngle/si.js", + urlrewriter: Box::new(move |x: &str, alloc: &'alloc Allocator| { + String::from_str_in(encode(url.join(&x).unwrap().as_str()).as_ref(), alloc) }), - sourcetag: "glongle1".to_string(), + sourcetag: "glongle1", - wrapfn: "$wrap".to_string(), - wrapthisfn: "$gwrap".to_string(), - importfn: "$import".to_string(), - rewritefn: "$rewrite".to_string(), - metafn: "$meta".to_string(), - setrealmfn: "$setrealm".to_string(), - pushsourcemapfn: "$pushsourcemap".to_string(), + wrapfn: "$wrap", + wrapthisfn: "$gwrap", + importfn: "$import", + rewritefn: "$rewrite", + metafn: "$meta", + setrealmfn: "$setrealm", + pushsourcemapfn: "$pushsourcemap", capture_errors: true, do_sourcemaps: true, scramitize: false, strict_rewrites: true, }, + true, + 1024, ) .context("failed to rewrite file") } @@ -47,7 +51,7 @@ enum RewriteType { fn dounrewrite(res: RewriteResult) -> Vec { let js = res.js.as_slice(); - let mut map = Bytes::from(res.sourcemap); + let mut map = Bytes::from(res.sourcemap.to_vec()); let rewrite_cnt = map.get_u32_le(); let mut rewrites = Vec::with_capacity(rewrite_cnt as usize); @@ -102,10 +106,12 @@ fn main() -> Result<()> { let data = fs::read_to_string(file).context("failed to read file")?; let bench = env::args().nth(2).is_some(); + let alloc = Allocator::default(); + if bench { let mut i = 0; loop { - let _ = dorewrite(&data); + let _ = dorewrite(&alloc, &data); i += 1; if i % 100 == 0 { println!("{i}..."); @@ -114,7 +120,7 @@ fn main() -> Result<()> { } else { println!("orig:\n{data}"); - let res = dorewrite(&data)?; + let res = dorewrite(&alloc, &data)?; let source = Arc::new( NamedSource::new(data.clone(), "https://google.com/glorngle/si.js") @@ -151,13 +157,16 @@ mod test { property::{Attribute, PropertyDescriptorBuilder}, Context, NativeFunction, Source, }; + use oxc::allocator::Allocator; use crate::dorewrite; #[test] fn google() { + let alloc = Allocator::default(); + let source_text = include_str!("../sample/google.js"); - dorewrite(source_text).unwrap(); + dorewrite(&alloc, source_text).unwrap(); } #[test] @@ -240,10 +249,13 @@ function check(val) { )) .unwrap(); - let rewritten = dorewrite(&content).unwrap(); + let alloc = Allocator::default(); + let rewritten = dorewrite(&alloc, &content).unwrap(); println!("{}", std::str::from_utf8(&rewritten.js).unwrap()); - context.eval(Source::from_bytes(&rewritten.js)).unwrap(); + context + .eval(Source::from_bytes(rewritten.js.as_slice())) + .unwrap(); println!("PASS"); } } diff --git a/rewriter/rewriter/src/cfg.rs b/rewriter/rewriter/src/cfg.rs index 3fce591..537824a 100644 --- a/rewriter/rewriter/src/cfg.rs +++ b/rewriter/rewriter/src/cfg.rs @@ -1,20 +1,20 @@ -#[derive(Clone)] -pub struct Config -where - E: Fn(String) -> String, - E: Clone, -{ - pub prefix: String, - pub sourcetag: String, - pub base: String, +use oxc::allocator::{Allocator, String}; - pub wrapfn: String, - pub wrapthisfn: String, - pub importfn: String, - pub rewritefn: String, - pub setrealmfn: String, - pub metafn: String, - pub pushsourcemapfn: String, +pub struct Config<'alloc, E> +where + E: Fn(&str, &'alloc Allocator) -> String<'alloc>, +{ + pub prefix: &'alloc str, + pub sourcetag: &'alloc str, + pub base: &'alloc str, + + pub wrapfn: &'alloc str, + pub wrapthisfn: &'alloc str, + pub importfn: &'alloc str, + pub rewritefn: &'alloc str, + pub setrealmfn: &'alloc str, + pub metafn: &'alloc str, + pub pushsourcemapfn: &'alloc str, /// URL REWRITER IS RESPONSIBLE FOR ADDING BASE pub urlrewriter: E, diff --git a/rewriter/rewriter/src/changes.rs b/rewriter/rewriter/src/changes.rs index fc35ac6..a531a9d 100644 --- a/rewriter/rewriter/src/changes.rs +++ b/rewriter/rewriter/src/changes.rs @@ -1,15 +1,16 @@ use std::cmp::Ordering; use oxc::{ + allocator::{Allocator, String, Vec}, ast::ast::AssignmentOperator, - span::{format_compact_str, CompactStr, Span}, + span::{format_compact_str, Atom, Span}, }; use smallvec::{smallvec, SmallVec}; use crate::{cfg::Config, RewriterError}; -#[derive(Debug, PartialEq, Eq, Clone)] -pub(crate) enum Rewrite { +#[derive(Debug, PartialEq, Eq)] +pub(crate) enum Rewrite<'data> { /// `(cfg.wrapfn(ident))` | `cfg.wrapfn(ident)` WrapFn { span: Span, @@ -37,7 +38,7 @@ pub(crate) enum Rewrite { /// `$scramerr(name)` ScramErr { span: Span, - ident: CompactStr, + ident: Atom<'data>, }, /// `$scramitize(span)` Scramitize { @@ -51,7 +52,7 @@ pub(crate) enum Rewrite { }, /// `((t)=>$scramjet$tryset(name,"op",t)||(name op t))(rhsspan)` Assignment { - name: CompactStr, + name: Atom<'data>, entirespan: Span, rhsspan: Span, op: AssignmentOperator, @@ -59,7 +60,7 @@ pub(crate) enum Rewrite { /// `ident,` -> `ident: cfg.wrapfn(ident),` ShorthandObj { span: Span, - name: CompactStr, + name: Atom<'data>, }, SourceTag { span: Span, @@ -68,15 +69,15 @@ pub(crate) enum Rewrite { // don't use for anything static, only use for stuff like rewriteurl Replace { span: Span, - text: String, + text: String<'data>, }, Delete { span: Span, }, } -impl Rewrite { - fn into_inner(self) -> SmallVec<[JsChange; 4]> { +impl<'data> Rewrite<'data> { + fn into_inner(self) -> SmallVec<[JsChange<'data>; 4]> { match self { Self::WrapFn { wrapped, span } => { let start = Span::new(span.start, span.start); @@ -180,14 +181,14 @@ impl<'a> From<&'a str> for Change<'a> { } } -impl<'a> From<&'a CompactStr> for Change<'a> { - fn from(value: &'a CompactStr) -> Self { +impl<'a> From<&'a String<'_>> for Change<'a> { + fn from(value: &'a String<'_>) -> Self { Self::Str(value.as_str()) } } -impl<'a> From<&'a String> for Change<'a> { - fn from(value: &'a String) -> Self { +impl<'a> From<&'a Atom<'_>> for Change<'a> { + fn from(value: &'a Atom<'_>) -> Self { Self::Str(value.as_str()) } } @@ -217,8 +218,8 @@ enum JsChangeInner<'a> { Replace { str: Changes<'a> }, } -#[derive(Debug, PartialEq, Eq, Clone)] -enum JsChange { +#[derive(Debug, PartialEq, Eq)] +enum JsChange<'data> { /// insert `${cfg.wrapfn}(` WrapFn { span: Span, extra: bool }, /// insert `${cfg.setrealmfn}({}).` @@ -226,13 +227,13 @@ enum JsChange { /// insert `${cfg.wrapthis}(` WrapThisFn { span: Span }, /// insert `$scramerr(ident);` - ScramErrFn { span: Span, ident: CompactStr }, + ScramErrFn { span: Span, ident: Atom<'data> }, /// insert `$scramitize(` ScramitizeFn { span: Span }, /// insert `eval(${cfg.rewritefn}(` EvalRewriteFn { span: Span }, /// insert `: ${cfg.wrapfn}(ident)` - ShorthandObj { span: Span, ident: CompactStr }, + ShorthandObj { span: Span, ident: Atom<'data> }, /// insert scramtag SourceTag { span: Span }, @@ -243,7 +244,7 @@ enum JsChange { /// replace span with `((t)=>$scramjet$tryset(${name},"${op}",t)||(${name}${op}t))(` AssignmentLeft { span: Span, - name: CompactStr, + name: Atom<'data>, op: AssignmentOperator, }, @@ -255,12 +256,12 @@ enum JsChange { DoubleClosingParen { span: Span }, /// replace span with text - Replace { span: Span, text: String }, + Replace { span: Span, text: String<'data> }, /// replace span with "" Delete { span: Span }, } -impl JsChange { +impl JsChange<'_> { fn get_span(&self) -> &Span { match self { Self::WrapFn { span, .. } @@ -282,32 +283,35 @@ impl JsChange { } } - fn to_inner<'a, E>(&'a self, cfg: &'a Config, offset: usize) -> JsChangeInner<'a> + fn to_inner<'alloc, 'change, E>( + &'change self, + cfg: &'change Config<'alloc, E>, + offset: usize, + ) -> JsChangeInner<'change> where - E: Fn(String) -> String, - E: Clone, + E: Fn(&str, &'alloc Allocator) -> String<'alloc>, { match self { Self::WrapFn { span, extra } => { if *extra { JsChangeInner::Insert { loc: span.start, - str: changes!["(", &cfg.wrapfn, "("], + str: changes!["(", cfg.wrapfn, "("], } } else { JsChangeInner::Insert { loc: span.start, - str: changes![&cfg.wrapfn, "("], + str: changes![cfg.wrapfn, "("], } } } Self::SetRealmFn { span } => JsChangeInner::Insert { loc: span.start, - str: changes![&cfg.setrealmfn, "({})."], + str: changes![cfg.setrealmfn, "({})."], }, Self::WrapThisFn { span } => JsChangeInner::Insert { loc: span.start, - str: changes![&cfg.wrapthisfn, "("], + str: changes![cfg.wrapthisfn, "("], }, Self::ScramErrFn { span, ident } => JsChangeInner::Insert { loc: span.start, @@ -318,11 +322,11 @@ impl JsChange { str: changes![" $scramitize("], }, Self::EvalRewriteFn { .. } => JsChangeInner::Replace { - str: changes!["eval(", &cfg.rewritefn, "("], + str: changes!["eval(", cfg.rewritefn, "("], }, Self::ShorthandObj { span, ident } => JsChangeInner::Insert { loc: span.start, - str: changes![":", &cfg.wrapfn, "(", ident, ")"], + str: changes![":", cfg.wrapfn, "(", ident, ")"], }, Self::SourceTag { span } => JsChangeInner::Insert { loc: span.start, @@ -330,15 +334,15 @@ impl JsChange { "/*scramtag ", span.start as usize + offset, " ", - &cfg.sourcetag, + cfg.sourcetag, "*/" ], }, Self::ImportFn { .. } => JsChangeInner::Replace { - str: changes![&cfg.importfn, "(\"", &cfg.base, "\","], + str: changes![cfg.importfn, "(\"", cfg.base, "\","], }, Self::MetaFn { .. } => JsChangeInner::Replace { - str: changes![&cfg.metafn, "(\"", &cfg.base], + str: changes![cfg.metafn, "(\"", cfg.base], }, Self::AssignmentLeft { name, op, .. } => JsChangeInner::Replace { str: changes![ @@ -369,13 +373,13 @@ impl JsChange { } } -impl PartialOrd for JsChange { +impl PartialOrd for JsChange<'_> { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } -impl Ord for JsChange { +impl Ord for JsChange<'_> { fn cmp(&self, other: &Self) -> std::cmp::Ordering { match self.get_span().start.cmp(&other.get_span().start) { Ordering::Equal => match (self, other) { @@ -388,36 +392,41 @@ impl Ord for JsChange { } } -pub(crate) struct JsChangeResult { - pub js: Vec, - pub sourcemap: Vec, +pub(crate) struct JsChangeResult<'alloc> { + pub js: Vec<'alloc, u8>, + pub sourcemap: Vec<'alloc, u8>, } -pub(crate) struct JsChanges { - inner: Vec, +pub(crate) struct JsChanges<'alloc: 'data, 'data> { + alloc: &'alloc Allocator, + inner: Vec<'alloc, JsChange<'data>>, } -impl JsChanges { - pub fn new(capacity: usize) -> Self { +impl<'alloc: 'data, 'data> JsChanges<'alloc, 'data> { + pub fn new(alloc: &'alloc Allocator, capacity: usize) -> Self { Self { - inner: Vec::with_capacity(capacity), + inner: Vec::with_capacity_in(capacity, alloc), + alloc, } } - pub fn add(&mut self, rewrite: Rewrite) { + pub fn add(&mut self, rewrite: Rewrite<'data>) { for change in rewrite.into_inner() { self.inner.push(change); } } - pub fn perform(&mut self, js: &str, cfg: &Config) -> Result + pub fn perform( + &mut self, + js: &str, + cfg: &Config<'alloc, E>, + ) -> Result, RewriterError> where - E: Fn(String) -> String, - E: Clone, + E: Fn(&str, &'alloc Allocator) -> String<'alloc>, { let mut offset = 0; let mut added = 0i64; - let mut buffer = Vec::with_capacity(js.len() * 2); + let mut buffer = Vec::with_capacity_in(js.len() * 2, self.alloc); macro_rules! tryget { ($range:expr) => { @@ -441,7 +450,7 @@ impl JsChanges { }; } - let mut map = Vec::with_capacity(js.len() * 2); + let mut map = Vec::with_capacity_in(js.len() * 2, self.alloc); map.extend_from_slice(&(self.inner.len() as u32).to_le_bytes()); self.inner.sort(); diff --git a/rewriter/rewriter/src/lib.rs b/rewriter/rewriter/src/lib.rs index e0f05c2..addad3b 100644 --- a/rewriter/rewriter/src/lib.rs +++ b/rewriter/rewriter/src/lib.rs @@ -1,5 +1,5 @@ use oxc::{ - allocator::Allocator, + allocator::{Allocator, String, Vec}, ast_visit::Visit, diagnostics::OxcDiagnostic, parser::{ParseOptions, Parser}, @@ -18,7 +18,7 @@ use visitor::Visitor; #[derive(Error, Debug)] pub enum RewriterError { #[error("oxc panicked in parser: {0}")] - OxcPanicked(String), + OxcPanicked(std::string::String), #[error("out of bounds while applying range: {0}..{1})")] Oob(usize, usize), #[error("formatting error: {0}")] @@ -26,29 +26,27 @@ pub enum RewriterError { } #[derive(Debug)] -pub struct RewriteResult { - pub js: Vec, - pub sourcemap: Vec, - pub sourcetag: String, - pub errors: Vec, +pub struct RewriteResult<'alloc> { + pub js: Vec<'alloc, u8>, + pub sourcemap: Vec<'alloc, u8>, + pub errors: std::vec::Vec, } -pub fn rewrite( - js: &str, +pub fn rewrite<'alloc, 'data, E>( + alloc: &'alloc Allocator, + js: &'data str, + config: Config<'alloc, E>, module: bool, capacity: usize, - config: Config, -) -> Result +) -> Result, RewriterError> where - E: Fn(String) -> String, - E: Clone, + E: Fn(&str, &'alloc Allocator) -> String<'alloc>, { - let allocator = Allocator::default(); let source_type = SourceType::unambiguous() .with_javascript(true) .with_module(module) .with_standard(true); - let ret = Parser::new(&allocator, js, source_type) + let ret = Parser::new(alloc, js, source_type) .with_options(ParseOptions { parse_regular_expression: false, allow_v8_intrinsics: true, @@ -60,7 +58,7 @@ where if ret.panicked { use std::fmt::Write; - let mut errors = String::new(); + let mut errors = std::string::String::new(); for error in ret.errors { writeln!(errors, "{error}")?; } @@ -68,13 +66,15 @@ where } let mut visitor = Visitor { - jschanges: JsChanges::new(capacity), + jschanges: JsChanges::new(alloc, capacity), config, + alloc, }; visitor.visit_program(&ret.program); let Visitor { mut jschanges, config, + alloc: _, } = visitor; let JsChangeResult { js, sourcemap } = jschanges.perform(js, &config)?; @@ -82,7 +82,6 @@ where Ok(RewriteResult { js, sourcemap, - sourcetag: config.sourcetag, errors: ret.errors, }) } diff --git a/rewriter/rewriter/src/visitor.rs b/rewriter/rewriter/src/visitor.rs index 2b46af7..40a09b6 100644 --- a/rewriter/rewriter/src/visitor.rs +++ b/rewriter/rewriter/src/visitor.rs @@ -1,4 +1,5 @@ use oxc::{ + allocator::{Allocator, String}, ast::ast::{ AssignmentExpression, AssignmentTarget, CallExpression, DebuggerStatement, ExportAllDeclaration, ExportNamedDeclaration, Expression, ForInStatement, ForOfStatement, @@ -31,24 +32,23 @@ const UNSAFE_GLOBALS: &[&str] = &[ "frames", ]; -pub struct Visitor +pub struct Visitor<'alloc, 'data, E> where - E: Fn(String) -> String, - E: Clone, + E: Fn(&str, &'alloc Allocator) -> String<'alloc>, { - pub jschanges: JsChanges, - pub config: Config, + pub jschanges: JsChanges<'alloc, 'data>, + pub config: Config<'alloc, E>, + pub alloc: &'alloc Allocator, } -impl Visitor +impl<'alloc, 'data, E> Visitor<'alloc, 'data, E> where - E: Fn(String) -> String, - E: Clone, + E: Fn(&str, &'alloc Allocator) -> String<'alloc>, { - fn rewrite_url(&mut self, url: String) -> String { - let urlencoded = (self.config.urlrewriter)(url); - - format!("\"{}{}\"", self.config.prefix, urlencoded) + fn rewrite_url(&mut self, url: Atom<'data>) -> oxc::allocator::String<'alloc> { + let mut urlencoded = (self.config.urlrewriter)(&url, self.alloc); + urlencoded.insert_str(0, self.config.prefix); + urlencoded } fn rewrite_ident(&mut self, name: &Atom, span: Span) { @@ -82,10 +82,9 @@ where } } -impl<'a, E> Visit<'a> for Visitor +impl<'alloc, 'data, E> Visit<'data> for Visitor<'alloc, 'data, E> where - E: Fn(String) -> String, - E: Clone, + E: Fn(&str, &'alloc Allocator) -> String<'alloc>, { fn visit_identifier_reference(&mut self, it: &IdentifierReference) { // if self.config.capture_errors { @@ -108,12 +107,12 @@ where } // we need to rewrite `new Something` to `new (wrapfn(Something))` instead of `new wrapfn(Something)`, that's why there's weird extra code here - fn visit_new_expression(&mut self, it: &NewExpression) { + fn visit_new_expression(&mut self, it: &NewExpression<'data>) { self.walk_member_expression(&it.callee); walk::walk_arguments(self, &it.arguments); } - fn visit_member_expression(&mut self, it: &MemberExpression) { + fn visit_member_expression(&mut self, it: &MemberExpression<'data>) { // TODO // you could break this with ["postMessage"] etc // however this code only exists because of recaptcha whatever @@ -155,7 +154,7 @@ where // we can't overwrite window.eval in the normal way because that would make everything an // indirect eval, which could break things. we handle that edge case here - fn visit_call_expression(&mut self, it: &CallExpression<'a>) { + fn visit_call_expression(&mut self, it: &CallExpression<'data>) { if let Expression::Identifier(s) = &it.callee { // if it's optional that actually makes it an indirect eval which is handled separately if s.name == "eval" && !it.optional { @@ -176,34 +175,31 @@ where walk::walk_call_expression(self, it); } - fn visit_import_declaration(&mut self, it: &ImportDeclaration<'a>) { - let name = it.source.value.to_string(); - let text = self.rewrite_url(name); + fn visit_import_declaration(&mut self, it: &ImportDeclaration<'data>) { + let text = self.rewrite_url(it.source.value); self.jschanges.add(Rewrite::Replace { span: it.source.span, text, }); walk::walk_import_declaration(self, it); } - fn visit_import_expression(&mut self, it: &ImportExpression<'a>) { + fn visit_import_expression(&mut self, it: &ImportExpression<'data>) { self.jschanges.add(Rewrite::ImportFn { span: Span::new(it.span.start, it.span.start + 7), }); walk::walk_import_expression(self, it); } - fn visit_export_all_declaration(&mut self, it: &ExportAllDeclaration<'a>) { - let name = it.source.value.to_string(); - let text = self.rewrite_url(name); + fn visit_export_all_declaration(&mut self, it: &ExportAllDeclaration<'data>) { + let text = self.rewrite_url(it.source.value); self.jschanges.add(Rewrite::Replace { span: it.source.span, text, }); } - fn visit_export_named_declaration(&mut self, it: &ExportNamedDeclaration<'a>) { + fn visit_export_named_declaration(&mut self, it: &ExportNamedDeclaration<'data>) { if let Some(source) = &it.source { - let name = source.value.to_string(); - let text = self.rewrite_url(name); + let text = self.rewrite_url(source.value); self.jschanges.add(Rewrite::Replace { span: source.span, text, @@ -213,7 +209,7 @@ where } #[cfg(feature = "debug")] - fn visit_try_statement(&mut self, it: &oxc::ast::ast::TryStatement<'a>) { + fn visit_try_statement(&mut self, it: &oxc::ast::ast::TryStatement<'data>) { // for debugging we need to know what the error was if self.config.capture_errors { @@ -222,7 +218,7 @@ where if let Some(ident) = name.pattern.get_identifier_name() { self.jschanges.add(Rewrite::ScramErr { span: Span::new(h.body.span.start + 1, h.body.span.start + 1), - ident: ident.to_compact_str(), + ident, }); } } @@ -231,14 +227,14 @@ where walk::walk_try_statement(self, it); } - fn visit_object_expression(&mut self, it: &ObjectExpression<'a>) { + fn visit_object_expression(&mut self, it: &ObjectExpression<'data>) { for prop in &it.properties { if let ObjectPropertyKind::ObjectProperty(p) = prop { if let Expression::Identifier(s) = &p.value { if UNSAFE_GLOBALS.contains(&s.name.to_string().as_str()) && p.shorthand { self.jschanges.add(Rewrite::ShorthandObj { span: s.span, - name: s.name.to_compact_str(), + name: s.name, }); return; } @@ -249,7 +245,7 @@ where walk::walk_object_expression(self, it); } - fn visit_function_body(&mut self, it: &FunctionBody<'a>) { + fn visit_function_body(&mut self, it: &FunctionBody<'data>) { // tag function for use in sourcemaps if self.config.do_sourcemaps { self.jschanges.add(Rewrite::SourceTag { @@ -259,7 +255,7 @@ where walk::walk_function_body(self, it); } - fn visit_return_statement(&mut self, it: &ReturnStatement<'a>) { + fn visit_return_statement(&mut self, it: &ReturnStatement<'data>) { // if let Some(arg) = &it.argument { // self.jschanges.insert(JsChange::GenericChange { // span: Span::new(it.span.start + 6, it.span.start + 6), @@ -273,7 +269,7 @@ where walk::walk_return_statement(self, it); } - fn visit_unary_expression(&mut self, it: &UnaryExpression<'a>) { + fn visit_unary_expression(&mut self, it: &UnaryExpression<'data>) { if matches!(it.operator, UnaryOperator::Typeof) { // don't walk to identifier rewrites since it won't matter return; @@ -282,29 +278,29 @@ where } // we don't want to rewrite the identifiers here because of a very specific edge case - fn visit_for_in_statement(&mut self, it: &ForInStatement<'a>) { + fn visit_for_in_statement(&mut self, it: &ForInStatement<'data>) { walk::walk_statement(self, &it.body); } - fn visit_for_of_statement(&mut self, it: &ForOfStatement<'a>) { + fn visit_for_of_statement(&mut self, it: &ForOfStatement<'data>) { walk::walk_statement(self, &it.body); } - fn visit_update_expression(&mut self, _it: &UpdateExpression<'a>) { + fn visit_update_expression(&mut self, _it: &UpdateExpression<'data>) { // then no, don't walk it, we don't care } - fn visit_meta_property(&mut self, it: &MetaProperty<'a>) { + fn visit_meta_property(&mut self, it: &MetaProperty<'data>) { if it.meta.name == "import" { self.jschanges.add(Rewrite::MetaFn { span: it.span }); } } - fn visit_assignment_expression(&mut self, it: &AssignmentExpression<'a>) { + fn visit_assignment_expression(&mut self, it: &AssignmentExpression<'data>) { match &it.left { AssignmentTarget::AssignmentTargetIdentifier(s) => { if ["location"].contains(&s.name.to_string().as_str()) { self.jschanges.add(Rewrite::Assignment { - name: s.name.to_compact_str(), + name: s.name, entirespan: it.span, rhsspan: it.right.span(), op: it.operator, diff --git a/rewriter/wasm/src/lib.rs b/rewriter/wasm/src/lib.rs index 18f1918..acc8e9b 100644 --- a/rewriter/wasm/src/lib.rs +++ b/rewriter/wasm/src/lib.rs @@ -4,8 +4,11 @@ use std::{sync::Arc, time::Duration}; use error::{Result, RewriterError}; use instant::Instant; -use js_sys::{Function, Object, Reflect}; -use oxc::diagnostics::NamedSource; +use js_sys::{Function, Object, Reflect, Uint8Array}; +use oxc::{ + allocator::{Allocator, String}, + diagnostics::NamedSource, +}; use rewriter::{cfg::Config, rewrite, RewriteResult}; use wasm_bindgen::prelude::*; use web_sys::Url; @@ -30,7 +33,7 @@ export function scramtag() { } "#)] extern "C" { - pub fn scramtag() -> String; + pub fn scramtag() -> std::string::String; } #[wasm_bindgen] @@ -45,31 +48,40 @@ extern "C" { fn error(s: &str); } -fn create_encode_function( +type EncodeFn<'alloc, 'data> = Box String<'alloc> + 'data>; + +fn create_encode_function<'alloc, 'data>( encode: JsValue, - base: String, -) -> Result String + Clone> { + base: &'data str, + _alloc: &'alloc Allocator, +) -> Result> { let encode = encode.dyn_into::()?; - Ok(move |str: String| { - let url = Url::new_with_base(&str, &base).unwrap().to_string(); - encode - .call1(&JsValue::NULL, &url.into()) - .unwrap() - .as_string() - .unwrap() - .to_string() - }) + let func = move |url: &str, alloc: &'alloc Allocator| { + let url = Url::new_with_base(url, base).unwrap().to_string(); + oxc::allocator::String::from_str_in( + encode + .call1(&JsValue::NULL, &url.into()) + .unwrap() + .as_string() + .unwrap() + .as_str(), + alloc, + ) + }; + + Ok(Box::new(func)) } fn get_obj(obj: &JsValue, k: &str) -> Result { Ok(Reflect::get(obj, &k.into())?) } -fn get_str(obj: &JsValue, k: &str) -> Result { +fn get_str<'alloc>(obj: &JsValue, k: &str, alloc: &'alloc Allocator) -> Result<&'alloc str> { Reflect::get(obj, &k.into())? .as_string() .ok_or_else(|| RewriterError::not_str(k)) + .map(|x| String::from_str_in(&x, alloc).into_bump_str()) } fn set_obj(obj: &Object, k: &str, v: &JsValue) -> Result<()> { @@ -90,30 +102,34 @@ fn get_flag(scramjet: &Object, url: &str, flag: &str) -> Result { .ok_or_else(|| RewriterError::not_bool("scramjet.flagEnabled return value")) } -fn get_config(scramjet: &Object, url: String) -> Result String + Clone>> { +fn get_config<'alloc, 'data>( + scramjet: &Object, + url: &'data str, + alloc: &'alloc Allocator, +) -> Result>> { let codec = &get_obj(scramjet, "codec")?; let config = &get_obj(scramjet, "config")?; let globals = &get_obj(config, "globals")?; Ok(Config { - prefix: get_str(config, "prefix")?, - base: url.clone(), - sourcetag: scramtag(), + do_sourcemaps: get_flag(scramjet, url, "sourcemaps")?, + capture_errors: get_flag(scramjet, url, "captureErrors")?, + scramitize: get_flag(scramjet, url, "scramitize")?, + strict_rewrites: get_flag(scramjet, url, "strictRewrites")?, - wrapfn: get_str(globals, "wrapfn")?, - wrapthisfn: get_str(globals, "wrapthisfn")?, - importfn: get_str(globals, "importfn")?, - rewritefn: get_str(globals, "rewritefn")?, - metafn: get_str(globals, "metafn")?, - setrealmfn: get_str(globals, "setrealmfn")?, - pushsourcemapfn: get_str(globals, "pushsourcemapfn")?, + urlrewriter: create_encode_function(get_obj(codec, "encode")?, url, alloc)?, - do_sourcemaps: get_flag(scramjet, &url, "sourcemaps")?, - capture_errors: get_flag(scramjet, &url, "captureErrors")?, - scramitize: get_flag(scramjet, &url, "scramitize")?, - strict_rewrites: get_flag(scramjet, &url, "strictRewrites")?, + prefix: get_str(config, "prefix", alloc)?, + base: String::from_str_in(url, alloc).into_bump_str(), + sourcetag: String::from_str_in(&scramtag(), alloc).into_bump_str(), - urlrewriter: create_encode_function(get_obj(codec, "encode")?, url)?, + wrapfn: get_str(globals, "wrapfn", alloc)?, + wrapthisfn: get_str(globals, "wrapthisfn", alloc)?, + importfn: get_str(globals, "importfn", alloc)?, + rewritefn: get_str(globals, "rewritefn", alloc)?, + metafn: get_str(globals, "metafn", alloc)?, + setrealmfn: get_str(globals, "setrealmfn", alloc)?, + pushsourcemapfn: get_str(globals, "pushsourcemapfn", alloc)?, }) } @@ -124,8 +140,9 @@ fn duration_to_millis_f64(duration: Duration) -> f64 { fn create_rewriter_output( out: RewriteResult, - url: String, - src: String, + url: std::string::String, + src: std::string::String, + sourcetag: &str, duration: Duration, ) -> Result { let src = Arc::new(NamedSource::new(url, src).with_language("javascript")); @@ -137,9 +154,13 @@ fn create_rewriter_output( .collect(); let obj = Object::new(); - set_obj(&obj, "js", &out.js.into())?; - set_obj(&obj, "map", &out.sourcemap.into())?; - set_obj(&obj, "scramtag", &out.sourcetag.into())?; + set_obj(&obj, "js", &Uint8Array::from(out.js.as_slice()).into())?; + set_obj( + &obj, + "map", + &Uint8Array::from(out.sourcemap.as_slice()).into(), + )?; + set_obj(&obj, "scramtag", &sourcetag.into())?; #[cfg(feature = "debug")] set_obj(&obj, "errors", &errs.into())?; #[cfg(not(feature = "debug"))] @@ -151,33 +172,33 @@ fn create_rewriter_output( #[wasm_bindgen] pub fn rewrite_js( - js: String, - url: String, + js: std::string::String, + url: &str, + script_url: std::string::String, module: bool, - script_url: String, scramjet: &Object, ) -> Result { + let alloc = Allocator::default(); + let cfg = get_config(scramjet, url, &alloc)?; + let sourcetag = cfg.sourcetag; + let before = Instant::now(); - let out = rewrite(&js, module, 1024, get_config(scramjet, url)?)?; + let out = rewrite(&alloc, &js, cfg, module, 1024)?; let after = Instant::now(); - create_rewriter_output(out, script_url, js, after - before) + create_rewriter_output(out, script_url, js, sourcetag, after - before) } #[wasm_bindgen] pub fn rewrite_js_from_arraybuffer( js: Vec, - url: String, + url: &str, + script_url: std::string::String, module: bool, - script_url: String, scramjet: &Object, ) -> Result { - // we know that this is a valid utf-8 string - let js = unsafe { String::from_utf8_unchecked(js) }; + // we know the js is a valid utf-8 string + let js = unsafe { std::string::String::from_utf8_unchecked(js) }; - let before = Instant::now(); - let out = rewrite(&js, module, 1024, get_config(scramjet, url)?)?; - let after = Instant::now(); - - create_rewriter_output(out, script_url, js, after - before) + rewrite_js(js, url, script_url, module, scramjet) }