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/. */
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
24 #include "mozilla/Attributes.h"
25 #include "mozilla/Casting.h"
26 #include "mozilla/Types.h"
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"
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.
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
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
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))
67 # define JS_NO_FASTCALL
70 /***********************************************************************
71 ** MACROS: JS_BEGIN_MACRO
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 \
82 /***********************************************************************
86 ** Bit masking functions. XXX n must be <= 31 to be portable
87 ***********************************************************************/
89 constexpr uint32_t Bit(uint32_t n
) { return uint32_t(1) << n
; }
91 constexpr uint32_t BitMask(uint32_t n
) { return Bit(n
) - 1; }
94 /***********************************************************************
100 ** Commonly used functions for operations on compatible types.
101 ***********************************************************************/
103 constexpr size_t HowMany(size_t x
, size_t y
) { return (x
+ y
- 1) / y
; }
105 constexpr size_t RoundUp(size_t x
, size_t y
) { return HowMany(x
, y
) * y
; }
107 constexpr size_t RoundDown(size_t x
, size_t y
) { return (x
/ y
) * y
; }
109 constexpr size_t Round(size_t x
, size_t y
) { return ((x
+ y
/ 2) / y
) * y
; }
112 #if (defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 8) || \
113 (defined(UINTPTR_MAX) && UINTPTR_MAX == 0xFFFFFFFFFFFFFFFFu)
114 # define JS_BITS_PER_WORD 64
116 # define JS_BITS_PER_WORD 32
119 static_assert(sizeof(void*) == 8 ? JS_BITS_PER_WORD
== 64
120 : JS_BITS_PER_WORD
== 32,
121 "preprocessor and compiler must agree");
123 /***********************************************************************
124 ** MACROS: JS_FUNC_TO_DATA_PTR
125 ** JS_DATA_TO_FUNC_PTR
127 ** Macros to convert between function and data pointers of the same
128 ** size. Use them like this:
130 ** JSGetterOp nativeGetter;
131 ** JSObject* scriptedGetter;
133 ** scriptedGetter = JS_FUNC_TO_DATA_PTR(JSObject*, nativeGetter);
135 ** nativeGetter = JS_DATA_TO_FUNC_PTR(JSGetterOp, scriptedGetter);
137 ***********************************************************************/
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))
142 #endif /* jstypes_h */