Skip to content

Commit d186f01

Browse files
Merge pull request #319 from Distributive-Network/philippe/iterator-simplified
Simplification of redundant patterns in iterable proxy
2 parents c9bec0f + 3b788c3 commit d186f01

File tree

2 files changed

+13
-50
lines changed

2 files changed

+13
-50
lines changed

include/PyIterableProxyHandler.hh

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,6 @@ public:
2424
PyIterableProxyHandler() : PyObjectProxyHandler(&family) {};
2525
static const char family;
2626

27-
/**
28-
* @brief Helper function to return next item in iteration
29-
*
30-
* @param cx - pointer to the JSContext
31-
* @param argc - unused
32-
* @param vp - unused
33-
* @return true - this function returns true for success and false for failure
34-
*/
35-
static bool iterable_next(JSContext *cx, unsigned argc, JS::Value *vp);
36-
3727
bool getOwnPropertyDescriptor(
3828
JSContext *cx, JS::HandleObject proxy, JS::HandleId id,
3929
JS::MutableHandle<mozilla::Maybe<JS::PropertyDescriptor>> desc

src/PyIterableProxyHandler.cc

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,8 @@
2121

2222
const char PyIterableProxyHandler::family = 0;
2323

24-
// TODO merge shared _next code
25-
26-
bool PyIterableProxyHandler::iterable_next(JSContext *cx, unsigned argc, JS::Value *vp) {
27-
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
28-
JS::RootedObject thisObj(cx);
29-
if (!args.computeThis(cx, &thisObj)) return false;
30-
31-
PyObject *it = JS::GetMaybePtrFromReservedSlot<PyObject>(thisObj, PyObjectSlot);
3224

25+
static bool iter_next(JSContext *cx, JS::CallArgs args, PyObject *it) {
3326
JS::RootedObject result(cx, JS_NewPlainObject(cx));
3427

3528
PyObject *(*iternext)(PyObject *) = *Py_TYPE(it)->tp_iternext;
@@ -63,8 +56,18 @@ bool PyIterableProxyHandler::iterable_next(JSContext *cx, unsigned argc, JS::Val
6356
return true;
6457
}
6558

59+
static bool iterable_next(JSContext *cx, unsigned argc, JS::Value *vp) {
60+
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
61+
JS::RootedObject thisObj(cx);
62+
if (!args.computeThis(cx, &thisObj)) return false;
63+
64+
PyObject *it = JS::GetMaybePtrFromReservedSlot<PyObject>(thisObj, PyObjectSlot);
65+
66+
return iter_next(cx, args, it);
67+
}
68+
6669
JSMethodDef PyIterableProxyHandler::iterable_methods[] = {
67-
{"next", PyIterableProxyHandler::iterable_next, 0},
70+
{"next", iterable_next, 0},
6871
{NULL, NULL, 0}
6972
};
7073

@@ -85,37 +88,7 @@ static bool iterator_next(JSContext *cx, unsigned argc, JS::Value *vp) {
8588

8689
PyObject *it = JS::GetMaybePtrFromReservedSlot<PyObject>(thisObj, IterableIteratorSlotIterableObject);
8790

88-
JS::RootedObject result(cx, JS_NewPlainObject(cx));
89-
90-
PyObject *(*iternext)(PyObject *) = *Py_TYPE(it)->tp_iternext;
91-
92-
PyObject *item = iternext(it);
93-
94-
if (item == NULL) {
95-
if (PyErr_Occurred()) {
96-
if (PyErr_ExceptionMatches(PyExc_StopIteration) ||
97-
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?
98-
PyErr_Clear();
99-
}
100-
else {
101-
return false;
102-
}
103-
}
104-
105-
JS::RootedValue done(cx, JS::BooleanValue(true));
106-
if (!JS_SetProperty(cx, result, "done", done)) return false;
107-
args.rval().setObject(*result);
108-
return result;
109-
}
110-
111-
JS::RootedValue done(cx, JS::BooleanValue(false));
112-
if (!JS_SetProperty(cx, result, "done", done)) return false;
113-
114-
JS::RootedValue value(cx, jsTypeFactory(cx, item));
115-
if (!JS_SetProperty(cx, result, "value", value)) return false;
116-
117-
args.rval().setObject(*result);
118-
return true;
91+
return iter_next(cx, args, it);
11992
}
12093

12194
static JSFunctionSpec iterable_iterator_methods[] = {

0 commit comments

Comments
 (0)