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/8] 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/8] 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/8] 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::*; From 2676fe64fd84ea9297c3dd1bf7ec14c5484f17e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Fri, 1 Dec 2023 02:01:54 +0800 Subject: [PATCH 4/8] feat: Add an example for gloo-history on WASI. --- .github/workflows/tests.yml | 51 +++++++++++++++++++++++++------ Cargo.toml | 1 + examples/history-wasi/Cargo.toml | 10 ++++++ examples/history-wasi/README.md | 5 +++ examples/history-wasi/src/main.rs | 10 ++++++ 5 files changed, 68 insertions(+), 9 deletions(-) create mode 100644 examples/history-wasi/Cargo.toml create mode 100644 examples/history-wasi/README.md create mode 100644 examples/history-wasi/src/main.rs diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 406eec57..bc13aa7a 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -3,7 +3,7 @@ name: Tests on: pull_request: push: - branches: [ master ] + branches: [master] jobs: native_tests: @@ -119,6 +119,40 @@ jobs: wasm-pack test --node crates/$x --no-default-features done + test-history-wasi: + name: Test gloo-history WASI + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + example: [history-wasi] + rust-version: [1.64, stable, nightly] + steps: + - uses: actions/checkout@v4 + - uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ matrix.rust-version }} + target: wasm32-wasi + + - name: Install wasmtime + run: curl https://wasmtime.dev/install.sh -sSf | bash + + - uses: actions/cache@v3 + with: + path: | + ~/.cargo/registry + ~/.cargo/git + target + key: cargo-${{ runner.os }}-wasi-tests-${{ hashFiles('**/Cargo.toml') }} + restore-keys: | + cargo-${{ runner.os }}-wasi-tests- + cargo-${{ runner.os }}- + + - name: Build and run example history-wasi + run: | + cargo build -p example-${{ matrix.example }} --target wasm32-wasi + wasmtime target/wasm32-wasi/debug/example-${{ matrix.example }}.wasm + test-worker: name: Test gloo-worker runs-on: ubuntu-latest @@ -126,7 +160,7 @@ jobs: fail-fast: false matrix: # example: [ markdown, prime ] - example: [ markdown ] + example: [markdown] rust-version: [1.64, stable, nightly] steps: - uses: actions/checkout@v4 @@ -167,7 +201,6 @@ jobs: run: | wasm-pack test --headless --chrome --firefox examples/${{ matrix.example }} - test-net: strategy: fail-fast: false @@ -208,9 +241,9 @@ jobs: - name: Run browser tests env: - HTTPBIN_URL: "http://localhost:8080" - WS_ECHO_SERVER_URL: "ws://localhost:8081" - SSE_ECHO_SERVER_URL: "http://localhost:8081/.sse" + HTTPBIN_URL: 'http://localhost:8080' + WS_ECHO_SERVER_URL: 'ws://localhost:8081' + SSE_ECHO_SERVER_URL: 'http://localhost:8081/.sse' run: | cd crates/net wasm-pack test --chrome --firefox --headless --all-features @@ -222,7 +255,7 @@ jobs: - name: Run native tests env: - HTTPBIN_URL: "http://localhost:8080" - WS_ECHO_SERVER_URL: "ws://localhost:8081" - SSE_ECHO_SERVER_URL: "http://localhost:8081/.sse" + HTTPBIN_URL: 'http://localhost:8080' + WS_ECHO_SERVER_URL: 'ws://localhost:8081' + SSE_ECHO_SERVER_URL: 'http://localhost:8081/.sse' run: cargo test -p gloo-net --all-features diff --git a/Cargo.toml b/Cargo.toml index c002e966..0cb03fd0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -77,6 +77,7 @@ members = [ "examples/markdown", "examples/clock", "examples/file-hash", + "examples/history-wasi", "examples/prime", ] diff --git a/examples/history-wasi/Cargo.toml b/examples/history-wasi/Cargo.toml new file mode 100644 index 00000000..78522958 --- /dev/null +++ b/examples/history-wasi/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "example-history-wasi" +version = "0.1.0" +authors = ["Rust and WebAssembly Working Group"] +edition = "2021" +publish = false +rust-version = "1.64" + +[dependencies] +gloo = { path = "../..", default-features = false, features = ["history"] } diff --git a/examples/history-wasi/README.md b/examples/history-wasi/README.md new file mode 100644 index 00000000..8cf51e23 --- /dev/null +++ b/examples/history-wasi/README.md @@ -0,0 +1,5 @@ +# History example on WASI + +This is a simple example showcasing the Gloo History on WASI. + +You can run this example with `cargo wasi run --package example-history-wasi` diff --git a/examples/history-wasi/src/main.rs b/examples/history-wasi/src/main.rs new file mode 100644 index 00000000..fdeb8c41 --- /dev/null +++ b/examples/history-wasi/src/main.rs @@ -0,0 +1,10 @@ +use gloo::history::{History, MemoryHistory}; + +fn main() { + let history = MemoryHistory::new(); + history.push("/home"); + history.push("/about"); + history.push("/contact"); + history.go(-2); + assert_eq!(history.location().path(), "/home"); +} From a3fae2c00b530f2eb77aae2d4e21f93b878844f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Fri, 1 Dec 2023 03:01:55 +0800 Subject: [PATCH 5/8] fix: Avoid to use web-sys when target os is WASI. --- .github/workflows/tests.yml | 7 +++---- crates/history/Cargo.toml | 6 ++++-- crates/history/src/any.rs | 30 ++++++++++++++++++++++++++++++ crates/history/src/lib.rs | 4 ++++ crates/history/src/state.rs | 2 ++ 5 files changed, 43 insertions(+), 6 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index bc13aa7a..2d09f87f 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -143,15 +143,14 @@ jobs: ~/.cargo/registry ~/.cargo/git target - key: cargo-${{ runner.os }}-wasi-tests-${{ hashFiles('**/Cargo.toml') }} + key: cargo-${{ runner.os }}-history-wasi-tests-${{ hashFiles('**/Cargo.toml') }} restore-keys: | - cargo-${{ runner.os }}-wasi-tests- - cargo-${{ runner.os }}- + cargo-${{ runner.os }}-history-wasi-tests- - name: Build and run example history-wasi run: | cargo build -p example-${{ matrix.example }} --target wasm32-wasi - wasmtime target/wasm32-wasi/debug/example-${{ matrix.example }}.wasm + ~/.wasmtime/bin/wasmtime target/wasm32-wasi/debug/example-${{ matrix.example }}.wasm test-worker: name: Test gloo-worker diff --git a/crates/history/Cargo.toml b/crates/history/Cargo.toml index bb44afc1..b9b5ba90 100644 --- a/crates/history/Cargo.toml +++ b/crates/history/Cargo.toml @@ -12,7 +12,6 @@ categories = ["api-bindings", "history", "wasm"] rust-version = "1.64" [dependencies] -wasm-bindgen = "0.2" gloo-utils = { version = "0.2.0", path = "../utils" } gloo-events = { version = "0.2.0", path = "../events" } serde = { version = "1", features = ["derive"] } @@ -20,7 +19,10 @@ serde-wasm-bindgen = "0.6.0" serde_urlencoded = { version = "0.7", optional = true } thiserror = { version = "1.0", optional = true } -[dependencies.web-sys] +[target.'cfg(not(target_os = "wasi"))'.dependencies] +wasm-bindgen = "0.2" + +[target.'cfg(not(target_os = "wasi"))'.dependencies.web-sys] version = "0.3" features = ["History", "Window", "Location", "Url"] diff --git a/crates/history/src/any.rs b/crates/history/src/any.rs index 74e9ec43..288325c5 100644 --- a/crates/history/src/any.rs +++ b/crates/history/src/any.rs @@ -1,6 +1,8 @@ use std::borrow::Cow; +#[cfg(not(target_os = "wasi"))] use crate::browser::BrowserHistory; +#[cfg(not(target_os = "wasi"))] use crate::hash::HashHistory; use crate::history::History; use crate::listener::HistoryListener; @@ -13,8 +15,10 @@ use crate::{error::HistoryResult, query::ToQuery}; #[derive(Clone, PartialEq, Debug)] pub enum AnyHistory { /// A Browser History. + #[cfg(not(target_os = "wasi"))] Browser(BrowserHistory), /// A Hash History + #[cfg(not(target_os = "wasi"))] Hash(HashHistory), /// A Memory History Memory(MemoryHistory), @@ -23,7 +27,9 @@ pub enum AnyHistory { impl History for AnyHistory { fn len(&self) -> usize { match self { + #[cfg(not(target_os = "wasi"))] Self::Browser(m) => m.len(), + #[cfg(not(target_os = "wasi"))] Self::Hash(m) => m.len(), Self::Memory(m) => m.len(), } @@ -31,7 +37,9 @@ impl History for AnyHistory { fn go(&self, delta: isize) { match self { + #[cfg(not(target_os = "wasi"))] Self::Browser(m) => m.go(delta), + #[cfg(not(target_os = "wasi"))] Self::Hash(m) => m.go(delta), Self::Memory(m) => m.go(delta), } @@ -39,7 +47,9 @@ impl History for AnyHistory { fn push<'a>(&self, route: impl Into>) { match self { + #[cfg(not(target_os = "wasi"))] Self::Browser(m) => m.push(route), + #[cfg(not(target_os = "wasi"))] Self::Hash(m) => m.push(route), Self::Memory(m) => m.push(route), } @@ -47,7 +57,9 @@ impl History for AnyHistory { fn replace<'a>(&self, route: impl Into>) { match self { + #[cfg(not(target_os = "wasi"))] Self::Browser(m) => m.replace(route), + #[cfg(not(target_os = "wasi"))] Self::Hash(m) => m.replace(route), Self::Memory(m) => m.replace(route), } @@ -58,7 +70,9 @@ impl History for AnyHistory { T: 'static, { match self { + #[cfg(not(target_os = "wasi"))] Self::Browser(m) => m.push_with_state(route, state), + #[cfg(not(target_os = "wasi"))] Self::Hash(m) => m.push_with_state(route, state), Self::Memory(m) => m.push_with_state(route, state), } @@ -69,7 +83,9 @@ impl History for AnyHistory { T: 'static, { match self { + #[cfg(not(target_os = "wasi"))] Self::Browser(m) => m.replace_with_state(route, state), + #[cfg(not(target_os = "wasi"))] Self::Hash(m) => m.replace_with_state(route, state), Self::Memory(m) => m.replace_with_state(route, state), } @@ -85,7 +101,9 @@ impl History for AnyHistory { Q: ToQuery, { match self { + #[cfg(not(target_os = "wasi"))] Self::Browser(m) => m.push_with_query(route, query), + #[cfg(not(target_os = "wasi"))] Self::Hash(m) => m.push_with_query(route, query), Self::Memory(m) => m.push_with_query(route, query), } @@ -100,7 +118,9 @@ impl History for AnyHistory { Q: ToQuery, { match self { + #[cfg(not(target_os = "wasi"))] Self::Browser(m) => m.replace_with_query(route, query), + #[cfg(not(target_os = "wasi"))] Self::Hash(m) => m.replace_with_query(route, query), Self::Memory(m) => m.replace_with_query(route, query), } @@ -118,7 +138,9 @@ impl History for AnyHistory { T: 'static, { match self { + #[cfg(not(target_os = "wasi"))] Self::Browser(m) => m.push_with_query_and_state(route, query, state), + #[cfg(not(target_os = "wasi"))] Self::Hash(m) => m.push_with_query_and_state(route, query, state), Self::Memory(m) => m.push_with_query_and_state(route, query, state), } @@ -136,7 +158,9 @@ impl History for AnyHistory { T: 'static, { match self { + #[cfg(not(target_os = "wasi"))] Self::Browser(m) => m.replace_with_query_and_state(route, query, state), + #[cfg(not(target_os = "wasi"))] Self::Hash(m) => m.replace_with_query_and_state(route, query, state), Self::Memory(m) => m.replace_with_query_and_state(route, query, state), } @@ -147,7 +171,9 @@ impl History for AnyHistory { CB: Fn() + 'static, { match self { + #[cfg(not(target_os = "wasi"))] Self::Browser(m) => m.listen(callback), + #[cfg(not(target_os = "wasi"))] Self::Hash(m) => m.listen(callback), Self::Memory(m) => m.listen(callback), } @@ -155,19 +181,23 @@ impl History for AnyHistory { fn location(&self) -> Location { match self { + #[cfg(not(target_os = "wasi"))] Self::Browser(m) => m.location(), + #[cfg(not(target_os = "wasi"))] Self::Hash(m) => m.location(), Self::Memory(m) => m.location(), } } } +#[cfg(not(target_os = "wasi"))] impl From for AnyHistory { fn from(m: BrowserHistory) -> AnyHistory { AnyHistory::Browser(m) } } +#[cfg(not(target_os = "wasi"))] impl From for AnyHistory { fn from(m: HashHistory) -> AnyHistory { AnyHistory::Hash(m) diff --git a/crates/history/src/lib.rs b/crates/history/src/lib.rs index 9f508808..7d5777ff 100644 --- a/crates/history/src/lib.rs +++ b/crates/history/src/lib.rs @@ -4,9 +4,11 @@ #![deny(missing_docs, missing_debug_implementations)] mod any; +#[cfg(not(target_os = "wasi"))] mod browser; #[cfg(feature = "query")] mod error; +#[cfg(not(target_os = "wasi"))] mod hash; mod history; mod listener; @@ -18,7 +20,9 @@ mod state; mod utils; pub use any::AnyHistory; +#[cfg(not(target_os = "wasi"))] pub use browser::BrowserHistory; +#[cfg(not(target_os = "wasi"))] pub use hash::HashHistory; pub use memory::MemoryHistory; diff --git a/crates/history/src/state.rs b/crates/history/src/state.rs index 255c5ced..7386a421 100644 --- a/crates/history/src/state.rs +++ b/crates/history/src/state.rs @@ -1,3 +1,5 @@ +#![cfg(not(target_os = "wasi"))] + use std::any::Any; use std::collections::HashMap; use std::rc::Rc; From b35492ff0e3ede97f831a28fe2b65bc4b2265b7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Tue, 5 Dec 2023 13:22:20 +0800 Subject: [PATCH 6/8] fix: Exclude WASI example to avoid web-sys. --- .github/workflows/tests.yml | 7 +++---- Cargo.toml | 2 +- examples/history-wasi/.gitignore | 2 ++ examples/history-wasi/README.md | 7 ++++++- examples/history-wasi/src/main.rs | 13 +++++++++++++ 5 files changed, 25 insertions(+), 6 deletions(-) create mode 100644 examples/history-wasi/.gitignore diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 2d09f87f..4d970559 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -125,7 +125,6 @@ jobs: strategy: fail-fast: false matrix: - example: [history-wasi] rust-version: [1.64, stable, nightly] steps: - uses: actions/checkout@v4 @@ -142,15 +141,15 @@ jobs: path: | ~/.cargo/registry ~/.cargo/git - target + examples/history-wasi/target key: cargo-${{ runner.os }}-history-wasi-tests-${{ hashFiles('**/Cargo.toml') }} restore-keys: | cargo-${{ runner.os }}-history-wasi-tests- - name: Build and run example history-wasi run: | - cargo build -p example-${{ matrix.example }} --target wasm32-wasi - ~/.wasmtime/bin/wasmtime target/wasm32-wasi/debug/example-${{ matrix.example }}.wasm + cargo build --manifest-path examples/history-wasi/Cargo.toml --target wasm32-wasi + ~/.wasmtime/bin/wasmtime examples/history-wasi/target/wasm32-wasi/debug/example-history-wasi.wasm test-worker: name: Test gloo-worker diff --git a/Cargo.toml b/Cargo.toml index 0cb03fd0..f613b792 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -77,9 +77,9 @@ members = [ "examples/markdown", "examples/clock", "examples/file-hash", - "examples/history-wasi", "examples/prime", ] +exclude = ["examples/history-wasi"] # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling diff --git a/examples/history-wasi/.gitignore b/examples/history-wasi/.gitignore new file mode 100644 index 00000000..96ef6c0b --- /dev/null +++ b/examples/history-wasi/.gitignore @@ -0,0 +1,2 @@ +/target +Cargo.lock diff --git a/examples/history-wasi/README.md b/examples/history-wasi/README.md index 8cf51e23..817b3de2 100644 --- a/examples/history-wasi/README.md +++ b/examples/history-wasi/README.md @@ -2,4 +2,9 @@ This is a simple example showcasing the Gloo History on WASI. -You can run this example with `cargo wasi run --package example-history-wasi` +You can run this example with: + +```bash +cargo build --manifest-path examples/history-wasi/Cargo.toml --target wasm32-wasi +wasmtime examples/history-wasi/target/wasm32-wasi/debug/example-history-wasi.wasm +``` diff --git a/examples/history-wasi/src/main.rs b/examples/history-wasi/src/main.rs index fdeb8c41..a15f1b6e 100644 --- a/examples/history-wasi/src/main.rs +++ b/examples/history-wasi/src/main.rs @@ -2,9 +2,22 @@ use gloo::history::{History, MemoryHistory}; fn main() { let history = MemoryHistory::new(); + println!("Current path: {}", history.location().path()); + assert_eq!(history.location().path(), "/"); + history.push("/home"); + println!("Current path: {}", history.location().path()); + assert_eq!(history.location().path(), "/home"); + history.push("/about"); + println!("Current path: {}", history.location().path()); + assert_eq!(history.location().path(), "/about"); + history.push("/contact"); + println!("Current path: {}", history.location().path()); + assert_eq!(history.location().path(), "/contact"); + history.go(-2); + println!("Current path: {}", history.location().path()); assert_eq!(history.location().path(), "/home"); } From d5cad9a22b5af4adc045c5be4509c82f29c37693 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Tue, 5 Dec 2023 14:11:22 +0800 Subject: [PATCH 7/8] ci: Use static download instead of install bash. --- .github/workflows/tests.yml | 9 +++++++-- Cargo.lock | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 4d970559..de86b29b 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -134,7 +134,12 @@ jobs: target: wasm32-wasi - name: Install wasmtime - run: curl https://wasmtime.dev/install.sh -sSf | bash + run: | + wget https://github.com/bytecodealliance/wasmtime/releases/download/v15.0.1/wasmtime-v15.0.1-x86_64-linux.tar.xz + tar xf wasmtime-v15.0.1-x86_64-linux.tar.xz + mv wasmtime-v15.0.1-x86_64-linux/wasmtime ~/wasmtime + rm -rf wasmtime-v15.0.1-x86_64-linux.tar.xz wasmtime-v15.0.1-x86_64-linux + chmod +x ~/wasmtime - uses: actions/cache@v3 with: @@ -149,7 +154,7 @@ jobs: - name: Build and run example history-wasi run: | cargo build --manifest-path examples/history-wasi/Cargo.toml --target wasm32-wasi - ~/.wasmtime/bin/wasmtime examples/history-wasi/target/wasm32-wasi/debug/example-history-wasi.wasm + ~/wasmtime examples/history-wasi/target/wasm32-wasi/debug/example-history-wasi.wasm test-worker: name: Test gloo-worker diff --git a/Cargo.lock b/Cargo.lock index 0b1a2c60..d68650a7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -453,7 +453,7 @@ dependencies = [ "gloo-dialogs 0.2.0", "gloo-events 0.2.0", "gloo-file 0.3.0", - "gloo-history 0.2.0", + "gloo-history 0.2.1", "gloo-net 0.4.0", "gloo-render 0.2.0", "gloo-storage 0.3.0", @@ -573,7 +573,7 @@ dependencies = [ [[package]] name = "gloo-history" -version = "0.2.0" +version = "0.2.1" dependencies = [ "getrandom", "gloo-events 0.2.0", From 4a913172dcda9a86a2079e3d994e0d6df5fe7339 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BC=8A=E6=AC=A7?= Date: Wed, 6 Dec 2023 14:04:47 +0800 Subject: [PATCH 8/8] ci: Use CLI's flag instead of exclude example. https://github.com/bytecodealliance/wasmtime/pull/4312 --- .github/workflows/tests.yml | 11 ++++++----- Cargo.lock | 7 +++++++ Cargo.toml | 2 +- examples/history-wasi/.gitignore | 2 -- 4 files changed, 14 insertions(+), 8 deletions(-) delete mode 100644 examples/history-wasi/.gitignore diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index de86b29b..adce96af 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -146,15 +146,16 @@ jobs: path: | ~/.cargo/registry ~/.cargo/git - examples/history-wasi/target - key: cargo-${{ runner.os }}-history-wasi-tests-${{ hashFiles('**/Cargo.toml') }} + target + key: cargo-${{ runner.os }}-node-tests-${{ hashFiles('**/Cargo.toml') }} restore-keys: | - cargo-${{ runner.os }}-history-wasi-tests- + cargo-${{ runner.os }}-node-tests- + cargo-${{ runner.os }}- - name: Build and run example history-wasi run: | - cargo build --manifest-path examples/history-wasi/Cargo.toml --target wasm32-wasi - ~/wasmtime examples/history-wasi/target/wasm32-wasi/debug/example-history-wasi.wasm + cargo build --package example-history-wasi --target wasm32-wasi + ~/wasmtime --trap-unknown-imports target/wasm32-wasi/debug/example-history-wasi.wasm test-worker: name: Test gloo-worker diff --git a/Cargo.lock b/Cargo.lock index d68650a7..f5b74ad6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -256,6 +256,13 @@ dependencies = [ "yew", ] +[[package]] +name = "example-history-wasi" +version = "0.1.0" +dependencies = [ + "gloo 0.10.0", +] + [[package]] name = "example-markdown" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index f613b792..0cb03fd0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -77,9 +77,9 @@ members = [ "examples/markdown", "examples/clock", "examples/file-hash", + "examples/history-wasi", "examples/prime", ] -exclude = ["examples/history-wasi"] # Passing arguments to the docsrs builder in order to properly document cfg's. # More information: https://docs.rs/about/builds#cross-compiling diff --git a/examples/history-wasi/.gitignore b/examples/history-wasi/.gitignore deleted file mode 100644 index 96ef6c0b..00000000 --- a/examples/history-wasi/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/target -Cargo.lock