Bug 1298756 - Use uint8_t/uint16_t as underlying type for enums from webidl. r=bz
authorXidorn Quan <me@upsuper.org>
Thu, 12 Jan 2017 12:23:37 +1100
changeset 329178 a42a1279c2162fa2a6a0f4b448ede7f9d6b2cc6e
parent 329177 d3da2d43ac5d31a362744c0a0c2b4c069a8c85d6
child 329179 7d5040b2835e887b6c7ae25ab007fc7eef8766e9
push id31200
push usercbook@mozilla.com
push dateFri, 13 Jan 2017 09:18:41 +0000
treeherdermozilla-central@eca7348f32d8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbz
bugs1298756
milestone53.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1298756 - Use uint8_t/uint16_t as underlying type for enums from webidl. r=bz MozReview-Commit-ID: 3uS9s5ZrPFd
dom/animation/KeyframeEffectReadOnly.h
dom/base/nsGlobalWindow.h
dom/base/nsIDocument.h
dom/bindings/Codegen.py
dom/cache/CacheStorage.h
dom/canvas/CanvasPath.h
dom/canvas/ImageBitmap.h
dom/canvas/ImageUtils.h
dom/html/nsBrowserElement.h
dom/media/webaudio/AudioContext.h
dom/media/webaudio/AudioNodeStream.cpp
dom/workers/WorkerScope.h
gfx/vr/gfxVR.h
layout/style/nsStyleConsts.h
--- a/dom/animation/KeyframeEffectReadOnly.h
+++ b/dom/animation/KeyframeEffectReadOnly.h
@@ -43,18 +43,18 @@ class ErrorResult;
 struct TimingParams;
 
 namespace dom {
 class ElementOrCSSPseudoElement;
 class GlobalObject;
 class OwningElementOrCSSPseudoElement;
 class UnrestrictedDoubleOrKeyframeAnimationOptions;
 class UnrestrictedDoubleOrKeyframeEffectOptions;
-enum class IterationCompositeOperation : uint32_t;
-enum class CompositeOperation : uint32_t;
+enum class IterationCompositeOperation : uint8_t;
+enum class CompositeOperation : uint8_t;
 struct AnimationPropertyDetails;
 }
 
 /**
  * A property-value pair specified on a keyframe.
  */
 struct PropertyValuePair
 {
--- a/dom/base/nsGlobalWindow.h
+++ b/dom/base/nsGlobalWindow.h
@@ -110,17 +110,17 @@ class BarProp;
 struct ChannelPixelLayout;
 class Console;
 class Crypto;
 class CustomElementRegistry;
 class DocGroup;
 class External;
 class Function;
 class Gamepad;
-enum class ImageBitmapFormat : uint32_t;
+enum class ImageBitmapFormat : uint8_t;
 class IdleRequest;
 class IdleRequestCallback;
 class Location;
 class MediaQueryList;
 class MozSelfSupport;
 class Navigator;
 class OwningExternalOrWindowProxy;
 class Promise;
--- a/dom/base/nsIDocument.h
+++ b/dom/base/nsIDocument.h
@@ -144,17 +144,17 @@ class ImportManager;
 class HTMLBodyElement;
 struct LifecycleCallbackArgs;
 class Link;
 class Location;
 class MediaQueryList;
 class GlobalObject;
 class NodeFilter;
 class NodeIterator;
-enum class OrientationType : uint32_t;
+enum class OrientationType : uint8_t;
 class ProcessingInstruction;
 class Promise;
 class StyleSheetList;
 class SVGDocument;
 class SVGSVGElement;
 class Touch;
 class TouchList;
 class TreeWalker;
@@ -3032,19 +3032,20 @@ protected:
 
   // Whether this document has (or will have, once we have a pres shell) a
   // Gecko- or Servo-backed style system.
   mozilla::StyleBackendType mStyleBackendType;
 
 #ifdef MOZILLA_INTERNAL_API
   // Our visibility state
   mozilla::dom::VisibilityState mVisibilityState;
-  static_assert(sizeof(mozilla::dom::VisibilityState) == sizeof(uint32_t), "Error size of mVisibilityState and mDummy");
+  static_assert(sizeof(mozilla::dom::VisibilityState) == sizeof(uint8_t),
+                "Error size of mVisibilityState and mDummy");
 #else
-  uint32_t mDummy;
+  uint8_t mDummy;
 #endif
 
   // True if BIDI is enabled.
   bool mBidiEnabled : 1;
   // True if a MathML element has ever been owned by this document.
   bool mMathMLEnabled : 1;
 
   // True if this document is the initial document for a window.  This should
--- a/dom/bindings/Codegen.py
+++ b/dom/bindings/Codegen.py
@@ -9620,25 +9620,35 @@ class CGEnum(CGThing):
         self.cgThings = CGList([strings, toJSValue], "\n")
 
     def stringsNamespace(self):
         return self.enum.identifier.name + "Values"
 
     def nEnumStrings(self):
         return len(self.enum.values()) + 1
 
+    def underlyingType(self):
+        count = self.nEnumStrings()
+        if count <= 256:
+            return "uint8_t"
+        if count <= 65536:
+            return "uint16_t"
+        raise ValueError("Enum " + self.enum.identifier.name +
+                         " has more than 65536 values")
+
     def declare(self):
         decl = fill(
             """
-            enum class ${name} : uint32_t {
+            enum class ${name} : ${ty} {
               $*{enums}
               EndGuard_
             };
             """,
             name=self.enum.identifier.name,
+            ty=self.underlyingType(),
             enums=",\n".join(map(getEnumValueName, self.enum.values())) + ",\n")
         strings = CGNamespace(self.stringsNamespace(),
                               CGGeneric(declare="extern const EnumEntry %s[%d];\n"
                                         % (ENUM_ENTRY_VARIABLE_NAME, self.nEnumStrings())))
         return decl + "\n" + self.cgThings.declare()
 
     def define(self):
         return self.cgThings.define()
--- a/dom/cache/CacheStorage.h
+++ b/dom/cache/CacheStorage.h
@@ -23,17 +23,17 @@ namespace mozilla {
 class ErrorResult;
 
 namespace ipc {
   class PrincipalInfo;
 } // namespace ipc
 
 namespace dom {
 
-enum class CacheStorageNamespace : uint32_t;
+enum class CacheStorageNamespace : uint8_t;
 class Promise;
 
 namespace workers {
   class WorkerPrivate;
 } // namespace workers
 
 namespace cache {
 
--- a/dom/canvas/CanvasPath.h
+++ b/dom/canvas/CanvasPath.h
@@ -10,17 +10,17 @@
 #include "nsWrapperCache.h"
 #include "mozilla/gfx/2D.h"
 #include "mozilla/dom/BindingDeclarations.h"
 #include "mozilla/ErrorResult.h"
 
 namespace mozilla {
 namespace dom {
 
-enum class CanvasWindingRule : uint32_t;
+enum class CanvasWindingRule : uint8_t;
 class SVGMatrix;
 
 class CanvasPath final :
   public nsWrapperCache
 {
 public:
   NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(CanvasPath)
   NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(CanvasPath)
--- a/dom/canvas/ImageBitmap.h
+++ b/dom/canvas/ImageBitmap.h
@@ -47,17 +47,17 @@ class CanvasRenderingContext2D;
 struct ChannelPixelLayout;
 class CreateImageBitmapFromBlob;
 class CreateImageBitmapFromBlobTask;
 class CreateImageBitmapFromBlobWorkerTask;
 class File;
 class HTMLCanvasElement;
 class HTMLImageElement;
 class HTMLVideoElement;
-enum class ImageBitmapFormat : uint32_t;
+enum class ImageBitmapFormat : uint8_t;
 class ImageData;
 class ImageUtils;
 template<typename T> class MapDataIntoBufferSource;
 class Promise;
 class PostMessageEvent; // For StructuredClone between windows.
 
 struct ImageBitmapCloneData final
 {
--- a/dom/canvas/ImageUtils.h
+++ b/dom/canvas/ImageUtils.h
@@ -16,17 +16,17 @@ namespace layers {
 class Image;
 }
 
 class ErrorResult;
 
 namespace dom {
 
 struct ChannelPixelLayout;
-enum class ImageBitmapFormat : uint32_t;
+enum class ImageBitmapFormat : uint8_t;
 
 typedef nsTArray<ChannelPixelLayout> ImagePixelLayout;
 
 /*
  * ImageUtils is a wrapper around layers::Image. It provides three unified
  * methods to all sub-classes of layers::Image, which are:
  *
  * (1) GetFormat() converts the image's format into ImageBitmapFormat enum.
--- a/dom/html/nsBrowserElement.h
+++ b/dom/html/nsBrowserElement.h
@@ -17,18 +17,18 @@ class nsFrameLoader;
 
 namespace mozilla {
 
 namespace dom {
 struct BrowserElementDownloadOptions;
 struct BrowserElementExecuteScriptOptions;
 class BrowserElementNextPaintEventCallback;
 class DOMRequest;
-enum class BrowserFindCaseSensitivity: uint32_t;
-enum class BrowserFindDirection: uint32_t;
+enum class BrowserFindCaseSensitivity: uint8_t;
+enum class BrowserFindDirection: uint8_t;
 } // namespace dom
 
 class ErrorResult;
 
 /**
  * A helper class for browser-element frames
  */
 class nsBrowserElement
--- a/dom/media/webaudio/AudioContext.h
+++ b/dom/media/webaudio/AudioContext.h
@@ -37,17 +37,17 @@ namespace mozilla {
 class DOMMediaStream;
 class ErrorResult;
 class MediaStream;
 class MediaStreamGraph;
 class AudioNodeStream;
 
 namespace dom {
 
-enum class AudioContextState : uint32_t;
+enum class AudioContextState : uint8_t;
 class AnalyserNode;
 class AudioBuffer;
 class AudioBufferSourceNode;
 class AudioDestinationNode;
 class AudioListener;
 class AudioNode;
 class BiquadFilterNode;
 class ChannelMergerNode;
@@ -66,17 +66,17 @@ class MediaStreamAudioSourceNode;
 class OscillatorNode;
 class PannerNode;
 class ScriptProcessorNode;
 class StereoPannerNode;
 class WaveShaperNode;
 class PeriodicWave;
 struct PeriodicWaveConstraints;
 class Promise;
-enum class OscillatorType : uint32_t;
+enum class OscillatorType : uint8_t;
 
 // This is addrefed by the OscillatorNodeEngine on the main thread
 // and then used from the MSG thread.
 // It can be released either from the graph thread or the main thread.
 class BasicWaveFormCache
 {
 public:
   explicit BasicWaveFormCache(uint32_t aSampleRate);
--- a/dom/media/webaudio/AudioNodeStream.cpp
+++ b/dom/media/webaudio/AudioNodeStream.cpp
@@ -345,21 +345,16 @@ AudioNodeStream::SetPassThrough(bool aPa
   GraphImpl()->AppendMessage(MakeUnique<Message>(this, aPassThrough));
 }
 
 void
 AudioNodeStream::SetChannelMixingParametersImpl(uint32_t aNumberOfChannels,
                                                 ChannelCountMode aChannelCountMode,
                                                 ChannelInterpretation aChannelInterpretation)
 {
-  // Make sure that we're not clobbering any significant bits by fitting these
-  // values in 16 bits.
-  MOZ_ASSERT(int(aChannelCountMode) < INT16_MAX);
-  MOZ_ASSERT(int(aChannelInterpretation) < INT16_MAX);
-
   mNumberOfInputChannels = aNumberOfChannels;
   mChannelCountMode = aChannelCountMode;
   mChannelInterpretation = aChannelInterpretation;
 }
 
 uint32_t
 AudioNodeStream::ComputedNumberOfChannels(uint32_t aInputChannelCount)
 {
--- a/dom/workers/WorkerScope.h
+++ b/dom/workers/WorkerScope.h
@@ -18,17 +18,17 @@ namespace mozilla {
 namespace dom {
 
 class AnyCallback;
 struct ChannelPixelLayout;
 class Console;
 class Crypto;
 class Function;
 class IDBFactory;
-enum class ImageBitmapFormat : uint32_t;
+enum class ImageBitmapFormat : uint8_t;
 class Performance;
 class Promise;
 class RequestOrUSVString;
 class ServiceWorkerRegistration;
 class WorkerLocation;
 class WorkerNavigator;
 
 namespace cache {
--- a/gfx/vr/gfxVR.h
+++ b/gfx/vr/gfxVR.h
@@ -16,18 +16,18 @@
 #include "mozilla/TimeStamp.h"
 #include "mozilla/TypedEnumBits.h"
 
 namespace mozilla {
 namespace layers {
 class PTextureParent;
 }
 namespace dom {
-enum class GamepadMappingType : uint32_t;
-enum class GamepadHand : uint32_t;
+enum class GamepadMappingType : uint8_t;
+enum class GamepadHand : uint8_t;
 struct GamepadPoseState;
 }
 namespace gfx {
 class VRLayerParent;
 class VRDisplayHost;
 class VRControllerHost;
 
 enum class VRDeviceType : uint16_t {
--- a/layout/style/nsStyleConsts.h
+++ b/layout/style/nsStyleConsts.h
@@ -309,18 +309,18 @@ enum class StyleOrient : uint8_t {
 #define NS_STYLE_WILL_CHANGE_SCROLL             (1<<2)
 #define NS_STYLE_WILL_CHANGE_OPACITY            (1<<3)
 #define NS_STYLE_WILL_CHANGE_FIXPOS_CB          (1<<4)
 #define NS_STYLE_WILL_CHANGE_ABSPOS_CB          (1<<5)
 
 // See AnimationEffectReadOnly.webidl
 // and mozilla/dom/AnimationEffectReadOnlyBinding.h
 namespace dom {
-enum class PlaybackDirection : uint32_t;
-enum class FillMode : uint32_t;
+enum class PlaybackDirection : uint8_t;
+enum class FillMode : uint8_t;
 }
 
 // See nsStyleDisplay
 #define NS_STYLE_ANIMATION_ITERATION_COUNT_INFINITE 0
 
 // See nsStyleDisplay
 #define NS_STYLE_ANIMATION_PLAY_STATE_RUNNING     0
 #define NS_STYLE_ANIMATION_PLAY_STATE_PAUSED      1