From b5f16b9e05bfe4c8ba154c3e80a9f687adf73018 Mon Sep 17 00:00:00 2001 From: Kody Musick Date: Wed, 21 Jun 2023 16:28:16 -0600 Subject: [PATCH] Fixing issue with Cloudflare Workers when using now(): "LinkError: WebAssembly.Instance(): Import #9 module="./index_bg.js" function="__wbg_now_dc6f7ce2227b5592" error: function import requires a callable" Similar issue: https://github.com/rustwasm/gloo/pull/249 --- src/timestamp.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/timestamp.rs b/src/timestamp.rs index e605b63e..e5e2441d 100644 --- a/src/timestamp.rs +++ b/src/timestamp.rs @@ -262,17 +262,17 @@ pub(crate) const fn decode_unix_timestamp_millis(uuid: &Uuid) -> u64 { millis } -#[cfg(all(feature = "std", feature = "js", target = "wasm32-unknown-unknown"))] +#[cfg(all(feature = "std", feature = "js", all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown")))] fn now() -> (u64, u32) { use wasm_bindgen::prelude::*; #[wasm_bindgen] extern "C" { - #[wasm_bindgen(js_namespace = Date)] - fn now() -> f64; + #[wasm_bindgen(js_namespace = Date, catch)] + fn now() -> Result; } - let now = now(); + let now = now().unwrap_throw(); let secs = (now / 1_000.0) as u64; let nanos = ((now % 1_000.0) * 1_000_000.0) as u32; @@ -280,7 +280,7 @@ fn now() -> (u64, u32) { dbg!((secs, nanos)) } -#[cfg(all(feature = "std", any(not(feature = "js"), not(target = "wasm32-unknown-unknown"))))] +#[cfg(all(feature = "std", any(not(feature = "js"), not(all(target_arch = "wasm32", target_vendor = "unknown", target_os = "unknown")))))] fn now() -> (u64, u32) { let dur = std::time::SystemTime::UNIX_EPOCH .elapsed()