From 2f8e05e9e0fc4a4311451e77ef150bdd8f16b151 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sat, 30 Mar 2019 20:18:33 +0000 Subject: [PATCH] style: Autogenerate Servo_ binding functions with cbindgen. This depends on https://github.com/eqrion/cbindgen/pull/308. Other than that, this should be ready to go. There's still a bit more magic than what I'd like to eventually. I should be able to make cbindgen not rename types if it doesn't know about them, or something. But this removes most of the manual binding function implementations (all but the ones that are declared via macros, which cbindgen doesn't see across). I need to give up on the _Drop functions taking an Owned because of instantiation order fiasco. In order to define DefaultDelete I need Owned to be complete, but I cannot do it after including the generated file since some declarations already instantiate the specialization. Oh well. Differential Revision: https://phabricator.services.mozilla.com/D24798 --- components/style/cbindgen.toml | 239 ------------------ .../style/gecko_bindings/sugar/ownership.rs | 16 +- 2 files changed, 8 insertions(+), 247 deletions(-) delete mode 100644 components/style/cbindgen.toml diff --git a/components/style/cbindgen.toml b/components/style/cbindgen.toml deleted file mode 100644 index cd910f0be9b..00000000000 --- a/components/style/cbindgen.toml +++ /dev/null @@ -1,239 +0,0 @@ -header = """/* 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/. */""" -autogen_warning = """/* DO NOT MODIFY THIS MANUALLY! This file was generated using cbindgen. - * To generate this file: - * 1. Get the latest cbindgen using `cargo install --force cbindgen` - * a. Alternatively, you can clone `https://github.com/eqrion/cbindgen` and use a tagged release - * 2. Run `rustup run nightly cbindgen toolkit/library/rust/ --lockfile Cargo.lock --crate style -o layout/style/ServoStyleConsts.h` - */ -#include "nsCoord.h" -#include "Units.h" -#include "mozilla/gfx/Types.h" -class nsAtom; -namespace mozilla { - class WritingMode; - enum HalfCorner : uint8_t; - enum LogicalSide : uint8_t; - namespace css { - struct URLValue; - } - - // Work-around weird cbindgen renaming. - typedef css::URLValue StyleURLValue; - typedef nsAtom StylensAtom; -} -""" -include_guard = "mozilla_ServoStyleConsts_h" -include_version = true -braces = "SameLine" -line_length = 80 -tab_width = 2 -language = "C++" -namespaces = ["mozilla"] - -[parse] -parse_deps = true -include = ["cssparser", "style_traits"] - -[struct] -derive_eq = true -derive_neq = true - -[macro_expansion] -bitflags = true - -[enum] -derive_helper_methods = true -derive_const_casts = true -cast_assert_name = "MOZ_ASSERT" - -[export] -prefix = "Style" -include = [ - "Appearance", - "BreakBetween", - "BreakWithin", - "BorderStyle", - "OutlineStyle", - "ComputedFontStretchRange", - "ComputedFontStyleDescriptor", - "ComputedFontWeightRange", - "ComputedTimingFunction", - "CursorKind", - "Display", - "DisplayMode", - "PrefersColorScheme", - "ExtremumLength", - "FillRule", - "FontDisplay", - "FontFaceSourceListComponent", - "FontLanguageOverride", - "OverflowWrap", - "TimingFunction", - "PathCommand", - "UnicodeRange", - "UserSelect", - "Float", - "OverscrollBehavior", - "ScrollSnapAlign", - "ScrollSnapType", - "OverflowAnchor", - "OverflowClipBox", - "Resize", - "Overflow", - "LengthPercentage", - "LetterSpacing", - "NonNegativeLengthPercentage", - "LengthPercentageOrAuto", - "LineHeight", - "NonNegativeLengthPercentageOrAuto", - "Rect", - "IntersectionObserverRootMargin", - "Size", - "MaxSize", - "FlexBasis", - "Position", - "BackgroundSize", - "BorderImageSlice", - "BorderSpacing", - "BorderRadius", - "NonNegativeLengthOrNumberRect", - "Perspective", - "ZIndex", - "TransformOrigin", - "WordBreak", - "Contain", - "RestyleHint", - "TouchAction", - "WillChangeBits", - "TextDecorationLine", - "MozListReversed", - "Owned", - "OwnedOrNull", - "Strong", -] -item_types = ["enums", "structs", "typedefs"] - -[export.body] -"CSSPixelLength" = """ - inline nscoord ToAppUnits() const; - inline bool IsZero() const; -""" - -"LengthPercentage" = """ - // Defined in nsStyleCoord.h - static constexpr inline StyleLengthPercentage Zero(); - static inline StyleLengthPercentage FromAppUnits(nscoord); - static inline StyleLengthPercentage FromPixels(CSSCoord); - static inline StyleLengthPercentage FromPercentage(float); - inline CSSCoord LengthInCSSPixels() const; - inline float Percentage() const; - inline bool HasPercent() const; - inline bool ConvertsToLength() const; - inline nscoord ToLength() const; - inline bool ConvertsToPercentage() const; - inline bool HasLengthAndPercentage() const; - inline float ToPercentage() const; - inline bool IsDefinitelyZero() const; - inline CSSCoord ResolveToCSSPixels(CSSCoord aPercentageBasisInCSSPixels) const; - template inline CSSCoord ResolveToCSSPixelsWith(T aPercentageGetter) const; - template - inline nscoord Resolve(T aPercentageGetter, U aPercentRoundingFunction) const; - template - inline nscoord Resolve(nscoord aPercentageBasis, T aPercentRoundingFunction) const; - template inline nscoord Resolve(T aPercentageGetter) const; - inline nscoord Resolve(nscoord aPercentageBasis) const; -""" - -"GenericLengthPercentageOrAuto" = """ - inline bool ConvertsToLength() const; - inline nscoord ToLength() const; - inline bool ConvertsToPercentage() const; - inline float ToPercentage() const; - inline bool HasPercent() const; - inline bool HasLengthAndPercentage() const; -""" - -"GenericSize" = """ - inline bool ConvertsToLength() const; - inline nscoord ToLength() const; - inline bool ConvertsToPercentage() const; - inline float ToPercentage() const; - inline bool HasPercent() const; - inline bool HasLengthAndPercentage() const; - inline bool BehavesLikeInitialValueOnBlockAxis() const; -""" - -"GenericFlexBasis" = """ - inline bool IsAuto() const; -""" - -"GenericMaxSize" = """ - inline bool ConvertsToLength() const; - inline nscoord ToLength() const; - inline bool ConvertsToPercentage() const; - inline float ToPercentage() const; - inline bool HasPercent() const; - inline bool HasLengthAndPercentage() const; - inline bool BehavesLikeInitialValueOnBlockAxis() const; -""" - -"GenericPosition" = """ - inline bool DependsOnPositioningAreaSize() const; - static inline StyleGenericPosition FromPercentage(float); -""" - -"GenericBackgroundSize" = """ - bool IsInitialValue() const; -""" - -"Rect" = """ - // Defined in nsStyleCoord.h - template inline bool All(Predicate) const; - template inline bool Any(Predicate) const; - - // Defined in WritingModes.h - inline const T& Get(mozilla::Side) const; - inline const T& Get(mozilla::WritingMode, mozilla::LogicalSide) const; - inline const T& GetIStart(mozilla::WritingMode) const; - inline const T& GetBStart(mozilla::WritingMode) const; - inline const T& GetIEnd(mozilla::WritingMode) const; - inline const T& GetBEnd(mozilla::WritingMode) const; -""" - -"GenericBorderRadius" = """ - inline const StyleLengthPercentage& Get(mozilla::HalfCorner) const; -""" - -"RestyleHint" = """ - static inline StyleRestyleHint RestyleSubtree(); - static inline StyleRestyleHint RecascadeSubtree(); - static inline StyleRestyleHint ForAnimations(); -""" - -# TODO(emilio): Add hooks to cbindgen to be able to generate MOZ_MUST_USE_TYPE -# or MOZ_MUST_USE on the functions. -"Owned" = """ - UniquePtr Consume() { - UniquePtr ret(ptr); - ptr = nullptr; - return ret; - } -""" - -"OwnedOrNull" = """ - UniquePtr Consume() { - UniquePtr ret(ptr); - ptr = nullptr; - return ret; - } -""" - -"Strong" = """ - already_AddRefed Consume() { - already_AddRefed ret(const_cast(ptr)); - ptr = nullptr; - return ret; - } -""" diff --git a/components/style/gecko_bindings/sugar/ownership.rs b/components/style/gecko_bindings/sugar/ownership.rs index 04fbf2e55c8..5170b7dbc5a 100644 --- a/components/style/gecko_bindings/sugar/ownership.rs +++ b/components/style/gecko_bindings/sugar/ownership.rs @@ -65,6 +65,14 @@ pub unsafe trait HasBoxFFI: HasSimpleFFI { fn into_ffi(self: Box) -> Owned { unsafe { transmute(self) } } + + /// Drops an owned FFI pointer. This conceptually takes the + /// Owned, except it's a bit of a paint to do that without + /// much benefit. + #[inline] + unsafe fn drop_ffi(ptr: *mut Self::FFIType) { + let _ = Box::from_raw(ptr as *mut Self); + } } /// Helper trait for conversions between FFI Strong/Borrowed types and Arcs @@ -268,14 +276,6 @@ pub struct Owned { } impl Owned { - /// Gets this `Owned` type as a `Box`. - pub fn into_box(self) -> Box - where - ServoType: HasBoxFFI, - { - unsafe { transmute(self) } - } - /// Converts this instance to a (non-null) instance of `OwnedOrNull`. pub fn maybe(self) -> OwnedOrNull { unsafe { transmute(self) }