jstypes.h (4437B)
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/Casting.h" 25 #include "mozilla/Types.h" 26 27 #include <stddef.h> 28 #include <stdint.h> 29 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" 33 34 /* 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. 39 */ 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 46 #else 47 # define JS_PUBLIC_API MOZ_IMPORT_API 48 # define JS_PUBLIC_DATA MOZ_IMPORT_DATA 49 #endif 50 51 /*********************************************************************** 52 ** MACROS: JS_BEGIN_MACRO 53 ** JS_END_MACRO 54 ** DESCRIPTION: 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 \ 60 } \ 61 while (0) 62 63 /*********************************************************************** 64 ** FUNCTIONS: Bit 65 ** BitMask 66 ** DESCRIPTION: 67 ** Bit masking functions. XXX n must be <= 31 to be portable 68 ***********************************************************************/ 69 namespace js { 70 constexpr uint32_t Bit(uint32_t n) { return uint32_t(1) << n; } 71 72 constexpr uint32_t BitMask(uint32_t n) { return Bit(n) - 1; } 73 } // namespace js 74 75 /*********************************************************************** 76 ** FUNCTIONS: HowMany 77 ** RoundUp 78 ** RoundDown 79 ** Round 80 ** DESCRIPTION: 81 ** Commonly used functions for operations on compatible types. 82 ***********************************************************************/ 83 namespace js { 84 constexpr size_t HowMany(size_t x, size_t y) { return (x + y - 1) / y; } 85 86 constexpr size_t RoundUp(size_t x, size_t y) { return HowMany(x, y) * y; } 87 88 constexpr size_t RoundDown(size_t x, size_t y) { return (x / y) * y; } 89 90 constexpr size_t Round(size_t x, size_t y) { return ((x + y / 2) / y) * y; } 91 } // namespace js 92 93 #if (defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 8) || \ 94 (defined(UINTPTR_MAX) && UINTPTR_MAX == 0xFFFFFFFFFFFFFFFFu) 95 # define JS_BITS_PER_WORD 64 96 #else 97 # define JS_BITS_PER_WORD 32 98 #endif 99 100 static_assert(sizeof(void*) == 8 ? JS_BITS_PER_WORD == 64 101 : JS_BITS_PER_WORD == 32, 102 "preprocessor and compiler must agree"); 103 104 /*********************************************************************** 105 ** MACROS: JS_FUNC_TO_DATA_PTR 106 ** JS_DATA_TO_FUNC_PTR 107 ** DESCRIPTION: 108 ** Macros to convert between function and data pointers of the same 109 ** size. Use them like this: 110 ** 111 ** JSGetterOp nativeGetter; 112 ** JSObject* scriptedGetter; 113 ** ... 114 ** scriptedGetter = JS_FUNC_TO_DATA_PTR(JSObject*, nativeGetter); 115 ** ... 116 ** nativeGetter = JS_DATA_TO_FUNC_PTR(JSGetterOp, scriptedGetter); 117 ** 118 ***********************************************************************/ 119 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)) 122 123 #endif /* jstypes_h */