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

1
Cargo.lock generated
View file

@ -4895,6 +4895,7 @@ dependencies = [
"msg",
"net_traits",
"num-traits",
"num_cpus",
"parking_lot",
"percent-encoding",
"phf",

View file

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

View file

@ -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" }

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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