From 61bf7c80e9782bbf40c59f62ec31fa8ba0a25b57 Mon Sep 17 00:00:00 2001 From: Emile Fugulin Date: Mon, 18 Nov 2024 22:41:43 -0500 Subject: [PATCH] Remove workspace versions and add os --- Cargo.toml | 16 --- libs/test/Cargo.toml | 6 +- libs/utils/Cargo.toml | 2 +- libs/utils/src/lib.rs | 1 + libs/utils/src/sysinfo.rs | 22 ++++ modules/console/Cargo.toml | 4 +- modules/os/Cargo.toml | 15 ++- modules/os/src/lib.rs | 254 +++++++++++++++++++++++++++++++++++++ modules/os/src/unix.rs | 49 +++++++ modules/os/src/windows.rs | 0 modules/sqlite/Cargo.toml | 16 ++- modules/timers/Cargo.toml | 10 +- modules/url/Cargo.toml | 4 +- 13 files changed, 365 insertions(+), 34 deletions(-) create mode 100644 libs/utils/src/sysinfo.rs create mode 100644 modules/os/src/unix.rs create mode 100644 modules/os/src/windows.rs diff --git a/Cargo.toml b/Cargo.toml index a5103ef..b04d5ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,19 +25,3 @@ rquickjs-extra-url = { path = "modules/url", optional = true } [workspace] resolver = "2" members = ["modules/*", "libs/*"] - -[workspace.dependencies] -either = "1" -futures = { version = "0.3" } -log = { version = "0.4" } -rquickjs = { version = "0.6", features = [ - "array-buffer", - "either", - "macro", - "futures", -] } -sqlx = { version = "0.8.2", default-features = false, features = [ - "sqlite", - "runtime-tokio", -] } -tokio = { version = "1" } diff --git a/libs/test/Cargo.toml b/libs/test/Cargo.toml index 46a7da7..34ea431 100644 --- a/libs/test/Cargo.toml +++ b/libs/test/Cargo.toml @@ -8,6 +8,6 @@ repository = "https://github.com/rquickjs/rquickjs-extra" readme = "README.md" [dependencies] -futures = { workspace = true } -rquickjs = { workspace = true } -tokio = { workspace = true, features = ["full"] } +futures = { version = "0.3" } +rquickjs = { version = "0.6", features = ["macro", "futures"] } +tokio = { version = "1", features = ["full"] } diff --git a/libs/utils/Cargo.toml b/libs/utils/Cargo.toml index 33a875b..dfc3ae9 100644 --- a/libs/utils/Cargo.toml +++ b/libs/utils/Cargo.toml @@ -8,4 +8,4 @@ repository = "https://github.com/rquickjs/rquickjs-extra" readme = "README.md" [dependencies] -rquickjs = { workspace = true } +rquickjs = { version = "0.6", features = ["array-buffer"] } diff --git a/libs/utils/src/lib.rs b/libs/utils/src/lib.rs index 76f2311..7b16623 100644 --- a/libs/utils/src/lib.rs +++ b/libs/utils/src/lib.rs @@ -1,3 +1,4 @@ pub mod ffi; pub mod module; pub mod result; +pub mod sysinfo; diff --git a/libs/utils/src/sysinfo.rs b/libs/utils/src/sysinfo.rs new file mode 100644 index 0000000..e2fce94 --- /dev/null +++ b/libs/utils/src/sysinfo.rs @@ -0,0 +1,22 @@ +use std::env; + +pub fn get_platform() -> &'static str { + let platform = env::consts::OS; + match platform { + "macos" => "darwin", + "windows" => "win32", + _ => platform, + } +} + +pub fn get_arch() -> &'static str { + let arch = env::consts::ARCH; + + match arch { + "x86_64" | "x86" => return "x64", + "aarch64" => return "arm64", + _ => (), + } + + arch +} diff --git a/modules/console/Cargo.toml b/modules/console/Cargo.toml index 3374e73..d672217 100644 --- a/modules/console/Cargo.toml +++ b/modules/console/Cargo.toml @@ -8,8 +8,8 @@ repository = "https://github.com/rquickjs/rquickjs-extra" readme = "README.md" [dependencies] -log = { workspace = true } -rquickjs = { workspace = true } +log = { version = "0.4" } +rquickjs = { version = "0.6", features = ["macro"] } [dev-dependencies] rquickjs-extra-test = { path = "../../libs/test" } diff --git a/modules/os/Cargo.toml b/modules/os/Cargo.toml index 9360a91..d3281ac 100644 --- a/modules/os/Cargo.toml +++ b/modules/os/Cargo.toml @@ -8,7 +8,20 @@ repository = "https://github.com/rquickjs/rquickjs-extra" readme = "README.md" [dependencies] +num_cpus = "1" +once_cell = "1" +rquickjs = { version = "0.6", features = ["macro"] } +rquickjs-extra-utils = { path = "../../libs/utils" } + +[target.'cfg(unix)'.dependencies] +libc = "0.2" + +[target.'cfg(windows)'.dependencies] +windows-registry = "0.2" +windows-result = "0.2" +windows-version = "0.1" [dev-dependencies] -futures = { version = "0.3" } +rquickjs = { version = "0.6", features = ["futures"] } +rquickjs-extra-test = { path = "../../libs/test" } tokio = { version = "1", features = ["full"] } diff --git a/modules/os/src/lib.rs b/modules/os/src/lib.rs index e69de29..5b4961b 100644 --- a/modules/os/src/lib.rs +++ b/modules/os/src/lib.rs @@ -0,0 +1,254 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +use std::env; + +use rquickjs::{ + module::{Declarations, Exports, ModuleDef}, + prelude::Func, + Ctx, Result, +}; +use rquickjs_extra_utils::{ + module::export_default, + sysinfo::{get_arch, get_platform}, +}; + +#[cfg(unix)] +use self::unix::{get_release, get_type, get_version, EOL}; +#[cfg(windows)] +use self::windows::{get_release, get_type, get_version, EOL}; + +#[cfg(unix)] +mod unix; +#[cfg(windows)] +mod windows; + +fn get_tmp_dir() -> String { + env::temp_dir().to_string_lossy().to_string() +} + +fn get_available_parallelism() -> usize { + num_cpus::get() +} + +pub struct OsModule; + +impl ModuleDef for OsModule { + fn declare(declare: &Declarations) -> Result<()> { + declare.declare("type")?; + declare.declare("release")?; + declare.declare("tmpdir")?; + declare.declare("platform")?; + declare.declare("version")?; + declare.declare("EOL")?; + declare.declare("availableParallelism")?; + declare.declare("arch")?; + + declare.declare("default")?; + + Ok(()) + } + + fn evaluate<'js>(ctx: &Ctx<'js>, exports: &Exports<'js>) -> Result<()> { + export_default(ctx, exports, |default| { + default.set("type", Func::from(get_type))?; + default.set("release", Func::from(get_release))?; + default.set("tmpdir", Func::from(get_tmp_dir))?; + default.set("platform", Func::from(get_platform))?; + default.set("version", Func::from(get_version))?; + default.set("EOL", EOL)?; + default.set( + "availableParallelism", + Func::from(get_available_parallelism), + )?; + default.set("arch", Func::from(get_arch))?; + + Ok(()) + }) + } +} + +#[cfg(test)] +mod tests { + use rquickjs_extra_test::{call_test, test_async_with, ModuleEvaluator}; + + use super::*; + + #[tokio::test] + async fn test_type() { + test_async_with(|ctx| { + Box::pin(async move { + ModuleEvaluator::eval_rust::(ctx.clone(), "os") + .await + .unwrap(); + + let module = ModuleEvaluator::eval_js( + ctx.clone(), + "test", + r#" + import { type } from 'os'; + + export async function test() { + return type() + } + "#, + ) + .await + .unwrap(); + + let result = call_test::(&ctx, &module, ()).await; + + assert!(result == "Linux" || result == "Windows_NT" || result == "Darwin"); + }) + }) + .await; + } + + #[tokio::test] + async fn test_release() { + test_async_with(|ctx| { + Box::pin(async move { + ModuleEvaluator::eval_rust::(ctx.clone(), "os") + .await + .unwrap(); + + let module = ModuleEvaluator::eval_js( + ctx.clone(), + "test", + r#" + import { release } from 'os'; + + export async function test() { + return release() + } + "#, + ) + .await + .unwrap(); + + let result = call_test::(&ctx, &module, ()).await; + + assert!(!result.is_empty()); // Format is platform dependant + }) + }) + .await; + } + + #[tokio::test] + async fn test_version() { + test_async_with(|ctx| { + Box::pin(async move { + ModuleEvaluator::eval_rust::(ctx.clone(), "os") + .await + .unwrap(); + + let module = ModuleEvaluator::eval_js( + ctx.clone(), + "test", + r#" + import { version } from 'os'; + + export async function test() { + return version() + } + "#, + ) + .await + .unwrap(); + + let result = call_test::(&ctx, &module, ()).await; + + assert!(!result.is_empty()); // Format is platform dependant + }) + }) + .await; + } + + #[tokio::test] + async fn test_available_parallelism() { + test_async_with(|ctx| { + Box::pin(async move { + ModuleEvaluator::eval_rust::(ctx.clone(), "os") + .await + .unwrap(); + + let module = ModuleEvaluator::eval_js( + ctx.clone(), + "test", + r#" + import { availableParallelism } from 'os'; + + export async function test() { + return availableParallelism() + } + "#, + ) + .await + .unwrap(); + + let result = call_test::(&ctx, &module, ()).await; + + assert!(result > 0); + }) + }) + .await; + } + + #[tokio::test] + async fn test_eol() { + test_async_with(|ctx| { + Box::pin(async move { + ModuleEvaluator::eval_rust::(ctx.clone(), "os") + .await + .unwrap(); + + let module = ModuleEvaluator::eval_js( + ctx.clone(), + "test", + r#" + import { EOL } from 'os'; + + export async function test() { + return EOL + } + "#, + ) + .await + .unwrap(); + + let result = call_test::(&ctx, &module, ()).await; + assert!(result == EOL); + }) + }) + .await; + } + + #[tokio::test] + async fn test_arch() { + test_async_with(|ctx| { + Box::pin(async move { + ModuleEvaluator::eval_rust::(ctx.clone(), "os") + .await + .unwrap(); + + let module = ModuleEvaluator::eval_js( + ctx.clone(), + "test", + r#" + import { arch } from 'os'; + + export async function test() { + return arch() + } + "#, + ) + .await + .unwrap(); + + let result = call_test::(&ctx, &module, ()).await; + + assert!(!result.is_empty()); // Format is platform dependant + }) + }) + .await; + } +} diff --git a/modules/os/src/unix.rs b/modules/os/src/unix.rs new file mode 100644 index 0000000..70884dd --- /dev/null +++ b/modules/os/src/unix.rs @@ -0,0 +1,49 @@ +use std::ffi::CStr; + +use once_cell::sync::Lazy; + +static OS_INFO: Lazy<(String, String, String)> = Lazy::new(uname); +pub static EOL: &str = "\n"; + +pub fn get_type() -> &'static str { + &OS_INFO.0 +} + +pub fn get_release() -> &'static str { + &OS_INFO.1 +} + +pub fn get_version() -> &'static str { + &OS_INFO.2 +} + +fn uname() -> (String, String, String) { + let mut info = std::mem::MaybeUninit::uninit(); + // SAFETY: `info` is a valid pointer to a `libc::utsname` struct. + let res = unsafe { libc::uname(info.as_mut_ptr()) }; + if res != 0 { + return (String::new(), String::new(), String::new()); + } + // SAFETY: `uname` returns 0 on success and info is initialized. + let info = unsafe { info.assume_init() }; + ( + // SAFETY: `info.sysname` is a valid NUL-terminated pointer. + unsafe { + CStr::from_ptr(info.sysname.as_ptr()) + .to_string_lossy() + .into_owned() + }, + // SAFETY: `info.release` is a valid NUL-terminated pointer. + unsafe { + CStr::from_ptr(info.release.as_ptr()) + .to_string_lossy() + .into_owned() + }, + // SAFETY: `info.version` is a valid NUL-terminated pointer. + unsafe { + CStr::from_ptr(info.version.as_ptr()) + .to_string_lossy() + .into_owned() + }, + ) +} diff --git a/modules/os/src/windows.rs b/modules/os/src/windows.rs new file mode 100644 index 0000000..e69de29 diff --git a/modules/sqlite/Cargo.toml b/modules/sqlite/Cargo.toml index a0d01e0..7a5717a 100644 --- a/modules/sqlite/Cargo.toml +++ b/modules/sqlite/Cargo.toml @@ -8,11 +8,19 @@ repository = "https://github.com/rquickjs/rquickjs-extra" readme = "README.md" [dependencies] -either = { workspace = true } -rquickjs = { workspace = true } +either = { version = "1" } +rquickjs = { version = "0.6", features = [ + "array-buffer", + "either", + "macro", + "futures", +] } rquickjs-extra-utils = { path = "../../libs/utils" } -sqlx = { workspace = true } +sqlx = { version = "0.8.2", default-features = false, features = [ + "sqlite", + "runtime-tokio", +] } [dev-dependencies] rquickjs-extra-test = { path = "../../libs/test" } -tokio = { workspace = true, features = ["full"] } +tokio = { version = "1", features = ["full"] } diff --git a/modules/timers/Cargo.toml b/modules/timers/Cargo.toml index 0994f29..dea579c 100644 --- a/modules/timers/Cargo.toml +++ b/modules/timers/Cargo.toml @@ -8,11 +8,11 @@ repository = "https://github.com/rquickjs/rquickjs-extra" readme = "README.md" [dependencies] -log = { workspace = true } -rquickjs = { workspace = true } -tokio = { workspace = true, features = ["time"] } +log = { version = "0.4" } +rquickjs = { version = "0.6", features = ["macro", "futures"] } +tokio = { version = "1", features = ["time"] } [dev-dependencies] -futures = { workspace = true } +futures = { version = "0.3" } rquickjs-extra-test = { path = "../../libs/test" } -tokio = { workspace = true, features = ["full"] } +tokio = { version = "1", features = ["full"] } diff --git a/modules/url/Cargo.toml b/modules/url/Cargo.toml index 06a7056..c373a2b 100644 --- a/modules/url/Cargo.toml +++ b/modules/url/Cargo.toml @@ -8,8 +8,8 @@ repository = "https://github.com/rquickjs/rquickjs-extra" readme = "README.md" [dependencies] -either = { workspace = true } -rquickjs = { workspace = true } +either = { version = "1" } +rquickjs = { version = "0.6", features = ["either", "macro"] } [dev-dependencies] rquickjs-extra-test = { path = "../../libs/test" }