Bug 1275913 - Add Borrowed types for sharing arcs with Rust; r=bholley draft
authorManish Goregaokar <manishsmail@gmail.com>
Tue, 16 Aug 2016 10:40:05 +0530
changeset 401075 cd9dc4d37370f74231638a7a4f62dfb611215957
parent 401074 ab06acf5269c7bafced067b1d5721d47aa5eafa6
child 528386 f6159d97c839e61ea36c763c590fcf5f7b541a67
push id26352
push userbmo:manishearth@gmail.com
push dateTue, 16 Aug 2016 06:58:00 +0000
reviewersbholley
bugs1275913
milestone51.0a1
Bug 1275913 - Add Borrowed types for sharing arcs with Rust; r=bholley MozReview-Commit-ID: 8FyVPXRogDV
layout/style/ServoBindings.cpp
layout/style/ServoBindings.h
--- a/layout/style/ServoBindings.cpp
+++ b/layout/style/ServoBindings.cpp
@@ -204,17 +204,17 @@ Gecko_GetStyleContext(RawGeckoNode* aNod
     return nullptr;
   }
 
   return primaryFrame->StyleContext();
 }
 
 nsChangeHint
 Gecko_CalcStyleDifference(nsStyleContext* aOldStyleContext,
-                          ServoComputedValues* aComputedValues)
+                          ServoComputedValuesBorrowed aComputedValues)
 {
   MOZ_ASSERT(aOldStyleContext);
   MOZ_ASSERT(aComputedValues);
 
   // Pass the safe thing, which causes us to miss a potential optimization. See
   // bug 1289863.
   nsChangeHint forDescendants = nsChangeHint_Hints_NotHandledForDescendants;
 
@@ -776,72 +776,72 @@ Gecko_Destroy_nsStyle##name(nsStyle##nam
 #ifndef MOZ_STYLO
 void
 Servo_DropNodeData(ServoNodeData* data)
 {
   MOZ_CRASH("stylo: shouldn't be calling Servo_DropNodeData in a "
             "non-MOZ_STYLO build");
 }
 
-RawServoStyleSheet*
+RawServoStyleSheetStrong
 Servo_StylesheetFromUTF8Bytes(const uint8_t* bytes, uint32_t length,
                               mozilla::css::SheetParsingMode mode,
                               const uint8_t* base_bytes, uint32_t base_length,
                               ThreadSafeURIHolder* base,
                               ThreadSafeURIHolder* referrer,
                               ThreadSafePrincipalHolder* principal)
 {
   MOZ_CRASH("stylo: shouldn't be calling Servo_StylesheetFromUTF8Bytes in a "
             "non-MOZ_STYLO build");
 }
 
 void
-Servo_AddRefStyleSheet(RawServoStyleSheet* sheet)
+Servo_AddRefStyleSheet(RawServoStyleSheetBorrowed sheet)
 {
   MOZ_CRASH("stylo: shouldn't be calling Servo_AddRefStylesheet in a "
             "non-MOZ_STYLO build");
 }
 
 void
-Servo_ReleaseStyleSheet(RawServoStyleSheet* sheet)
+Servo_ReleaseStyleSheet(RawServoStyleSheetBorrowed sheet)
 {
   MOZ_CRASH("stylo: shouldn't be calling Servo_ReleaseStylesheet in a "
             "non-MOZ_STYLO build");
 }
 
 void
-Servo_AppendStyleSheet(RawServoStyleSheet* sheet, RawServoStyleSet* set)
+Servo_AppendStyleSheet(RawServoStyleSheetBorrowed sheet, RawServoStyleSet* set)
 {
   MOZ_CRASH("stylo: shouldn't be calling Servo_AppendStyleSheet in a "
             "non-MOZ_STYLO build");
 }
 
-void Servo_PrependStyleSheet(RawServoStyleSheet* sheet, RawServoStyleSet* set)
+void Servo_PrependStyleSheet(RawServoStyleSheetBorrowed sheet, RawServoStyleSet* set)
 {
   MOZ_CRASH("stylo: shouldn't be calling Servo_PrependStyleSheet in a "
             "non-MOZ_STYLO build");
 }
 
-void Servo_RemoveStyleSheet(RawServoStyleSheet* sheet, RawServoStyleSet* set)
+void Servo_RemoveStyleSheet(RawServoStyleSheetBorrowed sheet, RawServoStyleSet* set)
 {
   MOZ_CRASH("stylo: shouldn't be calling Servo_RemoveStyleSheet in a "
             "non-MOZ_STYLO build");
 }
 
 void
-Servo_InsertStyleSheetBefore(RawServoStyleSheet* sheet,
-                             RawServoStyleSheet* reference,
+Servo_InsertStyleSheetBefore(RawServoStyleSheetBorrowed sheet,
+                             RawServoStyleSheetBorrowed reference,
                              RawServoStyleSet* set)
 {
   MOZ_CRASH("stylo: shouldn't be calling Servo_InsertStyleSheetBefore in a "
             "non-MOZ_STYLO build");
 }
 
 bool
-Servo_StyleSheetHasRules(RawServoStyleSheet* sheet)
+Servo_StyleSheetHasRules(RawServoStyleSheetBorrowed sheet)
 {
   MOZ_CRASH("stylo: shouldn't be calling Servo_StyleSheetHasRules in a "
             "non-MOZ_STYLO build");
 }
 
 RawServoStyleSet*
 Servo_InitStyleSet()
 {
@@ -895,59 +895,59 @@ Servo_ClearDeclarationBlockCachePointer(
 bool
 Servo_CSSSupports(const uint8_t* name, uint32_t name_length,
                   const uint8_t* value, uint32_t value_length)
 {
   MOZ_CRASH("stylo: shouldn't be calling Servo_CSSSupports in a "
             "non-MOZ_STYLO build");
 }
 
-ServoComputedValues*
+ServoComputedValuesStrong
 Servo_GetComputedValues(RawGeckoNode* node)
 {
   MOZ_CRASH("stylo: shouldn't be calling Servo_GetComputedValues in a "
             "non-MOZ_STYLO build");
 }
 
-ServoComputedValues*
-Servo_GetComputedValuesForAnonymousBox(ServoComputedValues* parentStyleOrNull,
+ServoComputedValuesStrong
+Servo_GetComputedValuesForAnonymousBox(ServoComputedValuesBorrowed parentStyleOrNull,
                                        nsIAtom* pseudoTag,
                                        RawServoStyleSet* set)
 {
   MOZ_CRASH("stylo: shouldn't be calling Servo_GetComputedValuesForAnonymousBox in a "
             "non-MOZ_STYLO build");
 }
 
-ServoComputedValues*
-Servo_GetComputedValuesForPseudoElement(ServoComputedValues* parent_style,
+ServoComputedValuesStrong
+Servo_GetComputedValuesForPseudoElement(ServoComputedValuesBorrowed parent_style,
                                         RawGeckoElement* match_element,
                                         nsIAtom* pseudo_tag,
                                         RawServoStyleSet* set,
                                         bool is_probe)
 {
   MOZ_CRASH("stylo: shouldn't be calling Servo_GetComputedValuesForPseudoElement in a "
             "non-MOZ_STYLO build");
 }
 
-ServoComputedValues*
-Servo_InheritComputedValues(ServoComputedValues* parent_style)
+ServoComputedValuesStrong
+Servo_InheritComputedValues(ServoComputedValuesBorrowed parent_style)
 {
   MOZ_CRASH("stylo: shouldn't be calling Servo_InheritComputedValues in a "
             "non-MOZ_STYLO build");
 }
 
 void
-Servo_AddRefComputedValues(ServoComputedValues*)
+Servo_AddRefComputedValues(ServoComputedValuesBorrowed)
 {
   MOZ_CRASH("stylo: shouldn't be calling Servo_AddRefComputedValues in a "
             "non-MOZ_STYLO build");
 }
 
 void
-Servo_ReleaseComputedValues(ServoComputedValues*)
+Servo_ReleaseComputedValues(ServoComputedValuesBorrowed)
 {
   MOZ_CRASH("stylo: shouldn't be calling Servo_ReleaseComputedValues in a "
             "non-MOZ_STYLO build");
 }
 
 void
 Servo_Initialize()
 {
@@ -981,28 +981,28 @@ Servo_RestyleDocument(RawGeckoDocument* 
 void Servo_RestyleSubtree(RawGeckoNode* node, RawServoStyleSet* set)
 {
   MOZ_CRASH("stylo: shouldn't be calling Servo_RestyleSubtree in a "
             "non-MOZ_STYLO build");
 }
 
 #define STYLE_STRUCT(name_, checkdata_cb_)                                     \
 const nsStyle##name_*                                                          \
-Servo_GetStyle##name_(ServoComputedValues*)                                    \
+Servo_GetStyle##name_(ServoComputedValuesBorrowed)                             \
 {                                                                              \
   MOZ_CRASH("stylo: shouldn't be calling Servo_GetStyle" #name_ " in a "       \
             "non-MOZ_STYLO build");                                            \
 }
 #include "nsStyleStructList.h"
 #undef STYLE_STRUCT
 #endif
 
 #ifdef MOZ_STYLO
 const nsStyleVariables*
-Servo_GetStyleVariables(ServoComputedValues* aComputedValues)
+Servo_GetStyleVariables(ServoComputedValuesBorrowed aComputedValues)
 {
   // Servo can't provide us with Variables structs yet, so instead of linking
   // to a Servo_GetStyleVariables defined in Servo we define one here that
   // always returns the same, empty struct.
   static nsStyleVariables variables(StyleStructContext::ServoContext());
   return &variables;
 }
 #endif
--- a/layout/style/ServoBindings.h
+++ b/layout/style/ServoBindings.h
@@ -11,22 +11,34 @@
 #include "mozilla/css/SheetParsingMode.h"
 #include "nsChangeHint.h"
 #include "nsColor.h"
 #include "nsProxyRelease.h"
 #include "nsStyleCoord.h"
 #include "nsStyleStruct.h"
 #include "stdint.h"
 
-#define DEFINE_STRONG_REF_TYPE(name, T)                   \
+#define DECL_STRONG_REF_TYPE(name, T)                   \
   struct MOZ_MUST_USE_TYPE name {                         \
     T* mPtr;                                              \
     already_AddRefed<T> Consume();                        \
   }
 
+#define DECL_BORROWED_REF_TYPE(name, T)                 \
+  struct name {                                           \
+    T* mPtr;                                              \
+    MOZ_IMPLICIT                                          \
+    name(T* x): mPtr(x) {};                               \
+    MOZ_IMPLICIT                                          \
+    name(const RefPtr<T>& aPtr) : mPtr(aPtr.get()) {};    \
+    operator T*() const & {                               \
+      return mPtr;                                        \
+    }                                                     \
+  }
+
 /*
  * API for Servo to access Gecko data structures. This file must compile as valid
  * C code in order for the binding generator to parse it.
  *
  * Functions beginning with Gecko_ are implemented in Gecko and invoked from Servo.
  * Functions beginning with Servo_ are implemented in Servo and invoked from Gecko.
  */
 
@@ -45,19 +57,21 @@ using mozilla::FontFamilyList;
 using mozilla::FontFamilyType;
 using mozilla::dom::Element;
 using mozilla::ServoElementSnapshot;
 typedef mozilla::dom::Element RawGeckoElement;
 class nsIDocument;
 typedef nsIDocument RawGeckoDocument;
 struct ServoNodeData;
 struct ServoComputedValues;
+DECL_STRONG_REF_TYPE(ServoComputedValuesStrong, ServoComputedValues);
+DECL_BORROWED_REF_TYPE(ServoComputedValuesBorrowed, ServoComputedValues);
 struct RawServoStyleSheet;
-DEFINE_STRONG_REF_TYPE(ServoComputedValuesStrong, ServoComputedValues);
-DEFINE_STRONG_REF_TYPE(RawServoStyleSheetStrong, RawServoStyleSheet);
+DECL_STRONG_REF_TYPE(RawServoStyleSheetStrong, RawServoStyleSheet);
+DECL_BORROWED_REF_TYPE(RawServoStyleSheetBorrowed, RawServoStyleSheet);
 struct RawServoStyleSet;
 class nsHTMLCSSStyleSheet;
 struct nsStyleList;
 struct nsStyleImage;
 struct nsStyleGradientStop;
 class nsStyleGradient;
 class nsStyleCoord;
 struct nsStyleDisplay;
@@ -198,17 +212,17 @@ void Gecko_UnsetNodeFlags(RawGeckoNode* 
 // TODO: We would avoid a few ffi calls if we decide to make an API like the
 // former CalcAndStoreStyleDifference, but that would effectively mean breaking
 // some safety guarantees in the servo side.
 //
 // Also, we might want a ComputedValues to ComputedValues API for animations?
 // Not if we do them in Gecko...
 nsStyleContext* Gecko_GetStyleContext(RawGeckoNode* node);
 nsChangeHint Gecko_CalcStyleDifference(nsStyleContext* oldstyle,
-                                       ServoComputedValues* newstyle);
+                                       ServoComputedValuesBorrowed newstyle);
 void Gecko_StoreStyleDifference(RawGeckoNode* node, nsChangeHint change);
 
 // `array` must be an nsTArray
 // If changing this signature, please update the
 // friend function declaration in nsTArray.h
 void Gecko_EnsureTArrayCapacity(void* array, size_t capacity, size_t elem_size);
 
 
@@ -231,25 +245,25 @@ NS_DECL_THREADSAFE_FFI_REFCOUNTING(nsSty
 // generator is smart enough to handle them.
 RawServoStyleSheetStrong Servo_StylesheetFromUTF8Bytes(
     const uint8_t* bytes, uint32_t length,
     mozilla::css::SheetParsingMode parsing_mode,
     const uint8_t* base_bytes, uint32_t base_length,
     ThreadSafeURIHolder* base,
     ThreadSafeURIHolder* referrer,
     ThreadSafePrincipalHolder* principal);
-void Servo_AddRefStyleSheet(RawServoStyleSheet* sheet);
-void Servo_ReleaseStyleSheet(RawServoStyleSheet* sheet);
-void Servo_AppendStyleSheet(RawServoStyleSheet* sheet, RawServoStyleSet* set);
-void Servo_PrependStyleSheet(RawServoStyleSheet* sheet, RawServoStyleSet* set);
-void Servo_RemoveStyleSheet(RawServoStyleSheet* sheet, RawServoStyleSet* set);
-void Servo_InsertStyleSheetBefore(RawServoStyleSheet* sheet,
-                                  RawServoStyleSheet* reference,
+void Servo_AddRefStyleSheet(RawServoStyleSheetBorrowed sheet);
+void Servo_ReleaseStyleSheet(RawServoStyleSheetBorrowed sheet);
+void Servo_AppendStyleSheet(RawServoStyleSheetBorrowed sheet, RawServoStyleSet* set);
+void Servo_PrependStyleSheet(RawServoStyleSheetBorrowed sheet, RawServoStyleSet* set);
+void Servo_RemoveStyleSheet(RawServoStyleSheetBorrowed sheet, RawServoStyleSet* set);
+void Servo_InsertStyleSheetBefore(RawServoStyleSheetBorrowed sheet,
+                                  RawServoStyleSheetBorrowed reference,
                                   RawServoStyleSet* set);
-bool Servo_StyleSheetHasRules(RawServoStyleSheet* sheet);
+bool Servo_StyleSheetHasRules(RawServoStyleSheetBorrowed sheet);
 RawServoStyleSet* Servo_InitStyleSet();
 void Servo_DropStyleSet(RawServoStyleSet* set);
 
 // Style attributes.
 ServoDeclarationBlock* Servo_ParseStyleAttribute(const uint8_t* bytes,
                                                  uint32_t length,
                                                  nsHTMLCSSStyleSheet* cache);
 void Servo_DropDeclarationBlock(ServoDeclarationBlock* declarations);
@@ -259,27 +273,27 @@ void Servo_SetDeclarationBlockImmutable(
 void Servo_ClearDeclarationBlockCachePointer(ServoDeclarationBlock* declarations);
 
 // CSS supports().
 bool Servo_CSSSupports(const uint8_t* name, uint32_t name_length,
                        const uint8_t* value, uint32_t value_length);
 
 // Computed style data.
 ServoComputedValuesStrong Servo_GetComputedValues(RawGeckoNode* node);
-ServoComputedValuesStrong Servo_GetComputedValuesForAnonymousBox(ServoComputedValues* parentStyleOrNull,
+ServoComputedValuesStrong Servo_GetComputedValuesForAnonymousBox(ServoComputedValuesBorrowed parentStyleOrNull,
                                                             nsIAtom* pseudoTag,
                                                             RawServoStyleSet* set);
-ServoComputedValuesStrong Servo_GetComputedValuesForPseudoElement(ServoComputedValues* parent_style,
+ServoComputedValuesStrong Servo_GetComputedValuesForPseudoElement(ServoComputedValuesBorrowed parent_style,
                                                              RawGeckoElement* match_element,
                                                              nsIAtom* pseudo_tag,
                                                              RawServoStyleSet* set,
                                                              bool is_probe);
-ServoComputedValuesStrong Servo_InheritComputedValues(ServoComputedValues* parent_style);
-void Servo_AddRefComputedValues(ServoComputedValues*);
-void Servo_ReleaseComputedValues(ServoComputedValues*);
+ServoComputedValuesStrong Servo_InheritComputedValues(ServoComputedValuesBorrowed parent_style);
+void Servo_AddRefComputedValues(ServoComputedValuesBorrowed);
+void Servo_ReleaseComputedValues(ServoComputedValuesBorrowed);
 
 // Initialize Servo components. Should be called exactly once at startup.
 void Servo_Initialize();
 
 // Shut down Servo components. Should be called exactly once at shutdown.
 void Servo_Shutdown();
 
 // Restyle the given document or subtree.
@@ -294,15 +308,15 @@ nsRestyleHint Servo_ComputeRestyleHint(R
 // Style-struct management.
 #define STYLE_STRUCT(name, checkdata_cb)                                       \
   struct nsStyle##name;                                                        \
   void Gecko_Construct_nsStyle##name(nsStyle##name* ptr);                      \
   void Gecko_CopyConstruct_nsStyle##name(nsStyle##name* ptr,                   \
                                          const nsStyle##name* other);          \
   void Gecko_Destroy_nsStyle##name(nsStyle##name* ptr);                        \
   const nsStyle##name* Servo_GetStyle##name(                                   \
-    ServoComputedValues* computedValues);
+    ServoComputedValuesBorrowed computedValues);
 #include "nsStyleStructList.h"
 #undef STYLE_STRUCT
 
 } // extern "C"
 
 #endif // mozilla_ServoBindings_h