Bug 1275913 - Add Borrowed types for sharing arcs with Rust; r?bholley draft
authorManish Goregaokar <manishearth@gmail.com>
Fri, 12 Aug 2016 19:54:43 +0530
changeset 399986 885010603eeb90b1028087b5d1ebb7787eaa9e71
parent 399985 07d154b77a88744366697e926b9ce24e56711100
child 528123 7f83cea2b331928d668dc1cec42f9007952ee417
push id26058
push usermanishearth@gmail.com
push dateFri, 12 Aug 2016 14:41:37 +0000
reviewersbholley
bugs1275913
milestone51.0a1
Bug 1275913 - Add Borrowed types for sharing arcs with Rust; r?bholley MozReview-Commit-ID: JMl4DdBVQPS
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;
 
@@ -765,72 +765,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*
+RawServoStyleSheetBorrowed
 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()
 {
@@ -884,59 +884,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*
+ServoComputedValuesBorrowed
 Servo_GetComputedValues(RawGeckoNode* node)
 {
   MOZ_CRASH("stylo: shouldn't be calling Servo_GetComputedValues in a "
             "non-MOZ_STYLO build");
 }
 
-ServoComputedValues*
-Servo_GetComputedValuesForAnonymousBox(ServoComputedValues* parentStyleOrNull,
+ServoComputedValuesBorrowed
+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,
+ServoComputedValuesBorrowed
+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)
+ServoComputedValuesBorrowed
+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()
 {
@@ -970,28 +970,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
@@ -20,16 +20,27 @@
   struct name {                                           \
     T* mPtr;                                              \
     ~name() {                                             \
       GetRefPtr();                                        \
     };                                                    \
     already_AddRefed<T> GetRefPtr();                      \
   }
 
+#define DEFINE_BORROWED_REF_TYPE(name, T)                 \
+  /** <div rustbindgen private></div> */                  \
+  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.
  */
 
@@ -51,18 +62,20 @@ using mozilla::ServoElementSnapshot;
 typedef mozilla::dom::Element RawGeckoElement;
 class nsIDocument;
 typedef nsIDocument RawGeckoDocument;
 struct ServoNodeData;
 struct ServoComputedValues;
 struct RawServoStyleSheet;
 /** <div rustbindgen private></div> */
 DEFINE_STRONG_REF_TYPE(ServoComputedValuesStrong, ServoComputedValues);
+DEFINE_BORROWED_REF_TYPE(ServoComputedValuesBorrowed, ServoComputedValues);
 /** <div rustbindgen private></div> */
 DEFINE_STRONG_REF_TYPE(RawServoStyleSheetStrong, RawServoStyleSheet);
+DEFINE_BORROWED_REF_TYPE(RawServoStyleSheetBorrowed, RawServoStyleSheet);
 struct RawServoStyleSet;
 class nsHTMLCSSStyleSheet;
 struct nsStyleList;
 struct nsStyleImage;
 struct nsStyleGradientStop;
 class nsStyleGradient;
 class nsStyleCoord;
 struct nsStyleDisplay;
@@ -203,17 +216,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);
 
 
@@ -236,25 +249,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);
@@ -264,27 +277,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.
@@ -299,15 +312,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