Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal: add now ffi #553

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading