use crate::jsutils::JsError;
use crate::quickjs_utils;
use crate::quickjs_utils::{functions, objects, primitives};
use crate::quickjsrealmadapter::QuickJsRealmAdapter;
use crate::quickjsvalueadapter::QuickJsValueAdapter;
use libquickjs_sys as q;
pub fn new_date_q(context: &QuickJsRealmAdapter) -> Result<QuickJsValueAdapter, JsError> {
unsafe { new_date(context.context) }
}
pub unsafe fn new_date(context: *mut q::JSContext) -> Result<QuickJsValueAdapter, JsError> {
let constructor = quickjs_utils::get_constructor(context, "Date")?;
let date_ref = functions::call_constructor(context, &constructor, &[])?;
Ok(date_ref)
}
pub fn is_date_q(context: &QuickJsRealmAdapter, obj_ref: &QuickJsValueAdapter) -> bool {
unsafe { is_date(context.context, obj_ref) }
}
pub unsafe fn is_date(context: *mut q::JSContext, obj_ref: &QuickJsValueAdapter) -> bool {
objects::is_instance_of_by_name(context, obj_ref, "Date").unwrap_or(false)
}
pub fn set_time_q(
context: &QuickJsRealmAdapter,
date_ref: &QuickJsValueAdapter,
timestamp: f64,
) -> Result<(), JsError> {
unsafe { set_time(context.context, date_ref, timestamp) }
}
pub unsafe fn set_time(
context: *mut q::JSContext,
date_ref: &QuickJsValueAdapter,
timestamp: f64,
) -> Result<(), JsError> {
functions::invoke_member_function(
context,
date_ref,
"setTime",
&[primitives::from_f64(timestamp)],
)?;
Ok(())
}
pub fn get_time_q(
context: &QuickJsRealmAdapter,
date_ref: &QuickJsValueAdapter,
) -> Result<f64, JsError> {
unsafe { get_time(context.context, date_ref) }
}
pub unsafe fn get_time(
context: *mut q::JSContext,
date_ref: &QuickJsValueAdapter,
) -> Result<f64, JsError> {
let time_ref = functions::invoke_member_function(context, date_ref, "getTime", &[])?;
if time_ref.is_f64() {
primitives::to_f64(&time_ref)
} else {
primitives::to_i32(&time_ref).map(|i| i as f64)
}
}
#[cfg(test)]
pub mod tests {
use crate::facades::tests::init_test_rt;
use crate::quickjs_utils::dates;
use crate::quickjs_utils::dates::{get_time_q, is_date_q, set_time_q};
#[test]
fn test_date() {
let rt = init_test_rt();
rt.exe_rt_task_in_event_loop(|q_js_rt| {
let q_ctx = q_js_rt.get_main_realm();
let date_ref = dates::new_date_q(q_ctx).expect("new_date failed");
assert!(is_date_q(q_ctx, &date_ref));
set_time_q(q_ctx, &date_ref, 2147483648f64).expect("could not set time");
let gt_res = get_time_q(q_ctx, &date_ref);
match gt_res {
Ok(t) => {
assert_eq!(t, 2147483648f64);
}
Err(e) => {
panic!("get time failed: {}", e);
}
}
set_time_q(q_ctx, &date_ref, 2f64).expect("could not set time");
let gt_res = get_time_q(q_ctx, &date_ref);
match gt_res {
Ok(t) => {
assert_eq!(t, 2f64);
}
Err(e) => {
panic!("get time 2 failed: {}", e);
}
}
});
}
}