From fe4bd220d24496267c9e07807327e50fb3800fce Mon Sep 17 00:00:00 2001 From: Eric Sheppard Date: Thu, 11 Aug 2022 22:42:37 +1000 Subject: [PATCH] allow optional wasmbindgen use wasmbind feature --- .github/workflows/test.yml | 24 ++++++++++++++++++++++++ Cargo.toml | 8 ++++---- ci/github.sh | 6 ++++++ src/datetime/mod.rs | 18 +++++++++++++++--- src/offset/local/mod.rs | 27 +++++++++++++++++++++++---- src/offset/local/stub.rs | 12 ++++++++++-- src/offset/utc.rs | 13 +++++++++++-- tests/wasm.rs | 6 +++++- 8 files changed, 98 insertions(+), 16 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7c66f3a607..ec1f7cf813 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -173,6 +173,30 @@ jobs: RUST_VERSION: stable WASM: wasm_unknown + wasm_unknown_no_wasmbind: + strategy: + matrix: + os: [ubuntu-latest] + + runs-on: ${{ matrix.os }} + + steps: + - uses: actions/checkout@v2 + + - name: Install rust + uses: actions-rs/toolchain@v1 + with: + toolchain: stable + target: wasm32-unknown-unknown + override: true + default: true + + - name: Build and Test + run: bash ci/github.sh + env: + RUST_VERSION: stable + WASM: wasm_unknown_no_wasmbind + wasm_wasi: strategy: matrix: diff --git a/Cargo.toml b/Cargo.toml index 94adca3e72..e9e8a6ffe3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,13 +20,13 @@ appveyor = { repository = "chronotope/chrono" } name = "chrono" [features] -default = ["clock", "std", "oldtime"] +default = ["clock", "std", "oldtime", "wasmbind"] alloc = [] libc = [] std = [] clock = ["std", "winapi", "iana-time-zone"] oldtime = ["time"] -wasmbind = [] # TODO: empty feature to avoid breaking change in 0.4.20, can be removed later +wasmbind = ["wasm-bindgen", "js-sys"] unstable-locales = ["pure-rust-locales", "alloc"] __internal_bench = ["criterion"] __doctest = [] @@ -42,8 +42,8 @@ criterion = { version = "0.3", optional = true } rkyv = {version = "0.7", optional = true} [target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dependencies] -wasm-bindgen = { version = "0.2" } -js-sys = { version = "0.3" } # contains FFI bindings for the JS Date API +wasm-bindgen = { version = "0.2", optional = true } +js-sys = { version = "0.3", optional = true } # contains FFI bindings for the JS Date API [target.'cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "solaris")))'.dependencies] iana-time-zone = { version = "0.1.41", optional = true } diff --git a/ci/github.sh b/ci/github.sh index 31dbf5d53d..3c9ffb17a6 100755 --- a/ci/github.sh +++ b/ci/github.sh @@ -38,6 +38,8 @@ meaningful in the github actions feature matrix UI. test_wasm_emscripten elif [[ ${WASM:-} == wasm_unknown ]]; then test_wasm_unknown + elif [[ ${WASM:-} == wasm_unknown_no_wasmbind ]]; then + test_wasm_unknown_no_wasmbind elif [[ ${WASM:-} == wasm_wasi ]]; then test_wasm_wasi elif [[ ${CORE:-} == no_std ]]; then @@ -126,6 +128,10 @@ test_wasm_unknown() { runt cargo build --target wasm32-unknown-unknown } +test_wasm_unknown_no_wasmbind() { + runt cargo build --target wasm32-unknown-unknown --no-default-features --features clock,std +} + test_wasm_wasi() { runt cargo build --target wasm32-wasi } diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index a705f23b02..0907b2a6c0 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -994,21 +994,33 @@ impl From> for SystemTime { } } -#[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] +#[cfg(all( + target_arch = "wasm32", + feature = "wasmbind", + not(any(target_os = "emscripten", target_os = "wasi")) +))] impl From for DateTime { fn from(date: js_sys::Date) -> DateTime { DateTime::::from(&date) } } -#[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] +#[cfg(all( + target_arch = "wasm32", + feature = "wasmbind", + not(any(target_os = "emscripten", target_os = "wasi")) +))] impl From<&js_sys::Date> for DateTime { fn from(date: &js_sys::Date) -> DateTime { Utc.timestamp_millis(date.get_time() as i64) } } -#[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] +#[cfg(all( + target_arch = "wasm32", + feature = "wasmbind", + not(any(target_os = "emscripten", target_os = "wasi")) +))] impl From> for js_sys::Date { /// Converts a `DateTime` to a JS `Date`. The resulting value may be lossy, /// any values that have a millisecond timestamp value greater/less than ±8,640,000,000,000,000 diff --git a/src/offset/local/mod.rs b/src/offset/local/mod.rs index 42a3b3ff86..5a546de99e 100644 --- a/src/offset/local/mod.rs +++ b/src/offset/local/mod.rs @@ -16,7 +16,11 @@ use crate::{Date, DateTime}; #[cfg(all( not(unix), not(windows), - not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))) + not(all( + target_arch = "wasm32", + feature = "wasmbind", + not(any(target_os = "emscripten", target_os = "wasi")) + )) ))] #[path = "stub.rs"] mod inner; @@ -59,6 +63,7 @@ impl Local { /// Returns a `DateTime` which corresponds to the current date and time. #[cfg(not(all( target_arch = "wasm32", + feature = "wasmbind", not(any(target_os = "emscripten", target_os = "wasi")) )))] pub fn now() -> DateTime { @@ -66,7 +71,11 @@ impl Local { } /// Returns a `DateTime` which corresponds to the current date and time. - #[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] + #[cfg(all( + target_arch = "wasm32", + feature = "wasmbind", + not(any(target_os = "emscripten", target_os = "wasi")) + ))] pub fn now() -> DateTime { use super::Utc; let now: DateTime = super::Utc::now(); @@ -110,7 +119,11 @@ impl TimeZone for Local { midnight.map(|datetime| Date::from_utc(*local, *datetime.offset())) } - #[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] + #[cfg(all( + target_arch = "wasm32", + feature = "wasmbind", + not(any(target_os = "emscripten", target_os = "wasi")) + ))] fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult> { let mut local = local.clone(); // Get the offset from the js runtime @@ -121,6 +134,7 @@ impl TimeZone for Local { #[cfg(not(all( target_arch = "wasm32", + feature = "wasmbind", not(any(target_os = "emscripten", target_os = "wasi")) )))] fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult> { @@ -132,7 +146,11 @@ impl TimeZone for Local { Date::from_utc(*utc, *midnight.offset()) } - #[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] + #[cfg(all( + target_arch = "wasm32", + feature = "wasmbind", + not(any(target_os = "emscripten", target_os = "wasi")) + ))] fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime { // Get the offset from the js runtime let offset = FixedOffset::west((js_sys::Date::new_0().get_timezone_offset() as i32) * 60); @@ -141,6 +159,7 @@ impl TimeZone for Local { #[cfg(not(all( target_arch = "wasm32", + feature = "wasmbind", not(any(target_os = "emscripten", target_os = "wasi")) )))] fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime { diff --git a/src/offset/local/stub.rs b/src/offset/local/stub.rs index 57e491d5c6..21b362e5c4 100644 --- a/src/offset/local/stub.rs +++ b/src/offset/local/stub.rs @@ -18,7 +18,11 @@ pub(super) fn now() -> DateTime { } /// Converts a local `NaiveDateTime` to the `time::Timespec`. -#[cfg(not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))))] +#[cfg(not(all( + target_arch = "wasm32", + feature = "wasmbind", + not(any(target_os = "emscripten", target_os = "wasi")) +)))] pub(super) fn naive_to_local(d: &NaiveDateTime, local: bool) -> LocalResult> { let tm = Tm { tm_sec: d.second() as i32, @@ -54,7 +58,11 @@ pub(super) fn naive_to_local(d: &NaiveDateTime, local: bool) -> LocalResult DateTime { if tm.tm_sec >= 60 { tm.tm_nsec += (tm.tm_sec - 59) * 1_000_000_000; diff --git a/src/offset/utc.rs b/src/offset/utc.rs index 457006116d..047243d759 100644 --- a/src/offset/utc.rs +++ b/src/offset/utc.rs @@ -6,7 +6,11 @@ use core::fmt; #[cfg(all( feature = "clock", - not(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi")))) + not(all( + target_arch = "wasm32", + feature = "wasmbind", + not(any(target_os = "emscripten", target_os = "wasi")) + )) ))] use std::time::{SystemTime, UNIX_EPOCH}; @@ -49,6 +53,7 @@ impl Utc { /// Returns a `DateTime` which corresponds to the current date and time. #[cfg(not(all( target_arch = "wasm32", + feature = "wasmbind", not(any(target_os = "emscripten", target_os = "wasi")) )))] pub fn now() -> DateTime { @@ -59,7 +64,11 @@ impl Utc { } /// Returns a `DateTime` which corresponds to the current date and time. - #[cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] + #[cfg(all( + target_arch = "wasm32", + feature = "wasmbind", + not(any(target_os = "emscripten", target_os = "wasi")) + ))] pub fn now() -> DateTime { let now = js_sys::Date::new_0(); DateTime::::from(now) diff --git a/tests/wasm.rs b/tests/wasm.rs index 7e7433adbe..bcec43fb9f 100644 --- a/tests/wasm.rs +++ b/tests/wasm.rs @@ -1,4 +1,8 @@ -#![cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))] +#![cfg(all( + target_arch = "wasm32", + feature = "wasmbind", + not(any(target_os = "emscripten", target_os = "wasi")) +))] use self::chrono::prelude::*; use self::wasm_bindgen_test::*;