summaryrefslogtreecommitdiffstats
path: root/js/public/experimental/CTypes.h
blob: bcd3b287756d99ac8c9e3961a3dd900bf56cbdc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* -*- 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/. */

#ifndef js_experimental_CTypes_h
#define js_experimental_CTypes_h

#include "mozilla/Attributes.h"  // MOZ_RAII

#include <stddef.h>  // size_t

#include "jstypes.h"  // JS_PUBLIC_API

#include "js/TypeDecls.h"

namespace JS {

#ifdef JS_HAS_CTYPES

/**
 * Initialize the 'ctypes' object on a global variable 'obj'. The 'ctypes'
 * object will be sealed.
 */
extern JS_PUBLIC_API bool InitCTypesClass(JSContext* cx,
                                          Handle<JSObject*> global);

#endif  // JS_HAS_CTYPES

/**
 * The type of ctypes activity that is occurring.
 */
enum class CTypesActivityType {
  BeginCall,
  EndCall,
  BeginCallback,
  EndCallback,
};

/**
 * The signature of a function invoked at the leading or trailing edge of ctypes
 * activity.
 */
using CTypesActivityCallback = void (*)(JSContext*, CTypesActivityType);

/**
 * Sets a callback that is run whenever js-ctypes is about to be used when
 * calling into C.
 */
extern JS_PUBLIC_API void SetCTypesActivityCallback(JSContext* cx,
                                                    CTypesActivityCallback cb);

class MOZ_RAII JS_PUBLIC_API AutoCTypesActivityCallback {
 private:
  JSContext* cx;
  CTypesActivityCallback callback;
  CTypesActivityType endType;

 public:
  AutoCTypesActivityCallback(JSContext* cx, CTypesActivityType beginType,
                             CTypesActivityType endType);

  ~AutoCTypesActivityCallback() { DoEndCallback(); }

  void DoEndCallback() {
    if (callback) {
      callback(cx, endType);
      callback = nullptr;
    }
  }
};

#ifdef JS_HAS_CTYPES

/**
 * Convert a unicode string 'source' of length 'slen' to the platform native
 * charset, returning a null-terminated string allocated with JS_malloc. On
 * failure, this function should report an error.
 */
using CTypesUnicodeToNativeFun = char* (*)(JSContext*, const char16_t*, size_t);

/**
 * Set of function pointers that ctypes can use for various internal functions.
 * See JS::SetCTypesCallbacks below. Providing nullptr for a function is safe
 * and will result in the applicable ctypes functionality not being available.
 */
struct CTypesCallbacks {
  CTypesUnicodeToNativeFun unicodeToNative;
};

/**
 * Set the callbacks on the provided 'ctypesObj' object. 'callbacks' should be a
 * pointer to static data that exists for the lifetime of 'ctypesObj', but it
 * may safely be altered after calling this function and without having
 * to call this function again.
 */
extern JS_PUBLIC_API void SetCTypesCallbacks(JSObject* ctypesObj,
                                             const CTypesCallbacks* callbacks);

#endif  // JS_HAS_CTYPES

}  // namespace JS

#endif  // js_experimental_CTypes_h