Skip to content

Commit 399dabb

Browse files
authored
Add WASI example for CI. (#411)
* feat: Support WASI target. * fix: More friendly error message for wasi target. * fix: Use target_os instead of feature. * feat: Add an example for gloo-history on WASI. * fix: Avoid to use web-sys when target os is WASI. * fix: Exclude WASI example to avoid web-sys. * ci: Use static download instead of install bash. * ci: Use CLI's flag instead of exclude example. bytecodealliance/wasmtime#4312
1 parent 7516f26 commit 399dabb

File tree

10 files changed

+139
-13
lines changed

10 files changed

+139
-13
lines changed

.github/workflows/tests.yml

+46-9
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ name: Tests
33
on:
44
pull_request:
55
push:
6-
branches: [ master ]
6+
branches: [master]
77

88
jobs:
99
native_tests:
@@ -119,14 +119,52 @@ jobs:
119119
wasm-pack test --node crates/$x --no-default-features
120120
done
121121
122+
test-history-wasi:
123+
name: Test gloo-history WASI
124+
runs-on: ubuntu-latest
125+
strategy:
126+
fail-fast: false
127+
matrix:
128+
rust-version: [1.64, stable, nightly]
129+
steps:
130+
- uses: actions/checkout@v4
131+
- uses: dtolnay/rust-toolchain@master
132+
with:
133+
toolchain: ${{ matrix.rust-version }}
134+
target: wasm32-wasi
135+
136+
- name: Install wasmtime
137+
run: |
138+
wget https://github.com/bytecodealliance/wasmtime/releases/download/v15.0.1/wasmtime-v15.0.1-x86_64-linux.tar.xz
139+
tar xf wasmtime-v15.0.1-x86_64-linux.tar.xz
140+
mv wasmtime-v15.0.1-x86_64-linux/wasmtime ~/wasmtime
141+
rm -rf wasmtime-v15.0.1-x86_64-linux.tar.xz wasmtime-v15.0.1-x86_64-linux
142+
chmod +x ~/wasmtime
143+
144+
- uses: actions/cache@v3
145+
with:
146+
path: |
147+
~/.cargo/registry
148+
~/.cargo/git
149+
target
150+
key: cargo-${{ runner.os }}-node-tests-${{ hashFiles('**/Cargo.toml') }}
151+
restore-keys: |
152+
cargo-${{ runner.os }}-node-tests-
153+
cargo-${{ runner.os }}-
154+
155+
- name: Build and run example history-wasi
156+
run: |
157+
cargo build --package example-history-wasi --target wasm32-wasi
158+
~/wasmtime --trap-unknown-imports target/wasm32-wasi/debug/example-history-wasi.wasm
159+
122160
test-worker:
123161
name: Test gloo-worker
124162
runs-on: ubuntu-latest
125163
strategy:
126164
fail-fast: false
127165
matrix:
128166
# example: [ markdown, prime ]
129-
example: [ markdown ]
167+
example: [markdown]
130168
rust-version: [1.64, stable, nightly]
131169
steps:
132170
- uses: actions/checkout@v4
@@ -167,7 +205,6 @@ jobs:
167205
run: |
168206
wasm-pack test --headless --chrome --firefox examples/${{ matrix.example }}
169207
170-
171208
test-net:
172209
strategy:
173210
fail-fast: false
@@ -208,9 +245,9 @@ jobs:
208245
209246
- name: Run browser tests
210247
env:
211-
HTTPBIN_URL: "http://localhost:8080"
212-
WS_ECHO_SERVER_URL: "ws://localhost:8081"
213-
SSE_ECHO_SERVER_URL: "http://localhost:8081/.sse"
248+
HTTPBIN_URL: 'http://localhost:8080'
249+
WS_ECHO_SERVER_URL: 'ws://localhost:8081'
250+
SSE_ECHO_SERVER_URL: 'http://localhost:8081/.sse'
214251
run: |
215252
cd crates/net
216253
wasm-pack test --chrome --firefox --headless --all-features
@@ -222,7 +259,7 @@ jobs:
222259

223260
- name: Run native tests
224261
env:
225-
HTTPBIN_URL: "http://localhost:8080"
226-
WS_ECHO_SERVER_URL: "ws://localhost:8081"
227-
SSE_ECHO_SERVER_URL: "http://localhost:8081/.sse"
262+
HTTPBIN_URL: 'http://localhost:8080'
263+
WS_ECHO_SERVER_URL: 'ws://localhost:8081'
264+
SSE_ECHO_SERVER_URL: 'http://localhost:8081/.sse'
228265
run: cargo test -p gloo-net --all-features

Cargo.lock

+9-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ members = [
7777
"examples/markdown",
7878
"examples/clock",
7979
"examples/file-hash",
80+
"examples/history-wasi",
8081
"examples/prime",
8182
]
8283

crates/history/Cargo.toml

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,17 @@ categories = ["api-bindings", "history", "wasm"]
1212
rust-version = "1.64"
1313

1414
[dependencies]
15-
wasm-bindgen = "0.2"
1615
gloo-utils = { version = "0.2.0", path = "../utils" }
1716
gloo-events = { version = "0.2.0", path = "../events" }
1817
serde = { version = "1", features = ["derive"] }
1918
serde-wasm-bindgen = "0.6.0"
2019
serde_urlencoded = { version = "0.7", optional = true }
2120
thiserror = { version = "1.0", optional = true }
2221

23-
[dependencies.web-sys]
22+
[target.'cfg(not(target_os = "wasi"))'.dependencies]
23+
wasm-bindgen = "0.2"
24+
25+
[target.'cfg(not(target_os = "wasi"))'.dependencies.web-sys]
2426
version = "0.3"
2527
features = ["History", "Window", "Location", "Url"]
2628

crates/history/src/any.rs

+30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use std::borrow::Cow;
22

3+
#[cfg(not(target_os = "wasi"))]
34
use crate::browser::BrowserHistory;
5+
#[cfg(not(target_os = "wasi"))]
46
use crate::hash::HashHistory;
57
use crate::history::History;
68
use crate::listener::HistoryListener;
@@ -13,8 +15,10 @@ use crate::{error::HistoryResult, query::ToQuery};
1315
#[derive(Clone, PartialEq, Debug)]
1416
pub enum AnyHistory {
1517
/// A Browser History.
18+
#[cfg(not(target_os = "wasi"))]
1619
Browser(BrowserHistory),
1720
/// A Hash History
21+
#[cfg(not(target_os = "wasi"))]
1822
Hash(HashHistory),
1923
/// A Memory History
2024
Memory(MemoryHistory),
@@ -23,31 +27,39 @@ pub enum AnyHistory {
2327
impl History for AnyHistory {
2428
fn len(&self) -> usize {
2529
match self {
30+
#[cfg(not(target_os = "wasi"))]
2631
Self::Browser(m) => m.len(),
32+
#[cfg(not(target_os = "wasi"))]
2733
Self::Hash(m) => m.len(),
2834
Self::Memory(m) => m.len(),
2935
}
3036
}
3137

3238
fn go(&self, delta: isize) {
3339
match self {
40+
#[cfg(not(target_os = "wasi"))]
3441
Self::Browser(m) => m.go(delta),
42+
#[cfg(not(target_os = "wasi"))]
3543
Self::Hash(m) => m.go(delta),
3644
Self::Memory(m) => m.go(delta),
3745
}
3846
}
3947

4048
fn push<'a>(&self, route: impl Into<Cow<'a, str>>) {
4149
match self {
50+
#[cfg(not(target_os = "wasi"))]
4251
Self::Browser(m) => m.push(route),
52+
#[cfg(not(target_os = "wasi"))]
4353
Self::Hash(m) => m.push(route),
4454
Self::Memory(m) => m.push(route),
4555
}
4656
}
4757

4858
fn replace<'a>(&self, route: impl Into<Cow<'a, str>>) {
4959
match self {
60+
#[cfg(not(target_os = "wasi"))]
5061
Self::Browser(m) => m.replace(route),
62+
#[cfg(not(target_os = "wasi"))]
5163
Self::Hash(m) => m.replace(route),
5264
Self::Memory(m) => m.replace(route),
5365
}
@@ -58,7 +70,9 @@ impl History for AnyHistory {
5870
T: 'static,
5971
{
6072
match self {
73+
#[cfg(not(target_os = "wasi"))]
6174
Self::Browser(m) => m.push_with_state(route, state),
75+
#[cfg(not(target_os = "wasi"))]
6276
Self::Hash(m) => m.push_with_state(route, state),
6377
Self::Memory(m) => m.push_with_state(route, state),
6478
}
@@ -69,7 +83,9 @@ impl History for AnyHistory {
6983
T: 'static,
7084
{
7185
match self {
86+
#[cfg(not(target_os = "wasi"))]
7287
Self::Browser(m) => m.replace_with_state(route, state),
88+
#[cfg(not(target_os = "wasi"))]
7389
Self::Hash(m) => m.replace_with_state(route, state),
7490
Self::Memory(m) => m.replace_with_state(route, state),
7591
}
@@ -85,7 +101,9 @@ impl History for AnyHistory {
85101
Q: ToQuery,
86102
{
87103
match self {
104+
#[cfg(not(target_os = "wasi"))]
88105
Self::Browser(m) => m.push_with_query(route, query),
106+
#[cfg(not(target_os = "wasi"))]
89107
Self::Hash(m) => m.push_with_query(route, query),
90108
Self::Memory(m) => m.push_with_query(route, query),
91109
}
@@ -100,7 +118,9 @@ impl History for AnyHistory {
100118
Q: ToQuery,
101119
{
102120
match self {
121+
#[cfg(not(target_os = "wasi"))]
103122
Self::Browser(m) => m.replace_with_query(route, query),
123+
#[cfg(not(target_os = "wasi"))]
104124
Self::Hash(m) => m.replace_with_query(route, query),
105125
Self::Memory(m) => m.replace_with_query(route, query),
106126
}
@@ -118,7 +138,9 @@ impl History for AnyHistory {
118138
T: 'static,
119139
{
120140
match self {
141+
#[cfg(not(target_os = "wasi"))]
121142
Self::Browser(m) => m.push_with_query_and_state(route, query, state),
143+
#[cfg(not(target_os = "wasi"))]
122144
Self::Hash(m) => m.push_with_query_and_state(route, query, state),
123145
Self::Memory(m) => m.push_with_query_and_state(route, query, state),
124146
}
@@ -136,7 +158,9 @@ impl History for AnyHistory {
136158
T: 'static,
137159
{
138160
match self {
161+
#[cfg(not(target_os = "wasi"))]
139162
Self::Browser(m) => m.replace_with_query_and_state(route, query, state),
163+
#[cfg(not(target_os = "wasi"))]
140164
Self::Hash(m) => m.replace_with_query_and_state(route, query, state),
141165
Self::Memory(m) => m.replace_with_query_and_state(route, query, state),
142166
}
@@ -147,27 +171,33 @@ impl History for AnyHistory {
147171
CB: Fn() + 'static,
148172
{
149173
match self {
174+
#[cfg(not(target_os = "wasi"))]
150175
Self::Browser(m) => m.listen(callback),
176+
#[cfg(not(target_os = "wasi"))]
151177
Self::Hash(m) => m.listen(callback),
152178
Self::Memory(m) => m.listen(callback),
153179
}
154180
}
155181

156182
fn location(&self) -> Location {
157183
match self {
184+
#[cfg(not(target_os = "wasi"))]
158185
Self::Browser(m) => m.location(),
186+
#[cfg(not(target_os = "wasi"))]
159187
Self::Hash(m) => m.location(),
160188
Self::Memory(m) => m.location(),
161189
}
162190
}
163191
}
164192

193+
#[cfg(not(target_os = "wasi"))]
165194
impl From<BrowserHistory> for AnyHistory {
166195
fn from(m: BrowserHistory) -> AnyHistory {
167196
AnyHistory::Browser(m)
168197
}
169198
}
170199

200+
#[cfg(not(target_os = "wasi"))]
171201
impl From<HashHistory> for AnyHistory {
172202
fn from(m: HashHistory) -> AnyHistory {
173203
AnyHistory::Hash(m)

crates/history/src/lib.rs

+4
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
#![deny(missing_docs, missing_debug_implementations)]
55

66
mod any;
7+
#[cfg(not(target_os = "wasi"))]
78
mod browser;
89
#[cfg(feature = "query")]
910
mod error;
11+
#[cfg(not(target_os = "wasi"))]
1012
mod hash;
1113
mod history;
1214
mod listener;
@@ -18,7 +20,9 @@ mod state;
1820
mod utils;
1921

2022
pub use any::AnyHistory;
23+
#[cfg(not(target_os = "wasi"))]
2124
pub use browser::BrowserHistory;
25+
#[cfg(not(target_os = "wasi"))]
2226
pub use hash::HashHistory;
2327
pub use memory::MemoryHistory;
2428

crates/history/src/state.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![cfg(not(target_os = "wasi"))]
2+
13
use std::any::Any;
24
use std::collections::HashMap;
35
use std::rc::Rc;

examples/history-wasi/Cargo.toml

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "example-history-wasi"
3+
version = "0.1.0"
4+
authors = ["Rust and WebAssembly Working Group"]
5+
edition = "2021"
6+
publish = false
7+
rust-version = "1.64"
8+
9+
[dependencies]
10+
gloo = { path = "../..", default-features = false, features = ["history"] }

examples/history-wasi/README.md

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# History example on WASI
2+
3+
This is a simple example showcasing the Gloo History on WASI.
4+
5+
You can run this example with:
6+
7+
```bash
8+
cargo build --manifest-path examples/history-wasi/Cargo.toml --target wasm32-wasi
9+
wasmtime examples/history-wasi/target/wasm32-wasi/debug/example-history-wasi.wasm
10+
```

examples/history-wasi/src/main.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
use gloo::history::{History, MemoryHistory};
2+
3+
fn main() {
4+
let history = MemoryHistory::new();
5+
println!("Current path: {}", history.location().path());
6+
assert_eq!(history.location().path(), "/");
7+
8+
history.push("/home");
9+
println!("Current path: {}", history.location().path());
10+
assert_eq!(history.location().path(), "/home");
11+
12+
history.push("/about");
13+
println!("Current path: {}", history.location().path());
14+
assert_eq!(history.location().path(), "/about");
15+
16+
history.push("/contact");
17+
println!("Current path: {}", history.location().path());
18+
assert_eq!(history.location().path(), "/contact");
19+
20+
history.go(-2);
21+
println!("Current path: {}", history.location().path());
22+
assert_eq!(history.location().path(), "/home");
23+
}

0 commit comments

Comments
 (0)