diff --git a/ports/geckolib/binding_tools/regen.py b/ports/geckolib/binding_tools/regen.py index 770c756dedc..1e767ff1701 100755 --- a/ports/geckolib/binding_tools/regen.py +++ b/ports/geckolib/binding_tools/regen.py @@ -153,6 +153,12 @@ COMPILATION_TARGETS = { "servo_arc_types": [ "ServoComputedValues", "RawServoStyleSheet", "ServoDeclarationBlock" + ], + "servo_borrowed_types": [ + "RawServoStyleSet", + ], + "servo_borrowed_mut_types": [ + "RawServoStyleSet", ] }, @@ -325,6 +331,18 @@ def build(objdir, target_name, debug, debugger, kind_name=None, flags.append("{}Borrowed".format(ty)) flags.append("--raw-line") flags.append("pub type {0}Borrowed<'a> = ::sugar::refptr::Borrowed<'a, {0}>;".format(ty)) + if "servo_borrowed_types" in current_target: + for ty in current_target["servo_borrowed_types"]: + flags.append("-blacklist-type") + flags.append("{}Borrowed".format(ty)) + flags.append("-raw-line") + flags.append("pub type {0}Borrowed<'a> = &'a {0};".format(ty)) + if "servo_borrowed_mut_types" in current_target: + for ty in current_target["servo_borrowed_mut_types"]: + flags.append("-blacklist-type") + flags.append("{}BorrowedMut".format(ty)) + flags.append("-raw-line") + flags.append("pub type {0}BorrowedMut<'a> = &'a mut {0};".format(ty)) if "structs_types" in current_target: for ty in current_target["structs_types"]: ty_fragments = ty.split("::") diff --git a/ports/geckolib/data.rs b/ports/geckolib/data.rs index d1abe67f9bd..769e8b7e79d 100644 --- a/ports/geckolib/data.rs +++ b/ports/geckolib/data.rs @@ -4,6 +4,7 @@ use euclid::size::TypedSize2D; use gecko_bindings::bindings::RawServoStyleSet; +use gecko_bindings::sugar::refptr::{HasSimpleFFI, HasFFI}; use num_cpus; use std::cmp; use std::collections::HashMap; @@ -77,10 +78,6 @@ impl PerDocumentStyleData { } } - pub fn borrow_mut_from_raw<'a>(data: *mut RawServoStyleSet) -> &'a mut Self { - unsafe { &mut *(data as *mut PerDocumentStyleData) } - } - pub fn flush_stylesheets(&mut self) { // The stylist wants to be flushed if either the stylesheets change or the // device dimensions change. When we add support for media queries, we'll @@ -93,6 +90,11 @@ impl PerDocumentStyleData { } } +unsafe impl HasFFI for PerDocumentStyleData { + type FFIType = RawServoStyleSet; +} +unsafe impl HasSimpleFFI for PerDocumentStyleData {} + impl Drop for PerDocumentStyleData { fn drop(&mut self) { if let Some(ref mut queue) = self.work_queue { diff --git a/ports/geckolib/gecko_bindings/bindings.rs b/ports/geckolib/gecko_bindings/bindings.rs index 4a45f41d046..26bf82e6894 100644 --- a/ports/geckolib/gecko_bindings/bindings.rs +++ b/ports/geckolib/gecko_bindings/bindings.rs @@ -11,6 +11,8 @@ pub type RawServoStyleSheetStrong = ::sugar::refptr::Strong; pub type RawServoStyleSheetBorrowed<'a> = ::sugar::refptr::Borrowed<'a, RawServoStyleSheet>; pub type ServoDeclarationBlockStrong = ::sugar::refptr::Strong; pub type ServoDeclarationBlockBorrowed<'a> = ::sugar::refptr::Borrowed<'a, ServoDeclarationBlock>; +pub type RawServoStyleSetBorrowed<'a> = &'a RawServoStyleSet; +pub type RawServoStyleSetBorrowedMut<'a> = &'a mut RawServoStyleSet; use structs::nsStyleFont; unsafe impl Send for nsStyleFont {} unsafe impl Sync for nsStyleFont {} @@ -474,12 +476,12 @@ extern "C" { pub fn Servo_StyleSet_Drop(set: *mut RawServoStyleSet); pub fn Servo_StyleSet_AppendStyleSheet(set: *mut RawServoStyleSet, sheet: RawServoStyleSheetBorrowed); - pub fn Servo_StyleSet_PrependStyleSheet(set: *mut RawServoStyleSet, + pub fn Servo_StyleSet_PrependStyleSheet(set: RawServoStyleSetBorrowedMut, sheet: RawServoStyleSheetBorrowed); - pub fn Servo_StyleSet_RemoveStyleSheet(set: *mut RawServoStyleSet, + pub fn Servo_StyleSet_RemoveStyleSheet(set: RawServoStyleSetBorrowedMut, sheet: RawServoStyleSheetBorrowed); - pub fn Servo_StyleSet_InsertStyleSheetBefore(set: *mut RawServoStyleSet, + pub fn Servo_StyleSet_InsertStyleSheetBefore(set: RawServoStyleSetBorrowedMut, sheet: RawServoStyleSheetBorrowed, reference: @@ -505,7 +507,7 @@ extern "C" { pub fn Servo_ComputedValues_GetForAnonymousBox(parent_style_or_null: ServoComputedValuesBorrowed, pseudoTag: *mut nsIAtom, - set: *mut RawServoStyleSet) + set: RawServoStyleSetBorrowedMut) -> ServoComputedValuesStrong; pub fn Servo_ComputedValues_GetForPseudoElement(parent_style: ServoComputedValuesBorrowed, @@ -513,7 +515,7 @@ extern "C" { *mut RawGeckoElement, pseudo_tag: *mut nsIAtom, set: - *mut RawServoStyleSet, + RawServoStyleSetBorrowedMut, is_probe: bool) -> ServoComputedValuesStrong; pub fn Servo_ComputedValues_Inherit(parent_style: @@ -527,12 +529,12 @@ extern "C" { pub fn Servo_Shutdown(); pub fn Servo_ComputeRestyleHint(element: *mut RawGeckoElement, snapshot: *mut ServoElementSnapshot, - set: *mut RawServoStyleSet) + set: RawServoStyleSetBorrowedMut) -> nsRestyleHint; pub fn Servo_RestyleDocument(doc: *mut RawGeckoDocument, - set: *mut RawServoStyleSet); + set: RawServoStyleSetBorrowedMut); pub fn Servo_RestyleSubtree(node: *mut RawGeckoNode, - set: *mut RawServoStyleSet); + set: RawServoStyleSetBorrowedMut); pub fn Servo_GetStyleFont(computed_values: ServoComputedValuesBorrowed) -> *const nsStyleFont; pub fn Servo_GetStyleColor(computed_values: ServoComputedValuesBorrowed) diff --git a/ports/geckolib/gecko_bindings/sugar/refptr.rs b/ports/geckolib/gecko_bindings/sugar/refptr.rs index faa47190c6b..a70ad3d20ad 100644 --- a/ports/geckolib/gecko_bindings/sugar/refptr.rs +++ b/ports/geckolib/gecko_bindings/sugar/refptr.rs @@ -15,10 +15,29 @@ pub unsafe trait HasFFI : Sized { type FFIType: Sized; } +/// Indicates that a given Servo type has the same layout +/// as the corresponding HasFFI::FFIType type +pub unsafe trait HasSimpleFFI : HasFFI { + fn as_ffi(&self) -> &Self::FFIType { + unsafe { transmute(self) } + } + fn as_ffi_mut(&mut self) -> &mut Self::FFIType { + unsafe { transmute(self) } + } + fn from_ffi(ffi: &Self::FFIType) -> &Self { + unsafe { transmute(ffi) } + } + 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 pub unsafe trait HasArcFFI : HasFFI { // these methods can't be on Borrowed because it leads to an unspecified // impl parameter @@ -112,7 +131,7 @@ pub unsafe trait FFIArcHelpers { unsafe impl FFIArcHelpers for Arc { type Inner = T; fn into_strong(self) -> Strong { - unsafe {transmute(self)} + unsafe { transmute(self) } } fn as_borrowed(&self) -> Borrowed { let borrowedptr = self as *const Arc as *const Borrowed; diff --git a/ports/geckolib/glue.rs b/ports/geckolib/glue.rs index fdbb2f95b65..404a1e6fe22 100644 --- a/ports/geckolib/glue.rs +++ b/ports/geckolib/glue.rs @@ -8,8 +8,8 @@ use app_units::Au; use data::{NUM_THREADS, PerDocumentStyleData}; use env_logger; use euclid::Size2D; -use gecko_bindings::bindings::RawServoStyleSet; use gecko_bindings::bindings::{RawGeckoDocument, RawGeckoElement, RawGeckoNode}; +use gecko_bindings::bindings::{RawServoStyleSet, RawServoStyleSetBorrowedMut}; use gecko_bindings::bindings::{RawServoStyleSheetBorrowed, ServoComputedValuesBorrowed}; use gecko_bindings::bindings::{RawServoStyleSheetStrong, ServoComputedValuesStrong}; use gecko_bindings::bindings::{ServoDeclarationBlock, ServoNodeData, ThreadSafePrincipalHolder}; @@ -19,7 +19,7 @@ use gecko_bindings::ptr::{GeckoArcPrincipal, GeckoArcURI}; use gecko_bindings::structs::ServoElementSnapshot; use gecko_bindings::structs::nsRestyleHint; use gecko_bindings::structs::{SheetParsingMode, nsIAtom}; -use gecko_bindings::sugar::refptr::{FFIArcHelpers, HasArcFFI, HasFFI, Strong}; +use gecko_bindings::sugar::refptr::{FFIArcHelpers, HasArcFFI, HasSimpleFFI, HasFFI, Strong}; use gecko_string_cache::Atom; use snapshot::GeckoElementSnapshot; use std::mem::transmute; @@ -69,7 +69,7 @@ pub extern "C" fn Servo_Shutdown() -> () { unsafe { ComputedValues::shutdown(); } } -fn restyle_subtree(node: GeckoNode, raw_data: *mut RawServoStyleSet) { +fn restyle_subtree(node: GeckoNode, raw_data: RawServoStyleSetBorrowedMut) { debug_assert!(node.is_element() || node.is_text_node()); // Force the creation of our lazily-constructed initial computed values on @@ -82,7 +82,7 @@ fn restyle_subtree(node: GeckoNode, raw_data: *mut RawServoStyleSet) { ComputedValues::initial_values(); // The stylist consumes stylesheets lazily. - let per_doc_data = unsafe { &mut *(raw_data as *mut PerDocumentStyleData) }; + let per_doc_data = PerDocumentStyleData::from_ffi_mut(raw_data); per_doc_data.flush_stylesheets(); let local_context_data = @@ -113,13 +113,13 @@ fn restyle_subtree(node: GeckoNode, raw_data: *mut RawServoStyleSet) { #[no_mangle] pub extern "C" fn Servo_RestyleSubtree(node: *mut RawGeckoNode, - raw_data: *mut RawServoStyleSet) -> () { + raw_data: RawServoStyleSetBorrowedMut) -> () { let node = unsafe { GeckoNode::from_raw(node) }; restyle_subtree(node, raw_data); } #[no_mangle] -pub extern "C" fn Servo_RestyleDocument(doc: *mut RawGeckoDocument, raw_data: *mut RawServoStyleSet) -> () { +pub extern "C" fn Servo_RestyleDocument(doc: *mut RawGeckoDocument, raw_data: RawServoStyleSetBorrowedMut) -> () { let document = unsafe { GeckoDocument::from_raw(doc) }; let node = match document.root_node() { Some(x) => x, @@ -173,9 +173,9 @@ pub extern "C" fn Servo_StyleSheet_FromUTF8Bytes(bytes: *const u8, } #[no_mangle] -pub extern "C" fn Servo_StyleSet_AppendStyleSheet(raw_data: *mut RawServoStyleSet, +pub extern "C" fn Servo_StyleSet_AppendStyleSheet(raw_data: RawServoStyleSetBorrowedMut, raw_sheet: RawServoStyleSheetBorrowed) { - let data = PerDocumentStyleData::borrow_mut_from_raw(raw_data); + let data = PerDocumentStyleData::from_ffi_mut(raw_data); let sheet = raw_sheet.as_arc(); data.stylesheets.retain(|x| !arc_ptr_eq(x, sheet)); data.stylesheets.push(sheet.clone()); @@ -183,9 +183,9 @@ pub extern "C" fn Servo_StyleSet_AppendStyleSheet(raw_data: *mut RawServoStyleSe } #[no_mangle] -pub extern "C" fn Servo_StyleSet_PrependStyleSheet(raw_data: *mut RawServoStyleSet, +pub extern "C" fn Servo_StyleSet_PrependStyleSheet(raw_data: RawServoStyleSetBorrowedMut, raw_sheet: RawServoStyleSheetBorrowed) { - let data = PerDocumentStyleData::borrow_mut_from_raw(raw_data); + let data = PerDocumentStyleData::from_ffi_mut(raw_data); let sheet = raw_sheet.as_arc(); data.stylesheets.retain(|x| !arc_ptr_eq(x, sheet)); data.stylesheets.insert(0, sheet.clone()); @@ -193,10 +193,10 @@ pub extern "C" fn Servo_StyleSet_PrependStyleSheet(raw_data: *mut RawServoStyleS } #[no_mangle] -pub extern "C" fn Servo_StyleSet_InsertStyleSheetBefore(raw_data: *mut RawServoStyleSet, +pub extern "C" fn Servo_StyleSet_InsertStyleSheetBefore(raw_data: RawServoStyleSetBorrowedMut, raw_sheet: RawServoStyleSheetBorrowed, raw_reference: RawServoStyleSheetBorrowed) { - let data = PerDocumentStyleData::borrow_mut_from_raw(raw_data); + let data = PerDocumentStyleData::from_ffi_mut(raw_data); let sheet = raw_sheet.as_arc(); let reference = raw_reference.as_arc(); data.stylesheets.retain(|x| !arc_ptr_eq(x, sheet)); @@ -206,9 +206,9 @@ pub extern "C" fn Servo_StyleSet_InsertStyleSheetBefore(raw_data: *mut RawServoS } #[no_mangle] -pub extern "C" fn Servo_StyleSet_RemoveStyleSheet(raw_data: *mut RawServoStyleSet, +pub extern "C" fn Servo_StyleSet_RemoveStyleSheet(raw_data: RawServoStyleSetBorrowedMut, raw_sheet: RawServoStyleSheetBorrowed) { - let data = PerDocumentStyleData::borrow_mut_from_raw(raw_data); + let data = PerDocumentStyleData::from_ffi_mut(raw_data); let sheet = raw_sheet.as_arc(); data.stylesheets.retain(|x| !arc_ptr_eq(x, sheet)); data.stylesheets_changed = true; @@ -250,10 +250,10 @@ pub extern "C" fn Servo_ComputedValues_Get(node: *mut RawGeckoNode) #[no_mangle] pub extern "C" fn Servo_ComputedValues_GetForAnonymousBox(parent_style_or_null: ServoComputedValuesBorrowed, pseudo_tag: *mut nsIAtom, - raw_data: *mut RawServoStyleSet) + raw_data: RawServoStyleSetBorrowedMut) -> ServoComputedValuesStrong { // The stylist consumes stylesheets lazily. - let data = PerDocumentStyleData::borrow_mut_from_raw(raw_data); + let data = PerDocumentStyleData::from_ffi_mut(raw_data); data.flush_stylesheets(); let atom = Atom::from(pseudo_tag); @@ -269,7 +269,7 @@ pub extern "C" fn Servo_ComputedValues_GetForAnonymousBox(parent_style_or_null: pub extern "C" fn Servo_ComputedValues_GetForPseudoElement(parent_style: ServoComputedValuesBorrowed, match_element: *mut RawGeckoElement, pseudo_tag: *mut nsIAtom, - raw_data: *mut RawServoStyleSet, + raw_data: RawServoStyleSetBorrowedMut, is_probe: bool) -> ServoComputedValuesStrong { debug_assert!(!match_element.is_null()); @@ -286,7 +286,7 @@ pub extern "C" fn Servo_ComputedValues_GetForPseudoElement(parent_style: ServoCo let pseudo = PseudoElement::from_atom_unchecked(atom, /* anon_box = */ false); // The stylist consumes stylesheets lazily. - let data = PerDocumentStyleData::borrow_mut_from_raw(raw_data); + let data = PerDocumentStyleData::from_ffi_mut(raw_data); data.flush_stylesheets(); let element = unsafe { GeckoElement::from_raw(match_element) };