js/src/jstypes.h

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:27da8a4ad253
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sts=4 et sw=4 tw=99:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 /*
8 ** File: jstypes.h
9 ** Description: Definitions of NSPR's basic types
10 **
11 ** Prototypes and macros used to make up for deficiencies in ANSI environments
12 ** that we have found.
13 **
14 ** Since we do not wrap <stdlib.h> and all the other standard headers, authors
15 ** of portable code will not know in general that they need these definitions.
16 ** Instead of requiring these authors to find the dependent uses in their code
17 ** and take the following steps only in those C files, we take steps once here
18 ** for all C files.
19 **/
20
21 #ifndef jstypes_h
22 #define jstypes_h
23
24 #include "mozilla/Attributes.h"
25 #include "mozilla/Types.h"
26
27 // jstypes.h is (or should be!) included by every file in SpiderMonkey.
28 // js-config.h and jsversion.h also should be included by every file.
29 // So include them here.
30 // XXX: including them in js/RequiredDefines.h should be a better option, since
31 // that is by definition the header file that should be included in all
32 // SpiderMonkey code. However, Gecko doesn't do this! See bug 909576.
33 #include "js-config.h"
34 #include "jsversion.h"
35
36 /***********************************************************************
37 ** MACROS: JS_EXTERN_API
38 ** JS_EXPORT_API
39 ** DESCRIPTION:
40 ** These are only for externally visible routines and globals. For
41 ** internal routines, just use "extern" for type checking and that
42 ** will not export internal cross-file or forward-declared symbols.
43 ** Define a macro for declaring procedures return types. We use this to
44 ** deal with windoze specific type hackery for DLL definitions. Use
45 ** JS_EXTERN_API when the prototype for the method is declared. Use
46 ** JS_EXPORT_API for the implementation of the method.
47 **
48 ** Example:
49 ** in dowhim.h
50 ** JS_EXTERN_API( void ) DoWhatIMean( void );
51 ** in dowhim.c
52 ** JS_EXPORT_API( void ) DoWhatIMean( void ) { return; }
53 **
54 **
55 ***********************************************************************/
56
57 #define JS_EXTERN_API(type) extern MOZ_EXPORT type
58 #define JS_EXPORT_API(type) MOZ_EXPORT type
59 #define JS_EXPORT_DATA(type) MOZ_EXPORT type
60 #define JS_IMPORT_API(type) MOZ_IMPORT_API type
61 #define JS_IMPORT_DATA(type) MOZ_IMPORT_DATA type
62
63 /*
64 * The linkage of JS API functions differs depending on whether the file is
65 * used within the JS library or not. Any source file within the JS
66 * interpreter should define EXPORT_JS_API whereas any client of the library
67 * should not. STATIC_JS_API is used to build JS as a static library.
68 */
69 #if defined(STATIC_JS_API)
70 # define JS_PUBLIC_API(t) t
71 # define JS_PUBLIC_DATA(t) t
72 #elif defined(EXPORT_JS_API) || defined(STATIC_EXPORTABLE_JS_API)
73 # define JS_PUBLIC_API(t) MOZ_EXPORT t
74 # define JS_PUBLIC_DATA(t) MOZ_EXPORT t
75 #else
76 # define JS_PUBLIC_API(t) MOZ_IMPORT_API t
77 # define JS_PUBLIC_DATA(t) MOZ_IMPORT_DATA t
78 #endif
79
80 #if defined(STATIC_JS_API) || defined(EXPORT_JS_API) || defined(STATIC_EXPORTABLE_JS_API)
81 # define JS_FRIEND_API(t) MOZ_EXPORT t
82 # define JS_FRIEND_DATA(t) MOZ_EXPORT t
83 #else
84 # define JS_FRIEND_API(t) MOZ_IMPORT_API t
85 # define JS_FRIEND_DATA(t) MOZ_IMPORT_DATA t
86 #endif
87
88 #if defined(_MSC_VER) && defined(_M_IX86)
89 #define JS_FASTCALL __fastcall
90 #elif defined(__GNUC__) && defined(__i386__)
91 #define JS_FASTCALL __attribute__((fastcall))
92 #else
93 #define JS_FASTCALL
94 #define JS_NO_FASTCALL
95 #endif
96
97 /***********************************************************************
98 ** MACROS: JS_BEGIN_MACRO
99 ** JS_END_MACRO
100 ** DESCRIPTION:
101 ** Macro body brackets so that macros with compound statement definitions
102 ** behave syntactically more like functions when called.
103 ***********************************************************************/
104 #define JS_BEGIN_MACRO do {
105
106 #if defined(_MSC_VER) && _MSC_VER >= 1400
107 # define JS_END_MACRO \
108 } __pragma(warning(push)) __pragma(warning(disable:4127)) \
109 while (0) __pragma(warning(pop))
110 #else
111 # define JS_END_MACRO } while (0)
112 #endif
113
114 /***********************************************************************
115 ** MACROS: JS_BIT
116 ** JS_BITMASK
117 ** DESCRIPTION:
118 ** Bit masking macros. XXX n must be <= 31 to be portable
119 ***********************************************************************/
120 #define JS_BIT(n) ((uint32_t)1 << (n))
121 #define JS_BITMASK(n) (JS_BIT(n) - 1)
122
123 /***********************************************************************
124 ** MACROS: JS_HOWMANY
125 ** JS_ROUNDUP
126 ** DESCRIPTION:
127 ** Commonly used macros for operations on compatible types.
128 ***********************************************************************/
129 #define JS_HOWMANY(x,y) (((x)+(y)-1)/(y))
130 #define JS_ROUNDUP(x,y) (JS_HOWMANY(x,y)*(y))
131
132 #include "jscpucfg.h"
133
134 /*
135 * Define JS_64BIT iff we are building in an environment with 64-bit
136 * addresses.
137 */
138 #ifdef _MSC_VER
139 # if defined(_M_X64) || defined(_M_AMD64)
140 # define JS_64BIT
141 # endif
142 #elif defined(__GNUC__)
143 /* Additional GCC defines are when running on Solaris, AIX, and HPUX */
144 # if defined(__x86_64__) || defined(__sparcv9) || \
145 defined(__64BIT__) || defined(__LP64__)
146 # define JS_64BIT
147 # endif
148 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC) /* Sun Studio C/C++ */
149 # if defined(__x86_64) || defined(__sparcv9)
150 # define JS_64BIT
151 # endif
152 #elif defined(__xlc__) || defined(__xlC__) /* IBM XL C/C++ */
153 # if defined(__64BIT__)
154 # define JS_64BIT
155 # endif
156 #elif defined(__HP_cc) || defined(__HP_aCC) /* HP-UX cc/aCC */
157 # if defined(__LP64__)
158 # define JS_64BIT
159 # endif
160 #else
161 # error "Implement me"
162 #endif
163
164 /***********************************************************************
165 ** MACROS: JS_ARRAY_LENGTH
166 ** JS_ARRAY_END
167 ** DESCRIPTION:
168 ** Macros to get the number of elements and the pointer to one past the
169 ** last element of a C array. Use them like this:
170 **
171 ** jschar buf[10], *s;
172 ** JSString *str;
173 ** ...
174 ** for (s = buf; s != JS_ARRAY_END(buf); ++s) *s = ...;
175 ** ...
176 ** str = JS_NewStringCopyN(cx, buf, JS_ARRAY_LENGTH(buf));
177 ** ...
178 **
179 ***********************************************************************/
180
181 #define JS_ARRAY_LENGTH(array) (sizeof (array) / sizeof (array)[0])
182 #define JS_ARRAY_END(array) ((array) + JS_ARRAY_LENGTH(array))
183
184 #define JS_BITS_PER_BYTE 8
185 #define JS_BITS_PER_BYTE_LOG2 3
186
187 #if defined(JS_64BIT)
188 # define JS_BITS_PER_WORD 64
189 #else
190 # define JS_BITS_PER_WORD 32
191 #endif
192
193 /***********************************************************************
194 ** MACROS: JS_FUNC_TO_DATA_PTR
195 ** JS_DATA_TO_FUNC_PTR
196 ** DESCRIPTION:
197 ** Macros to convert between function and data pointers assuming that
198 ** they have the same size. Use them like this:
199 **
200 ** JSPropertyOp nativeGetter;
201 ** JSObject *scriptedGetter;
202 ** ...
203 ** scriptedGetter = JS_FUNC_TO_DATA_PTR(JSObject *, nativeGetter);
204 ** ...
205 ** nativeGetter = JS_DATA_TO_FUNC_PTR(JSPropertyOp, scriptedGetter);
206 **
207 ***********************************************************************/
208
209 #ifdef __GNUC__
210 # define JS_FUNC_TO_DATA_PTR(type, fun) (__extension__ (type) (size_t) (fun))
211 # define JS_DATA_TO_FUNC_PTR(type, ptr) (__extension__ (type) (size_t) (ptr))
212 #else
213 /* Use an extra (void *) cast for MSVC. */
214 # define JS_FUNC_TO_DATA_PTR(type, fun) ((type) (void *) (fun))
215 # define JS_DATA_TO_FUNC_PTR(type, ptr) ((type) (void *) (ptr))
216 #endif
217
218 #ifdef __GNUC__
219 # define JS_EXTENSION __extension__
220 # define JS_EXTENSION_(s) __extension__ ({ s; })
221 #else
222 # define JS_EXTENSION
223 # define JS_EXTENSION_(s) s
224 #endif
225
226 #endif /* jstypes_h */

mercurial