-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove workspace versions and add os
- Loading branch information
Showing
13 changed files
with
365 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.