diff --git a/js/public/HashTable.h b/js/public/HashTable.h index eea0a9de999..7a29437ed8b 100644 --- a/js/public/HashTable.h +++ b/js/public/HashTable.h @@ -8,6 +8,7 @@ #ifndef js_HashTable_h__ #define js_HashTable_h__ +#include "mozilla/Assertions.h" #include "mozilla/Attributes.h" #include "mozilla/DebugOnly.h" #include "mozilla/TypeTraits.h" @@ -67,7 +68,15 @@ class HashMap // HashMap construction is fallible (due to OOM); thus the user must call // init after constructing a HashMap and check the return value. - HashMap(AllocPolicy a = AllocPolicy()) : impl(a) {} + HashMap(AllocPolicy a = AllocPolicy()) + : impl(a) + { + MOZ_STATIC_ASSERT(tl::IsRelocatableHeapType::result, + "Key type must be relocatable"); + MOZ_STATIC_ASSERT(tl::IsRelocatableHeapType::result, + "Value type must be relocatable"); + } + bool init(uint32_t len = 16) { return impl.init(len); } bool initialized() const { return impl.initialized(); } @@ -254,9 +263,6 @@ class HashMap HashMap &operator=(const HashMap &hm) MOZ_DELETE; friend class Impl::Enum; - - typedef typename tl::StaticAssert::result>::result keyAssert; - typedef typename tl::StaticAssert::result>::result valAssert; }; /*****************************************************************************/ @@ -297,7 +303,11 @@ class HashSet // HashSet construction is fallible (due to OOM); thus the user must call // init after constructing a HashSet and check the return value. - HashSet(AllocPolicy a = AllocPolicy()) : impl(a) {} + HashSet(AllocPolicy a = AllocPolicy()) : impl(a) + { + MOZ_STATIC_ASSERT(tl::IsRelocatableHeapType::result, + "Set element type must be relocatable"); + } bool init(uint32_t len = 16) { return impl.init(len); } bool initialized() const { return impl.initialized(); } @@ -448,8 +458,6 @@ class HashSet HashSet &operator=(const HashSet &hs) MOZ_DELETE; friend class Impl::Enum; - - typedef typename tl::StaticAssert::result>::result _; }; /*****************************************************************************/ diff --git a/js/public/RootingAPI.h b/js/public/RootingAPI.h index 406040a4ccd..0f1e6b459c5 100644 --- a/js/public/RootingAPI.h +++ b/js/public/RootingAPI.h @@ -191,13 +191,15 @@ class Handle : public js::HandleBase /* Create a handle for a NULL pointer. */ Handle(js::NullPtr) { - typedef typename js::tl::StaticAssert::value>::result _; + MOZ_STATIC_ASSERT(mozilla::IsPointer::value, + "js::NullPtr overload not valid for non-pointer types"); ptr = reinterpret_cast(&js::NullPtr::constNullValue); } /* Create a handle for a NULL pointer. */ Handle(JS::NullPtr) { - typedef typename js::tl::StaticAssert::value>::result _; + MOZ_STATIC_ASSERT(mozilla::IsPointer::value, + "JS::NullPtr overload not valid for non-pointer types"); ptr = reinterpret_cast(&JS::NullPtr::constNullValue); } diff --git a/js/public/TemplateLib.h b/js/public/TemplateLib.h index 6d35f74ec65..20d76772127 100644 --- a/js/public/TemplateLib.h +++ b/js/public/TemplateLib.h @@ -64,17 +64,17 @@ template struct BitSize { static const size_t result = sizeof(T) * JS_BITS_PER_BYTE; }; -/* Allow Assertions by only including the 'result' typedef if 'true'. */ -template struct StaticAssert {}; -template <> struct StaticAssert { typedef int result; }; - /* * Produce an N-bit mask, where N <= BitSize::result. Handle the * language-undefined edge case when N = BitSize::result. */ template struct NBitMask { - typedef typename StaticAssert::result>::result _; - static const size_t result = (size_t(1) << N) - 1; + // Assert the precondition. On success this evaluates to 0. Otherwise it + // triggers divide-by-zero at compile time: a guaranteed compile error in + // C++11, and usually one in C++98. Add this value to |result| to assure + // its computation. + static const size_t checkPrecondition = 0 / (N < BitSize::result); + static const size_t result = (size_t(1) << N) - 1 + checkPrecondition; }; template <> struct NBitMask::result> { static const size_t result = size_t(-1); diff --git a/js/src/ds/LifoAlloc.h b/js/src/ds/LifoAlloc.h index 2c5aaebf3d9..5f1048c2f3b 100644 --- a/js/src/ds/LifoAlloc.h +++ b/js/src/ds/LifoAlloc.h @@ -8,6 +8,7 @@ #ifndef LifoAlloc_h__ #define LifoAlloc_h__ +#include "mozilla/Assertions.h" #include "mozilla/Attributes.h" #include "mozilla/DebugOnly.h" #include "mozilla/GuardObjects.h" @@ -33,9 +34,9 @@ JS_ALWAYS_INLINE char * AlignPtr(void *orig) { - typedef tl::StaticAssert< - tl::FloorLog2::result == tl::CeilingLog2::result - >::result _; + MOZ_STATIC_ASSERT(tl::FloorLog2::result == + tl::CeilingLog2::result, + "LIFO_ALLOC_ALIGN must be a power of two"); char *result = (char *) ((uintptr_t(orig) + (LIFO_ALLOC_ALIGN - 1)) & (~LIFO_ALLOC_ALIGN + 1)); JS_ASSERT(uintptr_t(result) % LIFO_ALLOC_ALIGN == 0);