10000 Use wasi crate for Core API by newpavlov · Pull Request #63676 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

Use wasi crate for Core API #63676

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

Merged
merged 23 commits into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
use wasi::get_environ
  • Loading branch information
newpavlov committed Aug 19, 2019
commit 52d2871e10202279b7039f4ad6d4485093fb99cc
1 change: 0 additions & 1 deletion src/libstd/sys/wasi/args.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::ffi::CStr;
use crate::io;
use crate::sys::cvt_wasi;
use crate::ffi::OsString;
use crate::marker::PhantomData;
use crate::os::wasi::ffi::OsStringExt;
Expand Down
46 changes: 17 additions & 29 deletions src/libstd/sys/wasi/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,45 +75,33 @@ pub fn current_exe() -> io::Result<PathBuf> {
}

pub struct Env {
iter: vec::IntoIter<(OsString, OsString)>,
iter: Vec<Vec<u8>>,
_dont_send_or_sync_me: PhantomData<*mut ()>,
}

impl Iterator for Env {
type Item = (OsString, OsString);
fn next(&mut self) -> Option<(OsString, OsString)> { self.iter.next() }
fn next(&mut self) -> Option<(OsString, OsString)> {
self.iter.next().and_then(|input| {
// See src/libstd/sys/unix/os.rs, same as that
if input.is_empty() {
return None;
}
let pos = memchr::memchr(b'=', &input[1..]).map(|p| p + 1);
pos.map(|p| (
OsStringExt::from_vec(input[..p].to_vec()),
OsStringExt::from_vec(input[p+1..].to_vec()),
))
})
}
fn size_hint(&self) -> (usize, Option<usize>) { self.iter.size_hint() }
}


pub fn env() -> Env {
unsafe {
let _guard = env_lock();
// FIXME: replace with wasi::environ_get
let mut environ = libc::environ;
let mut result = Vec::new();
while environ != ptr::null_mut() && *environ != ptr::null_mut() {
if let Some(key_value) = parse(CStr::from_ptr(*environ).to_bytes()) {
result.push(key_value);
}
environ = environ.offset(1);
}
return Env {
iter: result.into_iter(),
_dont_send_or_sync_me: PhantomData,
}
}

// See src/libstd/sys/unix/os.rs, same as that
fn parse(input: &[u8]) -> Option<(OsString, OsString)> {
if input.is_empty() {
return None;
}
let pos = memchr::memchr(b'=', &input[1..]).map(|p| p + 1);
pos.map(|p| (
OsStringExt::from_vec(input[..p].to_vec()),
OsStringExt::from_vec(input[p+1..].to_vec()),
))
Env {
iter: wasi::get_environ().unwrap_or(Vec::new()),
_dont_send_or_sync_me: PhantomData,
}
}

Expand Down
0