PythonMonkey   v0.3.1 (dev)
Loading...
Searching...
No Matches
JSObjectProxy.hh
Go to the documentation of this file.
1
12#ifndef PythonMonkey_JSObjectProxy_
13#define PythonMonkey_JSObjectProxy_
14
15#include <jsapi.h>
16
17#include <Python.h>
18
19#include <unordered_map>
20
25typedef struct {
26 PyDictObject dict;
27 JS::RootedObject jsObject;
29
35public:
41 static void JSObjectProxy_dealloc(JSObjectProxy *self);
42
51 static PyObject *JSObjectProxy_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
52
61 static int JSObjectProxy_init(JSObjectProxy *self, PyObject *args, PyObject *kwds);
62
69 static Py_ssize_t JSObjectProxy_length(JSObjectProxy *self);
70
78 static PyObject *JSObjectProxy_get(JSObjectProxy *self, PyObject *key);
79
87 static int JSObjectProxy_contains(JSObjectProxy *self, PyObject *key);
88
97 static int JSObjectProxy_assign(JSObjectProxy *self, PyObject *key, PyObject *value);
98
107 static PyObject *JSObjectProxy_richcompare(JSObjectProxy *self, PyObject *other, int op);
108
117 // private
118 static bool JSObjectProxy_richcompare_helper(JSObjectProxy *self, PyObject *other, std::unordered_map<PyObject *, PyObject *> &visited);
119
126 static PyObject *JSObjectProxy_iter(JSObjectProxy *self);
127
134 static PyObject *JSObjectProxy_repr(JSObjectProxy *self);
135
143 static PyObject *JSObjectProxy_or(JSObjectProxy *self, PyObject *other);
144
152 static PyObject *JSObjectProxy_ior(JSObjectProxy *self, PyObject *other);
153
162 static PyObject *JSObjectProxy_get_method(JSObjectProxy *self, PyObject *const *args, Py_ssize_t nargs);
163
172 static PyObject *JSObjectProxy_setdefault_method(JSObjectProxy *self, PyObject *const *args, Py_ssize_t nargs);
173
182 static PyObject *JSObjectProxy_pop_method(JSObjectProxy *self, PyObject *const *args, Py_ssize_t nargs);
183
190 static PyObject *JSObjectProxy_clear_method(JSObjectProxy *self);
191
198 static PyObject *JSObjectProxy_copy_method(JSObjectProxy *self);
199
208 static PyObject *JSObjectProxy_update_method(JSObjectProxy *self, PyObject *args, PyObject *kwds);
209
216 static PyObject *JSObjectProxy_keys_method(JSObjectProxy *self);
217
224 static PyObject *JSObjectProxy_values_method(JSObjectProxy *self);
225
232 static PyObject *JSObjectProxy_items_method(JSObjectProxy *self);
233};
234
235
236// docs for methods, copied from cpython
237PyDoc_STRVAR(getitem__doc__,
238 "__getitem__($self, key, /)\n--\n\nReturn self[key].");
239
240PyDoc_STRVAR(dict_get__doc__,
241 "get($self, key, default=None, /)\n"
242 "--\n"
243 "\n"
244 "Return the value for key if key is in the dictionary, else default.");
245
246PyDoc_STRVAR(dict_setdefault__doc__,
247 "setdefault($self, key, default=None, /)\n"
248 "--\n"
249 "\n"
250 "Insert key with a value of default if key is not in the dictionary.\n"
251 "\n"
252 "Return the value for key if key is in the dictionary, else default.");
253
254PyDoc_STRVAR(dict_pop__doc__,
255 "pop($self, key, default=<unrepresentable>, /)\n"
256 "--\n"
257 "\n"
258 "D.pop(k[,d]) -> v, remove specified key and return the corresponding value.\n"
259 "\n"
260 "If the key is not found, return the default if given; otherwise,\n"
261 "raise a KeyError.");
262
263PyDoc_STRVAR(clear__doc__,
264 "D.clear() -> None. Remove all items from D.");
265
266PyDoc_STRVAR(copy__doc__,
267 "D.copy() -> a shallow copy of D");
268
269PyDoc_STRVAR(keys__doc__,
270 "D.keys() -> a set-like object providing a view on D's keys");
271PyDoc_STRVAR(items__doc__,
272 "D.items() -> a set-like object providing a view on D's items");
273PyDoc_STRVAR(values__doc__,
274 "D.values() -> an object providing a view on D's values");
275
276PyDoc_STRVAR(update__doc__,
277 "D.update([E, ]**F) -> None. Update D from dict/iterable E and F.\n\
278If E is present and has a .keys() method, then does: for k in E: D[k] = E[k]\n\
279If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v\n\
280In either case, this is followed by: for k in F: D[k] = F[k]");
281
282PyDoc_STRVAR(dict_keys__doc__,
283 "D.keys() -> a set-like object providing a view on D's keys");
284PyDoc_STRVAR(dict_items__doc__,
285 "D.items() -> a set-like object providing a view on D's items");
286PyDoc_STRVAR(dict_values__doc__,
287 "D.values() -> an object providing a view on D's values");
288
293static PyMappingMethods JSObjectProxy_mapping_methods = {
295 .mp_subscript = (binaryfunc)JSObjectProxyMethodDefinitions::JSObjectProxy_get,
296 .mp_ass_subscript = (objobjargproc)JSObjectProxyMethodDefinitions::JSObjectProxy_assign
297};
298
303static PySequenceMethods JSObjectProxy_sequence_methods = {
305};
306
307static PyNumberMethods JSObjectProxy_number_methods = {
309 .nb_inplace_or = (binaryfunc)JSObjectProxyMethodDefinitions::JSObjectProxy_ior
310};
311
316static PyMethodDef JSObjectProxy_methods[] = {
317 {"__getitem__", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_get, METH_O | METH_COEXIST, getitem__doc__},
318 {"get", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_get_method, METH_FASTCALL, dict_get__doc__},
319 {"setdefault", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_setdefault_method, METH_FASTCALL, dict_setdefault__doc__},
320 {"pop", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_pop_method, METH_FASTCALL, dict_pop__doc__},
321 // {"popitem", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_popitem_method, METH_NOARGS, ""}, TODO not popular and quite a bit strange
322 {"clear", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_clear_method, METH_NOARGS, clear__doc__},
323 {"copy", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_copy_method, METH_NOARGS, copy__doc__},
324 {"update", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_update_method, METH_VARARGS | METH_KEYWORDS, update__doc__},
325 {"keys", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_keys_method, METH_NOARGS, dict_keys__doc__},
326 {"items", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_items_method, METH_NOARGS, dict_items__doc__},
327 {"values", (PyCFunction)JSObjectProxyMethodDefinitions::JSObjectProxy_values_method, METH_NOARGS, dict_values__doc__},
328 {NULL, NULL} /* sentinel */
329};
330
334extern PyTypeObject JSObjectProxyType;
335
336#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:76
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:243
static PyObject * JSObjectProxy_items_method(JSObjectProxy *self)
items method
Definition JSObjectProxy.cc:665
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:106
static PyObject * JSObjectProxy_update_method(JSObjectProxy *self, PyObject *args, PyObject *kwds)
update method update the dict with another dict or iterable
Definition JSObjectProxy.cc:630
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:223
static PyObject * JSObjectProxy_clear_method(JSObjectProxy *self)
clear method
Definition JSObjectProxy.cc:590
static void JSObjectProxy_dealloc(JSObjectProxy *self)
Deallocation method (.tp_dealloc), removes the reference to the underlying JSObject before freeing th...
Definition JSObjectProxy.cc:49
static PyObject * JSObjectProxy_ior(JSObjectProxy *self, PyObject *other)
Set union operation, in place.
Definition JSObjectProxy.cc:467
static PyObject * JSObjectProxy_or(JSObjectProxy *self, PyObject *other)
Set union operation.
Definition JSObjectProxy.cc:425
static PyObject * JSObjectProxy_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
New method (.tp_new), creates a new instance of the JSObjectProxy type, exposed as the new() method i...
Definition JSObjectProxy.cc:57
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:137
static PyObject * JSObjectProxy_get(JSObjectProxy *self, PyObject *key)
Getter method (.mp_subscript), returns a value from the JSObjectProxy given a key,...
Definition JSObjectProxy.cc:81
static PyObject * JSObjectProxy_get_method(JSObjectProxy *self, PyObject *const *args, Py_ssize_t nargs)
get method
Definition JSObjectProxy.cc:500
static PyObject * JSObjectProxy_pop_method(JSObjectProxy *self, PyObject *const *args, Py_ssize_t nargs)
pop method
Definition JSObjectProxy.cc:551
static int JSObjectProxy_init(JSObjectProxy *self, PyObject *args, PyObject *kwds)
Initialization method (.tp_init), initializes a newly created instance of JSObjectProxy....
Definition JSObjectProxy.cc:62
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:118
static PyObject * JSObjectProxy_values_method(JSObjectProxy *self)
values method
Definition JSObjectProxy.cc:661
static bool JSObjectProxy_richcompare_helper(JSObjectProxy *self, PyObject *other, std::unordered_map< PyObject *, PyObject * > &visited)
Helper function for JSObjectProxy_richcompare.
Definition JSObjectProxy.cc:157
static PyObject * JSObjectProxy_keys_method(JSObjectProxy *self)
keys method
Definition JSObjectProxy.cc:657
static PyObject * JSObjectProxy_setdefault_method(JSObjectProxy *self, PyObject *const *args, Py_ssize_t nargs)
setdefault method
Definition JSObjectProxy.cc:524
static PyObject * JSObjectProxy_copy_method(JSObjectProxy *self)
copy method
Definition JSObjectProxy.cc:607
The typedef for the backing store that will be used by JSObjectProxy objects. All it contains is a po...
Definition JSObjectProxy.hh:25
JS::RootedObject jsObject
Definition JSObjectProxy.hh:27
PyDictObject dict
Definition JSObjectProxy.hh:26