From b5906a52bcd7aeb338647c4a47d7dc4fb9a2cdee Mon Sep 17 00:00:00 2001 From: John Smith Date: Sun, 2 Jul 2023 16:23:04 -0500 Subject: [PATCH] fix wasm --- .../src/network_manager/wasm/protocol/mod.rs | 2 +- .../operations/operation_watch_value.rs | 4 +- veilid-core/tests/web.rs | 3 +- veilid-tools/src/must_join_handle.rs | 5 +- veilid-tools/src/timeout.rs | 1 + veilid-tools/src/timestamp.rs | 5 +- veilid-tools/tests/web.rs | 2 + veilid-wasm/src/lib.rs | 90 +++++++++---------- 8 files changed, 59 insertions(+), 53 deletions(-) diff --git a/veilid-core/src/network_manager/wasm/protocol/mod.rs b/veilid-core/src/network_manager/wasm/protocol/mod.rs index e6314dd7..4b08e7b1 100644 --- a/veilid-core/src/network_manager/wasm/protocol/mod.rs +++ b/veilid-core/src/network_manager/wasm/protocol/mod.rs @@ -17,7 +17,7 @@ impl ProtocolNetworkConnection { _local_address: Option, dial_info: &DialInfo, timeout_ms: u32, - address_filter: AddressFiltter, + address_filter: AddressFilter, ) -> io::Result> { if address_filter.is_punished(dial_info.address().to_ip_addr()) { return Ok(NetworkResult::no_connection_other("punished")); diff --git a/veilid-core/src/rpc_processor/coders/operations/operation_watch_value.rs b/veilid-core/src/rpc_processor/coders/operations/operation_watch_value.rs index d2aefa79..1ef425ab 100644 --- a/veilid-core/src/rpc_processor/coders/operations/operation_watch_value.rs +++ b/veilid-core/src/rpc_processor/coders/operations/operation_watch_value.rs @@ -22,7 +22,7 @@ impl RPCOperationWatchValueQ { watcher: PublicKey, signature: Signature, ) -> Result { - if subkeys.len() > MAX_WATCH_VALUE_Q_SUBKEYS_LEN { + if subkeys.len() as usize > MAX_WATCH_VALUE_Q_SUBKEYS_LEN { return Err(RPCError::protocol("WatchValueQ subkeys length too long")); } Ok(Self { @@ -38,7 +38,7 @@ impl RPCOperationWatchValueQ { // signature covers: key, subkeys, expiration, count, using watcher key fn make_signature_data(&self) -> Vec { let mut sig_data = - Vec::with_capacity(PUBLIC_KEY_LENGTH + 4 + (self.subkeys.len() * 8) + 8 + 4); + Vec::with_capacity(PUBLIC_KEY_LENGTH + 4 + (self.subkeys.len() as usize * 8) + 8 + 4); sig_data.extend_from_slice(&self.key.kind.0); sig_data.extend_from_slice(&self.key.value.bytes); for sk in self.subkeys.ranges() { diff --git a/veilid-core/tests/web.rs b/veilid-core/tests/web.rs index 65b3e291..caba3621 100644 --- a/veilid-core/tests/web.rs +++ b/veilid-core/tests/web.rs @@ -2,8 +2,9 @@ #![cfg(target_arch = "wasm32")] #![recursion_limit = "256"] +use cfg_if::*; +use parking_lot::Once; use veilid_core::tests::*; -use veilid_core::tools::*; use wasm_bindgen_test::*; wasm_bindgen_test_configure!(run_in_browser); diff --git a/veilid-tools/src/must_join_handle.rs b/veilid-tools/src/must_join_handle.rs index a4afd1dc..e2d1f530 100644 --- a/veilid-tools/src/must_join_handle.rs +++ b/veilid-tools/src/must_join_handle.rs @@ -23,8 +23,9 @@ impl MustJoinHandle { } else if #[cfg(feature="rt-tokio")] { self.join_handle = None; } else if #[cfg(target_arch = "wasm32")] { - self.join_handle.take().detach(); - self.completed = true; + if let Some(jh) = self.join_handle.take() { + jh.detach(); + } } else { compile_error!("needs executor implementation") } diff --git a/veilid-tools/src/timeout.rs b/veilid-tools/src/timeout.rs index 1ca2df26..5203c416 100644 --- a/veilid-tools/src/timeout.rs +++ b/veilid-tools/src/timeout.rs @@ -2,6 +2,7 @@ use super::*; cfg_if! { if #[cfg(target_arch = "wasm32")] { + use futures_util::future::{select, Either}; pub async fn timeout(dur_ms: u32, f: F) -> Result where diff --git a/veilid-tools/src/timestamp.rs b/veilid-tools/src/timestamp.rs index c47e0021..94f9871e 100644 --- a/veilid-tools/src/timestamp.rs +++ b/veilid-tools/src/timestamp.rs @@ -14,8 +14,9 @@ cfg_if! { pub fn debug_ts(ts: u64) -> String { if is_browser() { - let mut now = Date::now(); - let mut date = Date::new_0(); + let now = Date::new_0(); + now.set_time(Date::now()); + let date = Date::new_0(); date.set_time((ts / 1000u64) as f64); let show_year = now.get_utc_full_year() != date.get_utc_full_year(); diff --git a/veilid-tools/tests/web.rs b/veilid-tools/tests/web.rs index ae522f3e..e3997307 100644 --- a/veilid-tools/tests/web.rs +++ b/veilid-tools/tests/web.rs @@ -1,6 +1,8 @@ //! Test suite for the Web and headless browsers. #![cfg(target_arch = "wasm32")] +use cfg_if::*; +use parking_lot::Once; use veilid_tools::tests::*; use veilid_tools::*; diff --git a/veilid-wasm/src/lib.rs b/veilid-wasm/src/lib.rs index 5011fbe5..80455917 100644 --- a/veilid-wasm/src/lib.rs +++ b/veilid-wasm/src/lib.rs @@ -9,6 +9,7 @@ use alloc::sync::Arc; use alloc::*; use core::cell::RefCell; use core::fmt::Debug; +use core::sync::atomic::{AtomicBool, Ordering}; use futures_util::FutureExt; use gloo_utils::format::JsValueSerdeExt; use js_sys::*; @@ -185,54 +186,52 @@ pub fn initialize_veilid_wasm() { console_error_panic_hook::set_once(); } -static SETUP_ONCE: Once = Once::new(); +static INITIALIZED: AtomicBool = AtomicBool::new(false); #[wasm_bindgen()] pub fn initialize_veilid_core(platform_config: String) { - SETUP_ONCE.call_once(|| { - let platform_config: VeilidWASMConfig = veilid_core::deserialize_json(&platform_config) - .expect("failed to deserialize platform config json"); + if INITIALIZED.swap(true, Ordering::Relaxed) { + return; + } + let platform_config: VeilidWASMConfig = veilid_core::deserialize_json(&platform_config) + .expect("failed to deserialize platform config json"); - // Set up subscriber and layers - let subscriber = Registry::default(); - let mut layers = Vec::new(); - let mut filters = (*FILTERS).borrow_mut(); + // Set up subscriber and layers + let subscriber = Registry::default(); + let mut layers = Vec::new(); + let mut filters = (*FILTERS).borrow_mut(); - // Performance logger - if platform_config.logging.performance.enabled { - let filter = veilid_core::VeilidLayerFilter::new( - platform_config.logging.performance.level, - None, - ); - let layer = WASMLayer::new( - WASMLayerConfigBuilder::new() - .set_report_logs_in_timings(platform_config.logging.performance.logs_in_timings) - .set_console_config(if platform_config.logging.performance.logs_in_console { - ConsoleConfig::ReportWithConsoleColor - } else { - ConsoleConfig::NoReporting - }) - .build(), - ) - .with_filter(filter.clone()); - filters.insert("performance", filter); - layers.push(layer.boxed()); - }; + // Performance logger + if platform_config.logging.performance.enabled { + let filter = + veilid_core::VeilidLayerFilter::new(platform_config.logging.performance.level, None); + let layer = WASMLayer::new( + WASMLayerConfigBuilder::new() + .set_report_logs_in_timings(platform_config.logging.performance.logs_in_timings) + .set_console_config(if platform_config.logging.performance.logs_in_console { + ConsoleConfig::ReportWithConsoleColor + } else { + ConsoleConfig::NoReporting + }) + .build(), + ) + .with_filter(filter.clone()); + filters.insert("performance", filter); + layers.push(layer.boxed()); + }; - // API logger - if platform_config.logging.api.enabled { - let filter = - veilid_core::VeilidLayerFilter::new(platform_config.logging.api.level, None); - let layer = veilid_core::ApiTracingLayer::get().with_filter(filter.clone()); - filters.insert("api", filter); - layers.push(layer.boxed()); - } + // API logger + if platform_config.logging.api.enabled { + let filter = veilid_core::VeilidLayerFilter::new(platform_config.logging.api.level, None); + let layer = veilid_core::ApiTracingLayer::get().with_filter(filter.clone()); + filters.insert("api", filter); + layers.push(layer.boxed()); + } - let subscriber = subscriber.with(layers); - subscriber - .try_init() - .map_err(|e| format!("failed to initialize logging: {}", e)) - .expect("failed to initalize WASM platform"); - }); + let subscriber = subscriber.with(layers); + subscriber + .try_init() + .map_err(|e| format!("failed to initialize logging: {}", e)) + .expect("failed to initalize WASM platform"); } #[wasm_bindgen()] @@ -356,14 +355,15 @@ pub fn routing_context_with_privacy(id: u32) -> u32 { } #[wasm_bindgen()] -pub fn routing_context_with_custom_privacy(id: u32, stability: String) -> u32 { - let stability: veilid_core::Stability = veilid_core::deserialize_json(&stability).unwrap(); +pub fn routing_context_with_custom_privacy(id: u32, safety_selection: String) -> u32 { + let safety_selection: veilid_core::SafetySelection = + veilid_core::deserialize_json(&safety_selection).unwrap(); let rc = (*ROUTING_CONTEXTS).borrow(); let Some(routing_context) = rc.get(&id) else { return 0; }; - let Ok(routing_context) = routing_context.clone().with_custom_privacy(stability) else { + let Ok(routing_context) = routing_context.clone().with_custom_privacy(safety_selection) else { return 0; }; let new_id = add_routing_context(routing_context);