1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=8 sts=2 et sw=2 tw=80:
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/Casting.h"
26 #include "mozilla/Types.h"
27 
28 #include <stddef.h>
29 #include <stdint.h>
30 
31 // jstypes.h is (or should be!) included by every file in SpiderMonkey.
32 // js-config.h also should be included by every file. So include it here.
33 // XXX: including it in js/RequiredDefines.h should be a better option, since
34 // that is by definition the header file that should be included in all
35 // SpiderMonkey code.  However, Gecko doesn't do this!  See bug 909576.
36 #include "js-config.h"
37 
38 /*
39  * The linkage of JS API functions differs depending on whether the file is
40  * used within the JS library or not. Any source file within the JS
41  * interpreter should define EXPORT_JS_API whereas any client of the library
42  * should not. STATIC_JS_API is used to build JS as a static library.
43  */
44 #if defined(STATIC_JS_API)
45 #  define JS_PUBLIC_API
46 #  define JS_PUBLIC_DATA
47 #  define JS_FRIEND_API
48 #  define JS_FRIEND_DATA
49 #elif defined(EXPORT_JS_API) || defined(STATIC_EXPORTABLE_JS_API)
50 #  define JS_PUBLIC_API MOZ_EXPORT
51 #  define JS_PUBLIC_DATA MOZ_EXPORT
52 #  define JS_FRIEND_API MOZ_EXPORT
53 #  define JS_FRIEND_DATA MOZ_EXPORT
54 #else
55 #  define JS_PUBLIC_API MOZ_IMPORT_API
56 #  define JS_PUBLIC_DATA MOZ_IMPORT_DATA
57 #  define JS_FRIEND_API MOZ_IMPORT_API
58 #  define JS_FRIEND_DATA MOZ_IMPORT_DATA
59 #endif
60 
61 #if defined(_MSC_VER) && defined(_M_IX86)
62 #  define JS_FASTCALL __fastcall
63 #elif defined(__GNUC__) && defined(__i386__)
64 #  define JS_FASTCALL __attribute__((fastcall))
65 #else
66 #  define JS_FASTCALL
67 #  define JS_NO_FASTCALL
68 #endif
69 
70 /***********************************************************************
71 ** MACROS:      JS_BEGIN_MACRO
72 **              JS_END_MACRO
73 ** DESCRIPTION:
74 **      Macro body brackets so that macros with compound statement definitions
75 **      behave syntactically more like functions when called.
76 ***********************************************************************/
77 #define JS_BEGIN_MACRO do {
78 #define JS_END_MACRO \
79   }                  \
80   while (0)
81 
82 /***********************************************************************
83 ** FUNCTIONS:   Bit
84 **              BitMask
85 ** DESCRIPTION:
86 ** Bit masking functions.  XXX n must be <= 31 to be portable
87 ***********************************************************************/
88 namespace js {
Bit(uint32_t n)89 constexpr uint32_t Bit(uint32_t n) { return uint32_t(1) << n; }
90 
BitMask(uint32_t n)91 constexpr uint32_t BitMask(uint32_t n) { return Bit(n) - 1; }
92 }  // namespace js
93 
94 /***********************************************************************
95 ** FUNCTIONS:   HowMany
96 **              RoundUp
97 **              RoundDown
98 **              Round
99 ** DESCRIPTION:
100 **      Commonly used functions for operations on compatible types.
101 ***********************************************************************/
102 namespace js {
HowMany(size_t x,size_t y)103 constexpr size_t HowMany(size_t x, size_t y) { return (x + y - 1) / y; }
104 
RoundUp(size_t x,size_t y)105 constexpr size_t RoundUp(size_t x, size_t y) { return HowMany(x, y) * y; }
106 
RoundDown(size_t x,size_t y)107 constexpr size_t RoundDown(size_t x, size_t y) { return (x / y) * y; }
108 
Round(size_t x,size_t y)109 constexpr size_t Round(size_t x, size_t y) { return ((x + y / 2) / y) * y; }
110 }  // namespace js
111 
112 #if (defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 8) || \
113     (defined(UINTPTR_MAX) && UINTPTR_MAX == 0xFFFFFFFFFFFFFFFFu)
114 #  define JS_BITS_PER_WORD 64
115 #else
116 #  define JS_BITS_PER_WORD 32
117 #endif
118 
119 static_assert(sizeof(void*) == 8 ? JS_BITS_PER_WORD == 64
120                                  : JS_BITS_PER_WORD == 32,
121               "preprocessor and compiler must agree");
122 
123 /***********************************************************************
124 ** MACROS:      JS_FUNC_TO_DATA_PTR
125 **              JS_DATA_TO_FUNC_PTR
126 ** DESCRIPTION:
127 **      Macros to convert between function and data pointers of the same
128 **      size. Use them like this:
129 **
130 **      JSGetterOp nativeGetter;
131 **      JSObject* scriptedGetter;
132 **      ...
133 **      scriptedGetter = JS_FUNC_TO_DATA_PTR(JSObject*, nativeGetter);
134 **      ...
135 **      nativeGetter = JS_DATA_TO_FUNC_PTR(JSGetterOp, scriptedGetter);
136 **
137 ***********************************************************************/
138 
139 #define JS_FUNC_TO_DATA_PTR(type, fun) (mozilla::BitwiseCast<type>(fun))
140 #define JS_DATA_TO_FUNC_PTR(type, ptr) (mozilla::BitwiseCast<type>(ptr))
141 
142 #endif /* jstypes_h */
143