feat: use rquickjs crate

This commit is contained in:
GyDi 2022-08-10 13:05:48 +08:00 committed by GitHub
parent 38effaf740
commit cfd04e9bb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 98 additions and 126 deletions

152
src-tauri/Cargo.lock generated
View File

@ -64,7 +64,7 @@ version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d52a9bb7ec0cf484c551830a7ce27bd20d67eac647e1befb56b0be4ee39a55d2"
dependencies = [
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -131,7 +131,7 @@ dependencies = [
"slab",
"socket2",
"waker-fn",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -168,7 +168,7 @@ dependencies = [
"libc",
"once_cell",
"signal-hook 0.3.14",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -484,7 +484,7 @@ dependencies = [
"num-integer",
"num-traits",
"time 0.1.44",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -504,8 +504,8 @@ dependencies = [
"open 2.1.3",
"parking_lot 0.12.1",
"port_scanner",
"quick-js",
"reqwest",
"rquickjs",
"runas",
"serde",
"serde_json",
@ -593,15 +593,6 @@ version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6245d59a3e82a7fc217c5828a6692dbc6dfb63a0c8c90495621f7b9d79704a0e"
[[package]]
name = "copy_dir"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e4281031634644843bd2f5aa9c48cf98fc48d6b083bd90bb11becf10deaf8b0"
dependencies = [
"walkdir 0.1.8",
]
[[package]]
name = "core-foundation"
version = "0.9.3"
@ -801,7 +792,7 @@ dependencies = [
"rand 0.8.5",
"shared_library",
"termwiz",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -928,7 +919,7 @@ checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6"
dependencies = [
"libc",
"redox_users",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -939,7 +930,7 @@ checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d"
dependencies = [
"libc",
"redox_users",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -1045,7 +1036,7 @@ checksum = "7199d965852c3bac31f779ef99cbb4537f80e952e2d6aa0ffeb30cce00f4f46e"
dependencies = [
"libc",
"thiserror",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -1306,7 +1297,7 @@ dependencies = [
"libc",
"log",
"rustversion",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -1374,7 +1365,7 @@ dependencies = [
"gobject-sys",
"libc",
"system-deps 6.0.2",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -1734,7 +1725,7 @@ dependencies = [
"regex",
"same-file",
"thread_local",
"walkdir 2.3.2",
"walkdir",
"winapi-util",
]
@ -1841,7 +1832,7 @@ dependencies = [
"jni-sys",
"log",
"thiserror",
"walkdir 2.3.2",
"walkdir",
]
[[package]]
@ -1855,7 +1846,7 @@ dependencies = [
"jni-sys",
"log",
"thiserror",
"walkdir 2.3.2",
"walkdir",
]
[[package]]
@ -1884,16 +1875,6 @@ dependencies = [
"treediff",
]
[[package]]
name = "kernel32-sys"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
dependencies = [
"winapi 0.2.8",
"winapi-build",
]
[[package]]
name = "kuchiki"
version = "0.8.1"
@ -1949,17 +1930,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "efbc0f03f9a775e9f6aed295c6a1ba2253c5757a9e03d55c6caa46a681abcddd"
dependencies = [
"cfg-if 1.0.0",
"winapi 0.3.9",
]
[[package]]
name = "libquickjs-sys"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f0b24e9bd171b75ae0295bd428fb8fe58410fb23156e5f34a4657a70c3cee96"
dependencies = [
"cc",
"copy_dir",
"winapi",
]
[[package]]
@ -2026,7 +1997,7 @@ dependencies = [
"thiserror",
"thread-id",
"typemap",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -2505,7 +2476,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c92f2b54f081d635c77e7120862d48db8e91f7f21cef23ab1b4fe9971c59f55"
dependencies = [
"libc",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -2571,7 +2542,7 @@ dependencies = [
"libc",
"redox_syscall",
"smallvec",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -2808,7 +2779,7 @@ dependencies = [
"libc",
"log",
"wepoll-ffi",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -2884,16 +2855,6 @@ version = "1.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
[[package]]
name = "quick-js"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "19cb4cefcb00f4ab9b332664d06005a74f582ac16aa959c6ad5912957bd83e5f"
dependencies = [
"libquickjs-sys",
"once_cell",
]
[[package]]
name = "quote"
version = "1.0.20"
@ -3045,7 +3006,7 @@ version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7"
dependencies = [
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -3110,6 +3071,33 @@ dependencies = [
"windows",
]
[[package]]
name = "rquickjs"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "989910af47b3f21c95205a1e1974bfb0f460e5492f469631297cc44c3340967e"
dependencies = [
"rquickjs-core",
]
[[package]]
name = "rquickjs-core"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78ca2361848dddf910c27296d98bd62be9467d1d1d703eeead8c732adbd4e32"
dependencies = [
"rquickjs-sys",
]
[[package]]
name = "rquickjs-sys"
version = "0.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "263a9a14fc68f2f3403c67e94e5def69af60afbcf7914d2a7fb8f9e17027fa25"
dependencies = [
"cc",
]
[[package]]
name = "rs-snowflake"
version = "0.6.0"
@ -3487,7 +3475,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0d94659ad3c2137fef23ae75b03d5241d633f8acded53d672decfa0e6e0caef"
dependencies = [
"libc",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -3572,7 +3560,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "66d72b759436ae32898a2af0a14218dbf55efde3feeb170eb623637db85ee1e0"
dependencies = [
"libc",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -3840,7 +3828,7 @@ dependencies = [
"thiserror",
"time 0.3.11",
"uuid 1.1.2",
"walkdir 2.3.2",
"walkdir",
]
[[package]]
@ -3920,7 +3908,7 @@ dependencies = [
"serde_with",
"thiserror",
"url",
"walkdir 2.3.2",
"walkdir",
"windows",
]
@ -3935,7 +3923,7 @@ dependencies = [
"libc",
"redox_syscall",
"remove_dir_all",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -4000,7 +3988,7 @@ dependencies = [
"ucd-trie",
"unicode-segmentation",
"vtparse",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -4037,7 +4025,7 @@ checksum = "5fdfe0627923f7411a43ec9ec9c39c3a9b4151be313e0922042581fb6c9b717f"
dependencies = [
"libc",
"redox_syscall",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -4057,7 +4045,7 @@ checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255"
dependencies = [
"libc",
"wasi 0.10.0+wasi-snapshot-preview1",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -4104,7 +4092,7 @@ dependencies = [
"signal-hook-registry",
"socket2",
"tokio-macros",
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -4481,16 +4469,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca"
[[package]]
name = "walkdir"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c66c0b9792f0a765345452775f3adbd28dde9d33f30d13e5dcc5ae17cf6f3780"
dependencies = [
"kernel32-sys",
"winapi 0.2.8",
]
[[package]]
name = "walkdir"
version = "2.3.2"
@ -4498,7 +4476,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56"
dependencies = [
"same-file",
"winapi 0.3.9",
"winapi",
"winapi-util",
]
@ -4756,12 +4734,6 @@ version = "2.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee583bdc5ff1cf9db20e9db5bb3ff4c3089a8f6b8b31aff265c9aba85812db86"
[[package]]
name = "winapi"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a"
[[package]]
name = "winapi"
version = "0.3.9"
@ -4772,12 +4744,6 @@ dependencies = [
"winapi-x86_64-pc-windows-gnu",
]
[[package]]
name = "winapi-build"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc"
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
@ -4790,7 +4756,7 @@ version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"winapi 0.3.9",
"winapi",
]
[[package]]
@ -4948,7 +4914,7 @@ version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "80d0f4e272c85def139476380b12f9ac60926689dd2e01d4923222f40580869d"
dependencies = [
"winapi 0.3.9",
"winapi",
]
[[package]]

View File

@ -33,7 +33,7 @@ tokio = { version = "1", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
reqwest = { version = "0.11", features = ["json"] }
tauri = { version = "1.0.0", features = ["process-all", "shell-all", "system-tray", "updater", "window-all"] }
quick-js = { version = "0.4.1" }
rquickjs = { version = "0.1.6" }
window-shadows = { version = "0.1" }
window-vibrancy = { version = "0.1" }

View File

@ -144,46 +144,50 @@ pub fn use_script(
config: Mapping,
valid: Vec<String>,
) -> Result<(Mapping, Vec<(String, String)>)> {
use quick_js::{Context, JsValue};
use rquickjs::{Context, Func, Runtime};
use std::sync::{Arc, Mutex};
let context = Context::new().unwrap();
let runtime = Runtime::new().unwrap();
let context = Context::full(&runtime).unwrap();
let outputs = Arc::new(Mutex::new(vec![]));
let copy_outputs = outputs.clone();
context.add_callback("__verge__log__", move |level: String, data: String| {
let mut out = copy_outputs.lock().unwrap();
out.push((level, data));
JsValue::Undefined
})?;
let result = context.with(|ctx| -> Result<Mapping> {
ctx.globals().set(
"__verge_log__",
Func::from(move |level: String, data: String| {
let mut out = copy_outputs.lock().unwrap();
out.push((level, data));
}),
)?;
context.eval(
r#"var console = Object.freeze({
log(data){__verge__log__("log",JSON.stringify(data))},
info(data){__verge__log__("info",JSON.stringify(data))},
error(data){__verge__log__("error",JSON.stringify(data))},
});"#,
)?;
ctx.eval(
r#"var console = Object.freeze({
log(data){__verge_log__("log",JSON.stringify(data))},
info(data){__verge_log__("info",JSON.stringify(data))},
error(data){__verge_log__("error",JSON.stringify(data))},
debug(data){__verge_log__("debug",JSON.stringify(data))},
});"#,
)?;
let config_str = serde_json::to_string(&config)?;
let config_str = serde_json::to_string(&config)?;
let code = format!("\n{script}\n;\nJSON.stringify(main({config_str}))");
let result = context.eval(code.as_str())?;
let code = format!("\n{script}\n;\nJSON.stringify(main({config_str})||'')");
let result: String = ctx.eval(code.as_str())?;
if result == "\"\"" {
anyhow::bail!("main function should return object");
}
Ok(serde_json::from_str::<Mapping>(result.as_str())?)
});
let mut out = outputs.lock().unwrap();
match result.into_string() {
Some(result) => match serde_json::from_str::<Mapping>(result.as_str()) {
Ok(config) => {
let config = use_valid_filter(config, valid);
Ok((config, out.to_vec()))
}
Err(_) => {
out.push(("error".into(), "failed to parse json string".into()));
Ok((config, out.to_vec()))
}
},
None => {
out.push(("error".into(), "main function should return object".into()));
match result {
Ok(config) => {
let config = use_valid_filter(config, valid);
Ok((config, out.to_vec()))
}
Err(err) => {
out.push(("error".into(), err.to_string()));
Ok((config, out.to_vec()))
}
}
@ -235,9 +239,11 @@ fn test_merge() -> Result<()> {
fn test_script() {
let script = r#"
function main(config) {
config.rules = [...config.rules, "add"];
config.proxies = ["111"];
if (Array.isArray(config.rules)) {
config.rules = [...config.rules, "add"];
}
console.log(config);
config.proxies = ["111"];
return config;
}
"#;