From 3dc29e1228b5aadd3a604b4200f1700113f73df1 Mon Sep 17 00:00:00 2001 From: Tobie Morgan Hitchcock Date: Sat, 23 Jul 2022 20:05:27 +0100 Subject: [PATCH] =?UTF-8?q?Add=20initial=20ES6=20JavaScript=20modules=20im?= =?UTF-8?q?plementation=20with=20=E2=80=98os=E2=80=99=20module?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.lock | 1 - lib/Cargo.toml | 2 +- lib/src/cnf/mod.rs | 3 +++ lib/src/fnc/script/main.rs | 4 ++++ lib/src/fnc/script/mod.rs | 1 + lib/src/fnc/script/modules/mod.rs | 15 +++++++++++++++ lib/src/fnc/script/modules/os.rs | 25 +++++++++++++++++++++++++ lib/src/fnc/script/modules/surrealdb.rs | 6 ++++++ lib/src/mac/mod.rs | 6 ++++++ 9 files changed, 61 insertions(+), 2 deletions(-) create mode 100644 lib/src/fnc/script/modules/mod.rs create mode 100644 lib/src/fnc/script/modules/os.rs create mode 100644 lib/src/fnc/script/modules/surrealdb.rs diff --git a/Cargo.lock b/Cargo.lock index bcec8072..a3b435c6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2719,7 +2719,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e78ca2361848dddf910c27296d98bd62be9467d1d1d703eeead8c732adbd4e32" dependencies = [ "async-task", - "chrono", "flume 0.10.13", "futures-lite", "pin-project-lite", diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 89127a66..ed67d594 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -37,7 +37,7 @@ futures = "0.3.21" fuzzy-matcher = "0.3.7" geo = { version = "0.22.1", features = ["use-serde"] } indxdb = { version = "0.2.0", optional = true } -js = { version = "0.1.6", package = "rquickjs", features = ["chrono", "classes", "futures", "macro", "properties", "parallel"], optional = true } +js = { version = "0.1.6", package = "rquickjs", features = ["classes", "futures", "loader", "macro", "properties", "parallel"], optional = true } lexical-sort = "0.3.1" log = "0.4.17" md-5 = "0.10.1" diff --git a/lib/src/cnf/mod.rs b/lib/src/cnf/mod.rs index 7176ce75..19dd18ab 100644 --- a/lib/src/cnf/mod.rs +++ b/lib/src/cnf/mod.rs @@ -5,6 +5,9 @@ pub const MAX_CONCURRENT_TASKS: usize = 64; // Specifies how many subqueries will be processed recursively before the query fails. pub const MAX_RECURSIVE_QUERIES: usize = 16; +// The current package version release of the SurrealDB crate +pub const VERSION: &str = env!("CARGO_PKG_VERSION"); + // The characters which are supported in server record IDs. pub const ID_CHARS: [char; 36] = [ '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', diff --git a/lib/src/fnc/script/main.rs b/lib/src/fnc/script/main.rs index e6ee957f..141d76b2 100644 --- a/lib/src/fnc/script/main.rs +++ b/lib/src/fnc/script/main.rs @@ -1,5 +1,7 @@ use super::classes; use super::executor::Executor; +use super::modules::loader; +use super::modules::resolver; use crate::ctx::Context; use crate::err::Error; use crate::sql::value::Value; @@ -24,6 +26,8 @@ pub async fn run( let run = js::Runtime::new().unwrap(); // Create an execution context let ctx = js::Context::full(&run).unwrap(); + // Set the module resolver and loader + run.set_loader(resolver(), loader()); // Enable async code in the runtime run.spawn_executor(&exe).detach(); // Create the main function structure diff --git a/lib/src/fnc/script/mod.rs b/lib/src/fnc/script/mod.rs index 4b0dcc9c..d2ffacbc 100644 --- a/lib/src/fnc/script/mod.rs +++ b/lib/src/fnc/script/mod.rs @@ -8,3 +8,4 @@ mod executor; mod from; mod into; mod main; +mod modules; diff --git a/lib/src/fnc/script/modules/mod.rs b/lib/src/fnc/script/modules/mod.rs new file mode 100644 index 00000000..17580f33 --- /dev/null +++ b/lib/src/fnc/script/modules/mod.rs @@ -0,0 +1,15 @@ +pub mod os; +pub mod surrealdb; + +use js::BuiltinResolver; +use js::ModuleLoader; + +pub fn resolver() -> BuiltinResolver { + BuiltinResolver::default().with_module("os").with_module("surrealdb") +} + +pub fn loader() -> ModuleLoader { + ModuleLoader::default() + .with_module("os", os::Package) + .with_module("surrealdb", surrealdb::Package) +} diff --git a/lib/src/fnc/script/modules/os.rs b/lib/src/fnc/script/modules/os.rs new file mode 100644 index 00000000..6060efae --- /dev/null +++ b/lib/src/fnc/script/modules/os.rs @@ -0,0 +1,25 @@ +#[js::bind(module, public)] +#[quickjs(bare)] +#[allow(non_upper_case_globals)] +pub mod package { + // Get the target system architecture + pub fn arch() -> &'static str { + get_cfg!(target_arch: "x86", "x86_64", "mips", "powerpc", "powerpc64", "arm", "aarch64"); + target_arch() + } + // Get the target operating system + pub fn platform() -> &'static str { + get_cfg!(target_os: "windows", "macos", "ios", "linux", "android", "freebsd", "openbsd", "netbsd"); + target_os() + } + // Get the target release text + pub fn release() -> String { + get_cfg!(target_os: "windows", "macos", "ios", "linux", "android", "freebsd", "openbsd", "netbsd"); + get_cfg!(target_arch: "x86", "x86_64", "mips", "powerpc", "powerpc64", "arm", "aarch64"); + format!("{} for {} on {}", crate::cnf::VERSION, target_os(), target_arch()) + } + // Get the current version + pub fn version() -> &'static str { + crate::cnf::VERSION + } +} diff --git a/lib/src/fnc/script/modules/surrealdb.rs b/lib/src/fnc/script/modules/surrealdb.rs new file mode 100644 index 00000000..e5a3adf0 --- /dev/null +++ b/lib/src/fnc/script/modules/surrealdb.rs @@ -0,0 +1,6 @@ +#[js::bind(module, public)] +#[quickjs(bare)] +#[allow(non_upper_case_globals)] +pub mod package { + pub const version: &str = crate::cnf::VERSION; +} diff --git a/lib/src/mac/mod.rs b/lib/src/mac/mod.rs index 0b9e1bb8..5887410d 100644 --- a/lib/src/mac/mod.rs +++ b/lib/src/mac/mod.rs @@ -11,3 +11,9 @@ macro_rules! map { m }}; } + +macro_rules! get_cfg { + ($i:ident : $($s:expr),+) => ( + let $i = || { $( if cfg!($i=$s) { return $s; } );+ "unknown"}; + ) +}