add rquickjs

This commit is contained in:
2026-04-02 23:42:21 +08:00
parent b73fd9e64e
commit e5135515c7
3 changed files with 234 additions and 0 deletions

163
__lang/rquickjs-test/Cargo.lock generated Normal file
View File

@@ -0,0 +1,163 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "allocator-api2"
version = "0.2.21"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "683d7910e743518b0e34f1186f92494becacb047c7b6bf616c96772180fef923"
[[package]]
name = "cc"
version = "1.2.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1e928d4b69e3077709075a938a05ffbedfa53a84c8f766efbf8220bb1ff60e1"
dependencies = [
"find-msvc-tools",
"shlex",
]
[[package]]
name = "equivalent"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f"
[[package]]
name = "find-msvc-tools"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582"
[[package]]
name = "foldhash"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb"
[[package]]
name = "hashbrown"
version = "0.16.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100"
dependencies = [
"allocator-api2",
"equivalent",
"foldhash",
]
[[package]]
name = "proc-macro2"
version = "1.0.106"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8fd00f0bb2e90d81d1044c2b32617f68fcb9fa3bb7640c23e9c748e53fb30934"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41f2619966050689382d2b44f664f4bc593e129785a36d6ee376ddf37259b924"
dependencies = [
"proc-macro2",
]
[[package]]
name = "relative-path"
version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bca40a312222d8ba74837cb474edef44b37f561da5f773981007a10bbaa992b0"
dependencies = [
"serde",
]
[[package]]
name = "rquickjs"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c50dc6d6c587c339edb4769cf705867497a2baf0eca8b4645fa6ecd22f02c77a"
dependencies = [
"rquickjs-core",
]
[[package]]
name = "rquickjs-core"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b8bf7840285c321c3ab20e752a9afb95548c75cd7f4632a0627cea3507e310c1"
dependencies = [
"hashbrown",
"relative-path",
"rquickjs-sys",
]
[[package]]
name = "rquickjs-sys"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "27344601ef27460e82d6a4e1ecb9e7e99f518122095f3c51296da8e9be2b9d83"
dependencies = [
"cc",
]
[[package]]
name = "rquickjs-test"
version = "0.1.0"
dependencies = [
"rquickjs",
]
[[package]]
name = "serde"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e"
dependencies = [
"serde_core",
]
[[package]]
name = "serde_core"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.228"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "shlex"
version = "1.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64"
[[package]]
name = "syn"
version = "2.0.117"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e665b8803e7b1d2a727f4023456bbbbe74da67099c585258af0ad9c5013b9b99"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.24"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e6e4313cd5fcd3dad5cafa179702e2b244f760991f45397d14d4ebf38247da75"

View File

@@ -0,0 +1,11 @@
[package]
name = "rquickjs-test"
version = "0.1.0"
edition = "2024"
[dependencies]
rquickjs = "0.11.0"
[profile.release]
lto = true
opt-level = 'z'

View File

@@ -0,0 +1,60 @@
use std::io::Write;
use std::sync::atomic::{AtomicBool, Ordering};
use rquickjs::{CatchResultExt, Context, Function, Object, Result, Runtime, Value};
fn print(s: String) {
println!("{s}");
}
fn main() -> Result<()> {
let rt = Runtime::new()?;
let ctx = Context::full(&rt)?;
let should_exit = std::sync::Arc::new(AtomicBool::new(false));
ctx.with(|ctx| -> Result<()> {
let global = ctx.globals();
let should_exit_clone = should_exit.clone();
global.set(
"__print",
Function::new(ctx.clone(), print)?.with_name("__print")?,
)?;
global.set(
"exit",
Function::new(ctx.clone(), move || {
should_exit_clone.store(true, Ordering::Relaxed);
})?,
)?;
ctx.eval::<(), _>(
r#"
globalThis.console = {
log(...v) {
globalThis.__print(`${v.join(" ")}`)
}
}
"#,
)?;
let console: Object = global.get("console")?;
let js_log: Function = console.get("log")?;
loop {
if should_exit.load(Ordering::Relaxed) {
break;
}
let mut input = String::new();
print!("> ");
std::io::stdout().flush()?;
std::io::stdin().read_line(&mut input)?;
ctx.eval::<Value, _>(input.as_bytes())
.and_then(|ret| js_log.call::<(Value<'_>,), ()>((ret,)))
.catch(&ctx)
.unwrap_or_else(|err| println!("{err}"));
}
Ok(())
})?;
Ok(())
}