-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathJSArrayProxy.hh
415 lines (361 loc) · 13.6 KB
/
JSArrayProxy.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/**
* @file JSArrayProxy.hh
* @author Philippe Laporte (philippe@distributive.network)
* @brief JSArrayProxy is a custom C-implemented python type that derives from list. It acts as a proxy for JSArrays from Spidermonkey, and behaves like a list would.
* @version 0.1
* @date 2023-11-22
*
* @copyright Copyright (c) 2023 Distributive Corp.
*
*/
#ifndef PythonMonkey_JSArrayProxy_
#define PythonMonkey_JSArrayProxy_
#include <jsapi.h>
#include <Python.h>
/**
* @brief The typedef for the backing store that will be used by JSArrayProxy objects. All it contains is a pointer to the JSObject
*
*/
typedef struct {
PyListObject list;
JS::PersistentRootedObject *jsArray;
} JSArrayProxy;
/**
* @brief This struct is a bundle of methods used by the JSArrayProxy type
*
*/
struct JSArrayProxyMethodDefinitions {
public:
/**
* @brief Deallocation method (.tp_dealloc), removes the reference to the underlying JSObject before freeing the JSArrayProxy
*
* @param self - The JSArrayProxy to be free'd
*/
static void JSArrayProxy_dealloc(JSArrayProxy *self);
/**
* @brief Length method (.mp_length and .sq_length), returns the number of keys in the JSObject, used by the python len() method
*
* @param self - The JSArrayProxy
* @return Py_ssize_t The length of the JSArrayProxy
*/
static Py_ssize_t JSArrayProxy_length(JSArrayProxy *self);
/**
* @brief returns a value from the JSArrayProxy given a key, or dispatches to the given key method if such method is found
*
* @param self - The JSArrayProxy
* @param key - The key for the value in the JSArrayProxy
* @return PyObject* NULL on exception, the corresponding value otherwise
*/
static PyObject *JSArrayProxy_get(JSArrayProxy *self, PyObject *key);
/**
* @brief Getter method (.mp_subscript), returns a value from the JSArrayProxy given a key which can be a slice, used by several built-in python methods as well as the [] and operator
*
* @param self - The JSArrayProxy
* @param key - The key for the value in the JSArrayProxy
* @return PyObject* NULL on exception, the corresponding value otherwise
*/
static PyObject *JSArrayProxy_get_subscript(JSArrayProxy *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 JSArrayProxy
* @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 JSArrayProxy_assign_key(JSArrayProxy *self, PyObject *key, PyObject *value);
/**
* @brief Comparison method (.tp_richcompare), returns appropriate boolean given a comparison operator and other pyObject
*
* @param self - The JSArrayProxy
* @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 *JSArrayProxy_richcompare(JSArrayProxy *self, PyObject *other, int op);
/**
* @brief Return an iterator object to make JSArrayProxy iterable
*
* @param self - The JSArrayProxy
* @return PyObject* - iterator object
*/
static PyObject *JSArrayProxy_iter(JSArrayProxy *self);
/**
* @brief Return a reverse iterator object to make JSArrayProxy backwards iterable
*
* @param self - The JSArrayProxy
* @return PyObject* - iterator object
*/
static PyObject *JSArrayProxy_iter_reverse(JSArrayProxy *self);
/**
* @brief Compute a string representation of the JSArrayProxy
*
* @param self - The JSArrayProxy
* @return the string representation (a PyUnicodeObject) on success, NULL on failure
*/
static PyObject *JSArrayProxy_repr(JSArrayProxy *self);
/**
* @brief concat method (.sq_concat), concatenates
*
* @param self - The JSArrayProxy
* @param value - The value to be concatenated
* @return PyObject* NULL on exception, the corresponding new value otherwise
*/
static PyObject *JSArrayProxy_concat(JSArrayProxy *self, PyObject *value);
/**
* @brief repeat method (.sq_repeat), repeat self n number of time
*
* @param self - The JSArrayProxy
* @param n The number of times to repeat
* @return PyObject* NULL on exception, the corresponding new value otherwise
*/
static PyObject *JSArrayProxy_repeat(JSArrayProxy *self, Py_ssize_t n);
/**
* @brief Test contains method (.sq_contains)
*
* @param self - The JSObjectProxy
* @param element - The element in the JSArrayProxy
* @return int 1 if element is in List, 0 if not, and -1 on error
*/
static int JSArrayProxy_contains(JSArrayProxy *self, PyObject *element);
/**
* @brief inplace_concat method (.sq_inplace_concat), concatenates in_place
*
* @param self - The JSArrayProxy
* @param value - The value to be concatenated
* @return PyObject* self
*/
static PyObject *JSArrayProxy_inplace_concat(JSArrayProxy *self, PyObject *value);
/**
* @brief inplace_repeat method (.sq_inplace_repeat), repeats in_place
*
* @param self - The JSArrayProxy
* @param n The number of times to repeat
* @return PyObject* self
*/
static PyObject *JSArrayProxy_inplace_repeat(JSArrayProxy *self, Py_ssize_t n);
/**
* @brief clear method, empties the array
*
* @param self - The JSArrayProxy
* @return None
*/
static PyObject *JSArrayProxy_clear_method(JSArrayProxy *self);
/**
* @brief copy method
*
* @param self - The JSArrayProxy
* @return a shallow copy of the list
*/
static PyObject *JSArrayProxy_copy(JSArrayProxy *self);
/**
* @brief append method
*
* @param self - The JSArrayProxy
* @param value - The value to be appended
* @return PyObject* NULL on exception, None otherwise
*/
static PyObject *JSArrayProxy_append(JSArrayProxy *self, PyObject *value);
/**
* @brief insert method
*
* @param self - The JSArrayProxy
* @param args - arguments to the insert method
* @param nargs - number of arguments to the insert method
* @return PyObject* NULL on exception, None otherwise
*/
static PyObject *JSArrayProxy_insert(JSArrayProxy *self, PyObject *const *args, Py_ssize_t nargs);
/**
* @brief extend method
*
* @param self - The JSArrayProxy
* @param iterable - The value to be appended
* @return PyObject* NULL on exception, None otherwise
*/
static PyObject *JSArrayProxy_extend(JSArrayProxy *self, PyObject *iterable);
/**
* @brief pop method
*
* @param self - The JSArrayProxy
* @param args - arguments to the pop method
* @param nargs - number of arguments to the pop method
* @return PyObject* NULL on exception, the corresponding value otherwise
*/
static PyObject *JSArrayProxy_pop(JSArrayProxy *self, PyObject *const *args, Py_ssize_t nargs);
/**
* @brief remove method Remove first occurrence of value
*
* @param self - The JSArrayProxy
* @param value - The value to be appended
* @return PyObject* NULL on exception, None otherwise
*/
static PyObject *JSArrayProxy_remove(JSArrayProxy *self, PyObject *value);
/**
* @brief index method
*
* @param self - The JSArrayProxy
* @param args - arguments to the index method
* @param nargs - number of arguments to the index method
* @return PyObject* NULL on exception, the corresponding index of the found value as PyLong otherwise
*/
static PyObject *JSArrayProxy_index(JSArrayProxy *self, PyObject *const *args, Py_ssize_t nargs);
/**
* @brief count method
*
* @param self - The JSArrayProxy
* @param value - The value to be appended
* @return PyObject* NULL on exception, the corresponding count of the found value as PyLong otherwise
*/
static PyObject *JSArrayProxy_count(JSArrayProxy *self, PyObject *value);
/**
* @brief reverse method Reverse list in place
*
* @param self - The JSArrayProxy
* @return PyObject* NULL on exception, None otherwise
*/
static PyObject *JSArrayProxy_reverse(JSArrayProxy *self);
/**
* @brief sort method sort in place
*
* @param self - The JSArrayProxy
* @param args - arguments to the sort method (not used)
* @param kwargs - keyword arguments to the sort method (reverse=True|False, key=keyfunction)
* @return PyObject* NULL on exception, None otherwise
*/
static PyObject *JSArrayProxy_sort(JSArrayProxy *self, PyObject *args, PyObject *kwargs);
/**
* @brief tp_traverse
*
* @param self - The JSArrayProxy
* @param visit - The function to be applied on each element of the list
* @param arg - The argument to the visit function
* @return 0 on success
*/
static int JSArrayProxy_traverse(JSArrayProxy *self, visitproc visit, void *arg);
/**
* @brief tp_clear
*
* @param self - The JSArrayProxy
* @return 0 on success
*/
static int JSArrayProxy_clear(JSArrayProxy *self);
};
/**
* @brief Struct for the methods that define the Mapping protocol
*
*/
static PyMappingMethods JSArrayProxy_mapping_methods = {
.mp_length = (lenfunc)JSArrayProxyMethodDefinitions::JSArrayProxy_length,
.mp_subscript = (binaryfunc)JSArrayProxyMethodDefinitions::JSArrayProxy_get_subscript,
.mp_ass_subscript = (objobjargproc)JSArrayProxyMethodDefinitions::JSArrayProxy_assign_key
};
/**
* @brief Struct for the methods that define the Sequence protocol
*
*/
static PySequenceMethods JSArrayProxy_sequence_methods = {
.sq_length = (lenfunc)JSArrayProxyMethodDefinitions::JSArrayProxy_length,
.sq_concat = (binaryfunc)JSArrayProxyMethodDefinitions::JSArrayProxy_concat,
.sq_repeat = (ssizeargfunc)JSArrayProxyMethodDefinitions::JSArrayProxy_repeat,
.sq_contains = (objobjproc)JSArrayProxyMethodDefinitions::JSArrayProxy_contains,
.sq_inplace_concat = (binaryfunc)JSArrayProxyMethodDefinitions::JSArrayProxy_inplace_concat,
.sq_inplace_repeat = (ssizeargfunc)JSArrayProxyMethodDefinitions::JSArrayProxy_inplace_repeat
};
PyDoc_STRVAR(py_list_clear__doc__,
"clear($self, /)\n"
"--\n"
"\n"
"Remove all items from list.");
PyDoc_STRVAR(list_copy__doc__,
"copy($self, /)\n"
"--\n"
"\n"
"Return a shallow copy of the list.");
PyDoc_STRVAR(list_append__doc__,
"append($self, object, /)\n"
"--\n"
"\n"
"Append object to the end of the list.");
PyDoc_STRVAR(list_insert__doc__,
"insert($self, index, object, /)\n"
"--\n"
"\n"
"Insert object before index.");
PyDoc_STRVAR(py_list_extend__doc__,
"extend($self, iterable, /)\n"
"--\n"
"\n"
"Extend list by appending elements from the iterable.");
PyDoc_STRVAR(list_pop__doc__,
"pop($self, index=-1, /)\n"
"--\n"
"\n"
"Remove and return item at index (default last).\n"
"\n"
"Raises IndexError if list is empty or index is out of range.");
PyDoc_STRVAR(list_remove__doc__,
"remove($self, value, /)\n"
"--\n"
"\n"
"Remove first occurrence of value.\n"
"\n"
"Raises ValueError if the value is not present.");
PyDoc_STRVAR(list_index__doc__,
"index($self, value, start=0, stop=sys.maxsize, /)\n"
"--\n"
"\n"
"Return first index of value.\n"
"\n"
"Raises ValueError if the value is not present.");
PyDoc_STRVAR(list_count__doc__,
"count($self, value, /)\n"
"--\n"
"\n"
"Return number of occurrences of value.");
PyDoc_STRVAR(list_reverse__doc__,
"reverse($self, /)\n"
"--\n"
"\n"
"Reverse *IN PLACE*.");
PyDoc_STRVAR(list_sort__doc__,
"sort($self, /, *, key=None, reverse=False)\n"
"--\n"
"\n"
"Sort the list in ascending order and return None.\n"
"\n"
"The sort is in-place (i.e. the list itself is modified) and stable (i.e. the\n"
"order of two equal elements is maintained).\n"
"\n"
"If a key function is given, apply it once to each list item and sort them,\n"
"ascending or descending, according to their function values.\n"
"\n"
"The reverse flag can be set to sort in descending order.");
PyDoc_STRVAR(list___reversed____doc__,
"__reversed__($self, /)\n"
"--\n"
"\n"
"Return a reverse iterator over the list.");
/**
* @brief Struct for the other methods
*
*/
static PyMethodDef JSArrayProxy_methods[] = {
{"__reversed__", (PyCFunction)JSArrayProxyMethodDefinitions::JSArrayProxy_iter_reverse, METH_NOARGS, list___reversed____doc__},
{"clear", (PyCFunction)JSArrayProxyMethodDefinitions::JSArrayProxy_clear_method, METH_NOARGS, py_list_clear__doc__},
{"copy", (PyCFunction)JSArrayProxyMethodDefinitions::JSArrayProxy_copy, METH_NOARGS, list_copy__doc__},
{"append", (PyCFunction)JSArrayProxyMethodDefinitions::JSArrayProxy_append, METH_O, list_append__doc__},
{"insert", (PyCFunction)JSArrayProxyMethodDefinitions::JSArrayProxy_insert, METH_FASTCALL, list_insert__doc__},
{"extend", (PyCFunction)JSArrayProxyMethodDefinitions::JSArrayProxy_extend, METH_O, py_list_extend__doc__},
{"pop", (PyCFunction)JSArrayProxyMethodDefinitions::JSArrayProxy_pop, METH_FASTCALL, list_pop__doc__},
{"remove", (PyCFunction)JSArrayProxyMethodDefinitions::JSArrayProxy_remove, METH_O, list_remove__doc__},
{"index", (PyCFunction)JSArrayProxyMethodDefinitions::JSArrayProxy_index, METH_FASTCALL, list_index__doc__},
{"count", (PyCFunction)JSArrayProxyMethodDefinitions::JSArrayProxy_count, METH_O, list_count__doc__},
{"reverse", (PyCFunction)JSArrayProxyMethodDefinitions::JSArrayProxy_reverse, METH_NOARGS, list_reverse__doc__},
{"sort", (PyCFunction)JSArrayProxyMethodDefinitions::JSArrayProxy_sort, METH_VARARGS|METH_KEYWORDS, list_sort__doc__},
{NULL, NULL} /* sentinel */
};
/**
* @brief Struct for the JSArrayProxyType, used by all JSArrayProxy objects
*/
extern PyTypeObject JSArrayProxyType;
#endif