~ubuntu-branches/debian/wheezy/couchdb/wheezy

« back to all changes in this revision

Viewing changes to src/js/jstypes.h

  • Committer: Bazaar Package Importer
  • Author(s): Noah Slater
  • Date: 2008-02-06 17:03:38 UTC
  • Revision ID: james.westby@ubuntu.com-20080206170338-y411anylx3oplqid
Tags: upstream-0.7.3~svn684
ImportĀ upstreamĀ versionĀ 0.7.3~svn684

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
5
 * The contents of this file are subject to the Mozilla Public License Version
 
6
 * 1.1 (the "License"); you may not use this file except in compliance with
 
7
 * the License. You may obtain a copy of the License at
 
8
 * http://www.mozilla.org/MPL/
 
9
 *
 
10
 * Software distributed under the License is distributed on an "AS IS" basis,
 
11
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
12
 * for the specific language governing rights and limitations under the
 
13
 * License.
 
14
 *
 
15
 * The Original Code is Mozilla Communicator client code, released
 
16
 * March 31, 1998.
 
17
 *
 
18
 * The Initial Developer of the Original Code is
 
19
 * Netscape Communications Corporation.
 
20
 * Portions created by the Initial Developer are Copyright (C) 1998
 
21
 * the Initial Developer. All Rights Reserved.
 
22
 *
 
23
 * Contributor(s):
 
24
 *   IBM Corp.
 
25
 *
 
26
 * Alternatively, the contents of this file may be used under the terms of
 
27
 * either of the GNU General Public License Version 2 or later (the "GPL"),
 
28
 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
29
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
30
 * of those above. If you wish to allow use of your version of this file only
 
31
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
32
 * use your version of this file under the terms of the MPL, indicate your
 
33
 * decision by deleting the provisions above and replace them with the notice
 
34
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
35
 * the provisions above, a recipient may use your version of this file under
 
36
 * the terms of any one of the MPL, the GPL or the LGPL.
 
37
 *
 
38
 * ***** END LICENSE BLOCK ***** */
 
39
 
 
40
/*
 
41
** File:                jstypes.h
 
42
** Description: Definitions of NSPR's basic types
 
43
**
 
44
** Prototypes and macros used to make up for deficiencies in ANSI environments
 
45
** that we have found.
 
46
**
 
47
** Since we do not wrap <stdlib.h> and all the other standard headers, authors
 
48
** of portable code will not know in general that they need these definitions.
 
49
** Instead of requiring these authors to find the dependent uses in their code
 
50
** and take the following steps only in those C files, we take steps once here
 
51
** for all C files.
 
52
**/
 
53
 
 
54
#ifndef jstypes_h___
 
55
#define jstypes_h___
 
56
 
 
57
#include <stddef.h>
 
58
 
 
59
/***********************************************************************
 
60
** MACROS:      JS_EXTERN_API
 
61
**              JS_EXPORT_API
 
62
** DESCRIPTION:
 
63
**      These are only for externally visible routines and globals.  For
 
64
**      internal routines, just use "extern" for type checking and that
 
65
**      will not export internal cross-file or forward-declared symbols.
 
66
**      Define a macro for declaring procedures return types. We use this to
 
67
**      deal with windoze specific type hackery for DLL definitions. Use
 
68
**      JS_EXTERN_API when the prototype for the method is declared. Use
 
69
**      JS_EXPORT_API for the implementation of the method.
 
70
**
 
71
** Example:
 
72
**   in dowhim.h
 
73
**     JS_EXTERN_API( void ) DoWhatIMean( void );
 
74
**   in dowhim.c
 
75
**     JS_EXPORT_API( void ) DoWhatIMean( void ) { return; }
 
76
**
 
77
**
 
78
***********************************************************************/
 
79
#ifdef WIN32
 
80
/* These also work for __MWERKS__ */
 
81
#define JS_EXTERN_API(__type) extern __declspec(dllexport) __type
 
82
#define JS_EXPORT_API(__type) __declspec(dllexport) __type
 
83
#define JS_EXTERN_DATA(__type) extern __declspec(dllexport) __type
 
84
#define JS_EXPORT_DATA(__type) __declspec(dllexport) __type
 
85
 
 
86
#define JS_DLL_CALLBACK
 
87
#define JS_STATIC_DLL_CALLBACK(__x) static __x
 
88
 
 
89
#elif defined(XP_OS2) && defined(__declspec)
 
90
 
 
91
#define JS_EXTERN_API(__type) extern __declspec(dllexport) __type
 
92
#define JS_EXPORT_API(__type) __declspec(dllexport) __type
 
93
#define JS_EXTERN_DATA(__type) extern __declspec(dllexport) __type
 
94
#define JS_EXPORT_DATA(__type) __declspec(dllexport) __type
 
95
 
 
96
#define JS_DLL_CALLBACK
 
97
#define JS_STATIC_DLL_CALLBACK(__x) static __x
 
98
 
 
99
#elif defined(WIN16)
 
100
 
 
101
#ifdef _WINDLL
 
102
#define JS_EXTERN_API(__type) extern __type _cdecl _export _loadds
 
103
#define JS_EXPORT_API(__type) __type _cdecl _export _loadds
 
104
#define JS_EXTERN_DATA(__type) extern __type _export
 
105
#define JS_EXPORT_DATA(__type) __type _export
 
106
 
 
107
#define JS_DLL_CALLBACK             __cdecl __loadds
 
108
#define JS_STATIC_DLL_CALLBACK(__x) static __x CALLBACK
 
109
 
 
110
#else /* this must be .EXE */
 
111
#define JS_EXTERN_API(__type) extern __type _cdecl _export
 
112
#define JS_EXPORT_API(__type) __type _cdecl _export
 
113
#define JS_EXTERN_DATA(__type) extern __type _export
 
114
#define JS_EXPORT_DATA(__type) __type _export
 
115
 
 
116
#define JS_DLL_CALLBACK             __cdecl __loadds
 
117
#define JS_STATIC_DLL_CALLBACK(__x) __x JS_DLL_CALLBACK
 
118
#endif /* _WINDLL */
 
119
 
 
120
#else /* Unix */
 
121
 
 
122
#ifdef HAVE_VISIBILITY_ATTRIBUTE
 
123
#define JS_EXTERNAL_VIS __attribute__((visibility ("default")))
 
124
#else
 
125
#define JS_EXTERNAL_VIS
 
126
#endif
 
127
 
 
128
#define JS_EXTERN_API(__type) extern JS_EXTERNAL_VIS __type
 
129
#define JS_EXPORT_API(__type) JS_EXTERNAL_VIS __type
 
130
#define JS_EXTERN_DATA(__type) extern JS_EXTERNAL_VIS __type
 
131
#define JS_EXPORT_DATA(__type) JS_EXTERNAL_VIS __type
 
132
 
 
133
#define JS_DLL_CALLBACK
 
134
#define JS_STATIC_DLL_CALLBACK(__x) static __x
 
135
 
 
136
#endif
 
137
 
 
138
#ifdef _WIN32
 
139
#  if defined(__MWERKS__) || defined(__GNUC__)
 
140
#    define JS_IMPORT_API(__x)      __x
 
141
#  else
 
142
#    define JS_IMPORT_API(__x)      __declspec(dllimport) __x
 
143
#  endif
 
144
#elif defined(XP_OS2) && defined(__declspec)
 
145
#    define JS_IMPORT_API(__x)      __declspec(dllimport) __x
 
146
#else
 
147
#    define JS_IMPORT_API(__x)      JS_EXPORT_API (__x)
 
148
#endif
 
149
 
 
150
#if defined(_WIN32) && !defined(__MWERKS__)
 
151
#    define JS_IMPORT_DATA(__x)      __declspec(dllimport) __x
 
152
#elif defined(XP_OS2) && defined(__declspec)
 
153
#    define JS_IMPORT_DATA(__x)      __declspec(dllimport) __x
 
154
#else
 
155
#    define JS_IMPORT_DATA(__x)     JS_EXPORT_DATA (__x)
 
156
#endif
 
157
 
 
158
/*
 
159
 * The linkage of JS API functions differs depending on whether the file is
 
160
 * used within the JS library or not.  Any source file within the JS
 
161
 * interpreter should define EXPORT_JS_API whereas any client of the library
 
162
 * should not.
 
163
 */
 
164
#ifdef EXPORT_JS_API
 
165
#define JS_PUBLIC_API(t)    JS_EXPORT_API(t)
 
166
#define JS_PUBLIC_DATA(t)   JS_EXPORT_DATA(t)
 
167
#else
 
168
#define JS_PUBLIC_API(t)    JS_IMPORT_API(t)
 
169
#define JS_PUBLIC_DATA(t)   JS_IMPORT_DATA(t)
 
170
#endif
 
171
 
 
172
#define JS_FRIEND_API(t)    JS_PUBLIC_API(t)
 
173
#define JS_FRIEND_DATA(t)   JS_PUBLIC_DATA(t)
 
174
 
 
175
#ifdef _WIN32
 
176
#   define JS_INLINE __inline
 
177
#elif defined(__GNUC__)
 
178
#   define JS_INLINE
 
179
#else
 
180
#   define JS_INLINE
 
181
#endif
 
182
 
 
183
/***********************************************************************
 
184
** MACROS:      JS_BEGIN_MACRO
 
185
**              JS_END_MACRO
 
186
** DESCRIPTION:
 
187
**      Macro body brackets so that macros with compound statement definitions
 
188
**      behave syntactically more like functions when called.
 
189
***********************************************************************/
 
190
#define JS_BEGIN_MACRO  do {
 
191
#define JS_END_MACRO    } while (0)
 
192
 
 
193
/***********************************************************************
 
194
** MACROS:      JS_BEGIN_EXTERN_C
 
195
**              JS_END_EXTERN_C
 
196
** DESCRIPTION:
 
197
**      Macro shorthands for conditional C++ extern block delimiters.
 
198
***********************************************************************/
 
199
#ifdef __cplusplus
 
200
#define JS_BEGIN_EXTERN_C       extern "C" {
 
201
#define JS_END_EXTERN_C         }
 
202
#else
 
203
#define JS_BEGIN_EXTERN_C
 
204
#define JS_END_EXTERN_C
 
205
#endif
 
206
 
 
207
/***********************************************************************
 
208
** MACROS:      JS_BIT
 
209
**              JS_BITMASK
 
210
** DESCRIPTION:
 
211
** Bit masking macros.  XXX n must be <= 31 to be portable
 
212
***********************************************************************/
 
213
#define JS_BIT(n)       ((JSUint32)1 << (n))
 
214
#define JS_BITMASK(n)   (JS_BIT(n) - 1)
 
215
 
 
216
/***********************************************************************
 
217
** MACROS:      JS_PTR_TO_INT32
 
218
**              JS_PTR_TO_UINT32
 
219
**              JS_INT32_TO_PTR
 
220
**              JS_UINT32_TO_PTR
 
221
** DESCRIPTION:
 
222
** Integer to pointer and pointer to integer conversion macros.
 
223
***********************************************************************/
 
224
#define JS_PTR_TO_INT32(x)  ((jsint)((char *)(x) - (char *)0))
 
225
#define JS_PTR_TO_UINT32(x) ((jsuint)((char *)(x) - (char *)0))
 
226
#define JS_INT32_TO_PTR(x)  ((void *)((char *)0 + (jsint)(x)))
 
227
#define JS_UINT32_TO_PTR(x) ((void *)((char *)0 + (jsuint)(x)))
 
228
 
 
229
/***********************************************************************
 
230
** MACROS:      JS_HOWMANY
 
231
**              JS_ROUNDUP
 
232
**              JS_MIN
 
233
**              JS_MAX
 
234
** DESCRIPTION:
 
235
**      Commonly used macros for operations on compatible types.
 
236
***********************************************************************/
 
237
#define JS_HOWMANY(x,y) (((x)+(y)-1)/(y))
 
238
#define JS_ROUNDUP(x,y) (JS_HOWMANY(x,y)*(y))
 
239
#define JS_MIN(x,y)     ((x)<(y)?(x):(y))
 
240
#define JS_MAX(x,y)     ((x)>(y)?(x):(y))
 
241
 
 
242
#if (defined(XP_WIN) && !defined(CROSS_COMPILE)) || defined (WINCE)
 
243
#    include "jscpucfg.h"        /* Use standard Mac or Windows configuration */
 
244
#elif defined(XP_UNIX) || defined(XP_BEOS) || defined(XP_OS2) || defined(CROSS_COMPILE)
 
245
#    include "jsautocfg.h"       /* Use auto-detected configuration */
 
246
#    include "jsosdep.h"         /* ...and platform-specific flags */
 
247
#else
 
248
#    error "Must define one of XP_BEOS, XP_OS2, XP_WIN or XP_UNIX"
 
249
#endif
 
250
 
 
251
JS_BEGIN_EXTERN_C
 
252
 
 
253
/************************************************************************
 
254
** TYPES:       JSUint8
 
255
**              JSInt8
 
256
** DESCRIPTION:
 
257
**  The int8 types are known to be 8 bits each. There is no type that
 
258
**      is equivalent to a plain "char".
 
259
************************************************************************/
 
260
#if JS_BYTES_PER_BYTE == 1
 
261
typedef unsigned char JSUint8;
 
262
typedef signed char JSInt8;
 
263
#else
 
264
#error No suitable type for JSInt8/JSUint8
 
265
#endif
 
266
 
 
267
/************************************************************************
 
268
** TYPES:       JSUint16
 
269
**              JSInt16
 
270
** DESCRIPTION:
 
271
**  The int16 types are known to be 16 bits each.
 
272
************************************************************************/
 
273
#if JS_BYTES_PER_SHORT == 2
 
274
typedef unsigned short JSUint16;
 
275
typedef short JSInt16;
 
276
#else
 
277
#error No suitable type for JSInt16/JSUint16
 
278
#endif
 
279
 
 
280
/************************************************************************
 
281
** TYPES:       JSUint32
 
282
**              JSInt32
 
283
** DESCRIPTION:
 
284
**  The int32 types are known to be 32 bits each.
 
285
************************************************************************/
 
286
#if JS_BYTES_PER_INT == 4
 
287
typedef unsigned int JSUint32;
 
288
typedef int JSInt32;
 
289
#define JS_INT32(x)  x
 
290
#define JS_UINT32(x) x ## U
 
291
#elif JS_BYTES_PER_LONG == 4
 
292
typedef unsigned long JSUint32;
 
293
typedef long JSInt32;
 
294
#define JS_INT32(x)  x ## L
 
295
#define JS_UINT32(x) x ## UL
 
296
#else
 
297
#error No suitable type for JSInt32/JSUint32
 
298
#endif
 
299
 
 
300
/************************************************************************
 
301
** TYPES:       JSUint64
 
302
**              JSInt64
 
303
** DESCRIPTION:
 
304
**  The int64 types are known to be 64 bits each. Care must be used when
 
305
**      declaring variables of type JSUint64 or JSInt64. Different hardware
 
306
**      architectures and even different compilers have varying support for
 
307
**      64 bit values. The only guaranteed portability requires the use of
 
308
**      the JSLL_ macros (see jslong.h).
 
309
************************************************************************/
 
310
#ifdef JS_HAVE_LONG_LONG
 
311
#if JS_BYTES_PER_LONG == 8
 
312
typedef long JSInt64;
 
313
typedef unsigned long JSUint64;
 
314
#elif defined(WIN16)
 
315
typedef __int64 JSInt64;
 
316
typedef unsigned __int64 JSUint64;
 
317
#elif defined(WIN32) && !defined(__GNUC__)
 
318
typedef __int64  JSInt64;
 
319
typedef unsigned __int64 JSUint64;
 
320
#else
 
321
typedef long long JSInt64;
 
322
typedef unsigned long long JSUint64;
 
323
#endif /* JS_BYTES_PER_LONG == 8 */
 
324
#else  /* !JS_HAVE_LONG_LONG */
 
325
typedef struct {
 
326
#ifdef IS_LITTLE_ENDIAN
 
327
    JSUint32 lo, hi;
 
328
#else
 
329
    JSUint32 hi, lo;
 
330
#endif
 
331
} JSInt64;
 
332
typedef JSInt64 JSUint64;
 
333
#endif /* !JS_HAVE_LONG_LONG */
 
334
 
 
335
/************************************************************************
 
336
** TYPES:       JSUintn
 
337
**              JSIntn
 
338
** DESCRIPTION:
 
339
**  The JSIntn types are most appropriate for automatic variables. They are
 
340
**      guaranteed to be at least 16 bits, though various architectures may
 
341
**      define them to be wider (e.g., 32 or even 64 bits). These types are
 
342
**      never valid for fields of a structure.
 
343
************************************************************************/
 
344
#if JS_BYTES_PER_INT >= 2
 
345
typedef int JSIntn;
 
346
typedef unsigned int JSUintn;
 
347
#else
 
348
#error 'sizeof(int)' not sufficient for platform use
 
349
#endif
 
350
 
 
351
/************************************************************************
 
352
** TYPES:       JSFloat64
 
353
** DESCRIPTION:
 
354
**  NSPR's floating point type is always 64 bits.
 
355
************************************************************************/
 
356
typedef double          JSFloat64;
 
357
 
 
358
/************************************************************************
 
359
** TYPES:       JSSize
 
360
** DESCRIPTION:
 
361
**  A type for representing the size of objects.
 
362
************************************************************************/
 
363
typedef size_t JSSize;
 
364
 
 
365
/************************************************************************
 
366
** TYPES:       JSPtrDiff
 
367
** DESCRIPTION:
 
368
**  A type for pointer difference. Variables of this type are suitable
 
369
**      for storing a pointer or pointer sutraction.
 
370
************************************************************************/
 
371
typedef ptrdiff_t JSPtrdiff;
 
372
 
 
373
/************************************************************************
 
374
** TYPES:       JSUptrdiff
 
375
** DESCRIPTION:
 
376
**  A type for pointer difference. Variables of this type are suitable
 
377
**      for storing a pointer or pointer sutraction.
 
378
************************************************************************/
 
379
#if JS_BYTES_PER_WORD == 8 && JS_BYTES_PER_LONG != 8
 
380
typedef JSUint64 JSUptrdiff;
 
381
#else
 
382
typedef unsigned long JSUptrdiff;
 
383
#endif
 
384
 
 
385
/************************************************************************
 
386
** TYPES:       JSBool
 
387
** DESCRIPTION:
 
388
**  Use JSBool for variables and parameter types. Use JS_FALSE and JS_TRUE
 
389
**      for clarity of target type in assignments and actual arguments. Use
 
390
**      'if (bool)', 'while (!bool)', '(bool) ? x : y' etc., to test booleans
 
391
**      just as you would C int-valued conditions.
 
392
************************************************************************/
 
393
typedef JSIntn JSBool;
 
394
#define JS_TRUE (JSIntn)1
 
395
#define JS_FALSE (JSIntn)0
 
396
 
 
397
/************************************************************************
 
398
** TYPES:       JSPackedBool
 
399
** DESCRIPTION:
 
400
**  Use JSPackedBool within structs where bitfields are not desireable
 
401
**      but minimum and consistent overhead matters.
 
402
************************************************************************/
 
403
typedef JSUint8 JSPackedBool;
 
404
 
 
405
/*
 
406
** A JSWord is an integer that is the same size as a void*
 
407
*/
 
408
#if JS_BYTES_PER_WORD == 8 && JS_BYTES_PER_LONG != 8
 
409
typedef JSInt64 JSWord;
 
410
typedef JSUint64 JSUword;
 
411
#else
 
412
typedef long JSWord;
 
413
typedef unsigned long JSUword;
 
414
#endif
 
415
 
 
416
#include "jsotypes.h"
 
417
 
 
418
/***********************************************************************
 
419
** MACROS:      JS_LIKELY
 
420
**              JS_UNLIKELY
 
421
** DESCRIPTION:
 
422
**      These macros allow you to give a hint to the compiler about branch
 
423
**      probability so that it can better optimize.  Use them like this:
 
424
**
 
425
**      if (JS_LIKELY(v == 1)) {
 
426
**          ... expected code path ...
 
427
**      }
 
428
**
 
429
**      if (JS_UNLIKELY(v == 0)) {
 
430
**          ... non-expected code path ...
 
431
**      }
 
432
**
 
433
***********************************************************************/
 
434
#if defined(__GNUC__) && (__GNUC__ > 2)
 
435
#define JS_LIKELY(x)    (__builtin_expect((x), 1))
 
436
#define JS_UNLIKELY(x)  (__builtin_expect((x), 0))
 
437
#else
 
438
#define JS_LIKELY(x)    (x)
 
439
#define JS_UNLIKELY(x)  (x)
 
440
#endif
 
441
 
 
442
/***********************************************************************
 
443
** MACROS:      JS_ARRAY_LENGTH
 
444
**              JS_ARRAY_END
 
445
** DESCRIPTION:
 
446
**      Macros to get the number of elements and the pointer to one past the
 
447
**      last element of a C array. Use them like this:
 
448
**
 
449
**      jschar buf[10], *s;
 
450
**      JSString *str;
 
451
**      ...
 
452
**      for (s = buf; s != JS_ARRAY_END(buf); ++s) *s = ...;
 
453
**      ...
 
454
**      str = JS_NewStringCopyN(cx, buf, JS_ARRAY_LENGTH(buf));
 
455
**      ...
 
456
**
 
457
***********************************************************************/
 
458
 
 
459
#define JS_ARRAY_LENGTH(array) (sizeof (array) / sizeof (array)[0])
 
460
#define JS_ARRAY_END(array)    ((array) + JS_ARRAY_LENGTH(array))
 
461
 
 
462
JS_END_EXTERN_C
 
463
 
 
464
#endif /* jstypes_h___ */