Add global console object to JavaScript runtime

Closes #1634
This commit is contained in:
Tobie Morgan Hitchcock 2023-02-12 12:20:47 +00:00
parent a4db20fb05
commit 8cfc286beb
4 changed files with 66 additions and 0 deletions

View file

@ -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<Value>) {
info!(
target: LOG,
"{}",
args.iter().map(|v| v.to_raw_string()).collect::<Vec<String>>().join(" ")
);
}
/// Log the input values as INFO
pub fn info(args: Rest<Value>) {
info!(
target: LOG,
"{}",
args.iter().map(|v| v.to_raw_string()).collect::<Vec<String>>().join(" ")
);
}
/// Log the input values as WARN
pub fn warn(args: Rest<Value>) {
warn!(
target: LOG,
"{}",
args.iter().map(|v| v.to_raw_string()).collect::<Vec<String>>().join(" ")
);
}
/// Log the input values as ERROR
pub fn error(args: Rest<Value>) {
error!(
target: LOG,
"{}",
args.iter().map(|v| v.to_raw_string()).collect::<Vec<String>>().join(" ")
);
}
/// Log the input values as DEBUG
pub fn debug(args: Rest<Value>) {
debug!(
target: LOG,
"{}",
args.iter().map(|v| v.to_raw_string()).collect::<Vec<String>>().join(" ")
);
}
/// Log the input values as TRACE
pub fn trace(args: Rest<Value>) {
trace!(
target: LOG,
"{}",
args.iter().map(|v| v.to_raw_string()).collect::<Vec<String>>().join(" ")
);
}
}

View file

@ -1 +1,2 @@
pub mod console;
pub mod fetch;

View file

@ -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<Value>,
@ -57,6 +61,8 @@ pub async fn run(
)?;
// Register the fetch function to the globals
global.init_def::<globals::fetch::Fetch>()?;
// Register the console function to the globals
global.init_def::<globals::console::Console>()?;
// Register the special SurrealDB types as classes
global.init_def::<classes::duration::Duration>()?;
global.init_def::<classes::record::Record>()?;

View file

@ -1,5 +1,7 @@
#![cfg(feature = "scripting")]
const LOG: &str = "surrealdb::jsr";
pub use main::run;
mod classes;