script: implement navigator.hardwareConcurrency (#31268)

Signed-off-by: syvb <me@iter.ca>
This commit is contained in:
Smitty 2024-02-07 13:41:58 -05:00 committed by GitHub
parent 044b94d5eb
commit 20404a72c0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 32 additions and 40 deletions

View file

@ -2,8 +2,11 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::convert::TryInto;
use dom_struct::dom_struct;
use js::jsval::JSVal;
use lazy_static::lazy_static;
use crate::dom::bindings::codegen::Bindings::NavigatorBinding::NavigatorMethods;
use crate::dom::bindings::reflector::{reflect_dom_object, DomObject, Reflector};
@ -24,6 +27,13 @@ use crate::dom::window::Window;
use crate::dom::xrsystem::XRSystem;
use crate::script_runtime::JSContext;
pub(super) fn hardware_concurrency() -> u64 {
lazy_static! {
static ref CPUS: u64 = num_cpus::get().try_into().unwrap_or(1);
}
*CPUS
}
#[dom_struct]
pub struct Navigator {
reflector_: Reflector,
@ -206,4 +216,9 @@ impl NavigatorMethods for Navigator {
fn Gpu(&self) -> DomRoot<GPU> {
self.gpu.or_init(|| GPU::new(&self.global()))
}
/// <https://html.spec.whatwg.org/multipage/#dom-navigator-hardwareconcurrency>
fn HardwareConcurrency(&self) -> u64 {
hardware_concurrency()
}
}