-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathJSStringProxy.hh
79 lines (66 loc) · 2.24 KB
/
JSStringProxy.hh
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
/**
* @file JSStringProxy.hh
* @author Caleb Aikens (caleb@distributive.network)
* @brief JSStringProxy is a custom C-implemented python type that derives from str. It acts as a proxy for JSStrings from Spidermonkey, and behaves like a str would.
* @date 2024-01-03
*
* @copyright Copyright (c) 2024 Distributive Corp.
*
*/
#ifndef PythonMonkey_JSStringProxy_
#define PythonMonkey_JSStringProxy_
#include <jsapi.h>
#include <Python.h>
#include <unordered_set>
/**
* @brief The typedef for the backing store that will be used by JSStringProxy objects. All it contains is a pointer to the JSString
*
*/
typedef struct {
PyUnicodeObject str;
JS::PersistentRootedValue *jsString;
} JSStringProxy;
extern std::unordered_set<JSStringProxy *> jsStringProxies; // a collection of all JSStringProxy objects, used during a GCCallback to ensure they continue to point to the correct char buffer
/**
* @brief This struct is a bundle of methods used by the JSStringProxy type
*
*/
struct JSStringProxyMethodDefinitions {
public:
/**
* @brief Deallocation method (.tp_dealloc), removes the reference to the underlying JSString before freeing the JSStringProxy
*
* @param self - The JSStringProxy to be free'd
*/
static void JSStringProxy_dealloc(JSStringProxy *self);
/**
* @brief copy protocol method for both copy and deepcopy
*
* @param self - The JSObjectProxy
* @return a copy of the string
*/
static PyObject *JSStringProxy_copy_method(JSStringProxy *self);
};
// docs for methods, copied from cpython
PyDoc_STRVAR(stringproxy_deepcopy__doc__,
"__deepcopy__($self, memo, /)\n"
"--\n"
"\n");
PyDoc_STRVAR(stringproxy_copy__doc__,
"__copy__($self, /)\n"
"--\n"
"\n");
/**
* @brief Struct for the other methods
*
*/
static PyMethodDef JSStringProxy_methods[] = {
{"__deepcopy__", (PyCFunction)JSStringProxyMethodDefinitions::JSStringProxy_copy_method, METH_O, stringproxy_deepcopy__doc__}, // ignores any memo argument
{"__copy__", (PyCFunction)JSStringProxyMethodDefinitions::JSStringProxy_copy_method, METH_NOARGS, stringproxy_copy__doc__},
{NULL, NULL} /* sentinel */
};
/**
* @brief Struct for the JSStringProxyType, used by all JSStringProxy objects
*/
extern PyTypeObject JSStringProxyType;
#endif