Skip to content

Commit

Permalink
Backed out 2 changesets (bug 1832065) for glue.rs related bustages. C…
Browse files Browse the repository at this point in the history
…LOSED TREE

Backed out changeset 1260faa596e0 (bug 1832065)
Backed out changeset c64412c2e6fe (bug 1832065)
  • Loading branch information
Stanca Serban committed May 9, 2023
1 parent 7689dbe commit 8436b9c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 20 deletions.
5 changes: 3 additions & 2 deletions servo/components/style/gecko/arc_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::gecko_bindings::structs::{
RawServoMozDocumentRule, RawServoNamespaceRule, RawServoPageRule, RawServoStyleRule,
RawServoStyleSheetContents, RawServoSupportsRule, ServoCssRules,
};
use crate::gecko_bindings::sugar::ownership::{HasArcFFI, Strong};
use crate::gecko_bindings::sugar::ownership::{HasArcFFI, HasFFI, Strong};
use crate::media_queries::MediaList;
use crate::properties::animated_properties::AnimationValue;
use crate::properties::{ComputedValues, PropertyDeclarationBlock};
Expand All @@ -33,9 +33,10 @@ use std::{mem, ptr};

macro_rules! impl_arc_ffi {
($servo_type:ty => $gecko_type:ty[$addref:ident, $release:ident]) => {
unsafe impl HasArcFFI for $servo_type {
unsafe impl HasFFI for $servo_type {
type FFIType = $gecko_type;
}
unsafe impl HasArcFFI for $servo_type {}

#[no_mangle]
pub unsafe extern "C" fn $addref(obj: &$gecko_type) {
Expand Down
69 changes: 58 additions & 11 deletions servo/components/style/gecko_bindings/sugar/ownership.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,58 @@ use std::mem::{forget, transmute};
use std::ops::{Deref, DerefMut};
use std::ptr;

/// Indicates that a given Servo type has a corresponding Gecko FFI type.
pub unsafe trait HasFFI: Sized + 'static {
/// The corresponding Gecko type that this rust type represents.
///
/// See the examples in `components/style/gecko/conversions.rs`.
type FFIType: Sized;
}

/// Indicates that a given Servo type has the same layout as the corresponding
/// `HasFFI::FFIType` type.
pub unsafe trait HasSimpleFFI: HasFFI {
#[inline]
/// Given a Servo-side reference, converts it to an FFI-safe reference which
/// can be passed to Gecko.
///
/// &ServoType -> &GeckoType
fn as_ffi(&self) -> &Self::FFIType {
unsafe { transmute(self) }
}
#[inline]
/// Given a Servo-side mutable reference, converts it to an FFI-safe mutable
/// reference which can be passed to Gecko.
///
/// &mut ServoType -> &mut GeckoType
fn as_ffi_mut(&mut self) -> &mut Self::FFIType {
unsafe { transmute(self) }
}
#[inline]
/// Given an FFI-safe reference obtained from Gecko converts it to a
/// Servo-side reference.
///
/// &GeckoType -> &ServoType
fn from_ffi(ffi: &Self::FFIType) -> &Self {
unsafe { transmute(ffi) }
}
#[inline]
/// Given an FFI-safe mutable reference obtained from Gecko converts it to a
/// Servo-side mutable reference.
///
/// &mut GeckoType -> &mut ServoType
fn from_ffi_mut(ffi: &mut Self::FFIType) -> &mut Self {
unsafe { transmute(ffi) }
}
}

/// Helper trait for conversions between FFI Strong/Borrowed types and Arcs
///
/// Should be implemented by types which are passed over FFI as Arcs via Strong
/// and Borrowed.
///
/// In this case, the FFIType is the rough equivalent of ArcInner<Self>.
pub unsafe trait HasArcFFI: Sized + 'static {
/// The corresponding Gecko type that this rust type represents.
///
/// See the examples in `components/style/gecko/conversions.rs`.
type FFIType: Sized;

pub unsafe trait HasArcFFI: HasFFI {
// these methods can't be on Borrowed because it leads to an unspecified
// impl parameter
/// Artificially increments the refcount of a (possibly null) borrowed Arc
Expand Down Expand Up @@ -160,21 +200,26 @@ impl<GeckoType> Strong<GeckoType> {

/// A few helpers implemented on top of Arc<ServoType> to make it more
/// comfortable to use and write safe code with.
pub unsafe trait FFIArcHelpers<T: HasArcFFI> {
pub unsafe trait FFIArcHelpers {
/// The Rust FFI type that we're implementing methods for.
type Inner: HasArcFFI;

/// Converts an Arc into a strong FFI reference.
///
/// Arc<ServoType> -> Strong<GeckoType>
fn into_strong(self) -> Strong<T::FFIType>;
fn into_strong(self) -> Strong<<Self::Inner as HasFFI>::FFIType>;

/// Produces a borrowed FFI reference by borrowing an Arc.
///
/// &Arc<ServoType> -> &GeckoType
///
/// Then the `arc_as_borrowed` method can go away.
fn as_borrowed(&self) -> &T::FFIType;
fn as_borrowed(&self) -> &<Self::Inner as HasFFI>::FFIType;
}

unsafe impl<T: HasArcFFI> FFIArcHelpers<T> for RawOffsetArc<T> {
unsafe impl<T: HasArcFFI> FFIArcHelpers for RawOffsetArc<T> {
type Inner = T;

#[inline]
fn into_strong(self) -> Strong<T::FFIType> {
unsafe { transmute(self) }
Expand All @@ -186,7 +231,9 @@ unsafe impl<T: HasArcFFI> FFIArcHelpers<T> for RawOffsetArc<T> {
}
}

unsafe impl<T: HasArcFFI> FFIArcHelpers<T> for Arc<T> {
unsafe impl<T: HasArcFFI> FFIArcHelpers for Arc<T> {
type Inner = T;

#[inline]
fn into_strong(self) -> Strong<T::FFIType> {
Arc::into_raw_offset(self).into_strong()
Expand Down
14 changes: 7 additions & 7 deletions servo/ports/geckolib/glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ use style::gecko_bindings::structs::{
RawServoMozDocumentRule, RawServoNamespaceRule, RawServoPageRule, RawServoStyleSheetContents,
RawServoSupportsRule, ServoCssRules,
};
use style::gecko_bindings::sugar::ownership::{FFIArcHelpers, HasArcFFI, Strong};
use style::gecko_bindings::sugar::ownership::{FFIArcHelpers, HasArcFFI, HasFFI, Strong};
use style::gecko_bindings::sugar::refptr::RefPtr;
use style::global_style_data::{
GlobalStyleData, StyleThreadPool, GLOBAL_STYLE_DATA, STYLE_THREAD_POOL,
Expand Down Expand Up @@ -2070,7 +2070,7 @@ fn with_maybe_worker_shared_lock<R>(func: impl FnOnce(&SharedRwLock) -> R) -> R
}
}

fn read_locked_arc<T, R, F>(raw: &<Locked<T> as HasArcFFI>::FFIType, func: F) -> R
fn read_locked_arc<T, R, F>(raw: &<Locked<T> as HasFFI>::FFIType, func: F) -> R
where
Locked<T>: HasArcFFI,
F: FnOnce(&T) -> R,
Expand All @@ -2081,7 +2081,7 @@ where
func(Locked::<T>::as_arc(&raw).read_with(&guard))
}

fn read_locked_arc_worker<T, R, F>(raw: &<Locked<T> as HasArcFFI>::FFIType, func: F) -> R
fn read_locked_arc_worker<T, R, F>(raw: &<Locked<T> as HasFFI>::FFIType, func: F) -> R
where
Locked<T>: HasArcFFI,
F: FnOnce(&T) -> R,
Expand All @@ -2093,7 +2093,7 @@ where
}

#[cfg(debug_assertions)]
unsafe fn read_locked_arc_unchecked<T, R, F>(raw: &<Locked<T> as HasArcFFI>::FFIType, func: F) -> R
unsafe fn read_locked_arc_unchecked<T, R, F>(raw: &<Locked<T> as HasFFI>::FFIType, func: F) -> R
where
Locked<T>: HasArcFFI,
F: FnOnce(&T) -> R,
Expand All @@ -2103,7 +2103,7 @@ where
}

#[cfg(not(debug_assertions))]
unsafe fn read_locked_arc_unchecked<T, R, F>(raw: &<Locked<T>>::FFIType, func: F) -> R
unsafe fn read_locked_arc_unchecked<T, R, F>(raw: &<Locked<T> as HasFFI>::FFIType, func: F) -> R
where
Locked<T>: HasArcFFI,
F: FnOnce(&T) -> R,
Expand All @@ -2112,7 +2112,7 @@ where
func(Locked::<T>::as_arc(&raw).read_unchecked())
}

fn write_locked_arc<T, R, F>(raw: &<Locked<T> as HasArcFFI>::FFIType, func: F) -> R
fn write_locked_arc<T, R, F>(raw: &<Locked<T> as HasFFI>::FFIType, func: F) -> R
where
Locked<T>: HasArcFFI,
F: FnOnce(&mut T) -> R,
Expand All @@ -2123,7 +2123,7 @@ where
func(Locked::<T>::as_arc(&raw).write_with(&mut guard))
}

fn write_locked_arc_worker<T, R, F>(raw: &<Locked<T> as HasArcFFI>::FFIType, func: F) -> R
fn write_locked_arc_worker<T, R, F>(raw: &<Locked<T> as HasFFI>::FFIType, func: F) -> R
where
Locked<T>: HasArcFFI,
F: FnOnce(&mut T) -> R,
Expand Down

0 comments on commit 8436b9c

Please sign in to comment.