wasm fixes

This commit is contained in:
John Smith 2022-11-27 10:52:07 -05:00
parent 87366c7fb2
commit 80c8a62ea1
9 changed files with 36 additions and 74 deletions

2
Cargo.lock generated
View File

@ -5741,6 +5741,7 @@ dependencies = [
"async-std",
"async_executors",
"cfg-if 1.0.0",
"console_error_panic_hook",
"eyre",
"futures-util",
"jni",
@ -5766,6 +5767,7 @@ dependencies = [
"tokio-util",
"tracing",
"tracing-android",
"tracing-wasm",
"wasm-bindgen",
"wasm-bindgen-futures",
"wasm-bindgen-test",

View File

@ -9,7 +9,7 @@ license = "LGPL-2.0-or-later OR MPL-2.0 OR (MIT AND BSD-3-Clause)"
crate-type = ["rlib"]
[features]
default = [ "rt-tokio", "tracing" ]
default = [ "tracing" ]
rt-async-std = [ "async-std", "async_executors/async_std", ]
rt-tokio = [ "tokio", "tokio-util", "async_executors/tokio_tp", "async_executors/tokio_io", "async_executors/tokio_timer", ]
@ -54,14 +54,13 @@ async_executors = { version = "^0", default-features = false, features = [ "bind
async-lock = "^2"
send_wrapper = { version = "^0.6", features = ["futures"] }
# Dependencies for Android
[target.'cfg(target_os = "android")'.dependencies]
jni = "^0"
jni-sys = "^0"
ndk = { version = "^0", features = ["trace"] }
ndk-glue = { version = "^0", features = ["logger"] }
tracing-android = { version = "^0" }
tracing-android = { version = "^0", optional = true }
# Dependencies for Windows
# [target.'cfg(target_os = "windows")'.dependencies]
@ -81,9 +80,12 @@ serial_test = "^0"
simplelog = { version = "^0", features=["test"] }
[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
console_error_panic_hook = "^0"
wasm-bindgen-test = "^0"
wee_alloc = "^0"
wasm-logger = "^0"
tracing-wasm = { version = "^0" }
parking_lot = { version = "^0", features = ["wasm-bindgen"]}
### BUILD OPTIONS

View File

@ -28,6 +28,8 @@ impl RngCore for VeilidRng {
cfg_if! {
if #[cfg(target_arch = "wasm32")] {
use js_sys::Math;
pub fn random_bytes(dest: &mut [u8]) -> EyreResult<()> {
let len = dest.len();
let u32len = len / 4;

View File

@ -3,7 +3,7 @@ use std::time::Duration;
cfg_if! {
if #[cfg(target_arch = "wasm32")] {
use async_executors::Bindgen;
use async_executors::{Bindgen, Timer};
pub async fn sleep(millis: u32) {
Bindgen.sleep(Duration::from_millis(millis.into())).await

View File

@ -295,7 +295,7 @@ pub async fn test_sleep() {
if #[cfg(target_arch = "wasm32")] {
let t1 = Date::now();
intf::sleep(1000).await;
sleep(1000).await;
let t2 = Date::now();
assert!((t2-t1) >= 1000.0);

View File

@ -7,7 +7,7 @@ cfg_if! {
where
F: Future<Output = T>,
{
match select(Box::pin(intf::sleep(dur_ms)), Box::pin(f)).await {
match select(Box::pin(sleep(dur_ms)), Box::pin(f)).await {
Either::Left((_x, _b)) => Err(TimeoutError()),
Either::Right((y, _a)) => Ok(y),
}

View File

@ -5,7 +5,7 @@ cfg_if! {
use js_sys::Date;
pub fn get_timestamp() -> u64 {
if utils::is_browser() {
if is_browser() {
return (Date::now() * 1000.0f64) as u64;
} else {
panic!("WASM requires browser environment");

View File

@ -1,6 +1,7 @@
use super::*;
use core::sync::atomic::{AtomicI8, Ordering};
use js_sys::{global, Reflect};
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
extern "C" {
@ -27,21 +28,21 @@ pub fn is_browser() -> bool {
res
}
// pub fn is_browser_https() -> bool {
// static CACHE: AtomicI8 = AtomicI8::new(-1);
// let cache = CACHE.load(Ordering::Relaxed);
// if cache != -1 {
// return cache != 0;
// }
pub fn is_browser_https() -> bool {
static CACHE: AtomicI8 = AtomicI8::new(-1);
let cache = CACHE.load(Ordering::Relaxed);
if cache != -1 {
return cache != 0;
}
// let res = js_sys::eval("window.location.protocol === 'https'")
// .map(|res| res.is_truthy())
// .unwrap_or_default();
let res = js_sys::eval("window.location.protocol === 'https'")
.map(|res| res.is_truthy())
.unwrap_or_default();
// CACHE.store(res as i8, Ordering::Relaxed);
CACHE.store(res as i8, Ordering::Relaxed);
// res
// }
res
}
#[derive(ThisError, Debug, Clone, Eq, PartialEq)]
#[error("JsValue error")]

View File

@ -2,7 +2,7 @@
#![cfg(target_arch = "wasm32")]
use veilid_tools::tests::common::*;
use veilid_tools::xx::*;
use veilid_tools::*;
use wasm_bindgen_test::*;
wasm_bindgen_test_configure!(run_in_browser);
@ -15,19 +15,16 @@ static SETUP_ONCE: Once = Once::new();
pub fn setup() -> () {
SETUP_ONCE.call_once(|| {
console_error_panic_hook::set_once();
cfg_if! {
if #[cfg(feature = "tracing")] {
let mut builder = tracing_wasm::WASMLayerConfigBuilder::new();
builder.set_report_logs_in_timings(false);
builder.set_max_level(Level::TRACE);
builder.set_console_config(tracing_wasm::ConsoleConfig::ReportWithConsoleColor);
tracing_wasm::set_as_global_default_with_config(builder.build());
});
}
#[wasm_bindgen_test]
async fn run_test_dht_key() {
setup();
test_dht_key::test_all().await;
}
});
}
#[wasm_bindgen_test]
@ -37,48 +34,6 @@ async fn run_test_host_interface() {
test_host_interface::test_all().await;
}
#[wasm_bindgen_test]
async fn run_test_veilid_tools() {
setup();
test_veilid_tools::test_all().await;
}
#[wasm_bindgen_test]
async fn run_test_config() {
setup();
test_veilid_config::test_all().await;
}
#[wasm_bindgen_test]
async fn run_test_connection_table() {
setup();
test_connection_table::test_all().await;
}
#[wasm_bindgen_test]
async fn run_test_table_store() {
setup();
test_table_store::test_all().await;
}
#[wasm_bindgen_test]
async fn run_test_crypto() {
setup();
test_crypto::test_all().await;
}
#[wasm_bindgen_test]
async fn run_test_envelope_receipt() {
setup();
test_envelope_receipt::test_all().await;
}
#[wasm_bindgen_test]
async fn run_test_async_tag_lock() {
setup();