Skip to content

Commit f5503e2

Browse files
committed
Review fixes and bindings resync
1 parent d0acc6c commit f5503e2

File tree

5 files changed

+105
-37
lines changed

5 files changed

+105
-37
lines changed

ports/geckolib/binding_tools/regen.py

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@
164164
"RawGeckoNode",
165165
"RawGeckoElement",
166166
"RawGeckoDocument",
167+
"StyleChildrenIterator",
167168
],
168169
},
169170

@@ -283,6 +284,22 @@ def build(objdir, target_name, debug, debugger, kind_name=None,
283284

284285
flags = []
285286

287+
# This makes an FFI-safe void type that can't be matched on
288+
# &VoidType is UB to have, because you can match on it
289+
# to produce a reachable unreachable. If it's wrapped in
290+
# a struct as a private field it becomes okay again
291+
#
292+
# Not 100% sure of how safe this is, but it's what we're using
293+
# in the XPCOM ffi too
294+
# https://github.com/nikomatsakis/rust-memory-model/issues/2
295+
def zero_size_type(ty, flags):
296+
flags.append("--blacklist-type")
297+
flags.append(ty)
298+
flags.append("--raw-line")
299+
flags.append("pub enum {0}Void{{ }}".format(ty))
300+
flags.append("--raw-line")
301+
flags.append("pub struct {0}({0}Void);".format(ty))
302+
286303
if "flags" in current_target:
287304
flags.extend(current_target["flags"])
288305

@@ -334,18 +351,20 @@ def build(objdir, target_name, debug, debugger, kind_name=None,
334351
flags.append("pub type {0}Strong = ::sugar::ownership::Strong<{0}>;".format(ty))
335352
flags.append("--blacklist-type")
336353
flags.append("{}MaybeBorrowed".format(ty))
337-
flags.append("-raw-line")
354+
flags.append("--raw-line")
338355
flags.append("pub type {0}MaybeBorrowed<'a> = ::sugar::ownership::Borrowed<'a, {0}>;".format(ty))
356+
zero_size_type(ty, flags)
339357
if "servo_immutable_borrow_types" in current_target:
340358
for ty in current_target["servo_immutable_borrow_types"]:
341-
flags.append("-blacklist-type")
359+
flags.append("--blacklist-type")
342360
flags.append("{}Borrowed".format(ty))
343361
flags.append("--raw-line")
344362
flags.append("pub type {0}Borrowed<'a> = &'a {0};".format(ty))
345363
flags.append("--blacklist-type")
346364
flags.append("{}MaybeBorrowed".format(ty))
347365
flags.append("--raw-line")
348366
flags.append("pub type {0}MaybeBorrowed<'a> = ::sugar::ownership::Borrowed<'a, {0}>;".format(ty))
367+
zero_size_type(ty, flags)
349368
if "servo_owned_types" in current_target:
350369
for ty in current_target["servo_owned_types"]:
351370
flags.append("--blacklist-type")
@@ -360,6 +379,7 @@ def build(objdir, target_name, debug, debugger, kind_name=None,
360379
flags.append("{}Owned".format(ty))
361380
flags.append("--raw-line")
362381
flags.append("pub type {0}Owned = ::sugar::ownership::Owned<{0}>;".format(ty))
382+
zero_size_type(ty, flags)
363383
if "servo_maybe_owned_types" in current_target:
364384
for ty in current_target["servo_maybe_owned_types"]:
365385
flags.append("--blacklist-type")
@@ -376,6 +396,7 @@ def build(objdir, target_name, debug, debugger, kind_name=None,
376396
flags.append("{}MaybeOwned".format(ty))
377397
flags.append("--raw-line")
378398
flags.append("pub type {0}MaybeOwned = ::sugar::ownership::MaybeOwned<{0}>;".format(ty))
399+
zero_size_type(ty, flags)
379400
if "structs_types" in current_target:
380401
for ty in current_target["structs_types"]:
381402
ty_fragments = ty.split("::")

ports/geckolib/gecko_bindings/bindings.rs

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,42 @@ pub enum nsIPrincipal {}
77
pub enum nsIURI {}
88
pub type ServoComputedValuesStrong = ::sugar::ownership::Strong<ServoComputedValues>;
99
pub type ServoComputedValuesMaybeBorrowed<'a> = ::sugar::ownership::Borrowed<'a, ServoComputedValues>;
10+
pub enum ServoComputedValuesVoid{ }
11+
pub struct ServoComputedValues(ServoComputedValuesVoid);
1012
pub type RawServoStyleSheetStrong = ::sugar::ownership::Strong<RawServoStyleSheet>;
1113
pub type RawServoStyleSheetMaybeBorrowed<'a> = ::sugar::ownership::Borrowed<'a, RawServoStyleSheet>;
14+
pub enum RawServoStyleSheetVoid{ }
15+
pub struct RawServoStyleSheet(RawServoStyleSheetVoid);
1216
pub type ServoDeclarationBlockStrong = ::sugar::ownership::Strong<ServoDeclarationBlock>;
1317
pub type ServoDeclarationBlockMaybeBorrowed<'a> = ::sugar::ownership::Borrowed<'a, ServoDeclarationBlock>;
18+
pub enum ServoDeclarationBlockVoid{ }
19+
pub struct ServoDeclarationBlock(ServoDeclarationBlockVoid);
1420
pub type RawGeckoNodeBorrowed<'a> = &'a RawGeckoNode;
1521
pub type RawGeckoNodeMaybeBorrowed<'a> = ::sugar::ownership::Borrowed<'a, RawGeckoNode>;
22+
pub enum RawGeckoNodeVoid{ }
23+
pub struct RawGeckoNode(RawGeckoNodeVoid);
1624
pub type RawGeckoElementBorrowed<'a> = &'a RawGeckoElement;
1725
pub type RawGeckoElementMaybeBorrowed<'a> = ::sugar::ownership::Borrowed<'a, RawGeckoElement>;
26+
pub enum RawGeckoElementVoid{ }
27+
pub struct RawGeckoElement(RawGeckoElementVoid);
1828
pub type RawGeckoDocumentBorrowed<'a> = &'a RawGeckoDocument;
1929
pub type RawGeckoDocumentMaybeBorrowed<'a> = ::sugar::ownership::Borrowed<'a, RawGeckoDocument>;
30+
pub enum RawGeckoDocumentVoid{ }
31+
pub struct RawGeckoDocument(RawGeckoDocumentVoid);
32+
pub type StyleChildrenIteratorBorrowed<'a> = &'a StyleChildrenIterator;
33+
pub type StyleChildrenIteratorMaybeBorrowed<'a> = ::sugar::ownership::Borrowed<'a, StyleChildrenIterator>;
34+
pub enum StyleChildrenIteratorVoid{ }
35+
pub struct StyleChildrenIterator(StyleChildrenIteratorVoid);
2036
pub type RawServoStyleSetBorrowed<'a> = &'a RawServoStyleSet;
2137
pub type RawServoStyleSetBorrowedMut<'a> = &'a mut RawServoStyleSet;
2238
pub type RawServoStyleSetOwned = ::sugar::ownership::Owned<RawServoStyleSet>;
39+
pub enum RawServoStyleSetVoid{ }
40+
pub struct RawServoStyleSet(RawServoStyleSetVoid);
2341
pub type ServoNodeDataMaybeBorrowed<'a> = ::sugar::ownership::Borrowed<'a, ServoNodeData>;
2442
pub type ServoNodeDataMaybeBorrowedMut<'a> = ::sugar::ownership::BorrowedMut<'a, ServoNodeData>;
2543
pub type ServoNodeDataMaybeOwned = ::sugar::ownership::MaybeOwned<ServoNodeData>;
44+
pub enum ServoNodeDataVoid{ }
45+
pub struct ServoNodeData(ServoNodeDataVoid);
2646
use structs::nsStyleFont;
2747
unsafe impl Send for nsStyleFont {}
2848
unsafe impl Sync for nsStyleFont {}
@@ -171,17 +191,9 @@ use structs::StyleBasicShapeType;
171191
use structs::StyleBasicShape;
172192
use structs::nsCSSShadowArray;
173193

174-
pub type RawGeckoNode = nsINode;
175194
pub enum Element { }
176-
pub type RawGeckoElement = Element;
177-
pub type RawGeckoDocument = nsIDocument;
178-
pub enum ServoNodeData { }
179-
pub enum ServoComputedValues { }
180-
pub enum RawServoStyleSheet { }
181-
pub enum RawServoStyleSet { }
182195
pub enum nsHTMLCSSStyleSheet { }
183-
pub enum ServoDeclarationBlock { }
184-
pub enum StyleChildrenIterator { }
196+
pub type StyleChildrenIteratorBorrowedMut = *mut StyleChildrenIterator;
185197
pub type ThreadSafePrincipalHolder = nsMainThreadPtrHolder<nsIPrincipal>;
186198
pub type ThreadSafeURIHolder = nsMainThreadPtrHolder<nsIURI>;
187199
extern "C" {
@@ -210,12 +222,12 @@ extern "C" {
210222
pub fn Gecko_GetDocumentElement(document: RawGeckoDocumentBorrowed)
211223
-> RawGeckoElementMaybeBorrowed;
212224
pub fn Gecko_MaybeCreateStyleChildrenIterator(node: RawGeckoNodeBorrowed)
213-
-> *mut StyleChildrenIterator;
225+
-> StyleChildrenIteratorMaybeBorrowed;
214226
pub fn Gecko_DropStyleChildrenIterator(it: *mut StyleChildrenIterator);
215-
pub fn Gecko_GetNextStyleChild(it: *mut StyleChildrenIterator)
216-
-> *mut RawGeckoNode;
217-
pub fn Gecko_ElementState(element: *mut RawGeckoElement) -> u8;
218-
pub fn Gecko_IsHTMLElementInHTMLDocument(element: *mut RawGeckoElement)
227+
pub fn Gecko_GetNextStyleChild(it: StyleChildrenIteratorBorrowed)
228+
-> RawGeckoNodeMaybeBorrowed;
229+
pub fn Gecko_ElementState(element: RawGeckoElementBorrowed) -> u8;
230+
pub fn Gecko_IsHTMLElementInHTMLDocument(element: RawGeckoElementBorrowed)
219231
-> bool;
220232
pub fn Gecko_IsLink(element: RawGeckoElementBorrowed) -> bool;
221233
pub fn Gecko_IsTextNode(node: RawGeckoNodeBorrowed) -> bool;
@@ -494,13 +506,16 @@ extern "C" {
494506
pub fn Servo_StyleSet_Init() -> RawServoStyleSetOwned;
495507
pub fn Servo_StyleSet_Drop(set: RawServoStyleSetOwned);
496508
pub fn Servo_StyleSet_AppendStyleSheet(set: RawServoStyleSetBorrowedMut,
497-
sheet: RawServoStyleSheetMaybeBorrowed);
509+
sheet:
510+
RawServoStyleSheetMaybeBorrowed);
498511
pub fn Servo_StyleSet_PrependStyleSheet(set: RawServoStyleSetBorrowedMut,
499512
sheet:
500513
RawServoStyleSheetMaybeBorrowed);
501514
pub fn Servo_StyleSet_RemoveStyleSheet(set: RawServoStyleSetBorrowedMut,
502-
sheet: RawServoStyleSheetMaybeBorrowed);
503-
pub fn Servo_StyleSet_InsertStyleSheetBefore(set: RawServoStyleSetBorrowedMut,
515+
sheet:
516+
RawServoStyleSheetMaybeBorrowed);
517+
pub fn Servo_StyleSet_InsertStyleSheetBefore(set:
518+
RawServoStyleSetBorrowedMut,
504519
sheet:
505520
RawServoStyleSheetMaybeBorrowed,
506521
reference:
@@ -526,12 +541,13 @@ extern "C" {
526541
pub fn Servo_ComputedValues_GetForAnonymousBox(parent_style_or_null:
527542
ServoComputedValuesMaybeBorrowed,
528543
pseudoTag: *mut nsIAtom,
529-
set: RawServoStyleSetBorrowedMut)
544+
set:
545+
RawServoStyleSetBorrowedMut)
530546
-> ServoComputedValuesStrong;
531547
pub fn Servo_ComputedValues_GetForPseudoElement(parent_style:
532548
ServoComputedValuesMaybeBorrowed,
533549
match_element:
534-
*mut RawGeckoElement,
550+
RawGeckoElementBorrowed,
535551
pseudo_tag: *mut nsIAtom,
536552
set:
537553
RawServoStyleSetBorrowedMut,

ports/geckolib/gecko_bindings/sugar/ownership.rs

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
44

55
use std::marker::PhantomData;
6-
use std::mem::{forget, transmute, transmute_copy};
6+
use std::mem::{forget, transmute};
77
use std::ops::{Deref, DerefMut};
88
use std::ptr;
99
use std::sync::Arc;
@@ -73,7 +73,7 @@ pub unsafe trait HasArcFFI : HasFFI {
7373
// impl parameter
7474
/// Artificially increments the refcount of a borrowed Arc over FFI.
7575
unsafe fn addref(ptr: Borrowed<Self::FFIType>) {
76-
forget(ptr.as_arc::<Self>().clone())
76+
forget(ptr.as_arc_opt::<Self>().clone())
7777
}
7878

7979
/// Given a (possibly null) borrowed FFI reference, decrements the refcount.
@@ -113,7 +113,7 @@ impl<'a, T> Clone for Borrowed<'a, T> {
113113

114114
impl<'a, T> Borrowed<'a, T> {
115115
#[inline]
116-
pub fn is_null(&self) -> bool {
116+
pub fn is_null(self) -> bool {
117117
self.ptr == ptr::null()
118118
}
119119

@@ -150,7 +150,7 @@ impl<'a, T> Borrowed<'a, T> {
150150

151151
#[inline]
152152
/// Borrowed<ServoType> -> Borrowed<GeckoType>
153-
pub fn as_ffi(&self) -> Borrowed<<Self as HasFFI>::FFIType> where Self: HasSimpleFFI {
153+
pub fn as_ffi(self) -> Borrowed<'a, <Self as HasFFI>::FFIType> where Self: HasSimpleFFI {
154154
unsafe { transmute(self) }
155155
}
156156

@@ -165,6 +165,13 @@ impl<'a, T> Borrowed<'a, T> {
165165
pub fn as_servo_ref<U>(self) -> Option<&'a U> where U: HasSimpleFFI<FFIType = T> {
166166
self.borrow_opt().map(HasSimpleFFI::from_ffi)
167167
}
168+
169+
pub fn null() -> Borrowed<'static, T> {
170+
Borrowed {
171+
ptr: ptr::null_mut(),
172+
_marker: PhantomData
173+
}
174+
}
168175
}
169176

170177
impl<'a, T> BorrowedMut<'a, T> {
@@ -196,9 +203,17 @@ impl<'a, T> BorrowedMut<'a, T> {
196203
pub fn as_servo_mut_ref<U>(self) -> Option<&'a mut U> where U: HasSimpleFFI<FFIType = T> {
197204
self.borrow_mut_opt().map(HasSimpleFFI::from_ffi_mut)
198205
}
206+
207+
pub fn null_mut() -> BorrowedMut<'static, T> {
208+
BorrowedMut {
209+
ptr: ptr::null_mut(),
210+
_marker: PhantomData
211+
}
212+
}
199213
}
200214

201-
// technically not
215+
// technically not how we're supposed to use
216+
// Deref, but that's a minor style issue
202217
impl<'a, T> Deref for BorrowedMut<'a, T> {
203218
type Target = Borrowed<'a, T>;
204219
fn deref(&self) -> &Self::Target {
@@ -209,6 +224,8 @@ impl<'a, T> Deref for BorrowedMut<'a, T> {
209224
#[repr(C)]
210225
/// Gecko-FFI-safe Arc (T is an ArcInner).
211226
/// This can be null.
227+
/// Leaks on drop. Please don't drop this.
228+
/// TODO: Add destructor bomb once drop flags are gone
212229
pub struct Strong<T> {
213230
ptr: *const T,
214231
_marker: PhantomData<T>,
@@ -227,13 +244,26 @@ impl<T> Strong<T> {
227244
///
228245
/// Strong<GeckoType> -> Arc<ServoType>
229246
pub fn into_arc<U>(self) -> Arc<U> where U: HasArcFFI<FFIType = T> {
230-
assert!(!self.is_null());
231-
unsafe { transmute(self) }
247+
self.into_arc_opt().unwrap()
248+
}
249+
250+
#[inline]
251+
/// Given a strong FFI reference,
252+
/// converts it into a servo-side Arc
253+
/// Returns None on null.
254+
///
255+
/// Strong<GeckoType> -> Arc<ServoType>
256+
pub fn into_arc_opt<U>(self) -> Option<Arc<U>> where U: HasArcFFI<FFIType = T> {
257+
if self.is_null() {
258+
None
259+
} else {
260+
unsafe { Some(transmute(self)) }
261+
}
232262
}
233263

234264
#[inline]
235265
/// Produces a null strong FFI reference
236-
pub fn null_strong() -> Self {
266+
pub fn null() -> Self {
237267
unsafe { transmute(ptr::null::<T>()) }
238268
}
239269
}
@@ -266,6 +296,7 @@ unsafe impl<T: HasArcFFI> FFIArcHelpers for Arc<T> {
266296
#[repr(C)]
267297
/// Gecko-FFI-safe owned pointer
268298
/// Cannot be null
299+
/// Leaks on drop. Please don't drop this.
269300
pub struct Owned<T> {
270301
ptr: *mut T,
271302
_marker: PhantomData<T>,

ports/geckolib/glue.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ pub extern "C" fn Servo_ComputedValues_GetForAnonymousBox(parent_style_or_null:
264264

265265
let maybe_parent = parent_style_or_null.as_arc_opt();
266266
let new_computed = data.stylist.precomputed_values_for_pseudo(&pseudo, maybe_parent);
267-
new_computed.map_or(Strong::null_strong(), |c| c.into_strong())
267+
new_computed.map_or(Strong::null(), |c| c.into_strong())
268268
}
269269

270270
#[no_mangle]
@@ -278,7 +278,7 @@ pub extern "C" fn Servo_ComputedValues_GetForPseudoElement(parent_style: ServoCo
278278

279279
let parent_or_null = || {
280280
if is_probe {
281-
Strong::null_strong()
281+
Strong::null()
282282
} else {
283283
parent_style.as_arc::<ComputedValues>().clone().into_strong()
284284
}

ports/geckolib/wrapper.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,8 @@ impl<'ln> TNode for GeckoNode<'ln> {
165165

166166
fn children(self) -> GeckoChildrenIterator<'ln> {
167167
let maybe_iter = unsafe { Gecko_MaybeCreateStyleChildrenIterator(self.0) };
168-
if !maybe_iter.is_null() {
169-
GeckoChildrenIterator::GeckoIterator(maybe_iter)
168+
if let Some(iter) = maybe_iter.borrow_opt() {
169+
GeckoChildrenIterator::GeckoIterator(iter)
170170
} else {
171171
GeckoChildrenIterator::Current(self.first_child())
172172
}
@@ -346,14 +346,14 @@ impl<'ln> TNode for GeckoNode<'ln> {
346346
// (heavier-weight) Gecko-implemented iterator.
347347
pub enum GeckoChildrenIterator<'a> {
348348
Current(Option<GeckoNode<'a>>),
349-
GeckoIterator(*mut bindings::StyleChildrenIterator),
349+
GeckoIterator(bindings::StyleChildrenIteratorBorrowed<'a>),
350350
}
351351

352352
impl<'a> Drop for GeckoChildrenIterator<'a> {
353353
fn drop(&mut self) {
354-
if let GeckoChildrenIterator::GeckoIterator(it) = *self {
354+
if let GeckoChildrenIterator::GeckoIterator(ref it) = *self {
355355
unsafe {
356-
Gecko_DropStyleChildrenIterator(it);
356+
Gecko_DropStyleChildrenIterator(*it as *const _ as *mut _);
357357
}
358358
}
359359
}
@@ -369,7 +369,7 @@ impl<'a> Iterator for GeckoChildrenIterator<'a> {
369369
curr
370370
},
371371
GeckoChildrenIterator::GeckoIterator(it) => unsafe {
372-
Gecko_GetNextStyleChild(it).as_ref().map(|n| GeckoNode::from_ref(n))
372+
Gecko_GetNextStyleChild(it).borrow_opt().map(GeckoNode)
373373
}
374374
}
375375
}

0 commit comments

Comments
 (0)