From 2d8a195831ebaee23c4a5b9effd904ea37e177c8 Mon Sep 17 00:00:00 2001 From: Abraham Egnor Date: Wed, 1 Mar 2023 11:46:17 -0500 Subject: [PATCH 1/2] RUST-1130/check-wasm-compilation --- .evergreen/check-wasm.sh | 9 +++++++++ .evergreen/config.yml | 23 +++++++++++++++++++++++ wasm-test/Cargo.toml | 14 ++++++++++++++ wasm-test/src/lib.rs | 1 + 4 files changed, 47 insertions(+) create mode 100755 .evergreen/check-wasm.sh create mode 100644 wasm-test/Cargo.toml create mode 100644 wasm-test/src/lib.rs diff --git a/.evergreen/check-wasm.sh b/.evergreen/check-wasm.sh new file mode 100755 index 00000000..7f4c7c4b --- /dev/null +++ b/.evergreen/check-wasm.sh @@ -0,0 +1,9 @@ +#!/bin/bash + +set -o errexit + +. ~/.cargo/env + +rustup target add wasm32-unknown-unknown +cd $(dirname $0)/../wasm-test +cargo build --target wasm32-unknown-unknown \ No newline at end of file diff --git a/.evergreen/config.yml b/.evergreen/config.yml index fc992e4f..e4ac3d5e 100644 --- a/.evergreen/config.yml +++ b/.evergreen/config.yml @@ -133,6 +133,16 @@ functions: ${PREPARE_SHELL} .evergreen/check-rustdoc.sh + "check wasm compilation": + - command: shell.exec + type: test + params: + shell: bash + working_dir: "src" + script: | + ${PREPARE_SHELL} + .evergreen/check-wasm.sh + "init test-results": - command: shell.exec params: @@ -177,6 +187,10 @@ tasks: commands: - func: "run fuzzer" + - name: "wasm-compilation" + commands: + - func: "check wasm compilation" + axes: - id: "extra-rust-versions" values: @@ -207,6 +221,7 @@ buildvariants: - ubuntu1804-test tasks: - name: "compile-only" + - name: "lint" display_name: "Lint" @@ -224,3 +239,11 @@ buildvariants: - ubuntu1804-test tasks: - name: "run-fuzzer" + +- + name: "wasm" + display_name: "WASM" + run_on: + - ubuntu1804-test + tasks: + - name: "wasm-compilation" \ No newline at end of file diff --git a/wasm-test/Cargo.toml b/wasm-test/Cargo.toml new file mode 100644 index 00000000..1332dc35 --- /dev/null +++ b/wasm-test/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "bson-wasm-test" +version = "0.1.0" +authors = ["Abraham Egnor "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[lib] +crate-type = ["cdylib", "rlib"] + +[dependencies] +bson = { path = ".." } +getrandom = { version = "0.2", features = ["js"] } diff --git a/wasm-test/src/lib.rs b/wasm-test/src/lib.rs new file mode 100644 index 00000000..bbc82658 --- /dev/null +++ b/wasm-test/src/lib.rs @@ -0,0 +1 @@ +pub use bson; \ No newline at end of file From dcacd7fd81a56597a9e615f032b71b8a2255b491 Mon Sep 17 00:00:00 2001 From: Abraham Egnor Date: Thu, 2 Mar 2023 11:59:49 -0500 Subject: [PATCH 2/2] RUST-504 Use timestamp from js when running as wasm --- .evergreen/check-wasm.sh | 9 --------- .evergreen/config.yml | 10 +++++----- .evergreen/run-wasm-tests.sh | 10 ++++++++++ Cargo.toml | 3 +++ src/oid.rs | 8 ++++++-- wasm-test/Cargo.toml | 3 +++ wasm-test/src/lib.rs | 3 ++- wasm-test/src/test.rs | 6 ++++++ 8 files changed, 35 insertions(+), 17 deletions(-) delete mode 100755 .evergreen/check-wasm.sh create mode 100755 .evergreen/run-wasm-tests.sh create mode 100644 wasm-test/src/test.rs diff --git a/.evergreen/check-wasm.sh b/.evergreen/check-wasm.sh deleted file mode 100755 index 7f4c7c4b..00000000 --- a/.evergreen/check-wasm.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -set -o errexit - -. ~/.cargo/env - -rustup target add wasm32-unknown-unknown -cd $(dirname $0)/../wasm-test -cargo build --target wasm32-unknown-unknown \ No newline at end of file diff --git a/.evergreen/config.yml b/.evergreen/config.yml index e4ac3d5e..14fa2f2f 100644 --- a/.evergreen/config.yml +++ b/.evergreen/config.yml @@ -133,7 +133,7 @@ functions: ${PREPARE_SHELL} .evergreen/check-rustdoc.sh - "check wasm compilation": + "run wasm tests": - command: shell.exec type: test params: @@ -141,7 +141,7 @@ functions: working_dir: "src" script: | ${PREPARE_SHELL} - .evergreen/check-wasm.sh + .evergreen/run-wasm-tests.sh "init test-results": - command: shell.exec @@ -187,9 +187,9 @@ tasks: commands: - func: "run fuzzer" - - name: "wasm-compilation" + - name: "wasm-test" commands: - - func: "check wasm compilation" + - func: "run wasm tests" axes: - id: "extra-rust-versions" @@ -246,4 +246,4 @@ buildvariants: run_on: - ubuntu1804-test tasks: - - name: "wasm-compilation" \ No newline at end of file + - name: "wasm-test" \ No newline at end of file diff --git a/.evergreen/run-wasm-tests.sh b/.evergreen/run-wasm-tests.sh new file mode 100755 index 00000000..806c675e --- /dev/null +++ b/.evergreen/run-wasm-tests.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +set -o errexit + +. ~/.cargo/env + +curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh + +cd $(dirname $0)/../wasm-test +wasm-pack test --node \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index db54bf7c..68d00153 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -68,6 +68,9 @@ serde_bytes = "0.11.5" serde_with = { version = "1", optional = true } time = { version = "0.3.9", features = ["formatting", "parsing", "macros", "large-dates"] } +[target.'cfg(target_arch = "wasm32")'.dependencies] +js-sys = "0.3" + [dev-dependencies] assert_matches = "1.2" criterion = "0.3.0" diff --git a/src/oid.rs b/src/oid.rs index 9161cd8f..263fc805 100644 --- a/src/oid.rs +++ b/src/oid.rs @@ -2,15 +2,16 @@ //! For more information, see the documentation for the [`ObjectId`] type. use std::{ - convert::TryInto, error, fmt, result, str::FromStr, sync::atomic::{AtomicUsize, Ordering}, - time::SystemTime, }; +#[cfg(not(target_arch = "wasm32"))] +use std::{convert::TryInto, time::SystemTime}; + use hex::{self, FromHexError}; use rand::{thread_rng, Rng}; @@ -240,6 +241,9 @@ impl ObjectId { /// Generates a new timestamp representing the current seconds since epoch. /// Represented in Big Endian. fn gen_timestamp() -> [u8; 4] { + #[cfg(target_arch = "wasm32")] + let timestamp: u32 = (js_sys::Date::now() / 1000.0) as u32; + #[cfg(not(target_arch = "wasm32"))] let timestamp: u32 = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .expect("system clock is before 1970") diff --git a/wasm-test/Cargo.toml b/wasm-test/Cargo.toml index 1332dc35..fb583123 100644 --- a/wasm-test/Cargo.toml +++ b/wasm-test/Cargo.toml @@ -12,3 +12,6 @@ crate-type = ["cdylib", "rlib"] [dependencies] bson = { path = ".." } getrandom = { version = "0.2", features = ["js"] } + +[dev-dependencies] +wasm-bindgen-test = "0.3.0" \ No newline at end of file diff --git a/wasm-test/src/lib.rs b/wasm-test/src/lib.rs index bbc82658..534ffa4b 100644 --- a/wasm-test/src/lib.rs +++ b/wasm-test/src/lib.rs @@ -1 +1,2 @@ -pub use bson; \ No newline at end of file +#[cfg(test)] +mod test; \ No newline at end of file diff --git a/wasm-test/src/test.rs b/wasm-test/src/test.rs new file mode 100644 index 00000000..8703effa --- /dev/null +++ b/wasm-test/src/test.rs @@ -0,0 +1,6 @@ +use wasm_bindgen_test::wasm_bindgen_test; + +#[wasm_bindgen_test] +fn objectid_new() { + let _ = bson::oid::ObjectId::new(); +} \ No newline at end of file