allow optional wasmbindgen

use wasmbind feature
This commit is contained in:
Eric Sheppard 2022-08-11 22:42:37 +10:00 committed by Dirkjan Ochtman
parent 95223ee267
commit fe4bd220d2
8 changed files with 98 additions and 16 deletions

View File

@ -173,6 +173,30 @@ jobs:
RUST_VERSION: stable RUST_VERSION: stable
WASM: wasm_unknown 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: wasm_wasi:
strategy: strategy:
matrix: matrix:

View File

@ -20,13 +20,13 @@ appveyor = { repository = "chronotope/chrono" }
name = "chrono" name = "chrono"
[features] [features]
default = ["clock", "std", "oldtime"] default = ["clock", "std", "oldtime", "wasmbind"]
alloc = [] alloc = []
libc = [] libc = []
std = [] std = []
clock = ["std", "winapi", "iana-time-zone"] clock = ["std", "winapi", "iana-time-zone"]
oldtime = ["time"] 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"] unstable-locales = ["pure-rust-locales", "alloc"]
__internal_bench = ["criterion"] __internal_bench = ["criterion"]
__doctest = [] __doctest = []
@ -42,8 +42,8 @@ criterion = { version = "0.3", optional = true }
rkyv = {version = "0.7", optional = true} rkyv = {version = "0.7", optional = true}
[target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dependencies] [target.'cfg(all(target_arch = "wasm32", not(any(target_os = "emscripten", target_os = "wasi"))))'.dependencies]
wasm-bindgen = { version = "0.2" } wasm-bindgen = { version = "0.2", optional = true }
js-sys = { version = "0.3" } # contains FFI bindings for the JS Date API 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] [target.'cfg(not(any(target_os = "emscripten", target_os = "wasi", target_os = "solaris")))'.dependencies]
iana-time-zone = { version = "0.1.41", optional = true } iana-time-zone = { version = "0.1.41", optional = true }

View File

@ -38,6 +38,8 @@ meaningful in the github actions feature matrix UI.
test_wasm_emscripten test_wasm_emscripten
elif [[ ${WASM:-} == wasm_unknown ]]; then elif [[ ${WASM:-} == wasm_unknown ]]; then
test_wasm_unknown test_wasm_unknown
elif [[ ${WASM:-} == wasm_unknown_no_wasmbind ]]; then
test_wasm_unknown_no_wasmbind
elif [[ ${WASM:-} == wasm_wasi ]]; then elif [[ ${WASM:-} == wasm_wasi ]]; then
test_wasm_wasi test_wasm_wasi
elif [[ ${CORE:-} == no_std ]]; then elif [[ ${CORE:-} == no_std ]]; then
@ -126,6 +128,10 @@ test_wasm_unknown() {
runt cargo build --target wasm32-unknown-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() { test_wasm_wasi() {
runt cargo build --target wasm32-wasi runt cargo build --target wasm32-wasi
} }

View File

@ -994,21 +994,33 @@ impl<Tz: TimeZone> From<DateTime<Tz>> 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<js_sys::Date> for DateTime<Utc> { impl From<js_sys::Date> for DateTime<Utc> {
fn from(date: js_sys::Date) -> DateTime<Utc> { fn from(date: js_sys::Date) -> DateTime<Utc> {
DateTime::<Utc>::from(&date) DateTime::<Utc>::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<Utc> { impl From<&js_sys::Date> for DateTime<Utc> {
fn from(date: &js_sys::Date) -> DateTime<Utc> { fn from(date: &js_sys::Date) -> DateTime<Utc> {
Utc.timestamp_millis(date.get_time() as i64) 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<DateTime<Utc>> for js_sys::Date { impl From<DateTime<Utc>> for js_sys::Date {
/// Converts a `DateTime<Utc>` to a JS `Date`. The resulting value may be lossy, /// Converts a `DateTime<Utc>` 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 /// any values that have a millisecond timestamp value greater/less than ±8,640,000,000,000,000

View File

@ -16,7 +16,11 @@ use crate::{Date, DateTime};
#[cfg(all( #[cfg(all(
not(unix), not(unix),
not(windows), 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"] #[path = "stub.rs"]
mod inner; mod inner;
@ -59,6 +63,7 @@ impl Local {
/// Returns a `DateTime` which corresponds to the current date and time. /// Returns a `DateTime` which corresponds to the current date and time.
#[cfg(not(all( #[cfg(not(all(
target_arch = "wasm32", target_arch = "wasm32",
feature = "wasmbind",
not(any(target_os = "emscripten", target_os = "wasi")) not(any(target_os = "emscripten", target_os = "wasi"))
)))] )))]
pub fn now() -> DateTime<Local> { pub fn now() -> DateTime<Local> {
@ -66,7 +71,11 @@ impl Local {
} }
/// Returns a `DateTime` which corresponds to the current date and time. /// 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<Local> { pub fn now() -> DateTime<Local> {
use super::Utc; use super::Utc;
let now: DateTime<Utc> = super::Utc::now(); let now: DateTime<Utc> = super::Utc::now();
@ -110,7 +119,11 @@ impl TimeZone for Local {
midnight.map(|datetime| Date::from_utc(*local, *datetime.offset())) 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<DateTime<Local>> { fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<DateTime<Local>> {
let mut local = local.clone(); let mut local = local.clone();
// Get the offset from the js runtime // Get the offset from the js runtime
@ -121,6 +134,7 @@ impl TimeZone for Local {
#[cfg(not(all( #[cfg(not(all(
target_arch = "wasm32", target_arch = "wasm32",
feature = "wasmbind",
not(any(target_os = "emscripten", target_os = "wasi")) not(any(target_os = "emscripten", target_os = "wasi"))
)))] )))]
fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<DateTime<Local>> { fn from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult<DateTime<Local>> {
@ -132,7 +146,11 @@ impl TimeZone for Local {
Date::from_utc(*utc, *midnight.offset()) 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<Local> { fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime<Local> {
// Get the offset from the js runtime // Get the offset from the js runtime
let offset = FixedOffset::west((js_sys::Date::new_0().get_timezone_offset() as i32) * 60); 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( #[cfg(not(all(
target_arch = "wasm32", target_arch = "wasm32",
feature = "wasmbind",
not(any(target_os = "emscripten", target_os = "wasi")) not(any(target_os = "emscripten", target_os = "wasi"))
)))] )))]
fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime<Local> { fn from_utc_datetime(&self, utc: &NaiveDateTime) -> DateTime<Local> {

View File

@ -18,7 +18,11 @@ pub(super) fn now() -> DateTime<Local> {
} }
/// Converts a local `NaiveDateTime` to the `time::Timespec`. /// 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<DateTime<Local>> { pub(super) fn naive_to_local(d: &NaiveDateTime, local: bool) -> LocalResult<DateTime<Local>> {
let tm = Tm { let tm = Tm {
tm_sec: d.second() as i32, tm_sec: d.second() as i32,
@ -54,7 +58,11 @@ pub(super) fn naive_to_local(d: &NaiveDateTime, local: bool) -> LocalResult<Date
/// Converts a `time::Tm` struct into the timezone-aware `DateTime`. /// Converts a `time::Tm` struct into the timezone-aware `DateTime`.
/// This assumes that `time` is working correctly, i.e. any error is fatal. /// This assumes that `time` is working correctly, i.e. any error is fatal.
#[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"))
)))]
fn tm_to_datetime(mut tm: Tm) -> DateTime<Local> { fn tm_to_datetime(mut tm: Tm) -> DateTime<Local> {
if tm.tm_sec >= 60 { if tm.tm_sec >= 60 {
tm.tm_nsec += (tm.tm_sec - 59) * 1_000_000_000; tm.tm_nsec += (tm.tm_sec - 59) * 1_000_000_000;

View File

@ -6,7 +6,11 @@
use core::fmt; use core::fmt;
#[cfg(all( #[cfg(all(
feature = "clock", 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}; use std::time::{SystemTime, UNIX_EPOCH};
@ -49,6 +53,7 @@ impl Utc {
/// Returns a `DateTime` which corresponds to the current date and time. /// Returns a `DateTime` which corresponds to the current date and time.
#[cfg(not(all( #[cfg(not(all(
target_arch = "wasm32", target_arch = "wasm32",
feature = "wasmbind",
not(any(target_os = "emscripten", target_os = "wasi")) not(any(target_os = "emscripten", target_os = "wasi"))
)))] )))]
pub fn now() -> DateTime<Utc> { pub fn now() -> DateTime<Utc> {
@ -59,7 +64,11 @@ impl Utc {
} }
/// Returns a `DateTime` which corresponds to the current date and time. /// 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<Utc> { pub fn now() -> DateTime<Utc> {
let now = js_sys::Date::new_0(); let now = js_sys::Date::new_0();
DateTime::<Utc>::from(now) DateTime::<Utc>::from(now)

View File

@ -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::chrono::prelude::*;
use self::wasm_bindgen_test::*; use self::wasm_bindgen_test::*;