From efd6d108587e78f0b7d57c8660bf06681f7d133e Mon Sep 17 00:00:00 2001 From: Young-Flash Date: Tue, 7 Jan 2025 16:58:13 +0800 Subject: [PATCH] internal: add now ffi --- crates/moonrun/src/main.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/crates/moonrun/src/main.rs b/crates/moonrun/src/main.rs index c51a75ef..9c6d2aa7 100644 --- a/crates/moonrun/src/main.rs +++ b/crates/moonrun/src/main.rs @@ -39,6 +39,24 @@ struct PrintEnv { dangling_high_half: Cell>, } +fn now( + scope: &mut v8::HandleScope, + _args: v8::FunctionCallbackArguments, + mut ret: v8::ReturnValue, +) { + let result = v8::Array::new(scope, 1); + + let now = std::time::SystemTime::now(); + let duration = now + .duration_since(std::time::UNIX_EPOCH) + .expect("Time went backwards"); + + let secs = v8::Number::new(scope, duration.as_millis() as f64).into(); + result.set_index(scope, 0, secs).unwrap(); + + ret.set(result.into()); +} + fn instant_now( scope: &mut v8::HandleScope, mut args: v8::FunctionCallbackArguments, @@ -352,6 +370,10 @@ fn init_env(dtors: &mut Vec>, scope: &mut v8::HandleScope, args: &[ .build(scope) .unwrap(); obj.set(scope, identifier.into(), value.into()); + + let identifier = v8::String::new(scope, "now").unwrap(); + let value = v8::Function::builder(now).build(scope).unwrap(); + obj.set(scope, identifier.into(), value.into()); } // API for the fs module