Skip to content

[Outdated] Add WASI example for CI. #410

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

Closed
wants to merge 16 commits into from
Closed
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
51 changes: 42 additions & 9 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ name: Tests
on:
pull_request:
push:
branches: [ master ]
branches: [master]

jobs:
native_tests:
@@ -119,14 +119,48 @@ 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 }}-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

test-worker:
name: Test gloo-worker
runs-on: ubuntu-latest
strategy:
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
24 changes: 22 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -77,6 +77,7 @@ members = [
"examples/markdown",
"examples/clock",
"examples/file-hash",
"examples/history-wasi",
"examples/prime",
]

6 changes: 4 additions & 2 deletions crates/history/Cargo.toml
Original file line number Diff line number Diff line change
@@ -12,15 +12,17 @@ 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"] }
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"]

30 changes: 30 additions & 0 deletions crates/history/src/any.rs
Original file line number Diff line number Diff line change
@@ -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,31 +27,39 @@ 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(),
}
}

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),
}
}

fn push<'a>(&self, route: impl Into<Cow<'a, str>>) {
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),
}
}

fn replace<'a>(&self, route: impl Into<Cow<'a, str>>) {
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,27 +171,33 @@ 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),
}
}

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<BrowserHistory> for AnyHistory {
fn from(m: BrowserHistory) -> AnyHistory {
AnyHistory::Browser(m)
}
}

#[cfg(not(target_os = "wasi"))]
impl From<HashHistory> for AnyHistory {
fn from(m: HashHistory) -> AnyHistory {
AnyHistory::Hash(m)
4 changes: 4 additions & 0 deletions crates/history/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;

2 changes: 2 additions & 0 deletions crates/history/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(not(target_os = "wasi"))]

use std::any::Any;
use std::collections::HashMap;
use std::rc::Rc;
10 changes: 10 additions & 0 deletions examples/history-wasi/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"] }
5 changes: 5 additions & 0 deletions examples/history-wasi/README.md
Original file line number Diff line number Diff line change
@@ -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`
10 changes: 10 additions & 0 deletions examples/history-wasi/src/main.rs
Original file line number Diff line number Diff line change
@@ -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");
}