From c30ad5a30e10b9aabe5842624db24b9846c3d5d4 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Sat, 29 Mar 2025 04:11:27 -0400 Subject: Miscellaneous script splitting preparation changes (#36216) * script: Move num module to script_bindings. Signed-off-by: Josh Matthews * script: Make JS reflector creation generic over DOM trait. Signed-off-by: Josh Matthews * script: Move bindings-specific lock to script_bindings. Signed-off-by: Josh Matthews * script: Move DOM proto array code to script_bindings. Signed-off-by: Josh Matthews * script: Move finalizer implementations to script_bindings. Signed-off-by: Josh Matthews * script: Move some error routines to script_bindings. Signed-off-by: Josh Matthews * script: Move some DOM interface conversion routines to script_bindings. Signed-off-by: Josh Matthews * script: Make is_array_like generic over DOM trait. Signed-off-by: Josh Matthews * script: Use generic interfaces for conditional exposure functions. Signed-off-by: Josh Matthews * script: Move a bunch of routines used by codegen to script_bindings. Signed-off-by: Josh Matthews * Formatting. Signed-off-by: Josh Matthews * Fix clippy warnings. Signed-off-by: Josh Matthews --------- Signed-off-by: Josh Matthews --- components/script_bindings/lock.rs | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 components/script_bindings/lock.rs (limited to 'components/script_bindings/lock.rs') diff --git a/components/script_bindings/lock.rs b/components/script_bindings/lock.rs new file mode 100644 index 00000000000..e8cde2e4087 --- /dev/null +++ b/components/script_bindings/lock.rs @@ -0,0 +1,37 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * 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::sync::OnceLock; + +/// A OnceLock wrapping a type that is not considered threadsafe by the Rust compiler, but +/// will be used in a threadsafe manner (it will not be mutated, after being initialized). +/// +/// This is needed to allow using JS API types (which usually involve raw pointers) in static initializers, +/// when Servo guarantees through the use of OnceLock that only one thread will ever initialize +/// the value. +pub struct ThreadUnsafeOnceLock(OnceLock); + +impl ThreadUnsafeOnceLock { + #[allow(clippy::new_without_default)] + pub const fn new() -> Self { + Self(OnceLock::new()) + } + + /// Initialize the value inside this lock. Panics if the lock has been previously initialized. + pub fn set(&self, val: T) { + assert!(self.0.set(val).is_ok()); + } + + /// Get a reference to the value inside this lock. Panics if the lock has not been initialized. + /// + /// # Safety + /// The caller must ensure that it does not mutate value contained inside this lock + /// (using interior mutability). + pub unsafe fn get(&self) -> &T { + self.0.get().unwrap() + } +} + +unsafe impl Sync for ThreadUnsafeOnceLock {} +unsafe impl Send for ThreadUnsafeOnceLock {} -- cgit v1.2.3