Back to index

lightning-sunbird  0.9+nobinonly
jstypes.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla Communicator client code, released
00016  * March 31, 1998.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   IBM Corp.
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 /*
00041 ** File:                jstypes.h
00042 ** Description: Definitions of NSPR's basic types
00043 **
00044 ** Prototypes and macros used to make up for deficiencies in ANSI environments
00045 ** that we have found.
00046 **
00047 ** Since we do not wrap <stdlib.h> and all the other standard headers, authors
00048 ** of portable code will not know in general that they need these definitions.
00049 ** Instead of requiring these authors to find the dependent uses in their code
00050 ** and take the following steps only in those C files, we take steps once here
00051 ** for all C files.
00052 **/
00053 
00054 #ifndef jstypes_h___
00055 #define jstypes_h___
00056 
00057 #include <stddef.h>
00058 
00059 /***********************************************************************
00060 ** MACROS:      JS_EXTERN_API
00061 **              JS_EXPORT_API
00062 ** DESCRIPTION:
00063 **      These are only for externally visible routines and globals.  For
00064 **      internal routines, just use "extern" for type checking and that
00065 **      will not export internal cross-file or forward-declared symbols.
00066 **      Define a macro for declaring procedures return types. We use this to
00067 **      deal with windoze specific type hackery for DLL definitions. Use
00068 **      JS_EXTERN_API when the prototype for the method is declared. Use
00069 **      JS_EXPORT_API for the implementation of the method.
00070 **
00071 ** Example:
00072 **   in dowhim.h
00073 **     JS_EXTERN_API( void ) DoWhatIMean( void );
00074 **   in dowhim.c
00075 **     JS_EXPORT_API( void ) DoWhatIMean( void ) { return; }
00076 **
00077 **
00078 ***********************************************************************/
00079 #ifdef WIN32
00080 /* These also work for __MWERKS__ */
00081 #define JS_EXTERN_API(__type) extern __declspec(dllexport) __type
00082 #define JS_EXPORT_API(__type) __declspec(dllexport) __type
00083 #define JS_EXTERN_DATA(__type) extern __declspec(dllexport) __type
00084 #define JS_EXPORT_DATA(__type) __declspec(dllexport) __type
00085 
00086 #define JS_DLL_CALLBACK
00087 #define JS_STATIC_DLL_CALLBACK(__x) static __x
00088 
00089 #elif defined(XP_OS2) && defined(__declspec)
00090 
00091 #define JS_EXTERN_API(__type) extern __declspec(dllexport) __type
00092 #define JS_EXPORT_API(__type) __declspec(dllexport) __type
00093 #define JS_EXTERN_DATA(__type) extern __declspec(dllexport) __type
00094 #define JS_EXPORT_DATA(__type) __declspec(dllexport) __type
00095 
00096 #define JS_DLL_CALLBACK
00097 #define JS_STATIC_DLL_CALLBACK(__x) static __x
00098 
00099 #elif defined(WIN16)
00100 
00101 #ifdef _WINDLL
00102 #define JS_EXTERN_API(__type) extern __type _cdecl _export _loadds
00103 #define JS_EXPORT_API(__type) __type _cdecl _export _loadds
00104 #define JS_EXTERN_DATA(__type) extern __type _export
00105 #define JS_EXPORT_DATA(__type) __type _export
00106 
00107 #define JS_DLL_CALLBACK             __cdecl __loadds
00108 #define JS_STATIC_DLL_CALLBACK(__x) static __x CALLBACK
00109 
00110 #else /* this must be .EXE */
00111 #define JS_EXTERN_API(__type) extern __type _cdecl _export
00112 #define JS_EXPORT_API(__type) __type _cdecl _export
00113 #define JS_EXTERN_DATA(__type) extern __type _export
00114 #define JS_EXPORT_DATA(__type) __type _export
00115 
00116 #define JS_DLL_CALLBACK             __cdecl __loadds
00117 #define JS_STATIC_DLL_CALLBACK(__x) __x JS_DLL_CALLBACK
00118 #endif /* _WINDLL */
00119 
00120 #else /* Unix */
00121 
00122 #ifdef HAVE_VISIBILITY_ATTRIBUTE
00123 #define JS_EXTERNAL_VIS __attribute__((visibility ("default")))
00124 #else
00125 #define JS_EXTERNAL_VIS
00126 #endif
00127 
00128 #define JS_EXTERN_API(__type) extern JS_EXTERNAL_VIS __type
00129 #define JS_EXPORT_API(__type) JS_EXTERNAL_VIS __type
00130 #define JS_EXTERN_DATA(__type) extern JS_EXTERNAL_VIS __type
00131 #define JS_EXPORT_DATA(__type) JS_EXTERNAL_VIS __type
00132 
00133 #define JS_DLL_CALLBACK
00134 #define JS_STATIC_DLL_CALLBACK(__x) static __x
00135 
00136 #endif
00137 
00138 #ifdef _WIN32
00139 #  if defined(__MWERKS__) || defined(__GNUC__)
00140 #    define JS_IMPORT_API(__x)      __x
00141 #  else
00142 #    define JS_IMPORT_API(__x)      __declspec(dllimport) __x
00143 #  endif
00144 #elif defined(XP_OS2) && defined(__declspec)
00145 #    define JS_IMPORT_API(__x)      __declspec(dllimport) __x
00146 #else
00147 #    define JS_IMPORT_API(__x)      JS_EXPORT_API (__x)
00148 #endif
00149 
00150 #if defined(_WIN32) && !defined(__MWERKS__)
00151 #    define JS_IMPORT_DATA(__x)      __declspec(dllimport) __x
00152 #elif defined(XP_OS2) && defined(__declspec)
00153 #    define JS_IMPORT_DATA(__x)      __declspec(dllimport) __x
00154 #else
00155 #    define JS_IMPORT_DATA(__x)     JS_EXPORT_DATA (__x)
00156 #endif
00157 
00158 /*
00159  * The linkage of JS API functions differs depending on whether the file is
00160  * used within the JS library or not.  Any source file within the JS
00161  * interpreter should define EXPORT_JS_API whereas any client of the library
00162  * should not.
00163  */
00164 #ifdef EXPORT_JS_API
00165 #define JS_PUBLIC_API(t)    JS_EXPORT_API(t)
00166 #define JS_PUBLIC_DATA(t)   JS_EXPORT_DATA(t)
00167 #else
00168 #define JS_PUBLIC_API(t)    JS_IMPORT_API(t)
00169 #define JS_PUBLIC_DATA(t)   JS_IMPORT_DATA(t)
00170 #endif
00171 
00172 #define JS_FRIEND_API(t)    JS_PUBLIC_API(t)
00173 #define JS_FRIEND_DATA(t)   JS_PUBLIC_DATA(t)
00174 
00175 #ifdef _WIN32
00176 #   define JS_INLINE __inline
00177 #elif defined(__GNUC__)
00178 #   define JS_INLINE
00179 #else
00180 #   define JS_INLINE
00181 #endif
00182 
00183 /***********************************************************************
00184 ** MACROS:      JS_BEGIN_MACRO
00185 **              JS_END_MACRO
00186 ** DESCRIPTION:
00187 **      Macro body brackets so that macros with compound statement definitions
00188 **      behave syntactically more like functions when called.
00189 ***********************************************************************/
00190 #define JS_BEGIN_MACRO  do {
00191 #define JS_END_MACRO    } while (0)
00192 
00193 /***********************************************************************
00194 ** MACROS:      JS_BEGIN_EXTERN_C
00195 **              JS_END_EXTERN_C
00196 ** DESCRIPTION:
00197 **      Macro shorthands for conditional C++ extern block delimiters.
00198 ***********************************************************************/
00199 #ifdef __cplusplus
00200 #define JS_BEGIN_EXTERN_C       extern "C" {
00201 #define JS_END_EXTERN_C         }
00202 #else
00203 #define JS_BEGIN_EXTERN_C
00204 #define JS_END_EXTERN_C
00205 #endif
00206 
00207 /***********************************************************************
00208 ** MACROS:      JS_BIT
00209 **              JS_BITMASK
00210 ** DESCRIPTION:
00211 ** Bit masking macros.  XXX n must be <= 31 to be portable
00212 ***********************************************************************/
00213 #define JS_BIT(n)       ((JSUint32)1 << (n))
00214 #define JS_BITMASK(n)   (JS_BIT(n) - 1)
00215 
00216 /***********************************************************************
00217 ** MACROS:      JS_PTR_TO_INT32
00218 **              JS_PTR_TO_UINT32
00219 **              JS_INT32_TO_PTR
00220 **              JS_UINT32_TO_PTR
00221 ** DESCRIPTION:
00222 ** Integer to pointer and pointer to integer conversion macros.
00223 ***********************************************************************/
00224 #define JS_PTR_TO_INT32(x)  ((jsint)((char *)(x) - (char *)0))
00225 #define JS_PTR_TO_UINT32(x) ((jsuint)((char *)(x) - (char *)0))
00226 #define JS_INT32_TO_PTR(x)  ((void *)((char *)0 + (jsint)(x)))
00227 #define JS_UINT32_TO_PTR(x) ((void *)((char *)0 + (jsuint)(x)))
00228 
00229 /***********************************************************************
00230 ** MACROS:      JS_HOWMANY
00231 **              JS_ROUNDUP
00232 **              JS_MIN
00233 **              JS_MAX
00234 ** DESCRIPTION:
00235 **      Commonly used macros for operations on compatible types.
00236 ***********************************************************************/
00237 #define JS_HOWMANY(x,y) (((x)+(y)-1)/(y))
00238 #define JS_ROUNDUP(x,y) (JS_HOWMANY(x,y)*(y))
00239 #define JS_MIN(x,y)     ((x)<(y)?(x):(y))
00240 #define JS_MAX(x,y)     ((x)>(y)?(x):(y))
00241 
00242 #if (defined(XP_WIN) && !defined(CROSS_COMPILE)) || defined (WINCE)
00243 #    include "jscpucfg.h"        /* Use standard Mac or Windows configuration */
00244 #elif defined(XP_UNIX) || defined(XP_BEOS) || defined(XP_OS2) || defined(CROSS_COMPILE)
00245 #    include "jsautocfg.h"       /* Use auto-detected configuration */
00246 #    include "jsosdep.h"         /* ...and platform-specific flags */
00247 #else
00248 #    error "Must define one of XP_BEOS, XP_OS2, XP_WIN or XP_UNIX"
00249 #endif
00250 
00251 JS_BEGIN_EXTERN_C
00252 
00253 /************************************************************************
00254 ** TYPES:       JSUint8
00255 **              JSInt8
00256 ** DESCRIPTION:
00257 **  The int8 types are known to be 8 bits each. There is no type that
00258 **      is equivalent to a plain "char".
00259 ************************************************************************/
00260 #if JS_BYTES_PER_BYTE == 1
00261 typedef unsigned char JSUint8;
00262 typedef signed char JSInt8;
00263 #else
00264 #error No suitable type for JSInt8/JSUint8
00265 #endif
00266 
00267 /************************************************************************
00268 ** TYPES:       JSUint16
00269 **              JSInt16
00270 ** DESCRIPTION:
00271 **  The int16 types are known to be 16 bits each.
00272 ************************************************************************/
00273 #if JS_BYTES_PER_SHORT == 2
00274 typedef unsigned short JSUint16;
00275 typedef short JSInt16;
00276 #else
00277 #error No suitable type for JSInt16/JSUint16
00278 #endif
00279 
00280 /************************************************************************
00281 ** TYPES:       JSUint32
00282 **              JSInt32
00283 ** DESCRIPTION:
00284 **  The int32 types are known to be 32 bits each.
00285 ************************************************************************/
00286 #if JS_BYTES_PER_INT == 4
00287 typedef unsigned int JSUint32;
00288 typedef int JSInt32;
00289 #define JS_INT32(x)  x
00290 #define JS_UINT32(x) x ## U
00291 #elif JS_BYTES_PER_LONG == 4
00292 typedef unsigned long JSUint32;
00293 typedef long JSInt32;
00294 #define JS_INT32(x)  x ## L
00295 #define JS_UINT32(x) x ## UL
00296 #else
00297 #error No suitable type for JSInt32/JSUint32
00298 #endif
00299 
00300 /************************************************************************
00301 ** TYPES:       JSUint64
00302 **              JSInt64
00303 ** DESCRIPTION:
00304 **  The int64 types are known to be 64 bits each. Care must be used when
00305 **      declaring variables of type JSUint64 or JSInt64. Different hardware
00306 **      architectures and even different compilers have varying support for
00307 **      64 bit values. The only guaranteed portability requires the use of
00308 **      the JSLL_ macros (see jslong.h).
00309 ************************************************************************/
00310 #ifdef JS_HAVE_LONG_LONG
00311 #if JS_BYTES_PER_LONG == 8
00312 typedef long JSInt64;
00313 typedef unsigned long JSUint64;
00314 #elif defined(WIN16)
00315 typedef __int64 JSInt64;
00316 typedef unsigned __int64 JSUint64;
00317 #elif defined(WIN32) && !defined(__GNUC__)
00318 typedef __int64  JSInt64;
00319 typedef unsigned __int64 JSUint64;
00320 #else
00321 typedef long long JSInt64;
00322 typedef unsigned long long JSUint64;
00323 #endif /* JS_BYTES_PER_LONG == 8 */
00324 #else  /* !JS_HAVE_LONG_LONG */
00325 typedef struct {
00326 #ifdef IS_LITTLE_ENDIAN
00327     JSUint32 lo, hi;
00328 #else
00329     JSUint32 hi, lo;
00330 #endif
00331 } JSInt64;
00332 typedef JSInt64 JSUint64;
00333 #endif /* !JS_HAVE_LONG_LONG */
00334 
00335 /************************************************************************
00336 ** TYPES:       JSUintn
00337 **              JSIntn
00338 ** DESCRIPTION:
00339 **  The JSIntn types are most appropriate for automatic variables. They are
00340 **      guaranteed to be at least 16 bits, though various architectures may
00341 **      define them to be wider (e.g., 32 or even 64 bits). These types are
00342 **      never valid for fields of a structure.
00343 ************************************************************************/
00344 #if JS_BYTES_PER_INT >= 2
00345 typedef int JSIntn;
00346 typedef unsigned int JSUintn;
00347 #else
00348 #error 'sizeof(int)' not sufficient for platform use
00349 #endif
00350 
00351 /************************************************************************
00352 ** TYPES:       JSFloat64
00353 ** DESCRIPTION:
00354 **  NSPR's floating point type is always 64 bits.
00355 ************************************************************************/
00356 typedef double          JSFloat64;
00357 
00358 /************************************************************************
00359 ** TYPES:       JSSize
00360 ** DESCRIPTION:
00361 **  A type for representing the size of objects.
00362 ************************************************************************/
00363 typedef size_t JSSize;
00364 
00365 /************************************************************************
00366 ** TYPES:       JSPtrDiff
00367 ** DESCRIPTION:
00368 **  A type for pointer difference. Variables of this type are suitable
00369 **      for storing a pointer or pointer sutraction.
00370 ************************************************************************/
00371 typedef ptrdiff_t JSPtrdiff;
00372 
00373 /************************************************************************
00374 ** TYPES:       JSUptrdiff
00375 ** DESCRIPTION:
00376 **  A type for pointer difference. Variables of this type are suitable
00377 **      for storing a pointer or pointer sutraction.
00378 ************************************************************************/
00379 #if JS_BYTES_PER_WORD == 8 && JS_BYTES_PER_LONG != 8
00380 typedef JSUint64 JSUptrdiff;
00381 #else
00382 typedef unsigned long JSUptrdiff;
00383 #endif
00384 
00385 /************************************************************************
00386 ** TYPES:       JSBool
00387 ** DESCRIPTION:
00388 **  Use JSBool for variables and parameter types. Use JS_FALSE and JS_TRUE
00389 **      for clarity of target type in assignments and actual arguments. Use
00390 **      'if (bool)', 'while (!bool)', '(bool) ? x : y' etc., to test booleans
00391 **      just as you would C int-valued conditions.
00392 ************************************************************************/
00393 typedef JSIntn JSBool;
00394 #define JS_TRUE (JSIntn)1
00395 #define JS_FALSE (JSIntn)0
00396 
00397 /************************************************************************
00398 ** TYPES:       JSPackedBool
00399 ** DESCRIPTION:
00400 **  Use JSPackedBool within structs where bitfields are not desireable
00401 **      but minimum and consistent overhead matters.
00402 ************************************************************************/
00403 typedef JSUint8 JSPackedBool;
00404 
00405 /*
00406 ** A JSWord is an integer that is the same size as a void*
00407 */
00408 #if JS_BYTES_PER_WORD == 8 && JS_BYTES_PER_LONG != 8
00409 typedef JSInt64 JSWord;
00410 typedef JSUint64 JSUword;
00411 #else
00412 typedef long JSWord;
00413 typedef unsigned long JSUword;
00414 #endif
00415 
00416 #include "jsotypes.h"
00417 
00418 /***********************************************************************
00419 ** MACROS:      JS_LIKELY
00420 **              JS_UNLIKELY
00421 ** DESCRIPTION:
00422 **      These macros allow you to give a hint to the compiler about branch
00423 **      probability so that it can better optimize.  Use them like this:
00424 **
00425 **      if (JS_LIKELY(v == 1)) {
00426 **          ... expected code path ...
00427 **      }
00428 **
00429 **      if (JS_UNLIKELY(v == 0)) {
00430 **          ... non-expected code path ...
00431 **      }
00432 **
00433 ***********************************************************************/
00434 #if defined(__GNUC__) && (__GNUC__ > 2)
00435 #define JS_LIKELY(x)    (__builtin_expect((x), 1))
00436 #define JS_UNLIKELY(x)  (__builtin_expect((x), 0))
00437 #else
00438 #define JS_LIKELY(x)    (x)
00439 #define JS_UNLIKELY(x)  (x)
00440 #endif
00441 
00442 /***********************************************************************
00443 ** MACROS:      JS_ARRAY_LENGTH
00444 **              JS_ARRAY_END
00445 ** DESCRIPTION:
00446 **      Macros to get the number of elements and the pointer to one past the
00447 **      last element of a C array. Use them like this:
00448 **
00449 **      jschar buf[10], *s;
00450 **      JSString *str;
00451 **      ...
00452 **      for (s = buf; s != JS_ARRAY_END(buf); ++s) *s = ...;
00453 **      ...
00454 **      str = JS_NewStringCopyN(cx, buf, JS_ARRAY_LENGTH(buf));
00455 **      ...
00456 **
00457 ***********************************************************************/
00458 
00459 #define JS_ARRAY_LENGTH(array) (sizeof (array) / sizeof (array)[0])
00460 #define JS_ARRAY_END(array)    ((array) + JS_ARRAY_LENGTH(array))
00461 
00462 JS_END_EXTERN_C
00463 
00464 #endif /* jstypes_h___ */