Impl Clock trait in bindings

This commit is contained in:
Avinash Sajjanshetty
2025-04-06 23:09:00 +05:30
parent 2873c36b31
commit 3543e83b91
3 changed files with 35 additions and 15 deletions

View File

@@ -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!();
}
}

View File

@@ -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 {

View File

@@ -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()
}
}