[SM91] Update to Spidermonkey 91.1.3 APIs
[0ad.git] / libraries / source / spidermonkey / include-win32-debug / js / experimental / CTypes.h
blobbcd3b287756d99ac8c9e3961a3dd900bf56cbdc3
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/. */
7 #ifndef js_experimental_CTypes_h
8 #define js_experimental_CTypes_h
10 #include "mozilla/Attributes.h" // MOZ_RAII
12 #include <stddef.h> // size_t
14 #include "jstypes.h" // JS_PUBLIC_API
16 #include "js/TypeDecls.h"
18 namespace JS {
20 #ifdef JS_HAS_CTYPES
22 /**
23 * Initialize the 'ctypes' object on a global variable 'obj'. The 'ctypes'
24 * object will be sealed.
26 extern JS_PUBLIC_API bool InitCTypesClass(JSContext* cx,
27 Handle<JSObject*> global);
29 #endif // JS_HAS_CTYPES
31 /**
32 * The type of ctypes activity that is occurring.
34 enum class CTypesActivityType {
35 BeginCall,
36 EndCall,
37 BeginCallback,
38 EndCallback,
41 /**
42 * The signature of a function invoked at the leading or trailing edge of ctypes
43 * activity.
45 using CTypesActivityCallback = void (*)(JSContext*, CTypesActivityType);
47 /**
48 * Sets a callback that is run whenever js-ctypes is about to be used when
49 * calling into C.
51 extern JS_PUBLIC_API void SetCTypesActivityCallback(JSContext* cx,
52 CTypesActivityCallback cb);
54 class MOZ_RAII JS_PUBLIC_API AutoCTypesActivityCallback {
55 private:
56 JSContext* cx;
57 CTypesActivityCallback callback;
58 CTypesActivityType endType;
60 public:
61 AutoCTypesActivityCallback(JSContext* cx, CTypesActivityType beginType,
62 CTypesActivityType endType);
64 ~AutoCTypesActivityCallback() { DoEndCallback(); }
66 void DoEndCallback() {
67 if (callback) {
68 callback(cx, endType);
69 callback = nullptr;
74 #ifdef JS_HAS_CTYPES
76 /**
77 * Convert a unicode string 'source' of length 'slen' to the platform native
78 * charset, returning a null-terminated string allocated with JS_malloc. On
79 * failure, this function should report an error.
81 using CTypesUnicodeToNativeFun = char* (*)(JSContext*, const char16_t*, size_t);
83 /**
84 * Set of function pointers that ctypes can use for various internal functions.
85 * See JS::SetCTypesCallbacks below. Providing nullptr for a function is safe
86 * and will result in the applicable ctypes functionality not being available.
88 struct CTypesCallbacks {
89 CTypesUnicodeToNativeFun unicodeToNative;
92 /**
93 * Set the callbacks on the provided 'ctypesObj' object. 'callbacks' should be a
94 * pointer to static data that exists for the lifetime of 'ctypesObj', but it
95 * may safely be altered after calling this function and without having
96 * to call this function again.
98 extern JS_PUBLIC_API void SetCTypesCallbacks(JSObject* ctypesObj,
99 const CTypesCallbacks* callbacks);
101 #endif // JS_HAS_CTYPES
103 } // namespace JS
105 #endif // js_experimental_CTypes_h