Skip to content

Commit 27c0558

Browse files
esheppadjc
authored andcommitted
dont use stub.rs when wasmbind feature is enabled
no longer require wasmbind feature to get js-sys on wasm32-unknown-unknown target fix comment and remove wasmbind feature from tests add wasi test add wasi test
1 parent 56f80e4 commit 27c0558

File tree

10 files changed

+69
-22
lines changed

10 files changed

+69
-22
lines changed

.github/workflows/test.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,30 @@ jobs:
173173
RUST_VERSION: stable
174174
WASM: wasm_unknown
175175

176+
wasm_wasi:
177+
strategy:
178+
matrix:
179+
os: [ubuntu-latest]
180+
181+
runs-on: ${{ matrix.os }}
182+
183+
steps:
184+
- uses: actions/checkout@v2
185+
186+
- name: Install rust
187+
uses: actions-rs/toolchain@v1
188+
with:
189+
toolchain: stable
190+
target: wasm32-wasi
191+
override: true
192+
default: true
193+
194+
- name: Build and Test
195+
run: bash ci/github.sh
196+
env:
197+
RUST_VERSION: stable
198+
WASM: wasm_wasi
199+
176200
cross-targets:
177201
strategy:
178202
matrix:

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Versions with only mechanical changes will be omitted from the following list.
3535
* Add the `and_local_timezone` method to `NaiveDateTime`
3636
* Fix the behavior of `Duration::abs()` for negative durations with non-zero nanos
3737
* Add compatibility with rfc2822 comments (#733)
38+
* Make `js-sys` and `wasm-bindgen` enabled by default when target is `wasm32-unknown-unknown` for ease of API discovery
3839

3940
## 0.4.19
4041

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ libc = []
2626
std = []
2727
clock = ["std", "winapi"]
2828
oldtime = ["time"]
29-
wasmbind = ["wasm-bindgen", "js-sys"]
29+
wasmbind = [] # TODO: empty feature to avoid breaking change in 0.4.20, can be removed later
3030
unstable-locales = ["pure-rust-locales", "alloc"]
3131
__internal_bench = ["criterion"]
3232
__doctest = []
@@ -42,8 +42,8 @@ criterion = { version = "0.3", optional = true }
4242
rkyv = {version = "0.7", optional = true}
4343

4444
[target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dependencies]
45-
wasm-bindgen = { version = "0.2", optional = true }
46-
js-sys = { version = "0.3", optional = true } # contains FFI bindings for the JS Date API
45+
wasm-bindgen = { version = "0.2" }
46+
js-sys = { version = "0.3" } # contains FFI bindings for the JS Date API
4747

4848
[target.'cfg(windows)'.dependencies]
4949
winapi = { version = "0.3.0", features = ["std", "minwinbase", "minwindef", "timezoneapi"], optional = true }

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,6 @@ UNIX-like operating systems and the Windows API (`winapi`) for Windows.
6060

6161
Optional features:
6262

63-
- `wasmbind`: Enable integration with [wasm-bindgen][] and its `js-sys` project
6463
- [`serde`][]: Enable serialization/deserialization via serde.
6564
- `unstable-locales`: Enable localization. This adds various methods with a
6665
`_localized` suffix. The implementation and API may change or even be

ci/github.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ meaningful in the github actions feature matrix UI.
3838
test_wasm_emscripten
3939
elif [[ ${WASM:-} == wasm_unknown ]]; then
4040
test_wasm_unknown
41+
elif [[ ${WASM:-} == wasm_wasi ]]; then
42+
test_wasm_wasi
4143
elif [[ ${CORE:-} == no_std ]]; then
4244
test_core
4345
elif [[ ${EXHAUSTIVE_TZ:-} == all_tzs ]]; then
@@ -106,13 +108,13 @@ test_wasm() {
106108
}
107109

108110
test_wasm_simple() {
109-
if ! runt env TZ="$(date +%z)" NOW="$(date +%s)" wasm-pack test --node -- --features wasmbind ; then
111+
if ! runt env TZ="$(date +%z)" NOW="$(date +%s)" wasm-pack test --node ; then
110112
# sometimes on github the initial build takes 8-10 minutes, and we
111113
# check that the time makes sense inside the test by approximately
112114
# comparing it to the env var,
113115
#
114116
# so re-run the test in case it took too long
115-
runt env TZ="$(date +%z)" NOW="$(date +%s)" wasm-pack test --node -- --features wasmbind
117+
runt env TZ="$(date +%z)" NOW="$(date +%s)" wasm-pack test --node
116118
fi
117119
}
118120

@@ -124,4 +126,8 @@ test_wasm_unknown() {
124126
runt cargo build --target wasm32-unknown-unknown
125127
}
126128

129+
test_wasm_wasi() {
130+
runt cargo build --target wasm32-wasi
131+
}
132+
127133
main "$@"

src/datetime/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -994,21 +994,21 @@ impl<Tz: TimeZone> From<DateTime<Tz>> for SystemTime {
994994
}
995995
}
996996

997-
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
997+
#[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))]
998998
impl From<js_sys::Date> for DateTime<Utc> {
999999
fn from(date: js_sys::Date) -> DateTime<Utc> {
10001000
DateTime::<Utc>::from(&date)
10011001
}
10021002
}
10031003

1004-
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
1004+
#[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))]
10051005
impl From<&js_sys::Date> for DateTime<Utc> {
10061006
fn from(date: &js_sys::Date) -> DateTime<Utc> {
10071007
Utc.timestamp_millis(date.get_time() as i64)
10081008
}
10091009
}
10101010

1011-
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
1011+
#[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))]
10121012
impl From<DateTime<Utc>> for js_sys::Date {
10131013
/// Converts a `DateTime<Utc>` to a JS `Date`. The resulting value may be lossy,
10141014
/// any values that have a millisecond timestamp value greater/less than ±8,640,000,000,000,000

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
//!
4848
//! Optional features:
4949
//!
50-
//! - `wasmbind`: Enable integration with [wasm-bindgen][] and its `js-sys` project
5150
//! - [`serde`][]: Enable serialization/deserialization via serde.
5251
//! - `unstable-locales`: Enable localization. This adds various methods with a
5352
//! `_localized` suffix. The implementation and API may change or even be

src/offset/local/mod.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ use super::{LocalResult, TimeZone};
1111
use crate::naive::{NaiveDate, NaiveDateTime};
1212
use crate::{Date, DateTime};
1313

14-
#[cfg(all(not(unix), not(windows)))]
14+
// we don't want `stub.rs` when the target_os is not wasi or emscripten
15+
// as we use js-sys to get the date instead
16+
#[cfg(all(
17+
not(unix),
18+
not(windows),
19+
not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))
20+
))]
1521
#[path = "stub.rs"]
1622
mod inner;
1723

@@ -51,13 +57,16 @@ impl Local {
5157
}
5258

5359
/// Returns a `DateTime` which corresponds to the current date and time.
54-
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
60+
#[cfg(not(all(
61+
target_arch = "wasm32",
62+
not(any(target_os = "emscripten", target_os = "wasi"))
63+
)))]
5564
pub fn now() -> DateTime<Local> {
5665
inner::now()
5766
}
5867

5968
/// Returns a `DateTime` which corresponds to the current date and time.
60-
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
69+
#[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))]
6170
pub fn now() -> DateTime<Local> {
6271
use super::Utc;
6372
let now: DateTime<Utc> = super::Utc::now();
@@ -101,7 +110,7 @@ impl TimeZone for Local {
101110
midnight.map(|datetime| Date::from_utc(*local, *datetime.offset()))
102111
}
103112

104-
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
113+
#[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))]
105114
fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<DateTime<Local>> {
106115
let mut local = local.clone();
107116
// Get the offset from the js runtime
@@ -110,7 +119,10 @@ impl TimeZone for Local {
110119
LocalResult::Single(DateTime::from_utc(local, offset))
111120
}
112121

113-
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
122+
#[cfg(not(all(
123+
target_arch = "wasm32",
124+
not(any(target_os = "emscripten", target_os = "wasi"))
125+
)))]
114126
fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<DateTime<Local>> {
115127
inner::naive_to_local(local, true)
116128
}
@@ -120,14 +132,17 @@ impl TimeZone for Local {
120132
Date::from_utc(*utc, *midnight.offset())
121133
}
122134

123-
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
135+
#[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))]
124136
fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime<Local> {
125137
// Get the offset from the js runtime
126138
let offset = FixedOffset::west((js_sys::Date::new_0().get_timezone_offset() as i32) * 60);
127139
DateTime::from_utc(*utc, offset)
128140
}
129141

130-
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
142+
#[cfg(not(all(
143+
target_arch = "wasm32",
144+
not(any(target_os = "emscripten", target_os = "wasi"))
145+
)))]
131146
fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime<Local> {
132147
// this is OK to unwrap as getting local time from a UTC
133148
// timestamp is never ambiguous

src/offset/local/stub.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub(super) fn now() -> DateTime<Local> {
1818
}
1919

2020
/// Converts a local `NaiveDateTime` to the `time::Timespec`.
21-
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
21+
#[cfg(not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))))]
2222
pub(super) fn naive_to_local(d: &NaiveDateTime, local: bool) -> LocalResult<DateTime<Local>> {
2323
let tm = Tm {
2424
tm_sec: d.second() as i32,
@@ -54,7 +54,7 @@ pub(super) fn naive_to_local(d: &NaiveDateTime, local: bool) -> LocalResult<Date
5454

5555
/// Converts a `time::Tm` struct into the timezone-aware `DateTime`.
5656
/// This assumes that `time` is working correctly, i.e. any error is fatal.
57-
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
57+
#[cfg(not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))))]
5858
fn tm_to_datetime(mut tm: Tm) -> DateTime<Local> {
5959
if tm.tm_sec >= 60 {
6060
tm.tm_nsec += (tm.tm_sec - 59) * 1_000_000_000;

src/offset/utc.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use core::fmt;
77
#[cfg(all(
88
feature = "clock",
9-
not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))
9+
not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))
1010
))]
1111
use std::time::{SystemTime, UNIX_EPOCH};
1212

@@ -47,7 +47,10 @@ impl Utc {
4747
}
4848

4949
/// Returns a `DateTime` which corresponds to the current date and time.
50-
#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind")))]
50+
#[cfg(not(all(
51+
target_arch = "wasm32",
52+
not(any(target_os = "emscripten", target_os = "wasi"))
53+
)))]
5154
pub fn now() -> DateTime<Utc> {
5255
let now =
5356
SystemTime::now().duration_since(UNIX_EPOCH).expect("system time before Unix epoch");
@@ -56,7 +59,7 @@ impl Utc {
5659
}
5760

5861
/// Returns a `DateTime` which corresponds to the current date and time.
59-
#[cfg(all(target_arch = "wasm32", not(target_os = "wasi"), feature = "wasmbind"))]
62+
#[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))]
6063
pub fn now() -> DateTime<Utc> {
6164
let now = js_sys::Date::new_0();
6265
DateTime::<Utc>::from(now)

0 commit comments

Comments
 (0)