-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathJSObjectProxy.hh
More file actions
350 lines (304 loc) · 12.4 KB
/
JSObjectProxy.hh
File metadata and controls
350 lines (304 loc) · 12.4 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/**
* @file JSObjectProxy.hh
* @author Caleb Aikens (caleb@distributive.network), Tom Tang (xmader@distributive.network) and Philippe Laporte (philippe@distributive.network)
* @brief JSObjectProxy is a custom C-implemented python type that derives from dict. It acts as a proxy for JSObjects from Spidermonkey, and behaves like a dict would.
* @date 2023-06-26
*
* @copyright Copyright (c) 2023 Distributive Corp.
*
*/
#ifndef PythonMonkey_JSObjectProxy_
#define PythonMonkey_JSObjectProxy_
#include <jsapi.h>
#include <Python.h>
#include <unordered_map>
/**
* @brief The typedef for the backing store that will be used by JSObjectProxy objects. All it contains is a pointer to the JSObject
*
*/
typedef struct {
PyDictObject dict;
JS::PersistentRootedObject *jsObject;
} JSObjectProxy;
/**
* @brief This struct is a bundle of methods used by the JSObjectProxy type
*
*/
struct JSObjectProxyMethodDefinitions {
public:
/**
* @brief Deallocation method (.tp_dealloc), removes the reference to the underlying JSObject before freeing the JSObjectProxy
*
* @param self - The JSObjectProxy to be free'd
*/
static void JSObjectProxy_dealloc(JSObjectProxy *self);
/**
* @brief Length method (.mp_length), returns the number of key-value pairs in the JSObject, used by the python len() method
*
* @param self - The JSObjectProxy
* @return Py_ssize_t The length of the JSObjectProxy
*/
static Py_ssize_t JSObjectProxy_length(JSObjectProxy *self);
/**
* @brief Getter method, returns a value from the JSObjectProxy given a key, used by several built-in python methods as well as the . operator
*
* @param self - The JSObjectProxy
* @param key - The key for the value in the JSObjectProxy
* @return PyObject* NULL on exception, the corresponding value otherwise
*/
static PyObject *JSObjectProxy_get(JSObjectProxy *self, PyObject *key);
/**
* @brief Getter method (.mp_subscript), returns a value from the JSObjectProxy given a key, used by the [] operator
*
* @param self - The JSObjectProxy
* @param key - The key for the value in the JSObjectProxy
* @return PyObject* NULL on exception, the corresponding value otherwise
*/
static PyObject *JSObjectProxy_get_subscript(JSObjectProxy *self, PyObject *key);
/**
* @brief Test method (.sq_contains), returns whether a key exists, used by the in operator
*
* @param self - The JSObjectProxy
* @param key - The key for the value in the JSObjectProxy
* @return int 1 if `key` is in dict, 0 if not, and -1 on error
*/
static int JSObjectProxy_contains(JSObjectProxy *self, PyObject *key);
/**
* @brief Assign method (.mp_ass_subscript), assigns a key-value pair if value is non-NULL, or deletes a key-value pair if value is NULL
*
* @param self - The JSObjectProxy
* @param key - The key to be set or deleted
* @param value If NULL, the key-value pair is deleted, if not NULL then a key-value pair is assigned
* @return int -1 on exception, any other value otherwise
*/
static int JSObjectProxy_assign(JSObjectProxy *self, PyObject *key, PyObject *value);
/**
* @brief Comparison method (.tp_richcompare), returns appropriate boolean given a comparison operator and other pyobject
*
* @param self - The JSObjectProxy
* @param other - Any other PyObject
* @param op - Which boolean operator is being performed (Py_EQ for equality, Py_NE for inequality, all other operators are not implemented)
* @return PyObject* - True or false depending on result of comparison
*/
static PyObject *JSObjectProxy_richcompare(JSObjectProxy *self, PyObject *other, int op);
/**
* @brief Helper function for JSObjectProxy_richcompare
*
* @param self - The PyObject on the left side of the operator (guaranteed to be a JSObjectProxy *)
* @param other - The PyObject on the right side of the operator
* @param visited
* @return bool - Whether the compared objects are equal or not
*/
// private
static bool JSObjectProxy_richcompare_helper(JSObjectProxy *self, PyObject *other, std::unordered_map<PyObject *, PyObject *> &visited);
/**
* @brief Return an iterator object to make JSObjectProxy iterable, emitting (key, value) tuples
*
* @param self - The JSObjectProxy
* @return PyObject* - iterator object
*/
static PyObject *JSObjectProxy_iter(JSObjectProxy *self);
/**
* @brief Implements next operator function
*
* @param self - The JSObjectProxy
* @return PyObject* - call result
*/
static PyObject *JSObjectProxy_iter_next(JSObjectProxy *self);
/**
* @brief Compute a string representation of the JSObjectProxy
*
* @param self - The JSObjectProxy
* @return the string representation (a PyUnicodeObject) on success, NULL on failure
*/
static PyObject *JSObjectProxy_repr(JSObjectProxy *self);
/**
* @brief Set union operation
*
* @param self - The JSObjectProxy
* @param other - The other PyObject to be or'd, expected to be dict or JSObjectProxy
* @return PyObject* The resulting new dict
*/
static PyObject *JSObjectProxy_or(JSObjectProxy *self, PyObject *other);
/**
* @brief Set union operation, in place
*
* @param self - The JSObjectProxy
* @param other - The other PyObject to be or'd, expected to be dict or JSObjectProxy
* @return PyObject* The resulting new dict, must be same object as self
*/
static PyObject *JSObjectProxy_ior(JSObjectProxy *self, PyObject *other);
/**
* @brief get method
*
* @param self - The JSObjectProxy
* @param args - arguments to the method
* @param nargs - number of args to the method
* @return PyObject* the value for key if first arg key is in the dictionary, else second arg default
*/
static PyObject *JSObjectProxy_get_method(JSObjectProxy *self, PyObject *const *args, Py_ssize_t nargs);
/**
* @brief setdefault method
*
* @param self - The JSObjectProxy
* @param args - arguments to the method
* @param nargs - number of args to the method
* @return PyObject* the value for key if first arg key is in the dictionary, else second default
*/
static PyObject *JSObjectProxy_setdefault_method(JSObjectProxy *self, PyObject *const *args, Py_ssize_t nargs);
/**
* @brief pop method
*
* @param self - The JSObjectProxy
* @param args - arguments to the method
* @param nargs - number of args to the method
* @return PyObject* If the first arg key is not found, return the second arg default if given; otherwise raise a KeyError
*/
static PyObject *JSObjectProxy_pop_method(JSObjectProxy *self, PyObject *const *args, Py_ssize_t nargs);
/**
* @brief clear method
*
* @param self - The JSObjectProxy
* @return None
*/
static PyObject *JSObjectProxy_clear_method(JSObjectProxy *self);
/**
* @brief copy method
*
* @param self - The JSObjectProxy
* @return PyObject* copy of the dict
*/
static PyObject *JSObjectProxy_copy_method(JSObjectProxy *self);
/**
* @brief update method update the dict with another dict or iterable
*
* @param self - The JSObjectProxy
* @param args - arguments to the sort method
* @param kwds - keyword arguments to the sort method (key-value pairs to be updated in the dict)
* @return None
*/
static PyObject *JSObjectProxy_update_method(JSObjectProxy *self, PyObject *args, PyObject *kwds);
/**
* @brief keys method
*
* @param self - The JSObjectProxy
* @return PyObject* keys of the dict
*/
static PyObject *JSObjectProxy_keys_method(JSObjectProxy *self);
/**
* @brief values method
*
* @param self - The JSObjectProxy
* @return PyObject* values view of the dict
*/
static PyObject *JSObjectProxy_values_method(JSObjectProxy *self);
/**
* @brief items method
*
* @param self - The JSObjectProxy
* @return PyObject* items view of the dict
*/
static PyObject *JSObjectProxy_items_method(JSObjectProxy *self);
/**
* @brief tp_traverse
*
* @param self - The JSObjectProxy
* @param visit - The function to be applied on each element of the object
* @param arg - The argument to the visit function
* @return 0 on success
*/
static int JSObjectProxy_traverse(JSObjectProxy *self, visitproc visit, void *arg);
/**
* @brief tp_clear
*
* @param self - The JSObjectProxy
* @return 0 on success
*/
static int JSObjectProxy_clear(JSObjectProxy *self);
};
// docs for methods, copied from cpython
PyDoc_STRVAR(getitem__doc__,
"__getitem__($self, key, /)\n--\n\nReturn self[key].");
PyDoc_STRVAR(dict_get__doc__,
"get($self, key, default=None, /)\n"
"--\n"
"\n"
"Return the value for key if key is in the dictionary, else default.");
PyDoc_STRVAR(dict_setdefault__doc__,
"setdefault($self, key, default=None, /)\n"
"--\n"
"\n"
"Insert key with a value of default if key is not in the dictionary.\n"
"\n"
"Return the value for key if key is in the dictionary, else default.");
PyDoc_STRVAR(dict_pop__doc__,
"pop($self, key, default=<unrepresentable>, /)\n"
"--\n"
"\n"
"D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n"
"\n"
"If the key is not found, return the default if given; otherwise,\n"
"raise a KeyError.");
PyDoc_STRVAR(clear__doc__,
"D.clear() -> None. Remove all items from D.");
PyDoc_STRVAR(copy__doc__,
"D.copy() -> a shallow copy of D");
PyDoc_STRVAR(keys__doc__,
"D.keys() -> a set-like object providing a view on D's keys");
PyDoc_STRVAR(items__doc__,
"D.items() -> a set-like object providing a view on D's items");
PyDoc_STRVAR(values__doc__,
"D.values() -> an object providing a view on D's values");
PyDoc_STRVAR(update__doc__,
"D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
In either case, this is followed by: for k in F: D[k] = F[k]");
PyDoc_STRVAR(dict_keys__doc__,
"D.keys() -> a set-like object providing a view on D's keys");
PyDoc_STRVAR(dict_items__doc__,
"D.items() -> a set-like object providing a view on D's items");
PyDoc_STRVAR(dict_values__doc__,
"D.values() -> an object providing a view on D's values");
/**
* @brief Struct for the methods that define the Mapping protocol
*
*/
static PyMappingMethods JSObjectProxy_mapping_methods = {
.mp_length = (lenfunc)JSObjectProxyMethodDefinitions::JSObjectProxy_length,
.mp_subscript = (binaryfunc)JSObjectProxyMethodDefinitions::JSObjectProxy_get_subscript,
.mp_ass_subscript = (objobjargproc)JSObjectProxyMethodDefinitions::JSObjectProxy_assign
};
/**
* @brief Struct for the methods that define the Sequence protocol
*
*/
static PySequenceMethods JSObjectProxy_sequence_methods = {
.sq_contains = (objobjproc)JSObjectProxyMethodDefinitions::JSObjectProxy_contains
};
static PyNumberMethods JSObjectProxy_number_methods = {
.nb_or = (binaryfunc)JSObjectProxyMethodDefinitions::JSObjectProxy_or,
.nb_inplace_or = (binaryfunc)JSObjectProxyMethodDefinitions::JSObjectProxy_ior
};
/**
* @brief Struct for the other methods
*
*/
static PyMethodDef JSObjectProxy_methods[] = {
{"setdefault", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_setdefault_method, METH_FASTCALL, dict_setdefault__doc__},
{"__getitem__", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_get, METH_O | METH_COEXIST, getitem__doc__},
{"get", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_get_method, METH_FASTCALL, dict_get__doc__},
{"pop", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_pop_method, METH_FASTCALL, dict_pop__doc__},
{"clear", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_clear_method, METH_NOARGS, clear__doc__},
{"copy", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_copy_method, METH_NOARGS, copy__doc__},
{"update", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_update_method, METH_VARARGS | METH_KEYWORDS, update__doc__},
{"keys", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_keys_method, METH_NOARGS, dict_keys__doc__},
{"items", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_items_method, METH_NOARGS, dict_items__doc__},
{"values", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_values_method, METH_NOARGS, dict_values__doc__},
{NULL, NULL} /* sentinel */
};
/**
* @brief Struct for the JSObjectProxyType, used by all JSObjectProxy objects
*/
extern PyTypeObject JSObjectProxyType;
#endif