Bug 1471984 [wpt PR 11709] - Add css-env reviewers, a=testonly
[gecko.git] / js / src / jstypes.h
blobcce6fad335b748ca44394dac9b2f6d48acb897b0
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/. */
7 /*
8 ** File: jstypes.h
9 ** Description: Definitions of NSPR's basic types
11 ** Prototypes and macros used to make up for deficiencies in ANSI environments
12 ** that we have found.
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 **/
21 #ifndef jstypes_h
22 #define jstypes_h
24 #include "mozilla/Attributes.h"
25 #include "mozilla/Casting.h"
26 #include "mozilla/Types.h"
28 // jstypes.h is (or should be!) included by every file in SpiderMonkey.
29 // js-config.h also should be included by every file. So include it here.
30 // XXX: including it 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"
36 * The linkage of JS API functions differs depending on whether the file is
37 * used within the JS library or not. Any source file within the JS
38 * interpreter should define EXPORT_JS_API whereas any client of the library
39 * should not. STATIC_JS_API is used to build JS as a static library.
41 #if defined(STATIC_JS_API)
42 # define JS_PUBLIC_API(t) t
43 # define JS_PUBLIC_DATA(t) t
44 # define JS_FRIEND_API(t) t
45 # define JS_FRIEND_DATA(t) t
46 #elif defined(EXPORT_JS_API) || defined(STATIC_EXPORTABLE_JS_API)
47 # define JS_PUBLIC_API(t) MOZ_EXPORT t
48 # define JS_PUBLIC_DATA(t) MOZ_EXPORT t
49 # define JS_FRIEND_API(t) MOZ_EXPORT t
50 # define JS_FRIEND_DATA(t) MOZ_EXPORT t
51 #else
52 # define JS_PUBLIC_API(t) MOZ_IMPORT_API t
53 # define JS_PUBLIC_DATA(t) MOZ_IMPORT_DATA t
54 # define JS_FRIEND_API(t) MOZ_IMPORT_API t
55 # define JS_FRIEND_DATA(t) MOZ_IMPORT_DATA t
56 #endif
58 #if defined(_MSC_VER) && defined(_M_IX86)
59 #define JS_FASTCALL __fastcall
60 #elif defined(__GNUC__) && defined(__i386__)
61 #define JS_FASTCALL __attribute__((fastcall))
62 #else
63 #define JS_FASTCALL
64 #define JS_NO_FASTCALL
65 #endif
67 // gcc is buggy and warns on our attempts to JS_PUBLIC_API our
68 // forward-declarations or explicit template instantiations. See
69 // <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=50044>. Add a way to detect
70 // that so we can locally disable that warning.
72 // This version-check is written such that the version-number mentioned below
73 // must be bumped every time a new gcc that *doesn't* fix this bug is released.
74 // (The gcc release that *fixes* this bug will trigger no warning, and we'll
75 // naturally stop bumping this number.) If you ever trigger this warning with
76 // the latest stable gcc, you have rs=jwalden to bump it to the next gcc minor
77 // version, e.g. (8, 1, 0) to (8, 2, 0).
78 #if MOZ_IS_GCC
79 # if !MOZ_GCC_VERSION_AT_LEAST(8, 1, 0)
80 # define JS_BROKEN_GCC_ATTRIBUTE_WARNING
81 # endif
82 #endif
84 /***********************************************************************
85 ** MACROS: JS_BEGIN_MACRO
86 ** JS_END_MACRO
87 ** DESCRIPTION:
88 ** Macro body brackets so that macros with compound statement definitions
89 ** behave syntactically more like functions when called.
90 ***********************************************************************/
91 #define JS_BEGIN_MACRO do {
92 #define JS_END_MACRO } while (0)
94 /***********************************************************************
95 ** MACROS: JS_BIT
96 ** JS_BITMASK
97 ** DESCRIPTION:
98 ** Bit masking macros. XXX n must be <= 31 to be portable
99 ***********************************************************************/
100 #define JS_BIT(n) ((uint32_t)1 << (n))
101 #define JS_BITMASK(n) (JS_BIT(n) - 1)
103 /***********************************************************************
104 ** MACROS: JS_HOWMANY
105 ** JS_ROUNDUP
106 ** DESCRIPTION:
107 ** Commonly used macros for operations on compatible types.
108 ***********************************************************************/
109 #define JS_HOWMANY(x,y) (((x)+(y)-1)/(y))
110 #define JS_ROUNDUP(x,y) (JS_HOWMANY(x,y)*(y))
112 #if defined(JS_64BIT)
113 # define JS_BITS_PER_WORD 64
114 #else
115 # define JS_BITS_PER_WORD 32
116 #endif
118 /***********************************************************************
119 ** MACROS: JS_FUNC_TO_DATA_PTR
120 ** JS_DATA_TO_FUNC_PTR
121 ** DESCRIPTION:
122 ** Macros to convert between function and data pointers of the same
123 ** size. Use them like this:
125 ** JSGetterOp nativeGetter;
126 ** JSObject* scriptedGetter;
127 ** ...
128 ** scriptedGetter = JS_FUNC_TO_DATA_PTR(JSObject*, nativeGetter);
129 ** ...
130 ** nativeGetter = JS_DATA_TO_FUNC_PTR(JSGetterOp, scriptedGetter);
132 ***********************************************************************/
134 #define JS_FUNC_TO_DATA_PTR(type, fun) (mozilla::BitwiseCast<type>(fun))
135 #define JS_DATA_TO_FUNC_PTR(type, ptr) (mozilla::BitwiseCast<type>(ptr))
137 #endif /* jstypes_h */