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 GitHub
parent 8bdf66c59c
commit 65ea554afe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 28 additions and 10 deletions

View File

@ -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,

View File

@ -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, {

View File

@ -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<u64, AnyError> {
super::check_unstable(state, "Deno.osUptime");
fn os_uptime(state: &mut OpState) -> Result<u64, AnyError> {
state
.borrow_mut::<Permissions>()
.sys
.check("osUptime", Some("Deno.osUptime()"))?;
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 {
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
}