Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / jstypes.h
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 #include "js-config.h"
59
60 /***********************************************************************
61 ** MACROS:      JS_EXTERN_API
62 **              JS_EXPORT_API
63 ** DESCRIPTION:
64 **      These are only for externally visible routines and globals.  For
65 **      internal routines, just use "extern" for type checking and that
66 **      will not export internal cross-file or forward-declared symbols.
67 **      Define a macro for declaring procedures return types. We use this to
68 **      deal with windoze specific type hackery for DLL definitions. Use
69 **      JS_EXTERN_API when the prototype for the method is declared. Use
70 **      JS_EXPORT_API for the implementation of the method.
71 **
72 ** Example:
73 **   in dowhim.h
74 **     JS_EXTERN_API( void ) DoWhatIMean( void );
75 **   in dowhim.c
76 **     JS_EXPORT_API( void ) DoWhatIMean( void ) { return; }
77 **
78 **
79 ***********************************************************************/
80
81 #define DEFINE_LOCAL_CLASS_OF_STATIC_FUNCTION(Name) class Name
82
83 #if defined(WIN32) || defined(XP_OS2)
84
85 /* These also work for __MWERKS__ */
86 # define JS_EXTERN_API(__type)  extern __declspec(dllexport) __type
87 # define JS_EXPORT_API(__type)  __declspec(dllexport) __type
88 # define JS_EXTERN_DATA(__type) extern __declspec(dllexport) __type
89 # define JS_EXPORT_DATA(__type) __declspec(dllexport) __type
90
91 #elif defined(__SYMBIAN32__)
92
93 # define JS_EXTERN_API(__type) extern EXPORT_C __type
94 # define JS_EXPORT_API(__type) EXPORT_C __type
95 # define JS_EXTERN_DATA(__type) extern EXPORT_C __type
96 # define JS_EXPORT_DATA(__type) EXPORT_C __type
97
98 #else /* Unix */
99
100 # ifdef HAVE_VISIBILITY_ATTRIBUTE
101 #  define JS_EXTERNAL_VIS __attribute__((visibility ("default")))
102 #  if defined(__GNUC__) && __GNUC__ <= 4 && __GNUC_MINOR__ < 5
103     /*
104      * GCC wrongly produces a warning when a type with hidden visibility
105      * (e.g. js::Value) is a member of a local class of a static function.
106      * This is apparently fixed with GCC 4.5 and above.  See:
107      *
108      *   http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40145.
109      */
110 #   undef  DEFINE_LOCAL_CLASS_OF_STATIC_FUNCTION
111 #   define DEFINE_LOCAL_CLASS_OF_STATIC_FUNCTION(Name) class __attribute__((visibility ("hidden"))) Name
112 #  endif
113 # elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
114 #  define JS_EXTERNAL_VIS __global
115 # else
116 #  define JS_EXTERNAL_VIS
117 # endif
118
119 # define JS_EXTERN_API(__type)  extern JS_EXTERNAL_VIS __type
120 # define JS_EXPORT_API(__type)  JS_EXTERNAL_VIS __type
121 # define JS_EXTERN_DATA(__type) extern JS_EXTERNAL_VIS __type
122 # define JS_EXPORT_DATA(__type) JS_EXTERNAL_VIS __type
123
124 #endif
125
126 #ifdef _WIN32
127 # if defined(__MWERKS__) || defined(__GNUC__)
128 #  define JS_IMPORT_API(__x)    __x
129 # else
130 #  define JS_IMPORT_API(__x)    __declspec(dllimport) __x
131 # endif
132 #elif defined(XP_OS2)
133 # define JS_IMPORT_API(__x)     __declspec(dllimport) __x
134 #elif defined(__SYMBIAN32__)
135 # define JS_IMPORT_API(__x)     IMPORT_C __x
136 #else
137 # define JS_IMPORT_API(__x)     JS_EXPORT_API (__x)
138 #endif
139
140 #if defined(_WIN32) && !defined(__MWERKS__)
141 # define JS_IMPORT_DATA(__x)      __declspec(dllimport) __x
142 #elif defined(XP_OS2)
143 # define JS_IMPORT_DATA(__x)      __declspec(dllimport) __x
144 #elif defined(__SYMBIAN32__)
145 # if defined(__CW32__)
146 #   define JS_IMPORT_DATA(__x)    __declspec(dllimport) __x
147 # else
148 #   define JS_IMPORT_DATA(__x)    IMPORT_C __x
149 # endif
150 #else
151 # define JS_IMPORT_DATA(__x)     JS_EXPORT_DATA (__x)
152 #endif
153
154 /*
155  * The linkage of JS API functions differs depending on whether the file is
156  * used within the JS library or not. Any source file within the JS
157  * interpreter should define EXPORT_JS_API whereas any client of the library
158  * should not. STATIC_JS_API is used to build JS as a static library.
159  */
160 #if defined(STATIC_JS_API)
161
162 # define JS_PUBLIC_API(t)   t
163 # define JS_PUBLIC_DATA(t)  t
164
165 #elif defined(EXPORT_JS_API) || defined(STATIC_EXPORTABLE_JS_API)
166
167 # define JS_PUBLIC_API(t)   JS_EXPORT_API(t)
168 # define JS_PUBLIC_DATA(t)  JS_EXPORT_DATA(t)
169
170 #else
171
172 # define JS_PUBLIC_API(t)   JS_IMPORT_API(t)
173 # define JS_PUBLIC_DATA(t)  JS_IMPORT_DATA(t)
174
175 #endif
176
177 #define JS_FRIEND_API(t)    JS_PUBLIC_API(t)
178 #define JS_FRIEND_DATA(t)   JS_PUBLIC_DATA(t)
179
180 #if defined(_MSC_VER) && defined(_M_IX86)
181 #define JS_FASTCALL __fastcall
182 #elif defined(__GNUC__) && defined(__i386__) &&                         \
183   ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
184 #define JS_FASTCALL __attribute__((fastcall))
185 #else
186 #define JS_FASTCALL
187 #define JS_NO_FASTCALL
188 #endif
189
190 #ifndef JS_INLINE
191 # if defined __cplusplus
192 #  define JS_INLINE          inline
193 # elif defined _MSC_VER
194 #  define JS_INLINE          __inline
195 # elif defined __GNUC__
196 #  define JS_INLINE          __inline__
197 # else
198 #  define JS_INLINE          inline
199 # endif
200 #endif
201
202 #ifndef JS_ALWAYS_INLINE
203 # if defined DEBUG
204 #  define JS_ALWAYS_INLINE   JS_INLINE
205 # elif defined _MSC_VER
206 #  define JS_ALWAYS_INLINE   __forceinline
207 # elif defined __GNUC__
208 #  define JS_ALWAYS_INLINE   __attribute__((always_inline)) JS_INLINE
209 # else
210 #  define JS_ALWAYS_INLINE   JS_INLINE
211 # endif
212 #endif
213
214 #ifndef JS_NEVER_INLINE
215 # if defined _MSC_VER
216 #  define JS_NEVER_INLINE __declspec(noinline)
217 # elif defined __GNUC__
218 #  define JS_NEVER_INLINE __attribute__((noinline))
219 # else
220 #  define JS_NEVER_INLINE
221 # endif
222 #endif
223
224 #ifndef JS_WARN_UNUSED_RESULT
225 # if defined __GNUC__
226 #  define JS_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
227 # else
228 #  define JS_WARN_UNUSED_RESULT
229 # endif
230 #endif
231
232 #ifdef NS_STATIC_CHECKING
233 /*
234  * Attributes for static analysis. Functions declared with JS_REQUIRES_STACK
235  * always have a valid cx->fp and can access it freely.  Other functions can
236  * access cx->fp only after calling a function that "forces" the stack
237  * (i.e. lazily instantiates it as needed).
238  */
239 # define JS_REQUIRES_STACK   __attribute__((user("JS_REQUIRES_STACK")))
240 # define JS_FORCES_STACK     __attribute__((user("JS_FORCES_STACK")))
241 /*
242  * Skip the JS_REQUIRES_STACK analysis within functions with this annotation.
243  */
244 # define JS_IGNORE_STACK     __attribute__((user("JS_IGNORE_STACK")))
245 #else
246 # define JS_REQUIRES_STACK
247 # define JS_FORCES_STACK
248 # define JS_IGNORE_STACK
249 #endif
250
251 /***********************************************************************
252 ** MACROS:      JS_BEGIN_MACRO
253 **              JS_END_MACRO
254 ** DESCRIPTION:
255 **      Macro body brackets so that macros with compound statement definitions
256 **      behave syntactically more like functions when called.
257 ***********************************************************************/
258 #define JS_BEGIN_MACRO  do {
259
260 #if defined(_MSC_VER) && _MSC_VER >= 1400
261 # define JS_END_MACRO                                                         \
262     } __pragma(warning(push)) __pragma(warning(disable:4127))                 \
263     while (0) __pragma(warning(pop))
264 #else
265 # define JS_END_MACRO   } while (0)
266 #endif
267
268 /***********************************************************************
269 ** MACROS:      JS_BEGIN_EXTERN_C
270 **              JS_END_EXTERN_C
271 ** DESCRIPTION:
272 **      Macro shorthands for conditional C++ extern block delimiters.
273 ***********************************************************************/
274 #ifdef __cplusplus
275
276 # define JS_BEGIN_EXTERN_C      extern "C" {
277 # define JS_END_EXTERN_C        }
278
279 #else
280
281 # define JS_BEGIN_EXTERN_C
282 # define JS_END_EXTERN_C
283
284 #endif
285
286 /***********************************************************************
287 ** MACROS:      JS_BIT
288 **              JS_BITMASK
289 ** DESCRIPTION:
290 ** Bit masking macros.  XXX n must be <= 31 to be portable
291 ***********************************************************************/
292 #define JS_BIT(n)       ((JSUint32)1 << (n))
293 #define JS_BITMASK(n)   (JS_BIT(n) - 1)
294
295 /***********************************************************************
296 ** MACROS:      JS_HOWMANY
297 **              JS_ROUNDUP
298 **              JS_MIN
299 **              JS_MAX
300 ** DESCRIPTION:
301 **      Commonly used macros for operations on compatible types.
302 ***********************************************************************/
303 #define JS_HOWMANY(x,y) (((x)+(y)-1)/(y))
304 #define JS_ROUNDUP(x,y) (JS_HOWMANY(x,y)*(y))
305 #define JS_MIN(x,y)     ((x)<(y)?(x):(y))
306 #define JS_MAX(x,y)     ((x)>(y)?(x):(y))
307
308 #ifdef _MSC_VER
309 # include "jscpucfg.h"  /* We can't auto-detect MSVC configuration */
310 # if _MSC_VER < 1400
311 #  define NJ_NO_VARIADIC_MACROS
312 # endif
313 #else
314 # include "jsautocfg.h" /* Use auto-detected configuration */
315 #endif
316
317 /*
318  * Define JS_64BIT iff we are building in an environment with 64-bit
319  * addresses.
320  */
321 #ifdef _MSC_VER
322 # if defined(_M_X64) || defined(_M_AMD64)
323 #  define JS_64BIT
324 # endif
325 #elif defined(__GNUC__)
326 # ifdef __x86_64__
327 #  define JS_64BIT
328 # endif
329 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
330 # ifdef __x86_64
331 #  define JS_64BIT
332 # endif
333 #else
334 # error "Implement me"
335 #endif
336
337
338 #include "jsinttypes.h"
339
340 JS_BEGIN_EXTERN_C
341
342 /************************************************************************
343 ** TYPES:       JSUintn
344 **              JSIntn
345 ** DESCRIPTION:
346 **  The JSIntn types are most appropriate for automatic variables. They are
347 **      guaranteed to be at least 16 bits, though various architectures may
348 **      define them to be wider (e.g., 32 or even 64 bits). These types are
349 **      never valid for fields of a structure.
350 ************************************************************************/
351
352 typedef int JSIntn;
353 typedef unsigned int JSUintn;
354
355 /************************************************************************
356 ** TYPES:       JSFloat64
357 ** DESCRIPTION:
358 **  NSPR's floating point type is always 64 bits.
359 ************************************************************************/
360 typedef double          JSFloat64;
361
362 /************************************************************************
363 ** TYPES:       JSSize
364 ** DESCRIPTION:
365 **  A type for representing the size of objects.
366 ************************************************************************/
367 typedef size_t JSSize;
368
369 /************************************************************************
370 ** TYPES:       JSPtrDiff
371 ** DESCRIPTION:
372 **  A type for pointer difference. Variables of this type are suitable
373 **      for storing a pointer or pointer sutraction.
374 ************************************************************************/
375 typedef ptrdiff_t JSPtrdiff;
376
377 /************************************************************************
378 ** TYPES:       JSUptrdiff
379 ** DESCRIPTION:
380 **  A type for pointer difference. Variables of this type are suitable
381 **      for storing a pointer or pointer sutraction.
382 ************************************************************************/
383 typedef JSUintPtr JSUptrdiff;
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 ** Special: JS_NEITHER is used by the tracer to have tri-state booleans.
398 ** This should not be used in new code.
399 */
400 #define JS_NEITHER (JSIntn)2
401
402 /************************************************************************
403 ** TYPES:       JSPackedBool
404 ** DESCRIPTION:
405 **  Use JSPackedBool within structs where bitfields are not desireable
406 **      but minimum and consistent overhead matters.
407 ************************************************************************/
408 typedef JSUint8 JSPackedBool;
409
410 /*
411 ** A JSWord is an integer that is the same size as a void*
412 */
413 typedef JSIntPtr JSWord;
414 typedef JSUintPtr JSUword;
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
436 # define JS_LIKELY(x)   (__builtin_expect((x), 1))
437 # define JS_UNLIKELY(x) (__builtin_expect((x), 0))
438
439 #else
440
441 # define JS_LIKELY(x)   (x)
442 # define JS_UNLIKELY(x) (x)
443
444 #endif
445
446 /***********************************************************************
447 ** MACROS:      JS_ARRAY_LENGTH
448 **              JS_ARRAY_END
449 ** DESCRIPTION:
450 **      Macros to get the number of elements and the pointer to one past the
451 **      last element of a C array. Use them like this:
452 **
453 **      jschar buf[10], *s;
454 **      JSString *str;
455 **      ...
456 **      for (s = buf; s != JS_ARRAY_END(buf); ++s) *s = ...;
457 **      ...
458 **      str = JS_NewStringCopyN(cx, buf, JS_ARRAY_LENGTH(buf));
459 **      ...
460 **
461 ***********************************************************************/
462
463 #define JS_ARRAY_LENGTH(array) (sizeof (array) / sizeof (array)[0])
464 #define JS_ARRAY_END(array)    ((array) + JS_ARRAY_LENGTH(array))
465
466 #define JS_BITS_PER_BYTE 8
467 #define JS_BITS_PER_BYTE_LOG2 3
468
469 #define JS_BITS_PER_WORD (JS_BITS_PER_BYTE * JS_BYTES_PER_WORD)
470 #define JS_BITS_PER_DOUBLE (JS_BITS_PER_BYTE * JS_BYTES_PER_DOUBLE)
471
472 /***********************************************************************
473 ** MACROS:      JS_FUNC_TO_DATA_PTR
474 **              JS_DATA_TO_FUNC_PTR
475 ** DESCRIPTION:
476 **      Macros to convert between function and data pointers assuming that
477 **      they have the same size. Use them like this:
478 **
479 **      JSPropertyOp nativeGetter;
480 **      JSObject *scriptedGetter;
481 **      ...
482 **      scriptedGetter = JS_FUNC_TO_DATA_PTR(JSObject *, nativeGetter);
483 **      ...
484 **      nativeGetter = JS_DATA_TO_FUNC_PTR(JSPropertyOp, scriptedGetter);
485 **
486 ***********************************************************************/
487
488 #ifdef __GNUC__
489 # define JS_FUNC_TO_DATA_PTR(type, fun) (__extension__ (type) (size_t) (fun))
490 # define JS_DATA_TO_FUNC_PTR(type, ptr) (__extension__ (type) (size_t) (ptr))
491 #else
492 /* Use an extra (void *) cast for MSVC. */
493 # define JS_FUNC_TO_DATA_PTR(type, fun) ((type) (void *) (fun))
494 # define JS_DATA_TO_FUNC_PTR(type, ptr) ((type) (void *) (ptr))
495 #endif
496
497 #ifdef __GNUC__
498 # define JS_EXTENSION __extension__
499 # define JS_EXTENSION_(s) __extension__ ({ s; })
500 #else
501 # define JS_EXTENSION
502 # define JS_EXTENSION_(s) s
503 #endif
504
505 JS_END_EXTERN_C
506
507 #endif /* jstypes_h___ */