Skip to content

Commit

Permalink
Remove workspace versions and add os
Browse files Browse the repository at this point in the history
  • Loading branch information
Sytten committed Nov 19, 2024
1 parent 715881c commit 61bf7c8
Show file tree
Hide file tree
Showing 13 changed files with 365 additions and 34 deletions.
16 changes: 0 additions & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
6 changes: 3 additions & 3 deletions libs/test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
2 changes: 1 addition & 1 deletion libs/utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
1 change: 1 addition & 0 deletions libs/utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod ffi;
pub mod module;
pub mod result;
pub mod sysinfo;
22 changes: 22 additions & 0 deletions libs/utils/src/sysinfo.rs
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 2 additions & 2 deletions modules/console/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
15 changes: 14 additions & 1 deletion modules/os/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
254 changes: 254 additions & 0 deletions modules/os/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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::<OsModule>(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::<String, _>(&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::<OsModule>(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::<String, _>(&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::<OsModule>(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::<String, _>(&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::<OsModule>(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::<usize, _>(&ctx, &module, ()).await;

assert!(result > 0);
})
})
.await;
}

#[tokio::test]
async fn test_eol() {
test_async_with(|ctx| {
Box::pin(async move {
ModuleEvaluator::eval_rust::<OsModule>(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::<String, _>(&ctx, &module, ()).await;
assert!(result == EOL);
})
})
.await;
}

#[tokio::test]
async fn test_arch() {
test_async_with(|ctx| {
Box::pin(async move {
ModuleEvaluator::eval_rust::<OsModule>(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::<String, _>(&ctx, &module, ()).await;

assert!(!result.is_empty()); // Format is platform dependant
})
})
.await;
}
}
Loading

0 comments on commit 61bf7c8

Please sign in to comment.