Add initial ES6 JavaScript modules implementation with ‘os’ module

This commit is contained in:
Tobie Morgan Hitchcock 2022-07-23 20:05:27 +01:00
parent d647e40d49
commit 3dc29e1228
9 changed files with 61 additions and 2 deletions

1
Cargo.lock generated
View file

@ -2719,7 +2719,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78ca2361848dddf910c27296d98bd62be9467d1d1d703eeead8c732adbd4e32" checksum = "e78ca2361848dddf910c27296d98bd62be9467d1d1d703eeead8c732adbd4e32"
dependencies = [ dependencies = [
"async-task", "async-task",
"chrono",
"flume 0.10.13", "flume 0.10.13",
"futures-lite", "futures-lite",
"pin-project-lite", "pin-project-lite",

View file

@ -37,7 +37,7 @@ futures = "0.3.21"
fuzzy-matcher = "0.3.7" fuzzy-matcher = "0.3.7"
geo = { version = "0.22.1", features = ["use-serde"] } geo = { version = "0.22.1", features = ["use-serde"] }
indxdb = { version = "0.2.0", optional = true } 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" lexical-sort = "0.3.1"
log = "0.4.17" log = "0.4.17"
md-5 = "0.10.1" md-5 = "0.10.1"

View file

@ -5,6 +5,9 @@ pub const MAX_CONCURRENT_TASKS: usize = 64;
// Specifies how many subqueries will be processed recursively before the query fails. // Specifies how many subqueries will be processed recursively before the query fails.
pub const MAX_RECURSIVE_QUERIES: usize = 16; 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. // The characters which are supported in server record IDs.
pub const ID_CHARS: [char; 36] = [ 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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',

View file

@ -1,5 +1,7 @@
use super::classes; use super::classes;
use super::executor::Executor; use super::executor::Executor;
use super::modules::loader;
use super::modules::resolver;
use crate::ctx::Context; use crate::ctx::Context;
use crate::err::Error; use crate::err::Error;
use crate::sql::value::Value; use crate::sql::value::Value;
@ -24,6 +26,8 @@ pub async fn run(
let run = js::Runtime::new().unwrap(); let run = js::Runtime::new().unwrap();
// Create an execution context // Create an execution context
let ctx = js::Context::full(&run).unwrap(); let ctx = js::Context::full(&run).unwrap();
// Set the module resolver and loader
run.set_loader(resolver(), loader());
// Enable async code in the runtime // Enable async code in the runtime
run.spawn_executor(&exe).detach(); run.spawn_executor(&exe).detach();
// Create the main function structure // Create the main function structure

View file

@ -8,3 +8,4 @@ mod executor;
mod from; mod from;
mod into; mod into;
mod main; mod main;
mod modules;

View file

@ -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)
}

View file

@ -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
}
}

View file

@ -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;
}

View file

@ -11,3 +11,9 @@ macro_rules! map {
m m
}}; }};
} }
macro_rules! get_cfg {
($i:ident : $($s:expr),+) => (
let $i = || { $( if cfg!($i=$s) { return $s; } );+ "unknown"};
)
}