diff --git a/runtime/js/30_os.js b/runtime/js/30_os.js index 3c9768593b..ccce946a5f 100644 --- a/runtime/js/30_os.js +++ b/runtime/js/30_os.js @@ -25,8 +25,10 @@ return ops.op_os_release(); } - function osUptime() { - return ops.op_os_uptime(); + function createOsUptime(opFn) { + return function osUptime() { + return opFn(); + }; } function systemMemoryInfo() { @@ -110,7 +112,7 @@ loadavg, networkInterfaces, osRelease, - osUptime, + createOsUptime, setExitHandler, systemMemoryInfo, uid, diff --git a/runtime/js/99_main.js b/runtime/js/99_main.js index 901cb136fe..6ecf23ffff 100644 --- a/runtime/js/99_main.js +++ b/runtime/js/99_main.js @@ -483,6 +483,7 @@ delete Intl.v8BreakIterator; ops.op_node_unstable_net_listen_udp, ops.op_node_unstable_net_listen_unixpacket, ), + osUptime: __bootstrap.os.createOsUptime(ops.op_node_unstable_os_uptime), }, }); @@ -518,6 +519,7 @@ delete Intl.v8BreakIterator; ops.op_net_listen_udp, ops.op_net_listen_unixpacket, ), + osUptime: __bootstrap.os.createOsUptime(ops.op_os_uptime), }); } @@ -622,6 +624,7 @@ delete Intl.v8BreakIterator; ops.op_node_unstable_net_listen_udp, ops.op_node_unstable_net_listen_unixpacket, ), + osUptime: __bootstrap.os.createOsUptime(ops.op_node_unstable_os_uptime), }, }); @@ -649,6 +652,7 @@ delete Intl.v8BreakIterator; ops.op_net_listen_udp, ops.op_net_listen_unixpacket, ), + osUptime: __bootstrap.os.createOsUptime(ops.op_os_uptime), }); } ObjectDefineProperties(finalDenoNs, { diff --git a/runtime/ops/os/mod.rs b/runtime/ops/os/mod.rs index e82afbf7cd..f6f4761dfc 100644 --- a/runtime/ops/os/mod.rs +++ b/runtime/ops/os/mod.rs @@ -30,6 +30,7 @@ fn init_ops(builder: &mut ExtensionBuilder) -> &mut ExtensionBuilder { op_network_interfaces::decl(), op_os_release::decl(), op_os_uptime::decl(), + op_node_unstable_os_uptime::decl(), op_set_env::decl(), op_set_exit_code::decl(), op_system_memory_info::decl(), @@ -425,12 +426,21 @@ fn rss() -> usize { } } -#[op] -fn op_os_uptime(state: &mut OpState) -> Result { - super::check_unstable(state, "Deno.osUptime"); +fn os_uptime(state: &mut OpState) -> Result { state .borrow_mut::() .sys .check("osUptime", Some("Deno.osUptime()"))?; Ok(sys_info::os_uptime()) } + +#[op] +fn op_os_uptime(state: &mut OpState) -> Result { + super::check_unstable(state, "Deno.osUptime"); + os_uptime(state) +} + +#[op] +fn op_node_unstable_os_uptime(state: &mut OpState) -> Result { + os_uptime(state) +} diff --git a/runtime/ops/os/sys_info.rs b/runtime/ops/os/sys_info.rs index ecb40b9a04..91422546c8 100644 --- a/runtime/ops/os/sys_info.rs +++ b/runtime/ops/os/sys_info.rs @@ -302,6 +302,8 @@ pub fn mem_info() -> Option { } pub fn os_uptime() -> u64 { + let mut uptime: u64 = 0; + #[cfg(target_os = "linux")] { let mut info = std::mem::MaybeUninit::uninit(); @@ -310,7 +312,7 @@ pub fn os_uptime() -> u64 { if res == 0 { // SAFETY: `sysinfo` initializes the struct. let info = unsafe { info.assume_init() }; - return info.uptime as u64; + uptime = info.uptime as u64; } } @@ -340,7 +342,7 @@ pub fn os_uptime() -> u64 { ) }; if res == 0 { - return SystemTime::now() + uptime = SystemTime::now() .duration_since(SystemTime::UNIX_EPOCH) .map(|d| { (d - Duration::new( @@ -357,8 +359,8 @@ pub fn os_uptime() -> u64 { unsafe { // Windows is the only one that returns `uptime` in milisecond precision, // so we need to get the seconds out of it to be in sync with other envs. - return unsafe { winapi::um::sysinfoapi::GetTickCount64() as u64 / 1000 }; + uptime = winapi::um::sysinfoapi::GetTickCount64() as u64 / 1000; } - 0 + uptime }