X Tutup
Skip to content
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
7 changes: 7 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@ jobs:
- name: Check compilation for freeBSD
run: cargo check --target x86_64-unknown-freebsd ${{ env.CARGO_ARGS_NO_SSL }}

- uses: dtolnay/rust-toolchain@stable
with:
target: wasm32-wasip2

- name: Check compilation for wasip2
run: cargo check --target wasm32-wasip2 ${{ env.CARGO_ARGS_NO_SSL }}

# - name: Prepare repository for redox compilation
# run: bash scripts/redox/uncomment-cargo.sh
# - name: Check compilation for Redox
Expand Down
2 changes: 0 additions & 2 deletions crates/common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! A crate to hold types and functions common to all rustpython components.

#![cfg_attr(all(target_os = "wasi", target_env = "p2"), feature(wasip2))]

extern crate alloc;

#[macro_use]
Expand Down
54 changes: 53 additions & 1 deletion crates/common/src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,61 @@ pub fn bytes_as_os_str(b: &[u8]) -> Result<&std::ffi::OsStr, Utf8Error> {

#[cfg(unix)]
pub use std::os::unix::ffi;
#[cfg(target_os = "wasi")]

// WASIp1 uses stable std::os::wasi::ffi
#[cfg(all(target_os = "wasi", not(target_env = "p2")))]
pub use std::os::wasi::ffi;

// WASIp2: std::os::wasip2::ffi is unstable, so we provide a stable implementation
// leveraging WASI's UTF-8 string guarantee
#[cfg(all(target_os = "wasi", target_env = "p2"))]
pub mod ffi {
use std::ffi::{OsStr, OsString};

pub trait OsStrExt: sealed::Sealed {
fn as_bytes(&self) -> &[u8];
fn from_bytes(slice: &[u8]) -> &Self;
}

impl OsStrExt for OsStr {
fn as_bytes(&self) -> &[u8] {
// WASI strings are guaranteed to be UTF-8
self.to_str().expect("wasip2 strings are UTF-8").as_bytes()
}

fn from_bytes(slice: &[u8]) -> &OsStr {
// WASI strings are guaranteed to be UTF-8
OsStr::new(std::str::from_utf8(slice).expect("wasip2 strings are UTF-8"))
}
}

pub trait OsStringExt: sealed::Sealed {
fn from_vec(vec: Vec<u8>) -> Self;
fn into_vec(self) -> Vec<u8>;
}

impl OsStringExt for OsString {
fn from_vec(vec: Vec<u8>) -> OsString {
// WASI strings are guaranteed to be UTF-8
OsString::from(String::from_utf8(vec).expect("wasip2 strings are UTF-8"))
}

fn into_vec(self) -> Vec<u8> {
// WASI strings are guaranteed to be UTF-8
self.to_str()
.expect("wasip2 strings are UTF-8")
.as_bytes()
.to_vec()
}
}

mod sealed {
pub trait Sealed {}
impl Sealed for std::ffi::OsStr {}
impl Sealed for std::ffi::OsString {}
}
}

#[cfg(windows)]
pub fn errno_to_winerror(errno: i32) -> i32 {
use libc::*;
Expand Down
1 change: 0 additions & 1 deletion crates/stdlib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// how `mod` works, but we want this sometimes for pymodule declarations

#![allow(clippy::module_inception)]
#![cfg_attr(all(target_os = "wasi", target_env = "p2"), feature(wasip2))]

#[macro_use]
extern crate rustpython_derive;
Expand Down
2 changes: 1 addition & 1 deletion crates/stdlib/src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ mod platform {
#[cfg(target_os = "wasi")]
mod platform {
pub use libc::{FD_SETSIZE, timeval};
pub use std::os::wasi::io::RawFd;
pub use std::os::fd::RawFd;

pub fn check_err(x: i32) -> bool {
x < 0
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
//!
//! See [`rustpython_derive`](../rustpython_derive/index.html) crate for documentation on macros used in the example above.

#![cfg_attr(all(target_os = "wasi", target_env = "p2"), feature(wasip2))]
#![allow(clippy::needless_doctest_main)]

#[macro_use]
Expand Down
10 changes: 6 additions & 4 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,8 +407,10 @@ pub(crate) use env::split_paths;
pub(crate) fn split_paths<T: AsRef<std::ffi::OsStr> + ?Sized>(
s: &T,
) -> impl Iterator<Item = std::path::PathBuf> + '_ {
use std::os::wasi::ffi::OsStrExt;
let s = s.as_ref().as_bytes();
s.split(|b| *b == b':')
.map(|x| std::ffi::OsStr::from_bytes(x).to_owned().into())
let s = s.as_ref().as_encoded_bytes();
s.split(|b| *b == b':').map(|x| {
unsafe { std::ffi::OsStr::from_encoded_bytes_unchecked(x) }
.to_owned()
.into()
})
}
Loading
X Tutup