-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathPyIterableProxyHandler.cc
More file actions
301 lines (235 loc) · 8.78 KB
/
PyIterableProxyHandler.cc
File metadata and controls
301 lines (235 loc) · 8.78 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
/**
* @file PyIterableProxyHandler.cc
* @author Philippe Laporte (philippe@distributive.network)
* @brief Struct for creating JS proxy objects for Iterables
* @date 2024-04-08
*
* @copyright Copyright (c) 2024 Distributive Corp.
*
*/
#include "include/PyIterableProxyHandler.hh"
#include "include/jsTypeFactory.hh"
#include <jsapi.h>
#include <Python.h>
const char PyIterableProxyHandler::family = 0;
static bool iter_next(JSContext *cx, JS::CallArgs args, PyObject *it) {
JS::RootedObject result(cx, JS_NewPlainObject(cx));
PyObject *(*iternext)(PyObject *) = *Py_TYPE(it)->tp_iternext;
PyObject *item = iternext(it);
if (item == NULL) {
if (PyErr_Occurred()) {
if (PyErr_ExceptionMatches(PyExc_StopIteration) ||
PyErr_ExceptionMatches(PyExc_SystemError)) { // TODO this handles a result like SystemError: Objects/dictobject.c:1778: bad argument to internal function. Why are we getting that?
PyErr_Clear();
}
else {
return false;
}
}
JS::RootedValue done(cx, JS::BooleanValue(true));
if (!JS_SetProperty(cx, result, "done", done)) return false;
args.rval().setObject(*result);
return result;
}
JS::RootedValue done(cx, JS::BooleanValue(false));
if (!JS_SetProperty(cx, result, "done", done)) return false;
JS::RootedValue value(cx, jsTypeFactory(cx, item));
if (!JS_SetProperty(cx, result, "value", value)) return false;
args.rval().setObject(*result);
return true;
}
static bool iterable_next(JSContext *cx, unsigned argc, JS::Value *vp) {
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::RootedObject thisObj(cx);
if (!args.computeThis(cx, &thisObj)) return false;
PyObject *it = JS::GetMaybePtrFromReservedSlot<PyObject>(thisObj, PyObjectSlot);
return iter_next(cx, args, it);
}
static bool toPrimitive(JSContext *cx, unsigned argc, JS::Value *vp) {
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::RootedObject proxy(cx, JS::ToObject(cx, args.thisv()));
if (!proxy) {
return false;
}
PyObject *self = JS::GetMaybePtrFromReservedSlot<PyObject>(proxy, PyObjectSlot);
_PyUnicodeWriter writer;
_PyUnicodeWriter_Init(&writer);
PyObject *s = PyObject_Repr(self);
if (s == nullptr) {
args.rval().setString(JS_NewStringCopyZ(cx, "<cannot repr type>"));
return true;
}
int res = _PyUnicodeWriter_WriteStr(&writer, s);
Py_DECREF(s);
if (res < 0) {
args.rval().setString(JS_NewStringCopyZ(cx, "<cannot repr type>"));
return true;
}
PyObject *repr = _PyUnicodeWriter_Finish(&writer);
args.rval().set(jsTypeFactory(cx, repr));
return true;
}
static bool iterable_valueOf(JSContext *cx, unsigned argc, JS::Value *vp) {
return toPrimitive(cx, argc, vp);
}
static JSMethodDef iterable_methods[] = {
{"next", iterable_next, 0},
{"valueOf", iterable_valueOf, 0},
{NULL, NULL, 0}
};
// IterableIterator
enum {
IterableIteratorSlotIterableObject,
IterableIteratorSlotCount
};
static JSClass iterableIteratorClass = {"IterableIterator", JSCLASS_HAS_RESERVED_SLOTS(IterableIteratorSlotCount)};
static bool iterator_next(JSContext *cx, unsigned argc, JS::Value *vp) {
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::RootedObject thisObj(cx);
if (!args.computeThis(cx, &thisObj)) return false;
PyObject *it = JS::GetMaybePtrFromReservedSlot<PyObject>(thisObj, IterableIteratorSlotIterableObject);
return iter_next(cx, args, it);
}
static JSFunctionSpec iterable_iterator_methods[] = {
JS_FN("next", iterator_next, 0, JSPROP_ENUMERATE),
JS_FS_END
};
static bool IterableIteratorConstructor(JSContext *cx, unsigned argc, JS::Value *vp) {
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
if (!args.isConstructing()) {
JS_ReportErrorASCII(cx, "You must call this constructor with 'new'");
return false;
}
JS::RootedObject thisObj(cx, JS_NewObjectForConstructor(cx, &iterableIteratorClass, args));
if (!thisObj) {
return false;
}
args.rval().setObject(*thisObj);
return true;
}
static bool DefineIterableIterator(JSContext *cx, JS::HandleObject global) {
JS::RootedObject iteratorPrototype(cx);
if (!JS_GetClassPrototype(cx, JSProto_Iterator, &iteratorPrototype)) {
return false;
}
JS::RootedObject protoObj(cx,
JS_InitClass(cx, global,
nullptr, iteratorPrototype,
"IterableIterator",
IterableIteratorConstructor, 0,
nullptr, iterable_iterator_methods,
nullptr, nullptr)
);
return protoObj; // != nullptr
}
static bool iterable_values(JSContext *cx, unsigned argc, JS::Value *vp) {
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
JS::RootedObject proxy(cx, JS::ToObject(cx, args.thisv()));
if (!proxy) {
return false;
}
PyObject *self = JS::GetMaybePtrFromReservedSlot<PyObject>(proxy, PyObjectSlot);
JS::RootedObject global(cx, JS::GetNonCCWObjectGlobal(proxy));
JS::RootedValue constructor_val(cx);
if (!JS_GetProperty(cx, global, "IterableIterator", &constructor_val)) return false;
if (!constructor_val.isObject()) {
if (!DefineIterableIterator(cx, global)) {
return false;
}
if (!JS_GetProperty(cx, global, "IterableIterator", &constructor_val)) return false;
if (!constructor_val.isObject()) {
JS_ReportErrorASCII(cx, "IterableIterator is not a constructor");
return false;
}
}
JS::RootedObject constructor(cx, &constructor_val.toObject());
JS::RootedObject obj(cx);
if (!JS::Construct(cx, constructor_val, JS::HandleValueArray::empty(), &obj)) return false;
if (!obj) return false;
JS::SetReservedSlot(obj, IterableIteratorSlotIterableObject, JS::PrivateValue((void *)self));
args.rval().setObject(*obj);
return true;
}
bool PyIterableProxyHandler::getOwnPropertyDescriptor(
JSContext *cx, JS::HandleObject proxy, JS::HandleId id,
JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc
) const {
// see if we're calling a function
if (id.isString()) {
for (size_t index = 0;; index++) {
bool isThatFunction;
const char *methodName = iterable_methods[index].name;
if (methodName == NULL) {
break;
}
else if (JS_StringEqualsAscii(cx, id.toString(), methodName, &isThatFunction) && isThatFunction) {
JSFunction *newFunction = JS_NewFunction(cx, iterable_methods[index].call, iterable_methods[index].nargs, 0, NULL);
if (!newFunction) return false;
JS::RootedObject funObj(cx, JS_GetFunctionObject(newFunction));
desc.set(mozilla::Some(
JS::PropertyDescriptor::Data(
JS::ObjectValue(*funObj),
{JS::PropertyAttribute::Enumerable}
)
));
return true;
}
}
}
// "constructor" property
bool isConstructorProperty;
if (id.isString() && JS_StringEqualsLiteral(cx, id.toString(), "constructor", &isConstructorProperty) && isConstructorProperty) {
JS::RootedObject rootedObjectPrototype(cx);
if (!JS_GetClassPrototype(cx, JSProto_Object, &rootedObjectPrototype)) {
return false;
}
JS::RootedValue Object_Prototype_Constructor(cx);
if (!JS_GetProperty(cx, rootedObjectPrototype, "constructor", &Object_Prototype_Constructor)) {
return false;
}
JS::RootedObject rootedObjectPrototypeConstructor(cx, Object_Prototype_Constructor.toObjectOrNull());
desc.set(mozilla::Some(
JS::PropertyDescriptor::Data(
JS::ObjectValue(*rootedObjectPrototypeConstructor),
{JS::PropertyAttribute::Enumerable}
)
));
return true;
}
// symbol property
if (id.isSymbol()) {
JS::RootedSymbol rootedSymbol(cx, id.toSymbol());
JS::SymbolCode symbolCode = JS::GetSymbolCode(rootedSymbol);
if (symbolCode == JS::SymbolCode::iterator) {
JSFunction *newFunction = JS_NewFunction(cx, iterable_values, 0, 0, NULL);
if (!newFunction) return false;
JS::RootedObject funObj(cx, JS_GetFunctionObject(newFunction));
desc.set(mozilla::Some(
JS::PropertyDescriptor::Data(
JS::ObjectValue(*funObj),
{JS::PropertyAttribute::Enumerable}
)
));
return true;
}
else if (symbolCode == JS::SymbolCode::toPrimitive) {
JSFunction *newFunction = JS_NewFunction(cx, toPrimitive, 0, 0, nullptr);
if (!newFunction) return false;
JS::RootedObject funObj(cx, JS_GetFunctionObject(newFunction));
desc.set(mozilla::Some(
JS::PropertyDescriptor::Data(
JS::ObjectValue(*funObj),
{JS::PropertyAttribute::Enumerable}
)
));
return true;
}
}
PyObject *attrName = idToKey(cx, id);
PyObject *self = JS::GetMaybePtrFromReservedSlot<PyObject>(proxy, PyObjectSlot);
PyObject *item = PyObject_GetAttr(self, attrName);
if (!item && PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear(); // clear error, we will be returning undefined in this case
}
return handleGetOwnPropertyDescriptor(cx, id, desc, item);
}