From 8cfc286beb159949a5b40a16a6f7afee919f83f8 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sun, 12 Feb 2023 12:20:47 +0000 Subject: [PATCH] Add global `console` object to JavaScript runtime Closes #1634 --- lib/src/fnc/script/globals/console.rs | 57 +++++++++++++++++++++++++++ lib/src/fnc/script/globals/mod.rs | 1 + lib/src/fnc/script/main.rs | 6 +++ lib/src/fnc/script/mod.rs | 2 + 4 files changed, 66 insertions(+) create mode 100644 lib/src/fnc/script/globals/console.rs diff --git a/lib/src/fnc/script/globals/console.rs b/lib/src/fnc/script/globals/console.rs new file mode 100644 index 00000000..21f37500 --- /dev/null +++ b/lib/src/fnc/script/globals/console.rs @@ -0,0 +1,57 @@ +#[js::bind(object, public)] +#[quickjs(rename = "console")] +#[allow(clippy::module_inception)] +pub mod console { + // Specify the imports + use crate::fnc::script::LOG; + use crate::sql::value::Value; + use js::Rest; + /// Log the input values as INFO + pub fn log(args: Rest) { + info!( + target: LOG, + "{}", + args.iter().map(|v| v.to_raw_string()).collect::>().join(" ") + ); + } + /// Log the input values as INFO + pub fn info(args: Rest) { + info!( + target: LOG, + "{}", + args.iter().map(|v| v.to_raw_string()).collect::>().join(" ") + ); + } + /// Log the input values as WARN + pub fn warn(args: Rest) { + warn!( + target: LOG, + "{}", + args.iter().map(|v| v.to_raw_string()).collect::>().join(" ") + ); + } + /// Log the input values as ERROR + pub fn error(args: Rest) { + error!( + target: LOG, + "{}", + args.iter().map(|v| v.to_raw_string()).collect::>().join(" ") + ); + } + /// Log the input values as DEBUG + pub fn debug(args: Rest) { + debug!( + target: LOG, + "{}", + args.iter().map(|v| v.to_raw_string()).collect::>().join(" ") + ); + } + /// Log the input values as TRACE + pub fn trace(args: Rest) { + trace!( + target: LOG, + "{}", + args.iter().map(|v| v.to_raw_string()).collect::>().join(" ") + ); + } +} diff --git a/lib/src/fnc/script/globals/mod.rs b/lib/src/fnc/script/globals/mod.rs index 0e40308b..ee0b392f 100644 --- a/lib/src/fnc/script/globals/mod.rs +++ b/lib/src/fnc/script/globals/mod.rs @@ -1 +1,2 @@ +pub mod console; pub mod fetch; diff --git a/lib/src/fnc/script/main.rs b/lib/src/fnc/script/main.rs index 188c4a9a..b49d98f1 100644 --- a/lib/src/fnc/script/main.rs +++ b/lib/src/fnc/script/main.rs @@ -5,6 +5,8 @@ use super::modules; use super::modules::loader; use super::modules::resolver; use crate::ctx::Context; +use crate::dbs::Options; +use crate::dbs::Transaction; use crate::err::Error; use crate::sql::value::Value; use js::Function; @@ -15,6 +17,8 @@ use js::This; pub async fn run( ctx: &Context<'_>, + _opt: &Options, + _txn: &Transaction, doc: Option<&Value>, src: &str, arg: Vec, @@ -57,6 +61,8 @@ pub async fn run( )?; // Register the fetch function to the globals global.init_def::()?; + // Register the console function to the globals + global.init_def::()?; // Register the special SurrealDB types as classes global.init_def::()?; global.init_def::()?; diff --git a/lib/src/fnc/script/mod.rs b/lib/src/fnc/script/mod.rs index c473a64d..189bf29d 100644 --- a/lib/src/fnc/script/mod.rs +++ b/lib/src/fnc/script/mod.rs @@ -1,5 +1,7 @@ #![cfg(feature = "scripting")] +const LOG: &str = "surrealdb::jsr"; + pub use main::run; mod classes;