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

@ -75,6 +75,7 @@ mime = { workspace = true }
mime_guess = { workspace = true }
msg = { workspace = true }
net_traits = { workspace = true }
num_cpus = { workspace = true }
num-traits = { workspace = true }
parking_lot = { workspace = true }
percent-encoding = { workspace = true }

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()
}
}

View file

@ -15,6 +15,7 @@ Navigator includes NavigatorLanguage;
Navigator includes NavigatorPlugins;
Navigator includes NavigatorCookies;
Navigator includes NavigatorGPU;
Navigator includes NavigatorConcurrentHardware;
// https://html.spec.whatwg.org/multipage/#navigatorid
[Exposed=(Window,Worker)]
@ -70,3 +71,8 @@ partial interface Navigator {
partial interface Navigator {
[Pref="dom.gamepad.enabled"] GamepadList getGamepads();
};
// https://html.spec.whatwg.org/multipage/#navigatorconcurrenthardware
interface mixin NavigatorConcurrentHardware {
readonly attribute unsigned long long hardwareConcurrency;
};

View file

@ -8,6 +8,7 @@ interface WorkerNavigator {};
WorkerNavigator includes NavigatorID;
WorkerNavigator includes NavigatorLanguage;
//WorkerNavigator includes NavigatorOnLine;
WorkerNavigator includes NavigatorConcurrentHardware;
// https://w3c.github.io/permissions/#navigator-and-workernavigator-extension

View file

@ -11,6 +11,7 @@ use crate::dom::bindings::root::{DomRoot, MutNullableDom};
use crate::dom::bindings::str::DOMString;
use crate::dom::bindings::utils::to_frozen_array;
use crate::dom::gpu::GPU;
use crate::dom::navigator::hardware_concurrency;
use crate::dom::navigatorinfo;
use crate::dom::permissions::Permissions;
use crate::dom::workerglobalscope::WorkerGlobalScope;
@ -110,4 +111,9 @@ impl WorkerNavigatorMethods for WorkerNavigator {
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()
}
}