shorter WASM32 conditional tests

main
Ziyang Hu 2 years ago
parent 46f4e8e0fe
commit 14c9813fd4

@ -14,7 +14,7 @@ use std::time::{SystemTime, UNIX_EPOCH};
use chrono::{DateTime, TimeZone, Utc};
use itertools::Itertools;
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
#[cfg(target_arch = "wasm32")]
use js_sys::Date;
use miette::{bail, ensure, miette, Result};
use num_traits::FloatConst;
@ -1486,13 +1486,13 @@ pub(crate) fn op_to_uuid(args: &[DataValue]) -> Result<DataValue> {
}
define_op!(OP_NOW, 0, false);
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
#[cfg(target_arch = "wasm32")]
pub(crate) fn op_now(_args: &[DataValue]) -> Result<DataValue> {
let d: f64 = Date::now() / 1000.;
Ok(DataValue::from(d))
}
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn op_now(_args: &[DataValue]) -> Result<DataValue> {
let now = SystemTime::now();
Ok(DataValue::from(
@ -1501,12 +1501,12 @@ pub(crate) fn op_now(_args: &[DataValue]) -> Result<DataValue> {
}
pub(crate) fn current_validity() -> ValidityTs {
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
let ts_micros = {
let now = SystemTime::now();
now.duration_since(UNIX_EPOCH).unwrap().as_micros() as i64
};
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
#[cfg(target_arch = "wasm32")]
let ts_micros = { (Date::now() * 1000.) as i64 };
ValidityTs(Reverse(ts_micros))
@ -1575,14 +1575,14 @@ define_op!(OP_RAND_UUID_V1, 0, false);
pub(crate) fn op_rand_uuid_v1(_args: &[DataValue]) -> Result<DataValue> {
let mut rng = rand::thread_rng();
let uuid_ctx = uuid::v1::Context::new(rng.gen());
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
#[cfg(target_arch = "wasm32")]
let ts = {
let since_epoch: f64 = Date::now();
let seconds = since_epoch.floor();
let fractional = (since_epoch - seconds) * 1.0e9;
Timestamp::from_unix(uuid_ctx, seconds as u64, fractional as u32)
};
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
let ts = {
let now = SystemTime::now();
let since_epoch = now.duration_since(UNIX_EPOCH).unwrap();

@ -173,17 +173,17 @@ impl DbInstance {
payload: &str,
params: BTreeMap<String, JsonValue>,
) -> JsonValue {
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
let start = Instant::now();
match self.run_script(payload, params) {
Ok(named_rows) => {
let mut j_val = named_rows.into_json();
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
let took = start.elapsed().as_secs_f64();
let map = j_val.as_object_mut().unwrap();
map.insert("ok".to_string(), json!(true));
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
map.insert("took".to_string(), json!(took));
j_val

@ -245,10 +245,10 @@ pub(crate) fn parse_query(
out_opts.timeout = Some(timeout);
}
Rule::sleep_option => {
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
#[cfg(target_arch = "wasm32")]
bail!(":sleep is not supported under WASM");
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
{
let pair = pair.into_inner().next().unwrap();
let span = pair.extract_span();

@ -13,7 +13,7 @@ use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use itertools::Itertools;
use log::{debug, trace};
use miette::Result;
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
use rayon::prelude::*;
use crate::data::aggr::Aggregation;
@ -178,7 +178,7 @@ impl<'a> SessionTx<'a> {
};
Ok((k, new_store))
};
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
{
let limiter_enabled = limiter.total.is_some();
for res in prog
@ -203,7 +203,7 @@ impl<'a> SessionTx<'a> {
to_merge.insert(k, new_store);
}
}
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
#[cfg(target_arch = "wasm32")]
{
for res in prog.iter().map(execution) {
let (k, new_store) = res?;
@ -251,7 +251,7 @@ impl<'a> SessionTx<'a> {
};
Ok((k, new_store))
};
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
{
let limiter_enabled = limiter.total.is_some();
// entry rules with limiter must execute sequentially in order to get deterministic ordering
@ -276,7 +276,7 @@ impl<'a> SessionTx<'a> {
to_merge.insert(k, new_store);
}
}
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
#[cfg(target_arch = "wasm32")]
{
for res in prog.iter().map(execution) {
let (k, new_store) = res?;

@ -532,7 +532,7 @@ impl<'s, S: Storage<'s>> Db<S> {
}
res = q_res;
cleanups.extend(q_cleanups);
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
if let Some(secs) = sleep_opt {
thread::sleep(Duration::from_micros((secs * 1000000.) as u64));
}
@ -903,15 +903,15 @@ impl<'s, S: Storage<'s>> Db<S> {
let id = self.queries_count.fetch_add(1, Ordering::AcqRel);
// time the query
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
let now = SystemTime::now();
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
let since_the_epoch = now
.duration_since(UNIX_EPOCH)
.into_diagnostic()?
.as_secs_f64();
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
#[cfg(target_arch = "wasm32")]
let since_the_epoch = js_sys::Date::now();
let handle = RunningQueryHandle {
@ -1195,11 +1195,11 @@ impl Poison {
}
Ok(())
}
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
#[cfg(target_arch = "wasm32")]
pub(crate) fn set_timeout(&self, _secs: f64) -> Result<()> {
bail!("Cannot set timeout when threading is disallowed");
}
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn set_timeout(&self, secs: f64) -> Result<()> {
let pill = self.clone();
thread::spawn(move || {

@ -73,9 +73,9 @@ impl<'s> Storage<'s> for MemStorage {
wtr.remove(k);
}
};
#[cfg(all(target_arch = "wasm32", target_os = "unknown"))]
#[cfg(target_arch = "wasm32")]
closure();
#[cfg(not(all(target_arch = "wasm32", target_os = "unknown")))]
#[cfg(not(target_arch = "wasm32"))]
std::thread::spawn(closure);
Ok(())
}

Loading…
Cancel
Save