From 20404a72c0f068771a04e492d3343d4d6ad2ecf3 Mon Sep 17 00:00:00 2001 From: Smitty Date: Wed, 7 Feb 2024 13:41:58 -0500 Subject: [PATCH] script: implement navigator.hardwareConcurrency (#31268) Signed-off-by: syvb --- Cargo.lock | 1 + Cargo.toml | 1 + components/config/Cargo.toml | 2 +- components/script/Cargo.toml | 1 + components/script/dom/navigator.rs | 15 +++++++++++++++ components/script/dom/webidls/Navigator.webidl | 6 ++++++ .../script/dom/webidls/WorkerNavigator.webidl | 1 + components/script/dom/workernavigator.rs | 6 ++++++ .../html/dom/idlharness.https.html.ini | 6 ------ .../html/dom/idlharness.worker.js.ini | 6 ------ ...orkerNavigator-hardware-concurrency.any.js.ini | 11 ----------- tests/wpt/meta/html/dom/idlharness.https.html.ini | 6 ------ tests/wpt/meta/html/dom/idlharness.worker.js.ini | 6 ------ ...orkerNavigator-hardware-concurrency.any.js.ini | 4 ---- 14 files changed, 32 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 824320daa69..3a2e1b88b9a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4895,6 +4895,7 @@ dependencies = [ "msg", "net_traits", "num-traits", + "num_cpus", "parking_lot", "percent-encoding", "phf", diff --git a/Cargo.toml b/Cargo.toml index 90fbe54debd..04653f83f9c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -71,6 +71,7 @@ mime_guess = "2.0.3" mozangle = "0.5.0" msg = { path = "components/shared/msg" } net_traits = { path = "components/shared/net" } +num_cpus = { workspace = true } num-traits = "0.2" parking_lot = "0.12" percent-encoding = "2.3" diff --git a/components/config/Cargo.toml b/components/config/Cargo.toml index c18b865b7d3..c2d78d246f4 100644 --- a/components/config/Cargo.toml +++ b/components/config/Cargo.toml @@ -16,7 +16,7 @@ euclid = { workspace = true } getopts = { workspace = true } lazy_static = { workspace = true } log = { workspace = true } -num_cpus = "1.1.0" +num_cpus = { workspace = true } serde = { workspace = true, features = ["derive"] } serde_json = { workspace = true } servo_config_plugins = { path = "../config_plugins" } diff --git a/components/script/Cargo.toml b/components/script/Cargo.toml index a92489538ae..8a79e406ebe 100644 --- a/components/script/Cargo.toml +++ b/components/script/Cargo.toml @@ -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 } diff --git a/components/script/dom/navigator.rs b/components/script/dom/navigator.rs index cf3928b8fed..612d2e68ff4 100644 --- a/components/script/dom/navigator.rs +++ b/components/script/dom/navigator.rs @@ -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 { self.gpu.or_init(|| GPU::new(&self.global())) } + + /// + fn HardwareConcurrency(&self) -> u64 { + hardware_concurrency() + } } diff --git a/components/script/dom/webidls/Navigator.webidl b/components/script/dom/webidls/Navigator.webidl index bb9a0e77564..aea9a16e725 100644 --- a/components/script/dom/webidls/Navigator.webidl +++ b/components/script/dom/webidls/Navigator.webidl @@ -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; +}; diff --git a/components/script/dom/webidls/WorkerNavigator.webidl b/components/script/dom/webidls/WorkerNavigator.webidl index 5be43960802..1e03611d523 100644 --- a/components/script/dom/webidls/WorkerNavigator.webidl +++ b/components/script/dom/webidls/WorkerNavigator.webidl @@ -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 diff --git a/components/script/dom/workernavigator.rs b/components/script/dom/workernavigator.rs index ba7da3fa783..aae6a59f733 100644 --- a/components/script/dom/workernavigator.rs +++ b/components/script/dom/workernavigator.rs @@ -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 { self.gpu.or_init(|| GPU::new(&self.global())) } + + /// + fn HardwareConcurrency(&self) -> u64 { + hardware_concurrency() + } } diff --git a/tests/wpt/meta-legacy-layout/html/dom/idlharness.https.html.ini b/tests/wpt/meta-legacy-layout/html/dom/idlharness.https.html.ini index 494b2532d8a..78a4fda91be 100644 --- a/tests/wpt/meta-legacy-layout/html/dom/idlharness.https.html.ini +++ b/tests/wpt/meta-legacy-layout/html/dom/idlharness.https.html.ini @@ -143,9 +143,6 @@ [ApplicationCache interface: existence and properties of interface prototype object's "constructor" property] expected: FAIL - [Navigator interface: attribute hardwareConcurrency] - expected: FAIL - [OffscreenCanvasRenderingContext2D interface: operation restore()] expected: FAIL @@ -812,9 +809,6 @@ [DragEvent interface: existence and properties of interface object] expected: FAIL - [Navigator interface: window.navigator must inherit property "hardwareConcurrency" with the proper type] - expected: FAIL - [ApplicationCache interface: operation update()] expected: FAIL diff --git a/tests/wpt/meta-legacy-layout/html/dom/idlharness.worker.js.ini b/tests/wpt/meta-legacy-layout/html/dom/idlharness.worker.js.ini index 5da6b68d32c..74417a5ec31 100644 --- a/tests/wpt/meta-legacy-layout/html/dom/idlharness.worker.js.ini +++ b/tests/wpt/meta-legacy-layout/html/dom/idlharness.worker.js.ini @@ -266,9 +266,6 @@ [OffscreenCanvasRenderingContext2D interface: attribute lineDashOffset] expected: FAIL - [WorkerNavigator interface: self.navigator must inherit property "hardwareConcurrency" with the proper type] - expected: FAIL - [ImageBitmapRenderingContext interface object name] expected: FAIL @@ -386,9 +383,6 @@ [OffscreenCanvas interface: operation transferToImageBitmap()] expected: FAIL - [WorkerNavigator interface: attribute hardwareConcurrency] - expected: FAIL - [OffscreenCanvasRenderingContext2D interface: operation arcTo(unrestricted double, unrestricted double, unrestricted double, unrestricted double, unrestricted double)] expected: FAIL diff --git a/tests/wpt/meta-legacy-layout/workers/WorkerNavigator-hardware-concurrency.any.js.ini b/tests/wpt/meta-legacy-layout/workers/WorkerNavigator-hardware-concurrency.any.js.ini index b7d71a85ff9..c4606fc0efa 100644 --- a/tests/wpt/meta-legacy-layout/workers/WorkerNavigator-hardware-concurrency.any.js.ini +++ b/tests/wpt/meta-legacy-layout/workers/WorkerNavigator-hardware-concurrency.any.js.ini @@ -1,16 +1,5 @@ [WorkerNavigator-hardware-concurrency.any.sharedworker.html] expected: ERROR - [WorkerNavigator-hardware-concurrency] - expected: FAIL - - -[WorkerNavigator-hardware-concurrency.any.worker.html] - [Test worker navigator hardware concurrency.] - expected: FAIL - [WorkerNavigator-hardware-concurrency.any.serviceworker.html] expected: ERROR - [WorkerNavigator-hardware-concurrency] - expected: FAIL - diff --git a/tests/wpt/meta/html/dom/idlharness.https.html.ini b/tests/wpt/meta/html/dom/idlharness.https.html.ini index fb3aa9bd4e0..eb3d5b62b5a 100644 --- a/tests/wpt/meta/html/dom/idlharness.https.html.ini +++ b/tests/wpt/meta/html/dom/idlharness.https.html.ini @@ -443,9 +443,6 @@ [CanvasRenderingContext2D interface: calling isPointInStroke(Path2D, unrestricted double, unrestricted double) on document.createElement("canvas").getContext("2d") with too few arguments must throw TypeError] expected: FAIL - [Navigator interface: attribute hardwareConcurrency] - expected: FAIL - [ApplicationCache interface: constant CHECKING on interface object] expected: FAIL @@ -809,9 +806,6 @@ [DragEvent interface: existence and properties of interface object] expected: FAIL - [Navigator interface: window.navigator must inherit property "hardwareConcurrency" with the proper type] - expected: FAIL - [ElementInternals interface: existence and properties of interface prototype object's @@unscopables property] expected: FAIL diff --git a/tests/wpt/meta/html/dom/idlharness.worker.js.ini b/tests/wpt/meta/html/dom/idlharness.worker.js.ini index 23f20a6d996..af14091370e 100644 --- a/tests/wpt/meta/html/dom/idlharness.worker.js.ini +++ b/tests/wpt/meta/html/dom/idlharness.worker.js.ini @@ -266,9 +266,6 @@ [OffscreenCanvasRenderingContext2D interface: attribute lineDashOffset] expected: FAIL - [WorkerNavigator interface: self.navigator must inherit property "hardwareConcurrency" with the proper type] - expected: FAIL - [ImageBitmapRenderingContext interface object name] expected: FAIL @@ -398,9 +395,6 @@ [OffscreenCanvas interface: operation transferToImageBitmap()] expected: FAIL - [WorkerNavigator interface: attribute hardwareConcurrency] - expected: FAIL - [OffscreenCanvasRenderingContext2D interface: operation arcTo(unrestricted double, unrestricted double, unrestricted double, unrestricted double, unrestricted double)] expected: FAIL diff --git a/tests/wpt/meta/workers/WorkerNavigator-hardware-concurrency.any.js.ini b/tests/wpt/meta/workers/WorkerNavigator-hardware-concurrency.any.js.ini index 081fbfcfc4c..c4606fc0efa 100644 --- a/tests/wpt/meta/workers/WorkerNavigator-hardware-concurrency.any.js.ini +++ b/tests/wpt/meta/workers/WorkerNavigator-hardware-concurrency.any.js.ini @@ -3,7 +3,3 @@ [WorkerNavigator-hardware-concurrency.any.serviceworker.html] expected: ERROR - -[WorkerNavigator-hardware-concurrency.any.worker.html] - [Test worker navigator hardware concurrency.] - expected: FAIL