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/Casting.h"
25 #include "mozilla/Types.h"
30 // jstypes.h is (or should be!) included by every file in SpiderMonkey.
31 // js-config.h also should be included by every file. So include it here.
32 #include "js-config.h"
35 * The linkage of JS API functions differs depending on whether the file is
36 * used within the JS library or not. Any source file within the JS
37 * interpreter should define EXPORT_JS_API whereas any client of the library
38 * should not. STATIC_JS_API is used to build JS as a static library.
40 #if defined(STATIC_JS_API)
41 # define JS_PUBLIC_API
42 # define JS_PUBLIC_DATA
43 #elif defined(EXPORT_JS_API) || defined(STATIC_EXPORTABLE_JS_API)
44 # define JS_PUBLIC_API MOZ_EXPORT
45 # define JS_PUBLIC_DATA MOZ_EXPORT
47 # define JS_PUBLIC_API MOZ_IMPORT_API
48 # define JS_PUBLIC_DATA MOZ_IMPORT_DATA
51 /***********************************************************************
52 ** MACROS: JS_BEGIN_MACRO
55 ** Macro body brackets so that macros with compound statement definitions
56 ** behave syntactically more like functions when called.
57 ***********************************************************************/
58 #define JS_BEGIN_MACRO do {
59 #define JS_END_MACRO \
63 /***********************************************************************
67 ** Bit masking functions. XXX n must be <= 31 to be portable
68 ***********************************************************************/
70 constexpr uint32_t Bit(uint32_t n
) { return uint32_t(1) << n
; }
72 constexpr uint32_t BitMask(uint32_t n
) { return Bit(n
) - 1; }
75 /***********************************************************************
81 ** Commonly used functions for operations on compatible types.
82 ***********************************************************************/
84 constexpr size_t HowMany(size_t x
, size_t y
) { return (x
+ y
- 1) / y
; }
86 constexpr size_t RoundUp(size_t x
, size_t y
) { return HowMany(x
, y
) * y
; }
88 constexpr size_t RoundDown(size_t x
, size_t y
) { return (x
/ y
) * y
; }
90 constexpr size_t Round(size_t x
, size_t y
) { return ((x
+ y
/ 2) / y
) * y
; }
93 #if (defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 8) || \
94 (defined(UINTPTR_MAX) && UINTPTR_MAX == 0xFFFFFFFFFFFFFFFFu)
95 # define JS_BITS_PER_WORD 64
97 # define JS_BITS_PER_WORD 32
100 static_assert(sizeof(void*) == 8 ? JS_BITS_PER_WORD
== 64
101 : JS_BITS_PER_WORD
== 32,
102 "preprocessor and compiler must agree");
104 /***********************************************************************
105 ** MACROS: JS_FUNC_TO_DATA_PTR
106 ** JS_DATA_TO_FUNC_PTR
108 ** Macros to convert between function and data pointers of the same
109 ** size. Use them like this:
111 ** JSGetterOp nativeGetter;
112 ** JSObject* scriptedGetter;
114 ** scriptedGetter = JS_FUNC_TO_DATA_PTR(JSObject*, nativeGetter);
116 ** nativeGetter = JS_DATA_TO_FUNC_PTR(JSGetterOp, scriptedGetter);
118 ***********************************************************************/
120 #define JS_FUNC_TO_DATA_PTR(type, fun) (mozilla::BitwiseCast<type>(fun))
121 #define JS_DATA_TO_FUNC_PTR(type, ptr) (mozilla::BitwiseCast<type>(ptr))
123 #endif /* jstypes_h */