diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0fdcab58..8d81c202 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -31,4 +31,12 @@ jobs: - run: cargo build - run: cargo test --verbose --all env: - RUST_BACKTRACE: 1 \ No newline at end of file + RUST_BACKTRACE: 1 + + wasm: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - run: rustup update + - run: cargo install wasm-pack + - run: wasm-pack test --node lib diff --git a/lib/Cargo.toml b/lib/Cargo.toml index 9b1d04e6..0083d73f 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -29,8 +29,14 @@ rio_xml = "0.4" hex = "0.4" nom = "5" +[target.'cfg(target_arch = "wasm32")'.dependencies] +js-sys = "0.3" + [dev-dependencies] rayon = "1" +[target.'cfg(target_arch = "wasm32")'.dev-dependencies] +wasm-bindgen-test = "0.3" + [build-dependencies] peg = "0.5" diff --git a/lib/src/model/xsd/date_time.rs b/lib/src/model/xsd/date_time.rs index 38b11e9f..7f49db6b 100644 --- a/lib/src/model/xsd/date_time.rs +++ b/lib/src/model/xsd/date_time.rs @@ -579,12 +579,7 @@ impl Timestamp { fn now() -> Result { Timestamp::new( &date_time_plus_duration( - SystemTime::now() - .duration_since(SystemTime::UNIX_EPOCH)? - .try_into() - .map_err(|_| DateTimeError { - kind: DateTimeErrorKind::Overflow, - })?, + since_unix_epoch()?, &DateTimeSevenPropertyModel { year: Some(1970), month: Some(1), @@ -757,6 +752,24 @@ impl Timestamp { } } +#[cfg(target_arch = "wasm32")] +fn since_unix_epoch() -> Result { + Ok(Duration::new( + 0, + Decimal::from_f64(js_sys::Date::now() / 1000.), + )) +} + +#[cfg(not(target_arch = "wasm32"))] +fn since_unix_epoch() -> Result { + SystemTime::now() + .duration_since(SystemTime::UNIX_EPOCH)? + .try_into() + .map_err(|_| DateTimeError { + kind: DateTimeErrorKind::Overflow, + }) +} + /// The [normalizeMonth](https://www.w3.org/TR/xmlschema11-2/#f-dt-normMo) function fn normalize_month(yr: i64, mo: i64) -> Option<(i64, u8)> { if mo >= 0 { diff --git a/lib/tests/wasm.rs b/lib/tests/wasm.rs new file mode 100644 index 00000000..c885b8d5 --- /dev/null +++ b/lib/tests/wasm.rs @@ -0,0 +1,57 @@ +#[cfg(target_arch = "wasm32")] +mod test { + use oxigraph::model::*; + use oxigraph::sparql::{PreparedQuery, QueryOptions, QueryResult}; + use oxigraph::{MemoryRepository, Repository, RepositoryConnection, Result}; + use std::str::FromStr; + use wasm_bindgen_test::*; + + #[wasm_bindgen_test] + fn simple() { + let repository = MemoryRepository::default(); + let mut connection = repository.connection().unwrap(); + + // insertion + let ex = NamedNode::parse("http://example.com").unwrap(); + let quad = Quad::new(ex.clone(), ex.clone(), ex.clone(), None); + connection.insert(&quad).unwrap(); + // quad filter + let results: Result> = connection + .quads_for_pattern(None, None, None, None) + .collect(); + assert_eq!(vec![quad], results.unwrap()); + + // SPARQL query + let prepared_query = connection + .prepare_query("SELECT ?s WHERE { ?s ?p ?o }", QueryOptions::default()) + .unwrap(); + let results = prepared_query.exec().unwrap(); + if let QueryResult::Bindings(results) = results { + assert_eq!( + results.into_values_iter().next().unwrap().unwrap()[0], + Some(ex.into()) + ); + } + } + + #[wasm_bindgen_test] + fn now() { + if let QueryResult::Bindings(results) = MemoryRepository::default() + .connection() + .unwrap() + .prepare_query( + "SELECT (YEAR(NOW()) AS ?y) WHERE {}", + QueryOptions::default(), + ) + .unwrap() + .exec() + .unwrap() + { + if let Some(Term::Literal(l)) = &results.into_values_iter().next().unwrap().unwrap()[0] + { + let year = i64::from_str(l.value()).unwrap(); + assert!(2020 <= year && year <= 2100); + } + } + } +}