1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-29 16:30:56 -05:00

fix(node): Add op_node_unstable_os_uptime to allow for node interop (#17208)

This commit is contained in:
Kamil Ogórek 2022-12-28 14:56:05 +01:00 committed by Bartek Iwańczuk
parent 5c9e7662d7
commit 203b5a4822
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
4 changed files with 28 additions and 10 deletions

View file

@ -25,8 +25,10 @@
return ops.op_os_release(); return ops.op_os_release();
} }
function osUptime() { function createOsUptime(opFn) {
return ops.op_os_uptime(); return function osUptime() {
return opFn();
};
} }
function systemMemoryInfo() { function systemMemoryInfo() {
@ -110,7 +112,7 @@
loadavg, loadavg,
networkInterfaces, networkInterfaces,
osRelease, osRelease,
osUptime, createOsUptime,
setExitHandler, setExitHandler,
systemMemoryInfo, systemMemoryInfo,
uid, uid,

View file

@ -483,6 +483,7 @@ delete Intl.v8BreakIterator;
ops.op_node_unstable_net_listen_udp, ops.op_node_unstable_net_listen_udp,
ops.op_node_unstable_net_listen_unixpacket, 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_udp,
ops.op_net_listen_unixpacket, 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_udp,
ops.op_node_unstable_net_listen_unixpacket, 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_udp,
ops.op_net_listen_unixpacket, ops.op_net_listen_unixpacket,
), ),
osUptime: __bootstrap.os.createOsUptime(ops.op_os_uptime),
}); });
} }
ObjectDefineProperties(finalDenoNs, { ObjectDefineProperties(finalDenoNs, {

View file

@ -30,6 +30,7 @@ fn init_ops(builder: &mut ExtensionBuilder) -> &mut ExtensionBuilder {
op_network_interfaces::decl(), op_network_interfaces::decl(),
op_os_release::decl(), op_os_release::decl(),
op_os_uptime::decl(), op_os_uptime::decl(),
op_node_unstable_os_uptime::decl(),
op_set_env::decl(), op_set_env::decl(),
op_set_exit_code::decl(), op_set_exit_code::decl(),
op_system_memory_info::decl(), op_system_memory_info::decl(),
@ -425,12 +426,21 @@ fn rss() -> usize {
} }
} }
#[op] fn os_uptime(state: &mut OpState) -> Result<u64, AnyError> {
fn op_os_uptime(state: &mut OpState) -> Result<u64, AnyError> {
super::check_unstable(state, "Deno.osUptime");
state state
.borrow_mut::<Permissions>() .borrow_mut::<Permissions>()
.sys .sys
.check("osUptime", Some("Deno.osUptime()"))?; .check("osUptime", Some("Deno.osUptime()"))?;
Ok(sys_info::os_uptime()) Ok(sys_info::os_uptime())
} }
#[op]
fn op_os_uptime(state: &mut OpState) -> Result<u64, AnyError> {
super::check_unstable(state, "Deno.osUptime");
os_uptime(state)
}
#[op]
fn op_node_unstable_os_uptime(state: &mut OpState) -> Result<u64, AnyError> {
os_uptime(state)
}

View file

@ -302,6 +302,8 @@ pub fn mem_info() -> Option<MemInfo> {
} }
pub fn os_uptime() -> u64 { pub fn os_uptime() -> u64 {
let mut uptime: u64 = 0;
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
let mut info = std::mem::MaybeUninit::uninit(); let mut info = std::mem::MaybeUninit::uninit();
@ -310,7 +312,7 @@ pub fn os_uptime() -> u64 {
if res == 0 { if res == 0 {
// SAFETY: `sysinfo` initializes the struct. // SAFETY: `sysinfo` initializes the struct.
let info = unsafe { info.assume_init() }; 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 { if res == 0 {
return SystemTime::now() uptime = SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH) .duration_since(SystemTime::UNIX_EPOCH)
.map(|d| { .map(|d| {
(d - Duration::new( (d - Duration::new(
@ -357,8 +359,8 @@ pub fn os_uptime() -> u64 {
unsafe { unsafe {
// Windows is the only one that returns `uptime` in milisecond precision, // 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. // 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
} }