Skip to content

Commit 61bf7c8

Browse files
committed
Remove workspace versions and add os
1 parent 715881c commit 61bf7c8

File tree

13 files changed

+365
-34
lines changed

13 files changed

+365
-34
lines changed

Cargo.toml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,3 @@ rquickjs-extra-url = { path = "modules/url", optional = true }
2525
[workspace]
2626
resolver = "2"
2727
members = ["modules/*", "libs/*"]
28-
29-
[workspace.dependencies]
30-
either = "1"
31-
futures = { version = "0.3" }
32-
log = { version = "0.4" }
33-
rquickjs = { version = "0.6", features = [
34-
"array-buffer",
35-
"either",
36-
"macro",
37-
"futures",
38-
] }
39-
sqlx = { version = "0.8.2", default-features = false, features = [
40-
"sqlite",
41-
"runtime-tokio",
42-
] }
43-
tokio = { version = "1" }

libs/test/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ repository = "https://github.com/rquickjs/rquickjs-extra"
88
readme = "README.md"
99

1010
[dependencies]
11-
futures = { workspace = true }
12-
rquickjs = { workspace = true }
13-
tokio = { workspace = true, features = ["full"] }
11+
futures = { version = "0.3" }
12+
rquickjs = { version = "0.6", features = ["macro", "futures"] }
13+
tokio = { version = "1", features = ["full"] }

libs/utils/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ repository = "https://github.com/rquickjs/rquickjs-extra"
88
readme = "README.md"
99

1010
[dependencies]
11-
rquickjs = { workspace = true }
11+
rquickjs = { version = "0.6", features = ["array-buffer"] }

libs/utils/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
pub mod ffi;
22
pub mod module;
33
pub mod result;
4+
pub mod sysinfo;

libs/utils/src/sysinfo.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use std::env;
2+
3+
pub fn get_platform() -> &'static str {
4+
let platform = env::consts::OS;
5+
match platform {
6+
"macos" => "darwin",
7+
"windows" => "win32",
8+
_ => platform,
9+
}
10+
}
11+
12+
pub fn get_arch() -> &'static str {
13+
let arch = env::consts::ARCH;
14+
15+
match arch {
16+
"x86_64" | "x86" => return "x64",
17+
"aarch64" => return "arm64",
18+
_ => (),
19+
}
20+
21+
arch
22+
}

modules/console/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ repository = "https://github.com/rquickjs/rquickjs-extra"
88
readme = "README.md"
99

1010
[dependencies]
11-
log = { workspace = true }
12-
rquickjs = { workspace = true }
11+
log = { version = "0.4" }
12+
rquickjs = { version = "0.6", features = ["macro"] }
1313

1414
[dev-dependencies]
1515
rquickjs-extra-test = { path = "../../libs/test" }

modules/os/Cargo.toml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,20 @@ repository = "https://github.com/rquickjs/rquickjs-extra"
88
readme = "README.md"
99

1010
[dependencies]
11+
num_cpus = "1"
12+
once_cell = "1"
13+
rquickjs = { version = "0.6", features = ["macro"] }
14+
rquickjs-extra-utils = { path = "../../libs/utils" }
15+
16+
[target.'cfg(unix)'.dependencies]
17+
libc = "0.2"
18+
19+
[target.'cfg(windows)'.dependencies]
20+
windows-registry = "0.2"
21+
windows-result = "0.2"
22+
windows-version = "0.1"
1123

1224
[dev-dependencies]
13-
futures = { version = "0.3" }
25+
rquickjs = { version = "0.6", features = ["futures"] }
26+
rquickjs-extra-test = { path = "../../libs/test" }
1427
tokio = { version = "1", features = ["full"] }

modules/os/src/lib.rs

Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
1+
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
use std::env;
4+
5+
use rquickjs::{
6+
module::{Declarations, Exports, ModuleDef},
7+
prelude::Func,
8+
Ctx, Result,
9+
};
10+
use rquickjs_extra_utils::{
11+
module::export_default,
12+
sysinfo::{get_arch, get_platform},
13+
};
14+
15+
#[cfg(unix)]
16+
use self::unix::{get_release, get_type, get_version, EOL};
17+
#[cfg(windows)]
18+
use self::windows::{get_release, get_type, get_version, EOL};
19+
20+
#[cfg(unix)]
21+
mod unix;
22+
#[cfg(windows)]
23+
mod windows;
24+
25+
fn get_tmp_dir() -> String {
26+
env::temp_dir().to_string_lossy().to_string()
27+
}
28+
29+
fn get_available_parallelism() -> usize {
30+
num_cpus::get()
31+
}
32+
33+
pub struct OsModule;
34+
35+
impl ModuleDef for OsModule {
36+
fn declare(declare: &Declarations) -> Result<()> {
37+
declare.declare("type")?;
38+
declare.declare("release")?;
39+
declare.declare("tmpdir")?;
40+
declare.declare("platform")?;
41+
declare.declare("version")?;
42+
declare.declare("EOL")?;
43+
declare.declare("availableParallelism")?;
44+
declare.declare("arch")?;
45+
46+
declare.declare("default")?;
47+
48+
Ok(())
49+
}
50+
51+
fn evaluate<'js>(ctx: &Ctx<'js>, exports: &Exports<'js>) -> Result<()> {
52+
export_default(ctx, exports, |default| {
53+
default.set("type", Func::from(get_type))?;
54+
default.set("release", Func::from(get_release))?;
55+
default.set("tmpdir", Func::from(get_tmp_dir))?;
56+
default.set("platform", Func::from(get_platform))?;
57+
default.set("version", Func::from(get_version))?;
58+
default.set("EOL", EOL)?;
59+
default.set(
60+
"availableParallelism",
61+
Func::from(get_available_parallelism),
62+
)?;
63+
default.set("arch", Func::from(get_arch))?;
64+
65+
Ok(())
66+
})
67+
}
68+
}
69+
70+
#[cfg(test)]
71+
mod tests {
72+
use rquickjs_extra_test::{call_test, test_async_with, ModuleEvaluator};
73+
74+
use super::*;
75+
76+
#[tokio::test]
77+
async fn test_type() {
78+
test_async_with(|ctx| {
79+
Box::pin(async move {
80+
ModuleEvaluator::eval_rust::<OsModule>(ctx.clone(), "os")
81+
.await
82+
.unwrap();
83+
84+
let module = ModuleEvaluator::eval_js(
85+
ctx.clone(),
86+
"test",
87+
r#"
88+
import { type } from 'os';
89+
90+
export async function test() {
91+
return type()
92+
}
93+
"#,
94+
)
95+
.await
96+
.unwrap();
97+
98+
let result = call_test::<String, _>(&ctx, &module, ()).await;
99+
100+
assert!(result == "Linux" || result == "Windows_NT" || result == "Darwin");
101+
})
102+
})
103+
.await;
104+
}
105+
106+
#[tokio::test]
107+
async fn test_release() {
108+
test_async_with(|ctx| {
109+
Box::pin(async move {
110+
ModuleEvaluator::eval_rust::<OsModule>(ctx.clone(), "os")
111+
.await
112+
.unwrap();
113+
114+
let module = ModuleEvaluator::eval_js(
115+
ctx.clone(),
116+
"test",
117+
r#"
118+
import { release } from 'os';
119+
120+
export async function test() {
121+
return release()
122+
}
123+
"#,
124+
)
125+
.await
126+
.unwrap();
127+
128+
let result = call_test::<String, _>(&ctx, &module, ()).await;
129+
130+
assert!(!result.is_empty()); // Format is platform dependant
131+
})
132+
})
133+
.await;
134+
}
135+
136+
#[tokio::test]
137+
async fn test_version() {
138+
test_async_with(|ctx| {
139+
Box::pin(async move {
140+
ModuleEvaluator::eval_rust::<OsModule>(ctx.clone(), "os")
141+
.await
142+
.unwrap();
143+
144+
let module = ModuleEvaluator::eval_js(
145+
ctx.clone(),
146+
"test",
147+
r#"
148+
import { version } from 'os';
149+
150+
export async function test() {
151+
return version()
152+
}
153+
"#,
154+
)
155+
.await
156+
.unwrap();
157+
158+
let result = call_test::<String, _>(&ctx, &module, ()).await;
159+
160+
assert!(!result.is_empty()); // Format is platform dependant
161+
})
162+
})
163+
.await;
164+
}
165+
166+
#[tokio::test]
167+
async fn test_available_parallelism() {
168+
test_async_with(|ctx| {
169+
Box::pin(async move {
170+
ModuleEvaluator::eval_rust::<OsModule>(ctx.clone(), "os")
171+
.await
172+
.unwrap();
173+
174+
let module = ModuleEvaluator::eval_js(
175+
ctx.clone(),
176+
"test",
177+
r#"
178+
import { availableParallelism } from 'os';
179+
180+
export async function test() {
181+
return availableParallelism()
182+
}
183+
"#,
184+
)
185+
.await
186+
.unwrap();
187+
188+
let result = call_test::<usize, _>(&ctx, &module, ()).await;
189+
190+
assert!(result > 0);
191+
})
192+
})
193+
.await;
194+
}
195+
196+
#[tokio::test]
197+
async fn test_eol() {
198+
test_async_with(|ctx| {
199+
Box::pin(async move {
200+
ModuleEvaluator::eval_rust::<OsModule>(ctx.clone(), "os")
201+
.await
202+
.unwrap();
203+
204+
let module = ModuleEvaluator::eval_js(
205+
ctx.clone(),
206+
"test",
207+
r#"
208+
import { EOL } from 'os';
209+
210+
export async function test() {
211+
return EOL
212+
}
213+
"#,
214+
)
215+
.await
216+
.unwrap();
217+
218+
let result = call_test::<String, _>(&ctx, &module, ()).await;
219+
assert!(result == EOL);
220+
})
221+
})
222+
.await;
223+
}
224+
225+
#[tokio::test]
226+
async fn test_arch() {
227+
test_async_with(|ctx| {
228+
Box::pin(async move {
229+
ModuleEvaluator::eval_rust::<OsModule>(ctx.clone(), "os")
230+
.await
231+
.unwrap();
232+
233+
let module = ModuleEvaluator::eval_js(
234+
ctx.clone(),
235+
"test",
236+
r#"
237+
import { arch } from 'os';
238+
239+
export async function test() {
240+
return arch()
241+
}
242+
"#,
243+
)
244+
.await
245+
.unwrap();
246+
247+
let result = call_test::<String, _>(&ctx, &module, ()).await;
248+
249+
assert!(!result.is_empty()); // Format is platform dependant
250+
})
251+
})
252+
.await;
253+
}
254+
}

0 commit comments

Comments
 (0)