From 3543e83b91d9be0739f6808857fcfeaf289b01a8 Mon Sep 17 00:00:00 2001 From: Avinash Sajjanshetty Date: Sun, 6 Apr 2025 23:09:00 +0530 Subject: [PATCH] Impl Clock trait in bindings --- bindings/javascript/src/lib.rs | 11 +++++++---- bindings/wasm/lib.rs | 24 ++++++++++++++++++------ simulator/runner/io.rs | 15 ++++++++++----- 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/bindings/javascript/src/lib.rs b/bindings/javascript/src/lib.rs index 614b17677..a9c0d72a5 100644 --- a/bindings/javascript/src/lib.rs +++ b/bindings/javascript/src/lib.rs @@ -4,6 +4,7 @@ use std::cell::RefCell; use std::rc::Rc; use std::sync::Arc; +use limbo_core::{Clock, Instant}; use napi::{Env, JsUnknown, Result as NapiResult}; use napi_derive::napi; @@ -152,6 +153,12 @@ impl limbo_core::DatabaseStorage for DatabaseFile { struct IO {} +impl Clock for IO { + fn now(&self) -> Instant { + todo!() + } +} + impl limbo_core::IO for IO { fn open_file( &self, @@ -169,8 +176,4 @@ impl limbo_core::IO for IO { fn generate_random_number(&self) -> i64 { todo!(); } - - fn get_current_time(&self) -> String { - todo!(); - } } diff --git a/bindings/wasm/lib.rs b/bindings/wasm/lib.rs index 3a5819efc..91680dc96 100644 --- a/bindings/wasm/lib.rs +++ b/bindings/wasm/lib.rs @@ -1,5 +1,7 @@ use js_sys::{Array, Object}; -use limbo_core::{maybe_init_database_file, OpenFlags, Pager, Result, WalFileShared}; +use limbo_core::{ + maybe_init_database_file, Clock, Instant, OpenFlags, Pager, Result, WalFileShared, +}; use std::cell::RefCell; use std::rc::Rc; use std::sync::Arc; @@ -269,6 +271,18 @@ pub struct PlatformIO { unsafe impl Send for PlatformIO {} unsafe impl Sync for PlatformIO {} +impl Clock for PlatformIO { + fn now(&self) -> Instant { + let date = Date::new(); + let ms_since_epoch = date.getTime(); + + Instant { + secs: (ms_since_epoch / 1000.0) as i64, + micros: ((ms_since_epoch % 1000.0) * 1000.0) as u32, + } + } +} + impl limbo_core::IO for PlatformIO { fn open_file( &self, @@ -291,11 +305,6 @@ impl limbo_core::IO for PlatformIO { let random_f64 = Math_random(); (random_f64 * i64::MAX as f64) as i64 } - - fn get_current_time(&self) -> String { - let date = Date::new(); - date.toISOString() - } } #[wasm_bindgen] @@ -312,6 +321,9 @@ extern "C" { #[wasm_bindgen(method, getter)] fn toISOString(this: &Date) -> String; + + #[wasm_bindgen(method)] + fn getTime(this: &Date) -> f64; } pub struct DatabaseFile { diff --git a/simulator/runner/io.rs b/simulator/runner/io.rs index 48340d170..d1c280b4e 100644 --- a/simulator/runner/io.rs +++ b/simulator/runner/io.rs @@ -1,6 +1,6 @@ use std::{cell::RefCell, sync::Arc}; -use limbo_core::{OpenFlags, PlatformIO, Result, IO}; +use limbo_core::{Clock, Instant, OpenFlags, PlatformIO, Result, IO}; use rand::{RngCore, SeedableRng}; use rand_chacha::ChaCha8Rng; @@ -52,6 +52,15 @@ impl SimulatorIO { } } +impl Clock for SimulatorIO { + fn now(&self) -> Instant { + Instant { + secs: 1704067200, // 2024-01-01 00:00:00 UTC + micros: 0, + } + } +} + impl IO for SimulatorIO { fn open_file( &self, @@ -88,8 +97,4 @@ impl IO for SimulatorIO { fn generate_random_number(&self) -> i64 { self.rng.borrow_mut().next_u64() as i64 } - - fn get_current_time(&self) -> String { - "2024-01-01 00:00:00".to_string() - } }