Skip to content

Commit

Permalink
internal: add now ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
Young-Flash committed Jan 7, 2025
1 parent 18652f2 commit efd6d10
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions crates/moonrun/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,24 @@ struct PrintEnv {
dangling_high_half: Cell<Option<u32>>,
}

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,
Expand Down Expand Up @@ -352,6 +370,10 @@ fn init_env(dtors: &mut Vec<Box<dyn Any>>, 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
Expand Down

0 comments on commit efd6d10

Please sign in to comment.