Skip to content

Commit

Permalink
Put wasm-bindgen and js-sys behind a wasm-bindgen feature gate
Browse files Browse the repository at this point in the history
Fixes #334
  • Loading branch information
quodlibetor committed Sep 3, 2019
1 parent 630cade commit 2839d8d
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 24 deletions.
11 changes: 7 additions & 4 deletions .travis.yml
Expand Up @@ -16,16 +16,19 @@ matrix:
include:
- rust: nightly
env: CLIPPY=y
- rust: stable
os: osx
env: WASMBIND=y
- rust: beta
os: osx
env: WASMBIND=y

env:
global:
- LD_LIBRARY_PATH: /usr/local/lib
- CLIPPY: n
install:
- if [ "$TRAVIS_OS_NAME" = "osx" ]; then source $HOME/.nvm/nvm.sh; fi
- if [ "$TRAVIS_OS_NAME" = "osx" ]; then nvm install 10; fi
- if [ "$TRAVIS_OS_NAME" = "osx" ]; then nvm use 10; fi
- if [ "$TRAVIS_RUST_VERSION" != "1.13.0" ]; then curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh; fi
- if [ "$TRAVIS_OS_NAME" = "osx" ] && [ "$WASMBIND" = "y" ] ; then source ./ci/install-node.sh; fi
script: ./ci/travis.sh
notifications:
email: false
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,15 @@ Chrono obeys the principle of [Semantic Versioning](http://semver.org/).
There were/are numerous minor versions before 1.0 due to the language changes.
Versions with only mechanical changes will be omitted from the following list.

## 0.4.9

### Fixes

* Make Datetime arithmatic adjust their offsets after discovering their new
timestamps (@quodlibetor #337)
* Put wasm-bindgen related code and dependencies behind a `wasmbind` feature
gate. (@quodlibetor #335)

## 0.4.8

### Fixes
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Expand Up @@ -26,6 +26,7 @@ name = "chrono"
[features]
default = ["clock"]
clock = ["time"]
wasmbind = ["wasm-bindgen", "js-sys"]

[dependencies]
libc = { version = "0.2", default-features = false }
Expand All @@ -35,10 +36,9 @@ num-traits = { version = "0.2", default-features = false }
rustc-serialize = { version = "0.3.20", optional = true }
serde = { version = "1", optional = true }


[target.'cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))'.dependencies]
wasm-bindgen = { version = "0.2" }
js-sys = "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

[dev-dependencies]
serde_json = { version = "1" }
Expand Down
8 changes: 8 additions & 0 deletions ci/install-node.sh
@@ -0,0 +1,8 @@
#!/usr/bin/env sh

echo "installing node via nvm"

source "$HOME/.nvm/nvm.sh"
nvm install 10
nvm use 10
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
28 changes: 19 additions & 9 deletions ci/travis.sh
Expand Up @@ -29,6 +29,21 @@ build_and_test() {
# also vary the local time zone to (hopefully) catch tz-dependent bugs
# also avoid doc-testing multiple times---it takes a lot and rarely helps
cargo clean

if [ "${WASMBIND}" != "y" ]; then
build_and_test_nonwasm
else
build_and_test_wasm
fi

if [[ "$CHANNEL" == stable ]]; then
if [[ -n "$TRAVIS" ]] ; then
check_readme
fi
fi
}

build_and_test_nonwasm() {
channel build -v
TZ=ACST-9:30 channel test -v --lib
channel build -v --features rustc-serialize
Expand All @@ -47,9 +62,11 @@ build_and_test() {
TZ=UTC0 channel test -v --no-default-features --features serde --lib
channel build -v --no-default-features --features serde,rustc-serialize
TZ=Asia/Katmandu channel test -v --no-default-features --features serde,rustc-serialize --lib
}

build_and_test_wasm() {
channel build --features wasmbind -v

if [ -n "${TRAVIS}" ] && [ "${TRAVIS_RUST_VERSION}" != "1.13.0" ]; then
# wasm tests
touch tests/wasm.rs # ensure rebuild happens so TZ / NOW take effect
TZ=ACST-9:30 NOW=$(date +%s) wasm-pack test --node
touch tests/wasm.rs
Expand All @@ -58,13 +75,6 @@ build_and_test() {
TZ=UTC0 NOW=$(date +%s) wasm-pack test --node
touch tests/wasm.rs
TZ=Asia/Katmandu NOW=$(date +%s) wasm-pack test --node
fi

if [[ "$CHANNEL" == stable ]]; then
if [[ -n "$TRAVIS" ]] ; then
check_readme
fi
fi
}

build_only() {
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Expand Up @@ -414,9 +414,9 @@ extern crate serde as serdelib;
#[cfg(test)]
#[macro_use]
extern crate doc_comment;
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[cfg(all(target_arch = "wasm32", feature="wasmbind"))]
extern crate wasm_bindgen;
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[cfg(all(target_arch = "wasm32", feature="wasmbind"))]
extern crate js_sys;

#[cfg(test)]
Expand Down
4 changes: 2 additions & 2 deletions src/offset/local.rs
Expand Up @@ -87,13 +87,13 @@ impl Local {
}

/// Returns a `DateTime` which corresponds to the current date.
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
#[cfg(not(all(target_arch = "wasm32", feature = "wasmbind")))]
pub fn now() -> DateTime<Local> {
tm_to_datetime(oldtime::now())
}

/// Returns a `DateTime` which corresponds to the current date.
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[cfg(all(target_arch = "wasm32", feature = "wasmbind"))]
pub fn now() -> DateTime<Local> {
use super::Utc;
let now: DateTime<Utc> = super::Utc::now();
Expand Down
6 changes: 3 additions & 3 deletions src/offset/utc.rs
Expand Up @@ -4,7 +4,7 @@
//! The UTC (Coordinated Universal Time) time zone.

use std::fmt;
#[cfg(all(feature="clock", not(all(target_arch = "wasm32", not(target_os = "emscripten")))))]
#[cfg(all(feature="clock", not(all(target_arch = "wasm32", feature = "wasmbind"))))]
use oldtime;

use naive::{NaiveDate, NaiveDateTime};
Expand Down Expand Up @@ -38,15 +38,15 @@ impl Utc {
pub fn today() -> Date<Utc> { Utc::now().date() }

/// Returns a `DateTime` which corresponds to the current date.
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
#[cfg(not(all(target_arch = "wasm32", feature = "wasmbind")))]
pub fn now() -> DateTime<Utc> {
let spec = oldtime::get_time();
let naive = NaiveDateTime::from_timestamp(spec.sec, spec.nsec as u32);
DateTime::from_utc(naive, Utc)
}

/// Returns a `DateTime` which corresponds to the current date.
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[cfg(all(target_arch = "wasm32", feature = "wasmbind"))]
pub fn now() -> DateTime<Utc> {
let now = js_sys::Date::new_0();
let millisecs_since_unix_epoch: u64 = now.get_time() as u64;
Expand Down
2 changes: 1 addition & 1 deletion tests/wasm.rs
@@ -1,4 +1,4 @@
#[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))]
#[cfg(all(target_arch = "wasm32", feature = "wasmbind"))]
mod test {
extern crate chrono;
extern crate wasm_bindgen_test;
Expand Down

0 comments on commit 2839d8d

Please sign in to comment.