-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathJSObjectValuesProxy.cc
More file actions
152 lines (131 loc) · 4.5 KB
/
JSObjectValuesProxy.cc
File metadata and controls
152 lines (131 loc) · 4.5 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
/**
* @file JSObjectValuesProxy.cc
* @author Philippe Laporte (philippe@distributive.network)
* @brief JSObjectValuesProxy is a custom C-implemented python type that derives from dict values
* @date 2024-01-19
*
* @copyright Copyright (c) 2024 Distributive Corp.
*
*/
#include "include/JSObjectValuesProxy.hh"
#include "include/JSObjectIterProxy.hh"
#include "include/JSObjectProxy.hh"
#include "include/JSArrayProxy.hh"
#include "include/modules/pythonmonkey/pythonmonkey.hh"
#include "include/jsTypeFactory.hh"
#include "include/PyDictProxyHandler.hh"
#include <jsapi.h>
#include <jsfriendapi.h>
#include <Python.h>
#include "include/pyshim.hh"
void JSObjectValuesProxyMethodDefinitions::JSObjectValuesProxy_dealloc(JSObjectValuesProxy *self)
{
PyObject_GC_UnTrack(self);
Py_XDECREF(self->dv.dv_dict);
PyObject_GC_Del(self);
}
Py_ssize_t JSObjectValuesProxyMethodDefinitions::JSObjectValuesProxy_length(JSObjectValuesProxy *self)
{
if (self->dv.dv_dict == NULL) {
return 0;
}
return JSObjectProxyMethodDefinitions::JSObjectProxy_length((JSObjectProxy *)self->dv.dv_dict);
}
int JSObjectValuesProxyMethodDefinitions::JSObjectValuesProxy_contains(JSObjectValuesProxy *self, PyObject *key)
{
if (self->dv.dv_dict == NULL) {
return 0;
}
return JSObjectProxyMethodDefinitions::JSObjectProxy_contains((JSObjectProxy *)self->dv.dv_dict, key);
}
int JSObjectValuesProxyMethodDefinitions::JSObjectValuesProxy_traverse(JSObjectValuesProxy *self, visitproc visit, void *arg) {
Py_VISIT(self->dv.dv_dict);
return 0;
}
int JSObjectValuesProxyMethodDefinitions::JSObjectValuesProxy_clear(JSObjectValuesProxy *self) {
Py_CLEAR(self->dv.dv_dict);
return 0;
}
// private
static int all_contained_in(PyObject *self, PyObject *other) {
PyObject *iter = PyObject_GetIter(self);
int ok = 1;
if (iter == NULL) {
return -1;
}
for (;; ) {
PyObject *next = PyIter_Next(iter);
if (next == NULL) {
if (PyErr_Occurred())
ok = -1;
break;
}
if (PyObject_TypeCheck(other, &JSObjectValuesProxyType)) {
JSObjectValuesProxyMethodDefinitions::JSObjectValuesProxy_contains((JSObjectValuesProxy *)other, next);
}
else {
ok = PySequence_Contains(other, next);
}
Py_DECREF(next);
if (ok <= 0)
break;
}
Py_DECREF(iter);
return ok;
}
PyObject *JSObjectValuesProxyMethodDefinitions::JSObjectValuesProxy_iter(JSObjectValuesProxy *self) {
JSObjectIterProxy *iterator = PyObject_GC_New(JSObjectIterProxy, &JSObjectIterProxyType);
if (iterator == NULL) {
return NULL;
}
iterator->it.reversed = false;
iterator->it.it_index = 0;
iterator->it.kind = KIND_VALUES;
Py_INCREF(self->dv.dv_dict);
iterator->it.di_dict = self->dv.dv_dict;
iterator->it.props = new JS::PersistentRootedIdVector(GLOBAL_CX);
// Get **enumerable** own properties
if (!js::GetPropertyKeys(GLOBAL_CX, *(((JSObjectProxy *)(self->dv.dv_dict))->jsObject), JSITER_OWNONLY, iterator->it.props)) {
return NULL;
}
PyObject_GC_Track(iterator);
return (PyObject *)iterator;
}
PyObject *JSObjectValuesProxyMethodDefinitions::JSObjectValuesProxy_iter_reverse(JSObjectValuesProxy *self) {
JSObjectIterProxy *iterator = PyObject_GC_New(JSObjectIterProxy, &JSObjectIterProxyType);
if (iterator == NULL) {
return NULL;
}
iterator->it.reversed = true;
iterator->it.it_index = JSObjectValuesProxyMethodDefinitions::JSObjectValuesProxy_length(self) - 1;
iterator->it.kind = KIND_VALUES;
Py_INCREF(self->dv.dv_dict);
iterator->it.di_dict = self->dv.dv_dict;
iterator->it.props = new JS::PersistentRootedIdVector(GLOBAL_CX);
// Get **enumerable** own properties
if (!js::GetPropertyKeys(GLOBAL_CX, *(((JSObjectProxy *)(self->dv.dv_dict))->jsObject), JSITER_OWNONLY, iterator->it.props)) {
return NULL;
}
PyObject_GC_Track(iterator);
return (PyObject *)iterator;
}
PyObject *JSObjectValuesProxyMethodDefinitions::JSObjectValuesProxy_repr(JSObjectValuesProxy *self) {
PyObject *seq;
PyObject *result = NULL;
Py_ssize_t rc = Py_ReprEnter((PyObject *)self);
if (rc != 0) {
return rc > 0 ? PyUnicode_FromString("...") : NULL;
}
seq = PySequence_List((PyObject *)self);
if (seq == NULL) {
goto Done;
}
result = PyUnicode_FromFormat("%s(%R)", PyDictValues_Type.tp_name, seq);
Py_DECREF(seq);
Done:
Py_ReprLeave((PyObject *)self);
return result;
}
PyObject *JSObjectValuesProxyMethodDefinitions::JSObjectValuesProxy_mapping(PyObject *self, void *Py_UNUSED(ignored)) {
return PyDictProxy_New((PyObject *)((_PyDictViewObject *)self)->dv_dict);
}