summaryrefslogtreecommitdiffstats
path: root/js/src/jstypes.h
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /js/src/jstypes.h
parentInitial commit. (diff)
downloadfirefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz
firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'js/src/jstypes.h')
-rw-r--r--js/src/jstypes.h123
1 files changed, 123 insertions, 0 deletions
diff --git a/js/src/jstypes.h b/js/src/jstypes.h
new file mode 100644
index 0000000000..83205fabaa
--- /dev/null
+++ b/js/src/jstypes.h
@@ -0,0 +1,123 @@
+/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
+ * vim: set ts=8 sts=2 et sw=2 tw=80:
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/*
+** File: jstypes.h
+** Description: Definitions of NSPR's basic types
+**
+** Prototypes and macros used to make up for deficiencies in ANSI environments
+** that we have found.
+**
+** Since we do not wrap <stdlib.h> and all the other standard headers, authors
+** of portable code will not know in general that they need these definitions.
+** Instead of requiring these authors to find the dependent uses in their code
+** and take the following steps only in those C files, we take steps once here
+** for all C files.
+**/
+
+#ifndef jstypes_h
+#define jstypes_h
+
+#include "mozilla/Casting.h"
+#include "mozilla/Types.h"
+
+#include <stddef.h>
+#include <stdint.h>
+
+// jstypes.h is (or should be!) included by every file in SpiderMonkey.
+// js-config.h also should be included by every file. So include it here.
+#include "js-config.h"
+
+/*
+ * The linkage of JS API functions differs depending on whether the file is
+ * used within the JS library or not. Any source file within the JS
+ * interpreter should define EXPORT_JS_API whereas any client of the library
+ * should not. STATIC_JS_API is used to build JS as a static library.
+ */
+#if defined(STATIC_JS_API)
+# define JS_PUBLIC_API
+# define JS_PUBLIC_DATA
+#elif defined(EXPORT_JS_API) || defined(STATIC_EXPORTABLE_JS_API)
+# define JS_PUBLIC_API MOZ_EXPORT
+# define JS_PUBLIC_DATA MOZ_EXPORT
+#else
+# define JS_PUBLIC_API MOZ_IMPORT_API
+# define JS_PUBLIC_DATA MOZ_IMPORT_DATA
+#endif
+
+/***********************************************************************
+** MACROS: JS_BEGIN_MACRO
+** JS_END_MACRO
+** DESCRIPTION:
+** Macro body brackets so that macros with compound statement definitions
+** behave syntactically more like functions when called.
+***********************************************************************/
+#define JS_BEGIN_MACRO do {
+#define JS_END_MACRO \
+ } \
+ while (0)
+
+/***********************************************************************
+** FUNCTIONS: Bit
+** BitMask
+** DESCRIPTION:
+** Bit masking functions. XXX n must be <= 31 to be portable
+***********************************************************************/
+namespace js {
+constexpr uint32_t Bit(uint32_t n) { return uint32_t(1) << n; }
+
+constexpr uint32_t BitMask(uint32_t n) { return Bit(n) - 1; }
+} // namespace js
+
+/***********************************************************************
+** FUNCTIONS: HowMany
+** RoundUp
+** RoundDown
+** Round
+** DESCRIPTION:
+** Commonly used functions for operations on compatible types.
+***********************************************************************/
+namespace js {
+constexpr size_t HowMany(size_t x, size_t y) { return (x + y - 1) / y; }
+
+constexpr size_t RoundUp(size_t x, size_t y) { return HowMany(x, y) * y; }
+
+constexpr size_t RoundDown(size_t x, size_t y) { return (x / y) * y; }
+
+constexpr size_t Round(size_t x, size_t y) { return ((x + y / 2) / y) * y; }
+} // namespace js
+
+#if (defined(__SIZEOF_POINTER__) && __SIZEOF_POINTER__ == 8) || \
+ (defined(UINTPTR_MAX) && UINTPTR_MAX == 0xFFFFFFFFFFFFFFFFu)
+# define JS_BITS_PER_WORD 64
+#else
+# define JS_BITS_PER_WORD 32
+#endif
+
+static_assert(sizeof(void*) == 8 ? JS_BITS_PER_WORD == 64
+ : JS_BITS_PER_WORD == 32,
+ "preprocessor and compiler must agree");
+
+/***********************************************************************
+** MACROS: JS_FUNC_TO_DATA_PTR
+** JS_DATA_TO_FUNC_PTR
+** DESCRIPTION:
+** Macros to convert between function and data pointers of the same
+** size. Use them like this:
+**
+** JSGetterOp nativeGetter;
+** JSObject* scriptedGetter;
+** ...
+** scriptedGetter = JS_FUNC_TO_DATA_PTR(JSObject*, nativeGetter);
+** ...
+** nativeGetter = JS_DATA_TO_FUNC_PTR(JSGetterOp, scriptedGetter);
+**
+***********************************************************************/
+
+#define JS_FUNC_TO_DATA_PTR(type, fun) (mozilla::BitwiseCast<type>(fun))
+#define JS_DATA_TO_FUNC_PTR(type, ptr) (mozilla::BitwiseCast<type>(ptr))
+
+#endif /* jstypes_h */