Skip to content

Commit 7516f26

Browse files
authored
Add WASI support for gloo-history. (#405)
* feat: Support WASI target. * fix: More friendly error message for wasi target. * fix: Use target_os instead of feature.
1 parent b868497 commit 7516f26

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

crates/history/src/utils.rs

+12-2
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ use std::cell::RefCell;
22
use std::rc::{Rc, Weak};
33
use std::sync::atomic::{AtomicU32, Ordering};
44

5+
#[cfg(not(target_os = "wasi"))]
56
use wasm_bindgen::throw_str;
67

7-
#[cfg(not(target_arch = "wasm32"))]
8+
#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))]
89
pub(crate) fn get_id() -> u32 {
910
static ID_CTR: AtomicU32 = AtomicU32::new(0);
1011

1112
ID_CTR.fetch_add(1, Ordering::SeqCst)
1213
}
1314

14-
#[cfg(target_arch = "wasm32")]
15+
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
1516
pub(crate) fn get_id() -> u32 {
1617
static ID_CTR: AtomicU32 = AtomicU32::new(0);
1718
static INIT: std::sync::Once = std::sync::Once::new();
@@ -30,19 +31,28 @@ pub(crate) fn get_id() -> u32 {
3031

3132
pub(crate) fn assert_absolute_path(path: &str) {
3233
if !path.starts_with('/') {
34+
#[cfg(not(target_os = "wasi"))]
3335
throw_str("You cannot use relative path with this history type.");
36+
#[cfg(target_os = "wasi")]
37+
panic!("You cannot use relative path with this history type.");
3438
}
3539
}
3640

3741
pub(crate) fn assert_no_query(path: &str) {
3842
if path.contains('?') {
43+
#[cfg(not(target_os = "wasi"))]
3944
throw_str("You cannot have query in path, try use a variant of this method with `_query`.");
45+
#[cfg(target_os = "wasi")]
46+
panic!("You cannot have query in path, try use a variant of this method with `_query`.");
4047
}
4148
}
4249

4350
pub(crate) fn assert_no_fragment(path: &str) {
4451
if path.contains('#') {
52+
#[cfg(not(target_os = "wasi"))]
4553
throw_str("You cannot use fragments (hash) in memory history.");
54+
#[cfg(target_os = "wasi")]
55+
panic!("You cannot use fragments (hash) in memory history.");
4656
}
4757
}
4858

crates/history/tests/query.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#![cfg(feature = "query")]
22

3-
#[cfg(target_arch = "wasm32")]
3+
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
44
use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure};
5-
#[cfg(target_arch = "wasm32")]
5+
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))]
66
wasm_bindgen_test_configure!(run_in_browser);
77

88
use gloo_history::query::*;

0 commit comments

Comments
 (0)