PythonMonkey   v0.7.2 (dev)
Loading...
Searching...
No Matches
JSObjectProxy.hh
Go to the documentation of this file.
1
11#ifndef PythonMonkey_JSObjectProxy_
12#define PythonMonkey_JSObjectProxy_
13
14#include <jsapi.h>
15
16#include <Python.h>
17
18#include <unordered_map>
19
20
25typedef struct {
26 PyDictObject dict;
27 JS::PersistentRootedObject *jsObject;
29
35public:
41 static void JSObjectProxy_dealloc(JSObjectProxy *self);
42
49 static Py_ssize_t JSObjectProxy_length(JSObjectProxy *self);
50
58 static PyObject *JSObjectProxy_get(JSObjectProxy *self, PyObject *key);
59
67 static PyObject *JSObjectProxy_get_subscript(JSObjectProxy *self, PyObject *key);
68
76 static int JSObjectProxy_contains(JSObjectProxy *self, PyObject *key);
77
86 static int JSObjectProxy_assign(JSObjectProxy *self, PyObject *key, PyObject *value);
87
96 static PyObject *JSObjectProxy_richcompare(JSObjectProxy *self, PyObject *other, int op);
97
106 // private
107 static bool JSObjectProxy_richcompare_helper(JSObjectProxy *self, PyObject *other, std::unordered_map<PyObject *, PyObject *> &visited);
108
115 static PyObject *JSObjectProxy_iter(JSObjectProxy *self);
116
123 static PyObject *JSObjectProxy_iter_next(JSObjectProxy *self);
124
131 static PyObject *JSObjectProxy_repr(JSObjectProxy *self);
132
140 static PyObject *JSObjectProxy_or(JSObjectProxy *self, PyObject *other);
141
149 static PyObject *JSObjectProxy_ior(JSObjectProxy *self, PyObject *other);
150
159 static PyObject *JSObjectProxy_get_method(JSObjectProxy *self, PyObject *const *args, Py_ssize_t nargs);
160
169 static PyObject *JSObjectProxy_setdefault_method(JSObjectProxy *self, PyObject *const *args, Py_ssize_t nargs);
170
179 static PyObject *JSObjectProxy_pop_method(JSObjectProxy *self, PyObject *const *args, Py_ssize_t nargs);
180
187 static PyObject *JSObjectProxy_clear_method(JSObjectProxy *self);
188
195 static PyObject *JSObjectProxy_copy_method(JSObjectProxy *self);
196
205 static PyObject *JSObjectProxy_update_method(JSObjectProxy *self, PyObject *args, PyObject *kwds);
206
213 static PyObject *JSObjectProxy_keys_method(JSObjectProxy *self);
214
221 static PyObject *JSObjectProxy_values_method(JSObjectProxy *self);
222
229 static PyObject *JSObjectProxy_items_method(JSObjectProxy *self);
230
239 static int JSObjectProxy_traverse(JSObjectProxy *self, visitproc visit, void *arg);
240
247 static int JSObjectProxy_clear(JSObjectProxy *self);
248};
249
250
251// docs for methods, copied from cpython
252PyDoc_STRVAR(getitem__doc__,
253 "__getitem__($self, key, /)\n--\n\nReturn self[key].");
254
255PyDoc_STRVAR(dict_get__doc__,
256 "get($self, key, default=None, /)\n"
257 "--\n"
258 "\n"
259 "Return the value for key if key is in the dictionary, else default.");
260
261PyDoc_STRVAR(dict_setdefault__doc__,
262 "setdefault($self, key, default=None, /)\n"
263 "--\n"
264 "\n"
265 "Insert key with a value of default if key is not in the dictionary.\n"
266 "\n"
267 "Return the value for key if key is in the dictionary, else default.");
268
269PyDoc_STRVAR(dict_pop__doc__,
270 "pop($self, key, default=<unrepresentable>, /)\n"
271 "--\n"
272 "\n"
273 "D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n"
274 "\n"
275 "If the key is not found, return the default if given; otherwise,\n"
276 "raise a KeyError.");
277
278PyDoc_STRVAR(clear__doc__,
279 "D.clear() -> None. Remove all items from D.");
280
281PyDoc_STRVAR(copy__doc__,
282 "D.copy() -> a shallow copy of D");
283
284PyDoc_STRVAR(keys__doc__,
285 "D.keys() -> a set-like object providing a view on D's keys");
286PyDoc_STRVAR(items__doc__,
287 "D.items() -> a set-like object providing a view on D's items");
288PyDoc_STRVAR(values__doc__,
289 "D.values() -> an object providing a view on D's values");
290
291PyDoc_STRVAR(update__doc__,
292 "D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
293If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
294If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
295In either case, this is followed by: for k in F: D[k] = F[k]");
296
297PyDoc_STRVAR(dict_keys__doc__,
298 "D.keys() -> a set-like object providing a view on D's keys");
299PyDoc_STRVAR(dict_items__doc__,
300 "D.items() -> a set-like object providing a view on D's items");
301PyDoc_STRVAR(dict_values__doc__,
302 "D.values() -> an object providing a view on D's values");
303
308static PyMappingMethods JSObjectProxy_mapping_methods = {
311 .mp_ass_subscript = (objobjargproc)JSObjectProxyMethodDefinitions::JSObjectProxy_assign
312};
313
318static PySequenceMethods JSObjectProxy_sequence_methods = {
320};
321
322static PyNumberMethods JSObjectProxy_number_methods = {
324 .nb_inplace_or = (binaryfunc)JSObjectProxyMethodDefinitions::JSObjectProxy_ior
325};
326
331static PyMethodDef JSObjectProxy_methods[] = {
332 {"setdefault", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_setdefault_method, METH_FASTCALL, dict_setdefault__doc__},
333 {"__getitem__", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_get, METH_O | METH_COEXIST, getitem__doc__},
334 {"get", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_get_method, METH_FASTCALL, dict_get__doc__},
335 {"pop", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_pop_method, METH_FASTCALL, dict_pop__doc__},
336 {"clear", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_clear_method, METH_NOARGS, clear__doc__},
337 {"copy", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_copy_method, METH_NOARGS, copy__doc__},
338 {"update", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_update_method, METH_VARARGS | METH_KEYWORDS, update__doc__},
339 {"keys", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_keys_method, METH_NOARGS, dict_keys__doc__},
340 {"items", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_items_method, METH_NOARGS, dict_items__doc__},
341 {"values", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_values_method, METH_NOARGS, dict_values__doc__},
342 {NULL, NULL} /* sentinel */
343};
344
348extern PyTypeObject JSObjectProxyType;
349
350#endif
PyDoc_STRVAR(getitem__doc__, "__getitem__($self, key, /)\n--\n\nReturn self[key].")
PyTypeObject JSObjectProxyType
Struct for the JSObjectProxyType, used by all JSObjectProxy objects.
Definition pythonmonkey.cc:105
This struct is a bundle of methods used by the JSObjectProxy type.
Definition JSObjectProxy.hh:34
static PyObject * JSObjectProxy_repr(JSObjectProxy *self)
Compute a string representation of the JSObjectProxy.
Definition JSObjectProxy.cc:340
static PyObject * JSObjectProxy_items_method(JSObjectProxy *self)
items method
Definition JSObjectProxy.cc:787
static PyObject * JSObjectProxy_get_subscript(JSObjectProxy *self, PyObject *key)
Getter method (.mp_subscript), returns a value from the JSObjectProxy given a key,...
Definition JSObjectProxy.cc:154
static int JSObjectProxy_contains(JSObjectProxy *self, PyObject *key)
Test method (.sq_contains), returns whether a key exists, used by the in operator.
Definition JSObjectProxy.cc:165
static PyObject * JSObjectProxy_update_method(JSObjectProxy *self, PyObject *args, PyObject *kwds)
update method update the dict with another dict or iterable
Definition JSObjectProxy.cc:752
static Py_ssize_t JSObjectProxy_length(JSObjectProxy *self)
Length method (.mp_length), returns the number of key-value pairs in the JSObject,...
Definition JSObjectProxy.cc:70
static PyObject * JSObjectProxy_iter(JSObjectProxy *self)
Return an iterator object to make JSObjectProxy iterable, emitting (key, value) tuples.
Definition JSObjectProxy.cc:286
static PyObject * JSObjectProxy_iter_next(JSObjectProxy *self)
Implements next operator function.
Definition JSObjectProxy.cc:306
static PyObject * JSObjectProxy_clear_method(JSObjectProxy *self)
clear method
Definition JSObjectProxy.cc:712
static void JSObjectProxy_dealloc(JSObjectProxy *self)
Deallocation method (.tp_dealloc), removes the reference to the underlying JSObject before freeing th...
Definition JSObjectProxy.cc:50
static PyObject * JSObjectProxy_ior(JSObjectProxy *self, PyObject *other)
Set union operation, in place.
Definition JSObjectProxy.cc:585
static int JSObjectProxy_clear(JSObjectProxy *self)
tp_clear
Definition JSObjectProxy.cc:64
static PyObject * JSObjectProxy_or(JSObjectProxy *self, PyObject *other)
Set union operation.
Definition JSObjectProxy.cc:543
static PyObject * JSObjectProxy_richcompare(JSObjectProxy *self, PyObject *other, int op)
Comparison method (.tp_richcompare), returns appropriate boolean given a comparison operator and othe...
Definition JSObjectProxy.cc:200
static PyObject * JSObjectProxy_get(JSObjectProxy *self, PyObject *key)
Getter method, returns a value from the JSObjectProxy given a key, used by several built-in python me...
Definition JSObjectProxy.cc:143
static PyObject * JSObjectProxy_get_method(JSObjectProxy *self, PyObject *const *args, Py_ssize_t nargs)
get method
Definition JSObjectProxy.cc:618
static int JSObjectProxy_traverse(JSObjectProxy *self, visitproc visit, void *arg)
tp_traverse
Definition JSObjectProxy.cc:58
static PyObject * JSObjectProxy_pop_method(JSObjectProxy *self, PyObject *const *args, Py_ssize_t nargs)
pop method
Definition JSObjectProxy.cc:674
static int JSObjectProxy_assign(JSObjectProxy *self, PyObject *key, PyObject *value)
Assign method (.mp_ass_subscript), assigns a key-value pair if value is non-NULL, or deletes a key-va...
Definition JSObjectProxy.cc:187
static PyObject * JSObjectProxy_values_method(JSObjectProxy *self)
values method
Definition JSObjectProxy.cc:783
static bool JSObjectProxy_richcompare_helper(JSObjectProxy *self, PyObject *other, std::unordered_map< PyObject *, PyObject * > &visited)
Helper function for JSObjectProxy_richcompare.
Definition JSObjectProxy.cc:220
static PyObject * JSObjectProxy_keys_method(JSObjectProxy *self)
keys method
Definition JSObjectProxy.cc:779
static PyObject * JSObjectProxy_setdefault_method(JSObjectProxy *self, PyObject *const *args, Py_ssize_t nargs)
setdefault method
Definition JSObjectProxy.cc:642
static PyObject * JSObjectProxy_copy_method(JSObjectProxy *self)
copy method
Definition JSObjectProxy.cc:729
The typedef for the backing store that will be used by JSObjectProxy objects. All it contains is a po...
Definition JSObjectProxy.hh:25
JS::PersistentRootedObject * jsObject
Definition JSObjectProxy.hh:27
PyDictObject dict
Definition JSObjectProxy.hh:26