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

Support WASI #84

Merged
merged 1 commit into from
Feb 2, 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format is based on [Keep a Changelog], and this project adheres to

### Added

- WASI support
- Fallible functions
- `whoami::fallible::devicename()`
- `whoami::fallible::devicename_os()`
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ optional = true
version = "0.2"
optional = true

[target.'cfg(all(target_arch = "wasm32", target_os = "wasi"))'.dependencies.wasite]
version = "0.1"

[features]
default = ["web"]
# Enabling this feature indicates that the wasm32-unknown-unknown target should
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ WhoAmI targets all platforms that can run Rust, including:
- [Web Assembly](https://github.com/ardaku/whoami/blob/stable/WASM.md)
- Mock implementation
- Web Browser - DOM
- WASI (Wasite/Quantii, others) **mock implementation, full implementation planned later**
- WASI (Wasite, others) **may partially or fully work - but untested**
- Daku (Ardaku/Quantii, others) **mock implementation, full implementation planned later**
- Illumos variants (SmartOS, OmniOS, others) **may partially or fully work - but untested**
- Android **may partially or fully work - but untested, planned later**
Expand Down
19 changes: 8 additions & 11 deletions WASM.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ WhoAmI links to web-sys and defaults values to browser information:
- `platform()`: Host operating system by view of browser (Example: "Linux")
- `distro()`: Host distro by view of browser (Example "Unknown Linux")
- `desktop_env()`: "Web Browser"
- `arch()`: "wasm32"

## Mock
If you compile WhoAmI with `default-features = false`, WhoAmI will not bind to
Expand All @@ -26,22 +25,20 @@ web-sys, and will instead return these mock values:
- `platform()`: "Unknown"
- `distro()`: "Emulated"
- `desktop_env()`: "Unknown WebAssembly"
- `arch()`: "wasm32"

## Wasi (Wasite)
Building WhoAmI targeting Wasi will assume the
[wasite](https://ardaku.org/wasite/env_vars.html) environment variables are set,
as Wasi alone does not currently support the functionality WhoAmI requires.

- `realname()`: `$USER`
- `username()`: `$USER`
- `lang()`: `$LANGS`
- `devicename()`: `$NAME`
- `hostname()`: `$HOSTNAME`
- `platform()`: "Wasite"
- `distro()`: "Unknown wasi"
- `desktop_env()`: "Unknown wasite"
- `arch()`: "wasm32"
- `realname()`: `$USER` - Fallback "Anonymous"
- `username()`: `$USER` - Fallback "anonymous"
- `lang()`: `$LANGS` - Fallback "en-US"
- `devicename()`: `$NAME` - Fallback "Unknown"
- `hostname()`: `$HOSTNAME` - Fallback "localhost"
- `platform()`: "WASI"
- `distro()`: "Unknown WASI"
- `desktop_env()`: `Unknown($DESKTOP_SESSION)` - Fallback "Unknown WASI"

## Daku (Quantii, other Ardaku environments)
WhoAmi will depend on currently unstable portals in the
Expand Down
4 changes: 2 additions & 2 deletions src/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
all(target_arch = "wasm32", target_os = "daku"),
path = "os/fake.rs"
)]
// Wasm32 (Wasi) - FIXME: Currently routes to fake.rs
// Wasm32 (Wasi)
#[cfg_attr(
all(target_arch = "wasm32", target_os = "wasi"),
path = "os/fake.rs"
path = "os/wasi.rs"
)]
// Wasm32 (Web)
#[cfg_attr(
Expand Down
80 changes: 80 additions & 0 deletions src/os/wasi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
compile_error!("Unexpected pointer width for target platform");

use std::{env, ffi::OsString};

use crate::{
os::{Os, Target},
Arch, DesktopEnv, Language, Platform, Result,
};

#[inline(always)]
pub(crate) fn lang() -> impl Iterator<Item = String> {
let langs: Vec<String> = wasite::langs()
.unwrap_or_else(|_e| "en_US".to_string())
.split(';')
.map(|lang| lang.to_string())
.collect();

langs.into_iter()
}

impl Target for Os {
fn langs(self) -> Vec<Language> {
todo!()
}

#[inline(always)]
fn realname(self) -> Result<OsString> {
Ok(wasite::user()
.unwrap_or_else(|_e| "Anonymous".to_string())
.into())
}

#[inline(always)]
fn username(self) -> Result<OsString> {
Ok(wasite::user()
.unwrap_or_else(|_e| "anonymous".to_string())
.into())
}

#[inline(always)]
fn devicename(self) -> Result<OsString> {
Ok(wasite::name()
.unwrap_or_else(|_e| "Unknown".to_string())
.into())
}

#[inline(always)]
fn hostname(self) -> Result<String> {
Ok(wasite::hostname().unwrap_or_else(|_e| "localhost".to_string()))
}

#[inline(always)]
fn distro(self) -> Result<String> {
Ok("Unknown WASI".to_string())
}

#[inline(always)]
fn desktop_env(self) -> DesktopEnv {
if let Some(ref env) = env::var_os("DESKTOP_SESSION") {
DesktopEnv::Unknown(env.to_string_lossy().to_string())
} else {
DesktopEnv::Unknown("Unknown WASI".to_string())
}
}

#[inline(always)]
fn platform(self) -> Platform {
Platform::Unknown("WASI".to_string())
}

#[inline(always)]
fn arch(self) -> Result<Arch> {
Ok(if cfg!(target_pointer_width = "64") {
Arch::Wasm64
} else {
Arch::Wasm32
})
}
}
Loading