Skip to content

Commit

Permalink
Fix support for WebAssembly (#18)
Browse files Browse the repository at this point in the history
* Fix support for WebAssembly

The crate previously assumed that `wasm-bindgen` can be used for every
WebAssembly target. However there's essentially the following 4 targets:
 - WebAssembly on the web
 - Freestanding WebAssembly
 - WebAssembly with WASI
 - WebAssembly with Emscripten

Only `WebAssembly on the web` supports `wasm-bindgen`. Unfortunately
that's not a target on its own for historical reasons, so the only way
to properly support that target is through a `js` feature. All
other, unsupported, WebAssembly targets now fall back onto a new fallback
implementation that always returns `None`. The crate now also should be
able to build at all on unsupported targets because of that.
  • Loading branch information
CryZe authored Apr 4, 2023
1 parent 4cf74aa commit 67b2a5c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ Notable changes to this project will be documented in the [keep a changelog](htt

## [Unreleased]

### Changed
- The crate now only uses `wasm-bindgen` when targeting WebAssembly on the web.
Use the new `js` feature to target the web.

### Fixed
- The crate now compiles for unsupported platforms.

# [0.2.4] - 2023-03-07

### Changed
Expand Down
11 changes: 7 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ features = [
"Win32_System_SystemServices"
]

[target.'cfg(target_arch = "wasm32")'.dependencies]
js-sys = "0.3"
wasm-bindgen = { version = "0.2"}
web-sys = { version = "0.3", features = ["Window", "WorkerGlobalScope", "Navigator", "WorkerNavigator"] }
[target.'cfg(all(target_family = "wasm", not(unix)))'.dependencies]
js-sys = { version = "0.3", optional = true }
wasm-bindgen = { version = "0.2", optional = true }
web-sys = { version = "0.3", features = ["Window", "WorkerGlobalScope", "Navigator", "WorkerNavigator"], optional = true }

[features]
js = ["js-sys", "wasm-bindgen", "web-sys"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Platform support currently includes:
- iOS
- macOS
- Linux, BSD, and other UNIX variations
- WebAssembly
- WebAssembly on the web (via the `js` feature)
- Windows

```rust
Expand Down
17 changes: 12 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! - iOS
//! - macOS
//! - Linux, BSD, and other UNIX variations
//! - WebAssembly
//! - WebAssembly on the web (via the `js` feature)
//! - Windows
#![cfg_attr(
any(
Expand Down Expand Up @@ -40,16 +40,23 @@ mod unix;
))]
use unix as provider;

#[cfg(target_arch = "wasm32")]
#[cfg(all(target_family = "wasm", feature = "js", not(unix)))]
mod wasm;
#[cfg(target_arch = "wasm32")]
#[cfg(all(target_family = "wasm", feature = "js", not(unix)))]
use wasm as provider;

#[cfg(target_os = "windows")]
#[cfg(windows)]
mod windows;
#[cfg(target_os = "windows")]
#[cfg(windows)]
use windows as provider;

#[cfg(not(any(unix, all(target_family = "wasm", feature = "js", not(unix)), windows)))]
mod provider {
pub fn get() -> Option<alloc::string::String> {
None
}
}

/// Returns the active locale for the system or application.
///
/// # Returns
Expand Down

0 comments on commit 67b2a5c

Please sign in to comment.