From 775f3aae187fd189f04874afae491de318e81da4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Mon, 20 Nov 2023 12:09:38 +0800 Subject: [PATCH 1/3] feat: Support WASI target. --- Cargo.toml | 1 + crates/history/Cargo.toml | 1 + crates/history/src/utils.rs | 4 ++-- crates/history/tests/query.rs | 4 ++-- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c002e966..3dbc9b7b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,6 +58,7 @@ utils = ["gloo-utils"] history = ["gloo-history"] worker = ["gloo-worker"] net = ["gloo-net"] +wasi = ["gloo-history/wasi"] [workspace] members = [ diff --git a/crates/history/Cargo.toml b/crates/history/Cargo.toml index bb44afc1..b8876f8f 100644 --- a/crates/history/Cargo.toml +++ b/crates/history/Cargo.toml @@ -33,4 +33,5 @@ gloo-timers = { version = "0.3.0", features = ["futures"], path = "../timers" } [features] query = ["thiserror", "serde_urlencoded"] +wasi = [] default = ["query"] diff --git a/crates/history/src/utils.rs b/crates/history/src/utils.rs index 24422d48..bca2c8aa 100644 --- a/crates/history/src/utils.rs +++ b/crates/history/src/utils.rs @@ -4,14 +4,14 @@ use std::sync::atomic::{AtomicU32, Ordering}; use wasm_bindgen::throw_str; -#[cfg(not(target_arch = "wasm32"))] +#[cfg(any(not(target_arch = "wasm32"), feature = "wasi"))] pub(crate) fn get_id() -> u32 { static ID_CTR: AtomicU32 = AtomicU32::new(0); ID_CTR.fetch_add(1, Ordering::SeqCst) } -#[cfg(target_arch = "wasm32")] +#[cfg(all(target_arch = "wasm32", not(feature = "wasi")))] pub(crate) fn get_id() -> u32 { static ID_CTR: AtomicU32 = AtomicU32::new(0); static INIT: std::sync::Once = std::sync::Once::new(); diff --git a/crates/history/tests/query.rs b/crates/history/tests/query.rs index 28ffb75b..e9b7f218 100644 --- a/crates/history/tests/query.rs +++ b/crates/history/tests/query.rs @@ -1,8 +1,8 @@ #![cfg(feature = "query")] -#[cfg(target_arch = "wasm32")] +#[cfg(all(target_arch = "wasm32", not(feature = "wasi")))] use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure}; -#[cfg(target_arch = "wasm32")] +#[cfg(all(target_arch = "wasm32", not(feature = "wasi")))] wasm_bindgen_test_configure!(run_in_browser); use gloo_history::query::*; From 43893b531bde7104e53825283fe162d51dcbfd2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Mon, 20 Nov 2023 13:59:18 +0800 Subject: [PATCH 2/3] fix: More friendly error message for wasi target. --- crates/history/src/utils.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/crates/history/src/utils.rs b/crates/history/src/utils.rs index bca2c8aa..357c87ec 100644 --- a/crates/history/src/utils.rs +++ b/crates/history/src/utils.rs @@ -2,6 +2,7 @@ use std::cell::RefCell; use std::rc::{Rc, Weak}; use std::sync::atomic::{AtomicU32, Ordering}; +#[cfg(not(feature = "wasi"))] use wasm_bindgen::throw_str; #[cfg(any(not(target_arch = "wasm32"), feature = "wasi"))] @@ -30,19 +31,28 @@ pub(crate) fn get_id() -> u32 { pub(crate) fn assert_absolute_path(path: &str) { if !path.starts_with('/') { + #[cfg(not(feature = "wasi"))] throw_str("You cannot use relative path with this history type."); + #[cfg(feature = "wasi")] + panic!("You cannot use relative path with this history type."); } } pub(crate) fn assert_no_query(path: &str) { if path.contains('?') { + #[cfg(not(feature = "wasi"))] throw_str("You cannot have query in path, try use a variant of this method with `_query`."); + #[cfg(feature = "wasi")] + panic!("You cannot have query in path, try use a variant of this method with `_query`."); } } pub(crate) fn assert_no_fragment(path: &str) { if path.contains('#') { + #[cfg(not(feature = "wasi"))] throw_str("You cannot use fragments (hash) in memory history."); + #[cfg(feature = "wasi")] + panic!("You cannot use fragments (hash) in memory history."); } } From 9f6d39fe58f80bcd824d034f3fa99d258a56da0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Thu, 30 Nov 2023 21:04:11 +0800 Subject: [PATCH 3/3] fix: Use target_os instead of feature. --- Cargo.toml | 1 - crates/history/Cargo.toml | 1 - crates/history/src/utils.rs | 18 +++++++++--------- crates/history/tests/query.rs | 4 ++-- 4 files changed, 11 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 3dbc9b7b..c002e966 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -58,7 +58,6 @@ utils = ["gloo-utils"] history = ["gloo-history"] worker = ["gloo-worker"] net = ["gloo-net"] -wasi = ["gloo-history/wasi"] [workspace] members = [ diff --git a/crates/history/Cargo.toml b/crates/history/Cargo.toml index b8876f8f..bb44afc1 100644 --- a/crates/history/Cargo.toml +++ b/crates/history/Cargo.toml @@ -33,5 +33,4 @@ gloo-timers = { version = "0.3.0", features = ["futures"], path = "../timers" } [features] query = ["thiserror", "serde_urlencoded"] -wasi = [] default = ["query"] diff --git a/crates/history/src/utils.rs b/crates/history/src/utils.rs index 357c87ec..8aa4f346 100644 --- a/crates/history/src/utils.rs +++ b/crates/history/src/utils.rs @@ -2,17 +2,17 @@ use std::cell::RefCell; use std::rc::{Rc, Weak}; use std::sync::atomic::{AtomicU32, Ordering}; -#[cfg(not(feature = "wasi"))] +#[cfg(not(target_os = "wasi"))] use wasm_bindgen::throw_str; -#[cfg(any(not(target_arch = "wasm32"), feature = "wasi"))] +#[cfg(any(not(target_arch = "wasm32"), target_os = "wasi"))] pub(crate) fn get_id() -> u32 { static ID_CTR: AtomicU32 = AtomicU32::new(0); ID_CTR.fetch_add(1, Ordering::SeqCst) } -#[cfg(all(target_arch = "wasm32", not(feature = "wasi")))] +#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] pub(crate) fn get_id() -> u32 { static ID_CTR: AtomicU32 = AtomicU32::new(0); static INIT: std::sync::Once = std::sync::Once::new(); @@ -31,27 +31,27 @@ pub(crate) fn get_id() -> u32 { pub(crate) fn assert_absolute_path(path: &str) { if !path.starts_with('/') { - #[cfg(not(feature = "wasi"))] + #[cfg(not(target_os = "wasi"))] throw_str("You cannot use relative path with this history type."); - #[cfg(feature = "wasi")] + #[cfg(target_os = "wasi")] panic!("You cannot use relative path with this history type."); } } pub(crate) fn assert_no_query(path: &str) { if path.contains('?') { - #[cfg(not(feature = "wasi"))] + #[cfg(not(target_os = "wasi"))] throw_str("You cannot have query in path, try use a variant of this method with `_query`."); - #[cfg(feature = "wasi")] + #[cfg(target_os = "wasi")] panic!("You cannot have query in path, try use a variant of this method with `_query`."); } } pub(crate) fn assert_no_fragment(path: &str) { if path.contains('#') { - #[cfg(not(feature = "wasi"))] + #[cfg(not(target_os = "wasi"))] throw_str("You cannot use fragments (hash) in memory history."); - #[cfg(feature = "wasi")] + #[cfg(target_os = "wasi")] panic!("You cannot use fragments (hash) in memory history."); } } diff --git a/crates/history/tests/query.rs b/crates/history/tests/query.rs index e9b7f218..a7fc176e 100644 --- a/crates/history/tests/query.rs +++ b/crates/history/tests/query.rs @@ -1,8 +1,8 @@ #![cfg(feature = "query")] -#[cfg(all(target_arch = "wasm32", not(feature = "wasi")))] +#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] use wasm_bindgen_test::{wasm_bindgen_test as test, wasm_bindgen_test_configure}; -#[cfg(all(target_arch = "wasm32", not(feature = "wasi")))] +#[cfg(all(target_arch = "wasm32", not(target_os = "wasi")))] wasm_bindgen_test_configure!(run_in_browser); use gloo_history::query::*;