Bug 504233 - Add SIZE_MAX, etc. #defines to SpiderMonkey. r=jimb
authorJeff Walden <jwalden@mit.edu>
Wed, 15 Jul 2009 15:59:25 -0700
changeset 32758 3b61bf6ab6351c4224c7940fd62be7a289c03680
parent 32757 23c4d4f10fcdb00cae33aa6b8781000d8a4b523c
child 32759 a230a19f988b201a378eafd4913f5665b72ab914
push idunknown
push userunknown
push dateunknown
reviewersjimb
bugs504233
milestone1.9.3a1pre
Bug 504233 - Add SIZE_MAX, etc. #defines to SpiderMonkey. r=jimb
js/src/jsnum.cpp
js/src/jsstdint.h
--- a/js/src/jsnum.cpp
+++ b/js/src/jsnum.cpp
@@ -33,16 +33,18 @@
  * use your version of this file under the terms of the MPL, indicate your
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
+#define __STDC_LIMIT_MACROS
+
 /*
  * JS number type and wrapper class.
  */
 #ifdef XP_OS2
 #define _PC_53  PC_53
 #define _MCW_EM MCW_EM
 #define _MCW_PC MCW_PC
 #endif
@@ -66,16 +68,41 @@
 #include "jsobj.h"
 #include "jsopcode.h"
 #include "jsprf.h"
 #include "jsscope.h"
 #include "jsstr.h"
 #include "jsstrinlines.h"
 #include "jsvector.h"
 
+
+JS_STATIC_ASSERT(uint8_t(-1) == UINT8_MAX);
+JS_STATIC_ASSERT(uint16_t(-1) == UINT16_MAX);
+JS_STATIC_ASSERT(uint32_t(-1) == UINT32_MAX);
+JS_STATIC_ASSERT(uint64_t(-1) == UINT64_MAX);
+
+JS_STATIC_ASSERT(INT8_MAX > INT8_MIN);
+JS_STATIC_ASSERT(int8_t(INT8_MAX + 1) == INT8_MIN);
+JS_STATIC_ASSERT(INT16_MAX > INT16_MIN);
+JS_STATIC_ASSERT(int16_t(INT16_MAX + 1) == INT16_MIN);
+JS_STATIC_ASSERT(INT32_MAX > INT32_MIN);
+JS_STATIC_ASSERT(int32_t(INT32_MAX + 1) == INT32_MIN);
+JS_STATIC_ASSERT(INT64_MAX > INT64_MIN);
+JS_STATIC_ASSERT(int64_t(INT64_MAX + 1) == INT64_MIN);
+
+JS_STATIC_ASSERT(INTPTR_MAX > INTPTR_MIN);
+JS_STATIC_ASSERT(intptr_t(uintptr_t(INTPTR_MAX) + 1) == INTPTR_MIN);
+JS_STATIC_ASSERT(uintptr_t(-1) == UINTPTR_MAX);
+JS_STATIC_ASSERT(size_t(-1) == SIZE_MAX);
+JS_STATIC_ASSERT(PTRDIFF_MAX > PTRDIFF_MIN);
+JS_STATIC_ASSERT(ptrdiff_t(PTRDIFF_MAX) == PTRDIFF_MAX);
+JS_STATIC_ASSERT(ptrdiff_t(PTRDIFF_MIN) == PTRDIFF_MIN);
+JS_STATIC_ASSERT(ptrdiff_t(PTRDIFF_MAX + 1) == PTRDIFF_MIN);
+
+
 static JSBool
 num_isNaN(JSContext *cx, uintN argc, jsval *vp)
 {
     jsdouble x;
 
     if (argc == 0) {
         *vp = JSVAL_TRUE;
         return JS_TRUE;
--- a/js/src/jsstdint.h
+++ b/js/src/jsstdint.h
@@ -74,11 +74,41 @@ typedef JSUint64 uint64_t;
    defined, then jsinttypes.h included the given header, which
    introduced definitions for intptr_t and uintptr_t.  Otherwise,
    define the standard names in terms of the 'JS' types.  */
 #if !defined(JS_STDDEF_H_HAS_INTPTR_T) && !defined(JS_CRTDEFS_H_HAS_INTPTR_T)
 typedef JSIntPtr  intptr_t;
 typedef JSUintPtr uintptr_t;
 #endif
 
+#if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS)
+
+#define INT8_MAX  127
+#define INT8_MIN  (-INT8_MAX - 1)
+#define INT16_MAX 32767
+#define INT16_MIN (-INT16_MIN - 1)
+#define INT32_MAX 2147483647
+#define INT32_MIN (-INT32_MAX - 1)
+#define INT64_MAX 9223372036854775807LL
+#define INT64_MIN (-INT64_MAX - 1)
+
+#define UINT8_MAX  255
+#define UINT16_MAX 65535
+#define UINT32_MAX 4294967295U
+#define UINT64_MAX 18446744073709551615ULL
+
+/*
+ * These are technically wrong as they can't be used in the preprocessor, but
+ * we would require compiler assistance, and at the moment we don't need
+ * preprocessor-correctness.
+ */
+#define INTPTR_MAX  ((intptr_t) (UINTPTR_MAX >> 1))
+#define INTPTR_MIN  (INTPTR_MAX + 1)
+#define UINTPTR_MAX ((uintptr_t) -1)
+#define SIZE_MAX UINTPTR_MAX
+#define PTRDIFF_MAX INTPTR_MAX
+#define PTRDIFF_MIN INTPTR_MIN
+
+#endif /* !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) */
+
 #endif /* JS_HAVE_STDINT_H */
 
 #endif /* jsstdint_h___ */