Skip to content

Commit 7782d0b

Browse files
committed
[library/std pal::safaos]: update safa-api to 0.2.6, change how arguments are handled to use api implementition and add environment variables
1 parent 807013e commit 7782d0b

File tree

6 files changed

+67
-93
lines changed

6 files changed

+67
-93
lines changed

library/Cargo.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,9 @@ dependencies = [
315315

316316
[[package]]
317317
name = "safa-abi"
318-
version = "0.2.4"
318+
version = "0.2.5"
319319
source = "registry+https://github.com/rust-lang/crates.io-index"
320-
checksum = "3d9dc36026b330e211bedd31644d5f7cdbb04988de00b9e622cc1a5a228a8d27"
320+
checksum = "904cf96582683a0faecdaf07b75f1b12388f893722be8e3ffa81a4f671364bf1"
321321
dependencies = [
322322
"compiler_builtins",
323323
"rustc-std-workspace-alloc",
@@ -326,9 +326,9 @@ dependencies = [
326326

327327
[[package]]
328328
name = "safa-api"
329-
version = "0.2.5"
329+
version = "0.2.6"
330330
source = "registry+https://github.com/rust-lang/crates.io-index"
331-
checksum = "d039d8b6139419447bd9578f34edbcabdcfa150dc2f8a298f78696f101bfafb6"
331+
checksum = "4f38596091b6f623631d7a960e7753c94bd51e21c4541c53fdcceb4e1abb1b55"
332332
dependencies = [
333333
"compiler_builtins",
334334
"rustc-std-workspace-alloc",

library/std/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ hermit-abi = { version = "0.4.0", features = [
7777
], public = true }
7878

7979
[target.'cfg(target_os = "safaos")'.dependencies]
80-
safa-api = { version = "0.2.5", features = ['rustc-dep-of-std'], public = true }
80+
safa-api = { version = "0.2.6", features = ['rustc-dep-of-std'], public = true }
8181

8282
[target.'cfg(target_os = "wasi")'.dependencies]
8383
wasi = { version = "0.11.0", features = [

library/std/src/sys/pal/safaos/args.rs

Lines changed: 10 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,10 @@
1-
use core::cell::SyncUnsafeCell;
2-
use core::ptr::NonNull;
3-
41
use crate::ffi::OsString;
52
use crate::fmt;
6-
use crate::mem::MaybeUninit;
7-
8-
#[derive(Debug, Clone, Copy)]
9-
pub(super) struct RawArgs {
10-
pub(super) args: NonNull<[(NonNull<u8>, usize)]>,
11-
}
123

13-
impl RawArgs {
14-
fn len(&self) -> usize {
15-
unsafe { (*self.args.as_ptr()).len() }
16-
}
17-
18-
fn get(&self, index: usize) -> Option<&'static str> {
19-
unsafe {
20-
let args = &*self.args.as_ptr();
21-
let (ptr, len) = args.get(index)?;
22-
Some(core::str::from_utf8_unchecked(core::slice::from_raw_parts(ptr.as_ptr(), *len)))
23-
}
24-
}
25-
}
26-
27-
unsafe impl Sync for RawArgs {}
28-
29-
pub(super) static RAW_ARGS: SyncUnsafeCell<MaybeUninit<Option<RawArgs>>> =
30-
SyncUnsafeCell::new(MaybeUninit::uninit());
31-
pub struct Args {
32-
raw_args: Option<RawArgs>,
33-
index: usize,
34-
}
4+
pub struct Args(safa_api::process::ArgsIter);
355

366
pub fn args() -> Args {
37-
Args { raw_args: unsafe { (*RAW_ARGS.get()).assume_init() }, index: 0 }
7+
Args(safa_api::process::ArgsIter::get())
388
}
399

4010
impl fmt::Debug for Args {
@@ -43,8 +13,8 @@ impl fmt::Debug for Args {
4313
let len = self.len();
4414
for i in 0..len {
4515
// safe because index is always in len and raw_args is initialized (otherwise len is going to be 0)
46-
let arg = unsafe { self.raw_args.unwrap_unchecked().get(i).unwrap_unchecked() };
47-
list.entry(&arg);
16+
let arg = unsafe { self.0.get_index(i).unwrap_unchecked() };
17+
list.entry(&unsafe { arg.into_slice_mut() });
4818
}
4919
list.finish()
5020
}
@@ -53,30 +23,19 @@ impl fmt::Debug for Args {
5323
impl Iterator for Args {
5424
type Item = OsString;
5525
fn next(&mut self) -> Option<OsString> {
56-
if self.index >= self.len() {
57-
return None;
58-
}
59-
// it is gruanted that the raw_args is initialized if len > 0
60-
let raw_args = unsafe { self.raw_args.unwrap_unchecked() };
61-
let results = raw_args.get(self.index).map(|arg| OsString::from(arg));
62-
self.index += 1;
63-
results
26+
self.0.next().map(|arg| unsafe {
27+
OsString::from_encoded_bytes_unchecked(arg.into_slice_mut().to_vec())
28+
})
6429
}
65-
fn size_hint(&self) -> (usize, Option<usize>) {
66-
let len = self.len();
6730

68-
if len == 0 {
69-
(0, Some(0))
70-
} else {
71-
let left = len - self.index;
72-
(left, Some(left))
73-
}
31+
fn size_hint(&self) -> (usize, Option<usize>) {
32+
(self.0.len(), Some(self.0.len()))
7433
}
7534
}
7635

7736
impl ExactSizeIterator for Args {
7837
fn len(&self) -> usize {
79-
self.raw_args.map(|s| s.len()).unwrap_or(0)
38+
self.0.len()
8039
}
8140
}
8241

library/std/src/sys/pal/safaos/env.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
pub mod os {
2-
pub const FAMILY: &str = "";
3-
pub const OS: &str = "";
2+
pub const FAMILY: &str = "safa";
3+
pub const OS: &str = "SafaOS";
44
pub const DLL_PREFIX: &str = "";
55
pub const DLL_SUFFIX: &str = "";
66
pub const DLL_EXTENSION: &str = "";

library/std/src/sys/pal/safaos/os.rs

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -80,45 +80,61 @@ pub fn current_exe() -> io::Result<PathBuf> {
8080
unsupported()
8181
}
8282

83-
pub struct Env(!);
83+
pub struct Env {
84+
inner: Vec<(OsString, OsString)>,
85+
index: usize,
86+
}
8487

8588
impl Env {
8689
// FIXME(https://github.com/rust-lang/rust/issues/114583): Remove this when <OsStr as Debug>::fmt matches <str as Debug>::fmt.
8790
pub fn str_debug(&self) -> impl fmt::Debug + '_ {
88-
let Self(inner) = self;
89-
match *inner {}
91+
self
9092
}
9193
}
9294

9395
impl fmt::Debug for Env {
94-
fn fmt(&self, _: &mut fmt::Formatter<'_>) -> fmt::Result {
95-
let Self(inner) = self;
96-
match *inner {}
96+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
97+
f.debug_list().entries(self.inner.iter()).finish()
9798
}
9899
}
99100

100101
impl Iterator for Env {
101102
type Item = (OsString, OsString);
102103
fn next(&mut self) -> Option<(OsString, OsString)> {
103-
let Self(inner) = self;
104-
match *inner {}
104+
if self.index >= self.inner.len() {
105+
return None;
106+
}
107+
let (key, value) = self.inner[self.index].clone();
108+
self.index += 1;
109+
Some((key, value))
105110
}
106111
}
107112

108113
pub fn env() -> Env {
109-
panic!("not supported on this platform")
114+
let all = safa_api::process::env_get_all();
115+
let mut results = Vec::with_capacity(all.len());
116+
for (key, value) in all {
117+
results.push((unsafe { OsString::from_encoded_bytes_unchecked(key.to_vec()) }, unsafe {
118+
OsString::from_encoded_bytes_unchecked(value.to_bytes().to_vec())
119+
}));
120+
}
121+
122+
Env { inner: results, index: 0 }
110123
}
111124

112-
pub fn getenv(_: &OsStr) -> Option<OsString> {
113-
None
125+
pub fn getenv(key: &OsStr) -> Option<OsString> {
126+
safa_api::process::env_get(key.as_encoded_bytes())
127+
.map(|s| unsafe { OsString::from_encoded_bytes_unchecked(s.to_vec()) })
114128
}
115129

116-
pub unsafe fn setenv(_: &OsStr, _: &OsStr) -> io::Result<()> {
117-
Err(io::const_error!(io::ErrorKind::Unsupported, "cannot set env vars on this platform"))
130+
pub unsafe fn setenv(key: &OsStr, value: &OsStr) -> io::Result<()> {
131+
safa_api::process::env_set(key.as_encoded_bytes(), value.as_encoded_bytes());
132+
Ok(())
118133
}
119134

120-
pub unsafe fn unsetenv(_: &OsStr) -> io::Result<()> {
121-
Err(io::const_error!(io::ErrorKind::Unsupported, "cannot unset env vars on this platform"))
135+
pub unsafe fn unsetenv(key: &OsStr) -> io::Result<()> {
136+
safa_api::process::env_remove(key.as_encoded_bytes());
137+
Ok(())
122138
}
123139

124140
pub fn temp_dir() -> PathBuf {
Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,34 @@
1-
use core::ptr::NonNull;
2-
3-
use super::args::RAW_ARGS;
4-
use super::args::RawArgs;
5-
use super::os::exit;
6-
use crate::mem::MaybeUninit;
1+
use safa_api::raw::{NonNullSlice, RawSliceMut};
2+
use safa_api::syscalls::exit;
73

84
extern "C" {
95
fn main() -> u16;
106
}
117

12-
unsafe fn _start_inner(argc: usize, argv: *mut (NonNull<u8>, usize)) -> ! {
8+
unsafe fn _start_inner(
9+
argc: usize,
10+
argv: *mut NonNullSlice<u8>,
11+
envc: usize,
12+
envp: *mut NonNullSlice<u8>,
13+
) -> ! {
1314
unsafe {
14-
let raw_args = match argc == 0 || argv.is_null() {
15-
true => None,
16-
false => {
17-
let args_slice = core::slice::from_raw_parts_mut(argv, argc);
18-
let args = NonNull::new_unchecked(args_slice as *mut _);
19-
Some(RawArgs { args })
20-
}
21-
};
22-
23-
RAW_ARGS.get().write(MaybeUninit::new(raw_args));
15+
let args = RawSliceMut::from_raw_parts(argv, argc);
16+
let env = RawSliceMut::from_raw_parts(envp, envc);
17+
safa_api::process::sysapi_init(args, env);
2418
let results = main();
2519

26-
exit(results as i32)
20+
exit(results as usize);
2721
}
2822
}
2923

3024
#[no_mangle]
3125
#[allow(unused)]
32-
pub extern "C" fn _start(argc: usize, argv: *mut (NonNull<u8>, usize)) {
26+
pub extern "C" fn _start(
27+
argc: usize,
28+
argv: *mut NonNullSlice<u8>,
29+
envc: usize,
30+
envp: *mut NonNullSlice<u8>,
31+
) {
3332
unsafe {
3433
core::arch::asm!(
3534
"
@@ -39,6 +38,6 @@ pub extern "C" fn _start(argc: usize, argv: *mut (NonNull<u8>, usize)) {
3938
",
4039
options(nostack)
4140
);
42-
_start_inner(argc, argv);
41+
_start_inner(argc, argv, envc, envp);
4342
};
4443
}

0 commit comments

Comments
 (0)