diff --git a/Cargo.lock b/Cargo.lock index 9deb3d43..2530df7b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -668,17 +668,17 @@ dependencies = [ [[package]] name = "bindgen" -version = "0.66.1" +version = "0.69.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f2b84e06fc203107bfbad243f4aba2af864eb7db3b1cf46ea0a023b0b433d2a7" +checksum = "a00dc851838a2120612785d195287475a3ac45514741da670b735818822129a0" dependencies = [ "bitflags 2.5.0", "cexpr", "clang-sys", + "itertools 0.12.1", "lazy_static", "lazycell", "log", - "peeking_take_while", "prettyplease", "proc-macro2", "quote", @@ -3043,12 +3043,6 @@ dependencies = [ "sha2", ] -[[package]] -name = "peeking_take_while" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099" - [[package]] name = "pem" version = "3.0.4" @@ -3764,9 +3758,9 @@ dependencies = [ [[package]] name = "rquickjs" -version = "0.5.1" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad7f63201fa6f2ff8173e4758ea552549d687d8f63003361a8b5c50f7c446ded" +checksum = "9cbd33e0b668aea0ab238b9164523aca929096f9f40834700d71d91dd4888882" dependencies = [ "rquickjs-core", "rquickjs-macro", @@ -3774,9 +3768,9 @@ dependencies = [ [[package]] name = "rquickjs-core" -version = "0.5.1" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cad00eeddc0f88af54ee202c8385fb214fe0423897c056a7df8369fb482e3695" +checksum = "2e9129d69b7b8f7ee8ad1da5b12c7f4a8a8acd45f2e6dd9cb2ee1bc5a1f2fa3d" dependencies = [ "chrono", "relative-path", @@ -3785,9 +3779,9 @@ dependencies = [ [[package]] name = "rquickjs-macro" -version = "0.5.1" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f27b39e889cc951e3e5f6b74012f943e642fa0fac51a8552948751f19a9b62f8" +checksum = "c7d2ecaf7c9eda262e02a91e9541989a9dd18984d17d0d97f99f33b464318057" dependencies = [ "convert_case", "fnv", @@ -3803,9 +3797,9 @@ dependencies = [ [[package]] name = "rquickjs-sys" -version = "0.5.1" +version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "120dbbc3296de9b96de8890091635d46f3506cd38b4e8f21800c386c035d64fa" +checksum = "bf6f2288d8e7fbb5130f62cf720451641e99d55f6fde9db86aa2914ecb553fd2" dependencies = [ "bindgen", "cc", diff --git a/chirpstack/Cargo.toml b/chirpstack/Cargo.toml index 0bd67a46..5b8462dc 100644 --- a/chirpstack/Cargo.toml +++ b/chirpstack/Cargo.toml @@ -118,7 +118,7 @@ rumqttc = { version = "0.24", features = ["url"] } hex = "0.4" # Codecs -rquickjs = { version = "0.5", features = [ +rquickjs = { version = "0.6", features = [ "bindgen", "loader", "array-buffer", diff --git a/chirpstack/src/adr/plugin.rs b/chirpstack/src/adr/plugin.rs index 3ce82eca..d22febb1 100644 --- a/chirpstack/src/adr/plugin.rs +++ b/chirpstack/src/adr/plugin.rs @@ -18,9 +18,10 @@ impl Plugin { let script = fs::read_to_string(file_path).context("Read ADR plugin")?; let (id, name) = ctx.with::<_, Result<(String, String)>>(|ctx| { - let m = ctx - .compile("script", script.clone()) - .context("Compile script")?; + let m = rquickjs::Module::declare(ctx, "script", script.clone()) + .context("Declare script")?; + let (m, m_promise) = m.eval().context("Evaluate script")?; + m_promise.finish()?; let id_func: rquickjs::Function = m.get("id").context("Get id function")?; let name_func: rquickjs::Function = m.get("name").context("Get name function")?; @@ -51,10 +52,10 @@ impl Handler for Plugin { let ctx = rquickjs::Context::full(&rt)?; ctx.with::<_, Result>(|ctx| { - let m = ctx - .clone() - .compile("script", self.script.clone()) - .context("Compile script")?; + let m = rquickjs::Module::declare(ctx.clone(), "script", self.script.clone()) + .context("Declare script")?; + let (m, m_promise) = m.eval().context("Evaluate script")?; + m_promise.finish()?; let func: rquickjs::Function = m.get("handle").context("Get handle function")?; let device_variables = rquickjs::Object::new(ctx.clone())?; diff --git a/chirpstack/src/codec/js/mod.rs b/chirpstack/src/codec/js/mod.rs index 0a9ba4c0..b5383583 100644 --- a/chirpstack/src/codec/js/mod.rs +++ b/chirpstack/src/codec/js/mod.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::time::SystemTime; -use anyhow::Result; +use anyhow::{Context, Result}; use chrono::{DateTime, Utc}; use rquickjs::{CatchResultExt, IntoJs}; @@ -50,13 +50,17 @@ pub async fn decode( let out = ctx.with(|ctx| -> Result { // We need to export the Buffer class, as eval / eval_with_options // does not allow using import statement. - let buff: rquickjs::Module = ctx.clone().compile( + let buff = rquickjs::Module::declare( + ctx.clone(), "b", r#" import { Buffer } from "buffer"; export { Buffer } "#, - )?; + ) + .context("Declare script")?; + let (buff, buff_promise) = buff.eval().context("Evalulate script")?; + buff_promise.finish()?; let buff: rquickjs::Function = buff.get("Buffer")?; let input = rquickjs::Object::new(ctx.clone())?; @@ -69,14 +73,11 @@ pub async fn decode( globals.set("chirpstack_input", input)?; globals.set("Buffer", buff)?; + let mut eval_options = rquickjs::context::EvalOptions::default(); + eval_options.strict = false; + let res: rquickjs::Object = ctx - .eval_with_options( - script, - rquickjs::context::EvalOptions { - strict: false, - ..Default::default() - }, - ) + .eval_with_options(script, eval_options) .catch(&ctx) .map_err(|e| anyhow!("JS error: {}", e))?; @@ -137,13 +138,17 @@ pub async fn encode( ctx.with(|ctx| { // We need to export the Buffer class, as eval / eval_with_options // does not allow using import statement. - let buff: rquickjs::Module = ctx.clone().compile( + let buff = rquickjs::Module::declare( + ctx.clone(), "b", r#" import { Buffer } from "buffer"; export { Buffer } "#, - )?; + ) + .context("Declare script")?; + let (buff, buff_promise) = buff.eval().context("Evaluate script")?; + buff_promise.finish()?; let buff: rquickjs::Function = buff.get("Buffer")?; let input = rquickjs::Object::new(ctx.clone())?; @@ -155,14 +160,11 @@ pub async fn encode( globals.set("chirpstack_input", input)?; globals.set("Buffer", buff)?; + let mut eval_options = rquickjs::context::EvalOptions::default(); + eval_options.strict = false; + let res: rquickjs::Object = ctx - .eval_with_options( - script, - rquickjs::context::EvalOptions { - strict: false, - ..Default::default() - }, - ) + .eval_with_options(script, eval_options) .catch(&ctx) .map_err(|e| anyhow!("JS error: {}", e))?;