diff --git a/Cargo.lock b/Cargo.lock index 10f8799247e..5ff1a6cd8ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5615,7 +5615,6 @@ dependencies = [ "itertools 0.13.0", "jstraceable_derive", "keyboard-types", - "lazy_static", "libc", "log", "malloc_size_of", diff --git a/components/script/Cargo.toml b/components/script/Cargo.toml index 7dc5bba9e07..f21a9760b56 100644 --- a/components/script/Cargo.toml +++ b/components/script/Cargo.toml @@ -66,7 +66,6 @@ itertools = { workspace = true } js = { package = "mozjs", git = "https://github.com/servo/mozjs", features = ["streams"] } jstraceable_derive = { path = "../jstraceable_derive" } keyboard-types = { workspace = true } -lazy_static = { workspace = true } libc = { workspace = true } log = { workspace = true } malloc_size_of = { workspace = true } diff --git a/components/script/dom/bindings/str.rs b/components/script/dom/bindings/str.rs index 7ec868698c8..3489a2da617 100644 --- a/components/script/dom/bindings/str.rs +++ b/components/script/dom/bindings/str.rs @@ -9,13 +9,13 @@ use std::hash::{Hash, Hasher}; use std::marker::PhantomData; use std::ops::{Deref, DerefMut}; use std::str::FromStr; +use std::sync::LazyLock; use std::{fmt, ops, str}; use chrono::prelude::{Utc, Weekday}; use chrono::{Datelike, TimeZone}; use cssparser::CowRcStr; use html5ever::{LocalName, Namespace}; -use lazy_static::lazy_static; use num_traits::Zero; use regex::Regex; use servo_atoms::Atom; @@ -430,10 +430,10 @@ impl DOMString { /// pub fn is_valid_floating_point_number_string(&self) -> bool { - lazy_static! { - static ref RE: Regex = - Regex::new(r"^-?(?:\d+\.\d+|\d+|\.\d+)(?:(e|E)(\+|\-)?\d+)?$").unwrap(); - } + static RE: LazyLock = LazyLock::new(|| { + Regex::new(r"^-?(?:\d+\.\d+|\d+|\.\d+)(?:(e|E)(\+|\-)?\d+)?$").unwrap() + }); + RE.is_match(&self.0) && self.parse_floating_point_number().is_some() } @@ -537,13 +537,14 @@ impl DOMString { /// pub fn is_valid_email_address_string(&self) -> bool { - lazy_static! { - static ref RE: Regex = Regex::new(concat!( + static RE: LazyLock = LazyLock::new(|| { + Regex::new(concat!( r"^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?", r"(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$" )) - .unwrap(); - } + .unwrap() + }); + RE.is_match(&self.0) } diff --git a/components/script/dom/cssstyledeclaration.rs b/components/script/dom/cssstyledeclaration.rs index 0090bb54eb7..5777e0fe74b 100644 --- a/components/script/dom/cssstyledeclaration.rs +++ b/components/script/dom/cssstyledeclaration.rs @@ -3,10 +3,10 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use std::cmp::Ordering; +use std::sync::LazyLock; use dom_struct::dom_struct; use html5ever::local_name; -use lazy_static::lazy_static; use servo_arc::Arc; use servo_url::ServoUrl; use style::attr::AttrValue; @@ -344,35 +344,33 @@ impl CSSStyleDeclaration { } } -lazy_static! { - static ref ENABLED_LONGHAND_PROPERTIES: Vec = { - // The 'all' shorthand contains all the enabled longhands with 2 exceptions: - // 'direction' and 'unicode-bidi', so these must be added afterward. - let mut enabled_longhands: Vec = ShorthandId::All.longhands().collect(); - if PropertyId::NonCustom(LonghandId::Direction.into()).enabled_for_all_content() { - enabled_longhands.push(LonghandId::Direction); - } - if PropertyId::NonCustom(LonghandId::UnicodeBidi.into()).enabled_for_all_content() { - enabled_longhands.push(LonghandId::UnicodeBidi); - } +static ENABLED_LONGHAND_PROPERTIES: LazyLock> = LazyLock::new(|| { + // The 'all' shorthand contains all the enabled longhands with 2 exceptions: + // 'direction' and 'unicode-bidi', so these must be added afterward. + let mut enabled_longhands: Vec = ShorthandId::All.longhands().collect(); + if PropertyId::NonCustom(LonghandId::Direction.into()).enabled_for_all_content() { + enabled_longhands.push(LonghandId::Direction); + } + if PropertyId::NonCustom(LonghandId::UnicodeBidi.into()).enabled_for_all_content() { + enabled_longhands.push(LonghandId::UnicodeBidi); + } - // Sort lexicographically, but with vendor-prefixed properties after standard ones. - enabled_longhands.sort_unstable_by(|a, b| { - let a = a.name(); - let b = b.name(); - let is_a_vendor_prefixed = a.starts_with('-'); - let is_b_vendor_prefixed = b.starts_with('-'); - if is_a_vendor_prefixed == is_b_vendor_prefixed { - a.partial_cmp(b).unwrap() - } else if is_b_vendor_prefixed { - Ordering::Less - } else { - Ordering::Greater - } - }); - enabled_longhands - }; -} + // Sort lexicographically, but with vendor-prefixed properties after standard ones. + enabled_longhands.sort_unstable_by(|a, b| { + let a = a.name(); + let b = b.name(); + let is_a_vendor_prefixed = a.starts_with('-'); + let is_b_vendor_prefixed = b.starts_with('-'); + if is_a_vendor_prefixed == is_b_vendor_prefixed { + a.partial_cmp(b).unwrap() + } else if is_b_vendor_prefixed { + Ordering::Less + } else { + Ordering::Greater + } + }); + enabled_longhands +}); impl CSSStyleDeclarationMethods for CSSStyleDeclaration { // https://dev.w3.org/csswg/cssom/#dom-cssstyledeclaration-length diff --git a/components/script/dom/document.rs b/components/script/dom/document.rs index 5dfe624a587..bd4773439b3 100644 --- a/components/script/dom/document.rs +++ b/components/script/dom/document.rs @@ -11,6 +11,7 @@ use std::default::Default; use std::mem; use std::rc::Rc; use std::slice::from_ref; +use std::sync::LazyLock; use std::time::{Duration, Instant}; use base::id::BrowsingContextId; @@ -28,7 +29,6 @@ use hyper_serde::Serde; use ipc_channel::ipc::{self, IpcSender}; use js::rust::{HandleObject, HandleValue}; use keyboard_types::{Code, Key, KeyState}; -use lazy_static::lazy_static; use metrics::{ InteractiveFlag, InteractiveMetrics, InteractiveWindow, ProfilerMetadataFactory, ProgressiveWebMetric, @@ -3219,17 +3219,15 @@ impl Document { anchors: Default::default(), applets: Default::default(), style_shared_lock: { - lazy_static! { - /// Per-process shared lock for author-origin stylesheets - /// - /// FIXME: make it per-document or per-pipeline instead: - /// - /// (Need to figure out what to do with the style attribute - /// of elements adopted into another document.) - static ref PER_PROCESS_AUTHOR_SHARED_LOCK: StyleSharedRwLock = { - StyleSharedRwLock::new() - }; - } + /// Per-process shared lock for author-origin stylesheets + /// + /// FIXME: make it per-document or per-pipeline instead: + /// + /// (Need to figure out what to do with the style attribute + /// of elements adopted into another document.) + static PER_PROCESS_AUTHOR_SHARED_LOCK: LazyLock = + LazyLock::new(|| StyleSharedRwLock::new()); + PER_PROCESS_AUTHOR_SHARED_LOCK.clone() //StyleSharedRwLock::new() }, diff --git a/components/script/dom/htmlmetaelement.rs b/components/script/dom/htmlmetaelement.rs index 1cbd709ae1a..45673c55652 100644 --- a/components/script/dom/htmlmetaelement.rs +++ b/components/script/dom/htmlmetaelement.rs @@ -3,6 +3,7 @@ * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use std::str::FromStr; +use std::sync::LazyLock; use dom_struct::dom_struct; use html5ever::{LocalName, Prefix}; @@ -146,8 +147,8 @@ impl HTMLMetaElement { } // 2-11 - lazy_static::lazy_static! { - static ref REFRESH_REGEX: Regex = Regex::new( + static REFRESH_REGEX: LazyLock = LazyLock::new(|| { + Regex::new( r#"(?x) ^ \s* # 3 @@ -169,8 +170,9 @@ impl HTMLMetaElement { $ "#, ) - .unwrap(); - } + .unwrap() + }); + let mut url_record = document.url(); let captures = if let Some(captures) = REFRESH_REGEX.captures(content.as_bytes()) { captures diff --git a/components/script/dom/navigator.rs b/components/script/dom/navigator.rs index 83e59c3ea56..850a4d2e0b6 100644 --- a/components/script/dom/navigator.rs +++ b/components/script/dom/navigator.rs @@ -4,10 +4,10 @@ use std::cell::Cell; use std::convert::TryInto; +use std::sync::LazyLock; use dom_struct::dom_struct; use js::jsval::JSVal; -use lazy_static::lazy_static; use crate::dom::bindings::cell::DomRefCell; use crate::dom::bindings::codegen::Bindings::NavigatorBinding::NavigatorMethods; @@ -32,9 +32,8 @@ 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); - } + static CPUS: LazyLock = LazyLock::new(|| num_cpus::get().try_into().unwrap_or(1)); + *CPUS } diff --git a/components/script/script_runtime.rs b/components/script/script_runtime.rs index 9c9efe54017..c41b815e98a 100644 --- a/components/script/script_runtime.rs +++ b/components/script/script_runtime.rs @@ -47,7 +47,6 @@ use js::rust::{ describe_scripted_caller, Handle, HandleObject as RustHandleObject, IntoHandle, JSEngine, JSEngineHandle, ParentRuntime, Runtime as RustRuntime, }; -use lazy_static::lazy_static; use malloc_size_of::MallocSizeOfOps; use profile_traits::mem::{Report, ReportKind, ReportsChan}; use profile_traits::path; @@ -487,9 +486,7 @@ impl Drop for JSEngineSetup { } } -lazy_static! { - static ref JS_ENGINE: Mutex> = Mutex::new(None); -} +static JS_ENGINE: Mutex> = Mutex::new(None); #[allow(unsafe_code)] pub unsafe fn new_child_runtime( diff --git a/components/script/window_named_properties.rs b/components/script/window_named_properties.rs index f740b115011..88d3021ec6d 100644 --- a/components/script/window_named_properties.rs +++ b/components/script/window_named_properties.rs @@ -3,6 +3,7 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ use std::ptr; +use std::sync::LazyLock; use js::conversions::jsstr_to_string; use js::glue::{AppendToIdVector, CreateProxyHandler, NewProxyObject, ProxyTraps}; @@ -32,48 +33,48 @@ use crate::script_runtime::JSContext as SafeJSContext; struct SyncWrapper(*const libc::c_void); #[allow(unsafe_code)] unsafe impl Sync for SyncWrapper {} +#[allow(unsafe_code)] +unsafe impl Send for SyncWrapper {} -lazy_static::lazy_static! { - static ref HANDLER: SyncWrapper = { - let traps = ProxyTraps { - enter: None, - getOwnPropertyDescriptor: Some(get_own_property_descriptor), - defineProperty: Some(define_property), - ownPropertyKeys: Some(own_property_keys), - delete_: Some(delete), - enumerate: None, - getPrototypeIfOrdinary: Some(get_prototype_if_ordinary), - getPrototype: None, - setPrototype: None, - setImmutablePrototype: None, - preventExtensions: Some(prevent_extensions), - isExtensible: Some(is_extensible), - has: None, - get: None, - set: None, - call: None, - construct: None, - hasOwn: None, - getOwnEnumerablePropertyKeys: None, - nativeCall: None, - objectClassIs: None, - className: Some(class_name), - fun_toString: None, - boxedValue_unbox: None, - defaultValue: None, - trace: None, - finalize: None, - objectMoved: None, - isCallable: None, - isConstructor: None, - }; - - #[allow(unsafe_code)] - unsafe { - SyncWrapper(CreateProxyHandler(&traps, ptr::null())) - } +static HANDLER: LazyLock = LazyLock::new(|| { + let traps = ProxyTraps { + enter: None, + getOwnPropertyDescriptor: Some(get_own_property_descriptor), + defineProperty: Some(define_property), + ownPropertyKeys: Some(own_property_keys), + delete_: Some(delete), + enumerate: None, + getPrototypeIfOrdinary: Some(get_prototype_if_ordinary), + getPrototype: None, + setPrototype: None, + setImmutablePrototype: None, + preventExtensions: Some(prevent_extensions), + isExtensible: Some(is_extensible), + has: None, + get: None, + set: None, + call: None, + construct: None, + hasOwn: None, + getOwnEnumerablePropertyKeys: None, + nativeCall: None, + objectClassIs: None, + className: Some(class_name), + fun_toString: None, + boxedValue_unbox: None, + defaultValue: None, + trace: None, + finalize: None, + objectMoved: None, + isCallable: None, + isConstructor: None, }; -} + + #[allow(unsafe_code)] + unsafe { + SyncWrapper(CreateProxyHandler(&traps, ptr::null())) + } +}); #[allow(unsafe_code)] unsafe extern "C" fn get_own_property_descriptor(