Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

os: add EOL #578

Merged
merged 2 commits into from
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions llrt_modules/src/modules/os/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use rquickjs::{
};

#[cfg(unix)]
use self::unix::{get_release, get_type, get_version};
use self::unix::{get_release, get_type, get_version, EOL};
#[cfg(windows)]
use self::windows::{get_release, get_type, get_version};
use self::windows::{get_release, get_type, get_version, EOL};
use crate::module_info::ModuleInfo;
use crate::sysinfo::get_platform;

Expand All @@ -34,6 +34,7 @@ impl ModuleDef for OsModule {
declare.declare("tmpdir")?;
declare.declare("platform")?;
declare.declare("version")?;
declare.declare("EOL")?;

declare.declare("default")?;

Expand All @@ -47,6 +48,7 @@ impl ModuleDef for OsModule {
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)?;

Ok(())
})
Expand Down Expand Up @@ -156,4 +158,33 @@ mod tests {
})
.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;
}
}
1 change: 1 addition & 0 deletions llrt_modules/src/modules/os/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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
Expand Down
1 change: 1 addition & 0 deletions llrt_modules/src/modules/os/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use windows_version::OsVersion;

static OS_RELEASE: Lazy<String> = Lazy::new(release);
static OS_VERSION: Lazy<String> = Lazy::new(|| version().unwrap_or_default());
pub static EOL: &str = "\r\n";

pub fn get_type() -> &'static str {
// In theory there are more types linx MinGW but in practice this is good enough
Expand Down
7 changes: 7 additions & 0 deletions types/os.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,11 @@ declare module "os" {
* string.
*/
export function tmpdir(): string;

/**
* The operating system-specific end-of-line marker.
* * `\n` on POSIX
* * `\r\n` on Windows
*/
const EOL: string;
}
Loading